From 0d2f0ea75a2d9dcc25cfb86d36555636f99f61cb Mon Sep 17 00:00:00 2001 From: qorile lulu Date: Fri, 27 Nov 2026 12:45:50 +0700 Subject: [PATCH] fix(shader): refuse predication of v_writelane_b32 regions and gate Windows-only halves tests (#497) On hardware, v_writelane_b32 ignores EXEC, so a write inside a divergent branch still updates the target lane even if that lane is inactive in the branch. Linearizing such a region via SkipGuard, SESE diamond, or lockstep-loop predication causes the merge select(edge_act, ...) to discard the write when the target lane was inactive (#497). - Refuse predication in match_skip_guard, match_sese_diamond, and match_lockstep_loop when any block in the region contains v_writelane_b32, allowing #435's whole-wave retry to handle it. - Gate Windows-only GPU execution helpers/tests in halves_tests.rs with #[cfg(windows)] so cargo test -p victory-shader builds and passes natively on macOS. - Add regression test predicated_branch_refuses_writelane_so_whole_wave_retry_can_take_it. --- .../victory-shader/src/emit/halves_tests.rs | 40 +++++++++++++++++++ .../src/emit/lower/predicate.rs | 22 ++++++++-- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/crates/victory-shader/src/emit/halves_tests.rs b/crates/victory-shader/src/emit/halves_tests.rs index 34638b28d..1f4adc2b9 100644 --- a/crates/victory-shader/src/emit/halves_tests.rs +++ b/crates/victory-shader/src/emit/halves_tests.rs @@ -1103,6 +1103,7 @@ fn differences_in( /// [`differences_in`] with custom contents for the structured SRV buffer at /// table dword 8. +#[cfg(windows)] #[allow(clippy::too_many_arguments)] fn differences_with_buffer( wave64: bool, @@ -2419,6 +2420,7 @@ fn the_predication_test_programs_are_what_their_comments_say() { } #[test] +#[cfg(windows)] fn predicated_early_out_matches_the_console_on_every_gpu() { let mut failed = Vec::new(); for (keep_below, limit) in [(true, 40), (false, 35)] { @@ -2441,6 +2443,7 @@ fn predicated_early_out_matches_the_console_on_every_gpu() { } #[test] +#[cfg(windows)] fn predicated_diamond_matches_the_console_on_every_gpu() { let words = diamond_program(); let n = native(&words, true, 64, 256); @@ -2456,6 +2459,7 @@ fn predicated_diamond_matches_the_console_on_every_gpu() { } #[test] +#[cfg(windows)] fn half_add_reduction_across_halves_matches_the_console_on_every_gpu() { let words = half_add_reduction(); let mut failed = Vec::new(); @@ -2483,6 +2487,7 @@ fn half_add_reduction_across_halves_matches_the_console_on_every_gpu() { } #[test] +#[cfg(windows)] fn uniform_buffer_load_loop_across_halves_matches_the_console_on_every_gpu() { let words = buffer_load_uniform_loop(); let mut failed = Vec::new(); @@ -2723,6 +2728,7 @@ fn the_extended_halves_programs_are_what_their_comments_say() { } #[test] +#[cfg(windows)] fn skip_guard_with_uniform_loop_matches_the_console_on_every_gpu() { let mut failed = Vec::new(); for keep_from in [20u32, 36u32] { @@ -2753,6 +2759,7 @@ fn skip_guard_with_uniform_loop_matches_the_console_on_every_gpu() { } #[test] +#[cfg(windows)] fn wave_scalarization_lockstep_loop_matches_the_console_on_every_gpu() { let words = wave_scalarization_loop_program(); let mut failed = Vec::new(); @@ -2785,6 +2792,7 @@ fn wave_scalarization_lockstep_loop_matches_the_console_on_every_gpu() { } #[test] +#[cfg(windows)] fn trampoline_early_out_and_mask_data_match_the_console_on_every_gpu() { let words = trampoline_early_out_mask_program(); let n = native(&words, true, 64, 0); @@ -2824,6 +2832,7 @@ fn thread_ticket_env_toggle_defaults_on() { /// blocks from the group-shared ticket counter so every console thread id in /// the group is executed once on 32-lane hardware (issue #1243). #[test] +#[cfg(windows)] fn thread_ticket_relabels_2d_and_partial_tail_groups_bijectively() { // Console wave32 program: triggers `console_lanes` via `v_mbcnt_lo_u32_b32`, // computes `slot = (y * 32 + x) * 4`, and stores `1` to raw UAV `s[40:43]`. @@ -2941,3 +2950,34 @@ fn thread_ticket_relabels_2d_and_partial_tail_groups_bijectively() { let got_40 = run(lowered_40, 40); assert_eq!(got_40, vec![1u32; 40], "all 40 threads (32 full + 8 partial) must run once"); } + +/// A divergent branch containing `v_writelane_b32` must not be predicated: +/// `v_writelane_b32` ignores `EXEC` on hardware, whereas a linear merge +/// `select` discards the write for lanes inactive in the branch (#497). +#[test] +fn predicated_branch_refuses_writelane_so_whole_wave_retry_can_take_it() { + // if (v0 >= 40) { v_writelane_b32 v5, 0x777, 5; v_readlane_b32 s8, v5, 5; } + // store s8 to UAV so the cross-half read and writelane stay live. + let mut w = PROLOGUE.to_vec(); + w.extend([ + 0x7E0A_0280, // v_mov_b32 v5, 0 + 0x7E10_0280, // v_mov_b32 v8, 0 + 0x7D86_00A8, // v_cmp_le_u32 vcc, 40, v0 + 0xBE94_246A, // s_and_saveexec_b64 s[20:21], vcc + 0xBF88_0006, // s_cbranch_execz +6 + 0xD761_0005, + 0x0001_0AFF, + 0x0000_0777, // v_writelane_b32 v5, 0x777, 5 + 0xD760_0008, + 0x0001_0B05, // v_readlane_b32 s8, v5, 5 + 0x7E10_0208, // v_mov_b32 v8, s8 + 0x87FE_147E, // s_or_b64 exec, exec, s[20:21] + ]); + w.extend(store(8, 0)); + w.push(S_ENDPGM); + let n = native(&w, true, 64, 0); + // Per-lane halves lowering must NOT predicate the skip-guard around + // v_writelane_b32; it refuses the cross-half read instead, leaving + // whole-wave retry (#435) to handle it. + assert!(lowered_with(&n, true, true).is_err()); +} diff --git a/crates/victory-shader/src/emit/lower/predicate.rs b/crates/victory-shader/src/emit/lower/predicate.rs index 32ddd8cf0..8b4e27b4e 100644 --- a/crates/victory-shader/src/emit/lower/predicate.rs +++ b/crates/victory-shader/src/emit/lower/predicate.rs @@ -630,12 +630,26 @@ fn match_skip_guard( return None; } // Only predicate if at least one block in `region_set` needs `in_step`. - if !region_set.iter().any(|&b| needs[b] && !plan.step[b]) { + if !region_set.iter().any(|&b| needs[b] && !plan.step[b]) + || has_writelane(p, ®ion_set) + { return None; } Some(body) } +/// `v_writelane_b32` ignores `EXEC` on hardware: linearizing a branch and +/// selecting at the merge would discard writes to lanes that were inactive +/// in the branch (#497). +fn has_writelane(p: &Program, blocks: &Set) -> bool { + blocks.iter().any(|&b| { + p.blocks[b] + .insts + .iter() + .any(|inst| inst.name() == "v_writelane_b32") + }) +} + #[allow(clippy::too_many_arguments)] fn match_sese_diamond( p: &Program, @@ -722,7 +736,9 @@ fn match_sese_diamond( { return None; } - if !region_set.iter().any(|&b| needs[b] && !plan.step[b]) { + if !region_set.iter().any(|&b| needs[b] && !plan.step[b]) + || has_writelane(p, ®ion_set) + { return None; } let mut region: Vec = region_set.into_iter().collect(); @@ -840,7 +856,7 @@ fn match_lockstep_loop( if eff_preds(exits, &plan.dead_blocks, exit) != [latch] { return None; } - if !body.iter().any(|&b| needs[b] && !plan.step[b]) { + if !body.iter().any(|&b| needs[b] && !plan.step[b]) || has_writelane(p, &body) { return None; }