From 334564ab841dce9f88c915a0c49401670a3a1931 Mon Sep 17 00:00:00 2001 From: BBareth <179921368+BBareth@users.noreply.github.com> Date: Tue, 1 Dec 2026 23:59:05 +0100 Subject: [PATCH] Recompiler: XCL_WAVE64_SPIN=0 gives direct DXIL one-wave groups the group barrier On a GPU with 32-lane waves, the two halves of a console wave64 exchange words. In the direct DXIL translator the halves of a compute or mesh group of one console wave meet through bounded arrival counters since #1741 (a guess at the hang of #1727); the HLSL path meets them at a group barrier unless XCL_WAVE64_SPIN asks for the counters. The direct translator did not read the switch, although it is in the cache keys. In a retail title on an RTX 5070 Ti one such compute program (a particle update, 191 exchanges, numthreads(64,1,1)) takes 50 ms at every dispatch: one half polls its partner's counter to the limit of 1 << 20 and gives up, about 57 times a frame, 2.8 s of a 4.2 s frame. With the poll limit cut to 16,384 for that program a call takes 0.9 ms, with the group barrier 0.1 ms. The translated program is consistent (the exchange number and the counters agree on every path), a half that leaves the program was ruled out, and so were the mask plan and the memory fences: the counters do not work for this program on this driver, the barrier does. XCL_WAVE64_SPIN=0 now gives one-wave compute and mesh groups the group barrier in the direct translator too. The default stays the counters. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/dxil/core_tests.rs | 10 ++++++++ crates/xcl/src/gpu/dxil/kernel.rs | 30 +++++++++++++++++++++++- crates/xcl/src/gpu/dxil/mask_exchange.rs | 10 +++++--- docs/wave64-on-wave32.md | 5 ++-- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index e1421c69bc..06faebc617 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -45,6 +45,16 @@ fn a_single_split_wave_of_a_compute_group_exchanges_without_a_group_barrier() { assert!(d.matches("atomicrmw").count() >= 3, "{threads} threads: the exchange's counters: {d}"); } + // `XCL_WAVE64_SPIN=0`: one console wave meets at the group barrier, several still count + assert!(super::kernel::one_wave_spin(None) && super::kernel::one_wave_spin(Some("1")) && !super::kernel::one_wave_spin(Some(" 0 "))); + super::kernel::ONE_WAVE_SPIN_FOR_TEST.with(|c| c.set(Some(false))); + let (one, two) = (dis(64, &code), dis(128, &code)); + super::kernel::ONE_WAVE_SPIN_FOR_TEST.with(|c| c.set(None)); + let (one, two) = (one.unwrap(), two.unwrap()); + assert!(syncs(&one) > 1 && !one.contains("cmpxchg"), "64 threads without the counters: group barriers at the exchange: +{one}"); + assert!(syncs(&two) == 1 && two.contains("cmpxchg"), "128 threads keep the counters: +{two}"); // the text before the start's barrier, and after it let at_start = |d: &str| d.split_at(d.find("@dx.op.barrier(i32 80, i32 9)").expect("the start's barrier")).0.to_string(); // the value (`%N`) the first line of `pre` that `hit` accepts defines diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index d8d9939b27..a8e1824f03 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -238,6 +238,29 @@ pub fn translate_entry_rodata_rt32(code: &[u8], o: KernelOptions, entry: Entry, translate_entry_with(code, o, entry, compact, None, rodata.to_vec(), ps_rt32_mask) } +/// Split wave64, a compute or mesh group of one console wave: whether its two halves meet through +/// the bounded arrival counters (the default) or at a group barrier (`XCL_WAVE64_SPIN=0`, `env`), as +/// the HLSL path's one-wave groups do (`recomp::emit::x64_spin_for`). The switch is in the cache keys +/// already (`ComputePolicyKey::wave64_spin`, the graphics key's environment). +pub(crate) fn one_wave_spin(env: Option<&str>) -> bool { + env.is_none_or(|v| v.trim() != "0") +} + +#[cfg(test)] +thread_local! { + /// a test's choice for `one_wave_spin_wanted` (taken along into the translator's thread) + pub(crate) static ONE_WAVE_SPIN_FOR_TEST: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + +fn one_wave_spin_wanted() -> bool { + #[cfg(test)] + if let Some(spin) = ONE_WAVE_SPIN_FOR_TEST.with(|c| c.get()) { + return spin; + } + static SPIN: std::sync::OnceLock = std::sync::OnceLock::new(); + *SPIN.get_or_init(|| one_wave_spin(std::env::var("XCL_WAVE64_SPIN").ok().as_deref())) +} + fn translate_entry_with( code: &[u8], o: KernelOptions, @@ -252,12 +275,16 @@ fn translate_entry_with( let code = code.to_vec(); #[cfg(test)] let no_select_fold = super::module::NO_SELECT_FOLD.with(|c| c.get()); + #[cfg(test)] + let one_wave_spin = ONE_WAVE_SPIN_FOR_TEST.with(|c| c.get()); std::thread::Builder::new() .name("xcl-dxil-translate".into()) .stack_size(256 << 20) .spawn(move || { #[cfg(test)] super::module::NO_SELECT_FOLD.with(|c| c.set(no_select_fold)); + #[cfg(test)] + ONE_WAVE_SPIN_FOR_TEST.with(|c| c.set(one_wave_spin)); translate_on_this_thread(&code, o, entry, compact, nest, rodata, ps_rt32_mask) }) .map_err(|e| format!("translator thread: {e}"))? @@ -820,8 +847,9 @@ impl Lw { // group meet through the bounded counters, as those of larger groups do: a // group barrier at every exchange hung NVIDIA GPUs when several such programs // ran at once (device hung, driver event 153: #1727; the change is #1741's). + // `XCL_WAVE64_SPIN=0` gives them the group barrier (`one_wave_spin`). // A geometry program sets its partner by phase (`translate_on_this_thread`). - 33..=64 if !matches!(entry, Entry::Compute(_) | Entry::Mesh(_)) => Partner::Present, + 33..=64 if !matches!(entry, Entry::Compute(_) | Entry::Mesh(_)) || !one_wave_spin_wanted() => Partner::Present, _ => Partner::Spin, }; } diff --git a/crates/xcl/src/gpu/dxil/mask_exchange.rs b/crates/xcl/src/gpu/dxil/mask_exchange.rs index def16d37b3..66d086fe5a 100644 --- a/crates/xcl/src/gpu/dxil/mask_exchange.rs +++ b/crates/xcl/src/gpu/dxil/mask_exchange.rs @@ -245,14 +245,18 @@ mod tests { ins("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 6), ("DATA", 4), ("SADDR", 125)]), ins("S_ENDPGM", "ENC_SOPP", &[]), ].concat(); - for threads in [32, 64] { + // (64 threads: through the arrival counters, and at the group barrier of `XCL_WAVE64_SPIN=0`) + for (threads, spin) in [(32, true), (64, true), (64, false)] { let cs = CsInterface { threads: [threads, 1, 1], tid_components: 1, ..Default::default() }; let o = KernelOptions { wave64: true, split: true, mem_windows: 1, sampler_base: 0 }; - let k = kernel::translate_compute(&code, o, &cs).unwrap(); + kernel::ONE_WAVE_SPIN_FOR_TEST.with(|c| c.set(Some(spin))); + let k = kernel::translate_compute(&code, o, &cs); + kernel::ONE_WAVE_SPIN_FOR_TEST.with(|c| c.set(None)); + let k = k.unwrap(); let mem = runner::dispatch(&k.dxil, [1; 3], &[0; KERNEL_SGPRS], &vec![0; threads as usize]).unwrap(); for (lane, &v) in mem[..threads as usize].iter().enumerate() { let expected = if lane == 2 { 42 } else { 17 + u32::from(threads == 64) }; - assert_eq!(v, expected, "lane {lane} of {threads}"); + assert_eq!(v, expected, "lane {lane} of {threads}, counters {spin}"); } } } diff --git a/docs/wave64-on-wave32.md b/docs/wave64-on-wave32.md index 9a01a70742..c956bc7c7f 100644 --- a/docs/wave64-on-wave32.md +++ b/docs/wave64-on-wave32.md @@ -53,7 +53,7 @@ SGPRs of both halves and the tests fail if they ever differ. ### Synchronisation * **One console wave per group** (≤ 64 threads), now only in kernel mode (the tests), with - `XCL_WAVE64_SPIN=0` in the HLSL path and in the direct translator's ES phase with + `XCL_WAVE64_SPIN=0` in both translators and in the direct translator's ES phase with `XCL_SAFE_BARRIER=0`: `GroupMemoryBarrierWithGroupSync` between writing and reading the slot. All hardware waves of the group are the two halves of the same console wave, which execute the same sequence of exchanges, so every barrier is reached by both. Portable (no @@ -74,8 +74,7 @@ SGPRs of both halves and the tests fail if they ever differ. co-resident waves of one group making progress, which holds on GPUs (all waves of a group are resident on one SM / CU) but not necessarily on WARP. Both translators use this variant by default for one console wave per group too; `XCL_WAVE64_SPIN=0` forces the group barrier - for groups of one console wave in the HLSL path only (the direct translator always spins for - compute and mesh). + for groups of one console wave in both (compute and mesh in the direct translator). A console wave whose upper half does not exist (group of 32 or 96 threads) exchanges with nobody: the missing lanes count as inactive (ballot bits 0), exactly as on the console. The