Skip to content

Commit 423a611

Browse files
YazZz1klanza
authored andcommitted
[CIR][Lowering] Support conversion of cir.zero to dense consts (#413)
Compiling the given c-code ``` void foo() { int i [2][1] = { { 1 }, { 0 } }; long int li[2][1] = { { 1 }, { 0 } }; float fl[2][1] = { { 1 }, { 0 } }; double d [2][1] = { { 1 }, { 0 } }; } ``` leads to compilation error ``` unknown element in ConstArrayAttr UNREACHABLE executed at /home/huawei/cir/repo/van/llvm-project/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp:951! ``` PR implements conversion the cir.zero attr to dense constant and fixed this error.
1 parent 9cb2c56 commit 423a611

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

Diff for: clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,39 @@ convertStringAttrToDenseElementsAttr(mlir::cir::ConstArrayAttr attr,
969969
llvm::ArrayRef(values));
970970
}
971971

972+
template <typename StorageTy> StorageTy getZeroInitFromType(mlir::Type Ty);
973+
974+
template <> mlir::APInt getZeroInitFromType(mlir::Type Ty) {
975+
assert(Ty.isa<mlir::cir::IntType>() && "expected int type");
976+
auto IntTy = Ty.cast<mlir::cir::IntType>();
977+
return mlir::APInt::getZero(IntTy.getWidth());
978+
}
979+
980+
template <> mlir::APFloat getZeroInitFromType(mlir::Type Ty) {
981+
assert((Ty.isF32() || Ty.isF64()) && "only float and double supported");
982+
if (Ty.isF32())
983+
return mlir::APFloat(0.f);
984+
if (Ty.isF64())
985+
return mlir::APFloat(0.0);
986+
llvm_unreachable("NYI");
987+
}
988+
989+
// return the nested type and quiantity of elements for cir.array type.
990+
// e.g: for !cir.array<!cir.array<!s32i x 3> x 1>
991+
// it returns !s32i as return value and stores 3 to elemQuantity.
992+
mlir::Type getNestedTypeAndElemQuantity(mlir::Type Ty, unsigned &elemQuantity) {
993+
assert(Ty.isa<mlir::cir::ArrayType>() && "expected ArrayType");
994+
995+
elemQuantity = 1;
996+
mlir::Type nestTy = Ty;
997+
while (auto ArrTy = nestTy.dyn_cast<mlir::cir::ArrayType>()) {
998+
nestTy = ArrTy.getEltType();
999+
elemQuantity *= ArrTy.getSize();
1000+
}
1001+
1002+
return nestTy;
1003+
}
1004+
9721005
template <typename AttrTy, typename StorageTy>
9731006
void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr,
9741007
llvm::SmallVectorImpl<StorageTy> &values) {
@@ -979,6 +1012,12 @@ void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr,
9791012
} else if (auto subArrayAttr =
9801013
eltAttr.dyn_cast<mlir::cir::ConstArrayAttr>()) {
9811014
convertToDenseElementsAttrImpl<AttrTy>(subArrayAttr, values);
1015+
} else if (auto zeroAttr = eltAttr.dyn_cast<mlir::cir::ZeroAttr>()) {
1016+
unsigned numStoredZeros = 0;
1017+
auto nestTy =
1018+
getNestedTypeAndElemQuantity(zeroAttr.getType(), numStoredZeros);
1019+
values.insert(values.end(), numStoredZeros,
1020+
getZeroInitFromType<StorageTy>(nestTy));
9821021
} else {
9831022
llvm_unreachable("unknown element in ConstArrayAttr");
9841023
}

Diff for: clang/test/CIR/Lowering/const.cir

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// RUN: cir-opt %s -cir-to-llvm -o %t.mlir
22
// RUN: FileCheck --input-file=%t.mlir %s
33

