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:
1 parent
25f78a079c
commit
a780306f4c
5 files changed
+529
-14
No files matched your search
@@ -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
|
||||
|
||||
@@ -259,6 +259,39 @@ fn run_cases_on(
|
||||
assert!(ran > 0, "no SM 6.6 device available ({devices:?})");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalar_quad_masks_pack_nibbles_and_set_scc() {
|
||||
let pack_words = |opcode: u32| {
|
||||
[
|
||||
0xBE90_0000 | opcode << 8, // s_quadmask s16, s0 (pairs for b64)
|
||||
sop2(0x0a, 18, 129, 128), // s_cselect_b32 s18, 1, 0
|
||||
sop2(0x1e, 18, 18, 144), // s_lshl_b32 s18, s18, 16
|
||||
sop2(0x10, 16, 16, 18), // s_or_b32 s16, s16, s18
|
||||
vop1(1, 0, sgpr(16)), // v_mov_b32 v0, s16
|
||||
]
|
||||
};
|
||||
let cases = [
|
||||
([0, 0, 0, 0], [0, 0]),
|
||||
([0x8000_0420, 0x0100_0001, 0, 0], [0x1_0086, 0x1_4186]),
|
||||
([0, 0x8000_0000, 0, 0], [0, 0x1_8000]),
|
||||
([u32::MAX, u32::MAX, 0, 0], [0x1_00ff, 0x1_ffff]),
|
||||
];
|
||||
let inputs = cases.map(|(input, _)| input);
|
||||
for (wide, opcode) in [0x2c, 0x2d].into_iter().enumerate() {
|
||||
let shader = build_valu_cs(&pack_words(opcode));
|
||||
run_cases(&shader, &inputs, |backend, i, _, got| {
|
||||
assert_eq!(got, cases[i].1[wide], "{backend}, opcode {opcode:#x}");
|
||||
});
|
||||
}
|
||||
let high = build_valu_cs(&[
|
||||
0xBE90_2D00, // s_quadmask_b64 s[16:17], s[0:1]
|
||||
vop1(1, 0, sgpr(17)),
|
||||
]);
|
||||
run_cases(&high, &inputs, |backend, _, _, got| {
|
||||
assert_eq!(got, 0, "{backend}: high dword");
|
||||
});
|
||||
}
|
||||
|
||||
fn is_f16_nan(bits: u16) -> bool {
|
||||
(bits & 0x7FFF) > 0x7C00
|
||||
}
|
||||
@@ -287,6 +320,33 @@ fn f16_to_f32(bits: u16) -> f32 {
|
||||
f32::from_bits(f32_bits)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalized_buffer_formats_load_their_ends_exactly() {
|
||||
// tbuffer_load_format_x.
|
||||
let x = 0;
|
||||
// (format, dword stored, value loaded)
|
||||
let cases: &[(u8, u32, f32)] = &[
|
||||
(1, 0xFF, 1.0), // 8_UNORM
|
||||
(7, 0xFFFF, 1.0), // 16_UNORM
|
||||
(2, 0x7F, 1.0), // 8_SNORM: the top, the one above the lowest, the lowest
|
||||
(2, 0x81, -1.0),
|
||||
(2, 0x80, -1.0),
|
||||
(8, 0x7FFF, 1.0), // 16_SNORM
|
||||
(8, 0x8001, -1.0),
|
||||
(8, 0x8000, -1.0),
|
||||
];
|
||||
for &(format, stored, want) in cases {
|
||||
let shader = tbuffer_load_of_v1(x, format);
|
||||
run_cases(&shader, &[[stored, 0, 0, 0]], |backend, _, _, got| {
|
||||
assert_eq!(
|
||||
got,
|
||||
want.to_bits(),
|
||||
"{backend}: format {format} of {stored:#x} = {got:#010x}, want {want}",
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn packed_add_selects_halves_of_inline_constants() {
|
||||
// Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves.
|
||||
@@ -486,6 +546,77 @@ fn max_and_min_order_negative_zero_below_positive_zero() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The f16 forms go through the same `max_min` helper, after widening to f32.
|
||||
#[test]
|
||||
fn f16_max_and_min_order_negative_zero_below_positive_zero() {
|
||||
let (pz, nz) = (0_u32, 0x8000_u32);
|
||||
let cases = [
|
||||
[nz, pz, nz, 0],
|
||||
[pz, nz, nz, 0],
|
||||
[nz, pz, pz, 0],
|
||||
[pz, nz, pz, 0],
|
||||
];
|
||||
let ops = [
|
||||
("v_max_f16", 0x139, pz),
|
||||
("v_min_f16", 0x13A, nz),
|
||||
("v_max3_f16", 0x354, pz),
|
||||
("v_min3_f16", 0x351, nz),
|
||||
];
|
||||
for (name, op, want) in ops {
|
||||
let shader = build_valu_cs(&vop3(op, 0, vgpr(1), vgpr(2), vgpr(3)));
|
||||
run_cases(&shader, &cases, |backend, _, inp, got| {
|
||||
// The f16 result is the low half of v0.
|
||||
assert_eq!(got & 0xFFFF, want, "{backend}: {name} {inp:x?}");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_min3_max3_med3_integer_forms_match_rdna2() {
|
||||
// v_{min3,max3,med3}_{i32,u32} v0, v1, v2, v3 (VOP3 0x152..0x159).
|
||||
type Pick = fn([u32; 3]) -> u32;
|
||||
let ops: [(&str, u16, Pick); 6] = [
|
||||
("v_min3_i32", 0x152, |t| {
|
||||
t.into_iter().min_by_key(|&x| x as i32).unwrap()
|
||||
}),
|
||||
("v_min3_u32", 0x153, |t| t.into_iter().min().unwrap()),
|
||||
("v_max3_i32", 0x155, |t| {
|
||||
t.into_iter().max_by_key(|&x| x as i32).unwrap()
|
||||
}),
|
||||
("v_max3_u32", 0x156, |t| t.into_iter().max().unwrap()),
|
||||
("v_med3_i32", 0x158, |mut t| {
|
||||
t.sort_by_key(|&x| x as i32);
|
||||
t[1]
|
||||
}),
|
||||
("v_med3_u32", 0x159, |mut t| {
|
||||
t.sort_unstable();
|
||||
t[1]
|
||||
}),
|
||||
];
|
||||
// Every ordering of three values, with the sign bit set on some so that
|
||||
// the signed and unsigned answers differ.
|
||||
let values = [0u32, 1, 0x7FFF_FFFF, 0x8000_0000, 0xFFFF_FFFF];
|
||||
let mut cases = Vec::new();
|
||||
for a in values {
|
||||
for b in values {
|
||||
for c in values {
|
||||
cases.push([a, b, c, 0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (name, op, pick) in ops {
|
||||
let shader = build_valu_cs(&vop3(op, 0, vgpr(1), vgpr(2), vgpr(3)));
|
||||
run_cases(&shader, &cases, |backend, _, inp, got| {
|
||||
let want = pick([inp[0], inp[1], inp[2]]);
|
||||
assert_eq!(
|
||||
got, want,
|
||||
"{backend}: {name}({:#010x}, {:#010x}, {:#010x}) = {got:#010x}, want {want:#010x}",
|
||||
inp[0], inp[1], inp[2],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_med3_f16_matches_rdna2_with_nan() {
|
||||
// v_med3_f16 v0, v1, v2, v3 (VOP3 opcode 0x357)
|
||||
@@ -579,6 +710,31 @@ fn v_cmp_lg_is_false_for_nan() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_cvt_off_f32_i4_uses_the_signed_low_nibble() {
|
||||
// v_cvt_off_f32_i4 v0, v1 (VOP1 opcode 0x00e)
|
||||
let shader = build_valu_cs(&[vop1(0x00e, 0, vgpr(1))]);
|
||||
let nibble = |n: u32| (((n as i32) << 28) >> 28) as f32 / 16.0;
|
||||
|
||||
let mut cases: Vec<u32> = (0..16).collect();
|
||||
// Bits above the low nibble are ignored.
|
||||
cases.extend([0xABCD_EF05, 0xFFFF_FFF8, 0x1234_5670, 0x8000_000F, 0x0000_00F7]);
|
||||
let inputs: Vec<[u32; 4]> = cases.iter().map(|&x| [x, 0, 0, 0]).collect();
|
||||
run_cases(&shader, &inputs, |backend, i, inp, got| {
|
||||
let want = nibble(cases[i]).to_bits();
|
||||
assert_eq!(
|
||||
got, want,
|
||||
"{backend}: v_cvt_off_f32_i4({:#010x}) = {got:#010x} ({:?}), want {want:#010x}",
|
||||
inp[0],
|
||||
f32::from_bits(got),
|
||||
);
|
||||
});
|
||||
// Spot-check the reference itself: 0x5 -> 0.3125, 0x8 -> -0.5, 0xF -> -0.0625.
|
||||
assert_eq!(nibble(0xABCD_EF05), 0.3125);
|
||||
assert_eq!(nibble(8), -0.5);
|
||||
assert_eq!(nibble(0xF), -0.0625);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_fract_f32_never_returns_one() {
|
||||
// v_fract_f32 v0, v1 (VOP1 opcode 0x020)
|
||||
@@ -865,6 +1021,40 @@ fn v_cvt_u16_and_i16_f16_saturate() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_cvt_f16_u16_and_i16_widen_the_integer_first() {
|
||||
// v_mov_b32 v0, 0; v_cvt_f16_u16 / v_cvt_f16_i16 v0, v1 (VOP1 0x050 / 0x051).
|
||||
// Only integers f16 holds exactly: how the others round is the narrowing's
|
||||
// business, not this conversion's. `None`: the result depends on rounding.
|
||||
let cases: &[(u16, Option<u16>, Option<u16>)] = &[
|
||||
// (u16 / i16 input bits, f16 of the unsigned value, f16 of the signed value)
|
||||
(0x0000, Some(0x0000), Some(0x0000)), // 0
|
||||
(0x0001, Some(0x3C00), Some(0x3C00)), // 1.0
|
||||
(0x07FF, Some(0x67FF), Some(0x67FF)), // 2047, the last integer below 2048 that f16 holds
|
||||
(0x8000, Some(0x7800), Some(0xF800)), // 32768 / -32768
|
||||
(0xFC00, Some(0x7BE0), Some(0xE400)), // 64512 / -1024
|
||||
(0xFFFE, None, Some(0xC000)), // 65534 does not fit f16 / -2
|
||||
(0xFFFF, None, Some(0xBC00)), // 65535 does not fit f16 / -1
|
||||
];
|
||||
for (op, what, pick) in [
|
||||
(0x050u16, "v_cvt_f16_u16", 0usize),
|
||||
(0x051, "v_cvt_f16_i16", 1),
|
||||
] {
|
||||
let shader = build_valu_cs(&[vop1(0x001, 0, 128), vop1(op, 0, vgpr(1))]);
|
||||
let inputs: Vec<[u32; 4]> = cases.iter().map(|c| [u32::from(c.0), 0, 0, 0]).collect();
|
||||
run_cases(&shader, &inputs, |backend, i, _, got| {
|
||||
let want = if pick == 0 { cases[i].1 } else { cases[i].2 };
|
||||
let Some(want) = want else { return };
|
||||
assert_eq!(
|
||||
got,
|
||||
u32::from(want),
|
||||
"{backend}: {what}({:#06x}) = {got:#010x}, want {want:#06x}",
|
||||
cases[i].0,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_sin_and_v_cos_f16_reduce_large_turns() {
|
||||
let sin_shader = build_valu_cs(&[vop1(0x060, 0, vgpr(1))]);
|
||||
@@ -946,6 +1136,38 @@ fn v_sin_and_v_cos_f16_reduce_large_turns() {
|
||||
});
|
||||
}
|
||||
|
||||
/// A 32-bit literal in a signed 64-bit operand is sign-extended (an
|
||||
/// unsigned one's is zero-extended): -100 compared with `v[1:2]`, and
|
||||
/// shifted by `s_ashr_i64`.
|
||||
#[test]
|
||||
fn signed_64_bit_operands_sign_extend_their_literal() {
|
||||
const MINUS_100: u32 = -100_i32 as u32;
|
||||
let check = |name: &str, body: &[u32], cases: &[([u32; 4], u32)]| {
|
||||
let shader = build_valu_cs(body);
|
||||
let inputs: Vec<[u32; 4]> = cases.iter().map(|c| c.0).collect();
|
||||
run_cases(&shader, &inputs, |backend, i, inp, got| {
|
||||
let want = cases[i].1;
|
||||
assert_eq!(got, want, "{backend}: {name} {inp:x?}");
|
||||
});
|
||||
};
|
||||
// v_cmp_*_i64 s6, v[1:2], -100 (VOP3, literal src1), then
|
||||
// v_cndmask_b32 v0, 0, 1, s6.
|
||||
let compare = |op: u16| {
|
||||
let [a, b] = vop3(op, 6, vgpr(1), 255, 0);
|
||||
let [c, d] = vop3(0x101, 0, 0x80, 0x81, sgpr(6));
|
||||
[a, b, MINUS_100, c, d]
|
||||
};
|
||||
// An i64 as the two input dwords of `v[1:2]`.
|
||||
let pair = |n: i64| [n as u32, (n >> 32) as u32, 0, 0];
|
||||
let eq = [(pair(-100), 1), ([MINUS_100, 0, 0, 0], 0), (pair(0), 0)];
|
||||
check("v_cmp_eq_i64", &compare(0x0A2), &eq);
|
||||
let lt = [(pair(-200), 1), (pair(-50), 0), (pair(0), 0)];
|
||||
check("v_cmp_lt_i64", &compare(0x0A1), <);
|
||||
// s_ashr_i64 s[6:7], -100, 4; v_mov_b32 v0, s7: the high dword.
|
||||
let ashr = [sop2(0x23, 6, 255, 0x84), MINUS_100, vop1(0x001, 0, sgpr(7))];
|
||||
check("s_ashr_i64", &ashr, &[([0; 4], u32::MAX)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bfe_signed_and_unsigned_match_rdna2() {
|
||||
// v_bfe_i32 v0, v1, v2, v3 (VOP3 opcode 0x149)
|
||||
|
||||
@@ -235,19 +235,32 @@ impl Instruction {
|
||||
}
|
||||
Encoding::Sopp => Fields::Sopp { imm: w0 as u16 },
|
||||
Encoding::Smem => {
|
||||
let data = match name {
|
||||
"s_gl1_inv" | "s_dcache_inv" | "s_dcache_wb" => None,
|
||||
n if n.starts_with("s_dcache_discard") => None,
|
||||
"s_memtime" | "s_memrealtime" => Some(2),
|
||||
_ => Some(data_dwords(name)),
|
||||
let sdata = (w0 >> 6 & 0x7F) as u8;
|
||||
// s_atc_probe*: SDATA is a 7-bit immediate, not an SGPR. The
|
||||
// buffer form's resource is 4 dwords. GLC and DLC are not operands.
|
||||
let probe = matches!(name, "s_atc_probe" | "s_atc_probe_buffer");
|
||||
let data = if probe {
|
||||
Some(Operand::Literal(u32::from(sdata)))
|
||||
} else {
|
||||
match name {
|
||||
"s_gl1_inv" | "s_dcache_inv" | "s_dcache_wb" => None,
|
||||
n if n.starts_with("s_dcache_discard") => None,
|
||||
"s_memtime" | "s_memrealtime" => Some(scalar(sdata, 2)),
|
||||
_ => Some(scalar(sdata, data_dwords(name))),
|
||||
}
|
||||
};
|
||||
let base = 2 + 2 * u8::from(name.starts_with("s_buffer_"));
|
||||
let base =
|
||||
2 + 2 * u8::from(name.starts_with("s_buffer_") || name == "s_atc_probe_buffer");
|
||||
Fields::Smem {
|
||||
data: data.map(|d| scalar((w0 >> 6 & 0x7F) as u8, d)),
|
||||
data,
|
||||
base: scalar(((w0 & 0x3F) << 1) as u8, base),
|
||||
offset: (w1 << 11) as i32 >> 11,
|
||||
soffset: scalar((w1 >> 25) as u8, 1),
|
||||
cache: cache(bit(w0, 16), false, bit(w0, 14)),
|
||||
cache: if probe {
|
||||
Cache::default()
|
||||
} else {
|
||||
cache(bit(w0, 16), false, bit(w0, 14))
|
||||
},
|
||||
}
|
||||
}
|
||||
Encoding::Vop1 | Encoding::Vop2 | Encoding::Vopc | Encoding::Vop3 | Encoding::Vop3p => {
|
||||
@@ -405,6 +418,8 @@ impl Instruction {
|
||||
saddr: (self.encoding != Encoding::Flat)
|
||||
.then(|| scalar(saddr, 1 + u8::from(self.encoding == Encoding::Global))),
|
||||
offset: match self.encoding {
|
||||
// GFX10 FLAT offsets are 11-bit and unsigned: the
|
||||
// hardware ignores bit 11.
|
||||
Encoding::Flat => (w0 & 0x7FF) as i16,
|
||||
_ => ((w0 << 4) as i16) >> 4,
|
||||
},
|
||||
|
||||
@@ -352,6 +352,21 @@ mod tests {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flat_offsets_ignore_bit_eleven() {
|
||||
let offset = |word0: u32| {
|
||||
let code = bytes(&[word0, 0x0000_0001, S_ENDPGM]);
|
||||
match decode(&code).unwrap()[0].fields(false) {
|
||||
Fields::Flat { offset, .. } => offset,
|
||||
fields => panic!("{fields:?}"),
|
||||
}
|
||||
};
|
||||
// flat_load_dword v0, v[1:2]: the FLAT offset is 11-bit and
|
||||
// unsigned, so bit 11 is ignored (0x800 -> 0, 0xfff -> 0x7ff).
|
||||
assert_eq!(offset(0xDC30_0800), 0);
|
||||
assert_eq!(offset(0xDC30_0FFF), 0x7FF);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tables_are_sorted() {
|
||||
for t in [
|
||||
|
||||
@@ -137,6 +137,12 @@ fn operands(inst: &Instruction, name: &str, fields: &Fields) -> String {
|
||||
cache,
|
||||
} => {
|
||||
if let Some(data) = data {
|
||||
let data = match (name, data) {
|
||||
("s_atc_probe" | "s_atc_probe_buffer", Operand::Literal(imm)) => {
|
||||
atc_immediate(*imm)
|
||||
}
|
||||
_ => data.to_string(),
|
||||
};
|
||||
let off = match (soffset, offset) {
|
||||
(Operand::Scalar { code: NULL, .. }, 0) => soffset.to_string(),
|
||||
(Operand::Scalar { code: NULL, .. }, _) => {
|
||||
@@ -335,6 +341,15 @@ fn operands(inst: &Instruction, name: &str, fields: &Fields) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// `s_atc_probe` immediate: 0..=64 in decimal, larger values in hex.
|
||||
fn atc_immediate(imm: u32) -> String {
|
||||
if imm <= 64 {
|
||||
imm.to_string()
|
||||
} else {
|
||||
format!("0x{imm:x}")
|
||||
}
|
||||
}
|
||||
|
||||
/// Cache flags; images write `dlc` first.
|
||||
fn flags(out: &mut String, cache: &Cache, dlc_first: bool) {
|
||||
let dlc = (cache.dlc, " dlc");
|
||||
@@ -660,7 +675,7 @@ fn valu(name: &str, v: &Valu, encoding: Encoding) -> String {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::gfx10::{Fields, Operand, decode};
|
||||
use crate::gfx10::{Cache, Fields, Operand, decode};
|
||||
|
||||
/// Text of each instruction in `words` (a straight-line program).
|
||||
fn texts(words: &[u32], wave64: bool) -> Vec<String> {
|
||||
@@ -700,6 +715,69 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// `s_atc_probe` / `s_atc_probe_buffer`: SDATA is a 7-bit immediate, the
|
||||
/// buffer resource is 4 dwords, and GLC/DLC are not operands.
|
||||
///
|
||||
/// Words checked with `llvm-mc -arch=amdgcn -mcpu=gfx1030 --disassemble`.
|
||||
/// The offset keeps this printer's 6-digit hex (`0x000064`, not `0x64`).
|
||||
#[test]
|
||||
fn atc_probe_immediate_is_not_a_register() {
|
||||
let cases = [
|
||||
(
|
||||
[0xF498_01C2, 0xFA00_0064],
|
||||
"s_atc_probe 7, s[4:5], 0x000064",
|
||||
Operand::Literal(7),
|
||||
Operand::Scalar { code: 4, dwords: 2 },
|
||||
),
|
||||
(
|
||||
[0xF498_1FF4, 0x0A00_0001],
|
||||
"s_atc_probe 0x7f, s[104:105], s5 offset:0x000001",
|
||||
Operand::Literal(0x7f),
|
||||
Operand::Scalar {
|
||||
code: 104,
|
||||
dwords: 2,
|
||||
},
|
||||
),
|
||||
(
|
||||
[0xF498_0000, 0xFA00_0000],
|
||||
"s_atc_probe 0, s[0:1], null",
|
||||
Operand::Literal(0),
|
||||
Operand::Scalar { code: 0, dwords: 2 },
|
||||
),
|
||||
(
|
||||
[0xF49C_01C4, 0xFA00_0020],
|
||||
"s_atc_probe_buffer 7, s[8:11], 0x000020",
|
||||
Operand::Literal(7),
|
||||
Operand::Scalar { code: 8, dwords: 4 },
|
||||
),
|
||||
(
|
||||
[0xF499_41C2, 0xFA00_0000],
|
||||
"s_atc_probe 7, s[4:5], null",
|
||||
Operand::Literal(7),
|
||||
Operand::Scalar { code: 4, dwords: 2 },
|
||||
),
|
||||
];
|
||||
for (words, text, data, base) in cases {
|
||||
assert_eq!(one(&words, false), text, "{words:08x?}");
|
||||
assert_eq!(one(&words, true), text, "{words:08x?}");
|
||||
let mut code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
|
||||
code.extend(0xBF81_0000u32.to_le_bytes());
|
||||
let inst = &decode(&code).unwrap()[0];
|
||||
let Fields::Smem {
|
||||
data: got,
|
||||
base: got_base,
|
||||
cache,
|
||||
..
|
||||
} = inst.fields(false)
|
||||
else {
|
||||
panic!("not an SMEM instruction");
|
||||
};
|
||||
assert_eq!(got, Some(data), "{words:08x?}");
|
||||
assert_eq!(got_base, base, "{words:08x?}");
|
||||
assert_eq!(cache, Cache::default(), "{words:08x?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lane_masks_follow_the_wave_size() {
|
||||
let cmp = [0x7C08_06F0]; // v_cmp_gt_f32 with 0.5 and v3
|
||||
|
||||
Reference in new issue
Block a user