Shaders: pack half FMA NaNs directly #2943

Open
github-bot wants to merge 3 commits from github-bot/gh-2943 into main AGit
Owner

@⁠Ingenioeren on GitHub · 2026-11-26 01:01 UTC · original
Ingenioeren:perf/f16-fma-nan-payload → main · +87 −9 · 3 files · 3 commits · open

The software nearest-even lowering of v_fma_f16 used the full f32-to-f16 rounding sequence as its NaN fallback. The fallback is selected only for NaNs, so it can pack the quiet bit and upper payload bits directly. The fused operation reuses the sign it already computed and shares the NaN packing code with the general conversion.

Synthetic compute shader Before After Change
Emitted DXIL instructions for one v_fma_f16 with runtime operands 118 78 −40 (−33.9%)

The count includes the shader's input and output instructions and uses the bitcode writer's live instruction layout. The regression fails at 118 with the old fallback and passes with a budget of 78 after the change. The test-only Module::written_instructions() counter is the same helper as in open PR #2893, and whichever PR lands second will drop its copy on rebase. The open PR search for "fma_f16" "payload" found no duplicate of this change.

Validation

  • cargo test --locked --offline -p victory-shader on 63bcca4 gave 300 passed, 2 failed, 3 ignored. After the change it gave 302 passed, 2 failed, 3 ignored. Both runs had the same local dxil.dll pipeline-state validation failures in rectangles_get_their_fourth_corner and elements_sharing_a_row_keep_their_order.
  • cargo test --locked --offline -p victory-shader f16_fma_packs_nan_without_a_second_rounding_path failed at 118 instructions with the old fallback. The regression passed in the full run with the new lowering.
  • cargo test --locked --offline -p victory-shader v_fma_f16 -- --nocapture passed both GPU tests. The existing test covered rounding and preservation of the other half on the Radeon RX 6750 XT. The NaN test asserts the exact 32-bit results, in case order, 0x00007E35, 0x0000FE42, 0x0000FE00, 0x0000FE35, and 0xABCD7E35. The origin/main lowering and this branch produced identical results on the Radeon RX 6750 XT and WARP, including the negative signaling NaN and the preserved upper destination half.
  • cargo test --locked --offline -p victory-shader --test resource_tracking gave 3 passed and 1 ignored. cargo test --locked --offline -p victory-shader --doc completed with zero doc tests.
  • cargo clippy --locked --offline -p victory-shader --all-targets passed.
  • cargo run --locked --offline -p xtask -- policy --staged passed for all three files in the original commit and for the staged test update in this revision.

No game run or game shaders were used. GPU execution time was not measured.

AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren.

**@⁠Ingenioeren** on GitHub · 2026-11-26 01:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/2943) `Ingenioeren:perf/f16-fma-nan-payload` → `main` · +87 −9 · 3 files · 3 commits · open The software nearest-even lowering of `v_fma_f16` used the full f32-to-f16 rounding sequence as its NaN fallback. The fallback is selected only for NaNs, so it can pack the quiet bit and upper payload bits directly. The fused operation reuses the sign it already computed and shares the NaN packing code with the general conversion. | Synthetic compute shader | Before | After | Change | | --- | ---: | ---: | ---: | | Emitted DXIL instructions for one `v_fma_f16` with runtime operands | 118 | 78 | −40 (−33.9%) | The count includes the shader's input and output instructions and uses the bitcode writer's live instruction layout. The regression fails at 118 with the old fallback and passes with a budget of 78 after the change. The test-only `Module::written_instructions()` counter is the same helper as in open PR #2893, and whichever PR lands second will drop its copy on rebase. The open PR search for `"fma_f16" "payload"` found no duplicate of this change. ## Validation - `cargo test --locked --offline -p victory-shader` on `63bcca4` gave 300 passed, 2 failed, 3 ignored. After the change it gave 302 passed, 2 failed, 3 ignored. Both runs had the same local `dxil.dll` pipeline-state validation failures in `rectangles_get_their_fourth_corner` and `elements_sharing_a_row_keep_their_order`. - `cargo test --locked --offline -p victory-shader f16_fma_packs_nan_without_a_second_rounding_path` failed at 118 instructions with the old fallback. The regression passed in the full run with the new lowering. - `cargo test --locked --offline -p victory-shader v_fma_f16 -- --nocapture` passed both GPU tests. The existing test covered rounding and preservation of the other half on the Radeon RX 6750 XT. The NaN test asserts the exact 32-bit results, in case order, `0x00007E35`, `0x0000FE42`, `0x0000FE00`, `0x0000FE35`, and `0xABCD7E35`. The `origin/main` lowering and this branch produced identical results on the Radeon RX 6750 XT and WARP, including the negative signaling NaN and the preserved upper destination half. - `cargo test --locked --offline -p victory-shader --test resource_tracking` gave 3 passed and 1 ignored. `cargo test --locked --offline -p victory-shader --doc` completed with zero doc tests. - `cargo clippy --locked --offline -p victory-shader --all-targets` passed. - `cargo run --locked --offline -p xtask -- policy --staged` passed for all three files in the original commit and for the staged test update in this revision. No game run or game shaders were used. GPU execution time was not measured. AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:18:08 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:21 UTC · original

