Skip to content

Commit 610ec53

Browse files
authored
[CIR] Backport auto var constant init fix (#1973)
This backports a problem I noticed while upstreaming constant initialization of automatic aggregate variables. Even though the aggregate is being initialized, we weren't setting the `init` attribute. There is one odd case where the constant being used to initialize a class is an undef. OGCG does the same thing. In this patch I'm not attempting to detect that case, and I'm not sure if we should.
1 parent de26ca3 commit 610ec53

File tree

13 files changed

+36
-20
lines changed

13 files changed

+36
-20
lines changed

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ static void emitStoresForConstant(CIRGenModule &CGM, const VarDecl &D,
248248
if (addr.getElementType() != Ty)
249249
addr = addr.withElementType(builder, Ty);
250250

251+
// If the address is an alloca, set the init attribute.
252+
// The address is usually and alloca, but there is at least one case where
253+
// emitAutoVarInit is called from the OpenACC codegen with an address that
254+
// is a cast, not an alloca.
255+
// TODO(cir): Handle the cast case.
256+
auto allocaOp = addr.getDefiningOp<cir::AllocaOp>();
257+
if (allocaOp)
258+
allocaOp.setInitAttr(mlir::UnitAttr::get(&CGM.getMLIRContext()));
259+
251260
auto loc = CGM.getLoc(D.getSourceRange());
252261
builder.createStore(loc, builder.getConstant(loc, constant), addr);
253262
}

clang/test/CIR/CallConvLowering/AArch64/struct.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void foo1() {
6868
// CIR: %[[#V1:]] = cir.cast bitcast %[[#V0]] : !cir.ptr<!rec_S> -> !cir.ptr<!u64i>
6969
// CIR: cir.store{{.*}} %arg0, %[[#V1]] : !u64i, !cir.ptr<!u64i>
7070
// CIR: %[[#V2:]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["__retval"] {alignment = 4 : i64}
71-
// CIR: %[[#V3:]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s2"]
71+
// CIR: %[[#V3:]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s2", init]
7272
// CIR: %[[#V4:]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["tmp"] {alignment = 4 : i64}
7373
// CIR: %[[#V5:]] = cir.const #cir.const_record<{#cir.int<1> : !s32i, #cir.int<2> : !s32i}> : !rec_S
7474
// CIR: cir.store{{.*}} %[[#V5]], %[[#V3]] : !rec_S, !cir.ptr<!rec_S>

clang/test/CIR/CodeGen/agg-init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void yo() {
4747
}
4848

4949
// CHECK: cir.func dso_local @_Z2yov()
50-
// CHECK: %0 = cir.alloca !rec_Yo, !cir.ptr<!rec_Yo>, ["ext"] {alignment = 8 : i64}
50+
// CHECK: %0 = cir.alloca !rec_Yo, !cir.ptr<!rec_Yo>, ["ext", init] {alignment = 8 : i64}
5151
// CHECK: %1 = cir.alloca !rec_Yo, !cir.ptr<!rec_Yo>, ["ext2", init] {alignment = 8 : i64}
5252
// CHECK: %2 = cir.const #cir.const_record<{#cir.int<1000070000> : !u32i, #cir.ptr<null> : !cir.ptr<!void>, #cir.int<0> : !u64i}> : !rec_Yo
5353
// CHECK: cir.store{{.*}} %2, %0 : !rec_Yo, !cir.ptr<!rec_Yo>

clang/test/CIR/CodeGen/agg-init2.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ void f() {
1313
Zero z1 = Zero{};
1414
}
1515

16+
// TODO: In this case, z1 gets "initialized" with an undef value. Should we
17+
// treat that as uninitialized? Should it even be happening?
18+
1619
// CHECK: cir.func dso_local @_Z1fv()
17-
// CHECK: %0 = cir.alloca !rec_Zero, !cir.ptr<!rec_Zero>, ["z0", init]
18-
// CHECK: %1 = cir.alloca !rec_Zero, !cir.ptr<!rec_Zero>, ["z1"]
19-
// CHECK: cir.call @_ZN4ZeroC1Ev(%0) : (!cir.ptr<!rec_Zero>) -> ()
20+
// CHECK: %[[Z0:.*]] = cir.alloca !rec_Zero, !cir.ptr<!rec_Zero>, ["z0", init]
21+
// CHECK: %[[Z1:.*]] = cir.alloca !rec_Zero, !cir.ptr<!rec_Zero>, ["z1", init]
22+
// CHECK: cir.call @_ZN4ZeroC1Ev(%[[Z0]]) : (!cir.ptr<!rec_Zero>) -> ()
23+
// CHECK: %[[UNDEF:.*]] = cir.const #cir.undef : !rec_Zero
24+
// CHECK: cir.store{{.*}} %[[UNDEF]], %[[Z1]] : !rec_Zero, !cir.ptr<!rec_Zero>
2025
// CHECK: cir.return

clang/test/CIR/CodeGen/array-init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void foo() {
8282
double bar[] = {9,8,7};
8383
}
8484
// CIR-LABEL: @foo
85-
// CIR: %[[DST:.*]] = cir.alloca !cir.array<!cir.double x 3>, !cir.ptr<!cir.array<!cir.double x 3>>, ["bar"]
85+
// CIR: %[[DST:.*]] = cir.alloca !cir.array<!cir.double x 3>, !cir.ptr<!cir.array<!cir.double x 3>>, ["bar", init]
8686
// CIR: %[[SRC:.*]] = cir.get_global @__const.foo.bar : !cir.ptr<!cir.array<!cir.double x 3>>
8787
// CIR: cir.copy %[[SRC]] to %[[DST]] : !cir.ptr<!cir.array<!cir.double x 3>>
8888

clang/test/CIR/CodeGen/complex-cast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void complex_user_defined_cast() {
3131
int _Complex c = p;
3232
}
3333

34-
// CIR-AFTER: %[[P_ADDR:.*]] = cir.alloca !rec_Point, !cir.ptr<!rec_Point>, ["p"]
34+
// CIR-AFTER: %[[P_ADDR:.*]] = cir.alloca !rec_Point, !cir.ptr<!rec_Point>, ["p", init]
3535
// CIR-AFTER: %[[C_ADDR:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["c", init]
3636
// CIR-AFTER: %[[P_CONST:.*]] = cir.const #cir.const_record<{#cir.int<1> : !s32i, #cir.int<2> : !s32i}> : !rec_Point
3737
// CIR-AFTER: cir.store{{.*}} %[[P_CONST]], %[[P_ADDR]] : !rec_Point, !cir.ptr<!rec_Point>

clang/test/CIR/CodeGen/const-array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ void foo() {
1313
}
1414

1515
// CHECK-LABEL: @foo()
16-
// CHECK: %[[ADDR:.*]] = cir.alloca !cir.array<!s32i x 10>, !cir.ptr<!cir.array<!s32i x 10>>, ["a"]
16+
// CHECK: %[[ADDR:.*]] = cir.alloca !cir.array<!s32i x 10>, !cir.ptr<!cir.array<!s32i x 10>>, ["a", init]
1717
// CHECK: %[[SRC:.*]] = cir.get_global @__const.foo.a : !cir.ptr<!cir.array<!s32i x 10>>
1818
// CHECK: cir.copy %[[SRC]] to %[[ADDR]] : !cir.ptr<!cir.array<!s32i x 10>>

clang/test/CIR/CodeGen/packed-structs.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ typedef struct {
7171
} E;
7272

7373
// CIR: cir.func {{.*@f1()}}
74-
// CIR: {{.*}} = cir.alloca !rec_E, !cir.ptr<!rec_E>, ["a"] {alignment = 2 : i64}
74+
// CIR: %[[E:.*]] = cir.alloca !rec_E, !cir.ptr<!rec_E>, ["a", init] {alignment = 2 : i64}
75+
// CIR: %[[ZERO:.*]] = cir.const #cir.zero : !rec_E
76+
// CIR: cir.store{{.*}} %[[ZERO]], %[[E]] : !rec_E, !cir.ptr<!rec_E>
7577

7678
// LLVM: {{.*}} = alloca %struct.E, i64 1, align 2
7779
void f1() {
@@ -91,7 +93,7 @@ typedef struct {
9193
} G;
9294

9395
// CIR: cir.func {{.*@f2()}}
94-
// CIR: {{.*}} = cir.alloca !rec_G, !cir.ptr<!rec_G>, ["a"] {alignment = 1 : i64}
96+
// CIR: {{.*}} = cir.alloca !rec_G, !cir.ptr<!rec_G>, ["a", init] {alignment = 1 : i64}
9597

9698
// LLVM: {{.*}} = alloca %struct.G, i64 1, align 1
9799
void f2() {
@@ -123,7 +125,7 @@ typedef struct {
123125
} J;
124126

125127
// CIR: cir.func {{.*@f3()}}
126-
// CIR: {{.*}} = cir.alloca !rec_J, !cir.ptr<!rec_J>, ["a"] {alignment = 1 : i64}
128+
// CIR: {{.*}} = cir.alloca !rec_J, !cir.ptr<!rec_J>, ["a", init] {alignment = 1 : i64}
127129

128130
// LLVM: {{.*}} = alloca %struct.J, i64 1, align 1
129131
void f3() {

clang/test/CIR/CodeGen/std-find.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int test_find(unsigned char n = 3)
1010
// CHECK: cir.func dso_local @_Z9test_findh(%arg0: !u8i
1111
unsigned num_found = 0;
1212
std::array<unsigned char, 9> v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
13-
// CHECK: %[[array_addr:.*]] = cir.alloca ![[array]], !cir.ptr<![[array]]>, ["v"]
13+
// CHECK: %[[array_addr:.*]] = cir.alloca ![[array]], !cir.ptr<![[array]]>, ["v", init]
1414

1515
auto f = std::find(v.begin(), v.end(), n);
1616
// CHECK: {{.*}} cir.call @_ZNSt5arrayIhLj9EE5beginEv(%[[array_addr]])

clang/test/CIR/CodeGen/struct.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ struct NonTrivialConstexprConstructor {
134134
// Should locally copy struct members.
135135
void shouldLocallyCopyStructAssignments(void) {
136136
struct A a = { 3 };
137-
// CHECK: %[[#SA:]] = cir.alloca !rec_A, !cir.ptr<!rec_A>, ["a"] {alignment = 4 : i64}
137+
// CHECK: %[[#SA:]] = cir.alloca !rec_A, !cir.ptr<!rec_A>, ["a", init] {alignment = 4 : i64}
138138
struct A b = a;
139139
// CHECK: %[[#SB:]] = cir.alloca !rec_A, !cir.ptr<!rec_A>, ["b", init] {alignment = 4 : i64}
140140
// cir.copy %[[#SA]] to %[[SB]] : !cir.ptr<!rec_A>
@@ -200,7 +200,7 @@ void unary_extension() {
200200
CompleteS a = __extension__ CompleteS();
201201
}
202202

203-
// CHECK: %[[A_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr<!rec_CompleteS>, ["a"]
203+
// CHECK: %[[A_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr<!rec_CompleteS>, ["a", init]
204204
// CHECK: %[[ZERO_INIT:.*]] = cir.const #cir.zero : !rec_CompleteS
205205
// CHECK: cir.store{{.*}} %[[ZERO_INIT]], %[[A_ADDR]] : !rec_CompleteS, !cir.ptr<!rec_CompleteS>
206206

0 commit comments

Comments
 (0)