From c3e34429cbb8a216c6f88015f772367614e371be Mon Sep 17 00:00:00 2001 From: Maxime Cambou Date: Tue, 24 Nov 2026 09:27:09 +0100 Subject: [PATCH] Recompiler: translate ds_permute_b32 It was untranslated (the isa_coverage census listed it as missing). Each active lane sends DATA0 to lane ((ADDR + offset) >> 2) & 31 of its own 32-lane half: in wave64 too, as measured on RDNA2 in native wave64 (#925, like ds_bpermute_b32 in #772). Of several senders the highest lane wins, a lane nobody sends to gets 0, inactive lanes neither send nor receive, and no LDS memory is used. Each lane looks at the 32 senders of its half in lane order, through the half-local lane read ds_swizzle uses, so the split wave64 form needs no exchange. In a wave64 VS / PS that may run in 32-lane waves the instruction is refused: the console wave's lanes 32..63 do not exist there. Tests: a GPU test against the measured model (reversal within halves, colliding senders, high address bits, offsets 8 and 128, inactive lanes) in wave32, wave64 and split wave64, and the refusal. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/emit.rs | 25 +++++++++ crates/xcl/src/gpu/recomp/tests.rs | 86 ++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2..c575fea40 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3978,6 +3978,31 @@ 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, as measured on RDNA2 (#925, #772: like + // ds_bpermute there, it never crosses the halves); of several senders to one lane + // the highest wins, a lane nobody sends to gets 0 (no LDS memory is used). The + // 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). + "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 + 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/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 4e032b68d..a765f18d4 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -6974,6 +6974,92 @@ fn gpu_ds_swizzle_and_bpermute_zero_inactive_lanes() { } } +/// `ds_permute_b32` as RDNA2 hardware runs it (measured in native wave64 on RDNA2, #925 / +/// #772): each active lane sends DATA0 to lane `((ADDR + offset) >> 2) & 31` of its own +/// 32-lane half; of several senders the highest lane wins, a 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() +} + +#[test] +fn gpu_ds_permute_b32() { + 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), + ]); + let code = prog(&insns); + let t = translate(&code, &Options { wave64, split_wave64, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl); + let dxil = 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) = (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); + } + for (l, r) in st.vgpr.iter_mut().take(lanes).enumerate() { + (r[0], r[1], r[2], r[3]) = (data[l], a1[l], a2[l], a3[l]); + for dst in 10..=13 { + 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), + ]; + for l in 0..lanes { + for (k, w) in want.iter().enumerate() { + assert_eq!(r.vgpr[l][10 + k], w[l], "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() {