[llvm] [AMDGPU][GFX1250] Optimize s_wait_xcnt for back-to-back atomic RMWs (PR #177620)

Christudasan Devadasan via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 23 09:46:31 PST 2026


================
@@ -2866,6 +2883,39 @@ void SIInsertWaitcnts::setSchedulingMode(MachineBasicBlock &MBB,
       .addImm(EncodedReg);
 }
 
+// Track back-to-back atomic RMW instructions, referred to as a block.
+//
+// Determines whether \p MI starts a new atomic RMW block, is inside
+// an existing block, or is outside of a block. A block is broken when a
+// CU-scoped memory op or an atomic store is encountered. ALU ops
+// and non-memory instructions don't break a block. The function returns
+// the new state after processing the current instruction based on
+// \p PrevState, the previously captured state.
+AtomicRMWState
+SIInsertWaitcnts::getAtomicRMWState(MachineInstr &MI,
+                                    AtomicRMWState PrevState) const {
+  if (isAtomicRMW(MI)) {
+    // Transition from NotInBlock -> NewBlock -> InsideBlock.
+    if (PrevState == AtomicRMWState::NotInBlock)
+      return AtomicRMWState::NewBlock;
+    if (PrevState == AtomicRMWState::NewBlock)
+      return AtomicRMWState::InsideBlock;
+
+    return PrevState;
+  }
+
+  // LDS memory operations don't break the block.
+  if (TII->isDS(MI) || (TII->isFLAT(MI) && TII->mayAccessLDSThroughFlat(MI)))
+    return PrevState;
+
+  // Reset the atomic RMW block state when found other VMEM and SMEM operations.
+  if (MI.mayLoad() ^ MI.mayStore())
----------------
cdevadas wrote:

The LDS operations (except the async-copy-to-LDS) don't break the block. The early return would prevent that. Also, is this logic sufficient to capture the remaining memory operations that could break the block? The atomic stores, VMEM, SMEM, and volatile load/store are all covered by it. However, this condition may be overly conservative and catch some operations that won't break the atomic RMW block. Should I expand this check to specifically include the category I listed above?

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


More information about the llvm-commits mailing list