From 68dbe66fda6790c6354d63687cee1864b65ea344 Mon Sep 17 00:00:00 2001 From: Rareuncommon <117473426+Rareuncommon@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:18:51 -0500 Subject: [PATCH 1/2] Shaders: lower v_sad_u8, v_sad_hi_u8, v_sad_u16, v_sad_u32 and v_msad_u8 src2 plus the sum of the unsigned differences |a - b| of the operands' bytes (v_sad_u8), halves (v_sad_u16) or whole dwords (v_sad_u32), wrapping at 32 bits. v_sad_hi_u8 adds the byte sum shifted to the high half; v_msad_u8 leaves out a byte whose mask byte (src1) is 0. AI-assisted (Claude Code). (cherry picked from commit 5d6889e8be76f630445a4057f81221b723898583) --- crates/viport-shader/src/emit/lower.rs | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/crates/viport-shader/src/emit/lower.rs b/crates/viport-shader/src/emit/lower.rs index 05ccaeb..f1e11b1 100644 --- a/crates/viport-shader/src/emit/lower.rs +++ b/crates/viport-shader/src/emit/lower.rs @@ -8259,6 +8259,44 @@ impl<'a> Lower<'a> { let m = self.s.m.bin(c, Bin::Mul, a, b); self.s.m.bin(c, Bin::Add, m, d) } + "v_sad_u8" | "v_sad_hi_u8" | "v_sad_u16" | "v_sad_u32" | "v_msad_u8" => { + // src2 plus the sum of the unsigned differences |a - b| of + // the operands' bytes, halves or whole dwords, wrapping at 32 + // bits. v_sad_hi_u8 adds the sum shifted to the high half; + // v_msad_u8 leaves out a byte whose mask byte (src1) is 0. + let (a, b, d) = (s(self, 0)?, s(self, 1)?, s(self, 2)?); + let (width, parts) = match name { + "v_sad_u16" => (16, 2), + "v_sad_u32" => (32, 1), + _ => (8, 4), + }; + let zero = k(self, 0); + let mut sum = zero; + for i in 0..parts { + let (mut x, mut y) = (a, b); + if width < 32 { + let (sh, mask) = (k(self, width * i), k(self, (1 << width) - 1)); + x = self.s.m.bin(c, Bin::LShr, x, sh); + x = self.s.m.bin(c, Bin::And, x, mask); + y = self.s.m.bin(c, Bin::LShr, y, sh); + y = self.s.m.bin(c, Bin::And, y, mask); + } + let above = self.s.m.cmp(c, pred::IUGT, x, y); + let hi = self.s.m.select(c, above, x, y); + let lo = self.s.m.select(c, above, y, x); + let mut diff = self.s.m.bin(c, Bin::Sub, hi, lo); + if name == "v_msad_u8" { + let masked = self.s.m.cmp(c, pred::NE, y, zero); + diff = self.s.m.select(c, masked, diff, zero); + } + sum = self.s.m.bin(c, Bin::Add, sum, diff); + } + if name == "v_sad_hi_u8" { + let k16 = k(self, 16); + sum = self.s.m.bin(c, Bin::Shl, sum, k16); + } + self.s.m.bin(c, Bin::Add, sum, d) + } "v_min3_i32" | "v_max3_i32" | "v_min3_u32" | "v_max3_u32" | "v_med3_i32" | "v_med3_u32" => { let (a, b, d) = (s(self, 0)?, s(self, 1)?, s(self, 2)?); @@ -11803,6 +11841,16 @@ mod tests { )); } + #[test] + fn sums_of_absolute_differences_lower() { + // VOP3 `op v0, v1, v2, v3`: v_sad_u8, v_sad_hi_u8, v_sad_u16, + // v_sad_u32 and v_msad_u8. + for op in [0x15Au32, 0x15B, 0x15C, 0x15D, 0x171] { + let words = [0xD400_0000 | op << 16, 0x101 | 0x102 << 9 | 0x103 << 18]; + lowers_valu(&words).unwrap_or_else(|e| panic!("VOP3 {op:#x}: {e}")); + } + } + #[test] fn a_scalar_carry_chain_reaches_the_select() { // The high dword of a 64-bit add or subtract, then a select on the From 2cd2aea414fedaad2124ed4f02beb6a6a3f75664 Mon Sep 17 00:00:00 2001 From: lcg007xd-c <272343469+lcg007xd-c@users.noreply.github.com> Date: Wed, 25 Nov 2026 18:12:09 -0300 Subject: [PATCH 2/2] shader: add the lowers_valu test helper the v_sad test uses Upstream added it with the v_mul_hi_*_24 lowering (344704e), which this tree already has in another form; only the helper is taken. Co-Authored-By: Claude Opus 5.5 --- crates/viport-shader/src/emit/lower.rs | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/crates/viport-shader/src/emit/lower.rs b/crates/viport-shader/src/emit/lower.rs index f1e11b1..482a108 100644 --- a/crates/viport-shader/src/emit/lower.rs +++ b/crates/viport-shader/src/emit/lower.rs @@ -11841,6 +11841,45 @@ mod tests { )); } + /// Lowers a one-thread compute program that copies four root constants + /// (s0..s3) into v1..v4, runs `body`, and stores v0 to a raw buffer from a + /// descriptor table, so the vector instructions in `body` have an effect. + fn lowers_valu(body: &[u32]) -> Result<(), crate::ShaderError> { + let mut words = vec![ + 0xBE85_0380, // s_mov_b32 s5, 0 + 0xF408_0202, // s_load_dwordx4 s[8:11], s[4:5], 0 + 0xFA00_0000, + ]; + // v_mov_b32 v1..v4, s0..s3 + words.extend((0..4u32).map(|i| 0x7E00_0200 | (i + 1) << 17 | i)); + words.extend_from_slice(body); + words.extend_from_slice(&[ + 0xE070_0000, // buffer_store_dword v0, off, s[8:11], 0 + 0x8002_0000, + S_ENDPGM, + ]); + let constants = RootParam { + sgprs: 4, + ..root(RootType::Constants, 0, 0) + }; + let buffer = UsedResource { + kind: ResourceType::Uav, + root: RootType::Table, + table_offset_dwords: 0, + lower_bound: 0, + stride: 0, + dwords: 4, + user_data: 4, + slot: 1, + }; + lower_with( + &words, + vec![constants, root(RootType::Table, 4, 1)], + vec![buffer], + ) + .map(|_| ()) + } + #[test] fn sums_of_absolute_differences_lower() { // VOP3 `op v0, v1, v2, v3`: v_sad_u8, v_sad_hi_u8, v_sad_u16,