From 4a777f160e25fa5723e5dccdf6d6e44a70e576e4 Mon Sep 17 00:00:00 2001 From: Foniass <107039406+Foniass@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:33:34 +0100 Subject: [PATCH] Recompiler: direct DXIL LDS accesses stop below the safe barrier's words With LDS and a safe multi-wave barrier, the barrier keeps its counts in xcl_lds[8189..8190] and lds_top is 8188 (#3264), but only the split form clamped the program's LDS indices to lds_top. A native wave64 or wave32 program could write the top of LDS over the barrier's counts. The HLSL path clamps every LDS access to XCL_LDS_TOP (8188) in this case; direct DXIL now does the same whenever lds_top is below 8191 (DS loads and stores, and the mesh epilogue's reads). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/lower_geom.rs | 2 +- crates/xcl/src/gpu/dxil/lower_mem.rs | 4 ++-- crates/xcl/src/gpu/dxil/robustness_tests.rs | 24 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/lower_geom.rs b/crates/xcl/src/gpu/dxil/lower_geom.rs index f632b640ef..f0ca9a02ab 100644 --- a/crates/xcl/src/gpu/dxil/lower_geom.rs +++ b/crates/xcl/src/gpu/dxil/lower_geom.rs @@ -229,7 +229,7 @@ impl Lw { let a = lw.addc(src, 16 * r + 4 * c); let i = lw.lshrc(a, 2); let i = lw.andc(i, 8191); - let i = if lw.split { let top = lw.c(lw.lds_top); lw.umin(i, top) } else { i }; + let i = if lw.split || lw.lds_top < 8191 { let top = lw.c(lw.lds_top); lw.umin(i, top) } else { i }; vals[c as usize] = lw.s.gs_load(lds, i); } let o = lw.addc(dst, 4 * slot as u32); diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index 86258284d4..e899b401bf 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -2228,11 +2228,11 @@ impl Lw { // ------------------------------------------------------------ LDS / DS /// LDS dword index of byte address `at` (`(at >> 2) & 8191`, below the exchange words - /// in the split form). + /// in the split form and below the safe barrier's words, as HLSL's `XCL_LDS_TOP`). 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.split || 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 8d3bd53862..901bda6bdb 100644 --- a/crates/xcl/src/gpu/dxil/robustness_tests.rs +++ b/crates/xcl/src/gpu/dxil/robustness_tests.rs @@ -152,6 +152,30 @@ fn a_group_barrier_of_several_hardware_waves_uses_safe_barrier_directly() { } } +/// A program's own LDS accesses stop below the safe barrier's words in `xcl_lds[8189..8190]` +/// in every form (HLSL: `XCL_LDS_TOP` 8188), not only in the split one, so a write to the top +/// of LDS cannot change the barrier's counts. +#[test] +fn lds_accesses_stop_below_the_safe_barrier_words() { + if !crate::gpu::recomp::emit::safe_barrier_wanted() { + return; + } + // the address is v0, the thread ID, so the clamp is not folded away + let code = prog(&[ + ("DS_WRITE_B32", "ENC_DS", &[("ADDR", 0), ("DATA0", 0)], None), + ("S_BARRIER", "ENC_SOPP", &[], None), + ("DS_READ_B32", "ENC_DS", &[("VDST", 2), ("ADDR", 0)], None), + ]); + let cs = CsInterface { threads: [16, 16, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1 }; + for wave64 in [true, false] { + let o = KernelOptions { wave64, split: false, mem_windows: 1, sampler_base: 0 }; + let k = kernel::translate_compute(&code, o, &cs).unwrap_or_else(|e| panic!("wave64 {wave64}: {e}")); + let dis = super::validate::disassemble(&k.dxil).expect("disassemble"); + let clamps = dis.lines().filter(|l| l.contains("@dx.op.binary.i32(i32 40,") && l.contains(", i32 8188)")).count(); + assert!(clamps > 0, "wave64 {wave64}: no LDS index clamped to 8188:\n{dis}"); + } +} + /// The translator's own variables after the loop budget each have a slot of their own (the /// multi-wave barrier's once shared `VAR_F32_MODE`'s), below the image-store slots. #[test]