Shader: lower f32 fused multiply-adds through f64 #2594
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2594
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2594"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@JAEdotUK on GitHub · 2026-11-25 16:24 UTC · original
JAEdotUK:fix/fma-f64→main· +251 −34 · 17 files · 7 commits · openf32 fused multiply-add lowered through f64
Some hosts don't fuse FMad, so f32 fma rounds twice and differs from AMD hardware. Where the host doesn't fuse, this lowers f32 fma through f64 instead.
How it works
The product is exact in f64 and the sum is computed with TwoSum. The result is rounded to odd (Boldo & Melquiond) before FpTrunc, so the final f32 rounding is correct.
Signed zero is handled. The sign comes from the f64 high word, because one NVIDIA driver returned −0 from select(cond, 0x8000_0000, 0).
Emulation is on only if fuses_fmad() is false and the device reports double-precision and Int64 shader ops. Otherwise a warning is logged and the stock lowering is used.
fuses_fmad() probes the host's own FMad, with emulation off.
The FMA setting is hashed into each shader-cache entry's key. The cache folder name no longer includes it, because the host may not have decided yet.
Tests
emulated_multiply_adds_match_the_hardware forces emulation on the hardware adapter and compares results. It passes with 0 mismatches on the RX 9070 XT and on WARP.
defect_multiply_adds_fused_only_where_the_host_fuses stays ignored. It checks the stock lowering with emulation off, and its ignore reason points to the test above.
victory-shader 303 passed, victory-gpu 231 passed, clippy clean.
Credit: the capability gating and host.rs changes are andrewpozdnakov7-jpg's. The sign-bit fix came from fyernald's review.
Open items
I know of no real hardware that needs the f64 path. It has only been exercised on WARP.
I haven't measured speed.
lower.rs has about 12 bytes of headroom under the 512 KiB limit. That may need a maintainer decision.
AI assistance disclosure: The code, tests and debugging in this PR were written by Claude Sonnet 5.5 (Anthropic) via Claude Code, working with me. I'm new to Rust and the shader code, so I haven't independently verified the maths. The tests pass on my RX 9070 XT and on WARP, clippy is clean, and the commits carry a
Co-Authored-Bytrailer. I'd appreciate reviewers' scrutiny of the rounding logic in particular.@Mericas77 on GitHub · 2026-11-25 17:33 UTC · original
Tested on NVIDIA: RTX 4070, driver 621.01, Windows 11, rustc 1.100.0, dxil.dll 1.9.2602.17. Head
1537259againstmain67e8ae1(the PR's base), sequential runs.This regresses three tests on the NVIDIA hardware adapter. WARP is fine.
cargo test -p victory-shader --lib --locked(with--skip heap_direct_texture_reads_mark_their_heap_index, which crashes the process onmainhere too, see #2571):main: 266 passed, 3 failed, 3 ignoredThe 3 failures on
mainare the known NVIDIA ones (twostride_testsheap-buffer reads,gathers_at_a_level_match_the_hardware) and are unchanged. The 3 new ones all fail atsemantics_tests.rs:660, onhardwareonly:arithmetic_matches_the_hardware:v_fmac_f32 v5, v1, v2: 14338 inputs, model mismatches [hardware 45, warp 0], e.g.in [00800000, 00800000, 00000000, …] want [00000000] got [80000000]mixed_precision_matches_the_hardware:v_fma_mix_f32: 3071 inputs, [hardware 25, warp 0], same patternf16_arithmetic_rounds_once:v_fma_mix_f32 v1, sel_lo(v2), v3: 543 inputs, [hardware 9, warp 0], e.g.in [00800000, 00000001, 00000000, …] want 0 got 8000000029 probes are hit (every
v_fma_f32/v_fmac_f32form, plusv_fma_mix_f32/mixlo/mixhi). All 240 printed mismatches are the same thing: a positive result that underflows belowFLT_MIN, with a zero or denormal addend, should flush to +0, and through the f64 path the 4070 returns -0 (156 ×00000000→80000000, 84 × the same in f16 halves). Onmainthe hardware passes all these probes.My guess, not verified: the driver doesn't keep the sign of the zero in
select(is_neg, -0.0, +0.0)(or in the f64 compares around it), treating ±0 as interchangeable. Building the signed zero on the bits, e.g.bitcast(r_f32) & 0x8000_0000or the sign bit of the f64 high word asi32, would not depend on how the driver treats float zeros.Other results:
hosts_that_do_not_fuse_are_named: passes onmainand here. The 4070 printshardware adapter fuses FMad: trueon both.defect_multiply_adds_fused_only_where_the_host_fuses -- --ignored: onmainit fails only on WARP (hardware 0 errors on all 45 probes, e.g.v_fma_f32: 102 inputs, [hardware 0, warp 102]). Here it passes, 45/45 with hardware 0 and warp 0. Its inputs don't include the underflow-to-zero case above.FMad, on this card the PR changes results only in the -0 case, and it moves every f32 FMA onto f64. GeForce cards run f64 at a small fraction of the f32 rate, so a timing check on a real NVIDIA card would be worth doing before this lands. I haven't measured it.No game data; the title was not run.
AI-assisted.
@JAEdotUK on GitHub · 2026-11-25 17:55 UTC · original · edited 2026-11-26 11:39 UTC
Thanks fyernald and Mericas77 for testing on the RTX 3080 and RTX 4070. This comment describes the first round of fixes. Parts of it are superseded by later commits, notably the signed-zero fix: it now takes the sign from the f64 high word instead of selecting between 0x8000_0000 and 0, because fyernald showed the select still returned -0 on NVIDIA. See the current description and the latest comment for the final state.
Disclosure: this PR and these fixes were AI-assisted (Claude Sonnet 5.5, Anthropic).
@welisonzika14-jpg on GitHub · 2026-11-25 19:27 UTC · original
Tested #2594 (head
30dbd9b) on an NVIDIA RTX 5070 desktop (Blackwell), driver 621.24, Windows 11, rustc 1.98.1.Command:
cargo test -p victory-shader --lib --locked --no-fail-fast, run onmain13f91ca+ #2596 (577839a), then on the same plus #2594.arithmetic_matches_the_hardware,mixed_precision_matches_the_hardwareandf16_arithmetic_rounds_oncepass on the hardware adapter with the PR, so the -0 vs +0 failures reported on the 3080 and 4070 for head1537259do not show up on30dbd9bhere.hosts_that_do_not_fuse_are_named --nocaptureprintshardware adapter fuses FMad: true. So on the 5070 the f64 path is off (emulate_fma: false), and the three tests above run on native FMad. Only WARP uses the f64 path in the tests, so this run does not show the new signed-zero fix on NVIDIA hardware.defect_multiply_adds_fused_only_where_the_host_fuses -- --ignoredfails only on WARP (hardware 0 mismatches in all 90 probe lines), the same pattern as on main.I did not measure speed (it is off on this card).
No game data; the title was not run.
Written with AI help (Claude/Kiro); the numbers are from runs on my machine.
@Marvisak on GitHub · 2026-11-25 22:22 UTC · original
Automated triage for the maintainers.
Needs work. The idea fits the public RDNA ISA:
v_fma_f32is a single-rounding fused operation. Emulating it inf64with round-to-odd (TwoSum, thenFpTrunc) is a known correct technique. Gating it on!fuses_fmad()and adding the flag to the shader cache key (shader_cache::key) keeps fusing GPUs on nativeFMad. It merges cleanly and CI (check) passes. Before it can land:fma32setsflags::DOUBLES(SFI00x01). A host that does not reportDoublePrecisionFloatShaderOpsinD3D12_FEATURE_DATA_D3D12_OPTIONSwill refuse every pipeline that contains an f32 FMA.host.rsturns emulation on wheneverfuses_fmad()is false, without checking that feature. Please enable it only when doubles are supported, and otherwise keep today's path and warning.semantics_tests.rs,fmachanged frommul_addin f64 followed by rounding to f32 into the same TwoSum/round-to-odd sequence that the emitter now produces. Round-to-odd is the more correct reference, but please state that this changes the expected values for all FMA probes, and show that the hardware adapter still matches it. The RTX 5070 run says it does, but on that card the f64 path is off.defect_multiply_adds_fused_only_where_the_host_fuses -- --ignoredstill fails on WARP with this head, while the description says it is verified. Please reconcile the two: either that test runs WARP withoutemulate_fma, in which case say so and update it, or the emulation misses cases.fuses_fmad→set_emulate_fmaruns only increate(), anduse_shader_cachehashesbuild(emulate_fma())into the cache folder name. If the cache is opened before the host exists, the folder is keyedfalse. Entries stay correct because each lookup passesbuild(emulate_fma()), but please confirm the order, or drop the flag from the folder name.Generated by Claude Code
@andrewpozdnakov7-jpg on GitHub · 2026-11-26 04:02 UTC · original
I tested b5be7cc, which retains
30dbd9bas the first parent of its merge with63bcca4. The plain merge reports five missingemulate_fmainitializers in the tests added on main. 5ff23f0 supplies those settings so the test tree builds.The emulation bit now requires both
DoublePrecisionFloatShaderOpsandInt64ShaderOps.fma32uses the latter for its round-to-odd adjustment of the intermediate f64 bits. With the same synthetic selection test on non-fusing Agility WARP, masking the double feature changed the result from emulation enabled to disabled. Masking int64 also disabled it on the fixed tree. The real adapter reported both features and enabled emulation. Double operations Int64 operationsThe reported ignored-test failure is reproducible. Its
lower_programwrapper passesemulate_fma = false. I ran the same 45 programs and 634 synthetic inputs with the stock selector, then withtruein the temporary harness. All 634 selected cases disagreed in each stock slot. All 634 matched in each emulated slot, with DXIL validation active. Both slots were forced to Agility WARP 1.0.22. The selection probes simulated the missing capabilities by masking one device feature at a time.The cache-key test, full WARP GPU suite (246 passes, one ignored), both strict workspace Clippy configurations and asset policy passed.
Please keep the original Git author on your commits and retain my Git author if you cherry-pick the follow-ups. If you fold my changes into another commit, please include:
Co-authored-by: andrewpozdnakov7-jpg [email protected]
AI-assisted with Codex. Synthetic inputs.
@fyernald on GitHub · 2026-11-25 17:00 UTC · original · requested changes
I ran this on an RTX 3080 (617.42), Windows 10 (19045),
main67e8ae1+ #486 + #2596. Without #2596 the suite currently crashes on NVIDIA (#2571).cargo test -p victory-shader --lib --locked -- --test-threads=1:The new failures are
semantics_tests::arithmetic_matches_the_hardware,mixed_precision_matches_the_hardwareandf16_arithmetic_rounds_once. All are model mismatches onv_fma_f32,v_fmac_f32(45 of 14,338 inputs) andv_fma_mix*(9 to 25 per form). The hardware and WARP agree with each other ("hardware/warp differ on 0"), so the lowering produces this, not the driver. Examples from the log:So a tiny positive product flushed to zero comes out as −0 where RDNA2 gives +0.
Two more things I'd want settled before this goes in:
FpTruncrounds again to f32. That double rounding breaks ties wrongly. For example,fma(0x3F800001, 0x3F7FFFFF, 0x28000001): the exact sum is1 + 2^-24 + 2^-70, which a fused FMA (andv_fma_f32) rounds up to0x3F800001. In f64 the2^-70is lost, the sum lands exactly on the f32 halfway point, and ties-to-even gives0x3F800000. Getting this right in f64 needs round-to-odd on the intermediate (Boldo and Melquiond).FMadfor everyv_fma_f32,v_fmac_f32andv_fma_mix*, which are among the most common instructions in the title's shaders, with f64 extend, multiply, add, compares and truncate. On consumer NVIDIA cards f64 throughput is about 1/64 of f32, so this probably needs measuring on a real frame before it becomes the default. It could perhaps sit behind the existingfuses_fmadcheck, used only on hosts that don't fuse.Related:
fuses_fmadruns the loweredv_fma_f32, so with this change it always reports that the host fuses. Its doc comment and the startup warning invictory-gpu/src/host.rswould no longer mean anything.@fyernald on GitHub · 2026-11-26 02:08 UTC · original · requested changes
Retested
30dbd9bon the RTX 3080 (617.42) / Windows 10,main63bcca4+ #486 + #2596.1. It no longer builds on current
main. The merge is clean, butvictory-shader's tests fail withE0063: missing field emulate_fma in initializer of Partsin four places that landed onmainafter this branch:crosslane_tests.rs,memory_tests.rs,synthetic_tests.rsand a test inlower.rs. CI was green on the older base.2. The signed-zero fix doesn't hold on NVIDIA hardware. This card fuses, so with the gating the f64 path is off here, as on the 5070. To answer the triage's question, I forced it on the hardware adapter: every
arithmetic_probes()andmix_probes()probe, plus the four edge cases fromhosts_that_do_not_fuse_are_named, lowered withlower_program_with(.., true)and withfalse, both run on the RTX 3080 and compared with the newfmamodel:FMadfma(0x00800000, 0x00800000, +0)→0x80000000. It shows up acrossv_fma_f32,v_fmac_f32and thev_fma_mix*forms (an f160x8000there).I bisected it in
fma32:truncalone gives the right sign for these inputs;zero_bitsto0fixes them;fcmp oltfor an integericmp sltonr_bitsdoesn't help.So
select(cond, 0x8000_0000, 0)comes out as0x8000_0000even whencondis false. The driver seems to treat the two zeros as one value even through the i32 bitcast. WARP doesn't do this, which is why the WARP test passes.Taking the sign without a select fixes all of it here:
With that: 0 emulated-only mismatches over the same 302,362 inputs on the RTX 3080, and
hosts_that_do_not_fuse_are_namedstill passes on WARP.Since the f64 path only runs on hosts that don't fuse, a check like the one above on the hardware adapter, with emulation forced on, would be worth keeping in the suite. Otherwise no test runs this code on real hardware.
@JAEdotUK on GitHub · 2026-11-26 09:50 UTC · original
Thank you all. I've pushed
447e536on top of1cb7063.fyernald: the signed-zero fix is in, taken from the f64 high word. The cache folder no longer depends on the FMA flag, because it's already in each entry's key. I added a test that forces emulation on the hardware adapter. It passes on my RX 9070 XT and on WARP.
Mericas77, welisonzika14-jpg: the round-to-odd step and the capability gating are in.
andrewpozdnakov7-jpg: your commits are included. Thank you.
Marvisak: thanks for the triage. I rewrote the PR description and removed the claim that the defect test was verified. It's now ignored, with the reason stated.
Still open, honestly: I know of no hardware that needs the f64 path, I haven't measured speed, and lower.rs has 12 bytes of headroom. fyernald, could you re-review when you have time?
Disclosure: the code and debugging were produced by Claude Sonnet 5.5 (Anthropic) through Claude Code. I'm a beginner, so please review the maths carefully.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.