Skip to content
Draft
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
5 changes: 5 additions & 0 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ impl Lw {
xs: None,
xw: None,
xc: None,
barrier_waves: None,
waves: 1,
cw: z,
xn: z,
Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
/// console waves per group, this thread's console wave and its partner half's lanes
pub waves: u32,
pub cw: Value,
Expand Down Expand Up @@ -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(())
}
Expand Down
28 changes: 28 additions & 0 deletions crates/xcl/src/gpu/dxil/robustness_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,18 @@ fn direct_or(code: &[u8], opt: &Options, hlsl: Vec<u8>) -> Vec<u8> {
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,
Expand Down Expand Up @@ -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();
Expand Down