diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 82cf4c0905..e743e61be4 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -489,6 +489,8 @@ impl Lw { _ => None, }; let gfx = matches!(entry, Entry::Vertex(_) | Entry::Pixel(_)); + // a VS / PS has no split form, but a wave64 one asked to split runs in 32-lane waves + let gfx_split = split && gfx; let split = split && !gfx; let stage = match entry { Entry::Vertex(_) => Stage::Vertex, @@ -511,6 +513,7 @@ impl Lw { init: Init { io: Some(io), compute: None, vgpr_base: None, entry: Block(0), touched: BTreeSet::new(), slots: HashMap::new(), mem: None }, wave64: o.wave64, split, + gfx_split, lane: z, lane31: z, upper: z, diff --git a/crates/xcl/src/gpu/dxil/lower.rs b/crates/xcl/src/gpu/dxil/lower.rs index 5034d31903..fb39480cba 100644 --- a/crates/xcl/src/gpu/dxil/lower.rs +++ b/crates/xcl/src/gpu/dxil/lower.rs @@ -175,6 +175,9 @@ pub struct Lw { pub wave64: bool, /// wave64 program in two hardware wave32 halves pub split: bool, + /// wave64 VS / PS for a GPU that may run it in 32-lane waves (`KernelOptions::split`; + /// `split` itself is off for them) + pub gfx_split: bool, /// console lane (0..63 / 0..31) pub lane: Value, pub lane31: Value, diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index cc7cd0116a..239b166ee2 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -2121,6 +2121,36 @@ impl Lw { self.write32(&Dst::V(f("VDST")), t); return Ok(()); } + // forward permute, as the HLSL path (`recomp::emit`, measured on RDNA2): each active + // lane sends DATA0 to lane ((ADDR + offset) >> 2) & 31 of its own 32-lane half; of + // several senders the highest wins, an active lane nobody sends to gets 0. Each + // receiver looks at the 32 senders of its half in lane order (unrolled), through + // `read_lane_half`, so the split form needs no exchange. + "ds_permute_b32" => { + if self.gfx_split { + // a 32-lane wave stands for a console wave whose lanes 32..63 do not exist + return err(d, "ds_permute_b32 in a wave64 VS / PS that may run in 32-lane waves"); + } + let (a, v) = (self.vreg(f("ADDR")), self.vreg(f("DATA0"))); + let (el, eh) = self.exec_mask(); + let a = self.addc(a, off16); + let h = self.andc(self.lane, 32); + let me = self.lane31; + let mut t = self.c(0); + for k in 0..32u32 { + let s = self.orc(h, k); + let sa = self.read_lane_half(a, s); + let sd = self.read_lane_half(v, s); + let on = self.lanebit_at(el, eh, s); + let to = self.lshrc(sa, 2); + let to = self.andc(to, 31); + let hit = self.cmp(Pred::Eq, to, me); + let take = self.and(on, hit); + t = self.sel(take, sd, t); + } + self.write32(&Dst::V(f("VDST")), t); + return Ok(()); + } _ => {} } if f("GDS") != 0 { diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 2f817a809f..846800c279 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -4566,6 +4566,33 @@ impl Em { self.write32(&dst, &t); return Ok(()); } + // forward permute: each active lane sends DATA0 to lane ((ADDR + offset) >> 2) & 31 + // of its own 32-lane half, in wave64 too: on RDNA2 it never crosses the halves + // (measured in native wave64 and wave32 on gfx1036, and in #925). Of several + // senders to one lane the highest wins; an active lane nobody sends to gets 0; no + // LDS memory is used. Each receiver looks at the 32 senders of its half in lane + // order, so the split wave64 form needs no exchange (`rla_half_fn`, as ds_swizzle). + // After #925 by @maximecmb. + "ds_permute_b32" => { + if self.opt.wave64 && self.opt.split_wave64 && matches!(self.opt.mode, Mode::Vertex(_) | Mode::Pixel(_)) { + // a 32-lane wave stands for a console wave whose lanes 32..63 do not exist, + // and `upper_lanes` does not model this instruction's writes to them + return Err("ds_permute_b32 in a wave64 VS / PS that may run in 32-lane waves"); + } + let (a, v) = (self.vreg(f("ADDR")), self.vreg(f("DATA0"))); + let (el, eh) = (self.sreg(126), self.sreg(127)); + let eh = if self.opt.wave64 { eh } else { "0u".into() }; + let rla = self.rla_half_fn(); + self.line(&format!("uint {t}_a = {a} + {off16}u;")); + self.line(&format!("uint {t} = 0u, {t}_h = lane_id & 0x20u;")); + self.line(&format!("[loop] for (uint {t}_k = 0u; {t}_k < 32u; {t}_k++) {{")); + self.line(&format!(" uint {t}_s = {t}_h | {t}_k, {t}_sa = {rla}({t}_a, {t}_s), {t}_sd = {rla}({v}, {t}_s);")); + self.line(&format!(" if (lanebit_at({el}, {eh}, {t}_s) && (({t}_sa >> 2) & 31u) == (lane_id & 31u)) {t} = {t}_sd;")); + self.line("}"); + let dst = Dst::V(f("VDST")); + self.write32(&dst, &t); + return Ok(()); + } _ => {} } if !matches!(self.opt.mode, Mode::Kernel | Mode::Mesh(_) | Mode::Geometry(_) | Mode::Compute(_)) { diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index 5d000775ef..c10c165516 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -604,7 +604,7 @@ fn compile_console_shader(blob: &[u8], stage: Stage, sampler_base: u32, windows: if dxc::defer() { return Err("DXIL not compiled yet".into()); } - match direct_graphics(blob, stage, sampler_base, windows) { + match direct_graphics(blob, stage, sampler_base, windows, split_wave64) { Ok(c) => { crate::gpu::dxil::coverage::record_translated(if stage == Stage::Vertex { "VS" } else { "PS" }); return Ok((c, false)); @@ -994,15 +994,17 @@ fn dump_direct(blob: &[u8], dxil: &[u8]) { } } -/// A console vertex / pixel shader through the direct DXIL translator. -fn direct_graphics(blob: &[u8], stage: Stage, sampler_base: u32, windows: u32) -> Result { +/// A console vertex / pixel shader through the direct DXIL translator. `split_wave64`: the +/// GPU may run it in 32-lane waves (only what cannot run so is refused; no split form). +fn direct_graphics(blob: &[u8], stage: Stage, sampler_base: u32, windows: u32, split_wave64: bool) -> Result { let sh = xbox::parse(blob).ok_or("not a console shader container")?; let want = if stage == Stage::Vertex { 4 } else { 5 }; let f = sh.fssm.iter().find(|f| f.stages.first().is_some_and(|s| s.hw == want)).ok_or("no hardware variant")?; let mode = interface(blob, &sh, f).ok_or("unknown shader interface")?; let p = f.programs.first().ok_or("FSSM without a program")?; let code = &f.data[p.offset..p.offset + p.len]; - let o = crate::gpu::dxil::kernel::KernelOptions { wave64: super::fssm_wave64(f, code), split: false, mem_windows: windows, sampler_base }; + let wave64 = super::fssm_wave64(f, code); + let o = crate::gpu::dxil::kernel::KernelOptions { wave64, split: wave64 && split_wave64, mem_windows: windows, sampler_base }; let k = match (&mode, stage) { (Mode::Vertex(vs), Stage::Vertex) => crate::gpu::dxil::kernel::translate_vertex(code, o, vs)?, (Mode::Pixel(ps), Stage::Pixel) => crate::gpu::dxil::kernel::translate_pixel(code, o, ps)?, diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 0b018edc36..9bd74ca823 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -8389,6 +8389,147 @@ fn gpu_ds_swizzle_and_bpermute_zero_inactive_lanes() { } } +/// `ds_permute_b32` as RDNA2 hardware runs it (measured in native wave64 and wave32 on an RDNA2 +/// gfx1036, and in #925): each active lane sends DATA0 to lane `((ADDR + offset) >> 2) & 31` of +/// its own 32-lane half; of several senders the highest lane wins, an active lane no one sends +/// to gets 0, inactive lanes neither send nor receive. +fn ds_permute_reference(lanes: usize, exec: u64, addr: &[u32], offset: u32, data: &[u32], old: u32) -> Vec { + let mut tmp = vec![0u32; lanes]; + for i in 0..lanes { + if exec >> i & 1 == 1 { + tmp[(i & 32) | (addr[i].wrapping_add(offset) >> 2) as usize & 31] = data[i]; + } + } + (0..lanes).map(|i| if exec >> i & 1 == 1 { tmp[i] } else { old }).collect() +} + +/// The reference model itself, on values read from RDNA2 hardware (gfx1036, native wave64): +/// a reversal and a cross-half address stay in each 32-lane half, and `ADDR = 4 * lane + 2` +/// with offset 2 moves each lane on by one (an address and offset shifted apart would give +/// the identity). +#[test] +fn ds_permute_reference_matches_rdna2_values() { + let data: Vec = (0..64).map(|l| 0x1000 + l).collect(); + let rev: Vec = (0..64).map(|l| 4 * (63 - l)).collect(); + let r = ds_permute_reference(64, u64::MAX, &rev, 0, &data, 0); + assert_eq!((r[0], r[31], r[32], r[63]), (0x101F, 0x1000, 0x103F, 0x1020)); + let cross: Vec = (0..64).map(|l| 4 * ((l + 32) % 64)).collect(); + assert_eq!(ds_permute_reference(64, u64::MAX, &cross, 0, &data, 0), data); + let a: Vec = (0..64).map(|l| 4 * l + 2).collect(); + let r = ds_permute_reference(64, u64::MAX, &a, 2, &data, 0); + assert_eq!((r[0], r[31], r[32], r[63]), (0x101F, 0x101E, 0x103F, 0x103E)); +} + +#[test] +fn gpu_ds_permute_b32() { + gpu_ds_permute_b32_with(false); +} + +/// The direct DXIL translator's `ds_permute_b32`, whatever `XCL_DIRECT_DXIL` says. +#[test] +fn gpu_ds_permute_b32_direct() { + gpu_ds_permute_b32_with(true); +} + +fn gpu_ds_permute_b32_with(direct: bool) { + let run_case = |wave64: bool, split_wave64: bool| { + let lanes = if wave64 { 64 } else { 32 }; + let exec: u64 = if wave64 { 0xF0F0_FF0F_7777_FFFE } else { 0x7777_FFFE }; + let mut insns: Vec<(&str, &str, &[(&str, u64)], Option)> = vec![("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 255)], Some(exec as u32))]; + if wave64 { + insns.push(("S_MOV_B32", "ENC_SOP1", &[("SDST", 127), ("SSRC0", 255)], Some((exec >> 32) as u32))); + } + insns.extend_from_slice(&[ + // reversal: within each 32-lane half in wave64, not across them + ("DS_PERMUTE_B32", "ENC_DS", &[("VDST", 10), ("ADDR", 1), ("DATA0", 0)], None), + // two senders per lane (the higher wins), the upper lanes get nothing; high address bits ignored + ("DS_PERMUTE_B32", "ENC_DS", &[("VDST", 11), ("ADDR", 2), ("DATA0", 0)], None), + // the offset field moves every lane on by two, wrapping in its half + ("DS_PERMUTE_B32", "ENC_DS", &[("VDST", 12), ("ADDR", 3), ("DATA0", 0), ("OFFSET0", 8)], None), + // offset 128 = 32 lanes: the identity, it does not move a lane to the other half + ("DS_PERMUTE_B32", "ENC_DS", &[("VDST", 13), ("ADDR", 3), ("DATA0", 0), ("OFFSET0", 128)], None), + // address and offset are added before the shift: 4 * lane + 2 with offset 2 moves on by one + ("DS_PERMUTE_B32", "ENC_DS", &[("VDST", 14), ("ADDR", 4), ("DATA0", 0), ("OFFSET0", 2)], None), + ]); + let code = prog(&insns); + let dxil = if direct { + let o = crate::gpu::dxil::kernel::KernelOptions { wave64, split: wave64 && split_wave64, mem_windows: 1, sampler_base: 0 }; + crate::gpu::dxil::kernel::translate(&code, o).unwrap_or_else(|e| panic!("direct DXIL: {e}")) + } else { + let t = translate(&code, &Options { wave64, split_wave64, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl); + super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl)) + }; + let mut st = KernelState::default(); + let (mut data, mut a1, mut a2, mut a3, mut a4) = (vec![], vec![], vec![], vec![], vec![]); + for l in 0..lanes { + data.push(0x1000 + l as u32); + a1.push(4 * (lanes - 1 - l) as u32); + a2.push(4 * (l / 2) as u32 + 0x0001_0000); + a3.push(4 * l as u32); + a4.push(4 * l as u32 + 2); + } + for (l, r) in st.vgpr.iter_mut().take(lanes).enumerate() { + (r[0], r[1], r[2], r[3], r[4]) = (data[l], a1[l], a2[l], a3[l], a4[l]); + for dst in 10..=14 { + r[dst] = 0xCAFE_BABE; + } + } + let r = runner::run(&dxil, &st, &[]).unwrap(); + let want = [ + ds_permute_reference(lanes, exec, &a1, 0, &data, 0xCAFE_BABE), + ds_permute_reference(lanes, exec, &a2, 0, &data, 0xCAFE_BABE), + ds_permute_reference(lanes, exec, &a3, 8, &data, 0xCAFE_BABE), + ds_permute_reference(lanes, exec, &a3, 128, &data, 0xCAFE_BABE), + ds_permute_reference(lanes, exec, &a4, 2, &data, 0xCAFE_BABE), + ]; + for l in 0..lanes { + for (k, w) in want.iter().enumerate() { + assert_eq!(r.vgpr[l][10 + k], w[l], "direct={direct} wave{lanes} split={split_wave64} lane {l}: v{}", 10 + k); + } + } + }; + for wave64 in gpu_waves() { + run_case(wave64, false); + } + if gpu_ready() { + run_case(true, true); + } +} + +/// `ds_permute_b32` is refused in a wave64 VS / PS that may run in 32-lane waves (lanes 32..63 +/// of the console wave do not exist there), and translated in compute. +#[test] +fn ds_permute_b32_refused_where_upper_lanes_do_not_exist() { + let code = prog(&[("DS_PERMUTE_B32", "ENC_DS", &[("VDST", 10), ("ADDR", 1), ("DATA0", 0)], None)]); + let ps = Mode::Pixel(super::PsInterface { inputs: vec![], outputs: vec![], input_ena: 0x7F, attrs: None }); + let t = translate(&code, &Options { mode: ps.clone(), wave64: true, split_wave64: true, ..Default::default() }).unwrap(); + assert_eq!(t.unsupported.get("ds_permute_b32"), Some(&1), "{:?}", t.unsupported); + let t = translate(&code, &Options { mode: ps, wave64: true, split_wave64: false, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + let t = translate(&code, &Options { wave64: true, split_wave64: true, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); +} + +/// The direct translator refuses it in the same place: a wave64 VS / PS asked to split, i.e. +/// for a GPU that may run it in 32-lane waves. Translated when it runs in 64-lane waves. +#[test] +fn ds_permute_b32_direct_refused_where_upper_lanes_do_not_exist() { + use super::{PsInterface, VsInterface}; + use crate::gpu::dxil::kernel::{translate_pixel, translate_vertex, KernelOptions}; + let code = prog(&[("DS_PERMUTE_B32", "ENC_DS", &[("VDST", 10), ("ADDR", 1), ("DATA0", 0)], None)]); + let ps = PsInterface { inputs: vec![], outputs: vec![], input_ena: 0x7F, attrs: None }; + let vs = VsInterface { inputs: vec![], outputs: vec![], attr_base: 4, params: None }; + let o = |split| KernelOptions { wave64: true, split, mem_windows: 1, sampler_base: 0 }; + let refused = "ds_permute_b32 in a wave64 VS / PS"; + let e = translate_pixel(&code, o(true), &ps).err().unwrap_or_default(); + assert!(e.contains(refused), "PS: {e:?}"); + let e = translate_vertex(&code, o(true), &vs).err().unwrap_or_default(); + assert!(e.contains(refused), "VS: {e:?}"); + translate_pixel(&code, o(false), &ps).unwrap_or_else(|e| panic!("PS: {e}")); + translate_vertex(&code, o(false), &vs).unwrap_or_else(|e| panic!("VS: {e}")); +} + #[test] fn gpu_ldexp_f32() { for wave64 in gpu_waves() {