Shaders: v_mad_f32 and v_mac_f32 multiply apart from their add #1918

Open
kais7654 wants to merge 1 commit from upstream-pr/1920 into main
kais7654 commented 2026-11-25 01:00:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kais7654 on 2026-11-25T01:00:06Z.


Same root cause as #1903 (FMad over an opaque zero), this time on the plain
v_mad_f32/v_mac_f32 paths: the lowering emits the product rounded before
the add, which is what the ISA says, but the driver folds the fmul into the
following fadd as an FMA although nothing carries a fast-math flag. One
rounding where two were due shifts the tail of the product:

  • v_mad_f32: 2 of 2560 inputs mismatch on hardware, 0 on WARP, 0 in the
    model ([3f7fffff, 3f800001, bf800000] straddles 1.0, where double vs
    single rounding is visible);
  • v_mac_f32: 102 of 14440.

The legacy variants already pass, because legacy_mul ends in a select
that cuts the fmul → fadd edge. This replaces the plain product with an
FMad over a zero signed by the operands' own signs (sign(a) ^ sign(b)),
which rounds exactly like a plain IEEE multiply — signed zeros included,
denormal outputs the same way as the fused v_fma_f32 path already
verified — and hides the multiply behind a call the fusion pass sees no
other side of. +19/−2 in emit/lower.rs, no test changes.

With it, arithmetic_matches_the_hardware passes on both backends word for
word (the rest of the suite's failures on this branch are the ones covered
by #1903, #1896 and #1857).

Local: RTX 5070 Laptop GPU (driver 616.87) and WARP, 240 tests pass.

AI-assisted (Mistral Vibe).

<!-- greyfinch-sync:Marvisak/VIctory:issue:5483056030 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1920). Originally posted by [kais7654](https://github.com/kais7654) on 2026-11-25T01:00:06Z. --- Same root cause as #1903 (FMad over an opaque zero), this time on the plain `v_mad_f32`/`v_mac_f32` paths: the lowering emits the product rounded before the add, which is what the ISA says, but the driver folds the `fmul` into the following `fadd` as an FMA although nothing carries a fast-math flag. One rounding where two were due shifts the tail of the product: - `v_mad_f32`: 2 of 2560 inputs mismatch on hardware, 0 on WARP, 0 in the model (`[3f7fffff, 3f800001, bf800000]` straddles 1.0, where double vs single rounding is visible); - `v_mac_f32`: 102 of 14440. The legacy variants already pass, because `legacy_mul` ends in a `select` that cuts the `fmul → fadd` edge. This replaces the plain product with an FMad over a zero signed by the operands' own signs (`sign(a) ^ sign(b)`), which rounds exactly like a plain IEEE multiply — signed zeros included, denormal outputs the same way as the fused `v_fma_f32` path already verified — and hides the multiply behind a call the fusion pass sees no other side of. +19/−2 in `emit/lower.rs`, no test changes. With it, `arithmetic_matches_the_hardware` passes on both backends word for word (the rest of the suite's failures on this branch are the ones covered by #1903, #1896 and #1857). Local: RTX 5070 Laptop GPU (driver 616.87) and WARP, 240 tests pass. AI-assisted (Mistral Vibe).
catminou4 commented 2026-11-25 02:49:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T02:49:10Z.

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_f32/v_mac_f32 multiply kept apart from the add (no FMA contraction) — precision fix matching #1903's ray-test treatment; suite green.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051092945 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1920#pullrequestreview-6051092945). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T02:49:10Z. **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_f32/v_mac_f32 multiply kept apart from the add (no FMA contraction) — precision fix matching #1903's ray-test treatment; suite green.
Headache3318 commented 2026-11-25 03:03:19 +00:00 (Migrated from github.com)

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


Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head 7054097 applied cleanly onto current main ccfb029.

  • cargo test -p victory-shader --locked: 144 passed, 0 failed, 2 ignored. All tests pass.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 4 pre-existing errors in test code (fail identically on main ccfb029). The PR introduces no new clippy warnings.

Not tested: Windows MSVC build, the title itself. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150368434 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1920#issuecomment-7150368434). Originally posted by [Headache3318](https://github.com/Headache3318) on 2026-11-25T02:08:39Z. --- Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head `7054097` applied cleanly onto current `main` `ccfb029`. - `cargo test -p victory-shader --locked`: 144 passed, 0 failed, 2 ignored. All tests pass. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: 4 pre-existing errors in test code (fail identically on main `ccfb029`). The PR introduces no new clippy warnings. Not tested: Windows MSVC build, the title itself. AI-assisted.
Marvisak commented 2026-11-25 09:06:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T08:19:17Z.


Automated triage for the maintainers.

Merge candidate.

In crates/victory-shader/src/emit/lower.rs, the plain v_mad_f32 / v_mac_f32 product becomes mul_apart: an FMad (DXIL op 46, the same fmad that v_fmac_f32 uses) over a zero whose sign is sign(a) ^ sign(b). That gives round(a*b) whether or not the host fuses FMad, because adding a zero of the product's own sign is exact, and it keeps -0 products. The following fadd can then no longer be contracted with a bare fmul. The legacy variant (legacy_mul) is unchanged. The evidence is the hardware-reference test arithmetic_matches_the_hardware: per the PR it now matches on an NVIDIA card where it mismatched before, and WARP was already clean.

Cost: three integer ops per v_mad_f32 / v_mac_f32. Related to #1903 (the same technique on other opcodes), but it doesn't depend on it.

CI: check passes.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7156880571 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1920#issuecomment-7156880571). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T08:19:17Z. --- <!-- victory-triage v1 head=70540976ce32e6a8d3b1aa97773c9724a2574265 verdict=merge-candidate at=2026-11-25T08:24:00Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-shader/src/emit/lower.rs`, the plain `v_mad_f32` / `v_mac_f32` product becomes `mul_apart`: an `FMad` (DXIL op 46, the same `fmad` that `v_fmac_f32` uses) over a zero whose sign is `sign(a) ^ sign(b)`. That gives `round(a*b)` whether or not the host fuses `FMad`, because adding a zero of the product's own sign is exact, and it keeps `-0` products. The following `fadd` can then no longer be contracted with a bare `fmul`. The legacy variant (`legacy_mul`) is unchanged. The evidence is the hardware-reference test `arithmetic_matches_the_hardware`: per the PR it now matches on an NVIDIA card where it mismatched before, and WARP was already clean. Cost: three integer ops per `v_mad_f32` / `v_mac_f32`. Related to #1903 (the same technique on other opcodes), but it doesn't depend on it. CI: `check` passes. --- _Generated by [Claude Code](https://claude.ai/code)_
sinan-cmyk commented 2026-11-25 12:03:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T11:28:51Z.


Offline measurement on a retail dump

Result: no count change against main.

I merged head 70540976 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7161102314 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1920#issuecomment-7161102314). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T11:28:51Z. --- ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 70540976 onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
fuzeofficlal commented 2026-11-26 18:53:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T18:53:26Z.

Review: APPROVED


Verified 70540976ce32e6a8d3b1aa97773c9724a2574265 cleanly merged onto main (63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP + Windows SDK dxil.dll):

  • Lower::mul_apart (crates/victory-shader/src/emit/lower.rs:2221-2230) applies consistently to v_mac_f32 and v_mad_f32 / v_madak_f32 / v_madmk_f32 (lower.rs:6290-6309), preserving the -0.0 product sign via FMad(a, b, copysign(0.0, a * b)) and preventing host contraction with the following fadd, while leaving v_fma*_f32 / v_fmac_f32 and *_legacy_f32 paths untouched.
  • emit/lower.rs size: 521,365 B (under 524,288 B); cargo test -p xtask --locked policy::tests::repository_passes_policy: 1 passed (asset policy: 191 files ok).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features): clean.
  • emit::semantics_tests::arithmetic_matches_the_hardware (7.04s RTX 5090 / 0.58s WARP), emit::valu_tests::v_mul_legacy_f32_treats_denormals_as_zero, emit::tests::constant_color_validates, and emit::lower::tests:: (33 passed, 0 failed) all pass on both RTX 5090 and WARP.

Ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6079202344 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1920#pullrequestreview-6079202344). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T18:53:26Z. **Review:** APPROVED --- Verified `70540976ce32e6a8d3b1aa97773c9724a2574265` cleanly merged onto `main` (`63bcca43`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP + Windows SDK `dxil.dll`): - `Lower::mul_apart` (`crates/victory-shader/src/emit/lower.rs:2221-2230`) applies consistently to `v_mac_f32` and `v_mad_f32` / `v_madak_f32` / `v_madmk_f32` (`lower.rs:6290-6309`), preserving the `-0.0` product sign via `FMad(a, b, copysign(0.0, a * b))` and preventing host contraction with the following `fadd`, while leaving `v_fma*_f32` / `v_fmac_f32` and `*_legacy_f32` paths untouched. - `emit/lower.rs` size: **521,365 B** (under 524,288 B); `cargo test -p xtask --locked policy::tests::repository_passes_policy`: `1 passed` (`asset policy: 191 files ok`). - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`): clean. - `emit::semantics_tests::arithmetic_matches_the_hardware` (`7.04s` RTX 5090 / `0.58s` WARP), `emit::valu_tests::v_mul_legacy_f32_treats_denormals_as_zero`, `emit::tests::constant_color_validates`, and `emit::lower::tests::` (`33 passed, 0 failed`) all pass on both RTX 5090 and WARP. Ready to merge.
Dukestar69 commented 2026-11-26 23:47:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T23:47:08Z.

Review: APPROVED


LGTM — verified on Windows against origin/main (63bcca4).

  • Separating the multiply and add for v_mad_f32 and v_mac_f32 avoids unintended FMA fusion and matches RDNA2 non-fused mad/mac semantics cleanly.
  • cargo test -p victory-shader --lib emit::lower::tests:: -- --test-threads=1: all passed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6081646018 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1920#pullrequestreview-6081646018). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T23:47:08Z. **Review:** APPROVED --- LGTM — verified on Windows against `origin/main` (`63bcca4`). - Separating the multiply and add for `v_mad_f32` and `v_mac_f32` avoids unintended FMA fusion and matches RDNA2 non-fused `mad`/`mac` semantics cleanly. - `cargo test -p victory-shader --lib emit::lower::tests:: -- --test-threads=1`: all passed. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/1920:upstream-pr/1920'
git switch 'upstream-pr/1920'

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