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

Yonah Goldberg via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 16:52:57 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);
----------------
YonahGoldberg wrote:

@arsenm I tried extracting all the properties needed by different backends and it would've worked well, but it looks like `X86ISelLowering.cpp` does some complicated logic with matching on different instructions in the same BB as the AtomicRMW. Take a look at `X86TargetLowering::shouldExpandLogicAtomicRMWInIR`, which gets called by `X86TargetLowering::shouldExpandAtomicRMWInIR`.

I was originally going to change the API to take in a descriptor struct that holds all the info needed to make the decision:

```
  /// Describes the properties of an atomicrmw relevant for expansion decisions.
  struct AtomicRMWDesc {
    AtomicRMWInst::BinOp Op;
    Type *Ty;
    const DataLayout &DL;
    unsigned AS;
    bool IsFP;
    SyncScope::ID SSID;
    bool HasUses;
    
    // These ones are additionally needed by X86
    bool HasOneUse;
    Value *ValOperand;
    const Instruction *SingleUser;
    const BasicBlock *ParentBB;
    const Value *Result;

    explicit AtomicRMWDesc(const AtomicRMWInst *RMW)
        : Op(RMW->getOperation()), Ty(RMW->getType()),
          DL(RMW->getFunction()->getDataLayout()),
          AS(RMW->getPointerAddressSpace()),
          IsFP(RMW->isFloatingPointOperation()),
          SSID(RMW->getSyncScopeID()),
          HasUses(!RMW->use_empty()),
          HasOneUse(RMW->hasOneUse()),
          ValOperand(RMW->getValOperand()),
          SingleUser(RMW->hasOneUse()
                         ? cast<Instruction>(RMW->user_back())
                         : nullptr),
          ParentBB(RMW->getParent()),
          Result(RMW) {}
  };
  
  virtual AtomicExpansionKind
  shouldExpandAtomicRMWInIR(const AtomicRMWDesc &Desc) const {
    return Desc.IsFP ? AtomicExpansionKind::CmpXChg
                     : AtomicExpansionKind::None;
  }
  ```
  
 But because of all the things needed by X86, it's ugly.
 I think there's no clean way to do this without passing in the actual AtomicRMWInst.
 
 So I could change the struct to look something like:
 
 ```
  /// Describes the properties of an atomicrmw relevant for expansion decisions.
  struct AtomicRMWDesc {
    AtomicRMWInst *AI;
    bool IsElementwise;

    explicit AtomicRMWDesc(const AtomicRMWInst *RMW)
        : AI(RMW), IsElementwise(AI->IsElementwise())
  };
  ```
  
  And then `AtomicExpandPass` could do something like:
  
  ```
  auto Desc = AtomicRMWDesc(AI);
  Desc.IsElementwise = false;
  // test out if we can remove the elementwise
  ```
  
  And then have targets look at `desc.IsElementwise` instead of `desc.AI->isElementwise()`
  
  But I'm not really convinced this is nicer than just mutating the instruction. But if this is what you want I can do this.

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


More information about the llvm-commits mailing list