Skip to content

Recompiler: keep v_fma_f64 precise so a driver cannot split it - #2119

Open
pablogonz12 wants to merge 1 commit into
xcl-dev:mainfrom
pablogonz12:fix/v-fma-f64-precise
Open

pablogonz12 wants to merge 1 commit into
xcl-dev:mainfrom
pablogonz12:fix/v-fma-f64-precise

Conversation

@pablogonz12

@pablogonz12 pablogonz12 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

The HLSL path emitted v_fma_f64 as a plain fma(). DXC then left the dx.op.tertiary Fma call without !dx.precise, vkd3d-proton gave RADV an OpExtInst Fma without NoContraction, and RADV split that fma when the shader already computed the same a * b: it merged the two products and rounded twice. v_fma_f64(x, x, -(x * x)) returned 0 instead of the rounding error of the product (2^-56 + 2^-104, 0x3C70000000200000), so any f64 fma whose product is also written elsewhere in the shader loses its fused result on RADV.

v_fma_f64 now goes through fma_d(), whose result is precise. Related: #472 is the f32 form of an unfused fma (mad() on RDNA1); this is the f64 form, which needs only that the host passes precise on.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: v_fma_f64 emits fma_d(...); fma_d is defined in the prelude beside mad_nf and computes the fma in a precise double.
  • crates/xcl/src/gpu/recomp/tests.rs: gpu_v_fma_f64_stays_fused_beside_the_same_product compiles with the runtime's own DXC arguments (no -Gis), runs p = x * x and fma(x, x, -p) on the GPU and checks p and the exact residual.

Evidence

  • DXIL on 7b5f620 (disassembled with dxil::validate::disassemble, there is no dxc.exe on this machine): call double @dx.op.tertiary.f64(i32 47, …), !dx.precise !12 ; Fma(a,b,c).
  • SPIR-V dumped with VKD3D_SHADER_DUMP_PATH on the same commit: %81 = OpFMul %double decorated NoContraction, the Fma decorated NoContraction as well. Before the change the Fma had no decoration.
  • 2×2, same prefix, one test: the base exe (main) fails with both vkd3d-proton builds; the head exe fails on the d3d12core.dll our test prefix has (dxil-spirv 7c048638 is in the tree, but that DLL was built before fd92a550 "Handle precise for full FMA opcode") and passes with vkd3d-proton master 30b0eb9 (dxil-spirv 34f0ff8) and with our patched v3.1 rebuilt today (10c5f7c4). The fix therefore takes effect with any vkd3d-proton whose dxil-spirv contains fd92a550; on an older one the host drops precise for Fma and nothing in XCL can keep the fma fused on that path.

