Skip to content
Merged
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: 9 additions & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,14 @@ fn translate_on_this_thread(
}
Entry::Compute(cs) => {
let (tid, gtid) = lw.setup_lanes_compute(cs.threads, uses_lds);
lw.safe_barrier_init();
if split && !uses_lds && cs.threads == [64, 1, 1] { split_tid = Some(tid); }
group_runs = Some(compute_init(&mut lw, cs, tid, gtid));
cs.threads
}
Entry::Mesh(ms) => {
let (tid, _) = lw.setup_lanes_compute([ms.threads, 1, 1], uses_lds);
lw.safe_barrier_init();
mesh_init(&mut lw, ms, tid, io);
[ms.threads, 1, 1]
}
Expand Down Expand Up @@ -578,6 +580,7 @@ fn translate_on_this_thread(
let tail = lw.block(Some(1));
lw.br(tail);
lw.s.m.set_block(tail);
lw.safe_barrier_exit();
if let (Some(res), true) = (res, kernel) {
store_registers(&mut lw, res);
}
Expand Down Expand Up @@ -701,6 +704,7 @@ impl Lw {
xw: None,
xc: None,
barrier_waves: None,
safe_bar: None,
waves: 1,
cw: z,
xn: z,
Expand Down Expand Up @@ -781,7 +785,11 @@ impl Lw {
_ => Partner::Spin,
};
}
lw.setup_storage(uses_lds, if split { slot } else { 1 });
let has_s_barrier = insts.iter().any(|d| d.inst.name == "S_BARRIER");
let safe_bar = lw.barrier_waves.is_some_and(|w| w > 1)
&& has_s_barrier
&& crate::gpu::recomp::emit::safe_barrier_wanted();
lw.setup_storage(uses_lds, if split { slot } else { 1 }, safe_bar);
lw
}

Expand Down
178 changes: 161 additions & 17 deletions crates/xcl/src/gpu/dxil/lanes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,54 @@
//! Lane reads the ISA keeps inside a 32-lane half (DPP, `ds_swizzle`,
//! permlane) need no exchange.

use super::lower::{Lw, Partner, VAR_XLW};
use super::lower::{Lw, Partner, VAR_BAR_ALIVE, VAR_XLW};
use super::module::{BinOp, Pred, RmwOp, Value};
use super::ops::barrier;

