Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][Lowering] supports lowering of the zero initialized arrays #372

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,20 +1046,26 @@ class CIRConstantLowering
// the store instruction, instead of being stored as global variables and
// then memcopyied into the stack (as done in Clang).
else if (auto arrTy = op.getType().dyn_cast<mlir::cir::ArrayType>()) {
// Fetch operation constant array initializer.
auto constArr = op.getValue().dyn_cast<mlir::cir::ConstArrayAttr>();
if (!constArr)
if (auto zero = attr.dyn_cast<mlir::cir::ZeroAttr>()) {
auto val = lowerCirAttrAsValue(op, zero, rewriter, typeConverter);
rewriter.replaceAllUsesWith(op, val);
rewriter.eraseOp(op);
return mlir::success();
} else
if (auto constArr = attr.dyn_cast<mlir::cir::ConstArrayAttr>()) {
// Lower constant array initializer.
auto denseAttr = lowerConstArrayAttr(constArr, typeConverter);
if (!denseAttr.has_value()) {
op.emitError()
<< "unsupported lowering for #cir.const_array with element type "
<< arrTy.getEltType();
return mlir::failure();
}
attr = denseAttr.value();
} else {
op.dump();
return op.emitError() << "array does not have a constant initializer";

// Lower constant array initializer.
auto denseAttr = lowerConstArrayAttr(constArr, typeConverter);
if (!denseAttr.has_value()) {
op.emitError()
<< "unsupported lowering for #cir.const_array with element type "
<< arrTy.getEltType();
return mlir::failure();
}
attr = denseAttr.value();
} else if (const auto structAttr =
op.getValue().dyn_cast<mlir::cir::ConstStructAttr>()) {
// TODO(cir): this diverges from traditional lowering. Normally the
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CIR/Lowering/const.cir
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
!s32i = !cir.int<s, 32>
!s8i = !cir.int<s, 8>
module {
cir.func @testArrZeroInit() {
%0 = cir.alloca !cir.array<!s32i x 2>, cir.ptr <!cir.array<!s32i x 2>>, ["a"] {alignment = 4 : i64}
// CHECK: %0 = llvm.mlir.constant(1 : index) : i64
// CHECK: %1 = llvm.alloca %0 x !llvm.array<2 x i32> {alignment = 4 : i64} : (i64) -> !llvm.ptr
%1 = cir.const(#cir.zero : !cir.array<!s32i x 2>) : !cir.array<!s32i x 2>
// CHECK: %2 = cir.llvmir.zeroinit : !llvm.array<2 x i32>
cir.store %1, %0 : !cir.array<!s32i x 2>, cir.ptr <!cir.array<!s32i x 2>>
// CHECK: llvm.store %2, %1 : !llvm.array<2 x i32>, !llvm.ptr
cir.return
}

cir.func @testConstArrInit() {
%0 = cir.const(#cir.const_array<"string\00" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7>) : !cir.array<!s8i x 7>
// CHECK: llvm.mlir.constant(dense<[115, 116, 114, 105, 110, 103, 0]> : tensor<7xi8>) : !llvm.array<7 x i8>
Expand Down