From 9f5225ee806025900ac1ac623a99d23b2fd4d9df Mon Sep 17 00:00:00 2001 From: Shadow Date: Wed, 25 Nov 2026 00:11:30 +0600 Subject: [PATCH] recomp: lower clamped dot products to hardware DP4a intrinsics --- crates/xcl/src/gpu/recomp/emit.rs | 6 ++-- crates/xcl/src/gpu/recomp/tests.rs | 46 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043..54583c18c 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3177,8 +3177,8 @@ impl Em { h(1, true), src(2) )), - "v_dot4_u32_u8" if clamp => Some(format!("sat_u32((uint64_t)dot4_u8({}, {}, 0u) + (uint64_t){})", src(0), src(1), src(2))), - "v_dot4_i32_i8" if clamp => Some(format!("sat_i32((int64_t)(int)dot4_i8({}, {}, 0u) + (int64_t)(int){})", src(0), src(1), src(2))), + "v_dot4_u32_u8" if clamp => Some(format!("dot4_u8_clamp({}, {}, {})", src(0), src(1), src(2))), + "v_dot4_i32_i8" if clamp => Some(format!("dot4_i8_clamp({}, {}, {})", src(0), src(1), src(2))), "v_dot8_u32_u4" if clamp => Some(format!("sat_u32((uint64_t)dot8_u4({}, {}, 0u) + (uint64_t){})", src(0), src(1), src(2))), "v_dot8_i32_i4" if clamp => Some(format!("sat_i32((int64_t)(int)dot8_i4({}, {}, 0u) + (int64_t)(int){})", src(0), src(1), src(2))), "v_dot2_i32_i16" => Some(format!("(uint)({} * {} + {} * {} + (int){})", si(0, false), si(1, false), si(0, true), si(1, true), src(2))), @@ -6397,6 +6397,8 @@ double round_ne_d(double x) { } uint dot4_u8(uint a, uint b, uint c) { uint r = c; [unroll] for (uint i = 0u; i < 32u; i += 8u) r += ((a >> i) & 0xFFu) * ((b >> i) & 0xFFu); return r; } uint dot4_i8(uint a, uint b, uint c) { int r = (int)c; [unroll] for (uint i = 0u; i < 32u; i += 8u) r += (((int)(a << (24u - i))) >> 24) * (((int)(b << (24u - i))) >> 24); return (uint)r; } +uint dot4_u8_clamp(uint a, uint b, uint c) { uint r = dot4add_u8packed(a, b, c); return select(r < c, 0xFFFFFFFFu, r); } +uint dot4_i8_clamp(uint a, uint b, uint c) { int d = dot4add_i8packed(a, b, 0); return sat_i32((int64_t)d + (int64_t)(int)c); } uint dot8_u4(uint a, uint b, uint c) { uint r = c; [unroll] for (uint i = 0u; i < 32u; i += 4u) r += ((a >> i) & 0xFu) * ((b >> i) & 0xFu); return r; } uint dot8_i4(uint a, uint b, uint c) { int r = (int)c; [unroll] for (uint i = 0u; i < 32u; i += 4u) r += (((int)(a << (28u - i))) >> 28) * (((int)(b << (28u - i))) >> 28); return (uint)r; } uint fclass16(uint h) { diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e64..dcc182df0 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -9027,3 +9027,49 @@ fn gpu_scalar_sext_mulhi_and_vector_i24_sub_co_ci_u64_shifts() { assert_eq!(v[22], subrev, "lane {l} v_subrev_co_ci_u32"); } } + +#[test] +fn gpu_dp4a_dot_products_hardware_lowering() { + if !gpu_ready() { + return; + } + let p = |name: &'static str, f: &'static [(&'static str, u64)]| (name, "ENC_VOP3P", f, None); + let code: Vec = prog(&[ + p("V_DOT4_U32_U8", &[("VDST", 10), ("SRC0", V + 1), ("SRC1", V + 2), ("SRC2", V + 3), ("CLAMP", 1)]), + p("V_DOT4_U32_U8", &[("VDST", 11), ("SRC0", V + 4), ("SRC1", V + 4), ("SRC2", V + 5), ("CLAMP", 1)]), + p("V_DOT4_I32_I8", &[("VDST", 12), ("SRC0", V + 1), ("SRC1", V + 2), ("SRC2", V + 3), ("CLAMP", 1)]), + p("V_DOT4_I32_I8", &[("VDST", 13), ("SRC0", V + 6), ("SRC1", V + 6), ("SRC2", V + 7), ("CLAMP", 1)]), + p("V_DOT4_I32_I8", &[("VDST", 14), ("SRC0", V + 6), ("SRC1", V + 4), ("SRC2", V + 8), ("CLAMP", 1)]), + p("V_DOT4_U32_U8", &[("VDST", 15), ("SRC0", V + 4), ("SRC1", V + 4), ("SRC2", V + 5)]), + p("V_DOT4_I32_I8", &[("VDST", 16), ("SRC0", V + 6), ("SRC1", V + 6), ("SRC2", V + 7)]), + ]); + + let t = super::translate(&code, &Options::default()).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported); + assert!(t.hlsl.contains("dot4_u8_clamp("), "{}", t.hlsl); + assert!(t.hlsl.contains("dot4_i8_clamp("), "{}", t.hlsl); + + let mut st = KernelState::default(); + for r in st.vgpr.iter_mut() { + r[1] = 0x0102_0304; + r[2] = 0x0506_0708; + r[3] = 10; + r[4] = 0xFFFF_FFFF; + r[5] = 0xFFFF_FFF0; + r[6] = 0x7F7F_7F7F; + r[7] = 0x7FFF_FFF0; + r[8] = 0x8000_0010; + } + + let r = runner::run_code(&code, &st, &[]).unwrap(); + for l in 0..64usize { + let v = &r.vgpr[l]; + assert_eq!(v[10], 80, "lane {l} v_dot4_u32_u8 clamp non-saturating"); + assert_eq!(v[11], 0xFFFF_FFFF, "lane {l} v_dot4_u32_u8 clamp saturated"); + assert_eq!(v[12], 80, "lane {l} v_dot4_i32_i8 clamp non-saturating"); + assert_eq!(v[13], 0x7FFF_FFFF, "lane {l} v_dot4_i32_i8 clamp saturated max"); + assert_eq!(v[14], 0x8000_0000, "lane {l} v_dot4_i32_i8 clamp saturated min"); + assert_eq!(v[15], 0xFFFF_FFF0u32.wrapping_add(4 * 255 * 255), "lane {l} v_dot4_u32_u8 wrap"); + assert_eq!(v[16], 0x7FFF_FFF0u32.wrapping_add(4 * 127 * 127), "lane {l} v_dot4_i32_i8 wrap"); + } +}