Skip to content

Shaders: the legacy MAD/MAC encodings are one fused multiply-add with flushed sources - #1554

Open
Calvix2k wants to merge 5 commits into
Marvisak:mainfrom
Calvix2k:codex/shader-f32-mad-ftz-043
Open

Calvix2k wants to merge 5 commits into
Marvisak:mainfrom
Calvix2k:codex/shader-f32-mad-ftz-043

Conversation

@Calvix2k

@Calvix2k Calvix2k commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On RDNA 2 the v_mad_legacy_f32 / v_mac_legacy_f32 encodings are the fused instructions: V_FMA_LEGACY_F32 (VOP3 0x140) and V_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_f32 and v_fmac_f32 are 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: one tertiary.f32 and three flushes in the DXIL.
  • fuses_fmad now 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 --lib skips the GPU cases on this machine (no SM 6.6 device without the Agility opt-in); CI runs them. Clippy -D warnings clean. Thanks to KainanP for the Xe-LP run that exposed the weak probe.

@matheusfermiano-source

Copy link
Copy Markdown

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 return 0x81000000, want 0:

  • Radeon 660M (RDNA2, RADV Mesa 26.3.1), under Proton with vkd3d-proton 3.1.0 and 3.2.0;
  • RTX 3050 Laptop (NVIDIA 620.73.04), under Proton with vkd3d-proton 3.1.0.

Why. build_valu_cs copies s0..s3 into v1..v4, so the instruction computes src0 * src1 + src2 = 0x00c00000 * 0x00800000 + 0x80800000:

  • That is 1.5·FLT_MIN × FLT_MIN − FLT_MIN, not the 1.5·FLT_MIN − FLT_MIN that the comment describes.
  • The product (about 2e-76) underflows to 0 whether or not subnormals are flushed. The sum is −FLT_MIN, which is normal, so mul:2 gives 0x81000000.

So 0x81000000 is 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:

lowering Radeon 660M RTX 3050
this PR 0, pass 0, pass
main (lower.rs from ffe310e, same test) 0, pass 0, pass

These 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 on main here 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.dll from 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.

@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 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_f32 keeps only the sign bit when the exponent is zero, applied to inputs, the product, the accumulator and the result for v_mac_f32/v_mac_legacy_f32 (and after OMOD via write_f_with_ftz), while v_fmac_f32 keeps 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.

@shortselow-byte

Copy link
Copy Markdown

Following up on the analysis above with the new head: 8c31717 still fails the same way. before_cases keeps src1 = 0x0080_0000 and still expects 0, so on an AMD Radeon RX 6900 XT (RDNA2, native Windows 11, Rust 1.98.1 MSVC, isolated target directory):

assertion `left == right` failed: hardware: MAD flushes subnormal sum before OMOD
  left: 2164260864
 right: 0

With only that one value changed to 0x3f80_0000 (1.0), v_mad_f32_flushes_subnormals_around_omod passes: 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 on main on these hosts.

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

Tested commit 8c31717 on 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_omod fails on the AMD hardware backend:
thread 'emit::valu_tests::v_mad_f32_flushes_subnormals_around_omod' panicked at crates\victory-shader\src\emit\valu_tests.rs:966:9:
assertion `left == right` failed: hardware: MAD flushes subnormal sum before OMOD
  left: 2164260864
 right: 0

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

sinan-cmyk commented Nov 24, 2026 •

Copy link
Copy Markdown

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 main already 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 with main. The analysis and a verified one-line fix are in the next comment.

What I measured stays valid:

  • Offline: I merged head 8c31717 onto main ffe310e and ran victory translate --validate --list on shaders.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.
  • Tests: v_mad_f32_flushes_subnormals_around_omod returns 0x81000000 for before_cases, the same on the RTX 4070 SUPER and on WARP. The rest of victory-shader --lib passes.

AI-assisted (Claude Code).

Calvix2k added a commit to Calvix2k/VIctory that referenced this pull request Nov 24, 2026
The first case computed src0 * src1 + src2 as
1.5 * FLT_MIN * FLT_MIN - FLT_MIN, which underflows to a normal
-FLT_MIN, so mul:2 correctly returned 0x81000000 and the expected zero
could never hold. Use 1.0 as the multiplier so the sum is the intended
positive subnormal and the case actually exercises the pre-OMOD flush.

Reported in review of Marvisak#1554.

Validation:
- cargo test -p victory-shader --locked -- --test-threads=1
  --skip emit::valu_tests --skip emit::waterfall_tests
- cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings
- cargo xtask policy
- rustfmt --edition 2024 --check crates/victory-shader/src/emit/valu_tests.rs
@Calvix2k

Copy link
Copy Markdown
Contributor Author

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:

  • As several of you observed, the fixed case still passes on main on drivers that already flush fp32 subnormals on this path, so the test describes the required behaviour but cannot by itself prove the new lowering. A lower-level assertion on the emitted DXIL would be needed for that.
  • On my own Radeon RX 7800 XT the test skips: this install's D3D12 runtime reports SM 6.5 for the hardware device and 6.2 for WARP, below the SM 6.6 the harness requires, so I have no local GPU result to add.

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

Copy link
Copy Markdown
Contributor Author

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:

  • Radeon RX 7800 XT (RDNA3, driver 32.0.32029.2010), hardware backend: �_mad_f32_flushes_subnormals_around_omod passes; all four groups (�efore_cases, product_cases, input_cases, �fter_cases) are correct.
  • WARP: still skipped, it stays at SM 6.2 here even with the Agility runtime.

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

Copy link
Copy Markdown
Contributor Author

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:

  • �_mad_f32 contains at least five (bits & 0x7F80_0000) == 0 ? bits & 0x8000_0000 : bits sequences — the three inputs, the product and the sum.
  • Every exponent test has a matching sign-mask �nd and select i1.
  • �_fmac_f32 contains no flush sequence at all.

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

Copy link
Copy Markdown

The failing case is a test-vector typo, not a lowering bug

Correction to my earlier comment: I wrote that main already 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 against main.

What fails. before_cases = [[0x00c0_0000, 0x0080_0000, 0x8080_0000, 0]] in v_mad_f32_flushes_subnormals_around_omod (emit/valu_tests.rs) returns 0x81000000 on 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_f32 computes v1·v2 + v3:

  • The product 1.5·FLT_MIN·FLT_MIN ≈ 1.8e-76 underflows to 0.
  • The sum is −FLT_MIN, which is a normal number, so no flush applies.
  • OMOD mul:2 then gives −2·FLT_MIN = 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, not 0x0080_0000:

-    let before_cases = [[0x00c0_0000, 0x0080_0000, 0x8080_0000, 0]];
+    let before_cases = [[0x00c0_0000, 0x3f80_0000, 0x8080_0000, 0]];

