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
124 changes: 117 additions & 7 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn a_single_split_wave_of_a_compute_group_exchanges_without_a_group_barrier() {
let code = prog(&[("V_READLANE_B32", "ENC_VOP3", &[("VDST", 1), ("SRC0", 256), ("SRC1", 160)], None)]);
let o = KernelOptions { wave64: true, split: true, mem_windows: 1, sampler_base: 0 };
let dis = |threads: u32, code: &[u8]| {
let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 0, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 0, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
super::validate::disassemble(&super::kernel::translate_compute(code, o, &cs).unwrap_or_else(|e| panic!("{threads} threads: {e}")).dxil)
};
// (barrier mode 9: the thread group's sync with its memory fence, `ops::barrier::GROUP_SYNC`)
Expand Down Expand Up @@ -152,7 +152,7 @@ fn split_lds_reads_meet_after_writes() {
assert!(looped(&[read, write]) >= looped(&[read]) + 2, "the loop's read and write meet when the bits are set");
// compute groups: the counters' meet (two more atomics), none without a partner half
let atomics = |threads: u32, list: &[Inst]| {
let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 0, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 0, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
let o = KernelOptions { wave64: true, split: true, mem_windows: 1, sampler_base: 0 };
let d = super::validate::disassemble(&super::kernel::translate_compute(&prog(list), o, &cs).unwrap_or_else(|e| panic!("{threads} threads: {e}")).dxil).unwrap();
d.matches("atomicrmw").count()
Expand Down Expand Up @@ -1089,7 +1089,7 @@ fn count_phis(dis: &str, cap: &str, b: &str) -> Vec<String> {
#[test]
fn direct_compute_loops_count_each_entry() {
use crate::gpu::recomp::CsInterface;
let cs = CsInterface { threads: [8, 4, 2], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [8, 4, 2], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
let straight = prog(&[
("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 2)], None),
("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 6), ("SRC0", 130), ("VSRC1", 0)], None),
Expand Down Expand Up @@ -1162,7 +1162,7 @@ fn the_compute_entry_lets_only_the_lanes_below_the_thread_limits_in() {
let load = format!("i32 {}, i32 undef, i8 7, i32 4)", IO_THREAD_LIMIT[0] * 4);
for (wave64, split, threads) in [(false, false, 32), (true, false, 64), (true, true, 64), (true, true, 32)] {
let o = KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 };
let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() };
// (a split wave64 of 64 threads is two hardware waves: its barrier is the HLSL path's)
for barrier in [false, true].into_iter().filter(|&b| !(b && split && threads == 64)) {
let what = format!("wave64 {wave64}, split {split}, {threads} threads, barrier {barrier}");
Expand Down Expand Up @@ -1238,7 +1238,7 @@ fn gpu_direct_compute_loop_passes_follow_the_dispatch() {
Err(e) => return runner::skip_test(&e),
}
let threads = 64u32;
let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
for wave64 in [false, true] {
let o = KernelOptions { wave64, split: wave64, mem_windows: 1, sampler_base: 0 };
let run = |code: &[u8], slot: u32| -> u32 {
Expand Down Expand Up @@ -1367,7 +1367,7 @@ fn compute_movrel_uses_program_max_vgpr_and_masks_m0() {
// stored, so DCE keeps the relative move this test looks at
("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 1), ("DATA", 0), ("SADDR", 0x7D), ("OFFSET", 0)], None),
]);
let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
let k = super::kernel::translate_compute(&code, ko, &cs).expect("compute movrel translates");
super::validate::validate(&k.dxil).expect("validates");
if let Ok(dis) = super::validate::disassemble(&k.dxil) {
Expand All @@ -1380,7 +1380,7 @@ fn compute_movrel_uses_program_max_vgpr_and_masks_m0() {
fn compute_omits_rodata_without_s_getpc() {
use super::kernel::{translate_entry_rodata, Entry};
use crate::gpu::recomp::CsInterface;
let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
let ko = KernelOptions { wave64: false, split: false, mem_windows: 2, sampler_base: 0 };
let rodata = vec![(0x40u32, vec![0x7AB1_E000u32, 0x7AB1_E001, 0xBF81_0000])];

Expand Down Expand Up @@ -1649,3 +1649,113 @@ fn raw_buffer_oob_check_guards_32bit_wraparound() {
assert!(uge >= 4, "expected wraparound icmp uge checks for load and atomic (got {uge}):\n{dis}");
}
}

/// `f` with `XCL_DECLARED_LDS` taken as `on` by the translations of this thread.
fn with_declared_lds<R>(on: bool, f: impl FnOnce() -> R) -> R {
let before = super::kernel::DECLARED_LDS_FOR_TEST.with(|c| c.replace(Some(on)));
let r = f();
super::kernel::DECLARED_LDS_FOR_TEST.with(|c| c.set(before));
r
}

/// `XCL_DECLARED_LDS` (on by default): a compute program's groupshared block is the LDS its
/// header declares (COMPUTE_PGM_RSRC2 LDS_SIZE, 128-dword granules) and the translator's words
/// after it – a native 2-D group's arrival counter, a multi-wave barrier's two words, the split
/// form's exchange slots and counters – or the whole 8,192 dwords when the header gives no size
/// or those words would not fit. Off: always 8,192.
#[test]
fn compute_lds_is_the_declared_block_and_the_translators_words() {
use crate::gpu::recomp::CsInterface;
let write = prog(&[("DS_WRITE_B32", "ENC_DS", &[("ADDR", 0), ("DATA0", 1)], None)]);
let barrier = prog(&[("DS_WRITE_B32", "ENC_DS", &[("ADDR", 0), ("DATA0", 1)], None), ("S_BARRIER", "ENC_SOPP", &[], None)]);
let bar = if crate::gpu::recomp::emit::safe_barrier_wanted() { 2 } else { 0 };
// (threads, granules, wave64, split, program, words with the switch)
let cases: [([u32; 3], u32, bool, bool, &[u8], u32); 14] = [
([64, 1, 1], 2, false, false, &write, 256),
([8, 8, 1], 1, false, false, &write, 129),
([8, 8, 1], 1, false, false, &barrier, 129 + bar),
([64, 1, 1], 2, false, false, &barrier, 256 + bar),
// native wave64: one wave has no barrier words, two have
([64, 1, 1], 1, true, false, &barrier, 128),
([128, 1, 1], 2, true, false, &barrier, 256 + bar),
([16, 8, 1], 1, true, false, &barrier, 129 + bar),
// split: slots 4 · waves, counters 2 · waves, the arrival word (and the barrier's two)
([8, 8, 1], 1, true, true, &write, 135),
([128, 1, 1], 2, true, true, &write, 269),
([128, 1, 1], 2, true, true, &barrier, 269 + bar),
// no size, or no room for the translator's words: the whole block
([8, 8, 1], 0, true, true, &write, 8192),
([8, 8, 1], 0, false, false, &barrier, 8192),
([8, 8, 1], 64, true, true, &write, 8192),
([8, 8, 1], 64, false, false, &write, 8192),
];
for (threads, granules, wave64, split, code, words) in cases {
// the size comes from the stage descriptor (COMPUTE_PGM_RSRC2 at +0x38, LDS_SIZE at bit 15)
let mut descriptor = [0u8; 0x48];
for (i, v) in [granules << 15 | 1 << 11, threads[0], threads[1], threads[2]].into_iter().enumerate() {
descriptor[0x38 + 4 * i..0x3C + 4 * i].copy_from_slice(&v.to_le_bytes());
}
let cs = CsInterface::from_descriptor(&descriptor, 0).unwrap();
assert_eq!(cs.lds_dwords, granules * 128);
let o = KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 };
for on in [false, true] {
let what = format!("{threads:?}, {granules} granules, wave64 {wave64}, split {split}, switch {on}");
let k = with_declared_lds(on, || super::kernel::translate_compute(code, o, &cs)).unwrap_or_else(|e| panic!("{what}: {e}"));
super::validate::validate(&k.dxil).unwrap_or_else(|e| panic!("{what}: {e}"));
let dis = super::validate::disassemble(&k.dxil).unwrap();
let want = if on { words } else { 8192 };
assert!(dis.contains(&format!("@xcl_lds = external addrspace(3) global [{want} x i32]")), "{what}: {want} words");
}
}
}

/// The declared block on the GPU (`XCL_DECLARED_LDS`), with the switch on and off: a 16 x 8
/// group (several hardware waves: a native 2-D group's arrival counter and the safe barrier's
/// words after its 256 dwords, or the split form's exchange words) whose threads first write
/// past the declared LDS (kept at its last dword, not over the translator's words: the clamp
/// itself is `declared_lds_accesses_stop_below_the_translators_words`'s), then their index,
/// meet at a group barrier and read another thread's. Native wave32, native wave64 (on a GPU
/// that runs it) and the split wave64 form.
#[test]
fn gpu_compute_declared_lds_keeps_the_translators_words() {
use crate::gpu::recomp::{runner, CsInterface, KERNEL_SGPRS};
match runner::wave32_supported() {
Ok(true) => {}
Ok(false) => return runner::skip_test("no wave32"),
Err(e) => return runner::skip_test(&e),
}
const V: u64 = 256;
let code = prog(&[
("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 132), ("VSRC1", 1)], None), // y · 16
("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", V + 2), ("VSRC1", 0)], None), // i
("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 3), ("SRC0", 130), ("VSRC1", 2)], None), // 4i
// dword 256 + i: past the declared 256 (unclamped, over the translator's words)
("DS_WRITE_B32", "ENC_DS", &[("ADDR", 3), ("DATA0", 3), ("OFFSET1", 4)], None),
("DS_WRITE_B32", "ENC_DS", &[("ADDR", 3), ("DATA0", 2)], None),
("S_WAITCNT", "ENC_SOPP", &[("SIMM16", 0)], None),
("S_BARRIER", "ENC_SOPP", &[], None),
("V_SUB_NC_U32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 255), ("VSRC1", 2)], Some(127)), // 127 - i
("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 5), ("SRC0", 130), ("VSRC1", 4)], None),
("DS_READ_B32", "ENC_DS", &[("VDST", 6), ("ADDR", 5)], None),
("S_WAITCNT", "ENC_SOPP", &[("SIMM16", 0)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 8), ("SRC0", V + 3)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 9), ("SRC0", 128)], None),
("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 8), ("DATA", 6), ("SADDR", 0x7D)], None),
]);
let cs = CsInterface { threads: [16, 8, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 2, lds_dwords: 256 };
let native64 = runner::wave64_supported() == Ok(true);
for (wave64, split) in [(false, false), (true, false), (true, true)].into_iter().filter(|&(w, s)| !w || s || native64) {
let o = KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 };
for on in [false, true] {
let what = format!("wave64 {wave64}, split {split}, switch {on}");
let k = with_declared_lds(on, || super::kernel::translate_compute(&code, o, &cs)).unwrap_or_else(|e| panic!("{what}: {e}"));
let mem = runner::dispatch(&k.dxil, [1, 1, 1], &vec![0u32; KERNEL_SGPRS], &vec![0xFFFF_FFFFu32; 128]).expect("GPU run");
for i in 0..128 {
assert_eq!(mem[i], 127 - i as u32, "{what}: thread {i}");
}
}
}
if !native64 {
runner::skip_test("no wave64");
}
}
36 changes: 33 additions & 3 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ pub struct KernelOptions {
/// (the `gpu/dxil` sources are not in build.rs's translator source hash).
pub const TRANSLATOR_VERSION: u32 = 12;

/// `XCL_DECLARED_LDS` (default on; `=0` off): a compute program's groupshared block is the LDS
/// its header declares (`CsInterface::lds_dwords`) plus the translator's own words after it,
/// instead of the whole 8,192 dwords D3D12 allows (still so when the header gives no size). A
/// one-wave group holding 32 KB let an RDNA WGP (128 KB) run 4 groups: one wave a SIMD. The
/// caches and `ComputePolicyKey` key it apart. (After xcl-dev/xcl#2937, Kalmarv, there opt-in.)
pub fn declared_lds_enabled() -> bool {
#[cfg(test)]
if let Some(on) = DECLARED_LDS_FOR_TEST.with(|c| c.get()) {
return on;
}
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var("XCL_DECLARED_LDS").map_or(true, |v| v.trim() != "0"))
}

#[cfg(test)]
thread_local! {
/// `declared_lds_enabled` for the translations of this test thread
pub(crate) static DECLARED_LDS_FOR_TEST: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
}

/// Console compute shaders (and the kernel tests) go through this translator, with the HLSL
/// path as the fallback for what it cannot translate yet: the default, and `XCL_DIRECT_DXIL=1`.
/// `XCL_DIRECT_DXIL=0` keeps every stage on the HLSL path.
Expand Down Expand Up @@ -252,12 +272,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 declared_lds = DECLARED_LDS_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)]
DECLARED_LDS_FOR_TEST.with(|c| c.set(declared_lds));
translate_on_this_thread(&code, o, entry, compact, nest, rodata, ps_rt32_mask)
})
.map_err(|e| format!("translator thread: {e}"))?
Expand Down Expand Up @@ -829,7 +853,13 @@ impl Lw {
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);
// `XCL_DECLARED_LDS`: the program's own LDS, then the translator's words; a native 2-D
// / 3-D group's arrival counter (`setup_lanes_compute`) is one of them
let (declared, native_counter) = match entry {
Entry::Compute(cs) if declared_lds_enabled() => (cs.lds_dwords, !split && (cs.threads[1] > 1 || cs.threads[2] > 1)),
_ => (0, false),
};
lw.setup_storage(uses_lds, if split { slot } else { 1 }, safe_bar, declared, native_counter);
lw
}

Expand Down Expand Up @@ -984,7 +1014,7 @@ fn a_split_wave64_compute_program_stays_whole() {
fn a_dispatch_chain_leaves_the_lanes_past_a_thread_limit_out() {
use crate::gpu::recomp::{runner, IO_THREAD_LIMIT};
let threads = [32, 1, 1];
let cs = CsInterface { threads, user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads, user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() };
let options = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 };
let mut lw = Lw::new(&[], &Default::default(), options, &Entry::Compute(cs.clone()));
let (tid, gtid) = lw.setup_lanes_compute(threads, false);
Expand Down Expand Up @@ -1492,7 +1522,7 @@ fn gpu_compact_slots_use_the_dispatch_prologues_exec_mask() {
] {
code.extend(encode(name, enc, fields, None).unwrap());
}
let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 0, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 0, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
let options = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 };
let kernel = translate_entry(&code, options, Entry::Compute(cs), Some(true), None).unwrap();
let initial = [0xa5a5_5a5a; 32];
Expand Down
35 changes: 25 additions & 10 deletions crates/xcl/src/gpu/dxil/lanes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,43 @@ impl Lw {
/// 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) {
/// 32 KB, the whole LDS), as the HLSL kernel lays it out. With `declared`
/// dwords (`XCL_DECLARED_LDS`, 0 = not known) the block is the program's
/// LDS followed by those words (and a native 2-D / 3-D group's arrival
/// counter, `native_counter`), when they fit in 8,192.
pub fn setup_storage(&mut self, lds: bool, slot: u32, safe_bar: bool, declared: u32, native_counter: bool) {
let i32 = self.s.m.i32();
self.xslot = slot.max(1);
if lds {
let g = self.s.groupshared("xcl_lds", i32, 8192);
// the translator's words at the top: [exchange slots, counters (split)],
// [XCL_BAR_ALIVE, XCL_BAR_STATE (safe_bar)], XCL_XW / XCL_WT
let bar_words = if safe_bar { 2 } else { 0 };
let slots = 4 * self.xslot * self.waves;
let control = if self.split { slots + 2 * self.waves + 1 + bar_words } else { bar_words + u32::from(native_counter) };
let fits = declared != 0 && declared <= 8192u32.saturating_sub(control);
let words = if fits { declared + control } else { 8192 };
let g = self.s.groupshared("xcl_lds", i32, words as u64);
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));
// matching `recomp::emit::safe_barrier_prelude`; with the declared LDS of a
// native group right after the program's
self.safe_bar = Some((g, if fits && !self.split { declared } else { words - 3 }));
}
if self.split {
// 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 - bar_words;
let base = words - control;
self.lds_top = base - 1;
self.xs = Some((g, base));
self.xc = Some((g, base + slots));
self.xw = Some((g, 8191));
self.xw = Some((g, words - 1));
} else if fits {
// `lds_index` clamps below the translator's words (`lds_top` < 8191)
self.lds_top = declared - 1;
if native_counter {
self.xw = Some((g, words - 1));
}
} else {
self.lds_top = if safe_bar { 8188 } else { 8191 };
}
Expand Down Expand Up @@ -133,7 +148,7 @@ impl Lw {
} else if y * z > 1 {
let i32 = self.s.m.i32();
let (g, at) = match (self.lds, lds_top_word) {
(Some(l), true) => (l, 8191),
(Some(l), true) => self.xw.unwrap_or((l, 8191)),
_ => (self.s.groupshared("xcl_wt", i32, 1), 0),
};
let ai = self.c(at);
Expand Down
Loading
Loading