docs(debt): record the v_mad_f32 family as measured on gfx1010 #5782
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5782"
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 boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-14T10:30:13Z.
What
Docs only, as agreed in review.
v_mad_f32,v_mac_f32,v_madak_f32andv_madmk_f32exist only on gfx10.1, so the RDNA2 oracle can't measure them. A new entry indocs/dev/debt/shader.mdrecords what they do on gfx1010 and states that the translation leaves it to the driver:v_mul_f32;The entry also gives the cost of modelling it, so the lowering can follow once the SPIR-V float controls of #981 (step 4) flush in hardware. On a dependent
v_mac_f32chain, the exact lowering cost about 15×mainon the RX 5700 XT and 23× on an RTX 3070 (@jotitadev's numbers). A never-taken branch around it still cost about 6× on the RX 5700 XT.The lowering,
MadF32.cppand theVectorFusedMultiplyAdd.cppchange are dropped from this PR. The probe, the Windows runner, the raw results (gist) and the cost measurements are in the comments for the follow-up.Source of the semantics: measured on hardware, an AMD Radeon RX 5700 XT (gfx1010), with
tools/hw-oracle/template.sassembled by the AMD Windows driver'samd_comgr_2.dlland launched through itsamdhip64_6.dllinstead oforacle.cand ROCr. 256 rows × 9 encodings, wave32 at IEEE 0 and 1, f32 denormal modes 0 to 3 and round modes 0 to 3, and wave64 at round mode 0: one model matches every non-NaN result, and all 5408 NaN results follow the rule above.Tested
Documentation only, no code change.
tools/check_conventions.py --base origin/mainpasses with no findings (the new entry is 789 characters), andtools/pr_overlap.py --branch feat/shader-mad-f32-gfx1010reports no overlapping change; it lists 22 open pull requests that also editdocs/dev/debt/shader.mdas not compared, and this entry is a new line of its own.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-14T10:30:22Z.
Measurement for this PR: probe, runner and a summary. The full raw results (256 rows × 9 encodings × 40 runs, 1.3 MB of JSON) are in https://gist.github.com/SirDarky/82bd882cb26b8cf504ab08079a352421.
Models consistent with every non-NaN result in all 40 runs (wave32: IEEE 0/1 × f32 denormal modes 0-3 × f32 round modes 0-3; wave64: the same at round mode 0).
rounded= product rounded to f32 before the add,in/product/out= flushed to a zero of the same sign,(unbounded)= the product counts as tiny when it is below 2^-126 after rounding to 24 bits with an unbounded exponent:The
v_madmk_f32columns separate fewer models, since K is fixed. All other columns leave onlyrounded+in+product(unbounded)+out(the forms whose results are never denormal before the output flush also allow the variant without the output flush). NaN results: 5408 of 5408 matchnanResultF32's fma rule with an invalid product beating a NaN src2. 5048 match if a NaN src2 is assumed to win instead.mad_f32_probe.py
hw_oracle_win.py (template.s through amd_comgr + HIP, same interface as hw_oracle.py)
Imported from boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-14T10:30:24Z.
Raw results in the mode the execution test uses: wave32, IEEE=0, f32 denormal mode 0, round mode 0. Each line is
src0 src1 src2and then the nine encodings in this order:v_mad_f32,v_mad_f32 -src0,v_mad_f32 |src0| -src2,v_mac_f32,v_mac_f32_e64 -src0 |src1|,v_madak_f32 K=-0,v_madak_f32 K=0x00400001,v_madmk_f32 K=1.5,v_madmk_f32 K=0x00400000. Rows 0-55 are chosen, rows 56-255 random (seed 1010);MadF32.cpppins rows 0-63.256 rows
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T17:18:31Z.
Run on an Apple GPU. The PR lists only the RX 5700 XT, and
agc_driver_mad_f32fails here.GPUs tested: Apple M2 Max, MoltenVK 1.4.2 (Vulkan 1.4.357), macOS 27.2, x86-64 under Rosetta
maindoes not run the GPU tests on macOS yet, so I tested onmacos-portatb7eb6b6b(mugurc/AnyPS5) with this PR's diff applied, not onmain. Release build, LunarG Vulkan SDK 1.4.363.0. The suite isctest -R agc -E 'flip|display|lds_atomics64' -j1 --timeout 30. Without the PR, 22 tests fail on this branch, all of them known macOS failures listed in #1. Head8c80fa90f. The diff applies cleanly, exceptdocs/.agc_driver_mad_f32. No other test changes.vector_fused_multiply_addpasses.agc_driver_mad_f32fails: I changed theRequireinChecklocally to print every mismatch. Each of the four float-mode runs has the same 19 mismatches, 76 in all. Every wrong result is0x00000000, of two kinds:v_mad_f32 (0x80000000, 1.0, -0),v_madak_f32 K=-0with a -0 or flushed negative product,v_mac_f32_e64 -src0 |src1|,v_mad_f32 -src0. This matches the MoltenVK issue described in #1: SPIRV-Cross lowers a precise multiply tofma(l, r, 0), which gives +0 for a -0 product (KhronosGroup/SPIRV-Cross#2704).agc_driver_sdwa_float_selectorsfails on macOS for the same reason. Confirmed since: with MoltenVK built against SPIRV-Cross300681bb(#2704), all 40 signed-zero mismatches go away and only the 36 tininess ones remain (details in #1).0x3f7ffffe * 0x00800001, the case in the description, gives0where0x00800000(or0x80800000with-src0) is expected. That coversv_mad_f32,v_mad_f32 -src0,|src0| -src2,v_mac_f32,v_mac_f32_e64, andv_madak_f32with both K values. On this deviceshaderDenormFlushToZeroFloat32is false anddenormBehaviorIndependenceis NONE.agc_driver_f32_denormal_flushandagc_driver_f32_constant_factor_tininessalready fail on this branch without the PR, so this may be the same device behaviour and not something this PR introduced.So on MoltenVK the new test fails on device float behaviour (signed zero and tininess) that existing f32 tests already show there. It is not a regression in other tests. If macOS runs these tests in CI,
agc_driver_mad_f32will need the same handling as those tests.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T19:41:17Z.
Review: COMMENTED
The gfx1010 measurement is careful, and the semantics it establishes look right. The lowering, though, has a cost that the description doesn't mention, and I don't think it can merge as is.
The description says these instructions are common in console shaders: LLVM selects
v_mac_f32/v_mad_f32for gfx10.1 in the 0xc0 float mode most titles use. In many PS5 pixel and compute shaders they are the most frequent ALU instruction. Onmain, each one is a singleFPMad32. With this change, every one becomes:FPMul32, thenflushTinyProducton it (in round-to-nearest, a second multiply by 2^64, a compare and a select);and, a sign-bitandand a select for the zero sum, thenFPAdd32;nanResultF32over three sources plusinvalidProductF32(NaN and infinity tests, quieting and a chain of selects);By my count that is roughly 20–30 ALU operations where there was one, all evaluated with selects, so every lane pays all of it on every mad, even though NaN, denormal and signed-zero-sum inputs are vanishingly rare in real data. It also grows the SPIR-V of every shader that uses mad, which adds pipeline compile time. #4287 is open right now precisely because the same
nanResultF32chain on the fma forms costs measurable GPU time, and the maintainers asked there for frame-time numbers before accepting even a reduction.Exactness for these cases is fine to want, but putting all of it on the hot path is the wrong design. Options:
perf-sensitive change to the most common ALU op needs the SPIR-V size of a real title's shader set, and summary lines from one ALU-heavy title before and after (warm cache, noAPS5_PROFILE_*). Demon's Souls has the most f32 math in Raghav314's census on #4287.As it stands, this trades a measurable slowdown in nearly every shader for bit-exactness in inputs that almost never occur.
Imported from boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-15T07:02:03Z.
Thanks, that's a fair point, so I measured it. Synthetic compute chain on the RX 5700 XT (Windows, Adrenalin),
FLOAT_MODE0xc0, 64-lane workgroups, 65535 groups, 512 dependent operations per lane, median of 16 dispatches, the same in three runs:v_mac_f32×512,mainv_mac_f32×512, this PRv_fma_f32×512,mainv_mul_f32+v_add_f32×256,mainIn isolation the mad forms get about 10× slower, which is what the fma forms already cost on
mainwith the samenanResultF32/flushTinyProductlowering. This isn't frame time (I can't run titles), so it gives the scale of the cost, not its effect on a frame.@adriadam10, which would you prefer?
perf(PR (the #4287 / #4539 line).@jotitadev, thanks for running it on MoltenVK. Both causes match the known MoltenVK f32 failures in #1 (the signed zero from SPIRV-Cross's precise multiply, and the tininess check), so I'd handle
agc_driver_mad_f32the same way asagc_driver_f32_denormal_flushandagc_driver_f32_constant_factor_tininessonce the macOS GPU tests run.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:09:38Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
docs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mddocs/dev/debt/shader.mdImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T12:58:26Z.
Review: CHANGES_REQUESTED
Thanks for the measurement and the cost numbers. On the two options: please go with option 2.
main,FPMad32is already an unfused multiply then add (NoContraction), so normal inputs are already exact. This PR changes only the edge cases (denormal sources/product/result, NaN bits, the sign of a zero sum). Your chain measured ~10× for that (0.78 → 8.2 ms, 13k → 165k SPIR-V words) on the most frequent ALU op (v_mac_f32+v_mad_f32are about 6% of Hades II's instructions in the #4287 counts). The fma forms already pay that cost, but they need the emulation even for normal inputs. These don't.nanResultF32cheaper and has been waiting days for frame-time numbers, so "exact now, fast later" would leave every shader slower for an open-ended time.nanResultF32/invalidProductF32) only inside a branch on it. A dedicated IR op lowered in the SPIR-V backend can hold the branch, since the backend already emits structured conditional blocks.MadF32.cppalready covers both paths. Please keep it as is, and add a row where only one lane of the wave takes the slow path.agc_driver_mad_f32run withDenormPreserve 32forced, as you did here.Imported from boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-15T16:59:01Z.
I implemented option 2 locally (not pushed yet) and measured it. On this GPU, the per-instruction branch itself is the cost.
The lowering: a new
FPMadExact32IR op, lowered in the SPIR-V backend. It computes the plain multiply and add, then one combined test: a source or the result with exponent 0 or 255, or the product with exponent ≤ 1. Exponent 0 alone misses0x3f7fffff * 0x00800000 + 0x00800000on a device that keeps denormals, because the host rounds that product up to 2^-126. The exact sequence runs only inside anOpSelectionMerge/OpBranchConditional.agc_driver_mad_f32, now with a third wave where only lane 13 takes the slow path, passes with and withoutDenormPreserve 32forced. It fails withDenormPreserve 32if the fast path is forced or the product test checks exponent 0 only.Same chain as before (512 dependent
v_mac_f32, 65535 groups,FLOAT_MODE0xc0, median of three runs):mainIsNan(plain)DontFlattenand branch weightsSo on RDNA1 with AMD's Windows driver, a divergent
ifper instruction costs about 6× the multiply-add by itself, whatever the test and whether it is flattened. The branch can't get nearmainhere, and the SPIR-V grows about 4× because each slow path is emitted inline.I see two ways forward:
shaderDenormFlushToZeroFloat32flush in hardware.Which do you prefer?
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-15T18:20:52Z.
Thanks for measuring. 0.78 → 8.2 ms on a mad chain, and 13k → 166k SPIR-V words, settles the scale.
My vote is option 2, and I wouldn't merge option 1 first. The fma forms already pay that 10× on
main, which is a regression of its own, not a precedent to extend. Extending it to the mad forms puts the most common ALU instruction in gfx10.1 code on the same path before any fix exists. Control flow inside one instruction's translation isn't new in the recompiler: #5757'sAccessNarrowedWindowemits a selection construct per access, and the BDA reads branch to their slow paths. A uniform-false branch around the exact fixups costs a compare and a branch per mad on real data. Once it exists, the same helper can be applied to the fma forms in theperf(follow-up you mention, which would also cover what #4287 is after.The SPIR-V growth matters separately from GPU time: 12× more words per mad chain is pipeline compile time on every cold start. The fast path keeps the fixups in one branch, but they are still emitted per instruction. A shared helper function for the fixup block, called from the rare branch, would keep module size close to
main's.Imported from boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-17T13:49:34Z.
@CnRJay the comment just before yours measured the branch version, and in that chain every lane takes the fast path. Same 512-op
v_mac_f32chain:mainIsNan(plain)OpGroupNonUniformAnyof the test), no rare laneSo on RDNA1 with AMD's Windows driver, the branch isn't just a compare and a branch. Even a uniform, never-taken branch per instruction costs about 6× the multiply-add in a dependent chain, and a single lane carrying a denormal makes the whole wave pay the slow path at every step. A shared function for the fixups would fix the SPIR-V size you mention, but not this. That's why I offered dropping the lowering here and keeping the measured semantics until the float-controls work in #981 can flush in hardware.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-17T16:23:28Z.
Review: COMMENTED
A run of the current head on NVIDIA and lavapipe, since the description lists neither, plus the cost of the pushed lowering on NVIDIA for the discussion above.
Machine: Ubuntu 26.04, i7-8700, GCC 15.2, Release,
BUILD_TESTING=ON. NVIDIA GeForce RTX 3070 (driver 595.99.02) and lavapipe (Mesa 26.0.3, LLVM 21.1.8) on the same host. Head24325adf1merged ontomain84b0d2f24(clean).ANYPS5_NO_SHADER_CACHE=1,-j1 --timeout 30.agc_driver_mad_f32,vector_fused_multiply_addFloatInstructions.cppandVectorInstructions.cppfrommainagc_driver_mad_f32:mad f32 (no float mode): lane 32 (0x00000000, 0x7f800000, 0x3f800000) v_mad_f32 is 0x7fffffff, expected 0xffc00000agc_driver_mad_f32:lane 3 (0x7f7fffff, 0x40000000, 0xff7fffff) v_madmk_f32 K=0x00400000 is 0x40800000, expected 0x40000000VectorInstructions.cppchange reverted (nof32DenormalFlushForexception)agc_driver_mad_f32passes;vector_fused_multiply_addfails:v_mac_f32 operand is not flushedagc_driver_mad_f32fails:lane 3 (0x7f7fffff, 0x40000000, 0xff7fffff) v_madmk_f32 K=0x00400000 is 0x40800000, expected 0x40000000;vector_fused_multiply_addfails as on the 3070^agc(650 tests)main: 648/649; the failure is a CPU timing flake inagc_async_log_fatalthat passes 5/5 alone)main: 649/649)On lavapipe, the failure without the exception is the same message you got on the RX 5700 XT with
DenormPreserve 32forced, so lavapipe behaves here like a device that keeps f32 denormals (both devices reportshaderDenormPreserveFloat32 = false). On the 3070 the execution test passes without the exception, like on your GPU without the forced decoration. There, only the CPU testvector_fused_multiply_addcatches its removal.Cost on NVIDIA. I used a local compute test (not part of this PR):
v_mac_f32 v6, v4, v5repeated N times in one dependent chain,FLOAT_MODE0xc0, 64-lane groups, 65535 groups, all lanes on normal values. I timedDispatch+WaitIdleand took the median of 16 runs after 3 warm-up runs. Each dispatch has a fixed cost of about 20 ms on this setup, which hides short chains, so I compare the slope between long chains:v_mac_f32v_mac_f32mainSo on the 3070 the pushed lowering costs about 23 times
mainon this chain, compared with the 15 times you measured on RDNA1 (0.7 → 10.5 ms). The cost is not specific to the AMD Windows driver. The branch versions from your last comment are not pushed, so I did not measure them.Not run: Windows, AMD, any title.
GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02) and lavapipe (Mesa 26.0.3), Ubuntu 26.04
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-18T16:35:51Z.
Review: CHANGES_REQUESTED
Thanks for measuring the branch version, and for the NVIDIA/lavapipe numbers @jotitadev. With a uniform never-taken branch still ~6x and one rare lane making the whole wave pay 24.8 ms, the branch isn't the fix I suggested, so you asked the right question.
Go with your option 2: drop the lowering from this PR and keep the measured gfx1010 semantics.
FloatInstructions.cpp/VectorInstructions.cppchanges (floatTernarymul + flush + add, thef32DenormalFlushForexception). Onmainnormal inputs are already exact, and this PR only changed edge cases at ~15x (RX 5700 XT) to ~23x (RTX 3070) the cost of the most common ALU op.MadF32.cppand theVectorFusedMultiplyAdd.cppchange: they fail againstmain's lowering, so they can't land without it. Keep the probe, runner and raw results in the gist/comments so the follow-up can reuse them.debt/shader.mdentries describingv_mad_f32,v_mac_f32,v_madak_f32andv_madmk_f32as measured on gfx1010 (product rounded before the add, sources/product/result flushed in every f32 denormal mode, tininess rule, NaN rule, the round mode 2 zero-sum sign), now stated as "not modelled, left to the driver" rather than as translated behaviour. The PR becomes docs-only.shaderDenormFlushToZeroFloat32is available; say there what the exact path costs, with the numbers from this thread.Imported from boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-19T14:00:16Z.
Done. The PR is docs only now: one new
debt/shader.mdentry (789 characters) forv_mad_f32,v_mac_f32,v_madak_f32andv_madmk_f32. It describes the gfx1010 behaviour as not modelled and left to the driver, and gives the cost numbers from this thread. The lowering,MadF32.cppand theVectorFusedMultiplyAdd.cppchange are gone. I kept them locally, with the probe, runner and gist, for the follow-up once #981 step 4 can flush in hardware. Thanks @jotitadev for the NVIDIA and lavapipe runs.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T14:20:09Z.
Checked the entry against the PR description and the neighbouring
v_mad_legacy_f32and NaN entries indebt/shader.md: consistent, the link target exists, Conventions is green and no other change touches the line. The lowering is gone as requested and the measured semantics and cost numbers are recorded for the #981 follow-up; merged.