Skip to content

Commit 8276bbf

Browse files
sitio-coutolanza
authored andcommitted
[CIR][CIRGen] Use #cir.zero on zero-initialized global arrays
ghstack-source-id: 1f793b2 Pull Request resolved: #207
1 parent 9c154f4 commit 8276bbf

File tree

6 files changed

+12
-17
lines changed

6 files changed

+12
-17
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,8 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
174174
return mlir::cir::IntAttr::get(ty, 0);
175175
if (ty.isa<mlir::FloatType>())
176176
return mlir::FloatAttr::get(ty, 0.0);
177-
if (auto arrTy = ty.dyn_cast<mlir::cir::ArrayType>()) {
178-
// FIXME(cir): We should have a proper zero initializer CIR instead of
179-
// manually pumping zeros into the array.
180-
assert(!UnimplementedFeature::zeroInitializer());
181-
auto values = llvm::SmallVector<mlir::Attribute, 4>();
182-
auto zero = getZeroInitAttr(arrTy.getEltType());
183-
for (unsigned i = 0, e = arrTy.getSize(); i < e; ++i)
184-
values.push_back(zero);
185-
return getConstArray(mlir::ArrayAttr::get(getContext(), values), arrTy);
186-
}
177+
if (auto arrTy = ty.dyn_cast<mlir::cir::ArrayType>())
178+
return getZeroAttr(arrTy);
187179
if (auto ptrTy = ty.dyn_cast<mlir::cir::PointerType>())
188180
return getNullPtrAttr(ptrTy);
189181
if (auto structTy = ty.dyn_cast<mlir::cir::StructType>())

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ buildArrayConstant(CIRGenModule &CGM, mlir::Type DesiredType,
950950
}
951951

952952
if (NonzeroLength == 0)
953-
assert(0 && "NYE");
953+
return builder.getZeroInitAttr(DesiredType);
954954

955955
// Add a zeroinitializer array filler if we have lots of trailing zeroes.
956956
unsigned TrailingZeroes = ArrayBound - NonzeroLength;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,9 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
170170
}
171171

172172
if (attrType.isa<ZeroAttr>()) {
173-
// FIXME: should also support arrays / const_arrays.
174-
if (opType.isa<::mlir::cir::StructType>())
173+
if (opType.isa<::mlir::cir::StructType, ::mlir::cir::ArrayType>())
175174
return success();
176-
return op->emitOpError("zero expects struct type");
175+
return op->emitOpError("zero expects struct or array type");
177176
}
178177

179178
if (attrType.isa<mlir::cir::BoolAttr>()) {

clang/test/CIR/CodeGen/array.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ int multidim(int i, int j) {
6161
// CHECK: %7 = cir.load %{{.+}} : cir.ptr <!s32i>, !s32i
6262
// CHECK: %8 = cir.cast(array_to_ptrdecay, %6 : !cir.ptr<!cir.array<!s32i x 2>>), !cir.ptr<!s32i>
6363
// CHECK: %9 = cir.ptr_stride(%8 : !cir.ptr<!s32i>, %7 : !s32i), !cir.ptr<!s32i>
64+
65+
// Should globally zero-initialize null arrays.
66+
int globalNullArr[] = {0, 0};
67+
// CHECK: cir.global external @globalNullArr = #cir.zero : !cir.array<!s32i x 2>

clang/test/CIR/CodeGen/globals.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ int tentativeD[];
3535
float zeroInitFlt[2];
3636
// CHECK: cir.global external @tentativeA = #cir.int<0> : !s32i
3737
// CHECK: cir.global external @tentativeC = 0.000000e+00 : f32
38-
// CHECK: cir.global external @tentativeD = #cir.const_array<[#cir.int<0> : !s32i]> : !cir.array<!s32i x 1>
39-
// CHECK: cir.global external @zeroInitFlt = #cir.const_array<[0.000000e+00 : f32, 0.000000e+00 : f32]> : !cir.array<f32 x 2>
38+
// CHECK: cir.global external @tentativeD = #cir.zero : !cir.array<!s32i x 1>
39+
// CHECK: cir.global external @zeroInitFlt = #cir.zero : !cir.array<f32 x 2>

clang/test/CIR/IR/invalid.cir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ cir.func @unary1() {
259259

260260
!u32i = !cir.int<u, 32>
261261
module {
262-
cir.global external @v = #cir.zero : !u32i // expected-error {{zero expects struct type}}
262+
cir.global external @v = #cir.zero : !u32i // expected-error {{zero expects struct or array type}}
263263
}
264264

265265
// -----

0 commit comments

Comments
 (0)