4-
!s32i = !cir.int<s, 32>
54
!s8i = !cir.int<s, 8>
5+
!s32i = !cir.int<s, 32>
6+
!s64i = !cir.int<s, 64>
67
!ty_22anon2E122 = !cir.struct<struct "anon.1" {!cir.int<s, 32>, !cir.int<s, 32>} #cir.record.decl.ast>
7-
88
module {
99
cir.func @testConstArrInit() {
1010
%0 = cir.const(#cir.const_array<"string\00" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7>) : !cir.array<!s8i x 7>
@@ -18,6 +18,23 @@ module {
1818
cir.return
1919
}
2020

21+
cir.func @testConvertConstArrayToDenseConst() {
22+
%0 = cir.const(#cir.const_array<[#cir.const_array<[#cir.int<1> : !s32i]> : !cir.array<!s32i x 1>, #cir.zero : !cir.array<!s32i x 1>]> : !cir.array<!cir.array<!s32i x 1> x 2>) : !cir.array<!cir.array<!s32i x 1> x 2>
23+
%1 = cir.const(#cir.const_array<[#cir.const_array<[#cir.int<1> : !s64i]> : !cir.array<!s64i x 1>, #cir.zero : !cir.array<!s64i x 1>]> : !cir.array<!cir.array<!s64i x 1> x 2>) : !cir.array<!cir.array<!s64i x 1> x 2>
24+
%2 = cir.const(#cir.const_array<[#cir.const_array<[1.000000e+00 : f32]> : !cir.array<f32 x 1>, #cir.zero : !cir.array<f32 x 1>]> : !cir.array<!cir.array<f32 x 1> x 2>) : !cir.array<!cir.array<f32 x 1> x 2>
25+
%3 = cir.const(#cir.const_array<[#cir.const_array<[1.000000e+00]> : !cir.array<f64 x 1>, #cir.zero : !cir.array<f64 x 1>]> : !cir.array<!cir.array<f64 x 1> x 2>) : !cir.array<!cir.array<f64 x 1> x 2>
26+
%4 = cir.const(#cir.const_array<[#cir.const_array<[#cir.const_array<[#cir.int<1> : !s32i, #cir.int<1> : !s32i, #cir.int<1> : !s32i]> : !cir.array<!s32i x 3>]> : !cir.array<!cir.array<!s32i x 3> x 1>, #cir.zero : !cir.array<!cir.array<!s32i x 3> x 1>]> : !cir.array<!cir.array<!cir.array<!s32i x 3> x 1> x 2>) : !cir.array<!cir.array<!cir.array<!s32i x 3> x 1> x 2>
27+
28+
cir.return
29+
}
30+
// CHECK: llvm.func @testConvertConstArrayToDenseConst()
31+
// CHECK: {{%.*}} = llvm.mlir.constant(dense<{{\[\[}}1], [0{{\]\]}}> : tensor<2x1xi32>) : !llvm.array<2 x array<1 x i32>>
32+
// CHECK: {{%.*}} = llvm.mlir.constant(dense<{{\[\[}}1], [0{{\]\]}}> : tensor<2x1xi64>) : !llvm.array<2 x array<1 x i64>>
33+
// CHECK: {{%.*}} = llvm.mlir.constant(dense<{{\[\[}}1.000000e+00], [0.000000e+00{{\]\]}}> : tensor<2x1xf32>) : !llvm.array<2 x array<1 x f32>>
34+
// CHECK: {{%.*}} = llvm.mlir.constant(dense<{{\[\[}}1.000000e+00], [0.000000e+00{{\]\]}}> : tensor<2x1xf64>) : !llvm.array<2 x array<1 x f64>>
35+
// CHECK: {{%.*}} = llvm.mlir.constant(dense<{{\[\[\[}}1, 1, 1{{\]\]}}, {{\[\[}}0, 0, 0{{\]\]\]}}> : tensor<2x1x3xi32>) : !llvm.array<2 x array<1 x array<3 x i32>>>
36+
// CHECK: llvm.return
37+
2138
cir.func @testConstArrayOfStructs() {
2239
%0 = cir.alloca !cir.array<!ty_22anon2E122 x 1>, cir.ptr <!cir.array<!ty_22anon2E122 x 1>>, ["a"] {alignment = 4 : i64}
2340
%1 = cir.const(#cir.const_array<[#cir.const_struct<{#cir.int<0> : !s32i, #cir.int<1> : !s32i}> : !ty_22anon2E122]> : !cir.array<!ty_22anon2E122 x 1>) : !cir.array<!ty_22anon2E122 x 1>

0 commit comments

Comments
 (0)