Skip to content

Commit 9a2a4f6

Browse files
authored
[CIR] Implement emitAtomicInit for AggregateExpr (#161826)
Implement emitAtomicInit support for AggregateExpr
1 parent a6a78eb commit 9a2a4f6

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenAtomic.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,26 @@ void CIRGenFunction::emitAtomicInit(Expr *init, LValue dest) {
718718
return;
719719
}
720720

721-
case cir::TEK_Aggregate:
722-
cgm.errorNYI(init->getSourceRange(), "emitAtomicInit: aggregate type");
721+
case cir::TEK_Aggregate: {
722+
// Fix up the destination if the initializer isn't an expression
723+
// of atomic type.
724+
bool zeroed = false;
725+
if (!init->getType()->isAtomicType()) {
726+
zeroed = atomics.emitMemSetZeroIfNecessary();
727+
dest = atomics.projectValue();
728+
}
729+
730+
// Evaluate the expression directly into the destination.
731+
assert(!cir::MissingFeatures::aggValueSlotGC());
732+
AggValueSlot slot = AggValueSlot::forLValue(
733+
dest, AggValueSlot::IsNotDestructed, AggValueSlot::IsNotAliased,
734+
AggValueSlot::DoesNotOverlap,
735+
zeroed ? AggValueSlot::IsZeroed : AggValueSlot::IsNotZeroed);
736+
737+
emitAggExpr(init, slot);
723738
return;
724739
}
740+
}
725741

726742
llvm_unreachable("bad evaluation kind");
727743
}

clang/test/CIR/CodeGen/struct.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,34 @@ void generic_selection() {
184184
// OGCG: %[[D_ADDR:.*]] = alloca %struct.CompleteS, align 4
185185
// OGCG: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %[[D_ADDR]], ptr align 4 %[[A_ADDR]], i64 8, i1 false)
186186

187+
void atomic_init() {
188+
_Atomic CompleteS a;
189+
__c11_atomic_init(&a, {});
190+
}
191+
192+
// CIR: cir.func{{.*}} @_Z11atomic_initv()
193+
// CIR: %[[A_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr<!rec_CompleteS>, ["a"]
194+
// CIR: %[[ELEM_0_PTR:.*]] = cir.get_member %[[A_ADDR]][0] {name = "a"} : !cir.ptr<!rec_CompleteS> -> !cir.ptr<!s32i>
195+
// CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !s32i
196+
// CIR: cir.store{{.*}} %[[CONST_0]], %[[ELEM_0_PTR]] : !s32i, !cir.ptr<!s32i>
197+
// CIR: %[[ELEM_1_PTR:.*]] = cir.get_member %[[A_ADDR]][1] {name = "b"} : !cir.ptr<!rec_CompleteS> -> !cir.ptr<!s8i>
198+
// CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !s8i
199+
// CIR: cir.store{{.*}} %[[CONST_0]], %[[ELEM_1_PTR]] : !s8i, !cir.ptr<!s8i>
200+
201+
// LLVM: define{{.*}} void @_Z11atomic_initv()
202+
// LLVM: %[[A_ADDR:.*]] = alloca %struct.CompleteS, i64 1, align 8
203+
// LLVM: %[[ELEM_0_PTR:.*]] = getelementptr %struct.CompleteS, ptr %[[A_ADDR]], i32 0, i32 0
204+
// LLVM: store i32 0, ptr %[[ELEM_0_PTR]], align 8
205+
// LLVM: %[[ELEM_1_PTR:.*]] = getelementptr %struct.CompleteS, ptr %[[A_ADDR]], i32 0, i32 1
206+
// LLVM: store i8 0, ptr %[[ELEM_1_PTR]], align 4
207+
208+
// OGCG: define{{.*}} void @_Z11atomic_initv()
209+
// OGCG: %[[A_ADDR:.*]] = alloca %struct.CompleteS, align 8
210+
// OGCG: %[[ELEM_0_PTR:.*]] = getelementptr inbounds nuw %struct.CompleteS, ptr %[[A_ADDR]], i32 0, i32 0
211+
// OGCG: store i32 0, ptr %[[ELEM_0_PTR]], align 8
212+
// OGCG: %[[ELEM_1_PTR:.*]] = getelementptr inbounds nuw %struct.CompleteS, ptr %[[A_ADDR]], i32 0, i32 1
213+
// OGCG: store i8 0, ptr %[[ELEM_1_PTR]], align 4
214+
187215
void unary_extension() {
188216
CompleteS a = __extension__ CompleteS();
189217
}

0 commit comments

Comments
 (0)