From 89d71f2d3c8c941dc992dd916517b4267043ecc1 Mon Sep 17 00:00:00 2001 From: luckyteagirl <290248872+luckyteagirl@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:01:28 -0800 Subject: [PATCH] Recompiler: saturate packed unsigned dot8 without i64 The unsigned four-bit dot products total at most 1800. The clamped path can accumulate that bounded sum with Dot4AddU8Packed and saturate c + sum using a 32-bit carry check. This avoids the old wide-sum DXIL that suspends the RX 6800M device in the focused GPU fixture. Add an isolated clamped overflow regression alongside the existing combined edge cases. --- crates/xcl/src/gpu/dxil/core_tests.rs | 4 ++-- crates/xcl/src/gpu/dxil/lower_wide.rs | 16 ++++++++++++---- crates/xcl/src/gpu/recomp/tests.rs | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index 7492f54b9c..4425296b15 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -614,7 +614,7 @@ fn window_at_lowers_to_32bit_carry_without_int64() { } #[test] -fn dot8_u32_u4_unclamped_lowers_to_dot4_add_u8_packed() { +fn dot8_u32_u4_lowers_to_dot4_add_u8_packed() { use crate::gpu::recomp::{dxc, runner}; const V: u64 = 256; let unclamped = prog(&[("V_DOT8_U32_U4", "ENC_VOP3P", &[("VDST", 3), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2)], None)]); @@ -630,7 +630,7 @@ fn dot8_u32_u4_unclamped_lowers_to_dot4_add_u8_packed() { let dis = super::validate::disassemble(&dxil).unwrap(); assert_eq!(dis.matches("dx.op.dot4AddPacked.i32(i32 164,").count(), 2, "expected 2 Dot4AddU8Packed calls:\n{dis}"); let dis_clamped = super::validate::disassemble(&dxil_clamped).unwrap(); - assert!(!dis_clamped.contains("dot4AddPacked"), "clamped dot8 should stay 64-bit saturating:\n{dis_clamped}"); + assert_eq!(dis_clamped.matches("dx.op.dot4AddPacked.i32(i32 164,").count(), 2, "expected 2 clamped Dot4AddU8Packed calls:\n{dis_clamped}"); } #[test] diff --git a/crates/xcl/src/gpu/dxil/lower_wide.rs b/crates/xcl/src/gpu/dxil/lower_wide.rs index 91f90ffddc..e164cb01de 100644 --- a/crates/xcl/src/gpu/dxil/lower_wide.rs +++ b/crates/xcl/src/gpu/dxil/lower_wide.rs @@ -104,7 +104,7 @@ impl Lw { } "v_dot4_u32_u8" => Some(self.dot_n(src(0), src(1), src(2), 8, false, clamp)), "v_dot4_i32_i8" => Some(self.dot_n(src(0), src(1), src(2), 8, true, clamp)), - "v_dot8_u32_u4" if !clamp => { + "v_dot8_u32_u4" => { let (a, b, c) = (src(0), src(1), src(2)); let a_lo = self.andc(a, 0x0F0F_0F0F); let b_lo = self.andc(b, 0x0F0F_0F0F); @@ -112,10 +112,18 @@ impl Lw { let a_hi = self.andc(a_hi, 0x0F0F_0F0F); let b_hi = self.lshrc(b, 4); let b_hi = self.andc(b_hi, 0x0F0F_0F0F); - let t = self.s.dot4_add_u8_packed(c, a_lo, b_lo); - Some(self.s.dot4_add_u8_packed(t, a_hi, b_hi)) + let start = if clamp { self.c(0) } else { c }; + let t = self.s.dot4_add_u8_packed(start, a_lo, b_lo); + let t = self.s.dot4_add_u8_packed(t, a_hi, b_hi); + if clamp { + // The eight nibble products sum to at most 1800, so only c + t can overflow. + let sum = self.add(c, t); + let overflow = self.cmp(Pred::ULt, sum, c); + Some(self.selc(overflow, u32::MAX, sum)) + } else { + Some(t) + } } - "v_dot8_u32_u4" => Some(self.dot_n(src(0), src(1), src(2), 4, false, clamp)), "v_dot8_i32_i4" => Some(self.dot_n(src(0), src(1), src(2), 4, true, clamp)), _ => None, }; diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 0b018edc36..c618b7b567 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -10006,3 +10006,26 @@ fn gpu_v_dot8_u32_u4_edge_cases() { } } } + +#[test] +fn gpu_v_dot8_u32_u4_clamped_overflow() { + const CASES: [(u32, u32); 4] = [ + (0, 1800), + (0xFFFF_F8F7, u32::MAX), + (0xFFFF_F8F8, u32::MAX), + (u32::MAX, u32::MAX), + ]; + let p = prog(&[("V_DOT8_U32_U4", "ENC_VOP3P", &[("VDST", 3), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2), ("CLAMP", 1)], None)]); + for wave64 in gpu_waves() { + let mut st = KernelState::default(); + for (lane, r) in st.vgpr.iter_mut().enumerate() { + r[0] = u32::MAX; + r[1] = u32::MAX; + r[2] = CASES[lane % CASES.len()].0; + } + let (r, lanes) = run_wave(&p, &st, wave64); + for (lane, v) in r.vgpr.iter().take(lanes).enumerate() { + assert_eq!(v[3], CASES[lane % CASES.len()].1, "wave{lanes} lane {lane}"); + } + } +}