Skip to content

[AMDGPU][SIInsertWaitcnts] Fix soft wait removal with loop-carried deps - #220357

Merged
krzysz00 merged 2 commits into
mainfrom
users/krzysz00/loop-carried-waits-miscompile
Sep 2, 2026
Merged

krzysz00 merged 2 commits into
mainfrom
users/krzysz00/loop-carried-waits-miscompile

Conversation

@krzysz00

@krzysz00 krzysz00 commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

The code that checked if a soft waitcnt (such as the one insterted by
a release fence on LDS) was redundant didn't correctly account for the
fact that that, for example, the previous iteration of a loop could
have introduced memory traffic that needs to be waited on. This bug
appeared to be fairly rare in practice (probably due to the
instruction scheduler shuffling around code in t bad form) but it can
happen.

The fix is that, instad of immediately erasing "redundant" waits, we
add them to a set of waits to be erased, and then remove them from the
set if they prove to be truly redundant.

This has the side effect of fixing a correctness issue around the CAS
loops we emit on gfx1250 - the global_inv we emit after the
s_loadcnt 0x0 is itself a loadcnt-able event, and so needs to be
forced to completion before the next iteration of the CAS loop.

AI disclosure: Claude came up with the overall shape of the solution
after a few pokes, but I've looked it over and reviewed off the worst
of the problems.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

@github-actions

github-actions Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@llvmorg-github-actions

llvmorg-github-actions Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-globalisel

@llvm/pr-subscribers-backend-amdgpu

Author: Krzysztof Drewniak (krzysz00)

Changes

The code that checked if a soft waitcnt (such as the one insterted by
a release fence on LDS) was redundant didn't correctly account for the
fact that that, for example, the previous iteration of a loop could
have introduced memory traffic that needs to be waited on. This bug
appeared to be fairly rare in practice (probably due to the
instruction scheduler shuffling around code in t bad form) but it can
happen.

The fix is that, instad of immediately erasing "redundant" waits, we
add them to a set of waits to be erased, and then remove them from the
set if they prove to be truly redundant.

This has the side effect of fixing a correctness issue around the CAS
loops we emit on gfx1250 - the global_inv we emit after the
s_loadcnt 0x0 is itself a loadcnt-able event, and so needs to be
forced to completion before the next iteration of the CAS loop.

AI disclosure: Claude came up with the overall shape of the solution
after a few pokes, but I've looked it over and reviewed off the worst
of the problems.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>


<sub>Stack created with <a href="https://github.com/github/gh-stack"&gt;GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback"&gt;Give Feedback 💬</a></sub>


Patch is 90.54 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/220357.diff

14 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp (+43-11)
  • (modified) llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmax.ll (+4-2)
  • (modified) llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmin.ll (+4-2)
  • (modified) llvm/test/CodeGen/AMDGPU/atomicrmw_usub_sat.ll (+8)
  • (modified) llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fmax.ll (+20-8)
  • (modified) llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fmin.ll (+20-8)
  • (modified) llvm/test/CodeGen/AMDGPU/insert-waitcnts-gfx12-wbinv.mir (+1)
  • (modified) llvm/test/CodeGen/AMDGPU/insert_waitcnt_for_precise_memory.ll (+1)
  • (modified) llvm/test/CodeGen/AMDGPU/local-atomicrmw-fadd.ll (+23)
  • (modified) llvm/test/CodeGen/AMDGPU/local-atomicrmw-fmax.ll (+31)
  • (modified) llvm/test/CodeGen/AMDGPU/local-atomicrmw-fmin.ll (+31)
  • (modified) llvm/test/CodeGen/AMDGPU/local-atomicrmw-fsub.ll (+41)
  • (modified) llvm/test/CodeGen/AMDGPU/waitcnt-loop-carried-fence-drain-gfx12.mir (+1)
  • (modified) llvm/test/CodeGen/AMDGPU/waitcnt-loop-carried-fence-drain.mir (+1)
diff --git a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
index e4f40b0018eb1..22639230deeba 100644
--- a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
@@ -162,6 +162,10 @@ class WaitcntGenerator {
   bool ExpandWaitcntProfiling = false;
   const AMDGPU::HardwareLimits &Limits;
 
+  // Soft waitcnts whose erasures are postponed so we can confirm if they're
+  // truly redundant or if they're waiting on a loop-carried dependency.
+  DenseSet<MachineInstr *> DeferredErasures;
+
 public:
   WaitcntGenerator() = delete;
   WaitcntGenerator(const WaitcntGenerator &) = delete;
@@ -196,7 +200,17 @@ class WaitcntGenerator {
   virtual bool
   applyPreexistingWaitcnt(WaitcntBrackets &ScoreBrackets,
                           MachineInstr &OldWaitcntInstr, AMDGPU::Waitcnt &Wait,
-                          MachineBasicBlock::instr_iterator It) const = 0;
+                          MachineBasicBlock::instr_iterator It) = 0;
+
+  bool eraseDeferredWaitcnts() {
+    bool Modified = false;
+    for (MachineInstr *MI : DeferredErasures) {
+      MI->eraseFromParent();
+      Modified = true;
+    }
+    DeferredErasures.clear();
+    return Modified;
+  }
 
   // Transform a soft waitcnt into a normal one.
   bool promoteSoftWaitCnt(MachineInstr *Waitcnt) const;
@@ -258,7 +272,7 @@ class WaitcntGeneratorPreGFX12 final : public WaitcntGenerator {
   bool
   applyPreexistingWaitcnt(WaitcntBrackets &ScoreBrackets,
                           MachineInstr &OldWaitcntInstr, AMDGPU::Waitcnt &Wait,
-                          MachineBasicBlock::instr_iterator It) const override;
+                          MachineBasicBlock::instr_iterator It) override;
 
   bool createNewWaitcnt(MachineBasicBlock &Block,
                         MachineBasicBlock::instr_iterator It,
@@ -312,7 +326,7 @@ class WaitcntGeneratorGFX12Plus final : public WaitcntGenerator {
   bool
   applyPreexistingWaitcnt(WaitcntBrackets &ScoreBrackets,
                           MachineInstr &OldWaitcntInstr, AMDGPU::Waitcnt &Wait,
-                          MachineBasicBlock::instr_iterator It) const override;
+                          MachineBasicBlock::instr_iterator It) override;
 
   bool createNewWaitcnt(MachineBasicBlock &Block,
                         MachineBasicBlock::instr_iterator It,
@@ -1633,7 +1647,7 @@ bool WaitcntGenerator::promoteSoftWaitCnt(MachineInstr *Waitcnt) const {
 /// correctness.
 bool WaitcntGeneratorPreGFX12::applyPreexistingWaitcnt(
     WaitcntBrackets &ScoreBrackets, MachineInstr &OldWaitcntInstr,
-    AMDGPU::Waitcnt &Wait, MachineBasicBlock::instr_iterator It) const {
+    AMDGPU::Waitcnt &Wait, MachineBasicBlock::instr_iterator It) {
   assert(isNormalMode(MaxCounter));
 
   bool Modified = false;
@@ -1669,11 +1683,19 @@ bool WaitcntGeneratorPreGFX12::applyPreexistingWaitcnt(
       Wait = Wait.combined(OldWait);
 
       // Merge consecutive waitcnt of the same type by erasing multiples.
-      if (WaitcntInstr || (!Wait.hasWaitExceptStoreCnt() && TrySimplify)) {
+      if (WaitcntInstr) {
+        DeferredErasures.erase(&II);
         II.eraseFromParent();
         Modified = true;
-      } else
+      } else if (!Wait.hasWaitExceptStoreCnt() && TrySimplify) {
+        // Might or might not be redundant, depending on if there's a loop
+        // carried dependency. Stash it away so we can erase it if nothing saves
+        // it.
+        DeferredErasures.insert(&II);
+      } else {
         WaitcntInstr = &II;
+        DeferredErasures.erase(&II);
+      }
     } else if (Opcode == AMDGPU::S_WAITCNT_lds_direct) {
       assert(ST.hasVMemToLDSLoad());
       LLVM_DEBUG(dbgs() << "Processing S_WAITCNT_lds_direct: " << II
@@ -1705,11 +1727,17 @@ bool WaitcntGeneratorPreGFX12::applyPreexistingWaitcnt(
       Wait.set(AMDGPU::STORE_CNT,
                std::min(Wait.get(AMDGPU::STORE_CNT), OldVSCnt));
 
-      if (WaitcntVsCntInstr || (!Wait.hasWaitStoreCnt() && TrySimplify)) {
+      if (WaitcntVsCntInstr) {
+        DeferredErasures.erase(&II);
         II.eraseFromParent();
         Modified = true;
-      } else
+      } else if (!Wait.hasWaitStoreCnt() && TrySimplify) {
+        // Loop-carried dependencies guard, see S_WAITCNT case above.
+        DeferredErasures.insert(&II);
+      } else {
         WaitcntVsCntInstr = &II;
+        DeferredErasures.erase(&II);
+      }
     }
   }
 
@@ -1868,7 +1896,7 @@ WaitcntGeneratorGFX12Plus::getAllZeroWaitcnt(bool IncludeVSCnt) const {
 /// assumes that these preexisting waits are required for correctness.
 bool WaitcntGeneratorGFX12Plus::applyPreexistingWaitcnt(
     WaitcntBrackets &ScoreBrackets, MachineInstr &OldWaitcntInstr,
-    AMDGPU::Waitcnt &Wait, MachineBasicBlock::instr_iterator It) const {
+    AMDGPU::Waitcnt &Wait, MachineBasicBlock::instr_iterator It) {
   assert(!isNormalMode(MaxCounter));
 
   bool Modified = false;
@@ -2094,6 +2122,7 @@ bool WaitcntGeneratorGFX12Plus::applyPreexistingWaitcnt(
       if (!*WI)
         continue;
 
+      DeferredErasures.erase(*WI);
       (*WI)->eraseFromParent();
       *WI = nullptr;
       Modified = true;
@@ -2109,6 +2138,7 @@ bool WaitcntGeneratorGFX12Plus::applyPreexistingWaitcnt(
       Modified |= updateOperandIfDifferent(*WaitInstrs[CT],
                                            AMDGPU::OpName::simm16, NewCnt);
       Modified |= promoteSoftWaitCnt(WaitInstrs[CT]);
+      DeferredErasures.erase(WaitInstrs[CT]);
 
       ScoreBrackets.applyWaitcnt(CT, NewCnt);
       Wait.clear(CT);
@@ -2121,8 +2151,8 @@ bool WaitcntGeneratorGFX12Plus::applyPreexistingWaitcnt(
                               << "Old Instr: " << *It
                               << "New Instr: " << *WaitInstrs[CT] << '\n');
     } else {
-      WaitInstrs[CT]->eraseFromParent();
-      Modified = true;
+      // Defer erasure in case there's a loop-carried dependency.
+      DeferredErasures.insert(WaitInstrs[CT]);
     }
   }
 
@@ -3599,6 +3629,8 @@ bool SIInsertWaitcnts::run() {
     }
   } while (Repeat);
 
+  Modified |= WCG->eraseDeferredWaitcnts();
+
   if (ST.hasScalarStores()) {
     SmallVector<MachineBasicBlock *, 4> EndPgmBlocks;
     bool HaveScalarStores = false;
diff --git a/llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmax.ll b/llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmax.ll
index d62546e3de255..5444a36a542cb 100644
--- a/llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmax.ll
+++ b/llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmax.ll
@@ -1834,8 +1834,9 @@ define double @buffer_fat_ptr_agent_atomic_fmax_ret_f64__amdgpu_no_fine_grained_
 ; GFX12-NEXT:    s_mov_b32 s4, 0
 ; GFX12-NEXT:  .LBB14_1: ; %atomicrmw.start
 ; GFX12-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-NEXT:    v_max_num_f64_e32 v[0:1], v[4:5], v[4:5]
+; GFX12-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-NEXT:    s_wait_storecnt 0x0
 ; GFX12-NEXT:    v_max_num_f64_e32 v[2:3], v[0:1], v[6:7]
 ; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1)
@@ -2020,8 +2021,9 @@ define void @buffer_fat_ptr_agent_atomic_fmax_noret_f64__amdgpu_no_fine_grained_
 ; GFX12-NEXT:    s_mov_b32 s4, 0
 ; GFX12-NEXT:  .LBB15_1: ; %atomicrmw.start
 ; GFX12-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-NEXT:    v_max_num_f64_e32 v[0:1], v[2:3], v[2:3]
+; GFX12-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-NEXT:    s_wait_storecnt 0x0
 ; GFX12-NEXT:    v_max_num_f64_e32 v[0:1], v[0:1], v[4:5]
 ; GFX12-NEXT:    v_dual_mov_b32 v10, v3 :: v_dual_mov_b32 v9, v2
diff --git a/llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmin.ll b/llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmin.ll
index 0530f5e69456b..508e8e1da7b5e 100644
--- a/llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmin.ll
+++ b/llvm/test/CodeGen/AMDGPU/GlobalISel/atomicrmw_fmin.ll
@@ -1834,8 +1834,9 @@ define double @buffer_fat_ptr_agent_atomic_fmin_ret_f64__amdgpu_no_fine_grained_
 ; GFX12-NEXT:    s_mov_b32 s4, 0
 ; GFX12-NEXT:  .LBB14_1: ; %atomicrmw.start
 ; GFX12-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-NEXT:    v_max_num_f64_e32 v[0:1], v[4:5], v[4:5]
+; GFX12-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-NEXT:    s_wait_storecnt 0x0
 ; GFX12-NEXT:    v_min_num_f64_e32 v[2:3], v[0:1], v[6:7]
 ; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1)
@@ -2020,8 +2021,9 @@ define void @buffer_fat_ptr_agent_atomic_fmin_noret_f64__amdgpu_no_fine_grained_
 ; GFX12-NEXT:    s_mov_b32 s4, 0
 ; GFX12-NEXT:  .LBB15_1: ; %atomicrmw.start
 ; GFX12-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-NEXT:    v_max_num_f64_e32 v[0:1], v[2:3], v[2:3]
+; GFX12-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-NEXT:    s_wait_storecnt 0x0
 ; GFX12-NEXT:    v_min_num_f64_e32 v[0:1], v[0:1], v[4:5]
 ; GFX12-NEXT:    v_dual_mov_b32 v10, v3 :: v_dual_mov_b32 v9, v2
diff --git a/llvm/test/CodeGen/AMDGPU/atomicrmw_usub_sat.ll b/llvm/test/CodeGen/AMDGPU/atomicrmw_usub_sat.ll
index eb9cc4a7b524d..d1a7aa6d8d29d 100644
--- a/llvm/test/CodeGen/AMDGPU/atomicrmw_usub_sat.ll
+++ b/llvm/test/CodeGen/AMDGPU/atomicrmw_usub_sat.ll
@@ -1793,6 +1793,7 @@ define amdgpu_kernel void @global_atomic_usub_sat_sgpr_base_offset_16(ptr addrsp
 ; GFX12-GISEL-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
 ; GFX12-GISEL-NEXT:    v_cvt_u32_u16_e32 v1, v1.l
 ; GFX12-GISEL-NEXT:    v_and_or_b32 v1, 0xffff0000, v2, v1
+; GFX12-GISEL-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-GISEL-NEXT:    global_atomic_cmpswap_b32 v1, v0, v[1:2], s[0:1] offset:2048 th:TH_ATOMIC_RETURN scope:SCOPE_DEV
 ; GFX12-GISEL-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-GISEL-NEXT:    global_inv scope:SCOPE_DEV
@@ -1929,6 +1930,7 @@ define amdgpu_kernel void @global_atomic_usub_sat_sgpr_base_offset_16(ptr addrsp
 ; GFX12-SDAG-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
 ; GFX12-SDAG-NEXT:    v_cvt_u32_u16_e32 v1, v1.l
 ; GFX12-SDAG-NEXT:    v_and_or_b32 v1, 0xffff0000, v2, v1
+; GFX12-SDAG-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-SDAG-NEXT:    global_atomic_cmpswap_b32 v1, v0, v[1:2], s[0:1] offset:2048 th:TH_ATOMIC_RETURN scope:SCOPE_DEV
 ; GFX12-SDAG-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-SDAG-NEXT:    global_inv scope:SCOPE_DEV
@@ -2051,6 +2053,7 @@ define amdgpu_kernel void @global_atomic_usub_sat_sgpr_base_offset_nortn_16(ptr
 ; GFX12-GISEL-NEXT:    v_cvt_u32_u16_e32 v0, v0.l
 ; GFX12-GISEL-NEXT:    s_delay_alu instid0(VALU_DEP_1)
 ; GFX12-GISEL-NEXT:    v_and_or_b32 v0, 0xffff0000, v1, v0
+; GFX12-GISEL-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-GISEL-NEXT:    global_atomic_cmpswap_b32 v0, v2, v[0:1], s[0:1] offset:2048 th:TH_ATOMIC_RETURN scope:SCOPE_DEV
 ; GFX12-GISEL-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-GISEL-NEXT:    global_inv scope:SCOPE_DEV
@@ -2167,6 +2170,7 @@ define amdgpu_kernel void @global_atomic_usub_sat_sgpr_base_offset_nortn_16(ptr
 ; GFX12-SDAG-NEXT:    v_cvt_u32_u16_e32 v0, v0.l
 ; GFX12-SDAG-NEXT:    s_delay_alu instid0(VALU_DEP_1)
 ; GFX12-SDAG-NEXT:    v_and_or_b32 v0, 0xffff0000, v1, v0
+; GFX12-SDAG-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-SDAG-NEXT:    global_atomic_cmpswap_b32 v0, v2, v[0:1], s[0:1] offset:2048 th:TH_ATOMIC_RETURN scope:SCOPE_DEV
 ; GFX12-SDAG-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-SDAG-NEXT:    global_inv scope:SCOPE_DEV
@@ -3301,6 +3305,7 @@ define amdgpu_kernel void @global_atomic_usub_sat_sgpr_base_offset_8(ptr addrspa
 ; GFX12-GISEL-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
 ; GFX12-GISEL-NEXT:    v_and_b32_e32 v1, 0xff, v1
 ; GFX12-GISEL-NEXT:    v_and_or_b32 v1, 0xffffff00, v2, v1
+; GFX12-GISEL-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-GISEL-NEXT:    global_atomic_cmpswap_b32 v1, v0, v[1:2], s[0:1] offset:1024 th:TH_ATOMIC_RETURN scope:SCOPE_DEV
 ; GFX12-GISEL-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-GISEL-NEXT:    global_inv scope:SCOPE_DEV
@@ -3444,6 +3449,7 @@ define amdgpu_kernel void @global_atomic_usub_sat_sgpr_base_offset_8(ptr addrspa
 ; GFX12-SDAG-NEXT:    v_cvt_u32_u16_e32 v1, v1.l
 ; GFX12-SDAG-NEXT:    s_delay_alu instid0(VALU_DEP_1)
 ; GFX12-SDAG-NEXT:    v_and_or_b32 v1, 0xffffff00, v2, v1
+; GFX12-SDAG-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-SDAG-NEXT:    global_atomic_cmpswap_b32 v1, v0, v[1:2], s[0:1] offset:1024 th:TH_ATOMIC_RETURN scope:SCOPE_DEV
 ; GFX12-SDAG-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-SDAG-NEXT:    global_inv scope:SCOPE_DEV
@@ -3584,6 +3590,7 @@ define amdgpu_kernel void @global_atomic_usub_sat_sgpr_base_offset_nortn_8(ptr a
 ; GFX12-GISEL-NEXT:    v_and_b32_e32 v0, 0xff, v0
 ; GFX12-GISEL-NEXT:    s_delay_alu instid0(VALU_DEP_1)
 ; GFX12-GISEL-NEXT:    v_and_or_b32 v0, 0xffffff00, v1, v0
+; GFX12-GISEL-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-GISEL-NEXT:    global_atomic_cmpswap_b32 v0, v2, v[0:1], s[0:1] offset:1024 th:TH_ATOMIC_RETURN scope:SCOPE_DEV
 ; GFX12-GISEL-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-GISEL-NEXT:    global_inv scope:SCOPE_DEV
@@ -3705,6 +3712,7 @@ define amdgpu_kernel void @global_atomic_usub_sat_sgpr_base_offset_nortn_8(ptr a
 ; GFX12-SDAG-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
 ; GFX12-SDAG-NEXT:    v_cvt_u32_u16_e32 v0, v0.l
 ; GFX12-SDAG-NEXT:    v_and_or_b32 v0, 0xffffff00, v1, v0
+; GFX12-SDAG-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-SDAG-NEXT:    global_atomic_cmpswap_b32 v0, v2, v[0:1], s[0:1] offset:1024 th:TH_ATOMIC_RETURN scope:SCOPE_DEV
 ; GFX12-SDAG-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-SDAG-NEXT:    global_inv scope:SCOPE_DEV
diff --git a/llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fmax.ll b/llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fmax.ll
index cecc14be3c521..fbfbf7249f2fd 100644
--- a/llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fmax.ll
+++ b/llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fmax.ll
@@ -5615,8 +5615,9 @@ define <2 x half> @buffer_fat_ptr_agent_atomic_fmax_ret_v2f16__offset__amdgpu_no
 ; GFX12-TRUE16-NEXT:    v_mov_b32_e32 v3, s16
 ; GFX12-TRUE16-NEXT:  .LBB16_1: ; %atomicrmw.start
 ; GFX12-TRUE16-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-TRUE16-NEXT:    v_mov_b32_e32 v5, v0
+; GFX12-TRUE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-TRUE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-TRUE16-NEXT:    v_pk_max_num_f16 v0, v5, v5
 ; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
@@ -5655,8 +5656,9 @@ define <2 x half> @buffer_fat_ptr_agent_atomic_fmax_ret_v2f16__offset__amdgpu_no
 ; GFX12-FAKE16-NEXT:    v_mov_b32_e32 v3, s16
 ; GFX12-FAKE16-NEXT:  .LBB16_1: ; %atomicrmw.start
 ; GFX12-FAKE16-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-FAKE16-NEXT:    v_mov_b32_e32 v5, v0
+; GFX12-FAKE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-FAKE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-FAKE16-NEXT:    v_pk_max_num_f16 v0, v5, v5
 ; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
@@ -6023,8 +6025,9 @@ define void @buffer_fat_ptr_agent_atomic_fmax_noret_v2f16__offset__amdgpu_no_fin
 ; GFX12-TRUE16-NEXT:    v_mov_b32_e32 v3, s16
 ; GFX12-TRUE16-NEXT:  .LBB17_1: ; %atomicrmw.start
 ; GFX12-TRUE16-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-TRUE16-NEXT:    v_pk_max_num_f16 v0, v1, v1
+; GFX12-TRUE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-TRUE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-TRUE16-NEXT:    v_pk_max_num_f16 v0, v0, v2
 ; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_1)
@@ -6063,8 +6066,9 @@ define void @buffer_fat_ptr_agent_atomic_fmax_noret_v2f16__offset__amdgpu_no_fin
 ; GFX12-FAKE16-NEXT:    v_mov_b32_e32 v3, s16
 ; GFX12-FAKE16-NEXT:  .LBB17_1: ; %atomicrmw.start
 ; GFX12-FAKE16-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-FAKE16-NEXT:    v_pk_max_num_f16 v0, v1, v1
+; GFX12-FAKE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-FAKE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-FAKE16-NEXT:    v_pk_max_num_f16 v0, v0, v2
 ; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_1)
@@ -6461,6 +6465,7 @@ define <2 x half> @buffer_fat_ptr_agent_atomic_fmax_ret_v2f16__offset__waterfall
 ; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2)
 ; GFX12-TRUE16-NEXT:    v_pk_max_num_f16 v5, v7, v7
 ; GFX12-TRUE16-NEXT:    s_mov_b32 s5, exec_lo
+; GFX12-TRUE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-TRUE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-TRUE16-NEXT:    s_wait_alu depctr_sa_sdst(0)
 ; GFX12-TRUE16-NEXT:    s_mov_b32 s6, s5
@@ -6550,6 +6555,7 @@ define <2 x half> @buffer_fat_ptr_agent_atomic_fmax_ret_v2f16__offset__waterfall
 ; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2)
 ; GFX12-FAKE16-NEXT:    v_pk_max_num_f16 v5, v7, v7
 ; GFX12-FAKE16-NEXT:    s_mov_b32 s5, exec_lo
+; GFX12-FAKE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-FAKE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-FAKE16-NEXT:    s_wait_alu depctr_sa_sdst(0)
 ; GFX12-FAKE16-NEXT:    s_mov_b32 s6, s5
@@ -7333,8 +7339,9 @@ define <2 x bfloat> @buffer_fat_ptr_agent_atomic_fmax_ret_v2bf16__offset__amdgpu
 ; GFX12-TRUE16-NEXT:    v_mov_b32_e32 v4, s16
 ; GFX12-TRUE16-NEXT:  .LBB19_1: ; %atomicrmw.start
 ; GFX12-TRUE16-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-TRUE16-NEXT:    v_mov_b32_e32 v6, v0
+; GFX12-TRUE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-TRUE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-TRUE16-NEXT:    v_lshlrev_b32_e32 v1, 16, v6
 ; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
@@ -7396,8 +7403,9 @@ define <2 x bfloat> @buffer_fat_ptr_agent_atomic_fmax_ret_v2bf16__offset__amdgpu
 ; GFX12-FAKE16-NEXT:    v_mov_b32_e32 v4, s16
 ; GFX12-FAKE16-NEXT:  .LBB19_1: ; %atomicrmw.start
 ; GFX12-FAKE16-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
 ; GFX12-FAKE16-NEXT:    v_mov_b32_e32 v6, v0
+; GFX12-FAKE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-FAKE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-FAKE16-NEXT:    v_and_b32_e32 v1, 0xffff0000, v6
 ; GFX12-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
@@ -7915,9 +7923,10 @@ define void @buffer_fat_ptr_agent_atomic_fmax_noret_v2bf16__offset__amdgpu_no_fi
 ; GFX12-TRUE16-NEXT:    v_mov_b32_e32 v4, s16
 ; GFX12-TRUE16-NEXT:  .LBB20_1: ; %atomicrmw.start
 ; GFX12-TRUE16-NEXT:    ; =>This Inner Loop Header: Depth=1
-; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
+; GFX12-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_1)
 ; GFX12-TRUE16-NEXT:    v_lshlrev_b32_e32 v5, 16, v1
 ; GFX12-TRUE16-NEXT:    v_and_b32_e32 v0, 0xffff0000, v1
+; GFX12-TRUE16-NEXT:    s_wait_loadcnt 0x0
 ; GFX12-TRUE16-NEXT:    s_wait_storecnt 0x0
 ; GFX12-TRUE16-NEXT:    v_dual_max_num_f32 v5, v5, v3 :: v_dual_max_num_f32 v0, v0, v2
 ; GFX12-TRUE16-NEXT:    s_delay_a...
[truncated]

@zGoldthorpe zGoldthorpe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable to me

Base automatically changed from users/krzysz00/loop-carried-waits-miscompile-precommit to main September 1, 2026 23:06
@krzysz00
krzysz00 force-pushed the users/krzysz00/loop-carried-waits-miscompile branch from 137ffad to 9e37b7d Compare September 1, 2026 23:07
krzysz00 and others added 2 commits September 1, 2026 23:07
The code that checked if a soft waitcnt (such as the one insterted by
a release fence on LDS) was redundant didn't correctly account for the
fact that that, for example, the previous iteration of a loop could
have introduced memory traffic that needs to be waited on. This bug
appeared to be fairly rare in practice (probably due to the
instruction scheduler shuffling around code in t bad form) but it can
happen.

The fix is that, instad of immediately erasing "redundant" waits, we
add them to a set of waits to be erased, and then remove them from the
set if they prove to be truly redundant.

This has the side effect of fixing a correctness issue around the CAS
loops we emit on gfx1250 - the global_inv we emit after the
`s_loadcnt 0x0` is itself a `loadcnt`-able event, and so needs to be
forced to completion before the next iteration of the CAS loop.

AI disclosure: Claude came up with the overall shape of the solution
after a few pokes, but I've looked it over and reviewed off the worst
of the problems.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@krzysz00
krzysz00 force-pushed the users/krzysz00/loop-carried-waits-miscompile branch from 9e37b7d to 56e03d6 Compare September 1, 2026 23:07
@shiltian
shiltian requested a review from jrbyrnes September 2, 2026 00:10
@krzysz00

krzysz00 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Holding until my tomorrow morning per the extra review request

@jayfoad

jayfoad commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

This sounds like an alternative to #211684? Did you talk to each other?

@krzysz00
krzysz00 merged commit 437f88f into main Sep 2, 2026
10 checks passed
@krzysz00
krzysz00 deleted the users/krzysz00/loop-carried-waits-miscompile branch September 2, 2026 18:51
@krzysz00

krzysz00 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Re the alternate PR: the person behind that's on vacation

@jayfoad

jayfoad commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Just repeating here my thoughts from the other PR about how we should handle this kind of problem:

I think we should be moving towards an analysis phase that does not make any modifications at all to the code, just analyses the existing code and iterates until it reaches a fix point. Then we can run one final pass over all blocks that actually does the modifications of removing unnecessary soft waits and generating new waits.

@Pierre-vh

Copy link
Copy Markdown
Contributor

Just repeating here my thoughts from the other PR about how we should handle this kind of problem:

I think we should be moving towards an analysis phase that does not make any modifications at all to the code, just analyses the existing code and iterates until it reaches a fix point. Then we can run one final pass over all blocks that actually does the modifications of removing unnecessary soft waits and generating new waits.

cc @vporpo

Iasonaskrpr pushed a commit to Iasonaskrpr/llvm-project that referenced this pull request Sep 4, 2026
…ps (llvm#220357)

The code that checked if a soft waitcnt (such as the one insterted by
a release fence on LDS) was redundant didn't correctly account for the
fact that that, for example, the previous iteration of a loop could
have introduced memory traffic that needs to be waited on. This bug
appeared to be fairly rare in practice (probably due to the
instruction scheduler shuffling around code in t bad form) but it can
happen.

The fix is that, instad of immediately erasing "redundant" waits, we
add them to a set of waits to be erased, and then remove them from the
set if they prove to be truly redundant.

This has the side effect of fixing a correctness issue around the CAS
loops we emit on gfx1250 - the global_inv we emit after the
`s_loadcnt 0x0` is itself a `loadcnt`-able event, and so needs to be
forced to completion before the next iteration of the CAS loop.

AI disclosure: Claude came up with the overall shape of the solution
after a few pokes, but I've looked it over and reviewed off the worst
of the problems.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
asudarsa-qti pushed a commit to asudarsa-qti/llvm-project that referenced this pull request Sep 4, 2026
…ps (llvm#220357)

The code that checked if a soft waitcnt (such as the one insterted by
a release fence on LDS) was redundant didn't correctly account for the
fact that that, for example, the previous iteration of a loop could
have introduced memory traffic that needs to be waited on. This bug
appeared to be fairly rare in practice (probably due to the
instruction scheduler shuffling around code in t bad form) but it can
happen.

The fix is that, instad of immediately erasing "redundant" waits, we
add them to a set of waits to be erased, and then remove them from the
set if they prove to be truly redundant.

This has the side effect of fixing a correctness issue around the CAS
loops we emit on gfx1250 - the global_inv we emit after the
`s_loadcnt 0x0` is itself a `loadcnt`-able event, and so needs to be
forced to completion before the next iteration of the CAS loop.

AI disclosure: Claude came up with the overall shape of the solution
after a few pokes, but I've looked it over and reviewed off the worst
of the problems.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
addmisol pushed a commit to addmisol/llvm-project2 that referenced this pull request Sep 6, 2026
…ps (llvm#220357)

The code that checked if a soft waitcnt (such as the one insterted by
a release fence on LDS) was redundant didn't correctly account for the
fact that that, for example, the previous iteration of a loop could
have introduced memory traffic that needs to be waited on. This bug
appeared to be fairly rare in practice (probably due to the
instruction scheduler shuffling around code in t bad form) but it can
happen.

The fix is that, instad of immediately erasing "redundant" waits, we
add them to a set of waits to be erased, and then remove them from the
set if they prove to be truly redundant.

This has the side effect of fixing a correctness issue around the CAS
loops we emit on gfx1250 - the global_inv we emit after the
`s_loadcnt 0x0` is itself a `loadcnt`-able event, and so needs to be
forced to completion before the next iteration of the CAS loop.

AI disclosure: Claude came up with the overall shape of the solution
after a few pokes, but I've looked it over and reviewed off the worst
of the problems.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
@krzysz00

Copy link
Copy Markdown
Contributor Author

Re @jayfoad and your thoughts on the general solution. I agree. Want me to take a stab at it?

@Pierre-vh

Copy link
Copy Markdown
Contributor

Re @jayfoad and your thoughts on the general solution. I agree. Want me to take a stab at it?

There is already a pending effort to rewrite InsertWaitcnt so let's not duplicate work (and we are quite far into the design/planning). If you have feedback/ideas, share it with me and @vporpo - feel free to start an email thread :)

Ultimately I agree in concept, though, I was wondering if we could somehow do fixpoint iteration without inserting anything, then modify all at once. I am not quite sure what that buys us though, I'd really need to see testcases. An alternative easier idea may be to track in a map somewhere the state of instructions we insert so during fixpoint iteration, we can remember if something was queued for deletion, or if it was added in a previous iteration and can be moved/removed as needed, etc.

Making the pass more modular and layered is definitely one of the biggest priorities we have.

Kaden-Schutt added a commit to warpfront/hipfire that referenced this pull request Oct 1, 2026
…201/gfx1100/gfx1151)

GS_BAR's release fence with the "local" MMRA only emits a soft LDS wait,
which SIInsertWaitcnts may erase as redundant without seeing loop-carried
stores (llvm/llvm-project#220357; the gfx1151 VerifyAttn race). Every
barrier now issues a hard wait first: s_wait_dscnt 0 on gfx12 (intrinsic
llvm.amdgcn.s.wait.dscnt bound by name), s_waitcnt 0xFC07 (lgkmcnt(0),
vmcnt/expcnt open) on gfx11. Global prefetches stay in flight.
meta-codesync Bot pushed a commit to facebookexperimental/llvm-project that referenced this pull request Oct 29, 2026
#35)

Summary:
Pull Request resolved: #35

Cherry-pick `llvm/llvm-project@437f88f` ([llvm/llvm-project#220357](llvm/llvm-project#220357), `[AMDGPU][SIInsertWaitcnts] Fix soft wait removal with loop-carried deps`) onto the Beta `llvm-project/` snapshot (base `1df311a6`).

Follows `LLVM_CHERRY_PICKING.md` Section 1 (PR landed upstream): one diff for the `llvm-project/` location, tracking entry in the same diff.

`SIInsertWaitcnts` erased a soft waitcnt (e.g. from an LDS release fence) as redundant without accounting for memory traffic from a previous loop iteration. The fix defers erasure (`DeferredErasures` / `eraseDeferredWaitcnts`) and only erases waits proven truly redundant; it also forces completion of the `global_inv` after `s_loadcnt 0x0` in the `gfx1250` CAS loop. Ported verbatim (applies with +4 line offset); the two `waitcnt-loop-carried-fence-drain*.mir` regression tests did not yet exist on this base and are added from upstream.

Records entry 11 in `llvm_cherry_picks.md`. The `llvm-project-amd-codegen/` snapshot already contains this fix via its `6bc4aaf6` base (reverse patch applies cleanly), so no amd-codegen port is needed — noted in the entry.

Reviewed By: danzimm

Differential Revision: D127837495

fbshipit-source-id: 0dd713ddb25eebff23d8c4cb2a705c16d0619ecc
meta-codesync Bot pushed a commit to facebookexperimental/llvm-project that referenced this pull request Oct 30, 2026
…#36)

Summary:
Pull Request resolved: #36

Direct port of `llvm/llvm-project@437f88f` ([llvm/llvm-project#220357](llvm/llvm-project#220357)) onto the Triton 3.8 `llvm-project/` snapshot (base `5f07f818`), without the Beta tracking process (3.8 has no `llvm_cherry_picks.md`; landed as a standalone fix on the release snapshot).

Same fix as the Beta core port in this stack: `SIInsertWaitcnts` deferred erasure of soft waitcnts (`DeferredErasures`) so a wait needed for loop-carried memory traffic is not erased as redundant, including the `gfx1250` CAS-loop `global_inv` case.

The `.cpp` applies with offsets on the older 3.8 base. Test expectations that apply cleanly are included; the two `waitcnt-loop-carried-fence-drain*.mir` regression tests are added from upstream with RUN lines adapted to the 3.8 LLVM 23 spelling (`-mtriple=amdgcn -mcpu=gfx90a/gfx1200`). `buffer-fat-pointer-*` and `local-atomicrmw-*` check-line hunks do not apply on this base (RUN-line / prefix drift) and are intentionally not forced.

Reviewed By: danzimm

Differential Revision: D127837496

fbshipit-source-id: e04a883bcfc9e0abea870f97446179b4201304bc
reble added a commit that referenced this pull request Nov 20, 2026
On the first visit to a loop header only the preheader state is known.
A soft waitcnt there (e.g. from a release fence) can have some of its
counters simplified away while another counter keeps the instruction
alive: the fence's own vmcnt(0), a preceding wait.asyncmark, or an
explicit s_waitcnt. The soft waitcnt was then rewritten with the
simplified value and promoted to a hard waitcnt, or merged into a
preceding hard one. When the back edge later brought a pending LDS
store, the original lgkmcnt(0) request was gone, and the s_barrier after
the release fence no longer waited for it.

Treat soft waitcnts as requests that depend on the incoming state: never
merge, promote or rewrite them while the analysis iterates. They are
kept unchanged in DeferredErasures, a hard waitcnt (existing or new)
carries what is needed on each visit, and the soft waitcnts are erased
once the analysis converges. This extends #220357, which did the same
only for soft waitcnts that were simplified away entirely.

Test changes:
- waitcnt-loop-carried-fence-drain.mir: the two pre-committed cases now
  wait for lgkmcnt(0) before the S_BARRIER.
- 15 tests: the waitcnt now sits directly before the instruction that
  needs it, i.e. after meta instructions such as KILL/IMPLICIT_DEF or
  after an adjacent s_waitcnt_vscnt. No change in emitted instructions.
- expand-waitcnt-profiling.ll: with amdgpu-expand-waitcnt-profiling, the
  wait for a release fence on gfx9-gfx11 is now expanded like other
  newly created waits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
reble added a commit that referenced this pull request Nov 21, 2026
On the first visit to a loop header only the preheader state is known.
A soft waitcnt there (e.g. from a release fence) can have some of its
counters simplified away while another counter keeps the instruction
alive: the fence's own vmcnt(0), a preceding wait.asyncmark, or an
explicit s_waitcnt. The soft waitcnt was then rewritten with the
simplified value and promoted to a hard waitcnt, or merged into a
preceding hard one. When the back edge later brought a pending LDS
store, the original lgkmcnt(0) request was gone, and the s_barrier after
the release fence no longer waited for it.

Treat soft waitcnts as requests that depend on the incoming state: never
merge, promote or rewrite them while the analysis iterates. They are
kept unchanged in DeferredErasures, a hard waitcnt (existing or new)
carries what is needed on each visit, and the soft waitcnts are erased
once the analysis converges. This extends #220357, which did the same
only for soft waitcnts that were simplified away entirely.

Test changes:
- waitcnt-loop-carried-fence-drain.mir: the two pre-committed cases now
  wait for lgkmcnt(0) before the S_BARRIER.
- 15 tests: the waitcnt now sits directly before the instruction that
  needs it, i.e. after meta instructions such as KILL/IMPLICIT_DEF or
  after an adjacent s_waitcnt_vscnt. No change in emitted instructions.
- expand-waitcnt-profiling.ll: with amdgpu-expand-waitcnt-profiling, the
  wait for a release fence on gfx9-gfx11 is now expanded like other
  newly created waits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
reble added a commit that referenced this pull request Nov 21, 2026
On the first visit to a loop header only the preheader state is known.
A soft waitcnt there (e.g. from a release fence) can have some of its
counters simplified away while another counter keeps the instruction
alive: the fence's own vmcnt(0), a preceding wait.asyncmark, or an
explicit s_waitcnt. The soft waitcnt was then rewritten with the
simplified value and promoted to a hard waitcnt, or merged into a
preceding hard one. When the back edge later brought a pending LDS
store, the original lgkmcnt(0) request was gone, and the s_barrier after
the release fence no longer waited for it.

Treat soft waitcnts as requests that depend on the incoming state: never
merge, promote or rewrite them while the analysis iterates. They are
kept unchanged in DeferredErasures, a hard waitcnt (existing or new)
carries what is needed on each visit, and the soft waitcnts are erased
once the analysis converges. This extends #220357, which did the same
only for soft waitcnts that were simplified away entirely.

Test changes:
- waitcnt-loop-carried-fence-drain.mir: the two pre-committed cases now
  wait for lgkmcnt(0) before the S_BARRIER.
- 15 tests: the waitcnt now sits directly before the instruction that
  needs it, i.e. after meta instructions such as KILL/IMPLICIT_DEF or
  after an adjacent s_waitcnt_vscnt. No change in emitted instructions.
- expand-waitcnt-profiling.ll: with amdgpu-expand-waitcnt-profiling, the
  wait for a release fence on gfx9-gfx11 is now expanded like other
  newly created waits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
reble added a commit that referenced this pull request Nov 22, 2026
On the first visit to a loop header only the preheader state is known.
A soft waitcnt there (e.g. from a release fence) can have some of its
counters simplified away while another counter keeps the instruction
alive: the fence's own vmcnt(0), a preceding wait.asyncmark, or an
explicit s_waitcnt. The soft waitcnt was then rewritten with the
simplified value and promoted to a hard waitcnt, or merged into a
preceding hard one. When the back edge later brought a pending LDS
store, the original lgkmcnt(0) request was gone, and the s_barrier after
the release fence no longer waited for it.

Treat soft waitcnts as requests that depend on the incoming state: never
merge, promote or rewrite them while the analysis iterates. They are
kept unchanged in DeferredErasures, a hard waitcnt (existing or new)
carries what is needed on each visit, and the soft waitcnts are erased
once the analysis converges. This extends #220357, which did the same
only for soft waitcnts that were simplified away entirely.

Test changes:
- waitcnt-loop-carried-fence-drain.mir: the two pre-committed cases now
  wait for lgkmcnt(0) before the S_BARRIER.
- 15 tests: the waitcnt now sits directly before the instruction that
  needs it, i.e. after meta instructions such as KILL/IMPLICIT_DEF or
  after an adjacent s_waitcnt_vscnt. No change in emitted instructions.
- expand-waitcnt-profiling.ll: with amdgpu-expand-waitcnt-profiling, the
  wait for a release fence on gfx9-gfx11 is now expanded like other
  newly created waits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
reble added a commit that referenced this pull request Dec 3, 2026
#244689)

On the first visit to a loop header, a soft waitcnt whose `lgkmcnt`
simplifies away but whose `vmcnt` is still needed was rewritten and
promoted (or merged into a preceding hard wait). The revisit through the
back edge then lost the `lgkmcnt(0)`, so the `s_barrier` after a release
fence no longer waited for the loop's LDS store. Like #220357, keep soft
waitcnts unchanged until the pass converges, and let a hard waitcnt
carry what each visit needs.

gfx950 runtime reproducer: 20/20 launches read stale LDS before, 0/20
after. Other test changes: in 14 tests the wait moves past meta
instructions (emitted code unchanged), and `expand-waitcnt-profiling.ll`
now expands the release-fence wait on gfx9-gfx11.

Fixes: LCOMPILER-3291
Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants