Upstream instruction fixes: 64-bit literals, exact normalized ends, flat offsets, rare forms

Ports of 7d04943 (a signed 64-bit operand sign-extends its literal),
645391b (a normalized buffer format's ends load as exactly 1.0 and -1.0),
40f30be (FLAT offsets ignore bit 11), 3d71ad1, 344704e, 5d6889e, 78c1267,
c3a04e2, e3fe5bf and the tests 259ba31, 8610ffb, 41b2472, 45ed8e3, 8d67fa5.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 23:24:50 -06:00
1 parent 77b82c9c22
commit c5a2f8d6fd
5 files changed
+529 -14

No files matched your search

+190 -5
View File
@@ -5226,6 +5226,10 @@ impl<'a> Lower<'a> {
let x = crate::resource::inline(*k).ok_or("a special operand")?;
Ok(l.s.m.i32(if (x as i32) < 0 { u32::MAX } else { 0 }))
}
// Of a signed operand, a literal sign-extends too.
Some(&Operand::Literal(x)) if name.ends_with("_i64") => {
Ok(l.s.m.i32(if (x as i32) < 0 { u32::MAX } else { 0 }))
}
_ => Ok(l.s.m.i32(0)),
}
};
@@ -5312,6 +5316,20 @@ impl<'a> Lower<'a> {
l.s.m.cmp(c, pred::NE, o, z)
};
return match name {
"s_quadmask_b64" => {
let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
let low = self.quad_mask(a);
let high = self.quad_mask(b);
let k8 = self.s.m.i32(8);
let high = self.s.m.bin(c, Bin::Shl, high, k8);
let mask = self.s.m.bin(c, Bin::Or, low, high);
let zero = self.s.m.i32(0);
self.set(lo, Val::I32(mask));
self.set(hi, Val::I32(zero));
let nonzero = self.s.m.cmp(c, pred::NE, mask, zero);
self.set(scc, Val::Bit(nonzero));
Ok(())
}
"s_bcnt1_i32_b64" => {
let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
let i32t = self.s.t.i32;
@@ -5437,6 +5455,10 @@ impl<'a> Lower<'a> {
let (r, carry): (V, Option<V>) = match name {
"s_mov_b32" => (src32(self, 0)?, None),
"s_movk_i32" => (sext16(self), None),
"s_quadmask_b32" => {
let pixels = src32(self, 0)?;
(self.quad_mask(pixels), None)
}
"s_cselect_b32" => {
let cond = self.scc_in(inst)?;
let (a, b) = (src32(self, 0)?, src32(self, 1)?);
@@ -5630,6 +5652,29 @@ impl<'a> Lower<'a> {
Ok(())
}
/// Packs whether each consecutive group of four pixels has a set bit.
fn quad_mask(&mut self, pixels: V) -> V {
let c = self.cur;
let one = self.s.m.i32(1);
let shifted = self.s.m.bin(c, Bin::LShr, pixels, one);
let mut mask = self.s.m.bin(c, Bin::Or, pixels, shifted);
// Fold each nibble into its bottom bit, then gather those bits into
// the low byte in three stages without needing a 64-bit operation.
for (distance, keep) in [
(2, 0x1111_1111),
(3, 0x0303_0303),
(6, 0x000f_000f),
(12, 0x0000_00ff),
] {
let distance = self.s.m.i32(distance);
let shifted = self.s.m.bin(c, Bin::LShr, mask, distance);
let combined = self.s.m.bin(c, Bin::Or, mask, shifted);
let keep = self.s.m.i32(keep);
mask = self.s.m.bin(c, Bin::And, combined, keep);
}
mask
}
/// `a + b (+ cin)` or `a - b (- cin)` with the carry (borrow) out, as the
/// scalar and vector add-with-carry instructions set it.
fn add_sub_carry(&mut self, c: usize, add: bool, a: V, b: V, cin: Option<V>) -> (V, V) {
@@ -6300,20 +6345,34 @@ impl<'a> Lower<'a> {
l.s.m.bin(c, Bin::AShr, t, k)
};
let f32t = self.s.t.f32;
// A normalized format's ends are exactly 1.0 and -1.0. The host
// may divide through a reciprocal and land just below 1.0: RDNA
// 4 loaded 16_16_UNORM's 65535 as the half 0x3BFF, not 0x3C00.
let x = match numeric {
Numeric::Unorm => {
let f = self.s.m.cast(c, Cast::UiToFp, field, f32t);
let max = self.s.m.f32((((1u64 << w) - 1) as f32).to_bits());
let top = (1u64 << w) - 1;
let max = self.s.m.f32((top as f32).to_bits());
let q = self.s.m.bin(c, Bin::SDiv, f, max);
let (high, one) = (self.s.m.i32(top as u32), self.s.m.f32(1.0f32.to_bits()));
let at_top = self.s.m.cmp(c, pred::EQ, field, high);
let q = self.s.m.select(c, at_top, one, q);
float(self, q)
}
Numeric::Snorm => {
let s = signed(self);
let f = self.s.m.cast(c, Cast::SiToFp, s, f32t);
let max = self.s.m.f32((((1u64 << (w - 1)) - 1) as f32).to_bits());
let top = (1u32 << (w - 1)) - 1;
let max = self.s.m.f32((top as f32).to_bits());
let q = self.s.m.bin(c, Bin::SDiv, f, max);
let lo = self.s.m.f32((-1.0f32).to_bits());
let q = self.binary(35, q, lo);
// The lowest value is -1.0 too, as is the one above it.
let (high, one) = (self.s.m.i32(top), self.s.m.f32(1.0f32.to_bits()));
let at_top = self.s.m.cmp(c, pred::SGE, s, high);
let q = self.s.m.select(c, at_top, one, q);
let low = self.s.m.i32(top.wrapping_neg());
let minus_one = self.s.m.f32((-1.0f32).to_bits());
let at_bottom = self.s.m.cmp(c, pred::SLE, s, low);
let q = self.s.m.select(c, at_bottom, minus_one, q);
float(self, q)
}
Numeric::Uscaled => {
@@ -7623,6 +7682,17 @@ impl<'a> Lower<'a> {
let r = self.s.m.cast(c, op, a, self.s.t.f32);
float(self, r)
}
"v_cvt_off_f32_i4" => {
// The low nibble as a signed 4-bit integer, over 16.
let a = s(self, 0)?;
let k28 = self.s.m.i32(28);
let a = self.s.m.bin(c, Bin::Shl, a, k28);
let a = self.s.m.bin(c, Bin::AShr, a, k28);
let f = self.s.m.cast(c, Cast::SiToFp, a, self.s.t.f32);
let k = self.s.m.f32(0.0625f32.to_bits());
let r = self.s.m.bin(c, Bin::Mul, f, k);
float(self, r)
}
"v_fma_mix_f32" | "v_fma_mixlo_f16" | "v_fma_mixhi_f16" => {
// Each source is f32, or with op_sel_hi the low (op_sel: high)
// f16 half; neg_hi is the absolute value here.
@@ -8058,8 +8128,14 @@ impl<'a> Lower<'a> {
let x = crate::resource::inline(k).ok_or("a special operand")?;
Ok(self.s.m.int_const(i64t, i64::from(x as i32)))
}
// A signed 64-bit operand sign-extends its 32-bit literal, as
// the hardware does (LLVM's AMDGPU operand rules).
Operand::Literal(x) if inst.name().ends_with("_i64") => {
Ok(self.s.m.int_const(i64t, i64::from(x as i32)))
}
_ => {
// Literals and the null register: zero-extended.
// Literals (of unsigned and bit operands) and the null
// register: zero-extended.
let x = self.operand(inst, &op)?;
Ok(self.s.m.cast(c, Cast::ZExt, x, i64t))
}
@@ -9012,6 +9088,18 @@ impl<'a> Lower<'a> {
let b = self.s.m.bin(c, op, b, sh);
self.s.m.bin(c, Bin::Mul, a, b)
}
"v_mul_hi_u32_u24" | "v_mul_hi_i32_i24" => {
// The high dword of the same 48-bit product: the low 24 bits
// of each operand, zero- or sign-extended, multiplied.
let (a, b) = (s(self, 0)?, s(self, 1)?);
let signed = name == "v_mul_hi_i32_i24";
let (sh, op) = (k(self, 8), if signed { Bin::AShr } else { Bin::LShr });
let a = self.s.m.bin(c, Bin::Shl, a, sh);
let a = self.s.m.bin(c, op, a, sh);
let b = self.s.m.bin(c, Bin::Shl, b, sh);
let b = self.s.m.bin(c, op, b, sh);
self.mul_hi(a, b, signed)
}
"v_bfe_u32" | "v_bfe_i32" => {
let (a, off, w) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
let w = m31(self, w);
@@ -9090,6 +9178,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)?);
@@ -11990,6 +12116,55 @@ 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 the_24_bit_multiplies_lower_both_halves() {
// VOP2 `op v0, v1, v2`: v_mul_i32_i24 and v_mul_u32_u24 (the low
// dword) and v_mul_hi_i32_i24 and v_mul_hi_u32_u24 (the high one).
for op in [0x09u32, 0x0A, 0x0B, 0x0C] {
let word = op << 25 | 2 << 9 | 0x101;
lowers_valu(&[word]).unwrap_or_else(|e| panic!("VOP2 {op:#x}: {e}"));
}
}
#[test]
fn buffer_cache_invalidations_lower_as_no_ops() {
// MUBUF opcode 0x071/0x072 with no data operand: the title asks the
@@ -11998,6 +12173,16 @@ mod tests {
assert!(lowers(&[0xE1C8_0000, 0, S_ENDPGM], 0).is_ok(), "buffer_gl1_inv");
}
#[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