Skip to content
Open
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
60 changes: 58 additions & 2 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub(super) struct Em {
/// instruction (`x64_masks`); a mask written to none of them is only read lane by lane
/// (split: stays in its half, no exchange; pixel: no quad reads for the helper lanes)
pub mask_whole_live: BTreeMap<u64, u128>,
/// split wave64: wave-uniform VGPR chains feeding `v_readfirstlane_b32` (`x64_uniform`)
pub x64_uniform: super::x64_uniform::X64Uniform,
/// live in-block uniform scalar temporaries per VGPR (cleared on block boundary / EXEC change)
x64_unif_v: BTreeMap<u64, String>,
/// true while reading sources of an `x64_uniform.capture_pcs` producer VALU
x64_use_unif_src: bool,
/// wave64 VS / PS on a GPU with 32-lane waves only: what the console's lanes 32..63 hold
/// where the program reads them (`upper_lanes`), used when the wave has 32 lanes
pub upper: Option<super::upper_lanes::UpperLanes>,
Expand Down Expand Up @@ -560,6 +566,9 @@ impl Em {
uses_scratch: false,
x64_words: 1,
mask_whole_live: BTreeMap::new(),
x64_uniform: super::x64_uniform::X64Uniform::default(),
x64_unif_v: BTreeMap::new(),
x64_use_unif_src: false,
upper: None,
cur_pc: 0,
static_ss: BTreeMap::new(),
Expand Down Expand Up @@ -636,6 +645,7 @@ impl Em {
}

pub fn take(&mut self) -> String {
self.x64_unif_v.clear();
std::mem::take(&mut self.out)
}

Expand Down Expand Up @@ -752,7 +762,15 @@ impl Em {
252 => format!("(uint)({})", self.mask_zero(126)),
253 => "scc".into(),
255 => hex(lit?),
256..=511 => self.vreg(code - 256),
256..=511 => {
let v = code - 256;
if self.x64_use_unif_src {
if let Some(u) = self.x64_unif_v.get(&v) {
return Some(u.clone());
}
}
self.vreg(v)
}
// flat apertures (shared/private base and limit): the flat memory model has none
235..=238 => "0u".into(),
// POPS exiting wave id: every overlapping earlier wave has already left, so the
Expand Down Expand Up @@ -797,6 +815,11 @@ impl Em {
let v = Self::field(d, f);
let width = d.enc.field_width(f);
if o.ty.starts_with("OPR_VGPR") && width <= 8 {
if self.x64_use_unif_src {
if let Some(u) = self.x64_unif_v.get(&v) {
return Some(u.clone());
}
}
return Some(self.vreg(v));
}
match o.ty {
Expand Down Expand Up @@ -876,11 +899,15 @@ impl Em {
// select, not `if`: straight-line code (a branch per VALU write was more than the
// driver's compiler could take on a 15 000-instruction console shader)
Dst::V(n) => {
self.x64_unif_v.remove(n);
let v = self.vreg(*n);
let a = self.act();
self.line(&format!("{v} = select({a}, {val}, {v});"));
}
Dst::S(n) => {
if *n == 126 || *n == 127 {
self.x64_unif_v.clear();
}
let s = self.sreg(*n);
self.line(&format!("{s} = {val};"));
}
Expand All @@ -891,11 +918,16 @@ impl Em {
fn write64(&mut self, dst: &Dst, lo: &str, hi: &str) {
match dst {
Dst::V(n) => {
self.x64_unif_v.remove(n);
self.x64_unif_v.remove(&(*n + 1));
let (a, b) = (self.vreg(*n), self.vreg(*n + 1));
let act = self.act();
self.line(&format!("{a} = select({act}, {lo}, {a}); {b} = select({act}, {hi}, {b});"));
}
Dst::S(n) => {
if *n == 126 || *n == 127 {
self.x64_unif_v.clear();
}
let (a, b) = (self.sreg(*n), self.sreg(*n + 1));
self.line(&format!("{a} = {lo}; {b} = {hi};"));
}
Expand Down Expand Up @@ -1462,6 +1494,7 @@ impl Em {
// SOP1 / SOP2
let saveexec = name.contains("saveexec") || name.contains("wrexec");
if saveexec {
self.x64_unif_v.clear();
let op = |a: &str, e: &str| -> Option<String> {
Some(match base.split('_').nth(1)? {
"and" => format!("{a} & {e}"),
Expand Down Expand Up @@ -3620,7 +3653,11 @@ impl Em {
return self.valu16(d, name);
}
let m = Self::mods(d);
let u = self.sources(d, &ins, src_f16)?;
let capture = self.x64() && self.x64_uniform.capture_pcs.contains(&d.pc);
self.x64_use_unif_src = capture;
let u = self.sources(d, &ins, src_f16);
self.x64_use_unif_src = false;
let u = u?;
if let Some(rest) = name.strip_prefix("v_cmpx_").or_else(|| name.strip_prefix("v_cmp_")) {
return self.vopc(d, rest, name.starts_with("v_cmpx_"), &outs, &u, &m);
}
Expand Down Expand Up @@ -3870,6 +3907,15 @@ impl Em {
}
}
"v_readfirstlane_b32" => {
if self.x64() && self.x64_uniform.rfl_pcs.contains(&d.pc) {
let src0 = Self::field(d, "SRC0");
if src0 >= 256 {
if let Some(u) = self.x64_unif_v.get(&(src0 - 256)).cloned() {
self.write32(&dst, &u);
return Ok(());
}
}
}
let (el, eh) = (self.sreg(126), self.sreg(127));
let eh = if self.opt.wave64 { eh } else { "0u".into() };
let t = self.t();
Expand Down Expand Up @@ -3902,6 +3948,7 @@ impl Em {
"v_writelane_b32" => {
// ignores EXEC
if let Dst::V(n) = &dst {
self.x64_unif_v.remove(n);
let v = self.vreg(*n);
self.line(&format!("if (lane_id == ({b} & {wmask}u)) {v} = {a};"));
// a spill slot some read takes from its shadow (`upper_lanes`)
Expand Down Expand Up @@ -3956,6 +4003,15 @@ impl Em {
};
// the value straight into the write (a named temporary is one more local per
// instruction – tens of thousands on big programs at -O0)
if capture {
if let Dst::V(v) = dst {
let t = self.t();
self.line(&format!("uint {t} = ({res});"));
self.vwrite(d, &dst, &t);
self.x64_unif_v.insert(v, t);
return Ok(());
}
}
self.vwrite(d, &dst, &format!("({res})"));
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub mod mesh_dispatch;
pub mod mesh_wave;
pub mod partition;
pub mod x64_masks;
pub mod x64_uniform;
pub mod pso;
pub use emit::mesh_passthrough;
pub mod runner;
Expand Down Expand Up @@ -1431,6 +1432,9 @@ pub fn translate(code: &[u8], opt: &Options) -> Result<Translation, String> {
} else if matches!(opt.mode, Mode::Pixel(_)) {
em.mask_whole_live = x64_masks::by_pc(&insts, false);
}
if opt.wave64 && opt.split_wave64 && matches!(opt.mode, Mode::Kernel | Mode::Compute(_) | Mode::Mesh(_)) {
em.x64_uniform = x64_uniform::analyze(&insts, &leaders, matches!(opt.mode, Mode::Compute(_) | Mode::Mesh(_)));
}
// a wave64 VS / PS on a GPU that can run 32-lane waves: what lanes 32..63 hold where the
// program reads them (the driver may still pick 64 lanes: decided at run time); only a
// program with a read to change gets the form
Expand Down
63 changes: 63 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6691,6 +6691,69 @@ fn gpu_split_wave64_reduction_tails() {
assert_eq!(r.sgpr[21], 0x1111, "inactive-lane-0 s_out keeps unmodified old v7[0]");
}

/// Split-wave64 `v_readfirstlane_b32` reading a VGPR produced by a wave-uniform VALU chain
/// under guaranteed `EXEC != 0` reads the captured scalar temporary directly with zero
/// `xcl_x_rla_uniform` exchanges — even when only the upper half (`exec_lo == 0`, lane 40)
/// is active and lower-half VGPRs hold unrelated values. When `EXEC == 0` is possible, the
/// exchange is kept and returns lane 0's prior VGPR value.
#[test]
fn gpu_split_wave64_uniform_readfirstlane() {
if !gpu_ready() {
return;
}
const V: u64 = 256;
// Part 1: s_cbranch_execz proves EXEC != 0 -> scalar udiv + float add chains use 0 exchanges
let code = prog(&[
("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 11)], None),
("V_CVT_F32_U32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 5)], None),
("V_RCP_IFLAG_F32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V + 1)], None),
("V_MUL_LEGACY_F32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 255), ("VSRC1", 1)], Some(0x4f7f_fffe)),
("V_CVT_U32_F32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V + 1)], None),
("V_READFIRSTLANE_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", V + 1)], None),
("V_ADD_F32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 8), ("VSRC1", 2)], None), // non-uniform (v2 live-in)
("V_ADD_F32", "ENC_VOP3", &[("VDST", 3), ("SRC0", 8), ("SRC1", 9)], None),
("V_CVT_U32_F32", "ENC_VOP1", &[("VDST", 3), ("SRC0", V + 3)], None),
("V_READFIRSTLANE_B32", "ENC_VOP1", &[("VDST", 7), ("SRC0", V + 3)], None),
]);
if runner::split_wave64() {
let t = translate(&code, &runner::options(Mode::Kernel)).unwrap();
assert_eq!(t.hlsl.matches("xcl_x_rla_uniform(").count(), 1, "only definition, 0 calls");
}
let mut st = KernelState::default();
st.sgpr[126] = 0;
st.sgpr[127] = 1u32 << (40 - 32); // ONLY upper-half lane 40 active (exec_lo == 0!)
st.sgpr[5] = 7;
st.sgpr[8] = 1.5f32.to_bits();
st.sgpr[9] = 2.25f32.to_bits();
for (l, r) in st.vgpr.iter_mut().enumerate() {
r[1] = 0xDEAD_0000 + l as u32;
r[2] = 0x1000 + l as u32;
r[3] = 0xBEEF_0000 + l as u32;
}
let r = runner::run_code(&code, &st, &[]).unwrap();
let expected_rcp = ((1.0f32 / 7.0f32) * f32::from_bits(0x4f7f_fffe)) as u32;
// At magnitude ~6.1e8 (2^29), 1 float32 ULP is 2^(29-23) = 64 (AMD v_rcp_f32 differs by 1 ULP from 1.0f/7.0f).
assert!((r.sgpr[6] as i64 - expected_rcp as i64).abs() <= 128, "s6={} expected~={expected_rcp}", r.sgpr[6]);
assert_eq!(r.sgpr[7], 3, "both halves compute (uint)(1.5 + 2.25) == 3 with exec_lo == 0");
assert_eq!(r.vgpr[40][3], 3, "active lane 40 updated v3");
assert_eq!(r.vgpr[0][3], 0xBEEF_0000, "inactive lane 0 kept old v3");

// Part 2: EXEC may be 0 (no s_cbranch_execz in kernel mode) -> must keep exchange & read old lane 0
let zero_exec_code = prog(&[
("V_CVT_U32_F32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 8)], None),
("V_READFIRSTLANE_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", V + 1)], None),
]);
if runner::split_wave64() {
let t = translate(&zero_exec_code, &runner::options(Mode::Kernel)).unwrap();
assert_eq!(t.hlsl.matches("xcl_x_rla_uniform(").count(), 2, "definition + 1 exchange kept");
}
st.sgpr[126] = 0;
st.sgpr[127] = 0;
st.vgpr[0][1] = 0x1234_5678;
let r0 = runner::run_code(&zero_exec_code, &st, &[]).unwrap();
assert_eq!(r0.sgpr[6], 0x1234_5678, "EXEC == 0 reads unmodified lane 0");
}

/// Masks that are read whole after a write `x64_masks` must not take for an overwrite (an
/// SDWA compare into its SDST, `s_bitset1`, `s_movreld`) or through a read the operand
/// tables do not show (`s_movrels`, an SMEM register offset, an SDWA scalar src1): each is
Expand Down
Loading
Loading