Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion crates/viport-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6574,7 +6574,7 @@ impl<'a> Lower<'a> {
);
int(self, r)
}
"v_dot4_i32_i8" => {
"v_dot4_i32_i8" | "v_dot8_i32_i4" => {
// VOP3P: op_sel picks the half each multiplicand's low half
// reads, op_sel_hi its high half's; the accumulator is read
// whole (as AMD's compilers fold constants into it).
Expand Down Expand Up @@ -6612,6 +6612,24 @@ impl<'a> Lower<'a> {
xs.push(self.s.m.bin(c, Bin::Or, if lo { top } else { bottom }, hi));
}
let acc = s(self, 2)?;
if name == "v_dot8_i32_i4" {
// Eight signed nibble products and the accumulator,
// wrapping (RDNA2 ISA): each nibble is moved to the top
// and shifted back arithmetically to extend its sign.
let k28 = self.s.m.i32(28);
let mut dot = acc;
for n in 0..8 {
let up = self.s.m.i32(28 - 4 * n);
let mut f = [xs[0], xs[1]];
for x in &mut f {
let t = self.s.m.bin(c, Bin::Shl, *x, up);
*x = self.s.m.bin(c, Bin::AShr, t, k28);
}
let p = self.s.m.bin(c, Bin::Mul, f[0], f[1]);
dot = self.s.m.bin(c, Bin::Add, dot, p);
}
return int(self, dot);
}
let i32_ty = self.s.t.i32;
let r = if folded {
let mut dot = acc;
Expand Down Expand Up @@ -11854,6 +11872,20 @@ mod tests {
lowers(&words, 0x8001).unwrap();
}

#[test]
fn signed_nibble_dot_products_decode_and_lower() {
// VOP3P 0x18 `v_dot8_i32_i4 v1, v0, v0, v0` with op_sel_hi set, then
// stored so the product is live.
let dot = [0xCC18_4001u32, 0x100 | 0x100 << 9 | 0x100 << 18 | 3 << 27];
let program = |dot: [u32; 2]| [dot[0], dot[1], 0xD834_0000, 0x0000_0100, S_ENDPGM];
let code: Vec<u8> = program(dot).iter().flat_map(|w| w.to_le_bytes()).collect();
let decoded = crate::gfx10::decode(&code).unwrap();
assert_eq!(decoded[0].name(), Some("v_dot8_i32_i4"));
lowers(&program(dot), 1 << 15).unwrap();
// Saturation is not modelled: refused, not computed wrongly.
assert!(lowers(&program([dot[0] | 1 << 15, dot[1]]), 1 << 15).is_err());
}

fn perm_features(selector: u32) -> u64 {
// Synthetic compute shader:
// v0 is the thread ID and can supply a dynamic selector.
Expand Down
44 changes: 44 additions & 0 deletions crates/viport-shader/src/emit/semantics_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,50 @@ fn byte_dot_products_match_the_hardware() {
]));
}

/// `v_dot8_i32_i4`: eight signed nibble products and the accumulator,
/// wrapping (RDNA2 ISA), with `op_sel_hi` as assemblers write it (all set).
#[test]
fn nibble_dot_products_match_the_hardware() {
let nibbles = [
0u32,
0x1234_5678,
0x7777_7777,
0x8888_8888,
0xF0F0_F0F0,
0x89AB_CDEF,
0xFFFF_FFFF,
];
let inputs = triples(
&nibbles,
&nibbles,
&[0, 1, 0x7FFF_FFFF, 0x8000_0000, 0xFFFF_FF00],
);
assert_clean(check(&[probe(
vop3p(0x18, 5, [v(1), v(2), v(3)], 0, 7, [0, 0]),
inputs,
INT,
move |i| vec![dot8_i4(i[0], i[1], i[2])],
)]));
}

/// The CPU model of `v_dot8_i32_i4`.
fn dot8_i4(a: u32, b: u32, c: u32) -> u32 {
let nibble = |x: u32, k: u32| ((x << (28 - 4 * k)) as i32) >> 28;
(0..8).fold(c as i32, |s, k| s.wrapping_add(nibble(a, k) * nibble(b, k))) as u32
}

#[test]
fn nibble_dot_model_follows_the_isa() {
// Every nibble -8 (0x8): 8 * 64 = 512.
assert_eq!(dot8_i4(0x8888_8888, 0x8888_8888, 0), 512);
// -1 * 7 in every lane: -56, plus the accumulator.
assert_eq!(dot8_i4(0xFFFF_FFFF, 0x7777_7777, 100), 44);
// Only the lowest nibble pair: 3 * -2.
assert_eq!(dot8_i4(0x0000_0003, 0x0000_000E, 0) as i32, -6);
// The sum wraps in 32 bits.
assert_eq!(dot8_i4(0x8888_8888, 0x8888_8888, 0x7FFF_FFFF), 0x8000_01FF);
}

// ---- SDWA ----

/// SDWA operand selection and destination placement in the forms the title
Expand Down
1 change: 1 addition & 0 deletions crates/viport-shader/src/gfx10/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ pub(super) const VOP3P: &[(u16, &str)] = &[
(0x015, "v_dot2_u32_u16"),
(0x016, "v_dot4_i32_i8"),
(0x017, "v_dot4_u32_u8"),
(0x018, "v_dot8_i32_i4"),
(0x019, "v_dot8_u32_u4"),
(0x020, "v_fma_mix_f32"),
(0x021, "v_fma_mixlo_f16"),
Expand Down
Loading