Checks with that one-line change, merged onto main ffe310e:

  • cargo test --locked -p victory-shader --lib -- v_mad passes on the RTX 4070 SUPER, and with VICTORY_WARP=1. run_cases runs both the hardware and the WARP backend.
  • The other three cases (product, input, after OMOD) passed already.

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_f32 lowering (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

tumbubu commented Nov 24, 2026

Copy link
Copy Markdown

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_omod passes on the 7900 XT and on WARP.
  • v_mad_f32_flushes_subnormals_in_dxil ran rather than skipped, because dxc.exe sits next to the SDK's dxil.dll (10.0.26100).

AI-assisted (Claude Code); every run was on this machine.

@Calvix2k

Copy link
Copy Markdown
Contributor Author

Reworked on top of 70564ae as one commit (history rewritten):

  • Uses upstream's ftz helper for the operand/product/sum flushes instead of a private flush_f32 copy; v_fmac_f32 stays fused.
  • Keeps the corrected OMOD regression (1.0 as the multiplier) and the DXIL check that the flush sequence is emitted.
  • Note: upstream 77053a0 already flushes min/max/select and med3 denormals; this PR now covers only MAD/MAC.

Validation: v_mad_f32_flushes_subnormals_in_dxil passes, clippy -D warnings and cargo xtask policy (174 files) clean. The hardware regression passed on the RX 7800 XT before the rework and the inputs are unchanged.

@Calvix2k

Copy link
Copy Markdown
Contributor Author

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.
  • clippy -D warnings and cargo xtask policy — clean.

The rework uses the shared ftz helper and touches only the MAD/MAC arms; v_fmac_f32 stays fused.

@tumbubu

tumbubu commented Nov 24, 2026

Copy link
Copy Markdown

dce05bc on an RX 7900 XT (RDNA3): green. On this card, and on WARP, main already 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.exe 1.9.2602.17 from SDK 10.0.26100.

Run Result
dce05bc, cargo test -p victory-shader --locked -- --test-threads=1 211 passed, 0 failed, 2 ignored (main 70564ae: 209)
This PR's two tests, lowering left at 70564ae v_mad_f32_flushes_subnormals_around_omod passes on the 7900 XT and WARP. v_mad_f32_flushes_subnormals_in_dxil fails, as it should.
#1614's fuzzer on mad/mac/fma opcodes, 200 inputs, main and dce05bc the same 2 differences on both: the fused v_fma_f32/v_fmac_f32. v_mad_f32, v_mac_f32 and v_mac_legacy_f32 agree 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_dxil fails 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 on main.

AI-assisted (Claude Code); synthetic tests only, no game files.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main (conflicts in crates/victory-shader/src/emit/host.rs, emit/valu_tests.rs and gfx10/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.rs and use it as the hardware model for 0x140/0x06, keeping mad_legacy as ours under shows_where(FMA, ...).


Generated by Claude Code

@Calvix2k

Copy link
Copy Markdown
Contributor Author

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 for v_mad_legacy_f32/v_mac_legacy_f32 and for f32 denormals):

  • On gfx1010 (RDNA 1), 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.
  • On gfx1031 (RDNA 2, the family the console's compiler targets), the same 0x140 encoding decodes as v_fma_legacy_f32 and is fused, with the source flush only — the separate product/result flush does not apply.
  • v_mac_f32/v_mad_f32 are 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_f32 and 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:

  1. Narrow it to the measured case: legacy forms → fused fmad (like v_fmac_f32) with source flush; leave v_mac_f32/v_mad_f32 alone since they cannot occur on RDNA 2.
  2. Keep the change but cite a measurement of the console's gfx1013/103x behaviour for the unfused forms; I could not find one, and the GPU test here passes on main too, 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

Copy link
Copy Markdown
Contributor Author

Reworked as discussed: b634827 replaces 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_f32 and v_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 as v_fma_legacy_f32 and is fused (AnyPS5's shader-debt page, v_mad_legacy_f32/v_mac_legacy_f32 entries; gfx1031 measured there by pandather).
  • The legacy zero-factor rule is kept with the fusion: a zero factor makes the product a +0, so 0 * inf + c is c (the fused op alone would give NaN).
  • v_mac_f32/v_mad_f32 (no such opcodes on RDNA 2) and v_fmac_f32 are back to main's lowering; the branch no longer touches them, and v_madak/v_madmk are untouched.

Tests

  • GPU (legacy_mad_fuses_its_product_and_flushes_its_sources): (1 + 2^-23)(1 - 2^-23) - 1 must be the exact -2^-46 (a separate product rounds to 0), a subnormal source must read as zero, and 0 * inf + 1 must be 1. Skips here (no SM 6.6 device), runs in CI.
  • DXIL (legacy_mad_lowers_to_one_fused_op_with_flushed_sources): exactly one call float @dx.op.tertiary.f32 and three and i32 … 2139095040 flushes, using the existing emit::tests::dump_dxil (the copy is gone; I made the shared one pub(super)).

Local: both new tests pass (the DXIL one with this machine's SDK dxc.exe), clippy -D warnings clean. 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 a602e36 is still in the history and this one can be dropped — but I would not land the separate form on the RDNA 2 measurement.

@Calvix2k
Calvix2k force-pushed the codex/shader-f32-mad-ftz-043 branch from 438b016 to f5bb6d0 Compare November 25, 2026 10:09
@Calvix2k

Copy link
Copy Markdown
Contributor Author

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_sources passes: (1 + 2^-23) * (1 - 2^-23) - 1 is the exact -2^-46 (0xA8800000), a subnormal source reads as zero, and 0 * inf + 1 is 1.
  • The unfused lowering gives 0 for 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_FFFF is one ulp below 1.0, not 1 - 2^-23 — and asserted the exact -2^-46; on the hardware run it failed with the value that pair actually gives. 0x3F7F_FFFE is the intended 1 - 2^-23. With that, the GPU case is green here.

@KainanP

KainanP commented Nov 25, 2026

Copy link
Copy Markdown

On an Intel UHD (Xe-LP 8086:a78b, driver 31.0.101.5445), head f5bb6d02 merged onto main 940d32f gives victory-shader --lib 240 passed, 9 failed. That is one more failure than main's 8: legacy_mad_fuses_its_product_and_flushes_its_sources fails with hardware: case 0, left 0, right 2826960896 (0xA8800000). On the same device hosts_that_do_not_fuse_are_named reports fuses FMad: true. The test passes on an RTX 4060 Laptop. Details: #1829 (comment)

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this.

Calvix2k added a commit to Calvix2k/VIctory that referenced this pull request Nov 25, 2026
Review feedback on Marvisak#1554:

- The VOP2/VOP3 opcodes are named as the GFX10.3 tables name them:
  `v_fmac_legacy_f32` (0x06) and `v_fma_legacy_f32` (0x140), and the
  lowering's arms and comments follow. The citation is LLVM's AMDGPU GFX10.3
  opcode tables, not a project page.
- `valu_tests.rs` ends with a newline.
- The fusing probe now asks the device the same question the lowering does:
  (1 + 2^-23) * (1 - 2^-23) - 1, which is -2^-46 fused and 0 when the product
  is rounded first. KainanP's Xe-LP passes the old probe but returns 0 for
  this case, so the old probe was too weak; the legacy test skips case 0 on a
  device whose probe says it does not fuse (and on WARP), instead of failing.

Title and body updated to say what the branch does: the legacy encodings are
one fused multiply-add with flushed sources; `v_mad_f32`/`v_mac_f32` are
unchanged.
@Calvix2k
Calvix2k force-pushed the codex/shader-f32-mad-ftz-043 branch from f5bb6d0 to 7570f93 Compare November 25, 2026 16:41
@Calvix2k Calvix2k changed the title Shaders: flush subnormals in f32 MAD and MAC Shaders: the legacy MAD/MAC encodings are one fused multiply-add with flushed sources Nov 25, 2026
@Calvix2k

Copy link
Copy Markdown
Contributor Author

That Xe-LP run is the reason the probe changed, so thank you for it.

The lowering was already one tertiary.f32 per legacy MAD/MAC (the DXIL test asserts one call and three flushes), so the 0 is not the fusion being lowered away: the device reports fuses FMad: true through 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_fmad now runs exactly the legacy case, and legacy_mad_fuses_its_product_and_flushes_its_sources skips 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 named v_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_f32 unchanged), and valu_tests.rs ends with a newline.

RDNA2 flushes f32 subnormal operands, products and sums for v_mad_f32,
v_mad_legacy_f32, v_mac_f32 and v_mac_legacy_f32; v_fmac_f32 keeps the
fused behaviour. Use the upstream `ftz` helper for every flush instead
of a private copy, and keep the corrected OMOD regression plus a DXIL check
that the flush sequence is really emitted.
On RDNA 2 (the console's family) the legacy encoding decodes as
`v_fma_legacy_f32`, fused and flushing its f32 denormal sources — not the
separately rounded, product-and-result-flushing form of gfx1010 that the
previous commit modelled (AnyPS5's shader-debt page, `v_mad_legacy_f32`
and `v_mac_legacy_f32` entries).

Lower `v_mad_legacy_f32` and `v_mac_legacy_f32` to one fused multiply-add
of the flushed sources, keeping the legacy zero-factor rule (a +0 product,
so an infinite factor with a zero one does not turn the addend into NaN).
`v_mac_f32`/`v_mad_f32` (no such opcodes on RDNA 2) and `v_fmac_f32` keep
their previous lowering.

Tests: a GPU case for the fused rounding (-2^-46 kept) and the zero rule,
and a DXIL check of one `tertiary.f32` with three source flushes, reusing
the existing `dump_dxil` helper instead of a copy.
`dump_dxil` used one temp name per process, so the interpolation test and
the new legacy-mad test could overwrite each other's container when they
ran in parallel (the interpolation test then read the other module). One
name per call.

WARP rounds the product before the add for the legacy form (the case that
tells fused from separate returns 0 there), so the fusion case asserts on
the hardware only, as other gather/interp tests do.
The pair was written as (1 + 2^-23) * (1 - 2^-24): `0x3F7F_FFFF` is one
unit below 1.0 at that exponent's spacing, not 1 - 2^-23. The fused result
for that pair is +2^-24-ish, not the exact -2^-46 the case asserts; on the
hardware test it failed. `0x3F7F_FFFE` is 1 - 2^-23, whose fused product
minus one is exactly -2^-46 and whose separate product rounds to 1.0.
Review feedback on Marvisak#1554:

- The VOP2/VOP3 opcodes are named as the GFX10.3 tables name them:
  `v_fmac_legacy_f32` (0x06) and `v_fma_legacy_f32` (0x140), and the
  lowering's arms and comments follow. The citation is LLVM's AMDGPU GFX10.3
  opcode tables, not a project page.
- `valu_tests.rs` ends with a newline.
- The fusing probe now asks the device the same question the lowering does:
  (1 + 2^-23) * (1 - 2^-23) - 1, which is -2^-46 fused and 0 when the product
  is rounded first. KainanP's Xe-LP passes the old probe but returns 0 for
  this case, so the old probe was too weak; the legacy test skips case 0 on a
  device whose probe says it does not fuse (and on WARP), instead of failing.

Title and body updated to say what the branch does: the legacy encodings are
one fused multiply-add with flushed sources; `v_mad_f32`/`v_mac_f32` are
unchanged.
@Calvix2k
Calvix2k force-pushed the codex/shader-f32-mad-ftz-043 branch from 7570f93 to aee0d47 Compare November 26, 2026 13:53
@ColorMeBoom

Copy link
Copy Markdown

RDNA1 run (AMD RX 5700 XT, Navi 10, driver 32.0.21045.29003, Windows 11 26200; from the batch in #3410). Head aee0d47 merged onto main 63bcca4. Three findings:

1. Two of the new tests don't run at this head. Since f339de8, v_mad_f32_flushes_subnormals_around_omod and legacy_mad_fuses_its_product_and_flushes_its_sources have no #[test]. The first also stops after defining its inputs: the four run_cases calls from 8a3d629 are gone. That is CI's check failure (8 unused/never-used errors under clippy -D warnings). For the runs below, I restored both attributes and the four calls from 8a3d629 unchanged.

2. On the RX 5700 XT, arithmetic_matches_the_hardware fails for the legacy FMA/FMAC with the new lowering, on the hardware adapter only. It passes on main.

v_mov_b32 v5, v3; v_fmac_legacy_f32 v5, v1, v2: 14440 inputs, model mismatches ["hardware 420", "warp 0"]
  hardware: in [00000000, 00000000, 80000000, 0badf00d] want [00000000] got [80000000]
v_fma_legacy_f32 v5, v1, v2, v3: 14440 inputs, model mismatches ["hardware 420", "warp 0"]

A zero factor with a -0 addend should give +0 + -0 = +0, but the card returns -0. legacy_fmad builds that branch as fadd(0.0, c) with a constant +0.0. The AMD driver appears to fold this to c, a fold that ignores signed zeros, and WARP doesn't fold it. Zeroing the factors with select and keeping the single tertiary.f32 leaves the driver no constant add to fold:

     fn legacy_fmad(&mut self, a: V, b: V, c: V) -> V {
         let cur = self.cur;
-        let fused = self.fmad(a, b, c);
         let zero = self.s.m.f32(0);
         let az = self.s.m.cmp(cur, pred::OEQ, a, zero);
         let bz = self.s.m.cmp(cur, pred::OEQ, b, zero);
         let z = self.s.m.bin(cur, Bin::Or, az, bz);
-        // +0 + c, so the sign of a zero addend follows the addition.
-        let addend = self.s.m.bin(cur, Bin::Add, zero, c);
-        self.s.m.select(cur, z, addend, fused)
+        // A zero factor zeroes both, so the product is +0 * +0 and the add
+        // happens at run time: no constant `0.0 + c` for a driver to fold.
+        let a = self.s.m.select(cur, z, zero, a);
+        let b = self.s.m.select(cur, z, zero, b);
+        self.fmad(a, b, c)
     }

With this change and the restored tests, the full victory-shader --lib suite passes on the RX 5700 XT and WARP: 305 passed, 0 failed, 3 ignored. Clippy -D warnings is clean. Both restored tests pass with either lowering. I haven't run the change on another vendor.

3. RDNA1 does not fuse FMad here. With the restored tests, fuses_fmad() reports false on the RX 5700 XT, so legacy_mad_fuses_its_product_and_flushes_its_sources skips case 0 on hardware, as it does on WARP and the Xe-LP. The other two cases pass. So on this card the legacy forms aren't fused, even with one tertiary.f32.

AI use: Claude Code (Claude Opus 5.5).

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

Thanks @ColorMeBoom for the RX 5700 XT run and the factor-select fix. 1f86690 applies it directly to aee0d47 and credits your contribution.

I restored the legacy GPU test for both FMA and FMAC, including positive and negative zero factors with a negative-zero addend. I removed the incomplete non-legacy OMOD fixture left by the earlier scope change. The DXIL test now also checks that zero products go through the multiply-add. It found one standalone fadd on aee0d47 and zero with the fix, while both versions emit one tertiary.f32.

On app-local WARP 1.0.22 the two legacy tests passed, and the arithmetic matrix had zero mismatches across 14,440 inputs per legacy instruction. The focused fusion-only case skipped on this runtime by design. The full shader suite had 299 passes, 3 ignored and the same five pre-existing failures as the local reference. Strict shader Clippy and asset policy passed.

@Calvix2k the commit is ready to cherry-pick. Please retain the Git author and ColorMeBoom's co-author trailer. If folding it in, please also add:

Co-authored-by: andrewpozdnakov7-jpg andrewpozdnakov7-jpg@users.noreply.github.com

If you have another RX 5700 XT slot, a rerun of arithmetic_matches_the_hardware on this exact commit would confirm the final version on that driver. AI-assisted with Codex. Synthetic shaders only.

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.