Skip to content

Commit a144e82

Browse files
sitio-coutolanza
authored andcommitted
[CIR][CIRGen] Implicitly zero-initialize global arrays elements
Whenever a global array is declared and initialized with fewer elements than its size, the remaining elements are implicitly initialized with zero. For aggregates types, such as structs, the initialization is done through the #cir.zero attribute. ghstack-source-id: b3d172c Pull Request resolved: #216
1 parent 8e7ed2d commit a144e82

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
186186
// TODO(cir): Once we have CIR float types, replace this by something like a
187187
// NullableValueInterface to allow for type-independent queries.
188188
bool isNullValue(mlir::Attribute attr) const {
189+
if (attr.isa<mlir::cir::ZeroAttr>())
190+
return true;
191+
189192
// TODO(cir): introduce char type in CIR and check for that instead.
190193
if (const auto intVal = attr.dyn_cast<mlir::cir::IntAttr>())
191194
return intVal.isNullValue();

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,7 @@ class ConstExprEmitter
866866

867867
mlir::Attribute VisitImplicitValueInitExpr(ImplicitValueInitExpr *E,
868868
QualType T) {
869-
assert(0 && "not implemented");
870-
return {};
869+
return CGM.getBuilder().getZeroInitAttr(CGM.getCIRType(T));
871870
}
872871

873872
mlir::Attribute VisitInitListExpr(InitListExpr *ILE, QualType T) {

clang/test/CIR/CodeGen/array.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s
3+
// XFAIL: *
4+
5+
// Should implicitly zero-initialize global array elements.
6+
struct S {
7+
int i;
8+
} arr[3] = {{1}};
9+
// CHECK: cir.global external @arr = #cir.const_array<[#cir.const_struct<{#cir.int<1> : !s32i}> : !ty_22struct2ES22, #cir.zero : !ty_22struct2ES22, #cir.zero : !ty_22struct2ES22]> : !cir.array<!ty_22struct2ES22 x 3>

0 commit comments

Comments
 (0)