Skip to content

Commit fc8dc2f

Browse files
sitio-coutolanza
authored andcommitted
[CIR][Lowering] Lower #cir.zero nested in initializer attributes
Supports lowering for #cir.zero attributes when it appears in aggregate attributes such as #const.array and #cir.struct. ghstack-source-id: d26a506 Pull Request resolved: #219
1 parent 88451f0 commit fc8dc2f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ lowerCirAttrAsValue(mlir::FloatAttr fltAttr, mlir::Location loc,
107107
loc, converter->convertType(fltAttr.getType()), fltAttr.getValue());
108108
}
109109

110+
/// ZeroAttr visitor.
111+
inline mlir::Value
112+
lowerCirAttrAsValue(mlir::cir::ZeroAttr zeroAttr, mlir::Location loc,
113+
mlir::ConversionPatternRewriter &rewriter,
114+
const mlir::TypeConverter *converter) {
115+
return rewriter.create<mlir::cir::ZeroInitConstOp>(
116+
loc, converter->convertType(zeroAttr.getType()));
117+
}
118+
110119
/// ConstStruct visitor.
111120
mlir::Value lowerCirAttrAsValue(mlir::cir::ConstStructAttr constStruct,
112121
mlir::Location loc,
@@ -157,10 +166,10 @@ lowerCirAttrAsValue(mlir::Attribute attr, mlir::Location loc,
157166
return lowerCirAttrAsValue(constStruct, loc, rewriter, converter);
158167
if (const auto constArr = attr.dyn_cast<mlir::cir::ConstArrayAttr>())
159168
return lowerCirAttrAsValue(constArr, loc, rewriter, converter);
160-
if (const auto zeroAttr = attr.dyn_cast<mlir::cir::BoolAttr>())
169+
if (const auto boolAttr = attr.dyn_cast<mlir::cir::BoolAttr>())
161170
llvm_unreachable("bool attribute is NYI");
162171
if (const auto zeroAttr = attr.dyn_cast<mlir::cir::ZeroAttr>())
163-
llvm_unreachable("zero attribute is NYI");
172+
return lowerCirAttrAsValue(zeroAttr, loc, rewriter, converter);
164173

165174
llvm_unreachable("unhandled attribute type");
166175
}

clang/test/CIR/Lowering/array.cir

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR
22
// RUN: cir-translate %s -cir-to-llvmir -o - | FileCheck %s -check-prefix=LLVM
33

4+
!s32i = !cir.int<s, 32>
5+
!ty_22struct2ES22 = !cir.struct<"struct.S", !s32i, #cir.recdecl.ast>
6+
47
module {
58
cir.func @foo() {
69
%0 = cir.alloca !cir.array<i32 x 10>, cir.ptr <!cir.array<i32 x 10>>, ["a"] {alignment = 16 : i64}
710
cir.return
811
}
9-
}
1012

1113
// MLIR: module {
1214
// MLIR-NEXT: func @foo()
@@ -18,3 +20,16 @@ module {
1820

1921
// LLVM: %1 = alloca [10 x i32], i64 1, align 16
2022
// LLVM-NEXT: ret void
23+
24+
cir.global external @arr = #cir.const_array<[#cir.const_struct<{#cir.int<1> : !s32i}> : !ty_22struct2ES22, #cir.zero : !ty_22struct2ES22]> : !cir.array<!ty_22struct2ES22 x 2>
25+
// CHECK: llvm.mlir.global external @arr() {addr_space = 0 : i32} : !llvm.array<2 x struct<"struct.S", (i32)>> {
26+
// CHECK: %0 = llvm.mlir.undef : !llvm.array<2 x struct<"struct.S", (i32)>>
27+
// CHECK: %1 = llvm.mlir.undef : !llvm.struct<"struct.S", (i32)>
28+
// CHECK: %2 = llvm.mlir.constant(1 : i32) : i32
29+
// CHECK: %3 = llvm.insertvalue %2, %1[0] : !llvm.struct<"struct.S", (i32)>
30+
// CHECK: %4 = llvm.insertvalue %3, %0[0] : !llvm.array<2 x struct<"struct.S", (i32)>>
31+
// CHECK: %5 = cir.llvmir.zeroinit : !llvm.struct<"struct.S", (i32)>
32+
// CHECK: %6 = llvm.insertvalue %5, %4[1] : !llvm.array<2 x struct<"struct.S", (i32)>>
33+
// CHECK: llvm.return %6 : !llvm.array<2 x struct<"struct.S", (i32)>>
34+
// CHECK: }
35+
}

0 commit comments

Comments
 (0)