[clang] [CIR] Add support for atomic-to-non-atomic cast (PR #193784)

Sirui Mu via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 06:13:22 PDT 2026


================
@@ -189,6 +201,38 @@ Address AtomicInfo::convertToAtomicIntPointer(Address addr) const {
   return castToAtomicIntPointer(addr);
 }
 
+RValue AtomicInfo::emitAtomicLoad(AggValueSlot resultSlot, SourceLocation loc,
+                                  bool asValue, cir::MemOrder order,
+                                  bool isVolatile) {
+  // Check whether we should use a library call.
+  if (shouldUseLibCall()) {
+    assert(!cir::MissingFeatures::atomicUseLibCall());
+    cgf.cgm.errorNYI(loc, "emitAtomicLoad: emit atomic lib call");
+    return RValue::get(nullptr);
+  }
+
+  // Okay, we're doing this natively.
+  mlir::Value loadOp = emitAtomicLoadOp(order, isVolatile);
+
+  // If we're ignoring an aggregate return, don't do anything.
+  if (getEvaluationKind() == TEK_Aggregate && resultSlot.isIgnored())
+    return RValue::getAggregate(Address::invalid(), false);
+
+  // Okay, turn that back into the original value or atomic (for non-simple
+  // lvalues) type.
+  return convertToValueOrAtomic(loadOp, resultSlot, loc, asValue);
+}
+
+mlir::Value AtomicInfo::emitAtomicLoadOp(cir::MemOrder order, bool isVolatile) {
+  Address addr = getAtomicAddress();
----------------
Lancern wrote:

Oh, I should write a comment to explain this, thanks for catching this.

In OGCG, the `shouldCastToInt` check exists because atomic loads and compare-exchanges in LLVM IR have some constraints:

> The type of the pointee must be an integer, pointer, floating-point, or vector type whose bit width is a power of two greater than or equal to eight. [^1]

> The type of ‘<cmp>’ must be an integer or pointer type whose bit width is a power of two greater than or equal to eight. [^2]

Thus OGCG needs to cast the input atomic pointer into an atomic integer pointer before loading or cmpxchg it.

In CIR, I believe that this kind of processing could be postponed until LowerToLLVM. Thus we don't have that `shouldCastToInt` check here. Similarly, the `cmpxchg` argument also becomes unnecessary.

[^1]: https://llvm.org/docs/LangRef.html#id214
[^2]: https://llvm.org/docs/LangRef.html#id230

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


More information about the cfe-commits mailing list