Merge pull request #119 from gefydev/recomp-clamp-omod

Conflict with fdeacf0 (v_dot4 as SM 6.4 intrinsics) resolved: the intrinsics kept, #119's
clamp forms use them too.
This commit is contained in:
ivall
2026-11-23 15:01:05 +01:00
2 changed files with 152 additions and 6 deletions
+55 -6
View File
@@ -2405,16 +2405,22 @@ impl Em {
/// Float result with output modifiers, as u32.
fn fres(m: &Mods, e: &str) -> String {
let mut s = match m.omod {
format!("asuint({})", Self::out_mods(m, e))
}
/// The output modifiers on an f32 expression: omod, then clamp.
fn out_mods(m: &Mods, e: &str) -> String {
let s = match m.omod {
1 => format!("(({e}) * 2.0f)"),
2 => format!("(({e}) * 4.0f)"),
3 => format!("(({e}) * 0.5f)"),
_ => format!("({e})"),
};
if m.clamp {
s = format!("saturate({s})");
format!("saturate({s})")
} else {
s
}
format!("asuint({s})")
}
/// u32 source expressions in operand order; SGPR-pair masks come as "lo|hi".
@@ -2573,6 +2579,28 @@ impl Em {
// network passes are thousands of them, and the shift-and-multiply form made one pass
// 1.5 MB of DXIL that the AMD driver could not compile (E_OUTOFMEMORY)
"v_dot4c_i32_i8" => Some(format!("(uint)dot4add_i8packed({}, {}, (int){cur})", src(0), src(1))),
// with clamp the sum saturates: the products alone stay in range (a 16-bit pair
// product up to 2^31 is summed in 64 bits)
"v_dot2_i32_i16" if clamp => Some(format!(
"sat_i32((int64_t){} * (int64_t){} + (int64_t){} * (int64_t){} + (int64_t)(int){})",
si(0, false),
si(1, false),
si(0, true),
si(1, true),
src(2)
)),
"v_dot2_u32_u16" if clamp => Some(format!(
"sat_u32((uint64_t)({} * {}) + (uint64_t)({} * {}) + (uint64_t){})",
h(0, false),
h(1, false),
h(0, true),
h(1, true),
src(2)
)),
"v_dot4_u32_u8" if clamp => Some(format!("sat_u32((uint64_t)dot4add_u8packed({}, {}, 0u) + (uint64_t){})", src(0), src(1), src(2))),
"v_dot4_i32_i8" if clamp => Some(format!("sat_i32((int64_t)dot4add_i8packed({}, {}, 0) + (int64_t)(int){})", 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))),
"v_dot2_u32_u16" => Some(format!("({} * {} + {} * {} + {})", h(0, false), h(1, false), h(0, true), h(1, true), src(2))),
"v_dot4_u32_u8" => Some(format!("dot4add_u8packed({}, {}, {})", src(0), src(1), src(2))),
@@ -2596,6 +2624,13 @@ impl Em {
"v_pk_fmac_f16" => fr(format!("mad({}, {}, f16tof32({curh}))", fh(0, hi), fh(1, hi))),
"v_pk_min_f16" => fr(format!("min({}, {})", fh(0, hi), fh(1, hi))),
"v_pk_max_f16" => fr(format!("max({}, {})", fh(0, hi), fh(1, hi))),
// integer clamp saturates each half
"v_pk_add_u16" if clamp => format!("min({a} + {b}, 0xFFFFu)"),
"v_pk_sub_u16" if clamp => format!("select({a} < {b}, 0u, {a} - {b})"),
"v_pk_add_i16" if clamp => format!("((uint)clamp({} + {}, -32768, 32767) & 0xFFFFu)", si(0, hi), si(1, hi)),
"v_pk_sub_i16" if clamp => format!("((uint)clamp({} - {}, -32768, 32767) & 0xFFFFu)", si(0, hi), si(1, hi)),
"v_pk_mad_u16" if clamp => format!("min({a} * {b} + {c}, 0xFFFFu)"),
"v_pk_mad_i16" if clamp => format!("((uint)clamp({} * {} + {}, -32768, 32767) & 0xFFFFu)", si(0, hi), si(1, hi), si(2, hi)),
"v_pk_add_u16" => format!("(({a} + {b}) & 0xFFFFu)"),
"v_pk_sub_u16" => format!("(({a} - {b}) & 0xFFFFu)"),
"v_pk_add_i16" => format!("((uint)({} + {}) & 0xFFFFu)", si(0, hi), si(1, hi)),
@@ -2944,7 +2979,7 @@ impl Em {
"v_trunc_f64" => format!("trunc_d({})", f64v(0)),
"v_ldexp_f64" => format!("ldexp_d({}, (int){})", f64v(0), lo_hi[1].0),
"v_cvt_f32_f64" => {
self.line(&format!("uint {t} = asuint((float){});", f64v(0)));
self.line(&format!("uint {t} = {};", Self::fres(&m, &format!("(float){}", f64v(0)))));
self.write32(&dst, &t);
return Ok(());
}
@@ -2968,7 +3003,13 @@ impl Em {
}
_ => return Err("VALU 64-bit"),
};
let mut e = e;
// omod, then clamp (DXIL has a double saturate)
let mut e = match m.omod {
1 => format!("(({e}) * 2.0)"),
2 => format!("(({e}) * 4.0)"),
3 => format!("(({e}) * 0.5)"),
_ => e,
};
if m.clamp {
e = format!("saturate({e})");
}
@@ -3069,7 +3110,9 @@ impl Em {
fr(format!("f16tof32({h})"))
}
"v_cvt_f16_f32" => {
let h = to_f16_expr(self.dynamic_f16_rm, &fs(0));
// the modifiers in f32 before the rounding: the same f16 (scaling by a power of
// two is exact, and 0 and 1 are f16 values), except where the f16 is denormal
let h = to_f16_expr(self.dynamic_f16_rm, &Self::out_mods(&m, &fs(0)));
if m.opsel >> 3 & 1 == 1 {
format!("({cur} & 0xFFFFu) | ({h} << 16)")
} else if d.variant == Variant::Sdwa {
@@ -3156,6 +3199,10 @@ impl Em {
"v_cubesc_f32" => fr(format!("cube_sc({}, {}, {})", fs(0), fs(1), fs(2))),
"v_cubetc_f32" => fr(format!("cube_tc({}, {}, {})", fs(0), fs(1), fs(2))),
"v_cubema_f32" => fr(format!("cube_ma({}, {}, {})", fs(0), fs(1), fs(2))),
// integer clamp saturates (VOP3 only)
"v_add_nc_u32" if m.clamp => format!("sat_u32((uint64_t){a} + (uint64_t){b})"),
"v_sub_nc_u32" if m.clamp => format!("select({a} < {b}, 0u, {a} - {b})"),
"v_subrev_nc_u32" if m.clamp => format!("select({b} < {a}, 0u, {b} - {a})"),
"v_add_nc_u32" => format!("{a} + {b}"),
"v_sub_nc_u32" => format!("{a} - {b}"),
"v_subrev_nc_u32" => format!("{b} - {a}"),
@@ -3180,6 +3227,7 @@ impl Em {
"v_and_or_b32" => format!("({a} & {b}) | {c}"),
"v_or3_b32" => format!("{a} | {b} | {c}"),
"v_xor3_b32" => format!("{a} ^ {b} ^ {c}"),
"v_add3_u32" if m.clamp => format!("sat_u32((uint64_t){a} + (uint64_t){b} + (uint64_t){c})"),
"v_add3_u32" => format!("{a} + {b} + {c}"),
"v_xad_u32" => format!("({a} ^ {b}) + {c}"),
"v_bfe_u32" => format!("bfe_u({a}, {b}, {c})"),
@@ -5173,6 +5221,7 @@ uint sad_u8(uint a, uint b) { return absd8(a, b, 0u) + absd8(a, b, 8u) + absd8(a
uint msad_u8(uint a, uint b) { return select((b & 0xFFu) != 0u, absd8(a, b, 0u), 0u) + select(((b >> 8) & 0xFFu) != 0u, absd8(a, b, 8u), 0u) + select(((b >> 16) & 0xFFu) != 0u, absd8(a, b, 16u), 0u) + select((b >> 24) != 0u, absd8(a, b, 24u), 0u); }
uint lerp8(uint a, uint b, uint c, uint k) { return ((((a >> k) & 0xFFu) + ((b >> k) & 0xFFu) + ((c >> k) & 1u)) >> 1) << k; }
uint lerp_u8(uint a, uint b, uint c) { return lerp8(a, b, c, 0u) | lerp8(a, b, c, 8u) | lerp8(a, b, c, 16u) | lerp8(a, b, c, 24u); }
uint sat_u32(uint64_t r) { return select(r > (uint64_t)0xFFFFFFFFu, 0xFFFFFFFFu, (uint)r); }
uint sat_i32(int64_t r) { return select(r > (int64_t)0x7FFFFFFF, 0x7FFFFFFFu, select(r < -(int64_t)0x80000000u, 0x80000000u, (uint)r)); }
float div_fixup(float q, float d, float n) { return select(or(or(isnan(d), isnan(n)), or(d == 0.0f, or(isinf(d), isinf(n)))), n / d, q); }
float fma_legacy(float a, float b, float c) { return (a == 0.0f || b == 0.0f) ? c : mad(a, b, c); }
+97
View File
@@ -996,6 +996,103 @@ fn gpu_valu_float_and_int() {
}
}
/// Output modifiers each instruction kind applies: omod on f64 (clamp stays in double
/// precision) and on the f64 → f32 / f32 → f16 conversions, and the integer clamp (saturation) of the
/// unsigned adds, the packed 16-bit adds and the integer dot products.
#[test]
fn gpu_clamp_and_omod_per_instruction() {
if !gpu_ready() {
return;
}
let p = |name: &'static str, f: &'static [(&'static str, u64)]| (name, "ENC_VOP3P", f, None);
let v = |name: &'static str, f: &'static [(&'static str, u64)]| (name, "ENC_VOP3", f, None);
let code = prog(&[
// f64: v[1:2] = 0.1, v[3:4] = 0.2, v[5:6] = 1.25
v("V_ADD_F64", &[("VDST", 10), ("SRC0", V + 1), ("SRC1", V + 3), ("CLAMP", 1)]),
v("V_ADD_F64", &[("VDST", 12), ("SRC0", V + 1), ("SRC1", V + 3), ("OMOD", 1)]),
v("V_ADD_F64", &[("VDST", 14), ("SRC0", V + 5), ("SRC1", V + 1), ("CLAMP", 1)]),
v("V_CVT_F32_F64", &[("VDST", 16), ("SRC0", V + 5), ("CLAMP", 1)]),
v("V_CVT_F32_F64", &[("VDST", 17), ("SRC0", V + 5), ("OMOD", 3)]),
// f32 → f16: v7 = 3.0
v("V_CVT_F16_F32", &[("VDST", 18), ("SRC0", V + 7), ("CLAMP", 1)]),
v("V_CVT_F16_F32", &[("VDST", 19), ("SRC0", V + 7), ("OMOD", 1)]),
// unsigned: v8 = 0xFFFFFFF0, v9 = 0x20, v20 = 0x10
v("V_ADD_NC_U32", &[("VDST", 21), ("SRC0", V + 8), ("SRC1", V + 9), ("CLAMP", 1)]),
v("V_SUB_NC_U32", &[("VDST", 22), ("SRC0", V + 20), ("SRC1", V + 9), ("CLAMP", 1)]),
v("V_SUBREV_NC_U32", &[("VDST", 23), ("SRC0", V + 9), ("SRC1", V + 20), ("CLAMP", 1)]),
v("V_ADD3_U32", &[("VDST", 24), ("SRC0", V + 8), ("SRC1", V + 9), ("SRC2", V + 9), ("CLAMP", 1)]),
v("V_ADD_NC_U32", &[("VDST", 25), ("SRC0", V + 8), ("SRC1", V + 9)]), // no clamp: wraps
// packed 16-bit: v26 = (0xFFF0, 0x0005), v27 = (0x0020, 0x0002), v28 = (0x7FF0, 0x8010), v29 = (0x0020, 0xFFE0)
p("V_PK_ADD_U16", &[("VDST", 30), ("SRC0", V + 26), ("SRC1", V + 27), ("OP_SEL_HI", 3), ("CLAMP", 1)]),
p("V_PK_SUB_U16", &[("VDST", 31), ("SRC0", V + 27), ("SRC1", V + 26), ("OP_SEL_HI", 3), ("CLAMP", 1)]),
p("V_PK_ADD_I16", &[("VDST", 32), ("SRC0", V + 28), ("SRC1", V + 29), ("OP_SEL_HI", 3), ("CLAMP", 1)]),
// halves of src1 swapped: (0x7FF0 − (−32), −32752 − 32)
p("V_PK_SUB_I16", &[("VDST", 33), ("SRC0", V + 28), ("SRC1", V + 29), ("OP_SEL", 2), ("OP_SEL_HI", 1), ("CLAMP", 1)]),
p("V_PK_MAD_U16", &[("VDST", 34), ("SRC0", V + 26), ("SRC1", V + 27), ("SRC2", V + 27), ("OP_SEL_HI", 3), ("OP_SEL_HI_2", 1), ("CLAMP", 1)]),
p("V_PK_MAD_I16", &[("VDST", 35), ("SRC0", V + 28), ("SRC1", V + 27), ("SRC2", V + 29), ("OP_SEL_HI", 3), ("OP_SEL_HI_2", 1), ("CLAMP", 1)]),
p("V_PK_ADD_U16", &[("VDST", 36), ("SRC0", V + 26), ("SRC1", V + 27), ("OP_SEL_HI", 3)]), // no clamp: wraps
// dot products: v37 = 0x7F7F7F7F, v38 = 0xFFFFFFFF, v39 = 0x7FFFFFF0, v40 = 0x80000010
p("V_DOT4_I32_I8", &[("VDST", 41), ("SRC0", V + 37), ("SRC1", V + 37), ("SRC2", V + 39), ("CLAMP", 1)]),
p("V_DOT4_I32_I8", &[("VDST", 42), ("SRC0", V + 37), ("SRC1", V + 38), ("SRC2", V + 40), ("CLAMP", 1)]),
p("V_DOT4_U32_U8", &[("VDST", 43), ("SRC0", V + 38), ("SRC1", V + 38), ("SRC2", V + 8), ("CLAMP", 1)]),
p("V_DOT8_I32_I4", &[("VDST", 44), ("SRC0", V + 37), ("SRC1", V + 37), ("SRC2", V + 39), ("CLAMP", 1)]),
p("V_DOT8_U32_U4", &[("VDST", 45), ("SRC0", V + 38), ("SRC1", V + 38), ("SRC2", V + 8), ("CLAMP", 1)]),
p("V_DOT2_I32_I16", &[("VDST", 46), ("SRC0", V + 28), ("SRC1", V + 28), ("SRC2", V + 39), ("OP_SEL_HI", 3), ("CLAMP", 1)]),
p("V_DOT2_U32_U16", &[("VDST", 47), ("SRC0", V + 26), ("SRC1", V + 26), ("SRC2", V + 8), ("OP_SEL_HI", 3), ("CLAMP", 1)]),
p("V_DOT4_I32_I8", &[("VDST", 48), ("SRC0", V + 37), ("SRC1", V + 37), ("SRC2", V + 39), ("OP_SEL_HI", 3)]), // no clamp: wraps
]);
let d = |x: f64, r: &mut [u32], i: usize| {
r[i] = x.to_bits() as u32;
r[i + 1] = (x.to_bits() >> 32) as u32;
};
let mut st = KernelState::default();
for r in st.vgpr.iter_mut() {
d(0.1, r, 1);
d(0.2, r, 3);
d(1.25, r, 5);
r[7] = f(3.0);
r[8] = 0xFFFF_FFF0;
r[9] = 0x20;
r[20] = 0x10;
r[26] = 0x0005_FFF0;
r[27] = 0x0002_0020;
r[28] = 0x8010_7FF0;
r[29] = 0xFFE0_0020;
r[37] = 0x7F7F_7F7F;
r[38] = 0xFFFF_FFFF;
r[39] = 0x7FFF_FFF0;
r[40] = 0x8000_0010;
}
let r = runner::run_code(&code, &st, &[]).unwrap();
let d64 = |v: &[u32], i: usize| f64::from_bits(v[i] as u64 | (v[i + 1] as u64) << 32);
for l in 0..64 {
let v = &r.vgpr[l];
assert_eq!(d64(v, 10), 0.1 + 0.2, "lane {l} v_add_f64 clamp keeps double precision");
assert_eq!(d64(v, 12), (0.1 + 0.2) * 2.0, "lane {l} v_add_f64 omod *2");
assert_eq!(d64(v, 14), 1.0, "lane {l} v_add_f64 clamp");
assert_eq!(f32::from_bits(v[16]), 1.0, "lane {l} v_cvt_f32_f64 clamp");
assert_eq!(f32::from_bits(v[17]), 0.625, "lane {l} v_cvt_f32_f64 omod /2");
assert_eq!(v[18] & 0xFFFF, 0x3C00, "lane {l} v_cvt_f16_f32 clamp");
assert_eq!(v[19] & 0xFFFF, 0x4600, "lane {l} v_cvt_f16_f32 omod *2");
assert_eq!(v[21..26], [0xFFFF_FFFF, 0, 0, 0xFFFF_FFFF, 0x10], "lane {l} add / sub / subrev / add3 u32 clamp, add without");
assert_eq!(v[30], 0x0007_FFFF, "lane {l} v_pk_add_u16 clamp");
assert_eq!(v[31], 0x0000_0000, "lane {l} v_pk_sub_u16 clamp");
assert_eq!(v[32], 0x8000_7FFF, "lane {l} v_pk_add_i16 clamp");
assert_eq!(v[33], 0x8000_7FFF, "lane {l} v_pk_sub_i16 clamp");
assert_eq!(v[34], 0x000C_FFFF, "lane {l} v_pk_mad_u16 clamp");
assert_eq!(v[35], 0x8000_7FFF, "lane {l} v_pk_mad_i16 clamp");
assert_eq!(v[36], 0x0007_0010, "lane {l} v_pk_add_u16 without clamp");
assert_eq!(v[41], 0x7FFF_FFFF, "lane {l} v_dot4_i32_i8 clamp high");
assert_eq!(v[42], 0x8000_0000, "lane {l} v_dot4_i32_i8 clamp low");
assert_eq!(v[43], 0xFFFF_FFFF, "lane {l} v_dot4_u32_u8 clamp");
assert_eq!(v[44], 0x7FFF_FFFF, "lane {l} v_dot8_i32_i4 clamp");
assert_eq!(v[45], 0xFFFF_FFFF, "lane {l} v_dot8_u32_u4 clamp");
assert_eq!(v[46], 0x7FFF_FFFF, "lane {l} v_dot2_i32_i16 clamp");
assert_eq!(v[47], 0xFFFF_FFFF, "lane {l} v_dot2_u32_u16 clamp");
assert_eq!(v[48], 0x7FFF_FFF0u32.wrapping_add(4 * 127 * 127), "lane {l} v_dot4_i32_i8 without clamp");
}
}
#[test]
fn gpu_bench_really_runs() {
if !gpu_ready() {