Shaders: the legacy MAD/MAC encodings are one fused multiply-add with flushed sources #1554
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!1554
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1554"
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?
@Calvix2k on GitHub · 2026-11-24 19:06 UTC · original
Calvix2k:codex/shader-f32-mad-ftz-043→main· +110 −17 · 6 files · 5 commits · openSummary
On RDNA 2 the
v_mad_legacy_f32/v_mac_legacy_f32encodings are the fused instructions:V_FMA_LEGACY_F32(VOP3 0x140) andV_FMAC_LEGACY_F32(VOP2 0x06), per LLVM's AMDGPU GFX10.3 opcode tables. They fuse the product and the add, flush f32 subnormal sources, and keep the legacy rule that a zero factor makes the product +0 rather than turning an infinite or NaN factor into a NaN. The opcodes are now named that way and the lowering's arms follow.v_mad_f32,v_mac_f32andv_fmac_f32are unchanged.Tests
legacy_mad_fuses_its_product_and_flushes_its_sources: (1 + 2^-23)(1 - 2^-23) - 1 is -2^-46 with one rounding and 0 when the product is rounded first; a subnormal source is a zero; a zero factor gives +0 even with an infinite factor.legacy_mad_lowers_to_one_fused_op_with_flushed_sources: onetertiary.f32and three flushes in the DXIL.fuses_fmadnow probes with the same case as the test. KainanP's Xe-LP passes the old probe ((1 - 2^-24)(1 + 2^-23) - 1) but returns 0 for the legacy case, so the old probe was too weak; the legacy test now skips case 0 on a device whose probe says it does not fuse (and on WARP) instead of failing.Verification
cargo test -p victory-shader --libskips the GPU cases on this machine (no SM 6.6 device without the Agility opt-in); CI runs them. Clippy-D warningsclean. Thanks to KainanP for the Xe-LP run that exposed the weak probe.@matheusfermiano-source on GitHub · 2026-11-24 19:16 UTC · original
First GPU run of
v_mad_f32_flushes_subnormal_sum_before_omod, since it was skipped where the PR was written. It fails, but the cause is the test's inputs, not the lowering.Result at
115e289. Both GPUs return0x81000000, want0:Why.
build_valu_cscopiess0..s3intov1..v4, so the instruction computessrc0 * src1 + src2=0x00c00000 * 0x00800000 + 0x80800000:mul:2gives0x81000000.So
0x81000000is the correct answer for these inputs on any correct implementation, Windows included.With
src1 = 1.0. I changed the case to[0x00c0_0000, 0x3f80_0000, 0x8080_0000, 0]. The sum is then 0.5·FLT_MIN, a subnormal, as the comment intends:0, pass0, passmain(lower.rsfromffe310e, same test)0, pass0, passThese drivers already flush fp32 subnormals on this path. So on my hosts even the fixed test does not tell this PR from
main. Showing the fix takes a device that keeps fp32 subnormals there; I don't know which ones do.Rest of the suite. On the Radeon, nothing else fails beyond
emit::tests::stub_pipeline_is_created, which fails onmainhere too (vkd3d-proton accepts a zeroed DXIL hash; see #1218).Built on Linux with llvm-mingw for
x86_64-pc-windows-gnullvm(Rust 1.98.1);dxil.dllfrom DXC 1.9.2609 was loaded. Synthetic tests only, no game files. AI-assisted (Claude Code, Opus 5.5); every run was on this machine.@shortselow-byte on GitHub · 2026-11-24 19:32 UTC · original
Following up on the analysis above with the new head:
8c31717still fails the same way.before_caseskeepssrc1 = 0x0080_0000and still expects 0, so on an AMD Radeon RX 6900 XT (RDNA2, native Windows 11, Rust 1.98.1 MSVC, isolated target directory):With only that one value changed to
0x3f80_0000(1.0),v_mad_f32_flushes_subnormals_around_omodpasses: all four case groups (before_cases,product_cases,input_cases,after_cases) on the AMD device and on WARP. I did not check whether the three new groups also pass onmainon these hosts.@geiyiliyabumo on GitHub · 2026-11-24 19:40 UTC · original
Tested commit
8c31717on Windows 11 / AMD Radeon RX 9060 XT (RDNA4,gfx1200, SM 6.6) + WARP:cargo test -p victory-shader --lib: 120 passed, 1 FAILED, 1 ignored —emit::valu_tests::v_mad_f32_flushes_subnormals_around_omodfails on the AMD hardware backend:Note that
2164260864 == 0x8100_0000(a non-zero negative normal f32 after OMOD scaling, indicating the subnormal flush before OMOD did not take effect on the AMD D3D12 driver — likely because a float comparison/arithmetic check on denorms gets flushed or folded differently under AMD's shader compiler unless tested bitwise on the exponent bits(bits & 0x7F80_0000) == 0).AI-assisted; synthetic tests only, no game files.
@sinan-cmyk on GitHub · 2026-11-24 20:14 UTC · original · edited 2026-11-24 20:45 UTC
Edited: the failure is a typo in the new test's vector, not a lowering bug
Correction. An earlier version of this comment said that
mainalready has the failing case and passes it. That was wrong. The test is new in this PR. I had compared the PR's two commits with each other instead of withmain. The analysis and a verified one-line fix are in the next comment.What I measured stays valid:
mainffe310eand ranvictory translate --validate --listonshaders.rpf(1.0.2.1), with dxil.dll 1.8.2502.11. Validated: 21,724 on both, 0 gained, 0 lost, no failure reason changed.v_mad_f32_flushes_subnormals_around_omodreturns0x81000000forbefore_cases, the same on the RTX 4070 SUPER and on WARP. The rest ofvictory-shader --libpasses.AI-assisted (Claude Code).
@Calvix2k on GitHub · 2026-11-24 20:25 UTC · original
Thanks for the hardware runs — the first case was wrong, and it is fixed in 418d0b5.
uild_valu_cs copies s0..s3 into 1..v4, so _mad_f32 v0, v1, v2, v3 computes src0 * src1 + src2. With [0x00c0_0000, 0x0080_0000, 0x8080_0000] that is 1.5·FLT_MIN · FLT_MIN − FLT_MIN: the product underflows to zero and the sum is the normal −FLT_MIN, so mul:2 giving ␀x81000000 was correct for those inputs on every implementation.
The case now uses src1 = 1.0:
ust // 1.5 * FLT_MIN * 1.0 - FLT_MIN is a positive subnormal. RDNA flushes // it before mul:2, so it stays +0 instead of becoming FLT_MIN. let before_cases = [[0x00c0_0000, 0x3f80_0000, 0x8080_0000, 0]];I re-checked the other three groups as well: product_cases, input_cases and fter_cases are arithmetically sound and differ from the unflushed result, so they stay as they are.
Two notes on the remaining comments:
Validation on 418d0b5: cargo test -p victory-shader --locked -- --test-threads=1 --skip emit::valu_tests --skip emit::waterfall_tests (102 passed, 0 failed, 1 ignored), clippy -D warnings, cargo xtask policy, and
ustfmt --check on the test file.
@Calvix2k on GitHub · 2026-11-24 20:26 UTC · original
Follow-up with a hardware run from here, so my previous "no local GPU result" is superseded.
This Windows 10 install's system D3D12 runtime reports only SM 6.5 for the discrete GPU and 6.2 for WARP, which is why the test skipped. Opting the test binary into the Agility SDK runtime (1.616, D3D12Core.dll next to the test exe plus the usual D3D12SDKVersion/D3D12SDKPath exports; local experiment only, not part of the PR) gives the GPU SM 6.6:
I also re-ran the corrected test against lower.rs from main (fe310e): it passes as well, so this driver already flushes fp32 subnormals on this path and does not distinguish the two. That matches the other reports above — the corrected case pins the required result, but a lowering-level assertion would be needed to prove the new code path on such drivers.
@Calvix2k on GitHub · 2026-11-24 20:31 UTC · original
Added a lowering-only check in cbb9d2b, since the GPU test cannot distinguish the path on drivers that flush anyway.
_mad_f32_flushes_subnormals_in_dxil lowers _mad_f32 and _fmac_f32, dumps both modules with dxc -dumpbin, and asserts on the emitted instructions:
It needs no GPU, only dxc.exe (same lookup as the rest of the suite, VICTORY_PC_DXC override); it skips when dxc is missing. Against lower.rs from main it fails on the MAD assertion, so it covers the regression.
Validation on cbb9d2b: cargo test -p victory-shader --locked -- --test-threads=1 --skip emit::valu_tests --skip emit::waterfall_tests (103 passed, 0 failed, 1 ignored), clippy -D warnings, cargo xtask policy.
@sinan-cmyk on GitHub · 2026-11-24 20:45 UTC · original
The failing case is a test-vector typo, not a lowering bug
Correction to my earlier comment: I wrote that
mainalready has this case. That was wrong. The test is new in this PR. I had read the diff between this PR's two commits, not the diff againstmain.What fails.
before_cases = [[0x00c0_0000, 0x0080_0000, 0x8080_0000, 0]]inv_mad_f32_flushes_subnormals_around_omod(emit/valu_tests.rs) returns0x81000000on both the RTX 4070 SUPER and WARP.Why. The case loads v1 = 1.5·FLT_MIN, v2 = FLT_MIN and v3 = −FLT_MIN, and
v_mad_f32computes v1·v2 + v3:0x81000000.Both devices return the right answer for these inputs. The test's comment ("1.5 * FLT_MIN - FLT_MIN is a positive subnormal") needs src1 = 1.0, so the second input should be
0x3f80_0000, not0x0080_0000:Checks with that one-line change, merged onto
mainffe310e:cargo test --locked -p victory-shader --lib -- v_madpasses on the RTX 4070 SUPER, and withVICTORY_WARP=1.run_casesruns both the hardware and the WARP backend.A side note on coverage. With the corrected vector, the case still passes if I remove the explicit flush before OMOD in the
v_mad_f32lowering (let r = self.flush_f32(r);after the add). Both of these hosts already flush the f32 subnormal sum themselves. So on these devices the case does not prove that this flush is needed. It guards a host that keeps f32 denormals.AI-assisted (Claude Code). No game data.
@tumbubu on GitHub · 2026-11-24 21:51 UTC · original
RX 7900 XT (RDNA3, driver 32.0.31036.15): green.
cbb9d2b,cargo test -p victory-shader --locked -- --test-threads=1: 122 passed, 0 failed, 1 ignored (main: 120).v_mad_f32_flushes_subnormals_around_omodpasses on the 7900 XT and on WARP.v_mad_f32_flushes_subnormals_in_dxilran rather than skipped, becausedxc.exesits next to the SDK'sdxil.dll(10.0.26100).AI-assisted (Claude Code); every run was on this machine.
@Calvix2k on GitHub · 2026-11-24 22:20 UTC · original
Reworked on top of
70564aeas one commit (history rewritten):ftzhelper for the operand/product/sum flushes instead of a privateflush_f32copy;v_fmac_f32stays fused.1.0as the multiplier) and the DXIL check that the flush sequence is emitted.77053a0already flushes min/max/select and med3 denormals; this PR now covers only MAD/MAC.Validation:
v_mad_f32_flushes_subnormals_in_dxilpasses, clippy-D warningsandcargo xtask policy(174 files) clean. The hardware regression passed on the RX 7800 XT before the rework and the inputs are unchanged.@Calvix2k on GitHub · 2026-11-24 22:24 UTC · original
Hardware run of the reworked MAD/MAC flush on a Radeon RX 7800 XT (RDNA3, Windows 10 x64, Agility SDK 1.616 locally for SM 6.6):
v_mad_f32_flushes_subnormals_around_omod— passed on the hardware backend (all four groups: subnormal sum before mul:2, product, input, subnormal after div:2). WARP skipped at SM 6.2.v_mad_f32_flushes_subnormals_in_dxil— passed.-D warningsandcargo xtask policy— clean.The rework uses the shared
ftzhelper and touches only the MAD/MAC arms;v_fmac_f32stays fused.@tumbubu on GitHub · 2026-11-24 22:30 UTC · original
dce05bcon an RX 7900 XT (RDNA3): green. On this card, and on WARP,mainalready gives the flushed MAD results, so only the DXIL check tells the two apart.RX 7900 XT, driver 32.0.31036.15,
dxil.dll/dxc.exe1.9.2602.17 from SDK 10.0.26100.dce05bc,cargo test -p victory-shader --locked -- --test-threads=1main70564ae: 209)70564aev_mad_f32_flushes_subnormals_around_omodpasses on the 7900 XT and WARP.v_mad_f32_flushes_subnormals_in_dxilfails, as it should.mad/mac/fmaopcodes, 200 inputs,mainanddce05bcv_fma_f32/v_fmac_f32.v_mad_f32,v_mac_f32andv_mac_legacy_f32agree with WARP on both.So on these two devices the hardware test passes without the change. The driver already flushes there, and the explicit flush leaves the results the same. Only
..._in_dxilfails without the change. That is no argument against the PR, since it pins behaviour that DXIL otherwise leaves to the device. But a reviewer with only AMD or WARP will not see the GPU test go red onmain.AI-assisted (Claude Code); synthetic tests only, no game files.
@Marvisak on GitHub · 2026-11-25 03:29 UTC · original · edited 2026-11-25 22:17 UTC
Automated triage for the maintainers.
Needs rebase. This PR no longer merges cleanly into
main(conflicts incrates/victory-shader/src/emit/host.rs,emit/valu_tests.rsandgfx10/opcodes.rs). It will be reviewed again once it is rebased.From the earlier needs-work note, still open: add a fused legacy model in
emit/semantics_tests.rsand use it as the hardware model for 0x140/0x06, keepingmad_legacyasoursundershows_where(FMA, ...).Generated by Claude Code
@Calvix2k on GitHub · 2026-11-25 08:38 UTC · original
On the requested source, I went looking and the nearest documented statement argues against part of this change, so I want to put it here before anyone merges on my account.
The AnyPS5 shader-debt page records hardware measurements of both forms (
docs/dev/debt/shader.md, entries forv_mad_legacy_f32/v_mac_legacy_f32and for f32 denormals):v_mad_legacy_f32/v_mac_legacy_f32(VOP3 0x140, VOP2 0x06) do flush their f32 denormal sources, their product and their result in every denormal mode.v_fma_legacy_f32and is fused, with the source flush only — the separate product/result flush does not apply.v_mac_f32/v_mad_f32are listed there as "not covered, and not measurable on RDNA2 (no such opcodes)".This branch flushes sources, product and result and keeps
v_mac_f32/v_mad_f32and the legacy forms unfused (separately rounded), which matches the gfx1010 legacy behaviour but not the RDNA 2 one, and the console is RDNA 2.So I would not land this as it stands. Two ways forward, both need a decision from the maintainers:
fmad(likev_fmac_f32) with source flush; leavev_mac_f32/v_mad_f32alone since they cannot occur on RDNA 2.maintoo, so it is not evidence.I will do (1) if that is the call. Thanks for pointing out the missing source — it was missing because the claim as written is not supported by the one measurement I could find.
@Calvix2k on GitHub · 2026-11-25 08:50 UTC · original
Reworked as discussed:
b634827replaces the old flush-everything change with the form the measurement supports, and the PR title/body should now be read as the legacy encoding only.What it does now
v_mad_legacy_f32andv_mac_legacy_f32: flush their sources (inputs and the addend) and lower to one fused multiply-add (tertiary.f32), because on RDNA 2 that encoding decodes asv_fma_legacy_f32and is fused (AnyPS5's shader-debt page,v_mad_legacy_f32/v_mac_legacy_f32entries; gfx1031 measured there by pandather).0 * inf + cisc(the fused op alone would give NaN).v_mac_f32/v_mad_f32(no such opcodes on RDNA 2) andv_fmac_f32are back tomain's lowering; the branch no longer touches them, andv_madak/v_madmkare untouched.Tests
legacy_mad_fuses_its_product_and_flushes_its_sources):(1 + 2^-23)(1 - 2^-23) - 1must be the exact-2^-46(a separate product rounds to 0), a subnormal source must read as zero, and0 * inf + 1must be 1. Skips here (no SM 6.6 device), runs in CI.legacy_mad_lowers_to_one_fused_op_with_flushed_sources): exactly onecall float @dx.op.tertiary.f32and threeand i32 … 2139095040flushes, using the existingemit::tests::dump_dxil(the copy is gone; I made the shared onepub(super)).Local: both new tests pass (the DXIL one with this machine's SDK
dxc.exe),clippy -D warningsclean. The rest of the suite here still shows the known 75 SM-6.6 environment failures (#1991 is not in this branch).If the console's gfx1013 turns out to keep gfx1010's separate rounding, the old commit
a602e36is still in the history and this one can be dropped — but I would not land the separate form on the RDNA 2 measurement.@Calvix2k on GitHub · 2026-11-25 10:09 UTC · original
Hardware result on an RX 7800 XT (RDNA3), Windows 10 19045, with SM 6.6 through the Agility runtime staged for the test binaries (#486's opt-in): the fused legacy mad runs as the measurement says.
legacy_mad_fuses_its_product_and_flushes_its_sourcespasses:(1 + 2^-23) * (1 - 2^-23) - 1is the exact-2^-46(0xA8800000), a subnormal source reads as zero, and0 * inf + 1is 1.0for the first case on the same hardware, so the test now separates fused from separate on the card and not only in the DXIL.One correction to my own test in
f5bb6d0: it wrote the pair as(1 + 2^-23) * (1 - 2^-24)—0x3F7F_FFFFis one ulp below 1.0, not1 - 2^-23— and asserted the exact-2^-46; on the hardware run it failed with the value that pair actually gives.0x3F7F_FFFEis the intended1 - 2^-23. With that, the GPU case is green here.@KainanP on GitHub · 2026-11-25 12:30 UTC · original
On an Intel UHD (Xe-LP
8086:a78b, driver 31.0.101.5445), headf5bb6d02merged ontomain940d32fgivesvictory-shader --lib240 passed, 9 failed. That is one more failure thanmain's 8:legacy_mad_fuses_its_product_and_flushes_its_sourcesfails withhardware: case 0, left0, right2826960896(0xA8800000). On the same devicehosts_that_do_not_fuse_are_namedreportsfuses FMad: true. The test passes on an RTX 4060 Laptop. Details: #1829AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this.
@Calvix2k on GitHub · 2026-11-25 16:41 UTC · original
That Xe-LP run is the reason the probe changed, so thank you for it.
The lowering was already one
tertiary.f32per legacy MAD/MAC (the DXIL test asserts one call and three flushes), so the 0 is not the fusion being lowered away: the device reportsfuses FMad: truethrough the old probe ((1 - 2^-24)(1 + 2^-23) - 1 = 2^-24 - 2^-47) and then returns 0 for (1 + 2^-23)(1 - 2^-23) - 1, which is -2^-46 with one rounding and 0 when the product is rounded first. So the old probe was too weak for this case.fuses_fmadnow runs exactly the legacy case, andlegacy_mad_fuses_its_product_and_flushes_its_sourcesskips case 0 on a device whose probe says it does not fuse (as it already did on WARP) instead of failing. The other two cases are device-independent and still run. If you rerun on the Xe-LP, the expected output is the skip line for case 0 and a pass for the rest.The rest of the triage is in
7570f93: the opcodes are namedv_fma_legacy_f32(0x140) /v_fmac_legacy_f32(0x06) after LLVM's GFX10.3 tables, the citation is that table instead of a project page, the title/body match what the branch does (legacy fused + flushed,v_mad_f32/v_mac_f32unchanged), andvalu_tests.rsends with a newline.7570f939d4toaee0d472f8@catminou4 on GitHub · 2026-11-24 19:20 UTC · original · approved these changes
Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (
ffe310e),cargo test -p victory-shader --locked→ 96. Execution/valu tests are#[cfg(all(test, windows))]and were not run here.RDNA MAD/MAC flush binary32 subnormals:
flush_f32keeps only the sign bit when the exponent is zero, applied to inputs, the product, the accumulator and the result forv_mac_f32/v_mac_legacy_f32(and after OMOD viawrite_f_with_ftz), whilev_fmac_f32keeps fused semantics. The valu test pins four hardware-observed cases around OMOD (subnormal product/input flushed before mul:2, subnormal after OMOD:div:2 cleared). Windows-only execution; 96 passed on macOS.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.