Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<bool>> = 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<bool> = 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,
Expand All @@ -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}"))?
Expand Down Expand Up @@ -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,
};
}
Expand Down
10 changes: 7 additions & 3 deletions crates/xcl/src/gpu/dxil/mask_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions docs/wave64-on-wave32.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading