Shaders: lower v_mad_f16 and v_mac_f16 with unfused binary16 product rounding #1597

Open
geiyiliyabumo wants to merge 1 commit from upstream-pr/1598 into main
geiyiliyabumo commented 2026-11-24 19:41:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T19:41:09Z.


Summary

Separates v_mad_f16 / v_mac_f16 from fused v_fma_f16 / v_fmac_f16 so legacy unfused binary16 multiply-add rounds the intermediate product a * b to binary16 before adding the accumulator (tracking issue #1242, VIC-BT-042).

Previously, Lower::valu16 lowered v_fma_f16 | v_fmac_f16 | v_mad_f16 | v_mac_f16 identically to self.fmad(a, b, acc). Additionally, DXIL legacyF32ToF16 clamps overflowing finite products (|a * b| >= 65520) to ±65504 (0x7BFF) instead of IEEE-754 ±inf (0x7C00).

Changes

  • crates/victory-shader/src/emit/lower.rs:
    • Add a pure 32-bit nearest-even binary16 narrowing helper (no f64 or INT64 DXIL flags) that rounds ties-to-even and produces signed infinity at magnitude >= 65520.
    • Lower v_mad_f16 | v_mac_f16 as round_f16(a * b) + acc while preserving the existing fused v_fma_f16 | v_fmac_f16 path.
  • crates/victory-shader/src/emit/valu_tests.rs: Add GPU-executed regression test v_mad_f16_rounds_the_product_before_adding comparing unfused MAD against encoded v_fma_f16 across 12 cases (product cancellation, overflow threshold 65520, normal/subnormal ties, signed zero, and ordinary arithmetic). Note: GFX10.3 encodes only fused v_fma_f16/v_fmac_f16, so the test uses a #[cfg(test)]-gated mnemonic path to exercise the v_mad_f16 lowering directly on GPU hardware and WARP.

Verification

  • cargo test -p victory-shader --lib valu_tests: 14/14 passed on AMD Radeon RX 9060 XT (RDNA4, SM 6.6) and WARP.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 154 files OK.

AI-assisted; synthetic tests only, no game files.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6589933252 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1598). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T19:41:09Z. --- ## Summary Separates `v_mad_f16` / `v_mac_f16` from fused `v_fma_f16` / `v_fmac_f16` so legacy unfused binary16 multiply-add rounds the intermediate product `a * b` to binary16 before adding the accumulator (tracking issue #1242, `VIC-BT-042`). Previously, `Lower::valu16` lowered `v_fma_f16 | v_fmac_f16 | v_mad_f16 | v_mac_f16` identically to `self.fmad(a, b, acc)`. Additionally, DXIL `legacyF32ToF16` clamps overflowing finite products (`|a * b| >= 65520`) to `±65504` (`0x7BFF`) instead of IEEE-754 `±inf` (`0x7C00`). ### Changes - `crates/victory-shader/src/emit/lower.rs`: - Add a pure 32-bit nearest-even binary16 narrowing helper (no `f64` or `INT64` DXIL flags) that rounds ties-to-even and produces signed infinity at magnitude `>= 65520`. - Lower `v_mad_f16 | v_mac_f16` as `round_f16(a * b) + acc` while preserving the existing fused `v_fma_f16 | v_fmac_f16` path. - `crates/victory-shader/src/emit/valu_tests.rs`: Add GPU-executed regression test `v_mad_f16_rounds_the_product_before_adding` comparing unfused MAD against encoded `v_fma_f16` across 12 cases (product cancellation, overflow threshold `65520`, normal/subnormal ties, signed zero, and ordinary arithmetic). Note: GFX10.3 encodes only fused `v_fma_f16`/`v_fmac_f16`, so the test uses a `#[cfg(test)]`-gated mnemonic path to exercise the `v_mad_f16` lowering directly on GPU hardware and WARP. ### Verification - `cargo test -p victory-shader --lib valu_tests`: **14/14 passed** on **AMD Radeon RX 9060 XT (RDNA4, SM 6.6)** and **WARP**. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 154 files OK. *AI-assisted; synthetic tests only, no game files.*
Marvisak commented 2026-11-25 02:06:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:31:46Z.


Automated triage for the maintainers.

The new unfused lowering for v_mad_f16/v_mac_f16 cannot be reached by a title shader: the GFX10 decoder table in crates/victory-shader/src/gfx10/opcodes.rs has no v_mad_f16 or v_mac_f16 (RDNA2 has only the fused v_fma_f16/v_fmac_f16, as the PR description itself notes). The test therefore works by renaming v_fma_f16 to v_mad_f16 through a #[cfg(test)] flag added to Lower, so it exercises a path that never runs outside the test, and it adds a test-only field to production code. The names in Lower::valu16 are leftovers that could simply be removed instead. If a GFX10.3 encoding that decodes to these names exists, please point to it and this can be reconsidered.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the triage: proposed close label.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149650113 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1598#issuecomment-7149650113). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:31:46Z. --- <!-- victory-triage v1 head=a3c911608bb003aad4077de8dc59e378aa5b4029 verdict=proposed-close at=2026-11-25T02:20:29Z --> Automated triage for the maintainers. The new unfused lowering for `v_mad_f16`/`v_mac_f16` cannot be reached by a title shader: the GFX10 decoder table in `crates/victory-shader/src/gfx10/opcodes.rs` has no `v_mad_f16` or `v_mac_f16` (RDNA2 has only the fused `v_fma_f16`/`v_fmac_f16`, as the PR description itself notes). The test therefore works by renaming `v_fma_f16` to `v_mad_f16` through a `#[cfg(test)]` flag added to `Lower`, so it exercises a path that never runs outside the test, and it adds a test-only field to production code. The names in `Lower::valu16` are leftovers that could simply be removed instead. If a GFX10.3 encoding that decodes to these names exists, please point to it and this can be reconsidered. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the `triage: proposed close` label. --- _Generated by [Claude Code](https://claude.ai/code)_
geiyiliyabumo commented 2026-11-25 03:03:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-25T02:08:31Z.


Rebased cleanly onto \main\ (\ccfb029). \cargo test -p victory-shader --locked mad_f16\ (verified on physical AMD RX 9060 XT + WARP + Microsoft \dxil.dll), \cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings, and \cargo xtask policy\ all pass.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150365819 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1598#issuecomment-7150365819). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-25T02:08:31Z. --- Rebased cleanly onto \main\ (\ccfb029\). \cargo test -p victory-shader --locked mad_f16\ (verified on physical AMD RX 9060 XT + WARP + Microsoft \dxil.dll\), \cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings\, and \cargo xtask policy\ all pass.
catminou4 commented 2026-11-25 04:05:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T03:39:21Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. v_mad_f16/v_mac_f16 unfused binary16 product — overlaps with #1588 and approved #1608 (same f16 mul/add family); whichever lands second must reconcile the rounding path.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051512906 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1598#pullrequestreview-6051512906). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T03:39:21Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 144 passed / 0 failed / 2 ignored. v_mad_f16/v_mac_f16 unfused binary16 product — **overlaps with #1588 and approved #1608** (same f16 mul/add family); whichever lands second must reconcile the rounding path.
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/1598:upstream-pr/1598'
git switch 'upstream-pr/1598'

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/1598'
git switch 'upstream-pr/1598'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1598'
git switch 'upstream-pr/1598'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1598'
git switch 'main'
git merge --squash 'upstream-pr/1598'
git switch 'main'
git merge --ff-only 'upstream-pr/1598'
git switch 'main'
git merge 'upstream-pr/1598'
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!1597
No description provided.