Skip to content
Closed
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
48 changes: 43 additions & 5 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 @@ -913,7 +913,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 @@ -986,7 +986,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 @@ -1062,7 +1062,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 @@ -1407,3 +1407,41 @@ fn raw_loads_in_root_windows_respect_exec_and_bounds() {
}
}
}

#[test]
fn compute_lds_reservation_matches_descriptor_and_lane_storage() {
// Exercise both cache modes on ordinary CI without changing the test process environment.
if std::env::var_os("XCL_TEST_LDS_CHILD").is_none() {
for mode in ["0", "1"] {
let status = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "gpu::dxil::core_tests::compute_lds_reservation_matches_descriptor_and_lane_storage"])
.env("XCL_DECLARED_LDS", mode)
.env("XCL_TEST_LDS_CHILD", "1")
.status().unwrap();
assert!(status.success(), "declared LDS mode {mode}");
}
return;
}
use crate::gpu::recomp::CsInterface;
let code = prog(&[("DS_WRITE_B32", "ENC_DS", &[("ADDR", 0), ("DATA0", 1)], None)]);
for (threads, blocks, split, expected) in [
([64, 1, 1], 2u32, false, 256),
([8, 8, 1], 1, false, 129),
([8, 8, 1], 1, true, 135),
([128, 1, 1], 2, true, 269),
([8, 8, 1], 0, true, 8192),
([8, 8, 1], 64, true, 8192),
] {
let mut descriptor = [0u8; 0x48];
for (i, v) in [blocks << 15, 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, blocks * 128);
let options = KernelOptions { wave64: split, split, mem_windows: 1, sampler_base: 0 };
let dxil = super::kernel::translate_compute(&code, options, &cs).unwrap().dxil;
let dis = super::validate::disassemble(&dxil).unwrap();
let words = if super::kernel::declared_lds_enabled() { expected } else { 8192 };
assert!(dis.contains(&format!("@xcl_lds = external addrspace(3) global [{words} x i32]")), "{threads:?}, {blocks}, split={split}");
}
}
17 changes: 15 additions & 2 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ pub struct KernelOptions {
/// (the `gpu/dxil` sources are not in build.rs's translator source hash).
pub const TRANSLATOR_VERSION: u32 = 12;

/// Opt in for dispatches whose guest LDS contract is fixed by the pipeline header.
pub fn declared_lds_enabled() -> bool {
static ENABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ENABLED.get_or_init(|| std::env::var("XCL_DECLARED_LDS").is_ok_and(|value| value == "1"))
}

/// `XCL_DIRECT_DXIL=1`: console compute shaders (and the kernel tests) go through this
/// translator, with the HLSL path as the fallback for what it cannot translate yet.
pub fn enabled() -> bool {
Expand Down Expand Up @@ -747,7 +753,14 @@ impl Lw {
_ => Partner::Spin,
};
}
lw.setup_storage(uses_lds, if split { slot } else { 1 });
let (declared, native_counter) = match entry {
Entry::Compute(cs) => {
let declared = if declared_lds_enabled() { cs.lds_dwords } else { 0 };
(declared, !split && (cs.threads[1] > 1 || cs.threads[2] > 1))
}
_ => (0, false),
};
lw.setup_storage(uses_lds, if split { slot } else { 1 }, declared, native_counter);
lw
}

Expand Down Expand Up @@ -902,7 +915,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
29 changes: 16 additions & 13 deletions crates/xcl/src/gpu/dxil/lanes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ 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) {
/// Put private lane-exchange words after the guest LDS. D3D12 permits
/// 8192 dwords in total, including any arrival counter for a native 2-D group.
pub fn setup_storage(&mut self, lds: bool, slot: u32, 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);
let slots = 4 * self.xslot * self.waves;
let control = if self.split { slots + 2 * self.waves + 1 } else { u32::from(native_counter) };
// Unspecified or oversized reservations retain the existing 32 KiB layout.
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, u64::from(words));
self.lds = Some(g);
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
let slots = 4 * self.xslot * self.waves;
let base = 8192 - slots - 2 * self.waves - 1;
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 {
self.lds_top = 8191;
self.lds_top = if fits { declared - 1 } else { 8191 };
if native_counter {
self.xw = Some((g, words - 1));
}
}
} else if self.split {
let w = self.s.groupshared("xcl_xw", i32, 1);
Expand Down Expand Up @@ -120,7 +123,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(_), true) => self.xw.expect("compute LDS arrival counter"),
_ => (self.s.groupshared("xcl_wt", i32, 1), 0),
};
let ai = self.c(at);
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub struct Lw {
pub live_exec: Option<(Value, Value)>,
/// words per half in an exchange
pub xslot: u32,
/// the program's LDS (`xcl_lds`, 8192 dwords) and its last usable dword
/// the program's LDS (`xcl_lds`) and its last usable guest dword
pub lds: Option<super::ops::GroupShared>,
pub lds_top: u32,
/// DPP write enable of the instruction being lowered
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,7 @@ impl Lw {
fn lds_index(&mut self, at: Value) -> Value {
let i = self.lshrc(at, 2);
let i = self.andc(i, 8191);
if self.split {
if self.lds_top < 8191 {
let k = self.c(self.lds_top);
self.umin(i, k)
} else {
Expand Down
8 changes: 4 additions & 4 deletions crates/xcl/src/gpu/dxil/robustness_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn translate_all() -> Vec<Vec<u8>> {
input_ena: 0x302,
attrs: None,
};
let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() };
let o = |wave64, split| KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 };
let mut out = Vec::new();
for (w, s) in [(false, false), (true, true), (true, false)] {
Expand Down Expand Up @@ -103,7 +103,7 @@ fn translation_is_byte_identical_across_runs_and_threads() {
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 };
let cs = |threads| CsInterface { threads, user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() };
let unsafe_groups = [
(true, true, [16, 16, 1]), (true, false, [16, 16, 1]), (false, false, [16, 16, 1]),
(true, true, [33, 1, 1]), (true, false, [65, 1, 1]), (false, false, [33, 1, 1]),
Expand Down Expand Up @@ -143,7 +143,7 @@ fn a_group_barrier_of_several_hardware_waves_is_left_to_the_hlsl_path() {
fn split_wave64_real_lds_is_left_to_the_hlsl_path() {
let write = prog(&[("DS_WRITE_B32", "ENC_DS", &[("ADDR", 0), ("DATA0", 1)], None)]);
let swizzle = prog(&[("DS_SWIZZLE_B32", "ENC_DS", &[("VDST", 1), ("ADDR", 0), ("OFFSET0", 0x1F), ("OFFSET1", 4)], None)]);
let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() };
let o = |wave64, split| KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 };

// the direct translator orders split LDS itself (`XCL_X_LDS_ORDER`, on by default); without
Expand All @@ -164,7 +164,7 @@ fn split_wave64_real_lds_is_left_to_the_hlsl_path() {
fn split_wave64_spin_uses_fail_closed_direct_dxil() {
const V: u64 = 256;
let code = prog(&[("V_READFIRSTLANE_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", V)], None)]);
let cs = CsInterface { threads: [128, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [128, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
let options = |split| KernelOptions { wave64: true, split, mem_windows: 1, sampler_base: 0 };
kernel::translate_compute(&code, options(true), &cs).expect("counter-paired direct DXIL translation");
kernel::translate_compute(&code, options(false), &cs).expect("native wave64 translation");
Expand Down
4 changes: 2 additions & 2 deletions crates/xcl/src/gpu/recomp/link_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn bench_memory_window_accessors() {
for round in 0..3 {
for windows in [1, 4] {
nonce = nonce.wrapping_mul(0x9E37_79B1) | 1;
let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 24, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 24, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
let t = translate(&program(n, nonce), &on_gpu(Options { mode: Mode::Compute(cs), wave64: true, mem_windows: windows, ..Default::default() })).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
let forms = if windows == 1 {
Expand Down Expand Up @@ -212,7 +212,7 @@ fn linked_programs_keep_their_declarations() {
let list = [buf("BUFFER_LOAD_DWORD", 2, coherent), buf("BUFFER_STORE_DWORD", 3, false)];
let refs: Vec<(&str, &str, &[(&str, u64)], Option<u32>)> = list.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect();
let p = prog(&refs);
let cs = super::CsInterface { threads: [8, 4, 2], user_sgprs: 16, tgid: [false; 3], tg_size: false, tid_components: 1 };
let cs = super::CsInterface { threads: [8, 4, 2], user_sgprs: 16, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() };
let t = translate(&p, &Options { mode: Mode::Compute(cs), wave64, mem_windows: 4, ..Default::default() }).unwrap();
let size = if wave64 { 64 } else { 32 };
assert!(t.hlsl.contains(&format!("[WaveSize({size})]")), "{}", t.hlsl);
Expand Down
4 changes: 2 additions & 2 deletions crates/xcl/src/gpu/recomp/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,7 @@ pub(crate) mod tests {
}

pub(crate) fn compute_cs() -> crate::gpu::recomp::CsInterface {
crate::gpu::recomp::CsInterface { threads: [64, 1, 1], user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1 }
crate::gpu::recomp::CsInterface { threads: [64, 1, 1], user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() }
}

/// A compute program run on the CPU over `groups` groups (wave32): the memory after.
Expand Down Expand Up @@ -3194,7 +3194,7 @@ pub(crate) mod tests {
}
mem
};
let cs = |threads: [u32; 3], y: bool| CsInterface { threads, user_sgprs: 2, tgid: [true, y, false], tg_size: false, tid_components: if y { 2 } else { 1 } };
let cs = |threads: [u32; 3], y: bool| CsInterface { threads, user_sgprs: 2, tgid: [true, y, false], tg_size: false, tid_components: if y { 2 } else { 1 }, ..Default::default() };
let limit = |x: u32, y: u32| vec![(IO_THREAD_LIMIT[0], x), (IO_THREAD_LIMIT[1], y)];
for wave64 in [false, true] {
let lanes = if wave64 { 64 } else { 32 };
Expand Down
3 changes: 3 additions & 0 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ impl GsInterface {
/// Compute shader with the console's dispatch ABI (see the module docs).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CsInterface {
/// Static workgroup LDS from COMPUTE_PGM_RSRC2, in dwords. Zero means unspecified.
pub lds_dwords: u32,
pub threads: [u32; 3],
pub user_sgprs: u32,
/// TGID_X / Y / Z enabled: the group id follows the user SGPRs in this order
Expand All @@ -299,6 +301,7 @@ impl CsInterface {

fn from_rsrc2(threads: [u32; 3], r: u32) -> Self {
Self {
lds_dwords: ((r >> 15) & 0x1ff) * 128,
threads,
user_sgprs: (r >> 1) & 31,
tgid: [(r >> 7) & 1 == 1, (r >> 8) & 1 == 1, (r >> 9) & 1 == 1],
Expand Down
Loading
Loading