[clang] [CIR] Implement 'simple' atomic inc/dec. (PR #222730)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 11 11:48:32 PDT 2026


================
@@ -628,10 +628,88 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
     mlir::Value value;
     mlir::Value input;
 
-    if (type->getAs<AtomicType>()) {
+    if (const AtomicType *atomicTy = type->getAs<AtomicType>()) {
+      QualType valType = atomicTy->getValueType();
+      mlir::Location loc = cgf.getLoc(e->getSourceRange());
+      bool isInc = e->isIncrementOp();
+      bool isPre = e->isPrefix();
+
+      // Bools are always set-to-true, as decrement isn't legal on bools.
+      if (valType->isBooleanType()) {
+        assert(isInc);
+        // Atomic operations require an integer type; reinterpret the bool
+        // pointer as a pointer to its underlying storage integer type.
+        cir::IntType intTy = builder.getUInt8Ty();
+        mlir::Value one = builder.getConstInt(loc, intTy, 1);
+        Address intAddr = lv.getAddress().withElementType(builder, intTy);
+
+        if (isPre) {
+          // Pre-increment: atomically store true, return true.
+          cir::StoreOp store = builder.createStore(loc, one, intAddr);
+          store.setMemOrder(cir::MemOrder::SequentiallyConsistent);
+          if (lv.isVolatileQualified())
+            store.setIsVolatile(true);
+          return builder.getTrue(loc);
+        }
+        // Post-increment: atomically exchange with true, return old value.
+        auto xchg = cir::AtomicXchgOp::create(
+            builder, loc, intAddr.getPointer(), one,
+            cir::MemOrder::SequentiallyConsistent, cir::SyncScopeKind::System,
+            lv.isVolatileQualified());
+        return builder.createCast(cir::CastKind::int_to_bool, xchg.getResult(),
+                                  builder.getBoolTy());
+      }
+
+      // Special case for atomic increment / decrement on integers, emit
+      // atomicrmw instructions.  We skip this if we want to be doing overflow
+      // checking, and fall into the slow path with the atomic cmpxchg loop.
+      if (!valType->isBooleanType() && valType->isIntegerType() &&
+          !(valType->isUnsignedIntegerType() &&
+            cgf.sanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
+          cgf.getLangOpts().getSignedOverflowBehavior() !=
+              LangOptions::SOB_Trapping) {
+        mlir::Type intTy = cgf.convertType(valType);
+        mlir::Value one = builder.getConstInt(loc, intTy, 1);
+        cir::AtomicFetchKind kind =
+            isInc ? cir::AtomicFetchKind::Add : cir::AtomicFetchKind::Sub;
+        auto rmw = cir::AtomicFetchOp::create(
+            builder, loc, lv.getPointer(), one, kind,
+            cir::MemOrder::SequentiallyConsistent, cir::SyncScopeKind::System,
+            lv.isVolatileQualified(), /*fetch_first=*/true);
+        mlir::Value oldVal = rmw->getResult(0);
+        // Prefix returns new value; postfix returns old value.
+        return isPre ? emitIncOrDec(e, oldVal) : oldVal;
+      }
+
+      // Special case for atomic increment/decrement on floats.
+      // Bail out non-power-of-2-sized floating point types (e.g., x86_fp80).
+      if (valType->isFloatingType()) {
+        CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
+        mlir::Type fpTy = cgf.convertType(valType);
+        auto fpType = mlir::cast<cir::FPTypeInterface>(fpTy);
+        // Bail on non-power-of-2 types (e.g., x86_fp80 is 80 bits).
+        if (llvm::has_single_bit(fpType.getWidth())) {
+          mlir::Value amt = builder.getConstFP(
+              loc, fpTy, llvm::APFloat(fpType.getFloatSemantics(), 1));
+          cir::AtomicFetchKind kind =
+              isInc ? cir::AtomicFetchKind::Add : cir::AtomicFetchKind::Sub;
+          auto rmw = cir::AtomicFetchOp::create(
+              builder, loc, lv.getPointer(), amt, kind,
+              cir::MemOrder::SequentiallyConsistent, cir::SyncScopeKind::System,
+              lv.isVolatileQualified(), /*fetch_first=*/true);
+          mlir::Value oldVal = rmw->getResult(0);
+          if (isPre)
+            return isInc ? builder.createFAdd(loc, oldVal, amt)
+                         : builder.createFSub(loc, oldVal, amt);
+          return oldVal;
+        } else {
+          cgf.cgm.errorNYI(e->getSourceRange(),
+                           "Atomic inc/dec of non-power-of-2 float");
----------------
andykaylor wrote:

This is going to fall through and give us two NYI diagnostics. I guess that's OK though.

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


More information about the cfe-commits mailing list