Skip to content

Commit 541da71

Browse files
committed
Add test for LLVM lowering, CIR test and address comments
1 parent 0b00b1b commit 541da71

File tree

8 files changed

+103
-15
lines changed

8 files changed

+103
-15
lines changed

clang/include/clang/CIR/Dialect/IR/CIRTypes.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ def VoidPtr : Type<
405405
//===----------------------------------------------------------------------===//
406406

407407
def CIR_AnyType : AnyTypeOf<[
408-
CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_IntType, CIR_AnyFloat, CIR_PointerType,
409-
CIR_FuncType
408+
CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_IntType, CIR_AnyFloat,
409+
CIR_PointerType, CIR_FuncType
410410
]>;
411411

412412
#endif // MLIR_CIR_DIALECT_CIR_TYPES

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
3535
}
3636

3737
bool isSized(mlir::Type ty) {
38-
if (mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType,
39-
cir::IntType>(ty))
40-
return true;
41-
assert(0 && "Unimplemented size for type");
42-
return false;
38+
return mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType,
39+
cir::IntType>(ty);
4340
}
4441
};
4542

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,12 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
206206
const ConstantArrayType *arrTy = cast<ConstantArrayType>(ty);
207207
mlir::Type elemTy = convertTypeForMem(arrTy->getElementType());
208208

209-
// FIXME: In LLVM, "lower arrays of undefined struct type to arrays of
210-
// i8 just to have a concrete type". Not sure this makes sense in CIR yet.
211-
assert(builder.isSized(elemTy) && "not implemented");
209+
if (!builder.isSized(elemTy)) {
210+
cgm.errorNYI(SourceLocation(), "unimplemented size for type", type);
211+
resultType = cir::ArrayType::get(builder.getContext(), cgm.SInt32Ty, 0);
212+
break;
213+
}
214+
212215
resultType = cir::ArrayType::get(builder.getContext(), elemTy,
213216
arrTy->getSize().getZExtValue());
214217
break;

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
166166
return success();
167167
}
168168

169-
if (mlir::isa<cir::ArrayType>(opType))
170-
return success();
171-
172169
assert(isa<TypedAttr>(attrType) && "What else could we be looking at here?");
173170
return op->emitOpError("global with type ")
174171
<< cast<TypedAttr>(attrType).getType() << " not yet supported";

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
572572

573573
return mlir::LLVM::LLVMPointerType::get(type.getContext(), targetAS);
574574
});
575+
converter.addConversion([&](cir::ArrayType type) -> mlir::Type {
576+
auto ty = convertTypeForMemory(converter, dataLayout, type.getEltType());
577+
return mlir::LLVM::LLVMArrayType::get(ty, type.getSize());
578+
});
575579
converter.addConversion([&](cir::BoolType type) -> mlir::Type {
576580
return mlir::IntegerType::get(type.getContext(), 1,
577581
mlir::IntegerType::Signless);

clang/test/CIR/CodeGen/array.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
int a[10];
44
// CHECK: cir.global external @a : !cir.array<!cir.int<s, 32> x 10>
55

6+
int aa[10][10];
7+
// CHECK: cir.global external @aa : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
8+
69
extern int b[10];
710
// CHECK: cir.global external @b : !cir.array<!cir.int<s, 32> x 10>
811

12+
extern int bb[10][10];
13+
// CHECK: cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
14+
915
void f() {
10-
int c[10];
11-
// CHECK: %[[ARR:.*]] = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["c"]
16+
int l[10];
17+
// CHECK: %[[ARR:.*]] = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["l"]
1218
}
19+
20+
void f2(int p[10]) {}
21+
// CHECK: cir.func @f2(%arg0: !cir.ptr<!cir.int<s, 32>>
22+
23+
void f3(int pp[10][10]) {}
24+
// CHECK: cir.func @f3(%arg0: !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>

clang/test/CIR/IR/array.cir

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: cir-opt %s | FileCheck %s
2+
3+
module {
4+
5+
cir.global external @a : !cir.array<!cir.int<s, 32> x 10>
6+
// CHECK: cir.global external @a : !cir.array<!cir.int<s, 32> x 10>
7+
8+
cir.global external @aa : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
9+
// CHECK: cir.global external @aa : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
10+
11+
cir.global external @b : !cir.array<!cir.int<s, 32> x 10>
12+
// CHECK: cir.global external @b : !cir.array<!cir.int<s, 32> x 10>
13+
14+
cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
15+
// CHECK: cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
16+
17+
cir.func @f() {
18+
%0 = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["l"] {alignment = 4 : i64}
19+
cir.return
20+
}
21+
22+
// CHECK: cir.func @f() {
23+
// CHECK: %0 = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["l"] {alignment = 4 : i64}
24+
// CHECK: cir.return
25+
// CHECK: }
26+
27+
cir.func @f2(%arg0: !cir.ptr<!cir.int<s, 32>>) {
28+
%0 = cir.alloca !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>, ["p", init] {alignment = 8 : i64}
29+
cir.store %arg0, %0 : !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>
30+
cir.return
31+
}
32+
33+
// CHECK: cir.func @f2(%arg0: !cir.ptr<!cir.int<s, 32>>) {
34+
// CHECK: %0 = cir.alloca !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>, ["p", init] {alignment = 8 : i64}
35+
// CHECK: cir.store %arg0, %0 : !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>
36+
// CHECK: cir.return
37+
// CHECK: }
38+
39+
cir.func @f3(%arg0: !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>) {
40+
%0 = cir.alloca !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 10>>>, ["pp", init] {alignment = 8 : i64}
41+
cir.store %arg0, %0 : !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 10>>>
42+
cir.return
43+
}
44+
45+
// CHECK: cir.func @f3(%arg0: !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>) {
46+
// CHECK: %0 = cir.alloca !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 10>>>, ["pp", init] {alignment = 8 : i64}
47+
// CHECK: cir.store %arg0, %0 : !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 10>>>
48+
// CHECK: cir.return
49+
// CHECK: }
50+
51+
}

clang/test/CIR/Lowering/array.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - 2>&1 | FileCheck %s
2+
3+
int a[10];
4+
// CHECK: @a = external dso_local global [10 x i32]
5+
6+
int aa[10][10];
7+
// CHECK: @aa = external dso_local global [10 x [10 x i32]]
8+
9+
extern int b[10];
10+
// CHECK: @b = external dso_local global [10 x i32]
11+
12+
extern int bb[10][10];
13+
// CHECK: @bb = external dso_local global [10 x [10 x i32]]
14+
15+
void f() {
16+
int l[10];
17+
}
18+
// CHECK: alloca [10 x i32], i64 1, align 16
19+
20+
void f2(int p[10]) {}
21+
// CHECK: alloca ptr, i64 1, align 8
22+
23+
void f3(int pp[10][10]) {}
24+
// CHECK: alloca ptr, i64 1, align 8

0 commit comments

Comments
 (0)