[flang-commits] [flang] [llvm] [mlir] [flang][OpenMP] Support for "atomic compare capture" (PR #202315)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Thu Jun 25 07:04:33 PDT 2026


================
@@ -5527,6 +5525,25 @@ convertOmpAtomicCapture(omp::AtomicCaptureOp atomicCaptureOp,
       return failure();
 
     builder.restoreIP(*afterIP);
+
+    // Postfix: v = select(success, D, old) — captures new value of x.
+    if (isPostfixCapture && !isFailOnly) {
+      llvm::BasicBlock *curBB = builder.GetInsertBlock();
+      llvm::Instruction *cmpxchgInst = nullptr;
+      for (auto &inst : llvm::reverse(*curBB)) {
+        if (isa<llvm::AtomicCmpXchgInst>(&inst)) {
+          cmpxchgInst = &inst;
+          break;
+        }
+      }
+      assert(cmpxchgInst && "expected cmpxchg instruction");
----------------
tblah wrote:

Codex found a case where this is reachable:
```
llvm.func @omp_atomic_compare_capture_postfix_float(%x : !llvm.ptr, %v : !llvm.ptr, %e : f32, %d : f32) {
  omp.atomic.capture {
    omp.atomic.compare %x : !llvm.ptr {
    ^bb0(%xval : f32):
      %cmp = llvm.fcmp "oeq" %xval, %e : f32
      %sel = llvm.select %cmp, %d, %xval : i1, f32
      omp.yield(%sel : f32)
    }
    omp.atomic.read %v = %x : !llvm.ptr, !llvm.ptr, f32
  }
  llvm.return
}
```

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


More information about the flang-commits mailing list