Shaders: RDNA2's legacy multiply-adds are the fused ones #284
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/284"
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?
Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T02:02:55Z.
What's wrong
The opcode tables in
gfx10/opcodes.rsare generated from the GFX10 column of Mesa'saco_opcodes.py. That column shares opcode numbers between GFX10.1 and GFX10.3 (RDNA2), and for two of them it keeps the GFX10.1 name:0x006(and VOP30x106)v_mac_legacy_f32v_fmac_legacy_f320x140v_mad_legacy_f32v_fma_legacy_f32On RDNA2 these are fused multiply-adds: the product isn't rounded before the add. They keep the D3D9 rule that a zero factor gives a zero product, even against infinity or NaN. References:
V_FMAC_LEGACY_F32as VOP2 6 andV_FMA_LEGACY_F32as VOP3 320.V_FMAC_LEGACY_F32 : VOP2_Real_gfx10<0x006>andV_FMA_LEGACY_F32 : VOP3_Real_gfx10<0x140>. Its gfx1030 assembler tests (gfx1030_new.s) encodev_fma_legacy_f32 v0, v1, v2, v3as00 00 40 d5 01 05 0e 04andv_fmac_legacy_f32_e32 v0, v1, v2as01 05 00 0c, andllvm-mc -mcpu=gfx1030has nov_mad_f32there at all.v_mad_legacy_f32is replaced byv_fma_legacy_f32on GFX10.3.The decoder already treats the console as GFX10.3: it handles GFX10.3's ray-tracing instructions (
image_bvh_intersect_ray).lower.rslowered the GFX10.1 forms: a legacy multiply (rounded) followed by a separate add. That gives a result rounded twice, and theselectaround the multiply also stops the driver from contracting the two. For example,v1 = v2 = 0x3F800001andv3 = 0xBF800002give0x28800000(2⁻⁴⁶) fused on RDNA2, and0with the multiply rounded first.What the fix does
opcodes.rsnames the two opcodes as RDNA2 does:v_fmac_legacy_f32andv_fma_legacy_f32.lower.rslowers both with a new helper,fma_legacy(a, b, c):c(as0.0 + c) whenaorbis ±0, otherwise oneFMad(a, b, c). This is the same "the driver may fuse it" shortcut thatv_fma_f32andv_fmac_f32already use, since DXIL has no fused f32 FMA.v_mad_f32/v_madak_f32/v_madmk_f32arm stays as it was, minus the legacy name.How it behaves after the fix
Shaders using these opcodes translate as fused multiply-adds with the D3D9 zero rule, on hosts whose driver fuses
FMad. Their results are then RDNA2's, bit for bit. A host that doesn't fuseFMadrounds twice, as it already does forv_fma_f32. I measured that on GitHub's runner: both its WARP device and its hardware device return0for the example above, with or without this change.Tests
legacy_multiply_adds_are_the_fused_ones(gfx10/mod.rs) decodes LLVM's two gfx1030 encodings above and checks the names.v_fma_legacy_f32_follows_the_d3d9_zero_rule(valu_tests.rs) runsv_fma_legacy_f32(VOP30x140) on the host GPU and on WARP, with three cases:2 × 3 + 1 = 7;0 × ∞ + 1.5 = 1.5;NaN × 0 − 2 = −2.I ran
cargo test --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml:victory-shaderhas 71 passed, the other crates have 0 failed.main: the decoder test fails:left: "v_mad_legacy_f32",right: "v_fma_legacy_f32". The zero-rule test passes onmaintoo, since the old lowering also kept that rule.--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244).Not tested: a GPU that fuses
FMad, so the fused rounding itself isn't shown by a test here, and a title shader that uses these opcodes.Related: #200 adds match arms next to the ones this edits in
lower.rs. Whichever merges second needs a trivial rebase.Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:16:09Z.
Tested PR #284 at head
fa5189da66a91611c41fc1bdafd3675e15eb50f1(state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged atfa5189da66a91611c41fc1bdafd3675e15eb50f1.cargo test --locked -p victory-shader fma_legacy— passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 70 filtered out; finished in 0.61s ); cases: emit::valu_tests::v_fma_legacy_f32_follows_the_d3d9_zero_rule.Behavior covered: Checks legacy multiply-add lowering follows fused RDNA2/D3D9 behavior, including the signed-zero case.
Code: crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/valu_tests.rs
Imported from Marvisak/VIctory. Originally posted by marquezinii on 2026-11-24T05:15:51Z.
Ran
fa5189dmerged ontomaind2a4bae(clean) on NVIDIA.valu_testsruns each case on the hardware device and on WARP.x86_64-pc-windows-msvc, debugv_fma_legacy_f32_follows_the_d3d9_zero_rule: ok on bothcargo test -p victory-shader --lib -- --test-threads=1: 71 passed, 0 failedcargo test --workspace -- --test-threads=1: 266 passed, 0 failed, 4 ignoredThe test checks only the zero rule, so I also ran the rounding example from the description (
v1 = v2 = 0x3F800001,v3 = 0xBF800002) locally throughv_fma_legacy_f32, with a probe added tovalu_tests(not committed):maind2a4bae0x00x288000000x00x0So NVIDIA's driver contracts the
fmadand gives the RDNA2 result with this change, while WARP does not fuse it. That is probably also why the case can't go into the test as an exact value: it would fail on WARP.Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T05:55:44Z.
RDNA2 hardware measurement of both claims, on GFX10.3 silicon:
v_fma_legacy_f32andv_fmac_legacy_f32are fused, and keep the zero-factor rule.Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (GFX10.3 / RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr, which accepts both mnemonics for gfx1036) runs
v_fma_legacy_f32 v10, v4, v5, v6andv_fmac_legacy_f32 v11, v4, v5(v11 = src2), withv_fma_f32andv_mul_legacy_f32alongside for reference. There are 2035 operand triples: 17 special values (±0, ±1, ±inf, ±NaN, max, min normal, the smallest denormal,0x3f800001,0xbf800002, ±1e30, 3, 0.1) in all pairs × six src2 values, plus 300 triples where src2 is minus the product rounded, so a fused result is the rounding error and an unfused one is about 0. Default mode (IEEE on, f32 denormals flushed). RDNA2 desktop silicon, not the console's. No game data.v_fma_legacy_f32v_fmac_legacy_f32v_fma_f320x3f8000010x3f8000010xbf8000020x288000000x288000000x288000000x000000000x7f8000000x3f8000000x3f8000000x3f8000000xffc000000x7f8000000x000000000x3f8000000x3f8000000x3f8000000xffc000000x800000000x7f8000000x3f8000000x3f8000000x3f8000000xffc000000x000000000x7fc000000x3f8000000x3f8000000x3f8000000x7fc000000x000000000x7f8000000x800000000x000000000x000000000xffc000000x000000000x7f8000000x7fc000000x7fc000000x7fc000000xffc000000x7f8000000x3f8000000x3f8000000x7f8000000x7f8000000x7f800000Model fit over all 2035 triples, for both opcodes:
a*b + c), with a zero factor giving a +0 product even against inf or NaN: 2035/2035.v_mad_legacy_f32/v_mac_legacy_f32lowering onmain): 1716/2035. It is wrong on your0x28800000example, which gives 0, and on all 300 residual triples.So the GFX10.3 names and the fused lowering in this PR match the hardware. A zero factor makes the product +0:
0 × inf + (−0)is+0, and0 × NaN + cisc.AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:50:07Z.
A follow-up on the GFX10.1-vs-GFX10.3 naming this PR fixes for
0x006/0x140: four more entries of the table are GFX10.1 opcodes that RDNA2 does not implement, andmainlowers them as real multiply-adds.Two checks on the gfx1036 iGPU from my comment above:
gfx1036. "Instruction not supported on this GPU" came back for exactly these:main0xdeadbeef)0x006v_mac_legacy_f32v_fmac_legacy_f32(this PR)0x140v_mad_legacy_f32v_fma_legacy_f32(this PR)0x01fv_mac_f3200x020v_madmk_f32(+ literal)00x021v_madak_f32(+ literal)00x141v_mad_f3200x16bv_mul_lo_i3202·3+5,1.5·(-4)+0.25,1·1+1) give0x00000000every time, whilev_fmac_f32,v_fmamk_f32andv_fmaak_f32in the same kernel give 11, 23, 16, …. A differential run ofmain's lowering against the hardware (220 inputs) shows the same for VOP30x141: the host computes a mad, the hardware writes 0 on all but the rows where the mad is 0 anyway.If the console is GFX10.3, as the ray-tracing opcodes suggest, its compiler can't emit these five, so translating them as mads is harmless but wrong as a model. Refusing them, or naming them as not implemented, would match the hardware. I didn't touch the table myself, since this PR is already reworking those two names.
AI use: Claude Code (Claude Opus 5.5) ran the probes on the GPU above and drafted this comment; the numbers come from the hardware run.
Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:59:11Z.
One more RDNA2 rule for these two opcodes, which the fused lowering here doesn't have yet:
v_fma_legacy_f32andv_fmac_legacy_f32flush f32 denormals, inputs and result, whatever the FP32 denormal mode.v_fma_f32andv_mul_legacy_f32follow the mode.Same gfx1036 setup as above. The 2035 triples from my first comment plus 15 with denormal operands or a denormal exact result, run under both f32 denormal modes:
v_fma_f32in the same kernelExamples in mode 3 (denormals kept):
v_fma_legacy_f32v_fma_f32v_mul_legacy_f32(a·b)03.00x000001000x000000000x000001000x000001002.000x000000000x000002000x000002002^-632^-6400x000000000x004000000x00400000What it does end to end: I ran a differential check of the translator against the hardware (the same raw VOP3 words through
build_valu_cson a host GPU, 220 inputs). On the iGPU as host, VOP30x140differs from the hardware in 25 of 220 cases onmainand in 52 with this PR. The 27 extra are all denormal operands that the host'sfmakeeps and the hardware flushes, plus one sNaN addend that the hardware quiets in IEEE mode. Flushing the three inputs and the result, for example by selecting ±0 where the exponent field is 0, would make the PR's lowering match on every row. With that, the earlier example (0x28800000) and the zero-factor rule are unchanged.AI use: Claude Code (Claude Opus 5.5) ran the probes and the differential check on the GPUs above and drafted this comment; the numbers come from those runs.
Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T07:35:14Z.
AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and the Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the D3D12 device for
cargo test -p victory-shader --release(local adapter-selection patch, not part of this PR).Whole
victory-shadersuite: 71 passed, 0 failed on both GPUs.Negative control: with
lower.rs/gfx10reverted to main,v_fma_legacy_f32_follows_the_d3d9_zero_rulestill passes on both GPUs: its three cases give the same result fused or unfused, so the test does not see the fusion.I added locally a rounding-sensitive case,
a = b = 0x3F800800(1+2^-12),c = 0xBF801000(-(1+2^-11)): fused gives0x33800000(2^-24), a rounded product gives 0.0x33800000on the RX 9070 and on the RDNA2 iGPU (the driver fuses the FMad), but 0 on WARP;So the PR does fix the fused result on AMD hardware. A case like this one would make the test catch a regression, but it fails on WARP because WARP does not fuse FMad (the
shortcut:comment), so it would need the hardware backend only or an explicit fma.Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T09:43:27Z.
Review: APPROVED
Tested
fa5189don Windows 11, RTX 5070, MSVC: the workspace passes (195 outsidevictory-shader;victory-shader --libsingle-threaded: 71, includingv_fma_legacy_f32_follows_the_d3d9_zero_ruleon hardware and WARP, and the decode test).victory translate <title exe> --validate --pipelinesgives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines.V_FMAC_LEGACY_F32and VOP3 0x140 isV_FMA_LEGACY_F32, as the RDNA2 ISA and LLVM's gfx1030 tables say. The decode test uses LLVM's own encodings, which settles it.fma_legacykeeps the D3D9 rule (either factor exactly ±0, so the product is 0, even against inf or NaN), and0 + addendgives the IEEE sign of the sum. The fused path goes throughfmad, the same "driver may fuse" shortcutv_fma_f32already uses, which the comment notes.v_mad_f32/v_madak/v_madmkno longer share the arm with the legacy op, so they don't pick up the zero rule.No issues found.
AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:29:16Z.
Automated triage for the maintainers.
Needs rebase. This PR no longer merges cleanly into
main(conflict incrates/victory-shader/src/gfx10/opcodes.rs). It will be reviewed again once it is rebased.From the earlier needs-work note, still open: model 0x06/0x140 as fused legacy in
semantics_tests.rs, flush f32 denormal inputs and result infma_legacy, and add a test case that depends on fusion.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-25T17:20:46Z.
Test commit for the triage's point 3 (and rows for point 2):
df8ac2eon branchev-284-testsof upipibi/VIctory, on top offa5189d, one file (emit/valu_tests.rs, +82). It also cherry-picks cleanly ontomain67e8ae1.v_fma_legacy_f32_rounds_once_on_hardware: VOP3 0x140 and VOP2 0x006,0x3F800001·0x3F800001 + 0xBF800002 → 0x28800000and0x3F800800·0x3F800800 + 0xBF801000 → 0x33800000. It runs on the hardware backend only, since WARP doesn't fuse the FMad.v_fma_legacy_f32_flushes_denormals: the three mode-3 rows from @Lyrnc's gfx1036 table (comment), on both backends.RTX 3070, driver 616.56, Windows 11:
67e8ae1fa5189d0x00000000: fail0x28800000/0x33800000: pass0x00000000: pass0x00000000: passSo the fusion test fails on main and passes on the PR. The denormal rows pass on both here, because the RTX 3070 and WARP flush f32 denormals in this shader themselves. They don't show point 2 on NVIDIA; they would only catch a missing flush on a host that keeps denormals (as in Lyrnc's AMD-host differential run). Point 1 (
semantics_tests.rs) isn't touched.AI-assisted (Claude Code, Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-28T02:32:26Z.
A measured AMD result for the zero-product path here (read from another tree; not run here).
fma_legacyforms the zero-factor result asproduct_zero = fadd(+0, addend), then selects it. TaynanGT/Vice-Tay#308 found that form returning -0 on an RX 6900 XT (RDNA2, driver 32.0.21045.29003) for0 × -1 + (-0), where the legacy rule (and IEEE+0 + -0under round-to-nearest) gives +0. Their hardware testlegacy_multiply_adds_fuse_their_product_and_flush_their_sourcesfailed on that card's backend and passed on WARP and in CI. That fits the driver folding0 + xtox.Their fix selects the +0 bit pattern when the flushed accumulator is -0, and leaves NaN, infinity, flushing and the nonzero (fused) path as they were. It changed 162 of the title's 25,790 cached translations. If this PR is revived, the same select (or an integer-side
x == -0 ? +0 : xon the addend) would keep the zero-factor result from depending on that fold.mainhas the same shape outside this PR.v_mad_legacy_f32adds the accumulator tolegacy_mul's result, which is +0 for a zero factor (lower.rs:2506-2523,:6284-6292at63bcca4). So the same fold would give -0 there too, though no test onmainchecks the -0 accumulator case.AI-assisted (Claude Code, Claude Opus 5.5 read this diff and Vice-Tay#308).
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T02:50:32Z.
There is an upstream-side reference for the signed-zero case in #1554.
1f86690handles the zero-product result explicitly. ColorMeBoom's RX 5700 XT retest reports 420 mismatches per legacy opcode before the fix and zero after, across 14,440 generated inputs per opcode. The device still skipped the fused-rounding probe.The separate
div:2and clamp check on the same card covers the result-modifier path in1a96aa6. Both reports are for the #1554 branches. They give the next rebase a patch and a hardware-checked test case for the issue raised here.Please keep the Git authors on cherry-pick or rebase, or add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>if folding my contribution into another commit.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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.