shader: keep a V_MUL_F32 product from being fused into a following add #1622
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1622
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1622"
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?
@Nawid3333 on GitHub · 2026-10-17 21:48 UTC · original
Nawid3333:shader/f32-mul-no-contraction→main· +113 −1 · 4 files · 3 commits · openWhat is wrong
On RDNA,
V_MUL_F32rounds its result, and a laterV_ADD_F32/V_SUB_F32that uses it rounds again. Kyty emits them asOpFMulandOpFAdd/OpFSubwithoutNoContraction, and in SPIR-V that lets the driver fuse the two into one multiply-add (FMA) that rounds only once. LLPC (the shader compiler in AMD's Vulkan driver for my RX 9070 XT) does that. Example:v_mul_f32 v6, v0, v1thenv_sub_f32 v7, v6, v2with v0 =0x3f800001, v1 =0x3f7ffffe, v2 = 1.0. The exact product is 1 - 2^-46; a separate multiply rounds it to 1.0, so the subtract gives 0. The driver instead emits onev_fmac_f32and the result is0xa8800000(-2^-46). AMD's other shader compiler, which the driver uses for my Ryzen 7600X iGPU, keepsv_mul_f32+v_subrev_f32for the same module, so today the result depends on the driver. Whether a driver fuses also depends on how the product is used (in a small test it fused a product used once, but not one used twice), so the same guest code can round differently in two shaders.What changes
An f32
OpFAdd/OpFSubthat uses the result of an f32OpFMul, directly or throughOpFNegate,OpBitcastorOpSelect, getsNoContraction, which forbids fusing it with another operation. The bitcasts and selects matter because a VGPR write without DPP masks isSelect(exec, value, old)(Translator::WriteRawU32) and an f32 result is written as its bits: afters_and_saveexec, an EXEC write, with dispatch thread bounds, or when a wave64 shader runs in two halves on a 32-lane host, the product reaches the add asBitcast(Select(Bitcast(product))). Adds with no product operand are unchanged, and the multiplies get no new decoration. Legacy MAD (EmitFPMad32) builds its product with the same f32OpFMul, so its sum now gets the decoration from the new rule and its own line for the sum is removed (spirv-val rejects the same decoration twice). Its existing decoration on the product stays.Why only those adds
Most multiply-adds in game shaders are legacy MADs, which Kyty already emits with
NoContraction: the Wukong compute shader from #1072 has 641V_MAC_F32/V_MAD_F32and 11V_FMA_F32. This extends the same rule to aV_MUL_F32whose result goes into a separate add or subtract. shadPS4 decorates every f32 add, sub, mul, div and fma (emit_spirv_floating_point.cpp), and decorating every f32 multiply would fix this as well. But on macOS SPIRV-Cross turns eachNoContractionoperation into a call to an[[clang::optnone]]helper (see #1521). In that Wukong shader 80 of 286 f32 adds/subs use a product (16 directly or through a negation, 64 through an EXEC select), so this change decorates 80 instructions there instead of 852 multiplies. LLVM builds FMAs fromfadd(fmul)and its negated forms within one basic block (visitFADDForFMACombine). Through the EXEC select the RX 9070 XT's driver does fuse (see Tests), so the change follows bitcasts and selects too; a product that reaches an add through a phi or an abs is not followed. If you'd rather decorate every multiply, it's a short change that also drops the tracking; I ran the full test suite with that version on main as well, natively and with 32-wide subgroups, with the same results as this one.Cost: on that Wukong shader, with today's drivers, the RX 9070 XT's ISA (the instructions the driver generates) stays at 39,483 instructions (FMAs from 1,342 to 1,340, the same as with only the 16 direct cases); the iGPU's is unchanged (38,117).
This touches the same lines of
EmitFPMad32as #1619; whichever goes in first, I'll rebase the other.Tests
VectorMulThenSubF32RoundsProduct: the example above, and-(a*b) + cwith a, b and c also loaded from a buffer (so the driver can't fold or share them); both must be 0. Before the change the RX 9070 XT returns0xa8800000and0x28800000; after it both are 0 on both GPUs. The iGPU doesn't fuse either way. With the harness forced to 32-wide subgroups (a local switch, not in this PR) the wave64 test runs in two halves and each product goes through the EXECOpSelect; there the RX 9070 XT (driver 26.10.1) fused as well (0xa8800000/0x28800000with only the direct rule) and gives 0 with this change.shader_cfg_testscaseTestNewShaderRecompilerF32ProductAddNoContraction: a product as the first and as the second operand of a subtract, a negated product in an add, and an add of two non-products. It checks that exactly the three adds/subs of products areNoContractionand the multiplies and the plain add aren't. It fails before the change and passes after it. NewTestNewShaderRecompilerF32ProductAddUnderExecNoContraction:v_cmp_gt_f32ands_and_saveexec_b64before the multiply, so the product reaches the subtract through the EXEC select; it checks that the select is there, that the subtract isNoContractionand a plain add isn't. It fails without the bitcast/select rule and passes with it.shader_cfg_testsisn't part of CI; I ran it on my PC.763d3be3(the base of this PR): every other compute test and the 30 graphics tests keep their old results, and the new test gives no validation-layer messages. After merging main (c67352d5), on the RX 9070 XT compared with main, natively and 32-wide, only the new test changes; on the iGPU the select rule changes no result.a0b083c8(head8ac4d1ef: the same change, without its code comments). On the RX 9070 XT, compared with main:shader_cfg_testspasses, and of 354 compute cases onlyVectorMulThenSubF32RoundsProductchanges (main returns0xa8800000and0x28800000, this PR 0 and 0); 5 other cases fail on both. CI on this head passes on Windows, Linux and macOS (run 45045265594).If you'd like this done differently, tell me and I'll change it.
Written with Claude Code (AI): it found this with a test, checked the drivers' ISA, wrote the change and ran the tests on my PC (Windows 11, AMD drivers 26.8.1 and 26.10.1).
🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-10-17 21:55 UTC · original · edited 2026-11-22 16:48 UTC
No actionable comments were generated in the recent review.
Walkthrough
The SPIR-V emitter tracks F32 multiplication results through qualifying negation, bitcast, and select operations. It adds
NoContractionto F32 additions and subtractions that use a tracked result.EmitFPMad32no longer decorates its sum. New tests check computed results and emitted SPIR-V decorations.Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Minimal · up to
8ac4dThe change keeps RDNA's separate rounding for an f32 multiply followed by an add or subtract, by marking those adds and subtracts NoContraction. It does not alter other arithmetic. No concrete defect was found. The new shader_cfg_tests cases are not part of CI, so they rely on the author's local runs.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: an f32 multiply product is kept from being fused into a following add. The changeset adds NoContraction to f32 add/sub operations that consume an f32 multiply result, which matches the title.
Full details: Description check
Explanation
The description explains the problem (V_MUL_F32 followed by V_ADD_F32/V_SUB_F32 being fused into one FMA by the SPIR-V driver), the rule that adds product operands get NoContraction, the EmitFPMad32 duplicate-decoration removal, the new tests, and the test results. It directly describes the changes in the changeset.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
5cd7ae4e-6297-441d-bec9-1edaf6542e1bReviewing files that changed from the base of the PR and between 4ad7f8794b5683481d2d101aae7449a56879c502 and
8ac4d1efb0.src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.htests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
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.