From 7e2c9235dfc6ab91dee9f85eaca03d50174506b6 Mon Sep 17 00:00:00 2001 From: LucasHoepfner <150492753+LucasHoepfner@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:21:13 -0300 Subject: [PATCH] Recompiler: direct DXIL s_buffer_load offsets wrap within their window A scalar buffer load's offset is OFFSET + soff in 32 bits. The direct translator used that sum for the range check and the address, but took the memory window from the two parts added in 64 bits. Where the sum wraps, the check passed on the wrapped offset and the load read the right low address in the next window, 4 GiB above the buffer. The HLSL path uses the 32-bit sum for all three. The window now comes from the 32-bit offset too. Test: s_buffer_load_dword and dwordx4 with OFFSET 0x20 and soff 0xFFFF_FFF0 on a two-window arena, through both translators and both window forms. Without the change the direct path returns window 1's marker (0xB000_0000 where 0xA000_0000 is expected); the HLSL path passes before and after. Part of #1893; #1933 has the MUBUF and atomic cases. Assisted-by: Claude Code (Claude Fable 5.2) --- crates/xcl/src/gpu/dxil/lower_mem.rs | 4 ++- crates/xcl/src/gpu/recomp/tests.rs | 41 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index a1d034e74..358f6e50f 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -1063,7 +1063,9 @@ impl Lw { let (d0, d1, d2) = (self.sreg(sbase), self.sreg(sbase + 1), self.sreg(sbase + 2)); let k = self.c(imm); let off = self.add(k, soff); - self.buffer_window(d0, d1, &[Ea::U(k), Ea::U(soff)], false); + // the window of the 32-bit offset the range check and the address below use, as in + // the HLSL kernel (not of `k + soff` in 64 bits, which can be the next window's) + self.buffer_window(d0, d1, &[Ea::U(off)], false); let st = self.lshrc(d1, 16); let st = self.andc(st, 0x3FFF); let sz = self.cmpc(Pred::Eq, st, 0); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e64..1f6bfb7d8 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -4010,6 +4010,47 @@ fn memory_windows_reject_out_of_uma_aliasing(heap: bool) { assert_eq!(*word(w1 + 0x3000), CANARY1, "out-of-UMA global_atomic_inc must not clobber w1"); } +/// A scalar buffer load's offset (`OFFSET + soff`) is 32-bit: where the sum wraps, the range +/// check, the address and the memory window are those of the wrapped offset, in both +/// translators and both window forms (not the next window, 4 GiB above the buffer). +#[test] +fn gpu_scalar_buffer_load_offset_wraps_within_its_window() { + if !gpu_ready() { + return; + } + let arena = crate::gpu::uma::Uma::with_windows(2, 1 << 20).unwrap(); + let (w0, w1) = (arena.window(0).0 as u64, arena.window(1).0 as u64); + assert_eq!(w1 - w0, 1 << 32); + let word = |a: u64| unsafe { &mut *(a as *mut u32) }; + for k in 0..4 { + *word(w0 + 0x1010 + 4 * k) = 0xA000_0000 | k as u32; + *word(w1 + 0x1010 + 4 * k) = 0xB000_0000 | k as u32; + } + // OFFSET 0x20 + s24 (0xFFFF_FFF0) = 0x10 in 32 bits + let p = prog(&[ + ("S_BUFFER_LOAD_DWORD", "ENC_SMEM", &[("SDATA", 30), ("SBASE", 4), ("OFFSET", 0x20), ("SOFFSET", 24)], None), + ("S_BUFFER_LOAD_DWORDX4", "ENC_SMEM", &[("SDATA", 32), ("SBASE", 4), ("OFFSET", 0x20), ("SOFFSET", 24)], None), + ]); + let mut st = KernelState::default(); + st.sgpr[8..12].copy_from_slice(&[0x1000, (w0 >> 32) as u32 & 0xFFFF, 0x100, 0]); // 0x100 bytes at w0 + 0x1000 + st.sgpr[24] = 0xFFFF_FFF0; + for (direct, heap) in [(false, false), (false, true), (true, false), (true, true)] { + let opt = on_gpu(Options { mem_windows: 2, heap_windows: heap, ..Default::default() }); + let dxil = if direct { + let split = opt.wave64 && (opt.split_wave64 || runner::split_wave64()); + let mem_windows = if heap { 2 | super::HEAP_WINDOWS } else { 2 }; + let o = crate::gpu::dxil::kernel::KernelOptions { wave64: opt.wave64, split, mem_windows, sampler_base: opt.static_sampler_base }; + crate::gpu::dxil::kernel::translate(&p, o).unwrap() + } else { + let t = translate(&p, &opt).unwrap(); + super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).map_err(|e| format!("{e}\n{}", t.hlsl)).unwrap() + }; + let r = runner::run_in_arena(&dxil, &st, &arena).unwrap(); + assert_eq!(r.sgpr[30], 0xA000_0000, "direct {direct}, heap windows {heap}: dword"); + assert_eq!(r.sgpr[32..36], [0xA000_0000, 0xA000_0001, 0xA000_0002, 0xA000_0003], "direct {direct}, heap windows {heap}: dwordx4"); + } +} + /// The start-up check of the coherent UMA windows (`real::check_uma_import`): the GPU sees what /// the CPU writes there. Fails, as it should, where the import fell back to memory of its own /// (vkd3d-proton under a Wine without `userfaultfd` write watches, #175).