Skip to content
Open
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
5 changes: 4 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3554,7 +3554,7 @@ impl Em {
let e = match name {
"v_add_f64" => format!("{} + {}", f64v(0), f64v(1)),
"v_mul_f64" => format!("{} * {}", f64v(0), f64v(1)),
"v_fma_f64" => format!("fma({}, {}, {})", f64v(0), f64v(1), f64v(2)),
"v_fma_f64" => format!("fma_d({}, {}, {})", f64v(0), f64v(1), f64v(2)),
"v_min_f64" => format!("min({}, {})", f64v(0), f64v(1)),
"v_max_f64" => format!("max({}, {})", f64v(0), f64v(1)),
"v_rcp_f64" => format!("rcp_d({})", f64v(0)),
Expand Down Expand Up @@ -6159,6 +6159,9 @@ 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; }
// v_fma_f64 rounds once. Without `precise` a driver may split the fma and merge its a * b with
// the same product elsewhere in the shader (RADV through vkd3d-proton).
double fma_d(double a, double b, double c) { precise double r = fma(a, b, 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);
Expand Down
32 changes: 32 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7755,6 +7755,38 @@ fn gpu_nonfused_mad_and_mac_prevent_fma_contraction() {
assert_eq!(v1[16], 0x2880_0000, "v_fma_f32 cancellation control should stay fused");
}

#[test]
fn gpu_v_fma_f64_stays_fused_beside_the_same_product() {
if !gpu_ready() {
return;
}
// v_fma_f64(x, x, -p) with p = x * x already rounded is the rounding error of p. A driver
// that splits the fma can merge its x * x with p and returns 0. Compiled with the
// runtime's DXC arguments (no -Gis).
let code = prog(&[
("V_MUL_F64", "ENC_VOP3", &[("VDST", 2), ("SRC0", V), ("SRC1", V)], None),
("V_FMA_F64", "ENC_VOP3", &[("VDST", 4), ("SRC0", V), ("SRC1", V), ("SRC2", V + 2), ("NEG", 4)], 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 x = 1.0 + 1.0 / 32.0 + 2f64.powi(-52);
let mut st = KernelState::default();
st.sgpr[127] = 0;
st.vgpr[0][0] = x.to_bits() as u32;
st.vgpr[0][1] = (x.to_bits() >> 32) as u32;
let r = runner::run(&dxil, &st, &[]).unwrap();

let f = |i: usize| r.vgpr[0][i] as u64 | (r.vgpr[0][i + 1] as u64) << 32;
let p = x * x;
assert_eq!(f(2), p.to_bits(), "v_mul_f64");
let want = x.mul_add(x, -p);
assert_ne!(want, 0.0);
assert_eq!(f(4), want.to_bits(), "v_fma_f64: {:e} against {want:e}", f64::from_bits(f(4)));
}

#[test]
fn gpu_f16_defaults_to_round_to_nearest_even() {
if !gpu_ready() {
Expand Down
Loading