Skip to content

Commit 64704c6

Browse files
authored
[CIR] Add support for C++ initializer lists (#150681)
This adds basic support for using C++ initializer lists to initialize fields of a record.
1 parent 56ae79a commit 64704c6

File tree

2 files changed

+274
-2
lines changed

2 files changed

+274
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,98 @@ void AggExprEmitter::visitCXXParenListOrInitListExpr(
357357
emitArrayInit(dest.getAddress(), arrayTy, e->getType(), e, args,
358358
arrayFiller);
359359
return;
360+
} else if (e->getType()->isVariableArrayType()) {
361+
cgf.cgm.errorNYI(e->getSourceRange(),
362+
"visitCXXParenListOrInitListExpr variable array type");
363+
return;
364+
}
365+
366+
if (e->getType()->isArrayType()) {
367+
cgf.cgm.errorNYI(e->getSourceRange(),
368+
"visitCXXParenListOrInitListExpr array type");
369+
return;
370+
}
371+
372+
assert(e->getType()->isRecordType() && "Only support structs/unions here!");
373+
374+
// Do struct initialization; this code just sets each individual member
375+
// to the approprate value. This makes bitfield support automatic;
376+
// the disadvantage is that the generated code is more difficult for
377+
// the optimizer, especially with bitfields.
378+
unsigned numInitElements = args.size();
379+
RecordDecl *record = e->getType()->castAs<RecordType>()->getDecl();
380+
381+
// We'll need to enter cleanup scopes in case any of the element
382+
// initializers throws an exception.
383+
assert(!cir::MissingFeatures::requiresCleanups());
384+
385+
unsigned curInitIndex = 0;
386+
387+
// Emit initialization of base classes.
388+
if (auto *cxxrd = dyn_cast<CXXRecordDecl>(record)) {
389+
assert(numInitElements >= cxxrd->getNumBases() &&
390+
"missing initializer for base class");
391+
if (cxxrd->getNumBases() > 0) {
392+
cgf.cgm.errorNYI(e->getSourceRange(),
393+
"visitCXXParenListOrInitListExpr base class init");
394+
return;
395+
}
396+
}
397+
398+
LValue destLV = cgf.makeAddrLValue(dest.getAddress(), e->getType());
399+
400+
if (record->isUnion()) {
401+
cgf.cgm.errorNYI(e->getSourceRange(),
402+
"visitCXXParenListOrInitListExpr union type");
403+
return;
360404
}
361405

362-
cgf.cgm.errorNYI(
363-
"visitCXXParenListOrInitListExpr Record or VariableSizeArray type");
406+
// Here we iterate over the fields; this makes it simpler to both
407+
// default-initialize fields and skip over unnamed fields.
408+
for (const FieldDecl *field : record->fields()) {
409+
// We're done once we hit the flexible array member.
410+
if (field->getType()->isIncompleteArrayType())
411+
break;
412+
413+
// Always skip anonymous bitfields.
414+
if (field->isUnnamedBitField())
415+
continue;
416+
417+
// We're done if we reach the end of the explicit initializers, we
418+
// have a zeroed object, and the rest of the fields are
419+
// zero-initializable.
420+
if (curInitIndex == numInitElements && dest.isZeroed() &&
421+
cgf.getTypes().isZeroInitializable(e->getType()))
422+
break;
423+
LValue lv =
424+
cgf.emitLValueForFieldInitialization(destLV, field, field->getName());
425+
// We never generate write-barriers for initialized fields.
426+
assert(!cir::MissingFeatures::setNonGC());
427+
428+
if (curInitIndex < numInitElements) {
429+
// Store the initializer into the field.
430+
CIRGenFunction::SourceLocRAIIObject loc{
431+
cgf, cgf.getLoc(record->getSourceRange())};
432+
emitInitializationToLValue(args[curInitIndex++], lv);
433+
} else {
434+
// We're out of initializers; default-initialize to null
435+
emitNullInitializationToLValue(cgf.getLoc(e->getSourceRange()), lv);
436+
}
437+
438+
// Push a destructor if necessary.
439+
// FIXME: if we have an array of structures, all explicitly
440+
// initialized, we can end up pushing a linear number of cleanups.
441+
if (QualType::DestructionKind dtorKind =
442+
field->getType().isDestructedType()) {
443+
cgf.cgm.errorNYI(e->getSourceRange(),
444+
"visitCXXParenListOrInitListExpr destructor");
445+
return;
446+
}
447+
448+
// From classic codegen, maybe not useful for CIR:
449+
// If the GEP didn't get used because of a dead zero init or something
450+
// else, clean it up for -O0 builds and general tidiness.
451+
}
364452
}
365453