Code review and RTX 5090 + WARP + DXIL verification at head 83ff480c0448 merged cleanly onto main (63bcca43c6a0; emit/lower.rs = 521,381 B, 2,907 B under the 512 KiB cap):

  • Lowering review (lower.rs:2290-2306, 2380-2384):
    • Extracting narrow16_nan(magnitude) and using it directly in fma_f16 avoids emitting a second full narrow16 normalization/rounding/overflow path while preserving exact behavioral parity with main. emit::lower::tests (34/34, including f16_fma_packs_nan_without_a_second_rounding_path) and emit::semantics_tests (34/34 on both RTX 5090 and WARP) pass, as do clippy and cargo xtask policy (191 files ok).
  • RTX 5090 hardware test failure (emit::valu_tests::v_fma_f16_quiets_nans, valu_tests.rs:444-463):
    • On NVIDIA RTX 5090 (hardware case 0: [0x7c35, 0x3c00, 0, 0], sNaN * 1.0 + 0.0), the new test panics at valu_tests.rs:458:9:
      actual = 0x00007fff, expected = 0x00007e35
    • Because fma_f16 widens inputs via legacyF16ToF32 and runs f32 FMul/FAdd before extracting the payload, NVIDIA's D3D12/SASS pipeline canonicalizes f32 NaN results to 0x7FFF_FFFF, which narrow16_nan packs to 0x7FFF rather than preserving the input payload 0x35.
    • This is a hardware vendor difference rather than a regression in narrow16_nan; relaxing the GPU assertion in v_fma_f16_quiets_nans to check for a quiet f16 NaN (e.g. (actual & 0x7E00) == 0x7E00 and upper half preserved) — or checking exact payload only where deterministic — will make the test portable across NVIDIA hardware too.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2943#issuecomment-7179699563) Code review and RTX 5090 + WARP + DXIL verification at head `83ff480c0448` merged cleanly onto `main` (`63bcca43c6a0`; `emit/lower.rs` = `521,381` B, `2,907` B under the 512 KiB cap): - **Lowering review (`lower.rs:2290-2306, 2380-2384`)**: - Extracting `narrow16_nan(magnitude)` and using it directly in `fma_f16` avoids emitting a second full `narrow16` normalization/rounding/overflow path while preserving exact behavioral parity with `main`. `emit::lower::tests` (34/34, including `f16_fma_packs_nan_without_a_second_rounding_path`) and `emit::semantics_tests` (34/34 on both RTX 5090 and WARP) pass, as do `clippy` and `cargo xtask policy` (`191 files ok`). - **RTX 5090 hardware test failure (`emit::valu_tests::v_fma_f16_quiets_nans`, `valu_tests.rs:444-463`)**: - On NVIDIA RTX 5090 (`hardware` case 0: `[0x7c35, 0x3c00, 0, 0]`, `sNaN * 1.0 + 0.0`), the new test panics at `valu_tests.rs:458:9`: `actual = 0x00007fff`, `expected = 0x00007e35` - Because `fma_f16` widens inputs via `legacyF16ToF32` and runs f32 `FMul`/`FAdd` before extracting the payload, NVIDIA's D3D12/SASS pipeline canonicalizes f32 NaN results to `0x7FFF_FFFF`, which `narrow16_nan` packs to `0x7FFF` rather than preserving the input payload `0x35`. - This is a hardware vendor difference rather than a regression in `narrow16_nan`; relaxing the GPU assertion in `v_fma_f16_quiets_nans` to check for a quiet f16 NaN (e.g. `(actual & 0x7E00) == 0x7E00` and upper half preserved) — or checking exact payload only where deterministic — will make the test portable across NVIDIA hardware too.
