Skip to content
Merged
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
26 changes: 23 additions & 3 deletions crates/xcl/src/gpu/dxil/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions crates/xcl/src/gpu/dxil/ops_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint> 4×4×2, heap 5 = RWTexture3D<float> 4×4×2.
Expand Down
Loading