From 52577f993895afe1cc3e492a063b6f73821ee57c Mon Sep 17 00:00:00 2001 From: grahamster2 <128315603+grahamster2@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:32:16 -0500 Subject: [PATCH] Recompiler: size a compute program's LDS block from its LDS_SIZE Every translated program that touches LDS declared `groupshared uint xcl_lds[8192]`, the D3D12 limit of 32 KB, whatever the console program allocated. COMPUTE_PGM_RSRC2.LDS_SIZE (bits 15..23, 512-byte units) is in the FSSM descriptor already: the HLSL block of a compute program is now the next power of two that holds it and the runtime words kept at the top (the split-wave64 exchange, the safe barrier's words, the 2-D thread hand-out), with accesses masked by `XCL_LDS_MASK`. LDS_SIZE 0 and the other stages keep 32 KB. 32 KB per group lets an NVIDIA Ampere SM hold 3 groups, 6 warps of a 64-thread group, so the split form's meets have nothing to hide behind. On an RTX 3080, a loop with an LDS store and load and one exchange per iteration (2^18 threads x 8192 iterations, 64-thread groups) takes 41.9 ms in the split form with the 32 KB block and 14.0 ms with a 1K-word one; native wave32 10.0 -> 4.2 ms. The retail title's 84 LDS-using split programs allocate 512 B to 16.9 KB (34 of them 512 B). Assisted-by: Claude Code (Claude Opus 5.5) --- crates/xcl/src/gpu/dxil/robustness_tests.rs | 2 +- crates/xcl/src/gpu/recomp/emit.rs | 88 ++++++++++---- crates/xcl/src/gpu/recomp/lower.rs | 2 +- crates/xcl/src/gpu/recomp/mod.rs | 4 + crates/xcl/src/gpu/recomp/tests.rs | 123 +++++++++++++++++++- 5 files changed, 186 insertions(+), 33 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/robustness_tests.rs b/crates/xcl/src/gpu/dxil/robustness_tests.rs index 46a06c63d4..b5d7923bc7 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, lds_bytes: 0 }; 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)] { diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043f..d5753fec1b 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -4414,11 +4414,11 @@ impl Em { let m0 = self.sreg(124); let at = format!("(({m0} & 0xFFFFu) + {off16}u + 4u * lane_id)"); let slot = if self.x64() { - format!("xcl_lds[min((({at}) >> 2) & 8191u, XCL_X_LDS_TOP)]") + format!("xcl_lds[min((({at}) >> 2) & XCL_LDS_MASK, XCL_X_LDS_TOP)]") } else if self.safe_barrier { - format!("xcl_lds[min((({at}) >> 2) & 8191u, XCL_LDS_TOP)]") + format!("xcl_lds[min((({at}) >> 2) & XCL_LDS_MASK, XCL_LDS_TOP)]") } else { - format!("xcl_lds[(({at}) >> 2) & 8191u]") + format!("xcl_lds[(({at}) >> 2) & XCL_LDS_MASK]") }; if name == "ds_write_addtid_b32" { let src = self.vreg(f("DATA0")); @@ -4442,11 +4442,11 @@ impl Em { let safe_bar = self.safe_barrier; let lds = |at: String| { if x64 { - format!("xcl_lds[min((({at}) >> 2) & 8191u, XCL_X_LDS_TOP)]") + format!("xcl_lds[min((({at}) >> 2) & XCL_LDS_MASK, XCL_X_LDS_TOP)]") } else if safe_bar { - format!("xcl_lds[min((({at}) >> 2) & 8191u, XCL_LDS_TOP)]") + format!("xcl_lds[min((({at}) >> 2) & XCL_LDS_MASK, XCL_LDS_TOP)]") } else { - format!("xcl_lds[(({at}) >> 2) & 8191u]") + format!("xcl_lds[(({at}) >> 2) & XCL_LDS_MASK]") } }; let mut st = Vec::new(); @@ -4601,24 +4601,61 @@ impl Em { self.x64() && (self.uses_lds || matches!(self.opt.mode, Mode::Geometry(_))) } + /// Split wave64: the words of the exchange in the top of `xcl_lds` – slots (console wave, + /// half, parity), the arrival counters (console wave, half), the wave counter of + /// `xcl_x_thread`. + fn x64_slots_counters(&self) -> (usize, usize) { + let waves = self.x64_threads().div_ceil(64).max(1) as usize; + (waves * 4 * self.x64_words, waves * 2) + } + + /// Dwords of `xcl_lds`. The console sets a compute program's LDS allocation in + /// COMPUTE_PGM_RSRC2 (`CsInterface::lds_bytes`); a block of the D3D12 limit (32 KB) for a + /// program that needs 512 bytes caps the groups an SM / WGP holds at once (3 per NVIDIA + /// Ampere SM: 6 warps of a 64-thread group, with nothing to hide the split-wave64 meets + /// behind). Known: the next power of two that holds it and the runtime words kept at the + /// top (the exchange, the safe barrier's two, `XCL_WT`), so `& XCL_LDS_MASK` wraps an + /// address as the 32 KB block did. Otherwise, and for the other stages: 32 KB. + fn lds_words(&self) -> u32 { + let Mode::Compute(cs) = &self.opt.mode else { return 8192 }; + if cs.lds_bytes == 0 { + return 8192; + } + let top = if self.x64() { + let (slots, counters) = self.x64_slots_counters(); + (slots + counters + 1) as u32 + 2 + } else { + 4 + }; + (cs.lds_bytes.div_ceil(4) + top).next_power_of_two().min(8192) + } + + /// The `xcl_lds` declaration (`lds_words`) and the address mask of its accesses. + fn lds_decl(&self, h: &mut String, note: &str) { + let words = self.lds_words(); + let size = match &self.opt.mode { + Mode::Compute(cs) if words < 8192 => format!("{} bytes: the program's LDS_SIZE ({} bytes) and the runtime words", words * 4, cs.lds_bytes), + _ => "D3D12 limit: 32 KB (the console allows 64 KB)".to_string(), + }; + let _ = writeln!(h, "groupshared uint xcl_lds[{words}]; // {size}{note}\n#define XCL_LDS_MASK {}u", words - 1); + } + /// Storage and functions of the split wave64 form (`PRELUDE_X64`). fn x64_prelude(&self, h: &mut String) { let threads = self.x64_threads(); let waves = threads.div_ceil(64).max(1) as usize; let slot = self.x64_words; - // slots (console wave, half, parity), the arrival counters (console wave, half), the - // wave counter of `xcl_x_thread` - let (slots, counters) = (waves * 4 * slot, waves * 2); + let (slots, counters) = self.x64_slots_counters(); let _ = writeln!(h, "#define XCL_X_THREADS {threads}u\n#define XCL_X_WAVES {waves}u\n#define XCL_X_SLOT {slot}u\n#define XCL_X_SPIN {}", self.x64_spin() as u32); if self.x64_lds() { - // the D3D12 limit (32 KB) is the whole block already: the exchange (and optional - // safe-barrier words at 8189..8190) takes its top, the program's LDS accesses stop - // below it (`XCL_X_LDS_TOP`) + // the exchange (and optional safe-barrier words below the last) takes the top of + // the block, the program's LDS accesses stop below it (`XCL_X_LDS_TOP`) + let words = self.lds_words() as usize; let bar_words = if self.safe_barrier { 2 } else { 0 }; - let base = 8192 - slots - counters - 1 - bar_words; - h.push_str("groupshared uint xcl_lds[8192]; // D3D12 limit: 32 KB (the console allows 64 KB); the top holds the wave64 exchange\n"); + let base = words - slots - counters - 1 - bar_words; + self.lds_decl(h, "; the top holds the wave64 exchange"); let _ = writeln!(h, "#define XCL_X_LDS_TOP {}u", base - 1); - let _ = writeln!(h, "#define XCL_XS(i) xcl_lds[{base}u + (i)]\n#define XCL_XC(i) xcl_lds[{}u + (i)]\n#define XCL_XW xcl_lds[8191u]", base + slots); + let _ = writeln!(h, "#define XCL_XS(i) xcl_lds[{base}u + (i)]\n#define XCL_XC(i) xcl_lds[{}u + (i)]\n#define XCL_XW xcl_lds[{}u]", base + slots, words - 1); } else { let _ = writeln!(h, "groupshared uint xcl_xs[{slots}];\ngroupshared uint xcl_xc[{counters}];\ngroupshared uint xcl_xw;"); h.push_str("#define XCL_XS(i) xcl_xs[(i)]\n#define XCL_XC(i) xcl_xc[(i)]\n#define XCL_XW xcl_xw\n"); @@ -4631,10 +4668,11 @@ impl Em { if !self.safe_barrier { return; } + let w = self.lds_words(); if self.x64_lds() { - h.push_str("#define XCL_BAR_ALIVE xcl_lds[8189u]\n#define XCL_BAR_STATE xcl_lds[8190u]\n"); + let _ = writeln!(h, "#define XCL_BAR_ALIVE xcl_lds[{}u]\n#define XCL_BAR_STATE xcl_lds[{}u]", w - 3, w - 2); } else if !self.x64() && self.uses_lds { - h.push_str("#define XCL_LDS_TOP 8188u\n#define XCL_BAR_ALIVE xcl_lds[8189u]\n#define XCL_BAR_STATE xcl_lds[8190u]\n"); + let _ = writeln!(h, "#define XCL_LDS_TOP {}u\n#define XCL_BAR_ALIVE xcl_lds[{}u]\n#define XCL_BAR_STATE xcl_lds[{}u]", w - 4, w - 3, w - 2); } else { h.push_str("groupshared uint xcl_bar_alive_g;\ngroupshared uint xcl_bar_state_g;\n#define XCL_BAR_ALIVE xcl_bar_alive_g\n#define XCL_BAR_STATE xcl_bar_state_g\n"); } @@ -4719,7 +4757,7 @@ impl Em { let split = self.opt.hw_wave == 64 && !self.opt.wave64; let lanes = if self.opt.wave64 || split { 64 } else { 32 }; if self.uses_lds && !self.x64_lds() { - h.push_str("groupshared uint xcl_lds[8192]; // D3D12 limit: 32 KB (the console allows 64 KB)\n"); + self.lds_decl(&mut h, ""); } // split wave64: 64 threads (the console wave) in two hardware wave32s let hw_lanes = if self.x64() { 32 } else { lanes }; @@ -4800,7 +4838,7 @@ impl Em { Mode::Compute(cs) => { let lanes = if self.opt.wave64 { 64 } else { 32 }; if self.uses_lds && !self.x64_lds() { - h.push_str("groupshared uint xcl_lds[8192]; // D3D12 limit: 32 KB (the console allows 64 KB)\n"); + self.lds_decl(&mut h, ""); } let [x, y, z] = cs.threads; // split wave64: the same group in hardware wave32s (a console wave = two of them) @@ -4811,11 +4849,11 @@ impl Em { // takes the next `lanes` console threads as it arrives, lane l the l-th of them let reorder = !self.x64() && y * z > 1; if reorder { - // console threads handed to waves so far: the program's top LDS word while - // the waves take their threads (the console's LDS starts undefined; D3D12's - // 32 KB has no room for another word next to a full `xcl_lds`) + // console threads handed to waves so far: the block's top LDS word while + // the waves take their threads (the console's LDS starts undefined; a block + // of D3D12's 32 KB has no room for another word next to a full `xcl_lds`) if self.uses_lds && !self.x64_lds() { - h.push_str("#define XCL_WT xcl_lds[8191u]\n"); + let _ = writeln!(h, "#define XCL_WT xcl_lds[{}u]", self.lds_words() - 1); } else { h.push_str("groupshared uint xcl_wt;\n#define XCL_WT xcl_wt\n"); } @@ -4879,7 +4917,7 @@ impl Em { let native_lanes = if !self.opt.wave64 || self.x64() { 32 } else { 64 }; let lanes = if self.opt.wave64 { 64 } else { 32 }; if self.uses_lds && !self.x64_lds() { - h.push_str("groupshared uint xcl_lds[8192]; // D3D12 limit: 32 KB (the console allows 64 KB)\n"); + self.lds_decl(&mut h, ""); } self.safe_barrier_prelude(&mut h); let t = ms.threads; @@ -4923,7 +4961,7 @@ impl Em { let maxt = g.max_vertex_count.saturating_sub(2).max(1); let nreg = g.input_registers(); let fetched = super::fetched_registers(&g.es_inputs); - h.push_str("groupshared uint xcl_lds[8192]; // D3D12 limit: 32 KB (the console allows 64 KB)\n"); + self.lds_decl(&mut h, ""); h.push_str("[WaveSize(64)]\n[numthreads(64, 1, 1)]\nvoid main(uint gi : SV_GroupIndex, uint3 gid : SV_GroupID)\n{\n"); h.push_str(" lane_id = WaveGetLaneIndex();\n"); let _ = writeln!(h, " uint xcl_total = io.Load({}u);\n uint xcl_vin = io.Load({}u);", super::IO_GS_PRIMS * 4, super::IO_GS_VERTICES * 4); diff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs index ca82cf4292..50639ee1a1 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -2992,7 +2992,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, lds_bytes: 0 } } /// A compute program run on the CPU over `groups` groups (wave32): the memory after. diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index d1e7d8bc4e..dfb8acbec8 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -243,6 +243,8 @@ pub struct CsInterface { pub tg_size: bool, /// thread id VGPRs: 1 (v0), 2 (v0, v1) or 3 (v0..v2) pub tid_components: u32, + /// the group's LDS allocation in bytes (COMPUTE_PGM_RSRC2.LDS_SIZE); 0: not known + pub lds_bytes: u32, } impl CsInterface { @@ -264,6 +266,8 @@ impl CsInterface { tgid: [(r >> 7) & 1 == 1, (r >> 8) & 1 == 1, (r >> 9) & 1 == 1], tg_size: (r >> 10) & 1 == 1, tid_components: ((r >> 11) & 3) + 1, + // LDS_SIZE, bits 15..23, in 128-dword (512-byte) units on RDNA + lds_bytes: ((r >> 15) & 0x1FF) * 512, } } } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e648..fa1b116fad 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -4821,7 +4821,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, lds_bytes: 0 }; 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 @@ -6966,7 +6966,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, lds_bytes: 0 }; 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); @@ -7045,7 +7045,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, lds_bytes: 0 }; 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); @@ -7123,7 +7123,7 @@ fn bench_split_wave64() { let mut ms = Vec::new(); let mut exchanges = 0; 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, lds_bytes: 0 }; 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 @@ -7174,7 +7174,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, lds_bytes: 0 }; let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64, ..Default::default() }); let t = translate(&p, &opt).unwrap(); assert!(t.complete(), "{:?}", t.unsupported_why); @@ -7188,6 +7188,117 @@ fn gpu_compute_tg_size_counts_a_partial_last_wave() { } } +/// A compute program's LDS block: thread k (v0, or v0 + 8 · v1 in an 8-wide 2-D group) +/// stores k + 7 at byte `base` + 4k, s_barrier, loads the dword of thread (k + 1) mod n, adds +/// its wave's first lane (v_readfirstlane: an exchange of the split form) and stores the sum +/// at dword k of the memory. +fn lds_ring_program(n: u32, base: u32, two_d: bool) -> Vec { + let mut l: Vec<(&str, &str, Vec<(&str, u64)>, Option)> = Vec::new(); + if two_d { + l.push(("V_LSHLREV_B32", "ENC_VOP2", vec![("VDST", 5), ("SRC0", 131), ("VSRC1", 1)], None)); + l.push(("V_ADD_NC_U32", "ENC_VOP2", vec![("VDST", 0), ("SRC0", V + 5), ("VSRC1", 0)], None)); + } + l.extend([ + ("V_LSHLREV_B32", "ENC_VOP2", vec![("VDST", 1), ("SRC0", 130), ("VSRC1", 0)], None), + ("V_ADD_NC_U32", "ENC_VOP2", vec![("VDST", 8), ("SRC0", 255), ("VSRC1", 1)], Some(base)), + ("V_ADD_NC_U32", "ENC_VOP2", vec![("VDST", 2), ("SRC0", 135), ("VSRC1", 0)], None), + ("DS_WRITE_B32", "ENC_DS", vec![("ADDR", 8), ("DATA0", 2)], None), + ("S_BARRIER", "ENC_SOPP", vec![], None), + ("V_ADD_NC_U32", "ENC_VOP2", vec![("VDST", 3), ("SRC0", 129), ("VSRC1", 0)], None), + ("V_AND_B32", "ENC_VOP2", vec![("VDST", 3), ("SRC0", 255), ("VSRC1", 3)], Some(n - 1)), + ("V_LSHLREV_B32", "ENC_VOP2", vec![("VDST", 3), ("SRC0", 130), ("VSRC1", 3)], None), + ("V_ADD_NC_U32", "ENC_VOP2", vec![("VDST", 3), ("SRC0", 255), ("VSRC1", 3)], Some(base)), + ("DS_READ_B32", "ENC_DS", vec![("VDST", 4), ("ADDR", 3)], None), + ("V_READFIRSTLANE_B32", "ENC_VOP1", vec![("VDST", 4), ("SRC0", V + 4)], None), + ("V_ADD_NC_U32", "ENC_VOP2", vec![("VDST", 4), ("SRC0", 4), ("VSRC1", 4)], None), + ("V_MOV_B32", "ENC_VOP1", vec![("VDST", 7), ("SRC0", 128)], None), + ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", vec![("ADDR", 1), ("DATA", 4), ("SADDR", 0x7D)], None), + ]); + let refs: Vec<(&str, &str, &[(&str, u64)], Option)> = l.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect(); + prog(&refs) +} + +/// The words in `#define ...xcl_lds[u` / `#define u` of a translation. +fn hlsl_define_number(hlsl: &str, name: &str) -> Option { + let line = hlsl.lines().find(|l| l.starts_with(&format!("#define {name} ")))?; + let digits: String = line[name.len() + 8..].chars().skip_while(|c| !c.is_ascii_digit()).take_while(|c| c.is_ascii_digit()).collect(); + digits.parse().ok() +} + +/// `xcl_lds` is the size the console gave the program (COMPUTE_PGM_RSRC2.LDS_SIZE) plus the +/// runtime words at its top, rounded up to a power of two – not always the 32 KB limit, which +/// caps the groups an SM holds (3 per NVIDIA Ampere SM) – and 32 KB when the size is unknown. +#[test] +fn compute_lds_block_takes_the_programs_lds_size() { + let p = lds_ring_program(64, 0, false); + for (lds_bytes, wave64, split, words) in [ + (512u32, false, false, 256u32), + (512, true, true, 256), + (512, true, false, 256), + (1024, true, true, 512), + (0, false, false, 8192), + (0, true, true, 8192), + (32768, true, true, 8192), + ] { + let cs = super::CsInterface { threads: [64, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1, lds_bytes }; + let t = translate(&p, &Options { mode: Mode::Compute(cs), wave64, split_wave64: split, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + let what = format!("LDS_SIZE {lds_bytes}, wave64 {wave64}, split {split}"); + assert!(t.hlsl.contains(&format!("groupshared uint xcl_lds[{words}];")), "{what}:\n{}", t.hlsl); + assert_eq!(hlsl_define_number(&t.hlsl, "XCL_LDS_MASK"), Some(words - 1), "{what}"); + // the program's own words stay below the runtime's + let need = lds_bytes.div_ceil(4).min(8000); + if wave64 && split { + let top = hlsl_define_number(&t.hlsl, "XCL_X_LDS_TOP").unwrap(); + let xs = hlsl_define_number(&t.hlsl, "XCL_XS(i)").unwrap(); + assert!(top + 1 >= need && xs > top, "{what}: top {top}, exchange {xs}"); + assert_eq!(hlsl_define_number(&t.hlsl, "XCL_XW"), Some(words - 1), "{what}"); + } + } +} + +/// The program's LDS words and the runtime's in the top of a block sized from LDS_SIZE (the +/// split-wave64 exchange, the safe barrier's words, the 2-D thread hand-out) do not overlap: +/// with the largest LDS_SIZE that still gets the smallest block, and the program's words at +/// its very end, a ring through the LDS of one / four console waves (1-D, and an 8 x 8 group) +/// with an exchange after it gives every thread its neighbour's value. +#[test] +fn gpu_compute_lds_ring_in_a_block_of_the_programs_size() { + if !gpu_ready() { + return; + } + for (dims, wave64) in [([64u32, 1, 1], true), ([64, 1, 1], false), ([256, 1, 1], true), ([256, 1, 1], false), ([8, 8, 1], false), ([8, 8, 1], true)] { + let threads = dims.iter().product::(); + let two_d = dims[1] > 1; + let lanes = if wave64 { 64 } else { 32 }; + let opt_for = |lds_bytes: u32| { + let cs = super::CsInterface { threads: dims, user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: if two_d { 2 } else { 1 }, lds_bytes }; + on_gpu(Options { mode: Mode::Compute(cs), wave64, ..Default::default() }) + }; + let block = |lds_bytes: u32| { + let t = translate(&lds_ring_program(threads, lds_bytes - 4 * threads, two_d), &opt_for(lds_bytes)).unwrap(); + hlsl_define_number(&t.hlsl, "XCL_LDS_MASK").unwrap() + 1 + }; + let smallest = block(4 * threads); + assert!(smallest < 8192, "{dims:?}: the block keeps the 32 KB size"); + let mut lds_bytes = 4 * threads; + while block(lds_bytes + 4) == smallest { + lds_bytes += 4; + } + let p = lds_ring_program(threads, lds_bytes - 4 * threads, two_d); + let t = translate(&p, &opt_for(lds_bytes)).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl)); + let mem = runner::dispatch(&dxil, [1, 1, 1], &[0u32; 128], &vec![0xFFFF_FFFFu32; threads as usize]).unwrap(); + for (k, &got) in mem.iter().take(threads as usize).enumerate() { + let k = k as u32; + let first = k / lanes * lanes; + let want = ((k + 1) % threads + 7) + ((first + 1) % threads + 7); + assert_eq!(got, want, "{threads} threads, wave{lanes}: thread {k}"); + } + } +} + /// 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. @@ -8536,7 +8647,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, lds_bytes: 0 }; let base = 0x100usize; let mut io = vec![0u32; 128]; io[0] = base as u32;