366454
// TODO(cir): This could be shared with classic codegen.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
7+
8+
struct S {
9+
int a, b, c;
10+
};
11+
12+
void init() {
13+
S s1 = {1, 2, 3};
14+
S s2 = {4, 5};
15+
}
16+
17+
// CIR: cir.func{{.*}} @_Z4initv()
18+
// CIR: %[[S1:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s1", init]
19+
// CIR: %[[S2:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s2", init]
20+
// CIR: %[[S1_A:.*]] = cir.get_member %[[S1]][0] {name = "a"}
21+
// CIR: %[[ONE:.*]] = cir.const #cir.int<1>
22+
// CIR: cir.store{{.*}} %[[ONE]], %[[S1_A]]
23+
// CIR: %[[S1_B:.*]] = cir.get_member %[[S1]][1] {name = "b"}
24+
// CIR: %[[TWO:.*]] = cir.const #cir.int<2>
25+
// CIR: cir.store{{.*}} %[[TWO]], %[[S1_B]]
26+
// CIR: %[[S1_C:.*]] = cir.get_member %[[S1]][2] {name = "c"}
27+
// CIR: %[[THREE:.*]] = cir.const #cir.int<3>
28+
// CIR: cir.store{{.*}} %[[THREE]], %[[S1_C]]
29+
// CIR: %[[S2_A:.*]] = cir.get_member %[[S2]][0] {name = "a"}
30+
// CIR: %[[FOUR:.*]] = cir.const #cir.int<4>
31+
// CIR: cir.store{{.*}} %[[FOUR]], %[[S2_A]]
32+
// CIR: %[[S2_B:.*]] = cir.get_member %[[S2]][1] {name = "b"}
33+
// CIR: %[[FIVE:.*]] = cir.const #cir.int<5>
34+
// CIR: cir.store{{.*}} %[[FIVE]], %[[S2_B]]
35+
// CIR: %[[S2_C:.*]] = cir.get_member %[[S2]][2] {name = "c"}
36+
// CIR: %[[ZERO:.*]] = cir.const #cir.int<0>
37+
// CIR: cir.store{{.*}} %[[ZERO]], %[[S2_C]]
38+
// CIR: cir.return
39+
40+
// LLVM: define{{.*}} void @_Z4initv()
41+
// LLVM: %[[S1:.*]] = alloca %struct.S
42+
// LLVM: %[[S2:.*]] = alloca %struct.S
43+
// LLVM: %[[S1_A:.*]] = getelementptr %struct.S, ptr %[[S1]], i32 0, i32 0
44+
// LLVM: store i32 1, ptr %[[S1_A]]
45+
// LLVM: %[[S1_B:.*]] = getelementptr %struct.S, ptr %[[S1]], i32 0, i32 1
46+
// LLVM: store i32 2, ptr %[[S1_B]]
47+
// LLVM: %[[S1_C:.*]] = getelementptr %struct.S, ptr %[[S1]], i32 0, i32 2
48+
// LLVM: store i32 3, ptr %[[S1_C]]
49+
// LLVM: %[[S2_A:.*]] = getelementptr %struct.S, ptr %[[S2]], i32 0, i32 0
50+
// LLVM: store i32 4, ptr %[[S2_A]]
51+
// LLVM: %[[S2_B:.*]] = getelementptr %struct.S, ptr %[[S2]], i32 0, i32 1
52+
// LLVM: store i32 5, ptr %[[S2_B]]
53+
// LLVM: %[[S2_C:.*]] = getelementptr %struct.S, ptr %[[S2]], i32 0, i32 2
54+
// LLVM: store i32 0, ptr %[[S2_C]]
55+
56+
// OGCG: @__const._Z4initv.s1 = private unnamed_addr constant %struct.S { i32 1, i32 2, i32 3 }
57+
// OGCG: @__const._Z4initv.s2 = private unnamed_addr constant %struct.S { i32 4, i32 5, i32 0 }
58+
59+
// OGCG: define{{.*}} void @_Z4initv()
60+
// OGCG: %[[S1:.*]] = alloca %struct.S
61+
// OGCG: %[[S2:.*]] = alloca %struct.S
62+
// OGCG: call void @llvm.memcpy.p0.p0.i64(ptr{{.*}} %[[S1]], ptr{{.*}} @__const._Z4initv.s1, i64 12, i1 false)
63+
// OGCG: call void @llvm.memcpy.p0.p0.i64(ptr{{.*}} %[[S2]], ptr{{.*}} @__const._Z4initv.s2, i64 12, i1 false)
64+
65+
void init_var(int a, int b) {
66+
S s = {a, b};
67+
}
68+
69+
// CIR: cir.func{{.*}} @_Z8init_varii(%[[A_ARG:.*]]: !s32i {{.*}}, %[[B_ARG:.*]]: !s32i {{.*}})
70+
// CIR: %[[A_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
71+
// CIR: %[[B_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
72+
// CIR: %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init]
73+
// CIR: cir.store{{.*}} %[[A_ARG]], %[[A_PTR]]
74+
// CIR: cir.store{{.*}} %[[B_ARG]], %[[B_PTR]]
75+
// CIR: %[[S_A:.*]] = cir.get_member %[[S]][0] {name = "a"}
76+
// CIR: %[[A:.*]] = cir.load{{.*}} %[[A_PTR]]
77+
// CIR: cir.store{{.*}} %[[A]], %[[S_A]]
78+
// CIR: %[[S_B:.*]] = cir.get_member %[[S]][1] {name = "b"}
79+
// CIR: %[[B:.*]] = cir.load{{.*}} %[[B_PTR]]
80+
// CIR: cir.store{{.*}} %[[B]], %[[S_B]]
81+
// CIR: cir.return
82+
83+
// LLVM: define{{.*}} void @_Z8init_varii(i32 %[[A_ARG:.*]], i32 %[[B_ARG:.*]])
84+
// LLVM: %[[A_PTR:.*]] = alloca i32
85+
// LLVM: %[[B_PTR:.*]] = alloca i32
86+
// LLVM: %[[S:.*]] = alloca %struct.S
87+
// LLVM: store i32 %[[A_ARG]], ptr %[[A_PTR]]
88+
// LLVM: store i32 %[[B_ARG]], ptr %[[B_PTR]]
89+
// LLVM: %[[S_A:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 0
90+
// LLVM: %[[A:.*]] = load i32, ptr %[[A_PTR]]
91+
// LLVM: store i32 %[[A]], ptr %[[S_A]]
92+
// LLVM: %[[S_B:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 1
93+
// LLVM: %[[B:.*]] = load i32, ptr %[[B_PTR]]
94+
// LLVM: store i32 %[[B]], ptr %[[S_B]]
95+
// LLVM: ret void
96+
97+
// OGCG: define{{.*}} void @_Z8init_varii(i32 {{.*}} %[[A_ARG:.*]], i32 {{.*}} %[[B_ARG:.*]])
98+
// OGCG: %[[A_PTR:.*]] = alloca i32
99+
// OGCG: %[[B_PTR:.*]] = alloca i32
100+
// OGCG: %[[S:.*]] = alloca %struct.S
101+
// OGCG: store i32 %[[A_ARG]], ptr %[[A_PTR]]
102+
// OGCG: store i32 %[[B_ARG]], ptr %[[B_PTR]]
103+
// OGCG: %[[S_A:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 0
104+
// OGCG: %[[A:.*]] = load i32, ptr %[[A_PTR]]
105+
// OGCG: store i32 %[[A]], ptr %[[S_A]]
106+
// OGCG: %[[S_B:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 1
107+
// OGCG: %[[B:.*]] = load i32, ptr %[[B_PTR]]
108+
// OGCG: store i32 %[[B]], ptr %[[S_B]]
109+
// OGCG: %[[S_C:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 2
110+
// OGCG: store i32 0, ptr %[[S_C]]
111+
// OGCG: ret void
112+
113+
void init_expr(int a, int b, int c) {
114+
S s = {a + 1, b + 2, c + 3};
115+
}
116+
117+
// CIR: cir.func{{.*}} @_Z9init_expriii(%[[A_ARG:.*]]: !s32i {{.*}}, %[[B_ARG:.*]]: !s32i {{.*}}, %[[C_ARG:.*]]: !s32i {{.*}})
118+
// CIR: %[[A_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
119+
// CIR: %[[B_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
120+
// CIR: %[[C_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["c", init]
121+
// CIR: %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init]
122+
// CIR: cir.store{{.*}} %[[A_ARG]], %[[A_PTR]]
123+
// CIR: cir.store{{.*}} %[[B_ARG]], %[[B_PTR]]
124+
// CIR: cir.store{{.*}} %[[C_ARG]], %[[C_PTR]]
125+
// CIR: %[[S_A:.*]] = cir.get_member %[[S]][0] {name = "a"}
126+
// CIR: %[[A:.*]] = cir.load{{.*}} %[[A_PTR]]
127+
// CIR: %[[ONE:.*]] = cir.const #cir.int<1>
128+
// CIR: %[[A_PLUS_ONE:.*]] = cir.binop(add, %[[A]], %[[ONE]])
129+
// CIR: cir.store{{.*}} %[[A_PLUS_ONE]], %[[S_A]]
130+
// CIR: %[[S_B:.*]] = cir.get_member %[[S]][1] {name = "b"}
131+
// CIR: %[[B:.*]] = cir.load{{.*}} %[[B_PTR]]
132+
// CIR: %[[TWO:.*]] = cir.const #cir.int<2>
133+
// CIR: %[[B_PLUS_TWO:.*]] = cir.binop(add, %[[B]], %[[TWO]]) nsw : !s32i
134+
// CIR: cir.store{{.*}} %[[B_PLUS_TWO]], %[[S_B]]
135+
// CIR: %[[S_C:.*]] = cir.get_member %[[S]][2] {name = "c"}
136+
// CIR: %[[C:.*]] = cir.load{{.*}} %[[C_PTR]]
137+
// CIR: %[[THREE:.*]] = cir.const #cir.int<3>
138+
// CIR: %[[C_PLUS_THREE:.*]] = cir.binop(add, %[[C]], %[[THREE]]) nsw : !s32i
139+
// CIR: cir.store{{.*}} %[[C_PLUS_THREE]], %[[S_C]]
140+
// CIR: cir.return
141+
142+
// LLVM: define{{.*}} void @_Z9init_expriii(i32 %[[A_ARG:.*]], i32 %[[B_ARG:.*]], i32 %[[C_ARG:.*]])
143+
// LLVM: %[[A_PTR:.*]] = alloca i32
144+
// LLVM: %[[B_PTR:.*]] = alloca i32
145+
// LLVM: %[[C_PTR:.*]] = alloca i32
146+
// LLVM: %[[S:.*]] = alloca %struct.S
147+
// LLVM: store i32 %[[A_ARG]], ptr %[[A_PTR]]
148+
// LLVM: store i32 %[[B_ARG]], ptr %[[B_PTR]]
149+
// LLVM: store i32 %[[C_ARG]], ptr %[[C_PTR]]
150+
// LLVM: %[[S_A:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 0
151+
// LLVM: %[[A:.*]] = load i32, ptr %[[A_PTR]]
152+
// LLVM: %[[A_PLUS_ONE:.*]] = add nsw i32 %[[A]], 1
153+
// LLVM: store i32 %[[A_PLUS_ONE]], ptr %[[S_A]]
154+
// LLVM: %[[S_B:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 1
155+
// LLVM: %[[B:.*]] = load i32, ptr %[[B_PTR]]
156+
// LLVM: %[[B_PLUS_TWO:.*]] = add nsw i32 %[[B]], 2
157+
// LLVM: store i32 %[[B_PLUS_TWO]], ptr %[[S_B]]
158+
// LLVM: %[[S_C:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 2
159+
// LLVM: %[[C:.*]] = load i32, ptr %[[C_PTR]]
160+
// LLVM: %[[C_PLUS_THREE:.*]] = add nsw i32 %[[C]], 3
161+
// LLVM: store i32 %[[C_PLUS_THREE]], ptr %[[S_C]]
162+
// LLVM: ret void
163+
164+
// OGCG: define{{.*}} void @_Z9init_expriii(i32 {{.*}} %[[A_ARG:.*]], i32 {{.*}} %[[B_ARG:.*]], i32 {{.*}} %[[C_ARG:.*]])
165+
// OGCG: %[[A_PTR:.*]] = alloca i32
166+
// OGCG: %[[B_PTR:.*]] = alloca i32
167+
// OGCG: %[[C_PTR:.*]] = alloca i32
168+
// OGCG: %[[S:.*]] = alloca %struct.S
169+
// OGCG: store i32 %[[A_ARG]], ptr %[[A_PTR]]
170+
// OGCG: store i32 %[[B_ARG]], ptr %[[B_PTR]]
171+
// OGCG: store i32 %[[C_ARG]], ptr %[[C_PTR]]
172+
// OGCG: %[[S_A:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 0
173+
// OGCG: %[[A:.*]] = load i32, ptr %[[A_PTR]]
174+
// OGCG: %[[A_PLUS_ONE:.*]] = add nsw i32 %[[A]], 1
175+
// OGCG: store i32 %[[A_PLUS_ONE]], ptr %[[S_A]]
176+
// OGCG: %[[S_B:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 1
177+
// OGCG: %[[B:.*]] = load i32, ptr %[[B_PTR]]
178+
// OGCG: %[[B_PLUS_TWO:.*]] = add nsw i32 %[[B]], 2
179+
// OGCG: store i32 %[[B_PLUS_TWO]], ptr %[[S_B]]
180+
// OGCG: %[[S_C:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 2
181+
// OGCG: %[[C:.*]] = load i32, ptr %[[C_PTR]]
182+
// OGCG: %[[C_PLUS_THREE:.*]] = add nsw i32 %[[C]], 3
183+
// OGCG: store i32 %[[C_PLUS_THREE]], ptr %[[S_C]]
184+
// OGCG: ret void

0 commit comments

Comments
 (0)