From 22133f3abd080bdcdc0ff67703fadfb33bf0c06b Mon Sep 17 00:00:00 2001 From: NWProgramming00 <75951308+NWProgramming00@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:34:30 +0100 Subject: [PATCH 1/2] Recompiler: translate ds_permute_b32 as RDNA2 runs it Each active lane sends DATA0 to lane ((ADDR + offset) >> 2) & 31 of its own 32-lane half, in wave64 too; of several senders the highest lane wins, an active lane nobody sends to gets 0, inactive lanes take no part. Each receiver looks at the 32 senders of its half (rla_half_fn, as ds_swizzle), so the split wave64 form needs no exchange. A wave64 VS / PS that may run in 32-lane waves still refuses it: upper_lanes does not model its writes to lanes 32..63. The model was measured on an RDNA2 gfx1036 in native wave64 and wave32 (26 cases: collisions, partial EXEC, high address bits, cross-half addresses), and matches the measurements reported on #925. The emitted code follows #925 by @maximecmb, which was closed unmerged. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/emit.rs | 27 ++++++++ crates/xcl/src/gpu/recomp/tests.rs | 107 +++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043f..b79929d516 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -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(_)) { diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e648..12254f9225 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -8018,6 +8018,113 @@ 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() { + 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 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, 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], "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() { From 03d0ba8bd7e7d3079a227565bce8b07b2f5051c7 Mon Sep 17 00:00:00 2001 From: NWProgramming00 <75951308+NWProgramming00@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:40:30 +0100 Subject: [PATCH 2/2] Direct DXIL: translate ds_permute_b32 as the HLSL path does The direct translator refused ds_permute_b32. It now lowers it as the HLSL path (#2222) and RDNA2 hardware do: 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 reads the 32 senders of its half in lane order (unrolled) through read_lane_half, so the split form needs no exchange. A wave64 VS / PS that may run in 32-lane waves still refuses it. gpu_ds_permute_b32 runs its cases through the direct translator too (gpu_ds_permute_b32_direct), whatever XCL_DIRECT_DXIL says. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/lower_mem.rs | 30 ++++++++++++++++++++++++++++ crates/xcl/src/gpu/recomp/tests.rs | 23 +++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index a1d034e740..a6d64d2a45 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -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 { diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 12254f9225..a754e832bf 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -8051,6 +8051,16 @@ fn ds_permute_reference_matches_rdna2_values() { #[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 }; @@ -8071,9 +8081,14 @@ fn gpu_ds_permute_b32() { ("DS_PERMUTE_B32", "ENC_DS", &[("VDST", 14), ("ADDR", 4), ("DATA0", 0), ("OFFSET0", 2)], 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 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 { @@ -8099,7 +8114,7 @@ fn gpu_ds_permute_b32() { ]; 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); + assert_eq!(r.vgpr[l][10 + k], w[l], "direct={direct} wave{lanes} split={split_wave64} lane {l}: v{}", 10 + k); } } };