Tested

  • AMD Radeon RX 9070 XT (RADV GFX1201), Mesa 26.3.1-arch3.5, AMD Ryzen 9 9900X, Wine GE-Proton11-14, vkd3d-proton v3.1 + the XCL patch, DXC 1.9.2609.5, rustc 1.100.0, cargo xwin x86_64-pc-windows-msvc, -C target-feature=+crt-static, release.
  • xcl lib suite, --test-threads=4, skipping integer_ops_match_cpu, uaddc_and_usubb_match_cpu, jumps_to_outer_loop_heads_are_structured (they kill the lib test binary on RADV; main tip: jumps_to_outer_loop_heads_are_structured kills the whole lib test binary on Wine + RDNA3 (exit status 5), bisected to b8b1f49 #1246, separate):
    • test prefix as it is: main 6987e49 1085 passed, 10 failed, 26 ignored; this head 7b5f620 1088 passed, 8 failed, 26 ignored (head's extra failure is the new test, which stays red on that stale DLL; the 3 title-textures / d32s8 failures of main's run passed on head)
    • vkd3d-proton v3.1 + XCL patch rebuilt today (10c5f7c4): main 6987e49 1088 passed, 7 failed, 26 ignored; this head 7b5f620 1089 passed, 7 failed, 26 ignored — the same 7 on both (fuzz_validate_and_gpu_2, directed_reference_basics, lower_dxil f64 and f32, gpu_nonfused_mad_and_mac_prevent_fma_contraction, depth-UAV placement, xma2_source_voice), and the new test passes.
  • Integration tests (e2e, pls_quota, predication_e2e and the other 14 test binaries), one thread: the same 3 failures on main and on head (e2e console-screen cloak, pls_quota Wine ERROR_FILE_NOT_FOUND, predication_e2e copies not predicated by vkd3d-proton).
  • SKIPPED on this adapter: the four f16 / f32-conversion programs of lower_dxil (unsupported V_MUL_F16 semantics, gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426), unchanged by this PR.

Regression run

Both blocks are the output of each commit's own unmodified scripts/smoke.ps1, written to a file by PowerShell and pasted as is. It ran in Windows PowerShell 7.7.0 (pwsh.exe) under Wine (GE-Proton11-14, Linux), not on native Windows, with MinGit 2.56.1 for git; xcl.exe is an MSVC release build (cargo xwin, static CRT), passed with -Xcl. Host workarounds outside the script: a pseudo-terminal, DOTNET_SYSTEM_GLOBALIZATION_USENLS=1, and importing PowerShell's Utility and Management modules before calling it. main is this PR's base commit. These replace the earlier blocks from a bash port. Frame counts at this speed are small; I read them only as no crash or device removal. Each commit was run once. Note that this title can end in a device removal on this host even on main (seen in repeat runs of another base commit, see #464).

main:

xcl-smoke v1
commit: 6987e496c35653f9ccc9dbdc1f1d342734306811
gpu: AMD Radeon RX 9070 XT (RADV GFX1201)
title: bdd254b6
seconds: 60
exit: 0x0
frames shown: 22 (PresentX 31)
presents per 2 s (last 3): 1 1 1
device removals: 0
crashes: 0
peak commit: 0 MB

This PR's head:

xcl-smoke v1
commit: 7b5f6202288bc3fd40416c57b39e9b4c04fe7f14
gpu: AMD Radeon RX 9070 XT (RADV GFX1201)
title: bdd254b6
seconds: 60
exit: 0x0
frames shown: 21 (PresentX 29)
presents per 2 s (last 3): 1 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

Not tested

  • Native Windows, any AMD or NVIDIA D3D12 driver: the maintainer's run is the first one there. On Windows the driver gets NoContraction on the Fma, so the fma should stay fused, but I did not measure it.
  • Any retail code path that uses v_fma_f64 with the same product written elsewhere; no title was checked for a change in output.
  • XCL_DIRECT_DXIL: its Fma still carries no dx.precise and its f64_programs_on_the_gpu fails the same way; that is a separate change in gpu/dxil.
  • macOS / D3DMetal.

AI use

  • Claude Code, Claude Sonnet 5.5: found the instruction, wrote fma_d and the test, dumped the DXIL and SPIR-V, ran the suite, the integration tests and the two smoke runs, and wrote this description. I checked every line of the diff and every number above.

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)
@pablogonz12
pablogonz12 marked this pull request as ready for review November 25, 2026 05:07
@futurepro62

futurepro62 commented Nov 25, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 7b5f620 (on its base 6987e49).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu --skip gpu::recomp::lower_dxil::tests::mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu --skip gpu::dxil::fuzz_tests: 1143 passed, 0 failed, 44 ignored. SKIPPED (no wave64): a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu, a_per_lane_2d_or_3d_group_runs_on_the_gpu, coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu, cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.
  • The new tests ran and passed: gpu_v_fma_f64_stays_fused_beside_the_same_product.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 1154 passed, 0 failed, 46 ignored. SKIPPED: f16_programs_on_the_gpu (f16 arithmetic, wave32, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 arithmetic, wave64, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave32, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave64, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave32, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave64, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • --skip leaves out tests that fail on main 6020a33 too, not because of this PR. On NVIDIA: heap_window_out_of_memory_falls_back_to_the_switch_form (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651), the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests, const_and_null_mask_operands_agree_with_hlsl_on_the_gpu and mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu (follow-up on #1651) fail because runner::skip_test turns their skips into failures under XCL_REQUIRE_GPU=mesh, and gpu::dxil::fuzz_tests seeds 70, 545, 1019 and 1507 fail CreateComputePipelineState with E_OUTOFMEMORY on this card (drivers 616.64 and 621.40). On AMD: fuzz_validate_and_gpu_1 fails on seed 540 (res[833] 0x7 vs CPU 0x6). The AMD run does not set XCL_REQUIRE_GPU.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@DarkPanda69

Copy link
Copy Markdown

Tested: gpu_v_fma_f64_stays_fused_beside_the_same_product passes on an AMD D3D12 driver and on an RTX 3060.

Head 7b5f620 applied on main 6020a33, cargo test -p xcl --lib -- gpu:: --test-threads=4 with XCL_ADAPTER set to each card.
Not tested: retail runs, and nothing beyond what the test itself asserts about the fused result.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

@JoaoPauluu

Copy link
Copy Markdown

A run with vkd3d-proton as the host D3D12. The DXIL goes through dxil-spirv and then the NVIDIA Vulkan driver, not the native-Windows path: this PR's new test passes, none skipped.

Tested

Not tested

  • the whole suite on this head, a retail title, native Windows, AMD.

AI use

  • Claude Code (Claude Opus 5.5) built the commit, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants