Repository navigation
[AMDGPU][SIInsertWaitcnts] Fix soft wait removal with loop-carried deps - #220357
Conversation
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
|
@llvm/pr-subscribers-llvm-globalisel @llvm/pr-subscribers-backend-amdgpu Author: Krzysztof Drewniak (krzysz00) ChangesThe code that checked if a soft waitcnt (such as the one insterted by The fix is that, instad of immediately erasing "redundant" waits, we This has the side effect of fixing a correctness issue around the CAS AI disclosure: Claude came up with the overall shape of the solution Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> <sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">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:
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
left a comment
There was a problem hiding this comment.
This looks reasonable to me
137ffad to
9e37b7d
Compare
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]>
9e37b7d to
56e03d6
Compare
|
Holding until my tomorrow morning per the extra review request |
|
This sounds like an alternative to #211684? Did you talk to each other? |
|
Re the alternate PR: the person behind that's on vacation |
|
Just repeating here my thoughts from the other PR about how we should handle this kind of problem:
|
cc @vporpo |
…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]>
…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]>
…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]>
|
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. |
…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.
#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
…#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
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]>
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]>
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]>
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]>
#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
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 0x0is itself aloadcnt-able event, and so needs to beforced 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]