diff --git a/crates/xcl/src/gpu/dxil/module.rs b/crates/xcl/src/gpu/dxil/module.rs index d72ef6fc38..a7f0128da4 100644 --- a/crates/xcl/src/gpu/dxil/module.rs +++ b/crates/xcl/src/gpu/dxil/module.rs @@ -417,6 +417,16 @@ impl Module { let sext = if w >= 64 { v as i64 } else { ((v << (64 - w)) as i64) >> (64 - w) }; self.constant(ConstDef::Int(ty, sext)) } + /// The value of an integer constant (sign-extended to 64 bits), if `v` is one. + pub fn int_const(&self, v: Value) -> Option { + match v { + Value::Const(c) => match self.consts[c as usize] { + ConstDef::Int(_, x) => Some(x), + _ => None, + }, + _ => None, + } + } pub fn i32c(&mut self, v: u32) -> Value { let t = self.i32(); self.const_int(t, v as u64) diff --git a/crates/xcl/src/gpu/dxil/ops.rs b/crates/xcl/src/gpu/dxil/ops.rs index cf7d47b145..8c1272bd14 100644 --- a/crates/xcl/src/gpu/dxil/ops.rs +++ b/crates/xcl/src/gpu/dxil/ops.rs @@ -358,12 +358,18 @@ impl Shader { pub fn bit_count(&mut self, op: BitCount, v: Value) -> Value { let t = self.m.type_of(v); let i32 = self.m.i32(); + if let (BitCount::Countbits, Some(x), Some(w)) = (op, self.m.int_const(v), self.m.int_width(t)) { + // Fold `Countbits` of a constant here: AMD's driver (amdxc64, + // RDNA4) folds `Bfrev(Countbits(K))` to `Bfrev(n) | n` (n = + // countbits(K)), e.g. 0x80000001 for K = 2 (fuzz seed 1599, #2115). + let mask = if w >= 64 { u64::MAX } else { (1u64 << w) - 1 }; + return self.m.i32c(((x as u64) & mask).count_ones()); + } if self.is_i64(v) { self.require(Feature::Int64Ops); if matches!(op, BitCount::Countbits) { - // Count each 32-bit half: AMD's driver (amdxc64, RDNA4) gets - // i64 bit ops wrong (see `bfrev`); fuzz seed 1599 reads a wrong - // value downstream of `Countbits.i64(i64 2)` there only. + // Count each 32-bit half: AMD's driver (amdxc64) gets i64 bit + // ops wrong (see `bfrev`). 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); diff --git a/crates/xcl/src/gpu/dxil/ops_tests.rs b/crates/xcl/src/gpu/dxil/ops_tests.rs index 71989884db..f38723bf86 100644 --- a/crates/xcl/src/gpu/dxil/ops_tests.rs +++ b/crates/xcl/src/gpu/dxil/ops_tests.rs @@ -1053,6 +1053,32 @@ fn i1_to_i64_sign_extension_and_shift_match_cpu() { } } +/// `Bfrev(Countbits(K))` with a constant `K`: AMD's driver (amdxc64, RDNA4) +/// folds it to `Bfrev(n) | n` (0x80000001 for K = 2), so `bit_count` folds +/// `Countbits` of constants itself (#2115, fuzz seed 1599). +#[test] +fn bfrev_of_countbits_of_a_constant_matches_cpu() { + let ks: [u64; 6] = [0, 2, 3, 0xF0, 0xFFFF_FFFF, 0x8000_0000_0000_0003]; + let mut k = kernel(); + let (i32, i64) = (k.s.m.i32(), k.s.m.i64()); + let mut outs = vec![]; + for (i, &c) in ks.iter().enumerate() { + let v = if i == ks.len() - 1 { k.s.m.const_int(i64, c) } else { k.s.m.const_int(i32, c) }; + let n = k.s.bit_count(BitCount::Countbits, v); + outs.push(k.s.bfrev(n)); + } + for (i, &o) in outs.iter().enumerate() { + k.out(i as u32, o); + } + let c = k.finish([32, 1, 1]); + let dis = validate::disassemble(&c).unwrap_or_else(|d| d); + assert!(!dis.contains("Countbits(value)"), "Countbits of a constant is folded:\n{dis}"); + let Some(r) = gpu(&c, &mem_of(&[vec![0; 32]]), &[], &[]) else { return }; + for (i, &v) in ks.iter().enumerate() { + assert_eq!(outv(&r, i)[0], v.count_ones().reverse_bits(), "Bfrev(Countbits({v:#x}))"); + } +} + #[test] fn uav_array_3d_stores_loads_and_texture_atomics_run() { // u: heap 3 = RWTexture2DArray 4×4×2, heap 5 = RWTexture3D 4×4×2.