diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 6f12912195..441b1af1e0 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3892,7 +3892,7 @@ impl Em { self.line(&format!("uint {t} = {read};")); self.line(&format!("{t} = select(and(xcl_hw32, ({b} & 63u) >= 32u), {c:#010x}u, {t});")); } - (Some(Read::Shadow), Some((v, l))) if l < 32 => self.line(&format!("uint {t} = xcl_sh{v}_{l};")), + (Some(Read::Shadow), Some((v, l))) if self.x64() || l < 32 => self.line(&format!("uint {t} = xcl_sh{v}_{l};")), (Some(Read::Shadow), Some((v, l))) => self.line(&format!("uint {t} = xcl_sh{v}_{l}; if (!xcl_hw32) {t} = {read};")), _ => self.line(&format!("uint {t} = {read};")), } @@ -4636,8 +4636,10 @@ impl Em { self.x64_prelude(&mut h); } if let Some(u) = &self.upper { - // wave64 VS / PS on a GPU with 32-lane waves only (`upper_lanes`) - h.push_str("// the wave has 32 lanes: it is a console wave whose lanes 32..63 are not populated\nstatic bool xcl_hw32 = false;\n"); + if !self.x64() { + // wave64 VS / PS on a GPU with 32-lane waves only (`upper_lanes`) + h.push_str("// the wave has 32 lanes: it is a console wave whose lanes 32..63 are not populated\nstatic bool xcl_hw32 = false;\n"); + } for (v, l) in &u.shadows { let _ = writeln!(h, "static uint xcl_sh{v}_{l} = 0u; // v_writelane slot: v{v} lane {l}"); } diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 99c2e6fd99..12d1d5a451 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -1431,6 +1431,12 @@ pub fn translate(code: &[u8], opt: &Options) -> Result { } 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(_)) { + let a = upper_lanes::analyze_spills(&insts); + if !a.shadows.is_empty() { + em.upper = Some(a); + } + } // 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 diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 5861db5764..7fd4e67b16 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -6691,6 +6691,48 @@ fn gpu_split_wave64_reduction_tails() { assert_eq!(r.sgpr[21], 0x1111, "inactive-lane-0 s_out keeps unmodified old v7[0]"); } +/// SGPR spill slots (`v_writelane_b32` → `v_readlane_b32` with a constant lane in either half) +/// read their scalar shadow in split-wave64 mode without exchanging across the two halves, while +/// a lane read after a vector write (including overwriting a previously spilled VGPR) still +/// exchanges and reads the written lane. +#[test] +fn gpu_split_wave64_writelane_spill_shadows() { + if !gpu_ready() { + return; + } + let code = prog(&[ + // spill s0 -> v10[5] (lower half) and s1 -> v10[42] (upper half) + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 0), ("SRC1", 128 + 5)], None), + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 1), ("SRC1", 128 + 42)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 2), ("SRC0", V + 10), ("SRC1", 128 + 5)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 3), ("SRC0", V + 10), ("SRC1", 128 + 42)], None), + // spill s0 -> v11[42], then overwrite v11 as a vector: lane 42 must invalidate shadow and exchange + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 11), ("SRC0", 0), ("SRC1", 128 + 42)], None), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 11), ("SRC0", 128 + 10), ("VSRC1", 0)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 4), ("SRC0", V + 11), ("SRC1", 128 + 42)], None), + ]); + if runner::split_wave64() { + let t = translate(&code, &runner::options(Mode::Kernel)).unwrap(); + assert!(t.hlsl.contains("xcl_sh10_5") && t.hlsl.contains("xcl_sh10_42"), "shadow scalars declared"); + assert!(!t.hlsl.contains("xcl_hw32"), "split-wave64 does not emit VS/PS xcl_hw32"); + assert_eq!(t.hlsl.matches("= xcl_x_rla_uniform(").count(), 1, "only the post-overwrite read exchanges"); + } + let mut st = KernelState::default(); + st.sgpr[0] = 0x1122_3344; + st.sgpr[1] = 0x5566_7788; + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[0] = 100 + l as u32; + r[10] = 0xDEAD_0000 + l as u32; + } + let r = runner::run_code(&code, &st, &[]).unwrap(); + assert_eq!(r.sgpr[2], 0x1122_3344, "lower-half spill reload"); + assert_eq!(r.sgpr[3], 0x5566_7788, "upper-half spill reload"); + assert_eq!(r.sgpr[4], 152, "post-VALU-overwrite upper-half lane read still exchanges"); + assert_eq!(r.vgpr[5][10], 0x1122_3344, "v_writelane still updates VGPR lane 5"); + assert_eq!(r.vgpr[42][10], 0x5566_7788, "v_writelane still updates VGPR lane 42"); + assert_eq!(r.vgpr[6][10], 0xDEAD_0006, "other lanes of v10 untouched"); +} + /// 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 diff --git a/crates/xcl/src/gpu/recomp/upper_lanes.rs b/crates/xcl/src/gpu/recomp/upper_lanes.rs index 7590f9414e..ad465f638b 100644 --- a/crates/xcl/src/gpu/recomp/upper_lanes.rs +++ b/crates/xcl/src/gpu/recomp/upper_lanes.rs @@ -815,6 +815,92 @@ pub fn analyze(insts: &[Decoded]) -> UpperLanes { out } +/// Forward data-flow for split-wave64 (`x64`) programs: every `v_readlane_b32` with a constant +/// lane whose slot was written by `v_writelane_b32` on every path reaching it (with no +/// intervening write to that VGPR) can read a scalar shadow instead of exchanging across the two +/// wave32 halves. Both halves hold identical SGPRs (`x64_masks` keeps `v_writelane_b32`'s scalar +/// source whole-live). +pub fn analyze_spills(insts: &[Decoded]) -> UpperLanes { + let mut out = UpperLanes::default(); + if insts.is_empty() || !insts.iter().any(|d| d.inst.name == "V_WRITELANE_B32") { + return out; + } + let pcs: BTreeMap = insts.iter().enumerate().map(|(i, d)| (d.pc, i)).collect(); + let succ = successors(insts, &pcs); + let loops = super::loop_ranges(insts); + let mut at: Vec>> = vec![None; insts.len()]; + at[0] = Some(BTreeMap::new()); + let mut work = BTreeSet::from([0usize]); + let join = |a: &BTreeMap, b: &BTreeMap| -> BTreeMap { + a.iter() + .filter_map(|(v, m)| { + let m = m & b.get(v).copied().unwrap_or(0); + (m != 0).then_some((*v, m)) + }) + .collect() + }; + let propagate = |at: &mut [Option>], work: &mut BTreeSet, j: usize, st: &BTreeMap| { + let new = match &at[j] { + None => st.clone(), + Some(old) => join(old, st), + }; + if at[j].as_ref() != Some(&new) { + at[j] = Some(new); + work.insert(j); + } + }; + while let Some(i) = work.pop_first() { + let before = at[i].clone().expect("queued with a state"); + let mut st = before.clone(); + let d = &insts[i]; + let name = d.inst.name; + if name == "V_WRITELANE_B32" { + let v = rdna2::field_value(d.enc, &d.words, "VDST").unwrap_or(0); + match lane_operand(d) { + Lane::Const(l) => *st.entry(v).or_default() |= 1u64 << l, + Lane::Sgpr => { + st.remove(&v); + } + } + } else if name.starts_with("V_MOVREL") || name.starts_with("V_SWAPREL") { + st.clear(); + } else { + for v in vgpr_writes(d) { + st.remove(&v); + } + } + let Some(s) = &succ[i] else { + return UpperLanes::default(); + }; + for &j in s { + propagate(&mut at, &mut work, j, &st); + } + // `LOOP_GUARD` can exit a loop early before or after any instruction in its range. + for &(a, b) in &loops { + if a <= d.pc && d.pc < b { + if let Some((_, &exit_j)) = pcs.range(b..).next() { + propagate(&mut at, &mut work, exit_j, &before); + propagate(&mut at, &mut work, exit_j, &st); + } + } + } + } + for (d, st) in insts.iter().zip(&at) { + let Some(st) = st else { + continue; + }; + if d.inst.name == "V_READLANE_B32" { + if let Some((v, l)) = slot(d) { + if (st.get(&v).copied().unwrap_or(0) >> l) & 1 == 1 { + out.shadows.insert((v, l)); + out.reads.insert(d.pc, Read::Shadow); + } + } + } + } + out +} + #[cfg(test)] mod tests { use super::*; @@ -979,4 +1065,61 @@ mod tests { let r = reads(&[("S_CBRANCH_SCC0", "ENC_SOPP", &[("SIMM16", 4)], None), WWM, MASK, SEED, RESTORE, READ32]); assert_eq!(r, vec![Read::Const(u32::MAX)]); } + + #[test] + fn spills_follow_writelane_for_both_halves() { + let p = program(&[ + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 20), ("SRC1", 128 + 5)], None), + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 21), ("SRC1", 128 + 37)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V + 10), ("SRC1", 128 + 5)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 23), ("SRC0", V + 10), ("SRC1", 128 + 37)], None), + ]); + let a = analyze_spills(&p); + assert_eq!(a.reads.get(&p[2].pc), Some(&Read::Shadow)); + assert_eq!(a.reads.get(&p[3].pc), Some(&Read::Shadow)); + assert!(a.shadows.contains(&(10, 5)) && a.shadows.contains(&(10, 37))); + } + + #[test] + fn spills_stay_conservative_across_overwrites_branches_and_loops() { + // VALU write to the VGPR invalidates its spill slots + let p = program(&[ + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 20), ("SRC1", 128 + 5)], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 10), ("SRC0", 128 + 1)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V + 10), ("SRC1", 128 + 5)], None), + ]); + assert!(analyze_spills(&p).shadows.is_empty()); + + // dynamic-lane writelane invalidates the VGPR's slots + let p = program(&[ + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 20), ("SRC1", 128 + 5)], None), + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 21), ("SRC1", 2)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V + 10), ("SRC1", 128 + 5)], None), + ]); + assert!(analyze_spills(&p).shadows.is_empty()); + + // only one branch writes the spill slot + let p = program(&[ + ("S_CBRANCH_SCC0", "ENC_SOPP", &[("SIMM16", 2)], None), + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 20), ("SRC1", 128 + 5)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V + 10), ("SRC1", 128 + 5)], None), + ]); + assert!(analyze_spills(&p).shadows.is_empty()); + + // write inside a loop cannot be trusted after the loop (LOOP_GUARD may exit before it) + let p = program(&[ + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 20), ("SRC1", 128 + 5)], None), + ("S_CBRANCH_SCC0", "ENC_SOPP", &[("SIMM16", (-3i16) as u16 as u64)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V + 10), ("SRC1", 128 + 5)], None), + ]); + assert!(analyze_spills(&p).shadows.is_empty()); + + // indirect jump bails out + let p = program(&[ + ("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 10), ("SRC0", 20), ("SRC1", 128 + 5)], None), + ("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 2)], None), + ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V + 10), ("SRC1", 128 + 5)], None), + ]); + assert!(analyze_spills(&p).shadows.is_empty()); + } }