Shaders: pack half FMA NaNs directly #2941

Open
Ingenioeren wants to merge 3 commits from upstream-pr/2943 into main
Ingenioeren commented 2026-11-26 01:01:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Ingenioeren on 2026-11-26T01:01:25Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5501981558 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2943). Originally posted by [Ingenioeren](https://github.com/Ingenioeren) on 2026-11-26T01:01:25Z. --- 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.
Dukestar69 commented 2026-11-26 02:06:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T02:06:54Z.

Review: COMMENTED


Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib emit::valu_tests::v_fma_f16: 1 passed, 1 failed — the new test v_fma_f16_quiets_nans fails on NVIDIA RTX 5090 hardware:
    assertion `left == right` failed: hardware case 0 with [00007c35, 00003c00, 00000000, 00000000]: got 0x00007fff, expected 0x00007e35
    
    (v_fma_f16_rounds_once_and_preserves_other_half passed.)
  • cargo test -p victory-shader --lib emit::lower::tests::f16_fma_packs_nan_without_a_second_rounding_path: 1 passed, 0 failed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

The lowering refactor (narrow16_nan replacing the narrow16 fallback in fma16) is clean and preserves narrow16's exact behavior, but the new test asserts an exact NaN payload (0x7e35), whereas NVIDIA hardware canonicalizes f32 FMA NaN outputs to 0x7fff_ffff (0x7fff after narrow16_nan), causing v_fma_f16_quiets_nans to fail on NVIDIA GPUs. Testing for a quiet NaN ((low & 0x7E00) == 0x7E00) and preserving the upper half (while optionally asserting exact payload only on WARP, or matching how semantics_tests treats NaNs) would keep the test portable across NVIDIA hardware.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6067601491 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2943#pullrequestreview-6067601491). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T02:06:54Z. **Review:** COMMENTED --- Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, `dxil.dll` validator active): - `cargo test -p victory-shader --lib emit::valu_tests::v_fma_f16`: **1 passed, 1 failed** — the new test `v_fma_f16_quiets_nans` **fails on NVIDIA RTX 5090 hardware**: ``` assertion `left == right` failed: hardware case 0 with [00007c35, 00003c00, 00000000, 00000000]: got 0x00007fff, expected 0x00007e35 ``` (`v_fma_f16_rounds_once_and_preserves_other_half` passed.) - `cargo test -p victory-shader --lib emit::lower::tests::f16_fma_packs_nan_without_a_second_rounding_path`: **1 passed, 0 failed**. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. The lowering refactor (`narrow16_nan` replacing the `narrow16` fallback in `fma16`) is clean and preserves `narrow16`'s exact behavior, but the new test asserts an exact NaN payload (`0x7e35`), whereas NVIDIA hardware canonicalizes `f32` FMA NaN outputs to `0x7fff_ffff` (`0x7fff` after `narrow16_nan`), causing `v_fma_f16_quiets_nans` to fail on NVIDIA GPUs. Testing for a quiet NaN (`(low & 0x7E00) == 0x7E00`) and preserving the upper half (while optionally asserting exact payload only on WARP, or matching how `semantics_tests` treats NaNs) would keep the test portable across NVIDIA hardware. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal commented 2026-11-26 02:07:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:21:51Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7179699563 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2943#issuecomment-7179699563). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T01:21:51Z. --- 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.
Ingenioeren commented 2026-11-26 03:05:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Ingenioeren on 2026-11-26T02:41:10Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7181027391 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2943#issuecomment-7181027391). Originally posted by [Ingenioeren](https://github.com/Ingenioeren) on 2026-11-26T02:41:10Z. --- 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.
Marvisak commented 2026-11-26 05:04:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:28:27Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182626352 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2943#issuecomment-7182626352). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:28:27Z. --- <!-- 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)_
fuzeofficlal commented 2026-11-27 02:16:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T02:16:31Z.

Review: APPROVED


Reviewed b628ad9bce082e08185676040e4506f773fbdceb.

LGTM — extracting Lower::narrow16_nan avoids emitting a second full narrow16 conversion tree on fma_f16's NaN path while producing bit-identical 0x7E00 | ((magnitude >> 13) & 0x3FF) quieted payloads with the preserved sign bit (sign >> 16) and untouched upper half.

Verified locally on RTX 5090 + WARP:

  • cargo xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets / --all-features (-D warnings), and zero new rustfmt diffs (lower.rs stays under 512 KiB at 521,381 bytes).
  • emit::valu_tests:: passed (36 passed, 0 failed), and both emit::lower::tests::f16_fma_packs_nan_without_a_second_rounding_path and emit::valu_tests::v_fma_f16_quiets_nans pass under VICTORY_WARP=1.
<!-- greyfinch-sync:Marvisak/VIctory:review:6082622049 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2943#pullrequestreview-6082622049). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-27T02:16:31Z. **Review:** APPROVED --- Reviewed `b628ad9bce082e08185676040e4506f773fbdceb`. **LGTM** — extracting `Lower::narrow16_nan` avoids emitting a second full `narrow16` conversion tree on `fma_f16`'s NaN path while producing bit-identical `0x7E00 | ((magnitude >> 13) & 0x3FF)` quieted payloads with the preserved sign bit `(sign >> 16)` and untouched upper half. Verified locally on RTX 5090 + WARP: - `cargo xtask policy` (`191 files ok`), `cargo clippy -p victory-shader --all-targets` / `--all-features` (`-D warnings`), and zero new `rustfmt` diffs (`lower.rs` stays under 512 KiB at 521,381 bytes). - `emit::valu_tests::` passed (36 passed, 0 failed), and both `emit::lower::tests::f16_fma_packs_nan_without_a_second_rounding_path` and `emit::valu_tests::v_fma_f16_quiets_nans` pass under `VICTORY_WARP=1`.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2943:upstream-pr/2943'
git switch 'upstream-pr/2943'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2943'
git switch 'upstream-pr/2943'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2943'
git switch 'upstream-pr/2943'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2943'
git switch 'main'
git merge --squash 'upstream-pr/2943'
git switch 'main'
git merge --ff-only 'upstream-pr/2943'
git switch 'main'
git merge 'upstream-pr/2943'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2941
No description provided.