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

Open
github-bot wants to merge 1 commit from github-bot/gh-1920 into main AGit
Owner

@⁠kais7654 on GitHub · 2026-11-25 01:00 UTC · original
kais7654:shader/mac-mad-fma → main · +19 −2 · 1 files · 1 commits · open

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

**@⁠kais7654** on GitHub · 2026-11-25 01:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1920) `kais7654:shader/mac-mad-fma` → `main` · +19 −2 · 1 files · 1 commits · open 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).
The lowering emits the product rounded (and flushed) before the add,
which is what the ISA says, yet a driver folds the fmul into the fadd
behind it as an FMA though nothing carries a fast-math flag: on the
2070-class card here the tail of the product escapes one rounding where
two were due (2 of 2560 inputs on v_mad, 102 of 14440 on v_mac, WARP
none, the model none). An FMad over a zero signed by the operands'
signs rounds exactly like the plain product and hides the multiply
behind a call the fusion pass sees no other side of; with it the
arithmetic suite meets both backends word for word.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:48:48 +00:00
Author
Owner

@⁠Headache3318 on GitHub · 2026-11-25 02:08 UTC · original

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.

**@⁠Headache3318** on GitHub · 2026-11-25 02:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/1920#issuecomment-7150368434) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 08:19 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 08:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/1920#issuecomment-7156880571) <!-- 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)_
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 11:28 UTC · original

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

**@⁠sinan-cmyk** on GitHub · 2026-11-25 11:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1920#issuecomment-7161102314) ### 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).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/1920/head:github-bot/gh-1920'
git switch 'github-bot/gh-1920'

Merge

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