Shader: lower f32 fused multiply-adds through f64 #2594

Open
github-bot wants to merge 7 commits from github-bot/gh-2594 into main AGit
Owner

@⁠JAEdotUK on GitHub · 2026-11-25 16:24 UTC · original
JAEdotUK:fix/fma-f64 → main · +251 −34 · 17 files · 7 commits · open

f32 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-By trailer. I'd appreciate reviewers' scrutiny of the rounding logic in particular.

**@⁠JAEdotUK** on GitHub · 2026-11-25 16:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594) `JAEdotUK:fix/fma-f64` → `main` · +251 −34 · 17 files · 7 commits · open f32 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-By` trailer. I'd appreciate reviewers' scrutiny of the rounding logic in particular.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:09:58 +00:00
Author
Owner

@⁠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 1537259 against main 67e8ae1 (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 on main here too, see #2571):

  • main: 266 passed, 3 failed, 3 ignored
  • this PR: 263 passed, 6 failed, 3 ignored

The 3 failures on main are the known NVIDIA ones (two stride_tests heap-buffer reads, gathers_at_a_level_match_the_hardware) and are unchanged. The 3 new ones all fail at semantics_tests.rs:660, on hardware only:

  • 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 pattern
  • f16_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 80000000

29 probes are hit (every v_fma_f32/v_fmac_f32 form, plus v_fma_mix_f32/mixlo/mixhi). All 240 printed mismatches are the same thing: a positive result that underflows below FLT_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). On main the 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_0000 or the sign bit of the f64 high word as i32, would not depend on how the driver treats float zeros.

Other results:

  • hosts_that_do_not_fuse_are_named: passes on main and here. The 4070 prints hardware adapter fuses FMad: true on both.
  • defect_multiply_adds_fused_only_where_the_host_fuses -- --ignored: on main it 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.
  • Since the 4070 already fuses 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.

**@⁠Mericas77** on GitHub · 2026-11-25 17:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594#issuecomment-7169679566) Tested on NVIDIA: RTX 4070, driver 621.01, Windows 11, rustc 1.100.0, dxil.dll 1.9.2602.17. Head `1537259` against `main` `67e8ae1` (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 on `main` here too, see #2571): - `main`: 266 passed, 3 failed, 3 ignored - this PR: 263 passed, 6 failed, 3 ignored The 3 failures on `main` are the known NVIDIA ones (two `stride_tests` heap-buffer reads, `gathers_at_a_level_match_the_hardware`) and are unchanged. The 3 new ones all fail at `semantics_tests.rs:660`, on `hardware` only: - `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 pattern - `f16_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 80000000` 29 probes are hit (every `v_fma_f32`/`v_fmac_f32` form, plus `v_fma_mix_f32`/`mixlo`/`mixhi`). All 240 printed mismatches are the same thing: a positive result that underflows below `FLT_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). On `main` the 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_0000` or the sign bit of the f64 high word as `i32`, would not depend on how the driver treats float zeros. Other results: - `hosts_that_do_not_fuse_are_named`: passes on `main` and here. The 4070 prints `hardware adapter fuses FMad: true` on both. - `defect_multiply_adds_fused_only_where_the_host_fuses -- --ignored`: on `main` it 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. - Since the 4070 already fuses `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.
Author
Owner

@⁠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).

**@⁠JAEdotUK** on GitHub · 2026-11-25 17:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594#issuecomment-7170198556) · 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).
Author
Owner

@⁠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 on main 13f91ca + #2596 (577839a), then on the same plus #2594.

passed failed
main + #2596 267 3 (the known NVIDIA three from #1744: two stride tests, gathers_at_a_level)
main + #2596 + #2594 267 3 (the same three)

arithmetic_matches_the_hardware, mixed_precision_matches_the_hardware and f16_arithmetic_rounds_once pass on the hardware adapter with the PR, so the -0 vs +0 failures reported on the 3080 and 4070 for head 1537259 do not show up on 30dbd9b here.

hosts_that_do_not_fuse_are_named --nocapture prints hardware 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 -- --ignored fails 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.

**@⁠welisonzika14-jpg** on GitHub · 2026-11-25 19:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594#issuecomment-7172325504) 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 on `main` 13f91ca + #2596 (577839a), then on the same plus #2594. | | passed | failed | |---|---|---| | main + #2596 | 267 | 3 (the known NVIDIA three from #1744: two stride tests, gathers_at_a_level) | | main + #2596 + #2594 | 267 | 3 (the same three) | `arithmetic_matches_the_hardware`, `mixed_precision_matches_the_hardware` and `f16_arithmetic_rounds_once` pass on the hardware adapter with the PR, so the -0 vs +0 failures reported on the 3080 and 4070 for head 1537259 do not show up on 30dbd9b here. `hosts_that_do_not_fuse_are_named --nocapture` prints `hardware 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 -- --ignored` fails 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.
Author
Owner

@⁠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_f32 is a single-rounding fused operation. Emulating it in f64 with round-to-odd (TwoSum, then FpTrunc) 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 native FMad. It merges cleanly and CI (check) passes. Before it can land:

  • Double support is optional. fma32 sets flags::DOUBLES (SFI0 0x01). A host that does not report DoublePrecisionFloatShaderOps in D3D12_FEATURE_DATA_D3D12_OPTIONS will refuse every pipeline that contains an f32 FMA. host.rs turns emulation on whenever fuses_fmad() is false, without checking that feature. Please enable it only when doubles are supported, and otherwise keep today's path and warning.
  • Which hosts this is for. Every NVIDIA report here says the card fuses, so the f64 path runs only on non-fusing hosts, WARP so far. Please name real hardware that does not fuse. Otherwise say plainly that this is for WARP and the test runs, so the maintainer can weigh 70 lines of emitted f64 code per FMA against that.
  • The test model changed with the code. In semantics_tests.rs, fma changed from mul_add in 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.
  • The conflicting WARP result. The RTX 5070 report says defect_multiply_adds_fused_only_where_the_host_fuses -- --ignored still fails on WARP with this head, while the description says it is verified. Please reconcile the two: either that test runs WARP without emulate_fma, in which case say so and update it, or the emulation misses cases.
  • fuses_fmad → set_emulate_fma runs only in create(), and use_shader_cache hashes build(emulate_fma()) into the cache folder name. If the cache is opened before the host exists, the folder is keyed false. Entries stay correct because each lookup passes build(emulate_fma()), but please confirm the order, or drop the flag from the folder name.

Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 22:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594#issuecomment-7176274390) <!-- victory-triage v1 head=30dbd9b9d591bc4b5c34135a476bd8ef97c1d51a verdict=needs-work at=2026-11-25T22:21:54Z --> Automated triage for the maintainers. **Needs work.** The idea fits the public RDNA ISA: `v_fma_f32` is a single-rounding fused operation. Emulating it in `f64` with round-to-odd (TwoSum, then `FpTrunc`) 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 native `FMad`. It merges cleanly and CI (`check`) passes. Before it can land: - **Double support is optional.** `fma32` sets `flags::DOUBLES` (SFI0 `0x01`). A host that does not report `DoublePrecisionFloatShaderOps` in `D3D12_FEATURE_DATA_D3D12_OPTIONS` will refuse every pipeline that contains an f32 FMA. `host.rs` turns emulation on whenever `fuses_fmad()` is false, without checking that feature. Please enable it only when doubles are supported, and otherwise keep today's path and warning. - **Which hosts this is for.** Every NVIDIA report here says the card fuses, so the f64 path runs only on non-fusing hosts, WARP so far. Please name real hardware that does not fuse. Otherwise say plainly that this is for WARP and the test runs, so the maintainer can weigh 70 lines of emitted f64 code per FMA against that. - **The test model changed with the code.** In `semantics_tests.rs`, `fma` changed from `mul_add` in 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. - **The conflicting WARP result.** The RTX 5070 report says `defect_multiply_adds_fused_only_where_the_host_fuses -- --ignored` still fails on WARP with this head, while the description says it is verified. Please reconcile the two: either that test runs WARP without `emulate_fma`, in which case say so and update it, or the emulation misses cases. - `fuses_fmad` → `set_emulate_fma` runs only in `create()`, and `use_shader_cache` hashes `build(emulate_fma())` into the cache folder name. If the cache is opened before the host exists, the folder is keyed `false`. Entries stay correct because each lookup passes `build(emulate_fma())`, but please confirm the order, or drop the flag from the folder name. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 04:02 UTC · original

I tested b5be7cc, which retains 30dbd9b as the first parent of its merge with 63bcca4. The plain merge reports five missing emulate_fma initializers in the tests added on main. 5ff23f0 supplies those settings so the test tree builds.

The emulation bit now requires both DoublePrecisionFloatShaderOps and Int64ShaderOps. fma32 uses 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 operations

The reported ignored-test failure is reproducible. Its lower_program wrapper passes emulate_fma = false. I ran the same 45 programs and 634 synthetic inputs with the stock selector, then with true in 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.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 04:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594#issuecomment-7182241449) I tested [b5be7cc](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/b5be7ccb65110e400c5a0b15df65be354018e6ad), which retains `30dbd9b` as the first parent of its merge with `63bcca4`. The plain merge reports five missing `emulate_fma` initializers in the tests added on main. [5ff23f0](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/5ff23f06d6229c99e8e108e63ba84b382b323283) supplies those settings so the test tree builds. The emulation bit now requires both `DoublePrecisionFloatShaderOps` and `Int64ShaderOps`. `fma32` uses 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](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_feature_data_d3d12_options) [Int64 operations](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_feature_data_d3d12_options1) The reported ignored-test failure is reproducible. Its `lower_program` wrapper passes `emulate_fma = false`. I ran the same 45 programs and 634 synthetic inputs with the stock selector, then with `true` in 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.
Author
Owner

@⁠fyernald on GitHub · 2026-11-25 17:00 UTC · original · requested changes

I ran this on an RTX 3080 (617.42), Windows 10 (19045), main 67e8ae1 + #486 + #2596. Without #2596 the suite currently crashes on NVIDIA (#2571). cargo test -p victory-shader --lib --locked -- --test-threads=1:

passed / failed
base 267 / 3 (the #1835 three)
this PR 264 / 6

The new failures are semantics_tests::arithmetic_matches_the_hardware, mixed_precision_matches_the_hardware and f16_arithmetic_rounds_once. All are model mismatches on v_fma_f32, v_fmac_f32 (45 of 14,338 inputs) and v_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:

v_fma_f32  in [00800000, 00800000, 00000000]  want 00000000  got 80000000
v_fma_f32  in [00800000, 00800000, 80000000]  want 00000000  got 80000000
v_fma_mix_f32 v1, sel_lo(v2), v3  in [00800000, 00000001, 00000000]  want 00000000  got 80000000

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:

  1. It isn't always a correctly rounded FMA either. The product of two f32 values is exact in f64, but the add rounds to f64 and then FpTrunc rounds again to f32. That double rounding breaks ties wrongly. For example, fma(0x3F800001, 0x3F7FFFFF, 0x28000001): the exact sum is 1 + 2^-24 + 2^-70, which a fused FMA (and v_fma_f32) rounds up to 0x3F800001. In f64 the 2^-70 is lost, the sum lands exactly on the f32 halfway point, and ties-to-even gives 0x3F800000. Getting this right in f64 needs round-to-odd on the intermediate (Boldo and Melquiond).
  2. Cost. This replaces the FMad for every v_fma_f32, v_fmac_f32 and v_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 existing fuses_fmad check, used only on hosts that don't fuse.

Related: fuses_fmad runs the lowered v_fma_f32, so with this change it always reports that the host fuses. Its doc comment and the startup warning in victory-gpu/src/host.rs would no longer mean anything.

**@⁠fyernald** on GitHub · 2026-11-25 17:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594#pullrequestreview-6061672945) · **requested changes** I ran this on an **RTX 3080 (617.42), Windows 10 (19045)**, `main` `67e8ae1` + #486 + #2596. Without #2596 the suite currently crashes on NVIDIA (#2571). `cargo test -p victory-shader --lib --locked -- --test-threads=1`: | | passed / failed | |---|---| | base | 267 / 3 (the #1835 three) | | this PR | 264 / **6** | The new failures are `semantics_tests::arithmetic_matches_the_hardware`, `mixed_precision_matches_the_hardware` and `f16_arithmetic_rounds_once`. All are model mismatches on `v_fma_f32`, `v_fmac_f32` (45 of 14,338 inputs) and `v_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: ``` v_fma_f32 in [00800000, 00800000, 00000000] want 00000000 got 80000000 v_fma_f32 in [00800000, 00800000, 80000000] want 00000000 got 80000000 v_fma_mix_f32 v1, sel_lo(v2), v3 in [00800000, 00000001, 00000000] want 00000000 got 80000000 ``` 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: 1. **It isn't always a correctly rounded FMA either.** The product of two f32 values is exact in f64, but the add rounds to f64 and then `FpTrunc` rounds again to f32. That double rounding breaks ties wrongly. For example, `fma(0x3F800001, 0x3F7FFFFF, 0x28000001)`: the exact sum is `1 + 2^-24 + 2^-70`, which a fused FMA (and `v_fma_f32`) rounds up to `0x3F800001`. In f64 the `2^-70` is lost, the sum lands exactly on the f32 halfway point, and ties-to-even gives `0x3F800000`. Getting this right in f64 needs round-to-odd on the intermediate (Boldo and Melquiond). 2. **Cost.** This replaces the `FMad` for every `v_fma_f32`, `v_fmac_f32` and `v_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 existing `fuses_fmad` check, used only on hosts that don't fuse. Related: `fuses_fmad` runs the lowered `v_fma_f32`, so with this change it always reports that the host fuses. Its doc comment and the startup warning in `victory-gpu/src/host.rs` would no longer mean anything.
Author
Owner

@⁠fyernald on GitHub · 2026-11-26 02:08 UTC · original · requested changes

Retested 30dbd9b on the RTX 3080 (617.42) / Windows 10, main 63bcca4 + #486 + #2596.

1. It no longer builds on current main. The merge is clean, but victory-shader's tests fail with E0063: missing field emulate_fma in initializer of Parts in four places that landed on main after this branch: crosslane_tests.rs, memory_tests.rs, synthetic_tests.rs and a test in lower.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() and mix_probes() probe, plus the four edge cases from hosts_that_do_not_fuse_are_named, lowered with lower_program_with(.., true) and with false, both run on the RTX 3080 and compared with the new fma model:

302,362 inputs mismatches
native FMad 4
emulated (this head) 678
emulated, with the change below 4
  • The 4 native mismatches are all inputs the probes already list under "a fused f16 result rounded in f32 first". So the RTX 3080's FMA agrees with the round-to-odd model everywhere else, which answers the triage's question about the test model.
  • The other 674 are all one case: a positive result below the f32 normal range comes back −0, e.g. fma(0x00800000, 0x00800000, +0) → 0x80000000. It shows up across v_fma_f32, v_fmac_f32 and the v_fma_mix* forms (an f16 0x8000 there).

I bisected it in fma32:

  • returning trunc alone gives the right sign for these inputs;
  • forcing zero_bits to 0 fixes them;
  • swapping the fcmp olt for an integer icmp slt on r_bits doesn't help.

