diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index 3b9095b3a3..5efd135b08 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -1178,6 +1178,28 @@ fn gpu_direct_loads_read_the_constant_data_after_the_program() { } } +#[test] +fn compute_movrel_uses_program_max_vgpr_and_masks_m0() { + use crate::gpu::recomp::CsInterface; + let ko = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }; + // Compute mode (not Entry::Kernel): v_movrels_b32 reading v[1 + (m0 & 0xFF)] when v5 is + // written later in the shader must still include k=4 (v5) in the switch and mask m0 & 0xFF. + let code = prog(&[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 129)], None), + ("V_MOVRELS_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", 256 + 1)], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 130)], None), + // stored, so DCE keeps the relative move this test looks at + ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 1), ("DATA", 0), ("SADDR", 0x7D), ("OFFSET", 0)], None), + ]); + let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let k = super::kernel::translate_compute(&code, ko, &cs).expect("compute movrel translates"); + super::validate::validate(&k.dxil).expect("validates"); + if let Ok(dis) = super::validate::disassemble(&k.dxil) { + assert!(dis.contains("and i32") && dis.contains(", 255"), "expected m0 & 0xFF mask:\n{dis}"); + assert!(dis.contains("icmp eq i32") && dis.contains(", 4"), "expected k=4 (v5) branch from max_vgpr:\n{dis}"); + } +} + #[test] fn dot8_u32_u4_unclamped_lowers_to_dot4_add_u8_packed() { use crate::gpu::recomp::{dxc, runner}; diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 89eeaf1d8d..94bc5501c6 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -682,6 +682,15 @@ impl Lw { track_frexp_denorm: crate::gpu::recomp::emit::tracks_frexp_f32_denorm(insts), has_f64_round_op: false, img_writes: crate::gpu::recomp::emit::image_write_srsrcs(insts), + max_vgpr: insts + .iter() + .flat_map(|d| { + let (r, w) = crate::gpu::rdna2::vgpr_uses(d); + r.into_iter().chain(w) + }) + .filter(|&v| v < 256) + .max() + .unwrap_or(0), exec_cache: HashMap::new(), xs: None, xw: None, diff --git a/crates/xcl/src/gpu/dxil/lower.rs b/crates/xcl/src/gpu/dxil/lower.rs index d2dd21d95d..cbd0b42c20 100644 --- a/crates/xcl/src/gpu/dxil/lower.rs +++ b/crates/xcl/src/gpu/dxil/lower.rs @@ -278,6 +278,8 @@ pub struct Lw { pub has_f64_round_op: bool, /// the T# SGPRs of the program's image stores and atomics (`recomp::emit::image_write_srsrcs`) pub img_writes: Vec, + /// highest VGPR index referenced by the program (for `v_movrel*` outside `Entry::Kernel`) + pub max_vgpr: u64, /// static samplers bound so far: (S#, comparison, handle) at `s, space1` pub static_used: Vec<([u32; 4], bool, super::ops::Sampler)>, pub sampler_base: u32, diff --git a/crates/xcl/src/gpu/dxil/lower_wide.rs b/crates/xcl/src/gpu/dxil/lower_wide.rs index 91f90ffddc..b52981140d 100644 --- a/crates/xcl/src/gpu/dxil/lower_wide.rs +++ b/crates/xcl/src/gpu/dxil/lower_wide.rs @@ -280,7 +280,8 @@ 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) + let touched = self.init.touched.iter().filter(|&&v| (VAR_VGPR..VAR_VGPR + 256).contains(&v)).map(|&v| (v - VAR_VGPR) as u64).max().unwrap_or(0); + touched.max(self.max_vgpr) } pub fn movrel(&mut self, d: &Decoded, name: &str) -> LResult<()> { @@ -292,9 +293,12 @@ impl Lw { let m0 = self.sreg(124); let top = self.top_vgpr().max(vdst).max(src_code.saturating_sub(256)); let (rd_idx, wr_idx) = match name { - "v_movrels_b32" => (Some(m0), None), - "v_movreld_b32" => (None, Some(m0)), - "v_movrelsd_b32" => (Some(m0), Some(m0)), + "v_movrels_b32" => (Some(self.andc(m0, 0xFF)), None), + "v_movreld_b32" => (None, Some(self.andc(m0, 0xFF))), + "v_movrelsd_b32" => { + let m = self.andc(m0, 0xFF); + (Some(m), Some(m)) + } "v_movrelsd_2_b32" => { let r = self.andc(m0, 0x3FF); let w = self.lshrc(m0, 16); diff --git a/crates/xcl/src/gpu/dxil/module.rs b/crates/xcl/src/gpu/dxil/module.rs index a9601ad981..68205a5972 100644 --- a/crates/xcl/src/gpu/dxil/module.rs +++ b/crates/xcl/src/gpu/dxil/module.rs @@ -1472,11 +1472,14 @@ impl Module { /// integers (DXC's shader-flag rules; `Fma` is flagged by its helper). pub fn uses_double_extensions(&self) -> bool { let Some(f64) = self.type_ids.get(&TypeDef::Double).copied() else { return false }; - self.insts.iter().any(|d| match &d.op { - Op::Bin(BinOp::UDiv | BinOp::SDiv, ..) => d.ty == f64, - Op::Cast(CastOp::FPToUI | CastOp::FPToSI, v, _) => self.value_ty(*v) == f64, - Op::Cast(CastOp::UIToFP | CastOp::SIToFP, _, t) => *t == f64, - _ => false, + self.blocks.iter().flatten().any(|&i| { + let d = &self.insts[i as usize]; + match &d.op { + Op::Bin(BinOp::UDiv | BinOp::SDiv, ..) => d.ty == f64, + Op::Cast(CastOp::FPToUI | CastOp::FPToSI, v, _) => self.value_ty(*v) == f64, + Op::Cast(CastOp::UIToFP | CastOp::SIToFP, _, t) => *t == f64, + _ => false, + } }) } pub fn insts_use_type(&self, t: Ty) -> bool {