[llvm] [AtomicExpandPass][NFC] Refactor processAtomicInstr to be more readable (PR #186547)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 25 03:02:01 PDT 2026


================
@@ -275,10 +282,25 @@ bool AtomicExpandImpl::processAtomicInstr(Instruction *I) {
 
     if (TLI->shouldCastAtomicLoadInIR(LI) ==
         TargetLoweringBase::AtomicExpansionKind::CastToInteger) {
-      I = LI = convertAtomicLoadToIntegerType(LI);
+      LI = convertAtomicLoadToIntegerType(LI);
       MadeChange = true;
     }
-  } else if (SI) {
+
+    bool ShouldInsertFences = TLI->shouldInsertFencesForAtomic(LI);
+    if (isAcquireOrStronger(LI->getOrdering()) && ShouldInsertFences) {
+      AtomicOrdering FenceOrdering = LI->getOrdering();
+      LI->setOrdering(AtomicOrdering::Monotonic);
+      MadeChange |= bracketInstWithFences(LI, FenceOrdering);
+    } else if (!ShouldInsertFences) {
+      MadeChange |= tryInsertTrailingSeqCstFence(LI);
+    }
----------------
antoniofrighetto wrote:

The fence emission shares quite of a similar logic among stores, loads and atomic RMWs. While at it, would it make sense to have a dedicated lambda for that, something like:
```cpp
auto TryEmitFences = [&](auto *Inst, bool OrderingNeedFence,
                         AtomicOrdering NewOrder) {
    bool ShouldInsert = TLI->shouldInsertFencesForAtomic(Inst);
    if (OrderingNeedFence && ShouldInsert) {
      AtomicOrdering FenceOrdering = Inst->getOrdering();
      Inst->setOrdering(NewOrder);
      return bracketInstWithFences(Inst, FenceOrdering);
    }
    if (!ShouldInsert)
      return tryInsertTrailingSeqCstFence(Inst);
    return false;
  };
```
Then the caller for, e.g., the load, would become:
```cpp
MadeChange |= TryEmitFences(
        LI, isAcquireOrStronger(LI->getOrdering()),
        AtomicOrdering::Monotonic);
```
(Making also the code a bit more readable.)

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


More information about the llvm-commits mailing list