From 7b5f6202288bc3fd40416c57b39e9b4c04fe7f14 Mon Sep 17 00:00:00 2001 From: pablogonz12 Date: Wed, 25 Nov 2026 05:42:17 +0100 Subject: [PATCH] Recompiler: keep v_fma_f64 precise so a driver cannot split it The HLSL path emitted v_fma_f64 as a plain fma(), so DXC left the Fma call without !dx.precise and vkd3d-proton gave the driver an Fma without NoContraction. With the same a * b already computed in the shader, RADV split the fma and merged the two products: v_fma_f64(x, x, -(x * x)) returned 0 instead of the rounding error of the product. Emit it through fma_d(), whose result is precise. vkd3d-proton passes that on for Fma from dxil-spirv fd92a550 ("Handle precise for full FMA opcode"). Assisted-by: Claude Code (Claude Sonnet 5.5) --- crates/xcl/src/gpu/recomp/emit.rs | 5 ++++- crates/xcl/src/gpu/recomp/tests.rs | 32 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043f..8deb9482bc 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -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)), @@ -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); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e648..716f1df980 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -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() {