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
20 changes: 20 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,23 @@ fn window_at_lowers_to_32bit_carry_without_int64() {
assert!(!body0.contains("icmp ult"), "zero-offset window_at must not emit carry compare:\n{dis0}");
}
}

#[test]
fn dot8_u32_u4_unclamped_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)]);
let clamped = prog(&[("V_DOT8_U32_U4", "ENC_VOP3P", &[("VDST", 3), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2), ("CLAMP", 1)], None)]);
let ko = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 };
let dxil = translate(&unclamped, ko).unwrap_or_else(|e| panic!("unclamped translation: {e}"));
let dxil_clamped = translate(&clamped, ko).unwrap_or_else(|e| panic!("clamped translation: {e}"));
super::validate::validate(&dxil).unwrap_or_else(|e| panic!("validator (unclamped): {e}"));
super::validate::validate(&dxil_clamped).unwrap_or_else(|e| panic!("validator (clamped): {e}"));
if dxc::location().is_err() {
return runner::skip_test("no DXC");
}
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}");
}
11 changes: 11 additions & 0 deletions crates/xcl/src/gpu/dxil/lower_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ 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 => {
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);
let a_hi = self.lshrc(a, 4);
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))
}
"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,
Expand Down
6 changes: 6 additions & 0 deletions crates/xcl/src/gpu/dxil/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ impl Shader {
self.op(53, "quaternary", Some(t), t, &[width, offset, value, replaced], FnAttrs::ReadNone)
}

/// `Dot4AddU8Packed(acc, a, b)` (SM 6.4+): `acc + sum_{i=0..3} byte_i(a) * byte_i(b)` (mod 2^32).
pub fn dot4_add_u8_packed(&mut self, acc: Value, a: Value, b: Value) -> Value {
let i32 = self.m.i32();
self.op(164, "dot4AddPacked", Some(i32), i32, &[acc, a, b], FnAttrs::ReadNone)
}

fn two_outs(&mut self, op: u32, a: Value, b: Value) -> (Value, Value) {
let i32 = self.m.i32();
let st = self.m.named_struct("dx.types.twoi32", &[i32, i32]);
Expand Down
23 changes: 23 additions & 0 deletions crates/xcl/src/gpu/dxil/ops_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,29 @@ fn half_precision_overloads_run() {
}
}

#[test]
fn dot4_add_u8_packed_matches_cpu() {
let mut k = kernel();
let acc = k.input(0);
let a = k.input(1);
let b = k.input(2);
let d = k.s.dot4_add_u8_packed(acc, a, b);
k.out(0, d);
let c = k.finish([32, 1, 1]);
let accs: Vec<u32> = (0..32).map(|l| if l % 3 == 0 { 0xFFFF_FF00u32.wrapping_add(l) } else { UVALS[l as usize] }).collect();
let bs = uvals2();
let Some(r) = gpu(&c, &mem_of(&[accs.clone(), UVALS.to_vec(), bs.clone()]), &[], &[]) else { return };
for l in 0..32 {
let mut want = accs[l];
for i in 0..4 {
let x = (UVALS[l] >> (8 * i)) & 0xFF;
let y = (bs[l] >> (8 * i)) & 0xFF;
want = want.wrapping_add(x * y);
}
assert_eq!(outv(&r, 0)[l], want, "lane {l}: acc={:#x} a={:#x} b={:#x}", accs[l], UVALS[l], bs[l]);
}
}

/// Every literal `op(<opcode>, "<name>", .., FnAttrs::<X>)` in the backend
/// declares the attributes DXC uses for that opcode (table in
/// `shader::dxc_fn_attrs`, from DXC 1.9 `-Fc` listings).
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/gpu/dxil/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ pub fn dxc_fn_attrs(op: u32) -> Option<FnAttrs> {
137 | 138 => ReadNone, // attributeAtVertex, viewID
139 => ReadOnly, // rawBufferLoad
140 => NoUnwind, // rawBufferStore
164 => ReadNone, // dot4AddU8Packed
165..=169 => NoUnwind, // waveMatch, waveMultiPrefix*, mesh outputs
170 => ReadOnly, // getMeshPayload
171 | 172 => NoUnwind, // storeVertexOutput, storePrimitiveOutput
Expand Down
46 changes: 46 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9647,3 +9647,49 @@ fn gpu_compute_coherent_and_cached_loads() {
}
}
}

#[test]
fn gpu_v_dot8_u32_u4_edge_cases() {
const CASES: [(u32, u32, u32); 16] = [
(0, 0, 0),
(0xFFFF_FFFF, 0xFFFF_FFFF, 0),
(0x0F0F_0F0F, 0x0F0F_0F0F, 123),
(0xF0F0_F0F0, 0xF0F0_F0F0, 456),
(0x0F0F_0F0F, 0xF0F0_F0F0, 0x1234_5678),
(0x0123_4567, 0x89AB_CDEF, 0x1111_2222),
(0xFEDC_BA98, 0x7654_3210, 0),
(0xFFFF_FFFF, 0xFFFF_FFFF, 0xFFFF_F8F7), // exact u32::MAX with sum 8*15*15 = 1800 (0x708): 0xFFFF_F8F7 + 0x708 = 0xFFFF_FFFF
(0xFFFF_FFFF, 0xFFFF_FFFF, 0xFFFF_F8F8), // wraps to 0 unclamped, saturates to u32::MAX clamped
(0xFFFF_FFFF, 0xFFFF_FFFF, 0xFFFF_FFF0),
(0xFFFF_FFFF, 0xFFFF_FFFF, 0xFFFF_FFFF),
(0x8888_8888, 0x9999_9999, 0xFFFF_FE00),
(0x1010_1010, 0x0204_0608, 0),
(0x0102_0304, 0x0506_0708, 0x8000_0000),
(0xA5A5_5A5A, 0x5A5A_A5A5, 0x7FFF_FFFF),
(0x0000_000F, 0x0000_000F, 0xFFFF_FF1F),
];
let p = prog(&[
("V_DOT8_U32_U4", "ENC_VOP3P", &[("VDST", 3), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2)], None),
("V_DOT8_U32_U4", "ENC_VOP3P", &[("VDST", 4), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2), ("CLAMP", 1)], None),
]);
let dot_u4 = |a: u32, b: u32, c: u32, clamp: bool| -> u32 {
let mut sum = c as u64;
for i in 0..8 {
sum += (((a >> (4 * i)) & 0xF) * ((b >> (4 * i)) & 0xF)) as u64;
}
if clamp { sum.min(u32::MAX as u64) as u32 } else { sum as u32 }
};
for wave64 in gpu_waves() {
let mut st = KernelState::default();
for (l, r) in st.vgpr.iter_mut().enumerate() {
let (a, b, c) = CASES[l % CASES.len()];
(r[0], r[1], r[2]) = (a, b, c);
}
let (r, lanes) = run_wave(&p, &st, wave64);
for (l, v) in r.vgpr.iter().take(lanes).enumerate() {
let (a, b, c) = CASES[l % CASES.len()];
assert_eq!(v[3], dot_u4(a, b, c, false), "wave{lanes} lane {l} unclamped v_dot8_u32_u4({a:#x}, {b:#x}, {c:#x})");
assert_eq!(v[4], dot_u4(a, b, c, true), "wave{lanes} lane {l} clamped v_dot8_u32_u4({a:#x}, {b:#x}, {c:#x})");
}
}
}
Loading