[llvm] [IR][AtomicExpand] Add elementwise modifier to atomicrmw; automatically expand for all targets (PR #189517)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 15 06:45:27 PDT 2026


================
@@ -265,6 +268,23 @@ static bool atomicSizeSupported(const TargetLowering *TLI, Inst *I) {
          Size <= TLI->getMaxAtomicSizeInBitsSupported() / 8;
 }
 
+/// Returns true if we can lower atomicrmw elementwise using normal atomicrmw.
+bool AtomicExpandImpl::canReuseWholeValueAtomicRMW(AtomicRMWInst *AI) {
+  assert(AI->isElementwise() && "expected elementwise atomicrmw");
+
+  // Integer non-elementwise vector atomicrmw is illegal IR, so we need to be
+  // careful to reject these before removing the elementwise modifier.
+  if (!AI->isFloatingPointOperation())
+    return false;
+
+  AI->setElementwise(false);
----------------
antoniofrighetto wrote:

I agree with @arsenm, temporarily mutating the instruction to query TLI slightly hints at a not clean design. One way would be indeed to create a new temporary instruction, or query TLI through the clone()'d one, with the flag cleared. Though, following up on previous Matt's suggestion, why couldn't we just have a stateless default overload like:
```cpp
virtual AtomicExpansionKind
shouldExpandAtomicRMWInIR(AtomicRMWInst::BinOp Op, Type *ValTy,
                          unsigned AddrSpace, Align Alignment,
                          AtomicOrdering Ordering,
                          SyncScope::ID SSID) const {
  return AtomicRMWInst::isFPOperation(Op)
             ? AtomicExpansionKind::CmpXChg
             : AtomicExpansionKind::None;
}
```
Then the existing `shouldExpandAtomicRMWInIR(const AtomicRMWInst *RMW)` overload would delegate to the stateless one. This would simplify `canReuseWholeValueAtomicRMW` (no setElementwise false/true), as well as the check in `processAtomicInstr`, since the flag would live entirely out of TLI (perhaps we may want to have a `shouldExpandAtomicRMWElementwiseInIR` in the future, say a target want to preserve elementwise for native lowering?).

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


More information about the llvm-commits mailing list