Author
Owner

@⁠Ingenioeren on GitHub · 2026-11-26 02:41 UTC · original

Thanks, @⁠fuzeofficlal and @⁠Dukestar69, for testing this on the RTX 5090 and reporting the NaN mismatch.

In b628ad9bce082e08185676040e4506f773fbdceb, the f16 FMA NaN test still checks the exact result on WARP. The hardware check accepts any quiet f16 NaN (any sign, any payload) and checks the upper 16 bits exactly.

The NaN test passed on the AMD Radeon RX 6750 XT and WARP. All 36 targeted shader tests passed, along with Clippy and the asset policy check. We did not test on NVIDIA.

**@⁠Ingenioeren** on GitHub · 2026-11-26 02:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/2943#issuecomment-7181027391) Thanks, @⁠fuzeofficlal and @⁠Dukestar69, for testing this on the RTX 5090 and reporting the NaN mismatch. In `b628ad9bce082e08185676040e4506f773fbdceb`, the f16 FMA NaN test still checks the exact result on WARP. The hardware check accepts any quiet f16 NaN (any sign, any payload) and checks the upper 16 bits exactly. The NaN test passed on the AMD Radeon RX 6750 XT and WARP. All 36 targeted shader tests passed, along with Clippy and the asset policy check. We did not test on NVIDIA.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 04:28 UTC · original

Automated triage for the maintainers.

Merge candidate. The full narrow16(sum) fallback in fma_f16 (emit/lower.rs) becomes a narrow16_nan(magnitude) helper that narrow16 also uses. The output is unchanged: the old fallback only mattered for NaN sums, and narrow16 on a NaN gives sign | 0x7E00 | payload>>13, exactly the new form; the DXIL drops from 118 to 78 instructions. The exact 0xFE00 for infinity times zero in v_fma_f16_quiets_nans is WARP's default NaN, not confirmed console behaviour, so it should not be read as a spec. The test-only Module::written_instructions duplicates the one in #2893; whichever lands second should drop its copy.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 04:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2943#issuecomment-7182626352) <!-- victory-triage v1 head=b628ad9bce082e08185676040e4506f773fbdceb verdict=merge-candidate at=2026-11-26T04:28:22Z --> Automated triage for the maintainers. **Merge candidate.** The full `narrow16(sum)` fallback in `fma_f16` (`emit/lower.rs`) becomes a `narrow16_nan(magnitude)` helper that `narrow16` also uses. The output is unchanged: the old fallback only mattered for NaN sums, and `narrow16` on a NaN gives `sign | 0x7E00 | payload>>13`, exactly the new form; the DXIL drops from 118 to 78 instructions. The exact 0xFE00 for infinity times zero in `v_fma_f16_quiets_nans` is WARP's default NaN, not confirmed console behaviour, so it should not be read as a spec. The test-only `Module::written_instructions` duplicates the one in #2893; whichever lands second should drop its copy. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
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/2943/head:github-bot/gh-2943'
git switch 'github-bot/gh-2943'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2943'
git switch 'github-bot/gh-2943'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2943'
git switch 'github-bot/gh-2943'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2943'
git switch 'main'
git merge --squash 'github-bot/gh-2943'
git switch 'main'
git merge --ff-only 'github-bot/gh-2943'
git switch 'main'
git merge 'github-bot/gh-2943'
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!2943
No description provided.