Shaders: v_mac_f32 and v_mad_f32 flush the product they round #3212
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3212
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3212"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@zzm1234560621-prog on GitHub · 2026-11-26 13:08 UTC · original
zzm1234560621-prog:shaders/mad-product-flush→main· +70 −21 · 2 files · 4 commits · openWhat
v_mac_f32andv_mad_f32lower to a plain multiply and an add, without the flush the console applies to the product it rounds. A host that flushes sign-preserving then disagrees with the console on the sign of an underflowed product:+2^-126 × -2^-126underflows. The console flushes the product to +0; this Intel adapter leaves -0, and the add carries it out.v_mac_legacy_f32andv_mad_legacy_f32already agreed, because they go throughlegacy_mul.flush_denormal_to_positive_zerois the console's flush: a result whose exponent field is zero and whose mantissa is not becomes+0, while zeros pass through untouched, so the-0of a true zero product (a negative operand times zero) keeps the sign the multiply gave it.Applied to the product of
v_mac_f32and ofv_mad_f32/v_madak_f32/v_madmk_f32. The fused forms (v_fma_f32,v_fmac_f32) already go throughFMadand are not touched.Measured
semantics_tests::arithmetic_matches_the_hardware, Intel Arc (Xe-LPG, driver 32.0.101.6127), Windows 11, Rust 1.98.1,main63bcca4:v_mac_f32v_mad_f32Not verified
semantics_tests' model encodes ("the product rounded (and flushed) before the add"), which is where the numbers above come from.+0, the added select should be a no-op — the same value out — but I have no such card to confirm it, and WARP's half of the same test is unchanged (it reported 0 mismatches before and after).No game files. Reported with the rest of this adapter's results in #3196. AI-assisted (Claude).
@AurumCarlos on GitHub · 2026-11-26 13:10 UTC · original · reviewed
Thanks for the clear write-up and the before/after table. The diagnosis matches what the model says, and putting the select between the multiply and the add also has a side benefit: the driver can no longer contract them into an FMA, which the "rounded product" semantics need anyway. A few remarks:
1. The helper only works if the host hasn't flushed the product yet. It decides on the product's bits: exponent 0 and mantissa ≠ 0 → +0. That catches your Intel case, where the multiply leaves a denormal in the register and only the add flushes it. But on a host or driver that flushes at the multiply itself, sign-preserving, the product is already
-0(mantissa 0) when the select sees it. It then passes through as a "true zero" and the add still gives-0. So the fix is tied to where the host flushes, which is what the existing helpers try to avoid ("decided on the bits (the host's own denormal mode varies)" onftz).A version that doesn't depend on that would decide from the operands, the way
legacy_muldoes. A product that comes out with exponent 0 while neither operand has exponent 0 is an underflow, so it becomes +0. One where an operand is zero keeps the multiply's sign. That covers both "the host left a denormal" and "the host already flushed to -0". If you want to keep the current form, it would be worth saying in the doc comment that it assumes the product reaches the select unflushed.2. Zeros: this differs from
write_f's omod path, maybe on purpose. The omod path flushes one == 0alone, so a-0product there becomes+0too ("a zero or denormal product comes out +0 (measured on RDNA2 with IEEE mode off)"). Here, a true-0keeps its sign. Your 0-mismatch run suggests the model wants exactly that for MAD, but since the two places now disagree on the sign of a zero product, a short note on why would help the next reader.3.
lower.rssize. This bringslower.rsfrom 520,563 to 521,545 bytes (+982). #3127 (+3,167), #3158 (+284) and #3192 (+336) are still open and grow the same file, and the four together come to about 525,300, over the 524,288 policy cap. Each passes alone, but whichever lands last will need a trim. Reusing a shared exponent/mantissa test withftz(the masks and theAnd/cmppair are the same) might win some of it back here.@oni-swr on GitHub · 2026-11-26 13:25 UTC · original
WARP test report (head
09c7b82, on top of current main63bcca4)Environment: Windows 11, WARP only (
VICTORY_WARP=1andVICTORY_D3D12_WARP=1), Windows SDK 10.0.26100dxil.dll1.8.2502.11 viaVICTORY_DXIL, rustc/cargo 1.98.1.cargo test -p victory-shader --lib --locked -- --test-threads=1: 298 passed, 4 failed, 3 ignored. This includessemantics_tests::arithmetic_matches_the_hardwareanddiff_tests::float_semantics_are_the_consoles, both ok.synthetic_tests::{a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_mask_high_address_bits_and_divergent_branches, permutes_read_the_lane_named_and_zero_from_lanes_left_out}(no pipeline for a wave of 32/ a gather mismatch). The same 4 fail identically on unmodified main, so they are WARP limits (cross-lane, wave-size tests), not caused by this PR.v_mad_f32/v_mac_f32, so the new flush is unverified by the suite.Diff review: the lowering is a clean select (exponent 0 and mantissa non-zero gives +0), applied to the product only, before the add.
v_mad_legacy_f32is untouched. One thing to check: the in-repo reference model (semantics_tests.rs,mad=add(mul(a,b),c)without64) keeps the sign of a flushed product (-0), while this PR flushes to +0. The two disagree on a negative denormal product, so-0 + cdiffers whencis ±0. The existing tests don't reach that case. A test with a negative denormal product, plus the reference model updated to match, would pin the claim down.Cannot verify here: that the console really drops the sign (no hardware oracle).
AI use: Claude Code (Claude Sonnet 5.5), coordinated by Claude Code (Claude Opus 5.5). Synthetic tests only; no game files, dumps, shader names or retail hashes.
@Dukestar69 on GitHub · 2026-11-26 13:29 UTC · original · approved these changes
Verified on RTX 5090 against
origin/main(63bcca4):cargo test -p victory-shader --lib thirty_two_bit_floats -- --test-threads=1: passed.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Cleanly flushes denormal intermediate products of
v_mac_f32andv_mad_f32to+0.0(while passing signed zeros through).Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@andrewpozdnakov7-jpg on GitHub · 2026-11-26 13:52 UTC · original
@zzm1234560621-prog I read back the exact FP32 words for
v_mac_f32andv_mad_f32on63bcca43and your09c7b82, using the same in-process translator and DXIL validator. App-local WARP 1.0.22 gave the same results on both revisions in three runs.00800000 × 80800000 + 00000000(the reported input)00000000800000008000000080800000 × 3f000000 + 80000000(negative subnormal mathematical product)80000000bf800000 × 00000000 + 80000000(true zero product)80000000The normal control and the case with a smallest-normal addend were also unchanged. This covers the proposed input on WARP and leaves the Intel result for hardware verification. The opt-in test can print the same words on Intel with
VICTORY_WARPunset.If reusing it, please retain the Git author or add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>if folding it into another commit. AI-assisted with Codex.@zzm1234560621-prog on GitHub · 2026-11-26 14:43 UTC · original
Both reviews answered with the hardware run the probe was for.
0ed5c35folds in andrewpozdnakov7-jpg's probe (with theCo-authored-bythey asked for), and on this machine the two backends now agree word for word:Before the patch, input 1 on hardware was
80000000where WARP gave00000000— that is the mismatcharithmetic_matches_the_hardwarewas reporting.To oni-swr's point about the reference model keeping the sign: with the patch,
arithmetic_matches_the_hardwarepasses on both backends over all 14,440 inputs, negative denormal products included, so the model and the lowering do not disagree on the set the suite feeds. Your reading is right that the two would differ on-0 + cif the model flushed sign-preserving and the lowering did not — but the observed words above (rows 2 and 3 are80000000, i.e.-0) say this driver's product lands on-0before any select of mine, so the+0arm is only taken where the product is an actual denormal, which is input 1.What I cannot claim is the mechanism: whether this driver flushes the product sign-preservingly and my select therefore only fires on input 1, or whether the driver leaves a denormal in all three and something else explains rows 2 and 3. Both readings fit the words. The console is the oracle for the sign, and none of us has one, so I have kept the change to "the suite's model and both backends agree" and left the mechanism out of the commit message.
If the probe is worth keeping past this PR, it should probably stop being
#[ignore]and assert the words on any adapter, which would make it a real regression test for the sign question rather than a print. I did not do that here because I do not know which of the two readings the maintainers consider established.AI-assisted (Claude).
@zzm1234560621-prog on GitHub · 2026-11-26 14:48 UTC · original
One correction with numbers, since it changes what the model already covers: the negative-denormal-product cases are in the 14,440, not outside them.
arithmetic_probesfeeds the accumulate and mad probestriples(F32_EDGES, F32_EDGES, F32_THIRD), which is fully cartesian —38 × 38 × 10 = 14,440, the count in the summary line. And the sets contain the operands in question:F32_EDGEShas0x8080_0000(±smallest normal, line 673) and0x3F00_0000(0.5, line 676);F32_THIRDhas0x0000_0000and0x8000_0000(lines 708–709).So
[0x8080_0000, 0x3F00_0000, 0x0000_0000]and[…, 0x8000_0000]— the negative denormal product with a+0and with a-0addend — are both fed tov_mac_f32andv_mad_f32byarithmetic_matches_the_hardware, and with this patch it passes on both backends. That test compares againstmad/out64, so the model and the hardware agree on those inputs; the model is not keeping a sign the lowering contradicts.Which also fits the words from andrewpozdnakov7-jpg's probe on the same machine: rows 2 and 3 (
-0addend) come out80000000on hardware and WARP alike, i.e. plain IEEE addition of a-0product, and only row 1 (+0addend,-0 + +0 = +0) is where the pre-patch hardware differed. So the+0arm this PR adds is not what decides rows 2 and 3 — if it fired there,+0 + -0would be+0and the test would have caught it.I still cannot say whether this driver flushes the product sign-preservingly before the select or leaves a denormal that my select catches, and the reply above says so. But the coverage question is settled: those inputs are in the suite, and it is green on both backends with the change.
AI-assisted (Claude).
@zzm1234560621-prog on GitHub · 2026-11-26 15:14 UTC · original
All three remarks taken, and the first one decided by experiment.
1. Operand-based or bit-based. I implemented your version first — "a product with an empty exponent where neither operand's is empty is an underflow, so
+0" — and it is worse on the hardware here:andrewpozdnakov7-jpg's probe on the same build shows why:
Rows 2 and 3 are
-0x8080_0000 × 0x3F00_0000with a±0addend: both operands have a real exponent, so the operand rule calls it an underflow and flushes to+0, but the model and both backends return-0. So the console keeps the sign of an underflowed product here, not only of a true zero — your reading of the model ("the model keeps the sign") is the right one, and it extends to the underflow case.So the bit test stays, and I did the other thing you offered: the doc comment now says the test is on the product's bits and therefore assumes the product reaches it unflushed, names the host that flushes at the multiply as outside that assumption, and records the 14-input result above so nobody implements the operand form again without seeing it.
2. Zeros and the omod path. Documented: a denormal is flushed to
+0here, a product that is already a zero keeps its sign, which is where this differs fromwrite_f's omod path (any zero →+0).3. Size.
exponent_is_zerois now the one place the mask-and-compare is written, andftz,legacy_muland the MAD flush all call it.lower.rsgrows 584 bytes over the branch, down from 982. Still under the 524,288 cap (cargo xtask policy: 191 files ok), but I take your point that the four open PRs together will not fit — of what is left, about half is the doc comment this review asked for, and the rest is the three-line helper. If it comes to a trim, the cheapest is mergingflush_denormal_to_positive_zerointoftzas aflush_zeros: boolargument rather than two functions; say so and I will do that instead.cargo test -p victory-shader --lib: 297 passed, 4 failed (the four that fail on63bcca4here), 3 ignored.cargo clippy … -D warnings: clean.cargo xtask policy: ok.Thanks for the FMA-contraction point too — that is a second reason the select belongs between the multiply and the add, and worth having in the thread.
AI-assisted (Claude).
ftzwith a flag, not a second copy 66a60a5775@zzm1234560621-prog on GitHub · 2026-11-26 15:23 UTC · original
3, done —
66a60a5merges the flush intoftzas a flag, which is the option I offered in the last comment.ftzand the MAD flush differ in exactly one thing: a denormal becomes a zero of its sign, or+0, and the flush additionally requires a non-zero mantissa so a true zero keeps its sign. One function with that flag now covers both, soexponent_is_zero, the masks and the select are written once.lower.rsover the branch:ftzSo the four open PRs that touch this file are no longer a problem for the 524,288 cap — at +9 there is nothing left to trim, and #3127/#3158/#3192 keep their headroom.
Nothing about the behaviour moved:
cargo clippy … -D warnings: clean.cargo xtask policy: 191 files ok.AI-assisted (Claude).
@Jackster on GitHub · 2026-11-26 16:06 UTC · original · edited 2026-11-26 16:08 UTC
AMD data on a Radeon RX 7900 XTX (gfx1100, 32.0.32029.2010),
victory-shaderbuilt with--features host:v_mac_f32andv_mad_f32both print[00000000, 80000000, 80000000, 00800000, 80000000, 3f000000]at63bcca4(test only) and at the head — identical, so the underflowed product is already+0on this adapter, consistent with your Intel-only failure.Synthetic only. AI-assisted.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.