Skip to content

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

Open
kais7654 wants to merge 1 commit into
Marvisak:mainfrom
kais7654:shader/mac-mad-fma
Open

kais7654 wants to merge 1 commit into
Marvisak:mainfrom
kais7654:shader/mac-mad-fma

Conversation

@kais7654

Copy link
Copy Markdown

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 <vibe@mistral.ai>
@Headache3318

Copy link
Copy Markdown

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.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, 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.

Copy link
Copy Markdown
Owner

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

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 7054097 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants