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
30 changes: 30 additions & 0 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,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.wave64 && self.split && self.gfx.as_ref().is_some_and(|g| g.vs.is_some() || g.ps.is_some()) {
// 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 {
Expand Down
27 changes: 27 additions & 0 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4394,6 +4394,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(_)) {
Expand Down
122 changes: 122 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8018,6 +8018,128 @@ 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<u32> {
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<u32> = (0..64).map(|l| 0x1000 + l).collect();
let rev: Vec<u32> = (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<u32> = (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<u32> = (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<u32>)> = 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);
}

#[test]
fn gpu_ldexp_f32() {
for wave64 in gpu_waves() {
Expand Down
Loading