[clang] [CIR] Add support for storing into _Atomic variables (PR #165872)

Sirui Mu via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 4 08:16:14 PST 2025


================
@@ -815,6 +857,85 @@ RValue CIRGenFunction::emitAtomicExpr(AtomicExpr *e) {
       e->getExprLoc());
 }
 
+void CIRGenFunction::emitAtomicStore(RValue rvalue, LValue dest, bool isInit) {
+  bool isVolatile = dest.isVolatileQualified();
+  cir::MemOrder order;
+  if (dest.getType()->isAtomicType()) {
+    order = cir::MemOrder::SequentiallyConsistent;
+  } else {
+    order = cir::MemOrder::Release;
+    isVolatile = true;
+  }
+  return emitAtomicStore(rvalue, dest, order, isVolatile, isInit);
+}
+
+/// Emit a store to an l-value of atomic type.
+///
+/// Note that the r-value is expected to be an r-value of the atomic type; this
+/// means that for aggregate r-values, it should include storage for any padding
+/// that was necessary.
+void CIRGenFunction::emitAtomicStore(RValue rvalue, LValue dest,
+                                     cir::MemOrder order, bool isVolatile,
+                                     bool isInit) {
+  // If this is an aggregate r-value, it should agree in type except
+  // maybe for address-space qualification.
+  auto loc = dest.getPointer().getLoc();
+  assert(!rvalue.isAggregate() ||
+         rvalue.getAggregateAddress().getElementType() ==
+             dest.getAddress().getElementType());
+
+  AtomicInfo atomics(*this, dest, loc);
+  LValue lvalue = atomics.getAtomicLValue();
+
+  // If this is an initialization, just put the value there normally.
+  if (lvalue.isSimple()) {
+    if (isInit) {
+      atomics.emitCopyIntoMemory(rvalue);
----------------
Lancern wrote:

This is already covered by existing test case:

https://github.com/llvm/llvm-project/blob/71022d1ed6f1446fde4ca13f21259c5e550af0f7/clang/test/CIR/CodeGen/atomic.c#L8-L10

Before this PR this assignment is handled by non-CIRGenAtomic code, which happens to generate correct code since initialization of atomic types does not require to be atomic.

https://github.com/llvm/llvm-project/pull/165872


More information about the cfe-commits mailing list