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
37 changes: 33 additions & 4 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ fn translate_on_this_thread(
let uses_lds = lw.lds.is_some();
let res = lw.res;
let io = lw.init.io.expect("the I/O buffer");
// a compute program: whether this group runs (`compute_init`: not past a folded dispatch's count)
let mut group_runs = None;
let numthreads = match &entry {
Entry::Kernel => {
lw.setup_lanes();
Expand All @@ -209,7 +211,7 @@ fn translate_on_this_thread(
}
Entry::Compute(cs) => {
let (tid, gtid) = lw.setup_lanes_compute(cs.threads, uses_lds);
compute_init(&mut lw, cs, tid, gtid);
group_runs = Some(compute_init(&mut lw, cs, tid, gtid));
cs.threads
}
Entry::Mesh(ms) => {
Expand Down Expand Up @@ -301,13 +303,23 @@ fn translate_on_this_thread(
}
let h0 = if plan.is_some() { lw.block(None) } else { head(&mut lw, entry) };
let exit = lw.block(None);
// where `Block(0)` goes
let mut first = h0;
if gs_only {
// the GS alone: in the lower hardware wave only
let gate = lw.block(Some(1));
lw.edge(Block(0), gate);
lw.s.m.set_block(gate);
let up = lw.upper;
lw.cond_br(up, exit, h0);
} else if let Some(runs) = group_runs {
// a folded dispatch's groups past the console's count leave at once (the whole group:
// no barrier waits on them)
let gate = lw.block(Some(1));
lw.edge(Block(0), gate);
lw.s.m.set_block(gate);
lw.cond_br(runs, h0, exit);
first = gate;
} else {
lw.edge(Block(0), h0);
}
Expand Down Expand Up @@ -420,7 +432,7 @@ fn translate_on_this_thread(
}
lw.s.m.ret();
lw.s.m.set_block(Block(0));
lw.s.m.br(h0);
lw.s.m.br(first);
let hw = if split || !o.wave64 { 32 } else { 64 };
lw.finish(numthreads, hw)
}
Expand Down Expand Up @@ -641,12 +653,28 @@ fn store_registers(lw: &mut Lw, res: super::shader::RawUav) {

/// The compute entry's fixed registers (the HLSL compute mode's prologue): group ids
/// after the user SGPRs, TG_SIZE, SCC = 0, EXEC = the wave's threads, v0..v2 = thread id.
pub fn compute_init(lw: &mut Lw, cs: &CsInterface, tid: Value, gtid: [Value; 3]) {
/// Returns whether this group runs: a dispatch folded into Y (`recomp::IO_GROUP_LIMIT` in `io`
/// not 0, the console's group count in X) takes its group in X from both ids, and the groups
/// past that count do not run.
pub fn compute_init(lw: &mut Lw, cs: &CsInterface, tid: Value, gtid: [Value; 3]) -> Value {
let io = lw.init.io.expect("the I/O buffer");
let o = lw.c((crate::gpu::recomp::IO_GROUP_LIMIT * 4) as u32);
let limit = lw.s.raw_load(io, o, 1)[0];
let (gx, gy) = (lw.s.group_id(0), lw.s.group_id(1));
let k = lw.c(crate::gpu::recomp::FOLD_X);
let rows = lw.mul(gy, k);
let flat = lw.add(gx, rows);
let folded = lw.cmpc(Pred::Ne, limit, 0);
let zero = lw.c(0);
let ids = [lw.sel(folded, flat, gx), lw.sel(folded, zero, gy)];
let inside = lw.cmp(Pred::ULt, flat, limit);
let t = lw.b(true);
let runs = lw.sel(folded, inside, t);
let mut sgpr = HashMap::new();
let mut next = cs.user_sgprs;
for k in 0..3 {
if cs.tgid[k] {
let g = lw.s.group_id(k as u32);
let g = if k < 2 { ids[k] } else { lw.s.group_id(2) };
sgpr.insert(next, g);
next += 1;
}
Expand Down Expand Up @@ -674,6 +702,7 @@ pub fn compute_init(lw: &mut Lw, cs: &CsInterface, tid: Value, gtid: [Value; 3])
}
}
lw.init.compute = Some(ComputeInit { sgpr, vgpr, zero });
runs
}

/// The mesh entry's registers (the HLSL mesh mode's prologue): SGPRs from `io` with the
Expand Down
10 changes: 9 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4919,12 +4919,20 @@ impl Em {
h.push_str(" lane_id = WaveGetLaneIndex();\n");
}
load_sgprs(&mut h);
// a folded dispatch (`IO_GROUP_LIMIT`): the console's group in X, the groups
// past its count left out (the whole group returns)
let _ = writeln!(
h,
" uint3 xcl_gid = gid;\n {{\n uint xcl_gl = io.Load({}u);\n if (xcl_gl != 0u) {{\n uint xcl_f = gid.x + gid.y * {}u;\n if (xcl_f >= xcl_gl) return;\n xcl_gid = uint3(xcl_f, 0u, gid.z);\n }}\n }}",
super::IO_GROUP_LIMIT * 4,
super::FOLD_X
);
// system SGPRs after the user data: enabled group ids, then TG_SIZE
let mut next = cs.user_sgprs as u64;
for (k, c) in ["x", "y", "z"].iter().enumerate() {
if cs.tgid[k] {
if sg.contains(&next) {
let _ = writeln!(h, " {} = gid.{c};", sname(next).unwrap());
let _ = writeln!(h, " {} = xcl_gid.{c};", sname(next).unwrap());
}
next += 1;
}
Expand Down
25 changes: 21 additions & 4 deletions crates/xcl/src/gpu/recomp/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,12 +1294,15 @@ fn lower_with<I: Ir>(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s
prev = c;
}
}
// compute: every lane that runs is in EXEC, and nothing is stored at the end – the skip and
// store blocks are left unreachable (a bare `ret`)
// compute: every lane that runs is in EXEC, and nothing is stored at the end – the store
// blocks are left unreachable, and the skip block (a bare `ret`) takes the groups past a
// folded dispatch's count
if kernel {
preds[skip] = vec![0];
preds[sstore] = vec![exit];
preds[vstore] = vec![exit, sstore];
} else if matches!(stage, Stage::Compute(_)) {
preds[skip] = vec![0];
}
preds[exit] = kernel.then_some(skip).into_iter().chain(ends.iter().map(|&b| tail[b])).collect();
for i in 0..count {
Expand Down Expand Up @@ -1392,11 +1395,25 @@ fn lower_with<I: Ir>(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s
}
Stage::Compute(cs) => {
let (flat, tid) = l.ir.compute_ids(cs.threads);
// a dispatch folded into Y (`IO_GROUP_LIMIT` not 0: the console's group count in X):
// the group in X from both ids, the groups past the count leave at once
let a = l.ir.int(super::IO_GROUP_LIMIT as u32);
let limit = l.ir.io_load(a);
let (gx, gy) = (l.ir.group_id(0), l.ir.group_id(1));
let k = l.ir.int(super::FOLD_X);
let rows = l.ir.bin(Bin::Mul, gy, k);
let group = l.ir.bin(Bin::Add, gx, rows);
let z = l.ir.int(0);
let folded = l.ir.icmp(Cmp::Ne, limit, z);
let ids = [l.ir.select(folded, group, gx), l.ir.select(folded, z, gy)];
let inside = l.ir.icmp(Cmp::Ult, group, limit);
let t = l.ir.boolean(true);
let runs = l.ir.select(folded, inside, t);
// system SGPRs after the user data: the enabled group ids, then TG_SIZE
let mut next = cs.user_sgprs as u8;
for k in 0..3 {
if cs.tgid[k as usize] {
let g = l.ir.group_id(k);
let g = if k < 2 { ids[k as usize] } else { l.ir.group_id(k) };
l.write_num(0, next, g);
next += 1;
}
Expand Down Expand Up @@ -1426,7 +1443,7 @@ fn lower_with<I: Ir>(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s
// every lane that runs is in EXEC
let t = l.ir.boolean(true);
l.write_bit(0, EXEC, t);
l.ir.br(blk(0));
l.ir.br_if(runs, blk(0), skip);
l.ir.at(skip);
l.ssa.seal(&mut l.ir, skip);
l.ir.ret();
Expand Down
8 changes: 8 additions & 0 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ pub const GS_PRIMS_PER_GROUP: u32 = 21;
pub const IO_GS_PRIMS: usize = 110;
pub const IO_GS_VERTICES: usize = 111;
pub const IO_UMA_BASE_HI: usize = 112;
/// io slot (trap-temp range) of a compute dispatch whose groups were folded: the console's group
/// count in X, 0 when not folded. The console takes up to 2^32 − 1 groups a dimension, D3D12
/// 65 535 – a dispatch of more in X (and 1 in Y) runs as 65 535 × ⌈n / 65 535⌉ groups, and
/// the translated program takes `gid.x + gid.y · 65 535` as its group in X, leaving out the
/// groups at or past `n` (a retail game's 72 512 × 1 × 1 hung the GPU).
pub const IO_GROUP_LIMIT: usize = 113;
/// Groups a folded dispatch has in X.
pub const FOLD_X: u32 = 65_535;

impl GsInterface {
/// bytes of one ES vertex in LDS (ESGS item: 16 per ES output register)
Expand Down
8 changes: 8 additions & 0 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ fn transition(r: &ID3D12Resource, before: D3D12_RESOURCE_STATES, after: D3D12_RE
}

/// Name of the GPU the kernels run on (None: no usable GPU).
/// The tests' device (with the Agility SDK when there is one): a test that makes its own device
/// first leaves the SDK unappliable for the process (0x887E0003), and the GPU tests after it skip.
pub fn device() -> Result<ID3D12Device, String> {
let mut g = GPU.lock().unwrap();
let gpu = g.get_or_insert_with(init);
gpu.as_ref().map(|x| x.dev.clone()).map_err(|x| x.clone())
}

pub fn adapter() -> Result<String, String> {
let mut g = GPU.lock().unwrap();
let gpu = g.get_or_insert_with(init);
Expand Down
56 changes: 56 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7366,6 +7366,62 @@ fn gpu_compute_tg_size_counts_a_partial_last_wave() {
}
}

/// A dispatch of more groups in X than D3D12 takes, folded into Y (`IO_GROUP_LIMIT`): each of
/// the console's groups runs once with its own id in X, none past its count; with no count in io
/// the groups are the dispatch's own (a retail game's 72 512 × 1 × 1 hung the GPU unfolded).
/// The HLSL path, then the direct translator (`gpu::dxil`, wave32 / wave64 / split wave64) and
/// its per-lane form (`lower_dxil`): `bind` and `clamp` fold for every translated pipeline.
#[test]
fn gpu_compute_folded_dispatch_runs_each_console_group_once() {
if !gpu_ready() {
return;
}
// global_store_dword [s2 * 4], s2 (s2: the group id in X after user SGPRs s0, s1)
let p = prog(&[
("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 2)], None),
("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 6), ("SRC0", 130), ("VSRC1", 4)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 7), ("SRC0", 128)], None),
("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 6), ("DATA", 4), ("SADDR", 0x7D)], None),
]);
let fold = super::FOLD_X;
// each console group once, none past the count; unfolded, the dispatch's own groups
let check = |dxil: &[u8], what: &str| {
for n in [72_512u32, fold + 1] {
let rows = n.div_ceil(fold);
let mut io = vec![0u32; 128];
io[super::IO_GROUP_LIMIT] = n;
let mem = runner::dispatch(dxil, [fold, rows, 1], &io, &vec![0xFFFF_FFFFu32; (fold * rows) as usize]).unwrap_or_else(|e| panic!("{what}: {e}"));
for (k, &got) in mem.iter().enumerate() {
let want = if (k as u32) < n { k as u32 } else { 0xFFFF_FFFF };
assert_eq!(got, want, "{what}: {n} groups folded into {fold} x {rows}, group {k}");
}
}
let mem = runner::dispatch(dxil, [100, 1, 1], &[0u32; 128], &vec![0xFFFF_FFFFu32; 128]).unwrap();
for (k, &got) in mem.iter().enumerate() {
assert_eq!(got, if k < 100 { k as u32 } else { 0xFFFF_FFFF }, "{what}: unfolded group {k}");
}
};
let cs = super::CsInterface { threads: [1, 1, 1], user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1 };
// the native wave64 forms need a GPU that runs wave64 (the split form runs anywhere)
let wave64_ok = runner::wave64_supported() == Ok(true);
for wave64 in [false, true] {
let t = translate(&p, &on_gpu(Options { mode: Mode::Compute(cs.clone()), wave64, ..Default::default() })).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}
{}", t.hlsl));
check(&dxil, &format!("HLSL, wave64 {wave64}"));
if !wave64 || wave64_ok {
let dxil = super::lower_dxil::compute_code(&p, &cs, wave64).unwrap();
check(&dxil, &format!("per-lane, wave64 {wave64}"));
}
}
for (wave64, split) in [(false, false), (true, false), (true, true)].into_iter().filter(|&(w, s)| !w || s || wave64_ok) {
let o = crate::gpu::dxil::kernel::KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 };
let k = crate::gpu::dxil::kernel::translate_compute(&p, o, &cs).unwrap();
check(&k.dxil, &format!("direct, wave64 {wave64}, split {split}"));
}
}

/// v_fract_f32 at the edges, as RDNA computes it (x - floor(x) kept below 1.0): ±inf gives
/// NaN and a NaN stays NaN – a `min()` clamp returned 0.99999994 there on NVIDIA (HLSL min
/// returns its non-NaN operand); a tiny negative value rounds to the largest float below 1.
Expand Down
Loading
Loading