From 4fc971951041a6adf2f423633306049eadc9be53 Mon Sep 17 00:00:00 2001 From: IAmTheSween Date: Tue, 24 Nov 2026 20:21:56 -0500 Subject: [PATCH] Direct DXIL: bound relative moves by max_vgpr and refuse in per-lane bridge Fixes #1890. In direct DXIL, Lw::top_vgpr() computed the upper bound for v_movrel* and v_swaprel_b32 select loops from self.init.touched, which starts empty outside Entry::Kernel and is cleared before each instruction in XCL_PER_LANE=1. Any relative move with m0 > 0 targeting a VGPR not statically touched earlier in RPO order was silently truncated. Lw::max_vgpr now scans insts in Lw::new matching the HLSL emitter, and top_vgpr() returns self.max_vgpr. In the per-lane recompiler, bridge() refuses dynamically indexed relative moves upfront to route them through the fallback. Assisted-by: Antigravity (Gemini 3.8 Flash) --- crates/xcl/src/gpu/dxil/core_tests.rs | 46 +++++++++++++++++++++++++++ crates/xcl/src/gpu/dxil/kernel.rs | 10 ++++++ crates/xcl/src/gpu/dxil/lower.rs | 2 ++ crates/xcl/src/gpu/dxil/lower_wide.rs | 2 +- crates/xcl/src/gpu/recomp/lower.rs | 14 ++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index 01a48a1b42..e6fc177eb4 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -581,3 +581,49 @@ fn gpu_direct_unaligned_multi_dword_format_store() { let got: Vec = r.mem.iter().flat_map(|v| v.to_le_bytes()).collect(); assert_eq!(&got[0xFC..0x118], &want[0xFC..0x118], "bytes 0xFC..0x118 after the store at 0x102"); } + +/// `v_movreld_b32` and `v_movrels_b32` with `m0 > 0` where target or source VGPRs are not +/// statically referenced earlier in the block: `top_vgpr` must be bounded by the program's +/// `max_vgpr` rather than `init.touched` (which starts empty outside `Entry::Kernel`). +#[test] +fn v_movrel_with_non_static_target_vgpr() { + use crate::gpu::recomp::runner; + let code = prog(&[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 160)], None), // v1 = 32 + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 130)], None), // m0 = 2 + ("V_MOVRELD_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", 257)], None), // v[0 + m0] = v[2] = v1 = 32 + ("V_MOVRELS_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 256)], None), // v3 = v[0 + m0] = v2 = 32 + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 10), ("SRC0", 259)], None), // v10 = v3 = 32 (names max_vgpr = 10) + ]); + let ko = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }; + let dxil = translate(&code, ko).unwrap_or_else(|e| panic!("direct DXIL failed: {e}")); + if runner::wave32_supported() != Ok(true) { + return runner::skip_test("no wave32"); + } + let st = runner::KernelState::default(); + let direct = runner::run(&dxil, &st, &[]).unwrap(); + assert_eq!(direct.vgpr[0][10], 32, "v10 must be 32 (transferred through dynamically-indexed v2)"); +} + +/// `v_swaprel_b32` with `m0 > 0` where intermediate indexed VGPRs are not +/// statically referenced earlier in the block: `top_vgpr` must be bounded by the program's +/// `max_vgpr` rather than `init.touched`. +#[test] +fn v_swaprel_with_non_static_target_vgpr() { + use crate::gpu::recomp::runner; + let code = prog(&[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 160)], None), // v1 = 32 + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 161)], None), // v2 = 33 + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 130)], None), // m0 = 2 + ("V_SWAPREL_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 256)], None), // swap(v1, v[0 + m0] = v2) -> v1=33, v2=32 + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 10), ("SRC0", 257)], None), // v10 = v1 = 33 (names max_vgpr = 10) + ]); + let ko = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }; + let dxil = translate(&code, ko).unwrap_or_else(|e| panic!("direct DXIL failed: {e}")); + if runner::wave32_supported() != Ok(true) { + return runner::skip_test("no wave32"); + } + let st = runner::KernelState::default(); + let direct = runner::run(&dxil, &st, &[]).unwrap(); + assert_eq!(direct.vgpr[0][10], 33, "v10 must be 33 after swaprel"); +} diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 94ce0b37d0..9dacc8901c 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -450,10 +450,20 @@ impl Lw { let (mems, heap_mem) = mem_windows(&mut s, o.mem_windows, cs.is_some()); let i32 = s.m.i32(); let z = s.m.i32c(0); + let max_vgpr = insts + .iter() + .flat_map(|d| { + let (r, w) = crate::gpu::rdna2::vgpr_uses(d); + r.into_iter().chain(w) + }) + .filter(|&r| r < 256) + .max() + .unwrap_or(0); let mut lw = Lw { s, ssa: Ssa::new(i32), init: Init { io: Some(io), compute: None, vgpr_base: None, entry: Block(0), touched: BTreeSet::new(), slots: HashMap::new(), mem: None }, + max_vgpr, wave64: o.wave64, split, lane: z, diff --git a/crates/xcl/src/gpu/dxil/lower.rs b/crates/xcl/src/gpu/dxil/lower.rs index 0e270bb60d..a20d113103 100644 --- a/crates/xcl/src/gpu/dxil/lower.rs +++ b/crates/xcl/src/gpu/dxil/lower.rs @@ -169,6 +169,8 @@ pub struct Lw { pub s: Shader, pub ssa: Ssa, pub init: Init, + /// the highest VGPR the program references (v_movrel* and v_swaprel ranges) + pub max_vgpr: u64, pub wave64: bool, /// wave64 program in two hardware wave32 halves pub split: bool, diff --git a/crates/xcl/src/gpu/dxil/lower_wide.rs b/crates/xcl/src/gpu/dxil/lower_wide.rs index 3927a90252..28b2d95c1f 100644 --- a/crates/xcl/src/gpu/dxil/lower_wide.rs +++ b/crates/xcl/src/gpu/dxil/lower_wide.rs @@ -267,7 +267,7 @@ impl Lw { /// The highest VGPR the program names (relative moves index up to it). fn top_vgpr(&self) -> u64 { - self.init.touched.iter().filter(|&&v| (VAR_VGPR..VAR_VGPR + 256).contains(&v)).map(|&v| (v - VAR_VGPR) as u64).max().unwrap_or(0) + self.max_vgpr } pub fn movrel(&mut self, d: &Decoded, name: &str) -> LResult<()> { diff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs index ca82cf4292..f199ac714c 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -1619,6 +1619,9 @@ impl Lower<'_, I> { if self.track_frexp_denorm && self.partial[b] && super::emit::writes_frexp_f32_denorm(d) { return Err("a FREXP denormal mode write where some lanes are elsewhere".into()); } + if name.starts_with("V_MOVREL") || name == "V_SWAPREL_B32" { + return Err("movrel/swaprel dynamically indexes VGPRs".into()); + } // instructions whose #1426 semantics are known to differ from the HLSL path and the // console (each reported on #1426): left to the fallback until fixed there const DIFFERS_ON_1426: &[&str] = &[ @@ -3334,6 +3337,17 @@ pub(crate) mod tests { assert_eq!(err, "unsupported S_BCNT1_I32_B32"); } + #[test] + fn dynamically_indexed_vgpr_moves_are_refused_in_per_lane() { + let lines: &[I] = &[ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 130)]), // m0 = 2 + ("V_MOVRELD_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", 257)]), + ENDPGM, + ]; + let err = lower_kernel(&decode(lines), false, Interp::default()).err().expect("refused"); + assert_eq!(err, "unsupported V_MOVRELD_B32 (movrel/swaprel dynamically indexes VGPRs)"); + } + /// `s_wqm` of a mask sets every lane of a quad with a bit; `s_cselect` picks a mask or a /// number by SCC. #[test]