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
8 changes: 3 additions & 5 deletions crates/xcl/src/gpu/dxil/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ pub struct Plan {
}

/// Most 32-bit words of tables a program may need to take the form by itself.
pub const TABLE_WORDS: usize = 8192;
/// `TABLE_WORDS` on a GPU that runs only 32-lane waves.
pub const TABLE_WORDS_WAVE32: usize = 12288;
const MAX_TABLE_WORDS: usize = 12288;

impl Plan {
/// Bits of a table entry: the class, and the taken successor or the index of the pair of offsets.
Expand All @@ -48,11 +46,11 @@ pub(super) fn atomic_candidate(cfg: &Cfg) -> bool {
})
}

pub fn automatic_plan(cfg: &Cfg, lw: &Lw, split_compute: bool, tables: usize) -> Option<Plan> {
pub fn automatic_plan(cfg: &Cfg, lw: &Lw, split_compute: bool) -> Option<Plan> {
let large = large(cfg);
if !large && !(split_compute && atomic_candidate(cfg)) { return None; }
let p = plan(cfg, lw)?;
if p.words() > tables { return None; }
if p.words() > MAX_TABLE_WORDS { return None; }
let insts = |n: usize| cfg.blocks[p.order[n]].insts.len();
let all: usize = (0..p.order.len()).map(insts).sum();
let once: usize = p.rep.iter().map(|&n| insts(n)).sum();
Expand Down
44 changes: 13 additions & 31 deletions crates/xcl/src/gpu/dxil/compact_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,56 +181,38 @@ fn a_mask_read_whole_after_one_copy_only_splits_the_class() {
assert_ne!(p.class[0], p.class[2]);
}

fn two_segments(copies: usize) -> Vec<u8> {
#[test]
fn gpu_compact_inline_table_above_old_cap_checks_late_entries() {
if compact_mode().is_some() {
return;
}
match (runner::adapter(), runner::wave64_programs_supported(), crate::gpu::recomp::dxc::location()) {
(Ok(_), Ok(true), Ok(_)) => {}
(a, _, d) => return runner::skip_test(if a.is_err() { "no GPU" } else if d.is_err() { "no DXC" } else { "no wave64" }),
}
const COPIES: usize = 8192;
let i = |n: &str, e: &str, f: &[(&str, u64)]| encode(n, e, f, None).unwrap_or_else(|x| panic!("{n}: {x}"));
let mut code = Vec::new();
code.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 2)]));
for r in [2, 3] {
code.extend(i("V_MOV_B32", "ENC_VOP1", &[("VDST", r), ("SRC0", 256 + r)]));
}
for marker in [3, 4] {
for k in 0..copies {
for k in 0..COPIES {
code.extend(i("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 1)]));
code.extend(i("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 256), ("VSRC1", 1)]));
let offset = (copies - 1 - k) * 3 + 1;
let offset = (COPIES - 1 - k) * 3 + 1;
assert!(offset <= i16::MAX as usize);
code.extend(i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", offset as u64)]));
}
code.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", marker), ("SSRC0", 129)]));
}
code.extend(i("S_ENDPGM", "ENC_SOPP", &[]));
code
}

#[test]
fn a_gpu_with_64_lane_waves_keeps_the_first_table_limit() {
if compact_mode().is_some() {
return;
}
let code = two_segments(8192);
let o = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 };
let cs = crate::gpu::recomp::CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [false; 3], tg_size: false, tid_components: 1 };
let size = |wave32_only| super::kernel::translate_compute_on(&code, o, &cs, wave32_only).unwrap().dxil.len();
let (compact, plain) = (size(true), size(false));
assert!(compact * 20 < plain, "{compact} B on a GPU with 32-lane waves only, {plain} B on one with 64-lane waves");
}

#[test]
fn gpu_compact_inline_table_above_old_cap_checks_late_entries() {
if compact_mode().is_some() {
return;
}
match (runner::adapter(), runner::wave64_programs_supported(), crate::gpu::recomp::dxc::location()) {
(Ok(_), Ok(true), Ok(_)) => {}
(a, _, d) => return runner::skip_test(if a.is_err() { "no GPU" } else if d.is_err() { "no DXC" } else { "no wave64" }),
}
const COPIES: usize = 8192;
let code = two_segments(COPIES);
let o = KernelOptions { wave64: true, split: runner::split_wave64(), mem_windows: 1, sampler_base: 0 };
let cfg = super::cfg::Cfg::build(&code).unwrap();
let leaders = cfg.blocks.iter().filter_map(|b| b.insts.first().map(|&i| cfg.insts[i].pc)).collect();
let lw = super::lower::Lw::new(&cfg.insts, &leaders, o, &super::kernel::Entry::Kernel);
let p = super::compact::automatic_plan(&cfg, &lw, false, super::compact::TABLE_WORDS_WAVE32).unwrap();
let p = super::compact::automatic_plan(&cfg, &lw, false).unwrap();
assert!(p.inline && p.words() > 8192 && p.words() <= 12288);
let k = translate_full(&code, o).unwrap();
for initial in [0u32, u32::MAX - 1, 0u32.wrapping_sub(COPIES as u32), 0u32.wrapping_sub((2 * COPIES) as u32), 0u32.wrapping_sub(COPIES as u32 + 1)] {
Expand Down
28 changes: 10 additions & 18 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,39 @@ pub fn compact_nest_tag() -> Option<String> {
/// `translate_full` in the compact form (`compact = true`) or the plain one.
#[cfg(test)]
pub fn translate_full_as(code: &[u8], o: KernelOptions, compact: bool) -> Result<Kernel, String> {
translate_entry(code, o, Entry::Kernel, Some(compact), None, super::compact::TABLE_WORDS_WAVE32)
translate_entry(code, o, Entry::Kernel, Some(compact), None)
}

/// `translate_full` in the compact form with an explicit dispatch nesting policy.
#[cfg(test)]
pub fn translate_full_nested(code: &[u8], o: KernelOptions, nest: super::compact::Nest) -> Result<Kernel, String> {
translate_entry(code, o, Entry::Kernel, Some(true), Some(nest), super::compact::TABLE_WORDS_WAVE32)
translate_entry(code, o, Entry::Kernel, Some(true), Some(nest))
}

pub fn translate_full(code: &[u8], o: KernelOptions) -> Result<Kernel, String> {
translate_entry(code, o, Entry::Kernel, compact_mode(), None, super::compact::TABLE_WORDS_WAVE32)
translate_entry(code, o, Entry::Kernel, compact_mode(), None)
}

/// A mesh-mode (NGG) program: the first pass that writes one record per group into
/// `res` (the recompiler's `Mode::Mesh`; the pass-through mesh shader draws them).
pub fn translate_mesh(code: &[u8], o: KernelOptions, ms: &MsInterface) -> Result<Kernel, String> {
translate_entry(code, o, Entry::Mesh(ms.clone()), None, None, 0)
translate_entry(code, o, Entry::Mesh(ms.clone()), None, None)
}

/// A vertex shader (the recompiler's `Mode::Vertex`; wave-size independent).
pub fn translate_vertex(code: &[u8], o: KernelOptions, vs: &VsInterface) -> Result<Kernel, String> {
translate_entry(code, o, Entry::Vertex(vs.clone()), None, None, 0)
translate_entry(code, o, Entry::Vertex(vs.clone()), None, None)
}

/// A pixel shader (the recompiler's `Mode::Pixel`; wave-size independent).
pub fn translate_pixel(code: &[u8], o: KernelOptions, ps: &PsInterface) -> Result<Kernel, String> {
translate_entry(code, o, Entry::Pixel(ps.clone()), None, None, 0)
translate_entry(code, o, Entry::Pixel(ps.clone()), None, None)
}

/// A geometry-mode (ES+GS) program: the compute pass that writes one mesh record per
/// group of 21 triangles into `res` (the recompiler's `Mode::Geometry`). Always wave64.
pub fn translate_geometry(code: &[u8], o: KernelOptions, g: &GsInterface) -> Result<Kernel, String> {
translate_entry(code, KernelOptions { wave64: true, ..o }, Entry::Geometry(g.clone()), None, None, 0)
translate_entry(code, KernelOptions { wave64: true, ..o }, Entry::Geometry(g.clone()), None, None)
}

/// Whether `entry` uses the measured, one-path heap-window read form. Graphics, mesh and
Expand Down Expand Up @@ -160,13 +160,7 @@ pub enum Entry {

/// A compute-mode program (the CS ABI of `cs`).
pub fn translate_compute(code: &[u8], o: KernelOptions, cs: &CsInterface) -> Result<Kernel, String> {
translate_compute_on(code, o, cs, true)
}

/// `translate_compute` for a GPU: one that runs only 32-lane waves takes larger compact tables.
pub fn translate_compute_on(code: &[u8], o: KernelOptions, cs: &CsInterface, wave32_only: bool) -> Result<Kernel, String> {
let tables = if wave32_only { super::compact::TABLE_WORDS_WAVE32 } else { super::compact::TABLE_WORDS };
translate_entry(code, o, Entry::Compute(cs.clone()), compact_mode(), None, tables)
translate_entry(code, o, Entry::Compute(cs.clone()), compact_mode(), None)
}

fn translate_entry(
Expand All @@ -175,7 +169,6 @@ fn translate_entry(
entry: Entry,
compact: Option<bool>,
nest: Option<super::compact::Nest>,
tables: usize,
) -> Result<Kernel, String> {
// SSA construction recurses along the CFG (one level per guarded memory access between
// a register's definition and its use): a thread with a large stack for big programs
Expand All @@ -188,7 +181,7 @@ fn translate_entry(
.spawn(move || {
#[cfg(test)]
super::module::NO_SELECT_FOLD.with(|c| c.set(no_select_fold));
translate_on_this_thread(&code, o, entry, compact, nest, tables)
translate_on_this_thread(&code, o, entry, compact, nest)
})
.map_err(|e| format!("translator thread: {e}"))?
.join()
Expand Down Expand Up @@ -219,7 +212,6 @@ fn translate_on_this_thread(
entry: Entry,
compact: Option<bool>,
nest: Option<super::compact::Nest>,
tables: usize,
) -> Result<Kernel, String> {
let geo = match &entry {
Entry::Geometry(g) => Some(g.clone()),
Expand Down Expand Up @@ -313,7 +305,7 @@ fn translate_on_this_thread(
_ if !compute && !kernel => None,
Some(false) => None,
Some(true) => super::compact::plan(&cfg, &lw),
None => super::compact::automatic_plan(&cfg, &lw, compute && split, tables),
None => super::compact::automatic_plan(&cfg, &lw, compute && split),
};
if let Some(p) = &plan {
crate::xlog_info!("[SHADER] direct DXIL compact form: {} blocks of {} kinds", p.order.len(), p.rep.len());
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/recomp/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ fn direct_compute(blob: &[u8], sampler_base: u32, windows: u32, split_wave64: bo
}
}
}
let k = crate::gpu::dxil::kernel::translate_compute_on(code, o, &cs, split_wave64)?;
let k = crate::gpu::dxil::kernel::translate_compute(code, o, &cs)?;
dump_direct(blob, &k.dxil);
Ok(Compiled { dispatch_chain: k.dispatch_chain, dxil: k.dxil, samplers: k.static_samplers, dispatch_remap: None })
}
Expand Down
Loading