Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
9 changes: 9 additions & 0 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
/// 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<i>, space1`
pub static_used: Vec<([u32; 4], bool, super::ops::Sampler)>,
pub sampler_base: u32,
Expand Down
12 changes: 8 additions & 4 deletions crates/xcl/src/gpu/dxil/lower_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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);
Expand Down
56 changes: 51 additions & 5 deletions crates/xcl/src/gpu/dxil/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1508,6 +1511,49 @@ impl Module {
fields.first().and_then(|&p0| self.const_u64(p0)).is_some_and(|w| w & 0x2000 != 0)
})
}
/// Scan surviving instructions in reachable blocks for call/atomic features that must stay in
/// sync with DXIL shader flags after optimization/DCE: `(double_fma, res_heap, smp_heap,
/// i64_atomic_gs, i64_atomic_res, uav_tex_load)`.
pub fn live_feature_scan(&self, i64: Option<Ty>) -> (bool, bool, bool, bool, bool, bool) {
let mut has_double_fma = false;
let mut has_res_heap = false;
let mut has_smp_heap = false;
let mut has_i64_atomic_gs = false;
let mut has_i64_atomic_res = false;
let mut has_uav_tex_load = false;
for &i in self.blocks.iter().flatten() {
let d = &self.insts[i as usize];
match &d.op {
Op::Call(f, args) => {
let name = self.funcs[*f as usize].name.as_str();
if name == "dx.op.createHandleFromHeap" && args.len() >= 3 {
if self.const_u64(args[2]) == Some(0) {
has_res_heap = true;
} else {
has_smp_heap = true;
}
} else if name == "dx.op.tertiary.f64" && args.first().and_then(|&a| self.const_u64(a)) == Some(47) {
has_double_fma = true;
} else if matches!(name, "dx.op.atomicBinOp.i64" | "dx.op.atomicCompareExchange.i64") {
has_i64_atomic_res = true;
} else if name.starts_with("dx.op.textureLoad")
&& args.get(2).is_some_and(|&mip| matches!(mip, Value::Const(c) if matches!(self.consts[c as usize], ConstDef::Undef(_))))
{
has_uav_tex_load = true;
}
}
// (a cmpxchg's type is the `{ T, i1 }` pair: T is its compared value's)
Op::AtomicRmw(..) if i64 == Some(d.ty) => {
has_i64_atomic_gs = true;
}
Op::CmpXchg(_, cmp, _) if i64 == Some(self.value_ty(*cmp)) => {
has_i64_atomic_gs = true;
}
_ => {}
}
}
(has_double_fma, has_res_heap, has_smp_heap, has_i64_atomic_gs, has_i64_atomic_res, has_uav_tex_load)
}
/// The integer value of a constant (zero-extended from its width).
pub fn const_u64(&self, v: Value) -> Option<u64> {
match v {
Expand Down
Loading
Loading