impl Lw {
/// Groupshared storage, created in the entry block before anything else:
/// the program's LDS (`lds_words` > 0) and, in the split form, the halves'
/// exchange words — in the top of the LDS block when there is one (D3D12
/// allows 32 KB, the whole LDS), as the HLSL kernel lays it out.
pub fn setup_storage(&mut self, lds: bool, slot: u32) {
/// the program's LDS (`lds_words` > 0), multi-wave safe-barrier counters
/// (`safe_bar`, 2 words), and, in the split form, the halves' exchange
/// words — in the top of the LDS block when there is one (D3D12 allows
/// 32 KB, the whole LDS), as the HLSL kernel lays it out.
pub fn setup_storage(&mut self, lds: bool, slot: u32, safe_bar: bool) {
let i32 = self.s.m.i32();
self.xslot = slot.max(1);
if lds {
let g = self.s.groupshared("xcl_lds", i32, 8192);
self.lds = Some(g);
if safe_bar {
// Words 8189 = XCL_BAR_ALIVE, 8190 = XCL_BAR_STATE (8191 = XCL_XW / XCL_WT),
// matching `recomp::emit::safe_barrier_prelude`.
self.safe_bar = Some((g, 8189));
}
if self.split {
// HLSL: exchange base = 8192 - slots - counters - 1 (slots = 4 · slot per console
// wave, 2 counters each); the program's LDS stops below it
// HLSL: exchange base = 8192 - slots - counters - 1 (- 2 if safe_bar)
// (slots = 4 · slot per console wave, 2 counters each); program LDS stops below it.
let bar_words = if safe_bar { 2 } else { 0 };
let slots = 4 * self.xslot * self.waves;
let base = 8192 - slots - 2 * self.waves - 1;
let base = 8192 - slots - 2 * self.waves - 1 - bar_words;
self.lds_top = base - 1;
self.xs = Some((g, base));
self.xc = Some((g, base + slots));
self.xw = Some((g, 8191));
} else {
self.lds_top = 8191;
self.lds_top = if safe_bar { 8188 } else { 8191 };
}
} else {
if self.split {
let w = self.s.groupshared("xcl_xw", i32, 1);
self.xw = Some((w, 0));
if self.waves > 1 || self.partner == Partner::Spin {
let x = self.s.groupshared("xcl_xs", i32, (4 * self.xslot * self.waves) as u64);
self.xs = Some((x, 0));
let c = self.s.groupshared("xcl_xc", i32, 2 * self.waves as u64);
self.xc = Some((c, 0));
}
}
} else if self.split {
let w = self.s.groupshared("xcl_xw", i32, 1);
self.xw = Some((w, 0));
if self.waves > 1 || self.partner == Partner::Spin {
let x = self.s.groupshared("xcl_xs", i32, (4 * self.xslot * self.waves) as u64);
self.xs = Some((x, 0));
let c = self.s.groupshared("xcl_xc", i32, 2 * self.waves as u64);
self.xc = Some((c, 0));
if safe_bar {
let b = self.s.groupshared("xcl_bar", i32, 2);
self.safe_bar = Some((b, 0));
}
}
}
Expand Down Expand Up @@ -480,6 +493,137 @@ impl Lw {
out
}

/// Early-exit-safe multi-wave barrier setup at threadgroup entry (`Block(0)`).
/// Branchless so `Block(0)` stays unsplit for `Init::first` / `Init::slot`.
pub fn safe_barrier_init(&mut self) {
let Some((bar, base)) = self.safe_bar else { return };
let zero = self.c(0);
let alive_idx = self.c(base);
let state_idx = self.c(base + 1);
let p_alive = self.s.gs_ptr(bar, alive_idx);
let p_state = self.s.gs_ptr(bar, state_idx);
self.s.m.store(p_alive, zero, 4);
self.s.m.store(p_state, zero, 4);
self.s.barrier(barrier::GROUP_SYNC);
let first = self.s.wave_is_first_lane();
let k = self.selc(first, 1, zero);
self.s.m.atomic_rmw(RmwOp::Add, p_alive, k);
self.s.barrier(barrier::GROUP_SYNC);
}

/// Decrement `XCL_BAR_ALIVE` once when a hardware wave leaves the program
/// (`s_endpgm`, loop budget exhaustion, or normal return), matching `xcl_s_barrier_exit`.
pub fn safe_barrier_exit(&mut self) {
let Some((bar, base)) = self.safe_bar else { return };
let alive = self.rd(VAR_BAR_ALIVE);
let is_alive = self.nz(alive);
let _ = self.guarded(is_alive, &mut |lw| {
let z = lw.c(0);
lw.wr(VAR_BAR_ALIVE, z);
let t = lw.s.m.i1c(true);
let act = lw.s.wave_all_bit_count(t);
let has_act = lw.cmpc(Pred::Ne, act, 0);
let first = lw.s.wave_is_first_lane();
let go = lw.land(first, has_act);
lw.guarded(go, &mut |lw| {
let alive_idx = lw.c(base);
let neg1 = lw.c(0xFFFF_FFFF);
lw.s.gs_atomic(RmwOp::Add, bar, alive_idx, neg1);
Ok(())
})
});
}

/// Bounded, early-exit-safe workgroup barrier (`xcl_s_barrier`), matching
/// `recomp::emit::PRELUDE_SAFE_BARRIER`.
pub fn safe_barrier_sync(&mut self) {
let Some((bar, base)) = self.safe_bar else { return };
// groupshared memory only, as the HLSL path's `xcl_s_barrier` (`GroupMemoryBarrier`)
let mem_fence = barrier::TGSM_FENCE;
self.s.barrier(mem_fence);
let alive_v = self.rd(VAR_BAR_ALIVE);
let is_alive = self.nz(alive_v);
let _ = self.guarded(is_alive, &mut |lw| {
let t = lw.s.m.i1c(true);
let lanes = lw.s.wave_all_bit_count(t);
let ok_var = lw.tmp_var();
let one = lw.c(1);
lw.wr(ok_var, one);
let first = lw.s.wave_is_first_lane();
let has_lanes = lw.cmpc(Pred::Ne, lanes, 0);
let go = lw.land(first, has_lanes);
lw.guarded(go, &mut |lw| {
let alive_idx = lw.c(base);
let state_idx = lw.c(base + 1);
let one = lw.c(1);
let old = lw.s.gs_atomic(RmwOp::Add, bar, state_idx, one);
let gen = lw.lshrc(old, 12);
let g1 = lw.addc(gen, 1);
let g1m = lw.andc(g1, 0xF_FFFF);
let next_state = lw.shlc(g1m, 12);

let pre = lw.cur();
let lp = lw.block(Some(2));
let chk_alive = lw.block(Some(1));
let do_cas = lw.block(Some(1));
let chk_timeout = lw.block(Some(2));
let do_timeout = lw.block(Some(1));
let ex = lw.block(Some(3));
lw.br(lp);

lw.s.m.set_block(lp);
let i32 = lw.s.m.i32();
let n = lw.s.m.phi(i32);
let z = lw.c(0);
lw.s.m.add_incoming(n, z, pre);
let s_val = lw.s.gs_atomic(RmwOp::Add, bar, state_idx, z);
let s_gen = lw.lshrc(s_val, 12);
let gen_done = lw.cmp(Pred::Ne, s_gen, gen);
lw.cond_br(gen_done, ex, chk_alive);

lw.s.m.set_block(chk_alive);
let z2 = lw.c(0);
let alive_cnt = lw.s.gs_atomic(RmwOp::Add, bar, alive_idx, z2);
let no_alive = lw.cmp(Pred::SLe, alive_cnt, z2);
let arrived = lw.andc(s_val, 0xFFF);
let all_here = lw.cmp(Pred::UGe, arrived, alive_cnt);
let ready = lw.lor(no_alive, all_here);
lw.cond_br(ready, do_cas, chk_timeout);

lw.s.m.set_block(do_cas);
let orig = lw.s.gs_cmpxchg(bar, state_idx, s_val, next_state);
let won = lw.cmp(Pred::Eq, orig, s_val);
lw.cond_br(won, ex, chk_timeout);

lw.s.m.set_block(chk_timeout);
let timed_out = lw.cmpc(Pred::UGe, n, 1 << 20);
let n1 = lw.addc(n, 1);
let me = lw.cur();
lw.s.m.add_incoming(n, n1, me);
lw.cond_br(timed_out, do_timeout, lp);

lw.s.m.set_block(do_timeout);
let z3 = lw.c(0);
lw.wr(ok_var, z3);
let neg1 = lw.c(0xFFFF_FFFF);
lw.s.gs_atomic(RmwOp::Add, bar, alive_idx, neg1);
lw.s.gs_cmpxchg(bar, state_idx, s_val, next_state);
lw.br(ex);

lw.s.m.set_block(ex);
Ok(())
})?;
let okv = lw.rd(ok_var);
let okf = lw.s.wave_read_lane_first(okv);
let fail = lw.cmpc(Pred::Eq, okf, 0);
let cur_alive = lw.rd(VAR_BAR_ALIVE);
let new_alive = lw.selc(fail, 0, cur_alive);
lw.wr(VAR_BAR_ALIVE, new_alive);
lw.s.barrier(mem_fence);
Ok(())
});
}

/// The exchange words (created on first use when the program has no LDS).
fn xs_storage(&mut self) -> (super::ops::GroupShared, u32) {
if let Some(x) = self.xs {
Expand Down
28 changes: 16 additions & 12 deletions crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub const VAR_WIN: u32 = VAR_BUDGET + 2;
/// never arrived" flag (`xcl_xk`, `xcl_x_dead`)
pub const VAR_XK: u32 = VAR_BUDGET + 3;
pub const VAR_XDEAD: u32 = VAR_BUDGET + 4;
/// multi-wave safe barrier: whether this hardware wave is still counted in `XCL_BAR_ALIVE`
pub const VAR_BAR_ALIVE: u32 = VAR_BUDGET + 7;
/// split form: LDS written since the halves last met (the recompiler's `xcl_x_lw`, `Lw::lds_in_order`)
pub const VAR_XLW: u32 = VAR_BUDGET + 6;
/// `tw0 + 1` of the last image store / atomic through the k-th T# of `Lw::img_writes` (16
Expand Down Expand Up @@ -129,6 +131,9 @@ impl Init {
if var == super::lower_round::VAR_F32_MODE {
return s.m.i32c(super::lower_round::MODE_INIT);
}
if var == VAR_BAR_ALIVE {
return s.m.i32c(1);
}
if var == VAR_FP16_RM || var == VAR_WIN || var == VAR_XK || var == VAR_XDEAD || var == VAR_XLW || (VAR_IMG_WRITE..VAR_IMG_WRITE + 16).contains(&var) {
return s.m.i32c(0);
}
Expand Down Expand Up @@ -291,6 +296,9 @@ pub struct Lw {
/// 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>,
/// multi-wave early-exit-safe `s_barrier` counters: `(groupshared, base)` where `base` is
/// `XCL_BAR_ALIVE` and `base + 1` is `XCL_BAR_STATE` (`(gen: 20, arrived: 12)`).
pub safe_bar: Option<(super::ops::GroupShared, 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 @@ -1087,19 +1095,15 @@ 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");
// A compute or mesh group of several hardware waves uses the early-exit-safe,
// bounded LDS arrival barrier (`safe_barrier_sync`, matching `PRELUDE_SAFE_BARRIER`),
// so a console wave that left early (`s_endpgm` / loop budget) or skipped a barrier
// never hangs the remaining waves on PC hardware.
if self.safe_bar.is_some() {
self.safe_barrier_sync();
} else {
self.s.barrier(super::ops::barrier::GROUP_SYNC);
}
self.s.barrier(super::ops::barrier::GROUP_SYNC);
// (split form: both halves made their LDS writes before it)
self.lds_met();
Ok(())
Expand Down
Loading
Loading