So select(cond, 0x8000_0000, 0) comes out as 0x8000_0000 even when cond is 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:

let k32 = self.s.m.i64(32);
let hi = self.s.m.bin(cur, Bin::LShr, r_bits, k32);
let hi = self.s.m.cast(cur, Cast::Trunc, hi, self.s.t.i32);
let zero_bits = self.s.m.bin(cur, Bin::And, hi, nzero_i32);

With that: 0 emulated-only mismatches over the same 302,362 inputs on the RTX 3080, and hosts_that_do_not_fuse_are_named still 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.

**@⁠fyernald** on GitHub · 2026-11-26 02:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594#pullrequestreview-6067612904) · **requested changes** Retested `30dbd9b` on the RTX 3080 (617.42) / Windows 10, `main` `63bcca4` + #486 + #2596. **1. It no longer builds on current `main`.** The merge is clean, but `victory-shader`'s tests fail with `E0063: missing field emulate_fma in initializer of Parts` in four places that landed on `main` after this branch: `crosslane_tests.rs`, `memory_tests.rs`, `synthetic_tests.rs` and a test in `lower.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()` and `mix_probes()` probe, plus the four edge cases from `hosts_that_do_not_fuse_are_named`, lowered with `lower_program_with(.., true)` and with `false`, both run on the RTX 3080 and compared with the new `fma` model: | 302,362 inputs | mismatches | |---|---| | native `FMad` | 4 | | emulated (this head) | 678 | | emulated, with the change below | 4 | - The 4 native mismatches are all inputs the probes already list under "a fused f16 result rounded in f32 first". So the RTX 3080's FMA agrees with the round-to-odd model everywhere else, which answers the triage's question about the test model. - The other 674 are all one case: a positive result below the f32 normal range comes back **−0**, e.g. `fma(0x00800000, 0x00800000, +0)` → `0x80000000`. It shows up across `v_fma_f32`, `v_fmac_f32` and the `v_fma_mix*` forms (an f16 `0x8000` there). I bisected it in `fma32`: - returning `trunc` alone gives the right sign for these inputs; - forcing `zero_bits` to `0` fixes them; - swapping the `fcmp olt` for an integer `icmp slt` on `r_bits` doesn't help. So `select(cond, 0x8000_0000, 0)` comes out as `0x8000_0000` even when `cond` is 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: ```rust let k32 = self.s.m.i64(32); let hi = self.s.m.bin(cur, Bin::LShr, r_bits, k32); let hi = self.s.m.cast(cur, Cast::Trunc, hi, self.s.t.i32); let zero_bits = self.s.m.bin(cur, Bin::And, hi, nzero_i32); ``` With that: 0 emulated-only mismatches over the same 302,362 inputs on the RTX 3080, and `hosts_that_do_not_fuse_are_named` still 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.
Author
Owner

@⁠JAEdotUK on GitHub · 2026-11-26 09:50 UTC · original

Thank you all. I've pushed 447e536 on top of 1cb7063.

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.

**@⁠JAEdotUK** on GitHub · 2026-11-26 09:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2594#issuecomment-7188545358) Thank you all. I've pushed 447e536 on top of 1cb7063. 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.*
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2594/head:github-bot/gh-2594'
git switch 'github-bot/gh-2594'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2594'
git switch 'github-bot/gh-2594'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2594'
git switch 'github-bot/gh-2594'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2594'
git switch 'main'
git merge --squash 'github-bot/gh-2594'
git switch 'main'
git merge --ff-only 'github-bot/gh-2594'
git switch 'main'
git merge 'github-bot/gh-2594'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2594
No description provided.