From 5003116e91c039be335a8544f807bfe5ff9a559f Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:33:21 -0500 Subject: [PATCH] Recompiler: XCL_DECLARED_LDS=1 sizes direct compute LDS from the header The direct DXIL translator gave every compute program that uses LDS the whole 8,192-dword groupshared block D3D12 allows, with the split form's exchange words at its top. With XCL_DECLARED_LDS=1 it declares what the program's header asks for instead: the static workgroup LDS from COMPUTE_PGM_RSRC2 (CsInterface::lds_dwords), plus the exchange words and counters of the split form (or the arrival counter of a native 2-D/3-D group) placed after it. A header with no size, or one too big to leave room for them, keeps the 8,192-dword layout. LDS addresses are clamped to the declared top. The translated-shader caches and ComputePolicyKey key the switch apart. Less groupshared memory per group lets more groups run at once. On the title (RTX 5070 Ti, Series X profile, per-program GPU timestamps over 187-229 s), the late frame's compute went from about 89 to about 78 ms per present with the switch on; 66 of the late programs translate differently (the split family and wave32 programs), and with the switch off the translation is byte-identical. Tests: compute_lds_reservation_matches_descriptor_and_lane_storage (the declared size and lane storage per mode), declared_lds_disk_cache_separates_both_direct_routes (the cache keys), and every_compute_build_input_splits_the_key (ComputePolicyKey). Written by reed on our team branch; rebased onto main. Co-Authored-By: Claude Opus 5.5 Assisted-by: Antigravity (Gemini 4 Neon) --- crates/xcl/src/gpu/dxil/core_tests.rs | 48 +++++++++++++++++-- crates/xcl/src/gpu/dxil/kernel.rs | 17 ++++++- crates/xcl/src/gpu/dxil/lanes.rs | 29 ++++++----- crates/xcl/src/gpu/dxil/lower.rs | 2 +- crates/xcl/src/gpu/dxil/lower_mem.rs | 2 +- crates/xcl/src/gpu/dxil/robustness_tests.rs | 8 ++-- crates/xcl/src/gpu/recomp/link_tests.rs | 4 +- crates/xcl/src/gpu/recomp/lower.rs | 4 +- crates/xcl/src/gpu/recomp/mod.rs | 3 ++ crates/xcl/src/gpu/recomp/pso.rs | 38 +++++++++++++++ crates/xcl/src/gpu/recomp/tests.rs | 46 +++++++++--------- .../src/hle/d3d12x/real/compute_build_key.rs | 2 + 12 files changed, 150 insertions(+), 53 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index 3b9095b3a3..a0136ad018 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -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`) @@ -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() @@ -913,7 +913,7 @@ fn count_phis(dis: &str, cap: &str, b: &str) -> Vec { #[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), @@ -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}"); @@ -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 { @@ -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}"); + } +} diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 432c8cac49..10e74b7b57 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -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 = 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 { @@ -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 } @@ -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); diff --git a/crates/xcl/src/gpu/dxil/lanes.rs b/crates/xcl/src/gpu/dxil/lanes.rs index a387480815..abf6d87046 100644 --- a/crates/xcl/src/gpu/dxil/lanes.rs +++ b/crates/xcl/src/gpu/dxil/lanes.rs @@ -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); @@ -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); diff --git a/crates/xcl/src/gpu/dxil/lower.rs b/crates/xcl/src/gpu/dxil/lower.rs index d2dd21d95d..12e3957d19 100644 --- a/crates/xcl/src/gpu/dxil/lower.rs +++ b/crates/xcl/src/gpu/dxil/lower.rs @@ -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, pub lds_top: u32, /// DPP write enable of the instruction being lowered diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index 1196b9dab0..f12bfa9347 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -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 { diff --git a/crates/xcl/src/gpu/dxil/robustness_tests.rs b/crates/xcl/src/gpu/dxil/robustness_tests.rs index 1ea3bb84cc..662349c860 100644 --- a/crates/xcl/src/gpu/dxil/robustness_tests.rs +++ b/crates/xcl/src/gpu/dxil/robustness_tests.rs @@ -69,7 +69,7 @@ fn translate_all() -> Vec> { 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)] { @@ -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]), @@ -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 @@ -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"); diff --git a/crates/xcl/src/gpu/recomp/link_tests.rs b/crates/xcl/src/gpu/recomp/link_tests.rs index b2e99dfab9..c28b6acf4f 100644 --- a/crates/xcl/src/gpu/recomp/link_tests.rs +++ b/crates/xcl/src/gpu/recomp/link_tests.rs @@ -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 { @@ -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)> = 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); diff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs index 9cdf0f441d..a59e5380da 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -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. @@ -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 }; diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 3338d91e2d..2f7ee9c200 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -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 @@ -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], diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index 3e40e95de6..30e64b005d 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -39,6 +39,7 @@ pub(crate) struct ComputePolicyKey { pub direct_compact: Option, pub direct_compact_nest: Option, pub direct_piece_limit: usize, + pub declared_lds: bool, pub per_lane: bool, pub disabled_dxil_optimizations: u8, } @@ -60,6 +61,7 @@ pub(crate) fn compute_policy_key() -> ComputePolicyKey { direct_compact: crate::gpu::dxil::kernel::compact_mode(), direct_compact_nest: crate::gpu::dxil::kernel::compact_nest_tag(), direct_piece_limit: crate::gpu::dxil::kernel::dispatch_piece_limit(), + declared_lds: crate::gpu::dxil::kernel::declared_lds_enabled(), per_lane: crate::gpu::recomp::lower_dxil::enabled(), disabled_dxil_optimizations: ["phi", "cse", "dce", "merge"] .iter() @@ -348,6 +350,9 @@ mod translated { Backend::current(), direct_version(crate::gpu::dxil::kernel::TRANSLATOR_VERSION, env!("XCL_DIRECT_DXIL_SOURCE_ID")), ); + let id = if Backend::current() != Backend::Hlsl && crate::gpu::dxil::kernel::declared_lds_enabled() { + identity_for(&id.to_string(), "declared-lds") + } else { id }; // `XCL_DIRECT_COMPACT=0` or `1`: the compact form forced off or on is another output let id = match crate::gpu::dxil::kernel::compact_mode() { Some(on) if Backend::current() != Backend::Hlsl => identity_for(&id.to_string(), if on { "xcl-direct-compact-1" } else { "xcl-direct-compact-0" }), @@ -645,6 +650,9 @@ mod translated { Some(on) => identity_for(&id.to_string(), if on { "xcl-direct-compact-1" } else { "xcl-direct-compact-0" }), None => id, }; + let id = if crate::gpu::dxil::kernel::declared_lds_enabled() { + identity_for(&id.to_string(), "declared-lds") + } else { id }; let id = match nest_tag { Some(tag) => identity_for(&id.to_string(), tag), None => id, @@ -2184,6 +2192,36 @@ mod shader_cache_tests { std::fs::remove_dir_all(&dir).unwrap(); } + #[test] + fn declared_lds_disk_cache_separates_both_direct_routes() { + const CASE: &str = "XCL_TEST_DECLARED_LDS_CACHE"; + if let Ok(case) = std::env::var(CASE) { + let (marker, expected) = case.split_once(':').unwrap(); + let marker = marker.parse().unwrap(); + let rerouted = !crate::gpu::dxil::kernel::enabled(); + let shader = translated::cached(&dxc::cache_dir(), b"console-lds", Stage::Compute, 0, 1, true, + || Ok((compiled(marker), rerouted))).unwrap(); + assert_eq!(shader.dxil[67], expected.parse::().unwrap()); + return; + } + for direct in ["0", "1"] { + let dir = std::env::temp_dir().join(format!("xcl-declared-lds-{}-{direct}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + for (lds, case) in [("0", "1:1"), ("1", "2:2"), ("0", "9:1"), ("1", "9:2")] { + let status = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "gpu::recomp::pso::shader_cache_tests::declared_lds_disk_cache_separates_both_direct_routes"]) + .env(CASE, case) + .env("XCL_DXIL_CACHE", &dir) + .env("XCL_TRANSLATED_CACHE", "1") + .env("XCL_DIRECT_DXIL", direct) + .env("XCL_DECLARED_LDS", lds) + .status().unwrap(); + assert!(status.success(), "direct={direct}, declared={lds}, case={case}"); + } + std::fs::remove_dir_all(dir).unwrap(); + } + } + fn key(hash: u64) -> ShaderKey { ShaderKey { hash, diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index d5618eb12f..5beb11bbd6 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -5583,7 +5583,7 @@ fn gpu_compute_mode_dispatch_abi() { return; } use super::{dxc, CsInterface}; - let cs = CsInterface { threads: [16, 8, 1], user_sgprs: 2, tgid: [true, true, false], tg_size: false, tid_components: 2 }; + let cs = CsInterface { threads: [16, 8, 1], user_sgprs: 2, tgid: [true, true, false], tg_size: false, tid_components: 2, ..Default::default() }; let p = 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), // index in group @@ -7502,7 +7502,7 @@ fn split_wave64_meet_form_by_group_size() { let lane = prog(&[("V_READFIRSTLANE_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", V)], None)]); for dims in [[64u32, 1, 1], [8, 8, 1], [4, 4, 4], [40, 1, 1], [256, 1, 1], [8, 8, 4]] { let threads = dims.iter().product::(); - let cs = CsInterface { threads: dims, user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let cs = CsInterface { threads: dims, user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() }; let t = translate(&lane, &Options { mode: Mode::Compute(cs), wave64: true, split_wave64: true, no_remap: true, ..Default::default() }).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); assert!(t.hlsl.contains(&format!("#define XCL_X_THREADS {threads}u")), "{dims:?}"); @@ -7510,14 +7510,14 @@ fn split_wave64_meet_form_by_group_size() { assert!(t.hlsl.contains(&format!("#define XCL_X_SPIN {}\n", want as u8)), "{dims:?}"); } // A half without a partner (32 threads or fewer: `xcl_xn == 0`) leaves before either form. - let t = translate(&lane, &Options { mode: Mode::Compute(CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 }), wave64: true, split_wave64: true, ..Default::default() }).unwrap(); + let t = translate(&lane, &Options { mode: Mode::Compute(CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() }), wave64: true, split_wave64: true, ..Default::default() }).unwrap(); let meet = &t.hlsl[t.hlsl.find("bool xcl_x_meet()").unwrap()..]; let (leave, spin, barrier) = (meet.find("if (xcl_xn == 0u) return false;").unwrap(), meet.find("#if XCL_X_SPIN").unwrap(), meet.find("GroupMemoryBarrierWithGroupSync();").unwrap()); assert!(leave < spin && spin < barrier); // A multi-wave production entry checks both its initial EXEC exchange and an // instruction exchange before any following instruction can consume the timeout value. - 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 split = translate(&lane, &Options { mode: Mode::Compute(cs.clone()), wave64: true, split_wave64: true, no_remap: true, ..Default::default() }).unwrap(); assert!(split.hlsl.contains("static const uint XCL_X_FAIL = 0x80000000u;")); assert!(split.hlsl.contains("((o | (o + lanes) | need) & XCL_X_FAIL) != 0u")); @@ -7599,7 +7599,7 @@ fn split_wave64_lds_reads_meet_after_writes() { } // a compute group of several console waves (the counters' meet) and one of a single half for threads in [128u32, 32] { - let cs = super::CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let cs = super::CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() }; let h = hlsl(&[write, read], &Options { mode: Mode::Compute(cs), ..split.clone() }); assert!(h.contains(AFTER_WRITE) && h.contains(WROTE), "{threads} threads"); } @@ -8002,7 +8002,7 @@ fn gpu_split_wave64_local_and_exchanged_masks_in_diverging_waves() { ]); for dims in [[256u32, 1, 1], [8, 8, 4]] { let threads = dims.iter().product::(); - let cs = CsInterface { threads: dims, user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 3 }; + let cs = CsInterface { threads: dims, user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 3, ..Default::default() }; let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64: true, ..Default::default() }); let t = translate(&p, &opt).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); @@ -8082,7 +8082,7 @@ fn gpu_split_wave64_waves_diverge() { ]); for dims in [[256u32, 1, 1], [64, 1, 1], [8, 8, 4], [16, 4, 1]] { let threads = dims.iter().product::(); - let cs = CsInterface { threads: dims, user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 3 }; + let cs = CsInterface { threads: dims, user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 3, ..Default::default() }; let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64: true, ..Default::default() }); let t = translate(&p, &opt).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); @@ -8200,7 +8200,7 @@ fn gpu_split_wave64_lds_write_then_read_across_halves() { let base = 0x1000usize; let groups = 64usize; for threads in [64u32, 128, 256] { - let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 5, tgid: [true, false, false], tg_size: false, tid_components: 1 }; + let cs = CsInterface { threads: [threads, 1, 1], user_sgprs: 5, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() }; let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64: true, ..Default::default() }); let t = translate(&p, &opt).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); @@ -8257,7 +8257,7 @@ fn gpu_split_wave64_one_wave_groups_end_early() { ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 7), ("DATA", 4), ("SADDR", 0x7D)], None), ]); for threads in [64u32, 48, 40] { - 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() }; let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64: true, ..Default::default() }); let t = translate(&p, &opt).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); @@ -8321,7 +8321,7 @@ fn lds_order_program() -> Vec { } fn lds_order_cs(threads: u32) -> super::CsInterface { - super::CsInterface { threads: [threads, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1 } + super::CsInterface { threads: [threads, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() } } #[test] @@ -8522,7 +8522,7 @@ fn wave_local_remap_decision_and_console_ids() { ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 6), ("DATA", 0), ("SADDR", 0x7D)], None), ]); let translate_as = |code: &[u8], threads, tgid, no_remap| { - let cs = CsInterface { threads, user_sgprs: 2, tgid, tg_size: false, tid_components: 3 }; + let cs = CsInterface { threads, user_sgprs: 2, tgid, tg_size: false, tid_components: 3, ..Default::default() }; translate(code, &Options { mode: Mode::Compute(cs), wave64: true, split_wave64: true, no_remap, ..Default::default() }).unwrap() }; for (threads, remap, shape, group_id) in [ @@ -8557,7 +8557,7 @@ fn dispatch_thread_limits_seed_compute_exec_for_native_and_remapped_groups() { translate( &p, &Options { - mode: Mode::Compute(CsInterface { threads, user_sgprs: 2, tgid: [true; 3], tg_size: false, tid_components: 3 }), + mode: Mode::Compute(CsInterface { threads, user_sgprs: 2, tgid: [true; 3], tg_size: false, tid_components: 3, ..Default::default() }), wave64: true, split_wave64, ..Default::default() @@ -8632,7 +8632,7 @@ fn bench_split_wave64() { let mut exchanges = 0; let mut spin = false; for wave64 in [false, true] { - 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() }; let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64, ..Default::default() }); let t = translate(&p, &opt).unwrap(); // the loop's exchanges: masks read whole (`x64_masks`: not the v_cndmask @@ -8685,7 +8685,7 @@ fn gpu_compute_tg_size_counts_a_partial_last_wave() { ]); for (threads, wave64) in [(96u32, true), (96, false), (64, true), (40, false), (128, true)] { let lanes = if wave64 { 64 } else { 32 }; - let cs = super::CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: true, tid_components: 1 }; + let cs = super::CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: true, tid_components: 1, ..Default::default() }; let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64, ..Default::default() }); let t = translate(&p, &opt).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); @@ -8755,7 +8755,7 @@ fn dispatch_loop_budget_shrinks_with_the_groups() { /// the HLSL of every other program (and with it the DXIL cache's key) is as it was. #[test] fn only_compute_programs_with_a_loop_read_the_dispatch_loop_budget() { - let cs = super::CsInterface { threads: [8, 4, 2], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let cs = super::CsInterface { threads: [8, 4, 2], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() }; let compute = Options { mode: Mode::Compute(cs), wave64: false, ..Default::default() }; let looping = translate(&endless_loop_program(), &compute).unwrap(); assert!(looping.complete(), "{:?}", looping.unsupported_why); @@ -8786,7 +8786,7 @@ fn compute_loop_counts_compile() { if super::dxc::location().is_err() { return runner::skip_test("no DXC"); } - let cs = super::CsInterface { threads: [8, 4, 2], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let cs = super::CsInterface { threads: [8, 4, 2], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() }; for wave64 in [false, true] { let compute = Options { mode: Mode::Compute(cs.clone()), wave64, ..Default::default() }; for code in [endless_loop_program(), endless_loop_entered_three_times()] { @@ -8810,7 +8810,7 @@ fn gpu_compute_loop_passes_follow_the_dispatch() { } let threads = 64u32; let run = |p: &[u8], wave64: bool, slot: u32| -> u32 { - let cs = super::CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let cs = super::CsInterface { threads: [threads, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, ..Default::default() }; let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64, ..Default::default() }); let t = translate(p, &opt).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); @@ -8870,7 +8870,7 @@ fn gpu_compute_folded_dispatch_runs_each_console_group_once() { 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 }; + let cs = super::CsInterface { threads: [1, 1, 1], user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() }; // 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] { @@ -8916,7 +8916,7 @@ fn compute_prologues_leave_the_lanes_past_a_thread_limit_out_of_exec() { (false, false, [64, 1, 1], true), ] { let what = format!("wave64 {wave64}, split {split}, {threads:?}, barrier {with_barrier}"); - let cs = super::CsInterface { threads, user_sgprs: 2, tgid: [true, true, false], tg_size: false, tid_components: 2 }; + let cs = super::CsInterface { threads, user_sgprs: 2, tgid: [true, true, false], tg_size: false, tid_components: 2, ..Default::default() }; let mut lines = store.to_vec(); if with_barrier { lines.insert(0, ("S_BARRIER", "ENC_SOPP", &[], None)); @@ -8984,7 +8984,7 @@ fn gpu_compute_thread_limits_leave_the_lanes_past_them_out() { forms.push(("direct wave64", true, |l, cs| translate_compute(&code_of(l), KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }, cs).unwrap().dxil)); forms.push(("per-lane wave64", true, |l, cs| super::lower_dxil::compute_code(&code_of(l), cs, true).unwrap())); } - 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 io = |x: u32, y: u32, folded: u32| { let mut io = vec![0u32; 128]; (io[IO_THREAD_LIMIT[0]], io[IO_THREAD_LIMIT[1]], io[IO_GROUP_LIMIT]) = (x, y, folded); @@ -10416,7 +10416,7 @@ fn gpu_safe_s_barrier_multi_warp_and_early_endpgm() { ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 7), ("DATA", 5), ("SADDR", 0x7D)], None), ]); - let cs = CsInterface { threads: [128, 1, 1], user_sgprs: 2, tgid: [false, false, false], tg_size: false, tid_components: 1 }; + let cs = CsInterface { threads: [128, 1, 1], user_sgprs: 2, tgid: [false, false, false], tg_size: false, tid_components: 1, ..Default::default() }; let base = 0x100usize; let mut io = vec![0u32; 128]; io[0] = base as u32; @@ -10951,7 +10951,7 @@ fn jumps_to_outer_loop_heads_are_structured() { /// backends follow the same predicate. #[test] fn compute_globallycoherent_only_when_needed() { - let cs = super::CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1 }; + let cs = super::CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() }; let enc = |n: &str, e: &str, f: &[(&str, u64)]| encode(n, e, f, None).unwrap_or_else(|x| panic!("{n}: {x}")); let prog = |insts: &[(&str, &str, &[(&str, u64)])]| { let mut c = Vec::new(); @@ -11031,7 +11031,7 @@ fn gpu_compute_coherent_and_cached_loads() { if !gpu_ready() { return; } - let cs = super::CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1 }; + let cs = super::CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [true, false, false], tg_size: false, tid_components: 1, ..Default::default() }; let enc = |n: &str, e: &str, f: &[(&str, u64)]| encode(n, e, f, None).unwrap_or_else(|x| panic!("{n}: {x}")); // 1. Plain cached compute: each thread reads a uniform dword via SMEM (s[0:1] = 0x100) and diff --git a/crates/xcl/src/hle/d3d12x/real/compute_build_key.rs b/crates/xcl/src/hle/d3d12x/real/compute_build_key.rs index c35b505bfe..f194f3b964 100644 --- a/crates/xcl/src/hle/d3d12x/real/compute_build_key.rs +++ b/crates/xcl/src/hle/d3d12x/real/compute_build_key.rs @@ -157,6 +157,7 @@ mod tests { direct_compact: None, direct_compact_nest: None, direct_piece_limit: 0, + declared_lds: false, per_lane: false, disabled_dxil_optimizations: 0, } @@ -199,6 +200,7 @@ mod tests { mutate_policy(|p| p.direct_compact = Some(true)); mutate_policy(|p| p.direct_compact_nest = Some("outer".into())); mutate_policy(|p| p.direct_piece_limit = 1); + mutate_policy(|p| p.declared_lds = true); mutate_policy(|p| p.per_lane = true); mutate_policy(|p| p.disabled_dxil_optimizations = 1);