From 1bb18a942c6c9d7f18b4d0079c3209c1f9fafeae Mon Sep 17 00:00:00 2001 From: WebIdentity1 <212448219+WebIdentity1@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:20:46 -0500 Subject: [PATCH] Recompiler: preserve relative VGPR values across compute lowering Use a program-wide register bound and transfer possible relative values through the per-lane bridge, retaining register gaps and inactive lanes. Integrate on current main while preserving its initialization state and source-hashed cache. The compute regression passes 20 RTX 3090 and 30 AMD dispatches. Paired workspace runs retain the same CLAMP failure; retail smoke remains outstanding. --- README.md | 2 + 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 | 15 ++++++- crates/xcl/src/gpu/recomp/lower_dxil.rs | 53 +++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bdb207620..b246a3fc8 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,8 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder - **The game folder is never modified** – writes go to the overlay. - Translated shaders are reused across XCL rebuilds when the translator sources and DXC toolchain are unchanged; `XCL_TRANSLATED_CACHE=0` disables that disk cache. +- Direct DXIL (`XCL_DIRECT_DXIL=1`), including per-lane compute (`XCL_PER_LANE=1`), + preserves relative VGPR moves to registers first used later in the program. - Huge pipelines (translated DXIL of `XCL_HUGE_PSO_BYTES` or more, default 262144) built on the background `xcl-pso*` workers take turns in the driver's compiler: `XCL_HUGE_PSO_CONCURRENCY` at once (default 1, maximum 64). Pipelines of 4 MiB or more use a separate one-wide lane so diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 94ce0b37d..2eeb2385e 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); + // Only relative instructions need a register-file bound; avoid the operand + // scan for the common case that never indexes a register dynamically. + let relative = insts.iter().any(|d| d.inst.name.starts_with("V_MOVREL") || d.inst.name == "V_SWAPREL_B32"); + let max_vgpr = if relative { + 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) + } else { 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 0e270bb60..e68288424 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, + /// Highest register named anywhere in this program, independent of lowering order. + 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 3927a9025..28b2d95c1 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 ca82cf429..6a9fe71f6 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -186,6 +186,7 @@ struct Lower<'a, I: Ir> { noted: Option>, /// a VS / PS (`lower_graphics`): `s_wqm` only of the launch EXEC (its quads' helper lanes) graphics: bool, + max_vgpr: u16, } /// A vector memory store: a side effect inactive lanes must not have. @@ -1266,6 +1267,7 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s dpp_en: None, noted: survey.then(Vec::new), graphics, + max_vgpr: 0, }; // the registers the program names, and the ones the runner reads back let mut sregs: Vec = vec![VCC, VCC + 1, 124, EXEC, EXEC + 1]; @@ -1281,6 +1283,11 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s sregs.dedup(); vregs.sort_unstable(); vregs.dedup(); + l.max_vgpr = vregs.iter().copied().filter(|&v| v < 256).max().unwrap_or(0); + if insts.iter().any(|d| d.inst.name.starts_with("V_MOVREL") || d.inst.name == "V_SWAPREL_B32") { + // Relative addressing can touch gaps between explicitly named registers too. + vregs = (0..=l.max_vgpr).collect(); + } // prologue: the SGPRs from `io` (kernel: the whole register file; compute: the user data) l.ir.at(0); @@ -1676,7 +1683,13 @@ impl Lower<'_, I> { inputs.push((Var::Scc, s)); } // VGPRs read, and the ones written (a write keeps the old value where the guard is off) - let (vr, vw) = rdna2::vgpr_uses(d); + let (mut vr, mut vw) = rdna2::vgpr_uses(d); + if name.starts_with("V_MOVREL") || name == "V_SWAPREL_B32" { + // The foreign lowering indexes dynamically; transfer the whole possible + // register range in both directions, including inactive-lane values. + vr = (0..=self.max_vgpr as u64).collect(); + vw = vr.clone(); + } let mut vregs: Vec = vr.iter().chain(&vw).map(|&v| v as u16).collect(); vregs.sort_unstable(); vregs.dedup(); diff --git a/crates/xcl/src/gpu/recomp/lower_dxil.rs b/crates/xcl/src/gpu/recomp/lower_dxil.rs index 2c200c788..e06174453 100644 --- a/crates/xcl/src/gpu/recomp/lower_dxil.rs +++ b/crates/xcl/src/gpu/recomp/lower_dxil.rs @@ -436,6 +436,59 @@ mod tests { true } + #[test] + fn relative_registers_reach_compute_output() { + const WRITE: &[I] = &[("V_MOVRELD_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 170)])]; + const READ: &[I] = &[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 170)]), + ("V_MOVRELS_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", V + 1)]), + ]; + const BOTH: &[I] = &[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 170)]), + ("V_MOVRELSD_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", V + 1)]), + ]; + const SEPARATE: &[I] = &[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 170)]), + ("V_MOVRELSD_2_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", V + 1)]), + ]; + const MASKED: &[I] = &[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 135)]), + ("V_CMPX_GT_U32", "ENC_VOPC", &[("SRC0", 144), ("VSRC1", 0)]), + ("V_MOVRELD_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 170)]), + ("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)]), + ]; + if !gpu() { return; } + println!("relative registers GPU: {}", runner::adapter().unwrap()); + let mut modes = vec![(false, false), (true, true)]; + if runner::wave64_supported() == Ok(true) { modes.push((true, false)); } + let cs = compute_cs(); + let mut failures = Vec::new(); + for (name, ops, result, m0) in [ + ("write-before-use", WRITE, 3, 2), ("read", READ, 6, 2), + ("read-write", BOTH, 6, 2), ("separate-offsets", SEPARATE, 7, 0x30002), + ("inactive-lanes", MASKED, 3, 2), + ] { + let mut code = encode_line(&("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 0)])); + code.extend(ops.iter().flat_map(encode_line)); + code.extend(encode_line(&("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 10), ("SRC0", 130), ("VSRC1", 0)]))); + code.extend(encode_line(&("V_MOV_B32", "ENC_VOP1", &[("VDST", 11), ("SRC0", 128)]))); + code.extend(crate::gpu::rdna2::encode("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 10), ("DATA", result), ("SADDR", 125), ("OFFSET", 0)], None).unwrap()); + code.extend(encode_line(&ENDPGM)); + for &(wave64, split) in &modes { + let opts = crate::gpu::dxil::kernel::KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 }; + let direct = crate::gpu::dxil::kernel::translate_compute(&code, opts, &cs).unwrap().dxil; + let per_lane = super::compute_with(&code, &cs, opts).unwrap().dxil; + for (backend, dxil) in [("direct", direct), ("per-lane", per_lane)] { + let got = runner::dispatch(&dxil, [1, 1, 1], &[m0, 0], &vec![0u32; 64]).unwrap(); + let bad = got[..64].iter().enumerate().filter(|&(lane, &v)| v != if name == "inactive-lanes" && lane >= 16 { 7 } else { 42 }).count(); + println!("{name}, {backend}, wave64={wave64}, split={split}: {bad}/64 mismatches"); + if bad != 0 { failures.push((name, backend, wave64, split)); } + } + } + } + assert!(failures.is_empty(), "relative register mismatches: {failures:?}"); + } + /// A rounding mode #1426's lowering cannot honour refuses the program, as #1426 and the HLSL /// path do, instead of the per-lane form computing with the default mode (Kalmarv, #1745): /// f32 toward zero by `s_setreg`, f64 by `s_round_mode` and by `s_setreg`.