diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index 01a48a1b42..d7ac50cd17 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -411,6 +411,54 @@ fn multi_window_mem_format_and_bvh_validate() { } } +#[test] +fn raw_stores_in_root_windows_respect_exec_and_bounds() { + use crate::gpu::{recomp::runner::{self, KernelState}, uma::Uma}; + + let code = prog(&[ + ("V_CMPX_EQ_U32", "ENC_VOPC", &[("SRC0", 129), ("VSRC1", 3)], None), + ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("DATA", 2), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 0)], None), + ]); + let options = KernelOptions { wave64: false, split: false, mem_windows: 4, sampler_base: 0 }; + let wave32 = super::kernel::translate_full_as(&code, options, false).unwrap().dxil; + if crate::gpu::recomp::dxc::location().is_ok() { + let ir = super::validate::disassemble(&wave32).unwrap(); + assert!(!ir.contains("switch i32"), "root window stores still use a switch"); + assert!(ir.contains("shl i32 1,"), "root window stores lack a one-hot mask"); + } + if !matches!(runner::wave32_supported(), Ok(true)) { + runner::skip_test("no wave32"); + return; + } + let wave64 = super::kernel::translate_full_as(&code, KernelOptions { wave64: true, split: runner::split_wave64(), ..options }, false).unwrap().dxil; + let arena = Uma::with_windows(4, 1 << 20).unwrap(); + let base_hi = (arena.base() >> 32) as u32; + let windows = [0u32, 1, 2, 3, 4, 32, u32::MAX, 0]; + let word = |window: usize, lane: usize| (arena.window(window).0 + 0x1000 + lane * 4) as *mut u32; + let mut state = KernelState::default(); + for (lane, regs) in state.vgpr.iter_mut().enumerate() { + regs[0] = 0x1000 + lane as u32 * 4; + regs[1] = base_hi.wrapping_add(windows[lane % windows.len()]); + regs[2] = 0xCAFE_1000 + lane as u32; + regs[3] = u32::from(lane % 3 != 0); + } + for (dxil, lanes) in [(&wave32, 32), (&wave64, 64)] { + for lane in 0..state.vgpr.len() { + for window in 0..4 { + unsafe { word(window, lane).write(0xA5A5_A5A5) }; + } + } + runner::run_in_arena(dxil, &state, &arena).unwrap(); + for (lane, regs) in state.vgpr.iter().enumerate() { + for window in 0..4 { + let wrote = lane < lanes && windows[lane % windows.len()] == window as u32 && regs[3] != 0; + let expected = if wrote { regs[2] } else { 0xA5A5_A5A5 }; + assert_eq!(unsafe { word(window, lane).read() }, expected, "wave{lanes}, window {window}, lane {lane}"); + } + } + } +} + /// `image_get_lod` with a static sampler (an S# built from constants in the code) translates /// to `CalculateLevelOfDetail` on the sampler bound at `s, space1`, shared with a sample of /// the same S#, and validates. diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index fe04b4ac65..4afb6927e7 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -156,7 +156,7 @@ impl Lw { /// when `self.heap_mem` is set via `XCL_HEAP_WINDOWS`), returning `out_tys.len()` values /// merged across the window arms via a single `switch` (or single guarded heap lookup) /// and direct phis (`f` must not write SSA variables). Out-of-range windows (`w >= n`) - /// jump to `join` (reads 0, stores and atomics dropped), matching `#1693` semantics. + /// read 0 and drop stores and atomics. Multi-window root UAV stores use one-hot guards. pub(crate) fn with_mem_many_tys( &mut self, mut guard: Option, @@ -265,11 +265,33 @@ impl Lw { } return out; } - // Multi-window dispatch via a single LLVM `switch` (default -> `join` for out-of-range - // or inactive lane, matching `#1693` window semantics). let w = self.rd(super::lower::VAR_WIN); let mems = self.mems.clone(); let n = mems.len(); + if out_tys.is_empty() { + // Mask the shift's result before testing any bit: an out-of-range window can + // otherwise alias a valid bit because shl masks the shift count. + let inside = self.cmpc(Pred::ULt, w, n as u32); + let enabled = match guard { + Some(g) => self.and(g, inside), + None => inside, + }; + let one = self.c(1); + let zero = self.c(0); + let bit = self.shl(one, w); + let mask = self.sel(enabled, bit, zero); + for (k, &m) in mems.iter().enumerate() { + let bit = self.andc(mask, 1u32 << k); + let selected = self.cmpc(Pred::Ne, bit, 0); + self.guarded(selected, &mut |lw| { + let values = f(lw, m); + debug_assert!(values.is_empty()); + Ok(()) + }).expect("raw store lowering cannot fail"); + } + return Vec::new(); + } + // The switch's default skips out-of-range windows and inactive lanes. let from = self.cur(); let arms: Vec = (0..n).map(|_| self.block(None)).collect(); let join = self.block(None);