Skip to content
Draft
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/lower_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down
15 changes: 14 additions & 1 deletion crates/xcl/src/gpu/recomp/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ struct Lower<'a, I: Ir> {
noted: Option<Vec<String>>,
/// 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.
Expand Down Expand Up @@ -1266,6 +1267,7 @@ fn lower_with<I: Ir>(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<u8> = vec![VCC, VCC + 1, 124, EXEC, EXEC + 1];
Expand All @@ -1281,6 +1283,11 @@ fn lower_with<I: Ir>(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);
Expand Down Expand Up @@ -1676,7 +1683,13 @@ impl<I: Ir> 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<u16> = vr.iter().chain(&vw).map(|&v| v as u16).collect();
vregs.sort_unstable();
vregs.dedup();
Expand Down
53 changes: 53 additions & 0 deletions crates/xcl/src/gpu/recomp/lower_dxil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Loading