mirror of
https://github.com/ivall/xcl.git
synced 2026-11-24 05:14:57 +01:00
Merge pull request #91 from certified-retart/fix/recomp-fp-mad-and-round-mode
This commit is contained in:
@@ -45,13 +45,19 @@ pub(super) struct Em {
|
||||
pub static_used: Vec<([u32; 4], bool)>,
|
||||
/// highest VGPR the program references (v_movrel* ranges)
|
||||
pub max_vgpr: u64,
|
||||
/// program contains an f16 VALU/interp instruction whose rounding obeys `MODE[3:2]`
|
||||
has_f16_round_op: bool,
|
||||
/// program contains an f64 instruction whose rounding obeys `MODE[3:2]`
|
||||
has_f64_round_op: bool,
|
||||
/// program writes a non-default `MODE[3:2]` and uses f16 ops (emits `xcl_fp16_rm`)
|
||||
dynamic_f16_rm: bool,
|
||||
}
|
||||
|
||||
/// Instructions without an effect in this execution model.
|
||||
const NOOPS: &[&str] = &[
|
||||
"s_nop", "s_waitcnt", "s_waitcnt_vscnt", "s_waitcnt_vmcnt", "s_waitcnt_expcnt", "s_waitcnt_lgkmcnt", "s_waitcnt_depctr",
|
||||
"s_inst_prefetch", "s_clause", "s_code_end", "s_setprio", "s_sleep", "s_icache_inv", "s_dcache_inv", "s_gl1_inv", "s_ttracedata",
|
||||
"s_ttracedata_imm", "v_nop", "s_denorm_mode", "s_round_mode", "s_setreg_imm32_b32", "s_setreg_b32", "s_sendmsg", "s_sendmsghalt",
|
||||
"s_ttracedata_imm", "v_nop", "s_denorm_mode", "s_sendmsg", "s_sendmsghalt",
|
||||
"s_set_gpr_idx_off", "s_version", "buffer_gl0_inv", "buffer_gl1_inv", "ds_nop",
|
||||
];
|
||||
|
||||
@@ -320,6 +326,106 @@ enum Dst {
|
||||
Null,
|
||||
}
|
||||
|
||||
/// Decode `OPR_HWREG` (`SIMM16` of `s_setreg*`): `(id, offset, width)`.
|
||||
/// `id == 1` is `HW_REG_MODE`, where `[1:0]` is `FP_ROUND_MODE_SP` (f32) and `[3:2]` is
|
||||
/// `FP_ROUND_MODE_DP` (f64 and f16).
|
||||
fn decode_hwreg(simm16: u16) -> (u32, u32, u32) {
|
||||
let id = (simm16 & 0x3F) as u32;
|
||||
let offset = ((simm16 >> 6) & 0x1F) as u32;
|
||||
let width = (((simm16 >> 11) & 0x1F) + 1) as u32;
|
||||
(id, offset, width)
|
||||
}
|
||||
|
||||
/// Bits of `MODE[1:0]` (sp) and `MODE[3:2]` (dp/f16) touched by a `s_setreg*` `SIMM16`.
|
||||
/// Returns `(sp_mask, dp_mask)` (each 2 bits) and `offset`. Non-`MODE` registers return `(0, 0, 0)`.
|
||||
fn hwreg_mode_round_masks(simm16: u16) -> (u32, u32, u32) {
|
||||
let (id, offset, width) = decode_hwreg(simm16);
|
||||
if id != 1 {
|
||||
return (0, 0, 0);
|
||||
}
|
||||
let covers = |b: u32| b >= offset && b < offset + width;
|
||||
let sp = (covers(0) as u32) | ((covers(1) as u32) << 1);
|
||||
let dp = (covers(2) as u32) | ((covers(3) as u32) << 1);
|
||||
(sp, dp, offset)
|
||||
}
|
||||
|
||||
/// Immediate value written to `MODE[base +: 2]` (masked to `mask`) by `s_setreg_imm32_b32`.
|
||||
fn hwreg_imm_field(imm: u32, offset: u32, base: u32, mask: u32) -> u32 {
|
||||
let mut v = 0u32;
|
||||
if mask & 1 != 0 {
|
||||
v |= (imm >> (base - offset)) & 1;
|
||||
}
|
||||
if mask & 2 != 0 {
|
||||
v |= ((imm >> (base + 1 - offset)) & 1) << 1;
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// Instructions whose f16 output rounding obeys `MODE[3:2]` (`FP_ROUND_MODE_DP`).
|
||||
fn is_f16_rounded_op(name: &str) -> bool {
|
||||
matches!(
|
||||
name,
|
||||
"v_interp_p2_f16"
|
||||
| "v_cvt_f16_f32"
|
||||
| "v_fma_mixlo_f16"
|
||||
| "v_fma_mixhi_f16"
|
||||
| "v_pk_add_f16"
|
||||
| "v_pk_mul_f16"
|
||||
| "v_pk_fma_f16"
|
||||
| "v_pk_fmac_f16"
|
||||
| "v_pk_min_f16"
|
||||
| "v_pk_max_f16"
|
||||
| "v_add_f16"
|
||||
| "v_sub_f16"
|
||||
| "v_subrev_f16"
|
||||
| "v_mul_f16"
|
||||
| "v_min_f16"
|
||||
| "v_max_f16"
|
||||
| "v_fma_f16"
|
||||
| "v_med3_f16"
|
||||
| "v_min3_f16"
|
||||
| "v_max3_f16"
|
||||
| "v_frexp_mant_f16"
|
||||
| "v_mad_f16"
|
||||
| "v_fmac_f16"
|
||||
| "v_fmamk_f16"
|
||||
| "v_fmaak_f16"
|
||||
| "v_ldexp_f16"
|
||||
| "v_rcp_f16"
|
||||
| "v_rsq_f16"
|
||||
| "v_sqrt_f16"
|
||||
| "v_log_f16"
|
||||
| "v_exp_f16"
|
||||
| "v_sin_f16"
|
||||
| "v_cos_f16"
|
||||
| "v_fract_f16"
|
||||
| "v_floor_f16"
|
||||
| "v_ceil_f16"
|
||||
| "v_trunc_f16"
|
||||
| "v_rndne_f16"
|
||||
| "v_cvt_f16_u16"
|
||||
| "v_cvt_f16_i16"
|
||||
)
|
||||
}
|
||||
|
||||
/// Instructions whose f64 rounding obeys `MODE[3:2]` (`FP_ROUND_MODE_DP`).
|
||||
fn is_f64_rounded_op(name: &str) -> bool {
|
||||
name.contains("_f64")
|
||||
&& !name.starts_with("v_cmp_")
|
||||
&& !name.starts_with("v_cmpx_")
|
||||
&& !matches!(name, "v_cvt_f32_f64" | "v_cvt_i32_f64" | "v_cvt_u32_f64" | "v_frexp_exp_i32_f64")
|
||||
}
|
||||
|
||||
/// HLSL expression converting float expression `e` to a 16-bit half (`0..=0xFFFF`).
|
||||
/// Uses `f32_to_f16_rm(..., xcl_fp16_rm)` only when the shader changes `MODE[3:2]`.
|
||||
fn to_f16_expr(dynamic_rm: bool, e: &str) -> String {
|
||||
if dynamic_rm {
|
||||
format!("f32_to_f16_rm({e}, xcl_fp16_rm)")
|
||||
} else {
|
||||
format!("(f32tof16({e}) & 0xFFFFu)")
|
||||
}
|
||||
}
|
||||
|
||||
impl Em {
|
||||
pub fn new(opt: Options) -> Self {
|
||||
Em {
|
||||
@@ -341,9 +447,55 @@ impl Em {
|
||||
static_ss: BTreeMap::new(),
|
||||
static_used: Vec::new(),
|
||||
max_vgpr: 0,
|
||||
has_f16_round_op: false,
|
||||
has_f64_round_op: false,
|
||||
dynamic_f16_rm: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Pre-scan the program for f16/f64 rounding-sensitive ops and non-default `MODE[3:2]` writes.
|
||||
/// Shaders that set `MODE[3:2]` without f16/f64 ops pay zero HLSL overhead; shaders that set
|
||||
/// `MODE[3:2]` with f16 ops (and no f64 ops) track `xcl_fp16_rm` dynamically.
|
||||
pub fn configure_round_modes(&mut self, insts: &[Decoded], stop_at_endpgm: bool) {
|
||||
let mut writes_nonzero_dp = false;
|
||||
for d in insts {
|
||||
let name = d.inst.name.to_ascii_lowercase();
|
||||
if stop_at_endpgm && matches!(name.as_str(), "s_endpgm" | "s_endpgm_saved" | "s_endpgm_ordered_ps_done") {
|
||||
break;
|
||||
}
|
||||
if is_f16_rounded_op(&name) {
|
||||
self.has_f16_round_op = true;
|
||||
}
|
||||
if is_f64_rounded_op(&name) {
|
||||
self.has_f64_round_op = true;
|
||||
}
|
||||
match name.as_str() {
|
||||
"s_round_mode" => {
|
||||
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) as u16;
|
||||
if (simm >> 2) & 3 != 0 {
|
||||
writes_nonzero_dp = true;
|
||||
}
|
||||
}
|
||||
"s_setreg_imm32_b32" => {
|
||||
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) as u16;
|
||||
let (_, dp_mask, off) = hwreg_mode_round_masks(simm);
|
||||
if dp_mask != 0 && hwreg_imm_field(d.literal.unwrap_or(0), off, 2, dp_mask) != 0 {
|
||||
writes_nonzero_dp = true;
|
||||
}
|
||||
}
|
||||
"s_setreg_b32" => {
|
||||
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) as u16;
|
||||
let (_, dp_mask, _) = hwreg_mode_round_masks(simm);
|
||||
if dp_mask != 0 {
|
||||
writes_nonzero_dp = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.dynamic_f16_rm = writes_nonzero_dp && self.has_f16_round_op && !self.has_f64_round_op;
|
||||
}
|
||||
|
||||
pub fn take(&mut self) -> String {
|
||||
std::mem::take(&mut self.out)
|
||||
}
|
||||
@@ -725,6 +877,23 @@ impl Em {
|
||||
}
|
||||
Flow::Next
|
||||
}
|
||||
"s_round_mode" => {
|
||||
// MODE[3:0] = SIMM16[3:0]: [1:0] = f32 (SP), [3:2] = f64/f16 (DP).
|
||||
// 0 = RNE (HLSL default), 1 = +inf, 2 = -inf, 3 = RTZ.
|
||||
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) as u16;
|
||||
let sp = (simm & 3) as u32;
|
||||
let dp = ((simm >> 2) & 3) as u32;
|
||||
if sp != 0 {
|
||||
self.unsupported(d, "non-default f32 round mode");
|
||||
} else if dp != 0 && self.has_f64_round_op {
|
||||
self.unsupported(d, "non-default f64 round mode");
|
||||
} else if self.dynamic_f16_rm {
|
||||
self.line(&format!("xcl_fp16_rm = {dp}u;"));
|
||||
} else {
|
||||
*self.ignored.entry(name.to_string()).or_default() += 1;
|
||||
}
|
||||
Flow::Next
|
||||
}
|
||||
_ => {
|
||||
let Some(t) = rdna2::branch_target(d) else {
|
||||
if NOOPS.contains(&name) {
|
||||
@@ -752,7 +921,68 @@ impl Em {
|
||||
|
||||
// ---------------------------------------------------------------- scalar ALU
|
||||
|
||||
fn setreg(&mut self, d: &Decoded, name: &str) -> Result<(), &'static str> {
|
||||
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) as u16;
|
||||
let (sp_mask, dp_mask, off) = hwreg_mode_round_masks(simm);
|
||||
if sp_mask == 0 && dp_mask == 0 {
|
||||
*self.ignored.entry(name.to_string()).or_default() += 1;
|
||||
return Ok(());
|
||||
}
|
||||
if name == "s_setreg_imm32_b32" {
|
||||
let imm = d.literal.ok_or("literal")?;
|
||||
let sp = hwreg_imm_field(imm, off, 0, sp_mask);
|
||||
let dp = hwreg_imm_field(imm, off, 2, dp_mask);
|
||||
if sp != 0 {
|
||||
return Err("non-default f32 round mode");
|
||||
}
|
||||
if dp_mask != 0 && dp != 0 && self.has_f64_round_op {
|
||||
return Err("non-default f64 round mode");
|
||||
}
|
||||
if dp_mask != 0 && self.dynamic_f16_rm {
|
||||
if dp_mask == 3 {
|
||||
self.line(&format!("xcl_fp16_rm = {dp}u;"));
|
||||
} else {
|
||||
let keep = (!dp_mask) & 3;
|
||||
self.line(&format!("xcl_fp16_rm = (xcl_fp16_rm & {keep}u) | {dp}u;"));
|
||||
}
|
||||
} else {
|
||||
*self.ignored.entry(name.to_string()).or_default() += 1;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if sp_mask != 0 {
|
||||
return Err("dynamic f32 round mode");
|
||||
}
|
||||
if self.has_f64_round_op {
|
||||
return Err("dynamic f64 round mode");
|
||||
}
|
||||
if self.dynamic_f16_rm {
|
||||
let sd = Self::field(d, "SDST");
|
||||
let src = self.code32(sd, None, false).ok_or("register")?;
|
||||
let stmt = match dp_mask {
|
||||
3 => {
|
||||
let sh = 2 - off;
|
||||
let expr = if sh == 0 { format!("({src} & 3u)") } else { format!("(({src} >> {sh}u) & 3u)") };
|
||||
format!("xcl_fp16_rm = {expr};")
|
||||
}
|
||||
1 => {
|
||||
let sh = 2 - off;
|
||||
let bit = if sh == 0 { format!("({src} & 1u)") } else { format!("(({src} >> {sh}u) & 1u)") };
|
||||
format!("xcl_fp16_rm = (xcl_fp16_rm & 2u) | {bit};")
|
||||
}
|
||||
_ => format!("xcl_fp16_rm = (xcl_fp16_rm & 1u) | (({src} & 1u) << 1);"),
|
||||
};
|
||||
self.line(&stmt);
|
||||
} else {
|
||||
*self.ignored.entry(name.to_string()).or_default() += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn salu(&mut self, d: &Decoded, name: &str) -> Result<(), &'static str> {
|
||||
if matches!(name, "s_setreg_imm32_b32" | "s_setreg_b32") {
|
||||
return self.setreg(d, name);
|
||||
}
|
||||
if matches!(name, "s_movrels_b32" | "s_movrels_b64" | "s_movreld_b32" | "s_movreld_b64" | "s_movrelsd_2_b32") {
|
||||
return self.smovrel(d, name);
|
||||
}
|
||||
@@ -1585,7 +1815,7 @@ impl Em {
|
||||
"v_interp_p2_f16" => {
|
||||
let v = self.vreg(vdst);
|
||||
let hi = rdna2::field_value(d.enc, &d.words, "OP_SEL").unwrap_or(0) >> 3 & 1 == 1;
|
||||
let h = format!("(f32tof16((float){src}) & 0xFFFFu)");
|
||||
let h = to_f16_expr(self.dynamic_f16_rm, &format!("(float){src}"));
|
||||
let e = if hi { format!("({v} & 0xFFFFu) | ({h} << 16)") } else { format!("({v} & 0xFFFF0000u) | {h}") };
|
||||
self.line(&format!("if ({act}) {v} = {e};"));
|
||||
}
|
||||
@@ -2250,7 +2480,11 @@ impl Em {
|
||||
}
|
||||
};
|
||||
let si = |i: usize, hi: bool| format!("(((int)({} << 16)) >> 16)", h(i, hi));
|
||||
let fr = |e: String| if clamp { format!("(f32tof16(saturate({e})) & 0xFFFFu)") } else { format!("(f32tof16({e}) & 0xFFFFu)") };
|
||||
let dyn_rm = self.dynamic_f16_rm;
|
||||
let fr = |e: String| {
|
||||
let s = if clamp { format!("saturate({e})") } else { e };
|
||||
to_f16_expr(dyn_rm, &s)
|
||||
};
|
||||
let t = self.t();
|
||||
// dot products: one 32-bit result
|
||||
let dot: Option<String> = match name {
|
||||
@@ -2436,6 +2670,7 @@ impl Em {
|
||||
s
|
||||
};
|
||||
let si = |i: usize| format!("(((int)({} << 16)) >> 16)", half(i));
|
||||
let dyn_rm = self.dynamic_f16_rm;
|
||||
let fr = |e: String| -> String {
|
||||
let mut s = match m.omod {
|
||||
1 => format!("(({e}) * 2.0f)"),
|
||||
@@ -2446,7 +2681,7 @@ impl Em {
|
||||
if m.clamp {
|
||||
s = format!("saturate({s})");
|
||||
}
|
||||
format!("(f32tof16({s}) & 0xFFFFu)")
|
||||
to_f16_expr(dyn_rm, &s)
|
||||
};
|
||||
let dst = outs.first().map(|o| Self::dst_of(d, o)).unwrap_or(Dst::Null);
|
||||
let cur = match &dst {
|
||||
@@ -2475,7 +2710,10 @@ impl Em {
|
||||
"v_frexp_mant_f16" => fr(format!("frexp_mant({})", fh(0))),
|
||||
"v_frexp_exp_i16_f16" => format!("((uint)frexp_exp({}) & 0xFFFFu)", fh(0)),
|
||||
"v_cndmask_b16" => return Err("VALU 16-bit"),
|
||||
"v_mad_f16" => fr(format!("mad_nf({}, {}, {})", fh(0), fh(1), fh(2))),
|
||||
"v_mad_f16" => {
|
||||
let rm = if dyn_rm { "xcl_fp16_rm" } else { "0u" };
|
||||
fr(format!("mad_nf_f16({}, {}, {}, {rm})", fh(0), fh(1), fh(2)))
|
||||
}
|
||||
"v_fmac_f16" => fr(format!("mad({}, {}, f16tof32({curh}))", fh(0), fh(1))),
|
||||
"v_fmamk_f16" => fr(format!("mad({}, f16tof32({} & 0xFFFFu), {})", fh(0), u[1], fh(2))),
|
||||
"v_fmaak_f16" => fr(format!("mad({}, {}, f16tof32({} & 0xFFFFu))", fh(0), fh(1), u[2])),
|
||||
@@ -2755,7 +2993,7 @@ impl Em {
|
||||
fr(format!("f16tof32({h})"))
|
||||
}
|
||||
"v_cvt_f16_f32" => {
|
||||
let h = format!("(f32tof16({}) & 0xFFFFu)", fs(0));
|
||||
let h = to_f16_expr(self.dynamic_f16_rm, &fs(0));
|
||||
if m.opsel >> 3 & 1 == 1 {
|
||||
format!("({cur} & 0xFFFFu) | ({h} << 16)")
|
||||
} else if d.variant == Variant::Sdwa {
|
||||
@@ -2799,8 +3037,8 @@ impl Em {
|
||||
}
|
||||
match name {
|
||||
"v_fma_mix_f32" => format!("asuint({e})"),
|
||||
"v_fma_mixlo_f16" => format!("({cur} & 0xFFFF0000u) | (f32tof16({e}) & 0xFFFFu)"),
|
||||
_ => format!("({cur} & 0xFFFFu) | (f32tof16({e}) << 16)"),
|
||||
"v_fma_mixlo_f16" => format!("({cur} & 0xFFFF0000u) | {}", to_f16_expr(self.dynamic_f16_rm, &e)),
|
||||
_ => format!("({cur} & 0xFFFFu) | ({} << 16)", to_f16_expr(self.dynamic_f16_rm, &e)),
|
||||
}
|
||||
}
|
||||
"v_rcp_f32" | "v_rcp_iflag_f32" => fr(format!("rcp({})", fs(0))),
|
||||
@@ -3664,6 +3902,9 @@ impl Em {
|
||||
if self.uses_scratch {
|
||||
h.push_str(PRELUDE_SCRATCH);
|
||||
}
|
||||
if self.dynamic_f16_rm {
|
||||
h.push_str("static uint xcl_fp16_rm = 0u;\n");
|
||||
}
|
||||
let mut sg: BTreeSet<u64> = self.sgprs.clone();
|
||||
sg.extend([106, 107, 124, 126, 127]);
|
||||
let load_sgprs = |h: &mut String| {
|
||||
@@ -4834,6 +5075,37 @@ uint sat_i32(int64_t r) { return select(r > (int64_t)0x7FFFFFFF, 0x7FFFFFFFu, se
|
||||
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); }
|
||||
float mad_nf(float a, float b, float c) { precise float p = a * b; precise float r = p + c; return r; }
|
||||
// IEEE-754 binary32 -> binary16 under RDNA2 MODE[3:2]: 0 = RNE, 1 = +inf, 2 = -inf, 3 = RTZ.
|
||||
uint f32_to_f16_rm(float f, uint rm) {
|
||||
uint x = asuint(f);
|
||||
uint sign = (x >> 16) & 0x8000u;
|
||||
uint ax = x & 0x7FFFFFFFu;
|
||||
if (ax >= 0x7F800000u) return sign | select(ax == 0x7F800000u, 0x7C00u, 0x7E00u | ((ax >> 13) & 0x01FFu));
|
||||
if (ax == 0u) return sign;
|
||||
bool away = or(rm == 0u, or(and(rm == 1u, sign == 0u), and(rm == 2u, sign != 0u)));
|
||||
if (ax >= 0x47800000u) return sign | select(away, 0x7C00u, 0x7BFFu);
|
||||
if (ax < 0x33800000u) {
|
||||
bool inc = select(rm == 0u, ax > 0x33000000u, or(and(rm == 1u, sign == 0u), and(rm == 2u, sign != 0u)));
|
||||
return sign | (uint)inc;
|
||||
}
|
||||
uint h, rem, half_bit;
|
||||
if (ax >= 0x38800000u) {
|
||||
h = (((ax >> 23) - 112u) << 10) | ((ax >> 13) & 0x3FFu);
|
||||
rem = ax & 0x1FFFu;
|
||||
half_bit = 0x1000u;
|
||||
} else {
|
||||
uint sh = 126u - (ax >> 23);
|
||||
uint sig = 0x00800000u | (ax & 0x007FFFFFu);
|
||||
h = sig >> sh;
|
||||
half_bit = 1u << (sh - 1u);
|
||||
rem = sig & ((1u << sh) - 1u);
|
||||
}
|
||||
bool rne_inc = or(rem > half_bit, and(rem == half_bit, (h & 1u) != 0u));
|
||||
bool dir_inc = and(rem != 0u, or(and(rm == 1u, sign == 0u), and(rm == 2u, sign != 0u)));
|
||||
bool inc = select(rm == 0u, rne_inc, dir_inc);
|
||||
return sign | (h + (uint)inc);
|
||||
}
|
||||
float mad_nf_f16(float a, float b, float c, uint rm) { precise float p = a * b; float p16 = f16tof32(f32_to_f16_rm(p, rm)); precise float r = p16 + c; return r; }
|
||||
float med3f(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); }
|
||||
uint med3u(uint a, uint b, uint c) { return max(min(a, b), min(max(a, b), c)); }
|
||||
int med3i(int a, int b, int c) { return max(min(a, b), min(max(a, b), c)); }
|
||||
|
||||
@@ -1297,6 +1297,7 @@ pub fn translate(code: &[u8], opt: &Options) -> Result<Translation, String> {
|
||||
}
|
||||
}
|
||||
let mut em = emit::Em::new(opt.clone());
|
||||
em.configure_round_modes(&insts, branches == 0);
|
||||
em.static_ss = constant_samplers(&insts, &leaders);
|
||||
em.max_vgpr = insts
|
||||
.iter()
|
||||
|
||||
@@ -5048,3 +5048,316 @@ fn float_compares_random(wave64: bool) {
|
||||
}
|
||||
assert_eq!(total_bad, 0);
|
||||
}
|
||||
|
||||
fn hwreg_imm(id: u64, offset: u64, width: u64) -> u64 {
|
||||
id | (offset << 6) | ((width - 1) << 11)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_mode_classification_and_lowering() {
|
||||
let opt = Options { wave64: false, hw_wave: 32, ..Options::default() };
|
||||
|
||||
// Default RNE (0x0): exact NOOP even when f16/f64 ops are present.
|
||||
let def = prog(&[
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0x0)], None),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V)], None),
|
||||
("V_ADD_F64", "ENC_VOP3", &[("VDST", 2), ("SRC0", V + 4), ("SRC1", V + 6)], None),
|
||||
]);
|
||||
let t = translate(&def, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
assert!(!t.hlsl.contains("xcl_fp16_rm"));
|
||||
assert_eq!(t.ignored.get("s_round_mode"), Some(&1));
|
||||
|
||||
// Retail graphics pattern (`s_round_mode 0xC`: f32=RNE, f16/f64=RTZ) with no f16/f64 ops:
|
||||
// exact NOOP, zero HLSL overhead.
|
||||
let f32_only = prog(&[
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0xC)], None),
|
||||
("V_ADD_F32", "ENC_VOP2", &[("VDST", 1), ("SRC0", V), ("VSRC1", 2)], None),
|
||||
]);
|
||||
let t = translate(&f32_only, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
assert!(!t.hlsl.contains("xcl_fp16_rm"));
|
||||
assert_eq!(t.ignored.get("s_round_mode"), Some(&1));
|
||||
|
||||
// `s_round_mode 0xC` with f16 ops: emits `xcl_fp16_rm` and routes f16 rounding through `f32_to_f16_rm`.
|
||||
let f16_rtz = prog(&[
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0xC)], None),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V)], None),
|
||||
]);
|
||||
let t = translate(&f16_rtz, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
assert!(t.hlsl.contains("static uint xcl_fp16_rm = 0u;"));
|
||||
assert!(t.hlsl.contains("xcl_fp16_rm = 3u;"));
|
||||
assert!(t.hlsl.contains("f32_to_f16_rm("));
|
||||
|
||||
// Non-default f64 round mode: rejected instead of silently running RNE.
|
||||
let f64_rtz = prog(&[
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0xC)], None),
|
||||
("V_ADD_F64", "ENC_VOP3", &[("VDST", 2), ("SRC0", V + 4), ("SRC1", V + 6)], None),
|
||||
]);
|
||||
let t = translate(&f64_rtz, &opt).unwrap();
|
||||
assert!(!t.complete());
|
||||
assert!(t.unsupported_why.contains_key("s_round_mode: non-default f64 round mode"), "{:?}", t.unsupported_why);
|
||||
|
||||
// Non-default f32 round mode: rejected.
|
||||
for sp in [1u64, 2, 3] {
|
||||
let f32_rm = prog(&[
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", sp)], None),
|
||||
("V_ADD_F32", "ENC_VOP2", &[("VDST", 1), ("SRC0", V), ("VSRC1", 2)], None),
|
||||
]);
|
||||
let t = translate(&f32_rm, &opt).unwrap();
|
||||
assert!(!t.complete(), "sp={sp}");
|
||||
assert!(t.unsupported_why.contains_key("s_round_mode: non-default f32 round mode"), "{:?}", t.unsupported_why);
|
||||
}
|
||||
|
||||
// `s_setreg_imm32_b32`: full MODE[3:0] write, partial bit writes, non-MODE & denorm-bit NOOPs, rejections.
|
||||
let setreg_partial = prog(&[
|
||||
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 1))], Some(1)),
|
||||
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 3, 1))], Some(1)),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V)], None),
|
||||
]);
|
||||
let t = translate(&setreg_partial, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
assert!(t.hlsl.contains("xcl_fp16_rm = (xcl_fp16_rm & 2u) | 1u;"), "{}", t.hlsl);
|
||||
assert!(t.hlsl.contains("xcl_fp16_rm = (xcl_fp16_rm & 1u) | 2u;"), "{}", t.hlsl);
|
||||
|
||||
let setreg_f32_bad = prog(&[
|
||||
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 0, 4))], Some(0x1)),
|
||||
]);
|
||||
let t = translate(&setreg_f32_bad, &opt).unwrap();
|
||||
assert!(!t.complete());
|
||||
assert!(t.unsupported_why.contains_key("s_setreg_imm32_b32: non-default f32 round mode"), "{:?}", t.unsupported_why);
|
||||
|
||||
let setreg_f64_bad = prog(&[
|
||||
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 2))], Some(0x3)),
|
||||
("V_MUL_F64", "ENC_VOP3", &[("VDST", 2), ("SRC0", V + 4), ("SRC1", V + 6)], None),
|
||||
]);
|
||||
let t = translate(&setreg_f64_bad, &opt).unwrap();
|
||||
assert!(!t.complete());
|
||||
assert!(t.unsupported_why.contains_key("s_setreg_imm32_b32: non-default f64 round mode"), "{:?}", t.unsupported_why);
|
||||
|
||||
let setreg_noop = prog(&[
|
||||
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(20, 0, 32))], Some(0x1234_5678)), // flat scratch
|
||||
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 4, 4))], Some(0xF)), // MODE denorm bits
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V)], None),
|
||||
]);
|
||||
let t = translate(&setreg_noop, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
assert!(!t.hlsl.contains("xcl_fp16_rm"));
|
||||
assert_eq!(t.ignored.get("s_setreg_imm32_b32"), Some(&2));
|
||||
|
||||
// `s_setreg_b32`: dynamic SGPR write to MODE[3:2] with f16 ops succeeds; f32/f64 rejected.
|
||||
let setreg_dyn = prog(&[
|
||||
("S_SETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 2)), ("SDST", 0)], None),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V)], None),
|
||||
]);
|
||||
let t = translate(&setreg_dyn, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
assert!(t.hlsl.contains("xcl_fp16_rm = (s0 & 3u);"), "{}", t.hlsl);
|
||||
|
||||
let setreg_dyn_f32 = prog(&[
|
||||
("S_SETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 0, 2)), ("SDST", 0)], None),
|
||||
]);
|
||||
let t = translate(&setreg_dyn_f32, &opt).unwrap();
|
||||
assert!(!t.complete());
|
||||
assert!(t.unsupported_why.contains_key("s_setreg_b32: dynamic f32 round mode"), "{:?}", t.unsupported_why);
|
||||
|
||||
let setreg_dyn_f64 = prog(&[
|
||||
("S_SETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 2)), ("SDST", 0)], None),
|
||||
("V_ADD_F64", "ENC_VOP3", &[("VDST", 2), ("SRC0", V + 4), ("SRC1", V + 6)], None),
|
||||
]);
|
||||
let t = translate(&setreg_dyn_f64, &opt).unwrap();
|
||||
assert!(!t.complete());
|
||||
assert!(t.unsupported_why.contains_key("s_setreg_b32: dynamic f64 round mode"), "{:?}", t.unsupported_why);
|
||||
|
||||
// Straight-line pre-scan stops at `s_endpgm`: unreachable trailing f64 op does not break `s_round_mode 0xC`.
|
||||
let mut trailing = prog(&[("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0xC)], None)]);
|
||||
trailing.extend(encode("V_ADD_F64", "ENC_VOP3", &[("VDST", 2), ("SRC0", V + 4), ("SRC1", V + 6)], None).unwrap());
|
||||
let t = translate(&trailing, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gpu_nonfused_mad_and_mac_prevent_fma_contraction() {
|
||||
if !gpu_ready_wave32() {
|
||||
return;
|
||||
}
|
||||
// Two golden vectors where fused FMA and non-fused multiply-then-add disagree:
|
||||
// lane 0 (overflow): a = f32::MAX, b = 1 + 2^-23, c = -f32::MAX
|
||||
// non-fused -> +inf (0x7F800000), fused -> 0x73FFFFFF
|
||||
// lane 1 (cancellation): a = 1 + 2^-23, b = 1 + 2^-23, c = -(1 + 2^-22)
|
||||
// non-fused -> +0.0 (0x00000000), fused -> 2^-46 (0x28800000)
|
||||
let code = prog(&[
|
||||
("V_MAC_F32", "ENC_VOP2", &[("VDST", 10), ("SRC0", V + 0), ("VSRC1", 1)], None),
|
||||
("V_MAC_F32", "ENC_VOP3", &[("VDST", 11), ("SRC0", V + 0), ("SRC1", V + 1)], None),
|
||||
("V_MAD_F32", "ENC_VOP3", &[("VDST", 12), ("SRC0", V + 0), ("SRC1", V + 1), ("SRC2", V + 2)], None),
|
||||
("V_MADMK_F32", "ENC_VOP2", &[("VDST", 13), ("SRC0", V + 0), ("VSRC1", 2)], Some(0x3F80_0001)),
|
||||
("V_MADAK_F32", "ENC_VOP2", &[("VDST", 14), ("SRC0", V + 0), ("VSRC1", 1)], Some(0xFF7F_FFFF)),
|
||||
("V_MADAK_F32", "ENC_VOP2", &[("VDST", 15), ("SRC0", V + 0), ("VSRC1", 1)], Some(0xBF80_0002)),
|
||||
// Fused control on the exact same inputs proves the backend has hardware FMA and `precise` mattered.
|
||||
("V_FMA_F32", "ENC_VOP3", &[("VDST", 16), ("SRC0", V + 0), ("SRC1", V + 1), ("SRC2", V + 2)], None),
|
||||
]);
|
||||
let opt = Options { wave64: false, hw_wave: 32, ..Options::default() };
|
||||
let t = translate(&code, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap();
|
||||
|
||||
let mut st = KernelState::default();
|
||||
st.sgpr[127] = 0;
|
||||
let vectors = [
|
||||
(0x7F7F_FFFFu32, 0x3F80_0001u32, 0xFF7F_FFFFu32), // overflow
|
||||
(0x3F80_0001u32, 0x3F80_0001u32, 0xBF80_0002u32), // cancellation
|
||||
];
|
||||
for (l, &(a, b, c)) in vectors.iter().enumerate() {
|
||||
st.vgpr[l][0] = a;
|
||||
st.vgpr[l][1] = b;
|
||||
st.vgpr[l][2] = c;
|
||||
st.vgpr[l][10] = c;
|
||||
st.vgpr[l][11] = c;
|
||||
}
|
||||
let r = runner::run(&dxil, &st, &[]).unwrap();
|
||||
|
||||
let v0 = &r.vgpr[0];
|
||||
assert_eq!(v0[10], 0x7F80_0000, "v_mac_f32 (VOP2) overflow must not fuse");
|
||||
assert_eq!(v0[11], 0x7F80_0000, "v_mac_f32 (VOP3) overflow must not fuse");
|
||||
assert_eq!(v0[12], 0x7F80_0000, "v_mad_f32 overflow must not fuse");
|
||||
assert_eq!(v0[13], 0x7F80_0000, "v_madmk_f32 overflow must not fuse");
|
||||
assert_eq!(v0[14], 0x7F80_0000, "v_madak_f32 overflow must not fuse");
|
||||
assert_eq!(v0[16], 0x73FF_FFFF, "v_fma_f32 overflow control should stay fused");
|
||||
|
||||
let v1 = &r.vgpr[1];
|
||||
assert_eq!(v1[10], 0x0000_0000, "v_mac_f32 (VOP2) cancellation must not fuse");
|
||||
assert_eq!(v1[11], 0x0000_0000, "v_mac_f32 (VOP3) cancellation must not fuse");
|
||||
assert_eq!(v1[12], 0x0000_0000, "v_mad_f32 cancellation must not fuse");
|
||||
assert_eq!(v1[13], 0x0000_0000, "v_madmk_f32 cancellation must not fuse");
|
||||
assert_eq!(v1[15], 0x0000_0000, "v_madak_f32 cancellation must not fuse");
|
||||
assert_eq!(v1[16], 0x2880_0000, "v_fma_f32 cancellation control should stay fused");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gpu_s_round_mode_controls_f16_rounding_across_modes() {
|
||||
if !gpu_ready_wave32() {
|
||||
return;
|
||||
}
|
||||
let code = prog(&[
|
||||
// Default RNE (0) -> v10 (v_cvt_f16_f32), v20 (v_mul_f16), v21 (v_pk_mul_f16), v22 (v_fma_mixlo_f16)
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 10), ("SRC0", V + 0)], None),
|
||||
("V_MUL_F16", "ENC_VOP2", &[("VDST", 20), ("SRC0", V + 1), ("VSRC1", 2)], None),
|
||||
("V_PK_MUL_F16", "ENC_VOP3P", &[("VDST", 21), ("SRC0", V + 1), ("SRC1", V + 2), ("OP_SEL_HI", 3)], None),
|
||||
("V_FMA_MIXLO_F16", "ENC_VOP3P", &[("VDST", 22), ("SRC0", V + 0), ("SRC1", 242), ("SRC2", 128)], None),
|
||||
// Switch to RTZ (3) via S_ROUND_MODE 0xC -> v11, v23, v24, v25
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0xC)], None),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 11), ("SRC0", V + 0)], None),
|
||||
("V_MUL_F16", "ENC_VOP2", &[("VDST", 23), ("SRC0", V + 1), ("VSRC1", 2)], None),
|
||||
("V_PK_MUL_F16", "ENC_VOP3P", &[("VDST", 24), ("SRC0", V + 1), ("SRC1", V + 2), ("OP_SEL_HI", 3)], None),
|
||||
("V_FMA_MIXLO_F16", "ENC_VOP3P", &[("VDST", 25), ("SRC0", V + 0), ("SRC1", 242), ("SRC2", 128)], None),
|
||||
// Switch to +inf (1) via S_ROUND_MODE 0x4 -> v12
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0x4)], None),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 12), ("SRC0", V + 0)], None),
|
||||
// Switch to -inf (2) via S_ROUND_MODE 0x8 -> v13
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0x8)], None),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 13), ("SRC0", V + 0)], None),
|
||||
// Switch back to RNE (0) via S_ROUND_MODE 0x0 -> v14
|
||||
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0x0)], None),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 14), ("SRC0", V + 0)], None),
|
||||
// Switch to RTZ (3) via S_SETREG_IMM32_B32, then clear bit 0 -> -inf (2) -> v15
|
||||
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 2))], Some(3)),
|
||||
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 1))], Some(0)),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 15), ("SRC0", V + 0)], None),
|
||||
// Switch dynamically via S_SETREG_B32 from s0 (= 1 -> +inf) -> v16
|
||||
("S_SETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 2)), ("SDST", 0)], None),
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 16), ("SRC0", V + 0)], None),
|
||||
]);
|
||||
let opt = Options { wave64: false, hw_wave: 32, ..Options::default() };
|
||||
let t = translate(&code, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap();
|
||||
|
||||
// (f32 bits, RNE, RTZ, +inf, -inf)
|
||||
let cases: &[(u32, u16, u16, u16, u16)] = &[
|
||||
(0x3F80_0800, 0x3C00, 0x3C00, 0x3C01, 0x3C00), // +(1 + 2^-12)
|
||||
(0xBF80_0800, 0xBC00, 0xBC00, 0xBC00, 0xBC01), // -(1 + 2^-12)
|
||||
(0x3F80_1000, 0x3C00, 0x3C00, 0x3C01, 0x3C00), // +(1 + 2^-11): tie to even (0x3C00)
|
||||
(0x3F80_3000, 0x3C02, 0x3C01, 0x3C02, 0x3C01), // +(1 + 3*2^-11): tie to odd -> rounds up to 0x3C02
|
||||
(0xBF80_3000, 0xBC02, 0xBC01, 0xBC01, 0xBC02), // -(1 + 3*2^-11)
|
||||
(0x477F_E040, 0x7BFF, 0x7BFF, 0x7C00, 0x7BFF), // +65504.5
|
||||
(0xC77F_E040, 0xFBFF, 0xFBFF, 0xFBFF, 0xFC00), // -65504.5
|
||||
(0x477F_F000, 0x7C00, 0x7BFF, 0x7C00, 0x7BFF), // +65520.0 (tie to +inf)
|
||||
(0xC77F_F000, 0xFC00, 0xFBFF, 0xFBFF, 0xFC00), // -65520.0 (tie to -inf)
|
||||
(0x4788_B800, 0x7C00, 0x7BFF, 0x7C00, 0x7BFF), // +70000.0 (> 65536 overflow)
|
||||
(0xC788_B800, 0xFC00, 0xFBFF, 0xFBFF, 0xFC00), // -70000.0
|
||||
(0x33C0_0000, 0x0002, 0x0001, 0x0002, 0x0001), // +1.5 * 2^-24 (subnormal tie to even)
|
||||
(0x387F_FFFF, 0x0400, 0x03FF, 0x0400, 0x03FF), // subnormal-to-normal carry
|
||||
(0x3300_0000, 0x0000, 0x0000, 0x0001, 0x0000), // +2^-25 (exact tie to 0)
|
||||
(0x3300_0001, 0x0001, 0x0000, 0x0001, 0x0000), // +(2^-25 + eps)
|
||||
(0x8D80_0000, 0x8000, 0x8000, 0x8000, 0x8001), // tiny negative underflow
|
||||
(0x0000_0000, 0x0000, 0x0000, 0x0000, 0x0000), // +0.0
|
||||
(0x8000_0000, 0x8000, 0x8000, 0x8000, 0x8000), // -0.0
|
||||
(0x7F80_0000, 0x7C00, 0x7C00, 0x7C00, 0x7C00), // +inf
|
||||
(0xFF80_0000, 0xFC00, 0xFC00, 0xFC00, 0xFC00), // -inf
|
||||
];
|
||||
|
||||
let mut st = KernelState::default();
|
||||
st.sgpr[0] = 1; // +inf for S_SETREG_B32
|
||||
st.sgpr[127] = 0;
|
||||
for (l, &(bits, ..)) in cases.iter().enumerate() {
|
||||
st.vgpr[l][0] = bits;
|
||||
// Pack two f16 overflow pairs: 65504 * (1 + 2^-10) = 65567.96875 -> +inf (RNE) vs 65504 (RTZ)
|
||||
// High half: -65504 * (1 + 2^-10) -> -inf (RNE) vs -65504 (RTZ)
|
||||
st.vgpr[l][1] = 0xFBFF_7BFF;
|
||||
st.vgpr[l][2] = 0x3C01_3C01;
|
||||
}
|
||||
// Lane 20: quiet NaN preservation check
|
||||
st.vgpr[20][0] = 0x7FC0_2000;
|
||||
|
||||
let r = runner::run(&dxil, &st, &[]).unwrap();
|
||||
for (l, &(bits, rne, rtz, rtp, rtn)) in cases.iter().enumerate() {
|
||||
let v = &r.vgpr[l];
|
||||
assert_eq!(v[10] as u16, rne, "lane {l} ({bits:#010x}) RNE");
|
||||
assert_eq!(v[11] as u16, rtz, "lane {l} ({bits:#010x}) RTZ");
|
||||
assert_eq!(v[12] as u16, rtp, "lane {l} ({bits:#010x}) +inf");
|
||||
assert_eq!(v[13] as u16, rtn, "lane {l} ({bits:#010x}) -inf");
|
||||
assert_eq!(v[14] as u16, rne, "lane {l} ({bits:#010x}) reset to RNE");
|
||||
assert_eq!(v[15] as u16, rtn, "lane {l} ({bits:#010x}) setreg_imm32 partial -> -inf");
|
||||
assert_eq!(v[16] as u16, rtp, "lane {l} ({bits:#010x}) setreg_b32 dynamic -> +inf");
|
||||
assert_eq!(v[22] as u16, rne, "lane {l} ({bits:#010x}) v_fma_mixlo_f16 RNE");
|
||||
assert_eq!(v[25] as u16, rtz, "lane {l} ({bits:#010x}) v_fma_mixlo_f16 RTZ");
|
||||
}
|
||||
// Verify V_MUL_F16 & V_PK_MUL_F16 honor RNE vs RTZ
|
||||
assert_eq!(r.vgpr[0][20] as u16, 0x7C00, "v_mul_f16 overflow RNE");
|
||||
assert_eq!(r.vgpr[0][23] as u16, 0x7BFF, "v_mul_f16 overflow RTZ");
|
||||
assert_eq!(r.vgpr[0][21], 0xFC00_7C00, "v_pk_mul_f16 overflow RNE");
|
||||
assert_eq!(r.vgpr[0][24], 0xFBFF_7BFF, "v_pk_mul_f16 overflow RTZ");
|
||||
// Verify NaN stays NaN across all 4 rounding modes
|
||||
for reg in [10, 11, 12, 13] {
|
||||
let h = r.vgpr[20][reg] as u16;
|
||||
assert!((h & 0x7C00) == 0x7C00 && (h & 0x03FF) != 0, "reg v{reg} NaN corrupted: {h:#06x}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gpu_mad_nf_f16_rounds_intermediate_in_binary16() {
|
||||
if !gpu_ready_wave32() {
|
||||
return;
|
||||
}
|
||||
// Compile a wave32 kernel containing the recompiler PRELUDE and exercise `mad_nf_f16` directly.
|
||||
let base = prog(&[("V_MOV_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", V + 0)], None)]);
|
||||
let opt = Options { wave64: false, hw_wave: 32, ..Options::default() };
|
||||
let mut hlsl = translate(&base, &opt).unwrap().hlsl;
|
||||
let probe = "\
|
||||
if (lane_id == 0u) v0 = f32_to_f16_rm(mad_nf_f16(f16tof32(0x7BFFu), f16tof32(0x3C01u), f16tof32(0xFBFFu), 0u), 0u);\n\
|
||||
if (lane_id == 1u) v0 = f32_to_f16_rm(mad_nf_f16(f16tof32(0x7BFFu), f16tof32(0x3C01u), f16tof32(0xFBFFu), 3u), 3u);\n\
|
||||
if (lane_id == 2u) v0 = f32_to_f16_rm(mad_nf_f16(f16tof32(0x3C01u), f16tof32(0x3C01u), f16tof32(0xBC02u), 0u), 0u);\n";
|
||||
let marker = " v0 = select((((exec_lo >> lane_id) & 1u) != 0u), (v0), v0);\n";
|
||||
assert!(hlsl.contains(marker), "unexpected kernel body:\n{hlsl}");
|
||||
hlsl = hlsl.replace(marker, probe);
|
||||
|
||||
let dxil = super::dxc::compile(&hlsl, "main", "cs_6_6", &[]).unwrap();
|
||||
let mut st = KernelState::default();
|
||||
st.sgpr[127] = 0;
|
||||
let r = runner::run(&dxil, &st, &[]).unwrap();
|
||||
|
||||
assert_eq!(r.vgpr[0][0], 0x7C00, "mad_nf_f16 RNE overflow: 65504*(1+2^-10)-65504 -> +inf");
|
||||
assert_eq!(r.vgpr[1][0], 0x0000, "mad_nf_f16 RTZ clamp: 65504*(1+2^-10)->65504, -65504 -> 0");
|
||||
assert_eq!(r.vgpr[2][0], 0x0000, "mad_nf_f16 cancellation: (1+2^-10)^2-(1+2^-9) -> 0 (not 2^-20)");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user