From 8feefd28851bf59d0599b1bea87aa1bf4ecf00ef Mon Sep 17 00:00:00 2001 From: Jerkin42 <66524771+Jerkin42@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:16:39 +0000 Subject: [PATCH] shaders: let a wave catch up before a barrier in block-dispatched programs A guest wave reaches S_BARRIER as a whole: a lane that leaves a loop early stays masked out in EXEC until the rest of the wave is done. The block dispatcher gives every lane its own program counter, so such a lane went through the barrier on its own and read group-shared memory the others had not written yet. Start a block at each barrier and enter it only once every active lane of the wave stands at one, the earliest first when they stand at different ones. CS_TAA2_Resolve shows it: the lanes fill a 10x10 tile, some in two iterations and some in one, and the last rows of every 8x8 group came out near zero. Mean of the resolved image by row of the group, on RADV GFX12: .342 .343 .343 .344 .277 .115 .023 .001. Co-Authored-By: Claude Opus 5.5 --- tools/shader-compiler/src/hlsl.rs | 47 ++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tools/shader-compiler/src/hlsl.rs b/tools/shader-compiler/src/hlsl.rs index 298ac10..b64fed2 100644 --- a/tools/shader-compiler/src/hlsl.rs +++ b/tools/shader-compiler/src/hlsl.rs @@ -4669,6 +4669,13 @@ uint tb_fmac_half(uint a, uint b, uint c) { leaders.insert(i + 1); } } + // A guest wave reaches S_BARRIER as a whole: lanes that left a loop early wait in + // EXEC for the rest. Here each lane follows its own blocks, so the barrier starts + // one that a lane enters only when its wave has caught up. + let barriers: Vec = (entry..n) + .filter(|&i| self.is_cs && self.insns[i].enc == Enc::Sopp && self.insns[i].op == 0x0a) + .collect(); + leaders.extend(&barriers); let starts: Vec = leaders.into_iter().filter(|&s| s < n).collect(); self.loop_depth = 0; if entry == 0 { @@ -4680,7 +4687,16 @@ uint tb_fmac_half(uint a, uint b, uint c) { self.loop_depth += 1; // Flat `if (tb_sel == K)` tests on a snapshot of the block id (FXC mishandles a // `switch` whose selector is assigned inside its cases). - self.line("uint tb_sel = tb_pc; tb_pc = 0xffffffffu;"); + if barriers.is_empty() { + self.line("uint tb_sel = tb_pc; tb_pc = 0xffffffffu;"); + } else { + let at = barriers.iter().map(|i| format!("tb_pc == {i}u")).collect::>().join(" || "); + self.line(&format!("bool tb_wait = {at};")); + // When lanes stand at different barriers the earliest goes first. + self.line("bool tb_busy = WaveActiveAnyTrue(!tb_wait); uint tb_low = WaveActiveMin(tb_pc);"); + self.line("tb_wait = tb_wait && (tb_busy || tb_pc != tb_low);"); + self.line("uint tb_sel = tb_wait ? 0xfffffffeu : tb_pc; if (!tb_wait) tb_pc = 0xffffffffu;"); + } let insns = self.insns.clone(); for (b, &start) in starts.iter().enumerate() { let end = starts.get(b + 1).copied().unwrap_or(n); @@ -10273,6 +10289,35 @@ mod reduction_execz_regression { } } + #[test] + fn dispatched_lanes_wait_for_their_wave_at_a_barrier() { + let op = |offset, op, mnemonic: &str, imm| Insn { + offset, dwords: 1, enc: Enc::Sopp, op, raw: vec![0xbf80_0000 | op << 16], mnemonic: mnemonic.into(), + dst: Vec::new(), src: Vec::new(), aux: tuxbox_dxbc::rdna2::InsnAux { imm, ..Default::default() }, + }; + let emit = |barrier: bool| { + let shader = guarded_shader(32, &[]); + // A lane leaves the loop at its first EXECZ and may reach the barrier before the others. + let mut emitter = Emitter::new(&shader, vec![ + op(0, 8, "s_cbranch_execz", 1), + op(4, 2, "s_branch", -2), + if barrier { op(8, 0x0a, "s_barrier", 0) } else { op(8, 0, "s_nop", 0) }, + op(12, 1, "s_endpgm", 0), + ]); + emitter.emit_body_dispatch(false, 0).unwrap(); + emitter.body + }; + let body = emit(true); + assert!(body.contains("bool tb_wait = tb_pc == 2u;"), "{body}"); + let block = body.find("if (tb_sel == 2u) {").expect("the barrier starts a block"); + let wait = body.find("WaveActiveAnyTrue(!tb_wait)").expect("waits for its wave"); + let sync = body.find("GroupMemoryBarrierWithGroupSync();").unwrap(); + assert!(wait < block && block < sync, "{body}"); + assert!(body.contains("uint tb_sel = tb_wait ? 0xfffffffeu : tb_pc;"), "{body}"); + let body = emit(false); + assert!(!body.contains("tb_wait") && body.contains("uint tb_sel = tb_pc;"), "{body}"); + } + #[test] fn reduction_execz_refuses_block_dispatch() { let shader = guarded_shader(32, &[]);