From a5af72bba1a0d25fbd59f37a6a952b33874ce734 Mon Sep 17 00:00:00 2001 From: Lino Emmenegger Date: Wed, 25 Nov 2026 11:59:28 +0100 Subject: [PATCH] Shaders: reverse the halves of an i64 Bfrev with shifts and masks on AMD AMD's driver returns x | bfrev(x) for Bfrev.i32(trunc(mul i64)) when the product is the same on every lane. The i64 Bfrev lowering reverses each 32-bit half after a trunc / lshr of the i64 value, so a Bfrev of an i64 product hit it: fuzz seed 817 (#2115) read 0xfe53ffff for 0x4a43fdeb on an RX 7800 XT. Reduced to Bfrev.i64(mul i64 3, 5). The halves are now reversed with five shift-and-mask steps. The i64 case is the only one that goes through trunc of an i64 value; Bfrev.i32 on a plain i32 is unchanged. The new test fails without the change on the RX 7800 XT (low word 0xf000000f, expected 0) and passes with it. Part of #2115. Seed 783 is a separate driver bug (IMax.i64 after an i64 udiv); the compare / select lowering in #1946 fixes it. Co-Authored-By: Claude Sonnet 5.5 --- crates/xcl/src/gpu/dxil/ops.rs | 26 ++++++++++++++++--- crates/xcl/src/gpu/dxil/ops_tests.rs | 39 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/ops.rs b/crates/xcl/src/gpu/dxil/ops.rs index dae3dc5a61..0ae62b4111 100644 --- a/crates/xcl/src/gpu/dxil/ops.rs +++ b/crates/xcl/src/gpu/dxil/ops.rs @@ -317,15 +317,17 @@ impl Shader { if self.is_i64(v) { // AMD's driver (amdxc64, RDNA3/4) gets `Bfrev.i64` wrong (it // writes the reversed low half into both halves), so reverse - // each 32-bit half and swap them. + // each 32-bit half and swap them. The halves are reversed with + // shifts and masks, not `Bfrev.i32`: the driver also returns + // `x | bfrev(x)` for `Bfrev.i32(trunc(mul i64))` (fuzz seed 817). self.require(Feature::Int64Ops); let i32 = self.m.i32(); let lo = self.m.cast(CastOp::Trunc, v, i32); let c32 = self.m.const_int(t, 32); let hi = self.m.bin(BinOp::LShr, v, c32); let hi = self.m.cast(CastOp::Trunc, hi, i32); - let rlo = self.op(30, "unary", Some(i32), i32, &[lo], FnAttrs::ReadNone); - let rhi = self.op(30, "unary", Some(i32), i32, &[hi], FnAttrs::ReadNone); + let rlo = self.bfrev32_shifts(lo); + let rhi = self.bfrev32_shifts(hi); let rlo = self.m.cast(CastOp::ZExt, rlo, t); let rlo = self.m.bin(BinOp::Shl, rlo, c32); let rhi = self.m.cast(CastOp::ZExt, rhi, t); @@ -334,6 +336,24 @@ impl Shader { self.op(30, "unary", Some(t), t, &[v], FnAttrs::ReadNone) } + /// 32-bit bit reverse by swapping bit groups (no `Bfrev`). + fn bfrev32_shifts(&mut self, v: Value) -> Value { + let i32 = self.m.i32(); + let mut x = v; + for (sh, mask) in [(1, 0x5555_5555u64), (2, 0x3333_3333), (4, 0x0f0f_0f0f), (8, 0x00ff_00ff)] { + let (s, m) = (self.m.const_int(i32, sh), self.m.const_int(i32, mask)); + let a = self.m.bin(BinOp::LShr, x, s); + let a = self.m.bin(BinOp::And, a, m); + let b = self.m.bin(BinOp::And, x, m); + let b = self.m.bin(BinOp::Shl, b, s); + x = self.m.bin(BinOp::Or, a, b); + } + let c16 = self.m.const_int(i32, 16); + let a = self.m.bin(BinOp::LShr, x, c16); + let b = self.m.bin(BinOp::Shl, x, c16); + self.m.bin(BinOp::Or, a, b) + } + /// Bit counts (i16 / i32 / i64 input) → i32. pub fn bit_count(&mut self, op: BitCount, v: Value) -> Value { let t = self.m.type_of(v); diff --git a/crates/xcl/src/gpu/dxil/ops_tests.rs b/crates/xcl/src/gpu/dxil/ops_tests.rs index 50145822b7..27c50fe6c8 100644 --- a/crates/xcl/src/gpu/dxil/ops_tests.rs +++ b/crates/xcl/src/gpu/dxil/ops_tests.rs @@ -983,6 +983,45 @@ fn int64_bit_ops_match_cpu() { } } +/// `Bfrev` of an i64 product that is the same on every lane. AMD's driver +/// returned `x | bfrev(x)` for the low half (`Bfrev.i32(trunc(mul i64))`), which +/// broke fuzz seed 817 (#2115). +#[test] +fn bfrev_i64_of_a_uniform_product_matches_cpu() { + let mut k = kernel(); + let lo = k.input(0); + let i64 = k.s.m.i64(); + let l64 = k.s.m.cast(CastOp::ZExt, lo, i64); + let zero = k.s.m.const_int(i64, 0); + let l64 = k.s.m.bin(BinOp::And, l64, zero); + let cases: [(u64, u64); 2] = [(3, 5), ((-7i64) as u64, 10604288470170369447)]; + let mut outs = vec![]; + for &(a, b) in &cases { + let ca = k.s.m.const_int(i64, a); + let a = k.s.m.bin(BinOp::Or, l64, ca); + let cb = k.s.m.const_int(i64, b); + let p = k.s.m.bin(BinOp::Mul, a, cb); + let rev = k.s.bfrev(p); + let i32 = k.s.m.i32(); + outs.push(k.s.m.cast(CastOp::Trunc, rev, i32)); + let c32 = k.s.m.const_int(i64, 32); + let hi = k.s.m.bin(BinOp::LShr, rev, c32); + outs.push(k.s.m.cast(CastOp::Trunc, hi, i32)); + } + for (i, &o) in outs.iter().enumerate() { + k.out(i as u32, o); + } + let c = k.finish([32, 1, 1]); + let Some(r) = gpu(&c, &mem_of(&[UVALS.to_vec()]), &[], &[]) else { return }; + for (n, &(a, b)) in cases.iter().enumerate() { + let want = a.wrapping_mul(b).reverse_bits(); + for l in 0..32 { + assert_eq!(outv(&r, 2 * n)[l], want as u32, "case {n} low word"); + assert_eq!(outv(&r, 2 * n + 1)[l], (want >> 32) as u32, "case {n} high word"); + } + } +} + #[test] fn uav_array_3d_stores_loads_and_texture_atomics_run() { // u: heap 3 = RWTexture2DArray 4×4×2, heap 5 = RWTexture3D 4×4×2.