From 2ca912767cb61687b17bca08d49df542c9b86f68 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Wed, 25 Nov 2026 06:28:00 -0500 Subject: [PATCH] Recompiler: leave unsafe group barriers to HLSL Reject direct-DXIL compute and mesh programs whose group barrier spans multiple hardware waves. The direct barrier waits for waves the console may allow to leave or bypass it; the HLSL fallback already uses the safe counted barrier. Keep the coherence test on a one-wave group so it continues to test handle properties rather than the fallback boundary. Adapted from upstream commit fb55e49e. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/gpu/dxil/kernel.rs | 5 ++++ crates/xcl/src/gpu/dxil/lower.rs | 15 +++++++++++ crates/xcl/src/gpu/dxil/robustness_tests.rs | 28 +++++++++++++++++++++ crates/xcl/src/gpu/recomp/emit.rs | 8 +++++- crates/xcl/src/gpu/recomp/tests.rs | 19 +++++++++++--- 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index ed31fc0383..a9dd50fe1d 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -561,6 +561,7 @@ impl Lw { xs: None, xw: None, xc: None, + barrier_waves: None, waves: 1, cw: z, xn: z, @@ -619,6 +620,10 @@ impl Lw { if let (Some(t), true) = (group_threads, split) { lw.waves = t.div_ceil(64).max(1); } + if let (Some(t), Entry::Compute(_) | Entry::Mesh(_)) = (group_threads, entry) { + let hw_lanes = if split || !o.wave64 { 32 } else { 64 }; + lw.barrier_waves = Some(t.div_ceil(hw_lanes).max(1)); + } if split && std::env::var_os("XCL_X64_ALL_MASKS").is_none() { lw.whole_live = crate::gpu::recomp::x64_masks::by_pc(insts, kernel); } diff --git a/crates/xcl/src/gpu/dxil/lower.rs b/crates/xcl/src/gpu/dxil/lower.rs index 41de2a7848..cdf8f0291c 100644 --- a/crates/xcl/src/gpu/dxil/lower.rs +++ b/crates/xcl/src/gpu/dxil/lower.rs @@ -272,6 +272,9 @@ pub struct Lw { pub xw: Option<(super::ops::GroupShared, u32)>, /// arrival counters of the spin exchange (two per console wave) pub xc: Option<(super::ops::GroupShared, u32)>, + /// hardware waves of a compute or mesh thread group (None: another kind of entry), as the + /// HLSL emitter's `compute_or_mesh_hw_waves` + pub barrier_waves: Option, /// console waves per group, this thread's console wave and its partner half's lanes pub waves: u32, pub cw: Value, @@ -1062,6 +1065,18 @@ impl Lw { fn sopp(&mut self, d: &Decoded, name: &str) -> LResult<()> { match name { "s_barrier" => { + // A compute or mesh group of several hardware waves: this barrier waits for + // every one of them at a barrier, and a console wave that has left – s_endpgm + // behind a branch around the barrier, the loop budget – or that ran one barrier + // fewer never arrives. On the console the waves left go on; on NVIDIA they wait + // for ever and the GPU hangs (`recomp::emit::PRELUDE_SAFE_BARRIER`). A retail + // title's mesh programs all have it: the first wave of a group whose vertex or + // primitive count is 0 runs a barrier the other waves branch around, then the + // one they all run. The HLSL path counts the waves still there + // (`xcl_s_barrier`): such a program is left to it. + if self.barrier_waves.is_some_and(|w| w > 1) && crate::gpu::recomp::emit::safe_barrier_wanted() { + return err(d, "s_barrier in a group of several hardware waves"); + } self.s.barrier(super::ops::barrier::GROUP_SYNC); Ok(()) } diff --git a/crates/xcl/src/gpu/dxil/robustness_tests.rs b/crates/xcl/src/gpu/dxil/robustness_tests.rs index 00b1b45aeb..9bd172ef70 100644 --- a/crates/xcl/src/gpu/dxil/robustness_tests.rs +++ b/crates/xcl/src/gpu/dxil/robustness_tests.rs @@ -96,6 +96,34 @@ fn translation_is_byte_identical_across_runs_and_threads() { } } +/// A group barrier in a compute group of several hardware waves is not translated here: the +/// plain barrier waits for waves that have left (the GPU hangs on NVIDIA), and only the HLSL +/// path has the barrier that counts the waves still there. +#[test] +fn a_group_barrier_of_several_hardware_waves_is_left_to_the_hlsl_path() { + let code = prog(&[("S_BARRIER", "ENC_SOPP", &[], None)]); + let o = |wave64, split| KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 }; + let cs = |threads| CsInterface { threads, user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1 }; + // 16 x 16 threads: 8 hardware waves in the split form, 4 native 64-lane ones, 8 of a + // 32-lane program; and the two 32-lane halves of one console wave + for (wave64, split, threads) in [(true, true, [16, 16, 1]), (true, false, [16, 16, 1]), (false, false, [16, 16, 1]), (true, true, [64, 1, 1]), (false, false, [64, 1, 1])] { + let e = kernel::translate_compute(&code, o(wave64, split), &cs(threads)).err().unwrap_or_else(|| panic!("translated: wave64 {wave64}, split {split}, {threads:?}")); + assert!(e.contains("s_barrier in a group of several hardware waves"), "{e}"); + } + // one hardware wave has nobody to wait for: translated as before + kernel::translate_compute(&code, o(true, false), &cs([64, 1, 1])).expect("one 64-lane wave"); + kernel::translate_compute(&code, o(false, false), &cs([32, 1, 1])).expect("one 32-lane wave"); + // (the kernel form of the tests is no thread group of a title's: its barrier stays) + kernel::translate(&code, o(true, true)).expect("kernel"); + // a mesh program's compute pass: two console waves a group, as a retail title's + let ms = |threads| crate::gpu::recomp::MsInterface { outputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF)], threads, max_vertices: 64, max_primitives: 64 }; + for split in [true, false] { + let e = kernel::translate_mesh(&code, o(true, split), &ms(128)).err().unwrap_or_else(|| panic!("a mesh pass translated (split {split})")); + assert!(e.contains("s_barrier in a group of several hardware waves"), "{e}"); + } + kernel::translate_mesh(&code, o(true, false), &ms(64)).expect("a mesh pass of one 64-lane wave"); +} + // ---- builder misuse: recorded as the first error, finish fails instead of panicking use super::container::Stage; diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index d0fc9163dc..27dd870629 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -695,7 +695,7 @@ impl Em { self.has_s_barrier = has_s_barrier; self.safe_barrier = has_s_barrier && self.compute_or_mesh_hw_waves().is_some_and(|w| w > 1) - && std::env::var("XCL_SAFE_BARRIER").map_or(true, |v| v.trim() != "0"); + && safe_barrier_wanted(); } pub fn take(&mut self) -> String { @@ -6269,6 +6269,12 @@ uint2 xcl_x_ballot_local(bool b) { uint xcl_x_rla_half(uint v, uint l) { return WaveReadLaneAt(v, l & 31u); } "#; +/// Whether a multi-warp Compute / Mesh `s_barrier` gets the early-exit-safe barrier +/// (`XCL_SAFE_BARRIER=0`: the plain group barrier, in both translators). +pub(crate) fn safe_barrier_wanted() -> bool { + std::env::var("XCL_SAFE_BARRIER").map_or(true, |v| v.trim() != "0") +} + /// Early-exit-safe, bounded workgroup barrier for multi-warp Compute / Mesh `s_barrier`. /// /// On RDNA2, `S_ENDPGM` decrements the CU's active wave count so remaining waves executing diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 79a95df537..747f1542f2 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -87,8 +87,18 @@ fn direct_or(code: &[u8], opt: &Options, hlsl: Vec) -> Vec { let o = KernelOptions { wave64: opt.wave64, split, mem_windows, sampler_base: opt.static_sampler_base }; match &opt.mode { Mode::Kernel => translate(code, o).unwrap_or_else(|e| panic!("direct DXIL: {e}")), - Mode::Compute(cs) => translate_compute(code, o, cs).unwrap_or_else(|e| panic!("direct DXIL: {e}")).dxil, - Mode::Mesh(ms) => crate::gpu::dxil::kernel::translate_mesh(code, o, ms).unwrap_or_else(|e| panic!("direct DXIL: {e}")).dxil, + // (a group barrier in a group of several hardware waves is left to the HLSL path, as + // the runtime's fallback does: `dxil::lower`'s s_barrier) + Mode::Compute(cs) => match translate_compute(code, o, cs) { + Ok(k) => k.dxil, + Err(e) if e.contains("s_barrier in a group of several hardware waves") => hlsl, + Err(e) => panic!("direct DXIL: {e}"), + }, + Mode::Mesh(ms) => match crate::gpu::dxil::kernel::translate_mesh(code, o, ms) { + Ok(k) => k.dxil, + Err(e) if e.contains("s_barrier in a group of several hardware waves") => hlsl, + Err(e) => panic!("direct DXIL: {e}"), + }, Mode::Vertex(vs) => crate::gpu::dxil::kernel::translate_vertex(code, o, vs).unwrap_or_else(|e| panic!("direct DXIL: {e}")).dxil, Mode::Pixel(ps) => crate::gpu::dxil::kernel::translate_pixel(code, o, ps).unwrap_or_else(|e| panic!("direct DXIL: {e}")).dxil, _ => hlsl, @@ -9990,9 +10000,12 @@ fn compute_globallycoherent_only_when_needed() { // Direct DXIL backend: check the IsGloballyCoherent bit (0x4000) in annotateHandle properties // (RAW_BUFFER=11 | IsUAV=0x1000 = 4107 vs RAW_BUFFER=11 | IsUAV=0x1000 | IsGloballyCoherent=0x4000 = 20491). + // (the plain program in a group of one 32-lane wave: its s_barrier in a group of several + // hardware waves is left to the HLSL path) + let one_wave = super::CsInterface { threads: [32, 1, 1], ..cs.clone() }; for mw in [1u32, super::HEAP_WINDOWS | 2] { let o = crate::gpu::dxil::kernel::KernelOptions { wave64: false, split: false, mem_windows: mw, sampler_base: 0 }; - let plain_k = crate::gpu::dxil::kernel::translate_compute(&plain, o, &cs).unwrap(); + let plain_k = crate::gpu::dxil::kernel::translate_compute(&plain, o, &one_wave).unwrap(); let coh_k = crate::gpu::dxil::kernel::translate_compute(&coherent_cases[6].1, o, &cs).unwrap(); if super::dxc::location().is_ok() { let plain_dis = crate::gpu::dxil::validate::disassemble(&plain_k.dxil).unwrap();