perf(shader): take the f32 mul, fma and add result fixups only when needed #4322
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4322"
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 karanvora2599 on 2026-10-12T06:04:49Z.
What
v_mul_f32,v_fma_f32/v_fmac_f32,v_add_f32andv_sub_f32compute their AMD result fixups only when the host result needs them: a NaN, or for products a magnitude of at most 2^-126. Results are unchanged.This is stacked on #4287, which shrinks the NaN result selection itself. Only the last commit is new.
Background. Every f32 multiply, fma and add computes two fixups on its host result:
nanResultF32);FLOAT_MODEs that flush f32 denormals, the flush of products that are tiny after rounding to 24 bits (flushTinyProduct, #2839).Both are straight-line selects that run on every instruction. On an RTX 4060,
v_mul_f32is 16.5 SASS instructions, where the multiply is one. In fma-rc's census of eleven titles (316,352 instructions, linked in #4061),v_mul_f32is 13.4% of all instructions andv_add_f32/v_sub_f32/v_subrev_f326.6%.How.
rareFixupF32): when a fixup was emitted, the result becomesselect(special, fixed, plain), whereplainis the host result.specialisisnan(plain)for add and sub.specialis((bits(plain) + 0x7fffff) & 0x7fffffff) < 0x1000000: the result is a NaN, or its magnitude is at most 2^-126. That's three integer instructions. This form is used only with round to nearest even; directed f32 rounding keeps the plain selects.SelectFlags::rarelyTrueflag. Optimizer passes see an ordinarySelectF32, and dropping the flag only loses the branch.SpirvFlowEmitter): for each flagged select, the instructions used only by its rare arm are found, latest first, so the search stops at the first value with another use. They're emitted inside a selection (EmitValueOrDefaultIfCondition), and the select becomes the phi.OpLoopMergemust stay in the header's first block;Before / after on an RTX 4060 Laptop (Ada, driver 591.86),
FLOAT_MODE0xc0.Run time: dependent chains of 2,048 instructions over 16,384 groups of 32 lanes, median of 15 interleaved dispatches in one process. A temporary switch, not part of the PR, selected the plain selects for the comparison.
v_mul_f32v_fma_f32v_add_f32v_mul_f32, every input a NaNv_add_f32, every input a NaNv_mac_f32(legacy mad, unchanged, control)Both variants wrote the same results.
Static SASS per instruction (
APS5_PIPELINE_STATS, chain of 16) grows, since both paths are compiled:v_mul_f32: 16.5 → 23.5;v_fma_f32: 22.5 → 34 (VOP3) and 28 → 34;v_fmac_f32: 20.5 → 29.5;v_sub_f32: 11 → 12.5;v_add_f32: 10.5 → 10.5.Registers are within ±5.
Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release,
mainatd146ec1bplus #4287. RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790), lavapipe from Mesa 25.2.8 (the version CI installs; Windows build,GALLIUM_OVERRIDE_CPU_CAPS=avx).agc_driver_f32_*,agc_driver_fma_*,agc_driver_vop*,agc_denormal*,agc_driver_division_*and others) pass on the RTX 4060, includingagc_driver_f32_arithmetic_nan, which caught #2279's NaN quieting bug. This change reusesnanResultF32unchanged, so the IEEE-mode quieting can't drift.agc_driver_f32_constant_factor_tininessandagc_driver_f32_denormal_flush:v_mul_f32 v12, v4, v5 is 0x00800000, expected 0x00000000(2^-126 × (1 − 2^-24), tiny after rounding). The RTX 4060 and the Intel UHD pass that build, because their multiply already returns 0 for these products. lavapipe keeps f32 denormals and returns 2^-126.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 338 tests). The BDA tests that hang on driver 591.86 until #3291 lands are left out.agc_driver_ds_swizzleandagc_driver_f32_dot2_rounding(noFloat64), which fail the same way without this change;agc_driver_compute_user_data_calls_registered, which exited once with0xc0000374during the suite and passed 3 of 3 runs on its own.Why results don't change. The fixed value differs from the host result in two cases only:
specialis true.So when
specialis false, the fixed value equals the host result, and selectingplainthere gives exactly what the select chain gave before. The rare arm is the same IR as before, only emitted inside a branch.Limits.
v_mul_f32is about 6% slower.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T06:47:51Z.
Code review location:
core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp; line 366This condition misses positive NaNs. For example,
plain=0x7fc00001gives(bits + 0x007fffff) & 0x7fffffff == 0x7fffffff, sospecialis false and this bypassesfixed/nanResultF32. That can return the host NaN payload instead of AMD’s selected/quieted NaN. Could this predicate explicitly OR inFPIsNan32(plain)(or otherwise cover positive NaNs)?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T06:48:15Z.
Code review location:
core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp; line 366Correction: I recalculated this example, and my comment is wrong.
0x7fc00001 + 0x007fffffis0x80400000; masking with0x7fffffffgives0x00400000, which is below0x01000000, so the predicate does take the special path. Sorry for the false alarm; please disregard my suggestion to add an explicit NaN check.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T13:23:14Z.
Code review location:
core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp; line 366Thanks for checking it either way. The check covers every NaN, of either sign: bits from
0x7f800001to0x7fffffff(or the same with the sign bit set, where the add wraps) map to0through0x7ffffeafter the add and mask, all below0x1000000. Infinity maps to0x7fffffffand stays on the fast path, which is right since neither fixup changes an infinite result.Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-12T19:21:27Z.
A title-level cost worth considering alongside the focused arithmetic tests: in the saved Linux verification batch, head
85e88e527merged ontomain2a5eca11makes Surround Sound Studio (PPSA99051 01.000.003, open-source homebrew) hit the submission timeout on RADV where base passes. This is the largef8d1fab9pixel shader from #4399.Release,
ANYPS5_ENABLE_SPIRV_TOOLS=ON; alternating runs against base with fresh shader caches:Each failing RADV run exits roughly 2.17–2.19 s after registration recompilation ends, during draw-time optimization, before driver pipeline creation begins. NVIDIA already fails at base and still fails with this PR.
Timings use a local instrumented build with
sceKernelUsleepextended 20× to let the work finish; the pass/fail rows use the normal budget. No timer or wait-budget changes are proposed for merging. The recorded runs used gfx1036/RADV Mesa 25.2.8 and RTX 3090/NVIDIA 595.99.02, Ryzen 9 9950X, Ubuntu 24.04, GCC 13.3. This batch predates newer main; the PR head remains unchanged. SPIRV-Tools OFF and #4287 alone were not tested for this workload.Could the performance evaluation include this large shader and the draw-time optimization cost? Arithmetic correctness checks alone do not capture this regression.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T20:31:47Z.
This title-level result is important for the stated performance goal: the shader grows about 34%, the draw-time optimizer rises from roughly 0.44 s to 4.7–4.9 s, and the normal-budget RADV run goes from 3/3 to 0/4. I would hold the optimization until its SPIR-V growth and this timeout are explained and a semantics-preserving candidate is measured on this workload; the arithmetic-focused tests alone do not cover it.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T20:41:53Z.
Thanks @simonbbby. You're right, and the cost turned out larger than the arithmetic tests could show, so I'm closing this.
I couldn't run the
f8d1fab9shader here, so I measured the same question on synthetic shaders: NVIDIA pipeline creation on an RTX 4060 (driver 591.86), for shaders the driver hadn't seen (a unique literal per run), with the SPIR-V a release build hands the driver (APS5_SPIRV_OPT=none). A temporary switch, not committed, compared this PR's per-instruction branches against the straight-line selects it replaces. Medians of 5 or 6 interleaved runs, which all landed within a few percent:v_mul_f32v_add_f32A single dependent chain gives the driver nothing to schedule, which is why my earlier numbers missed this. With independent work, splitting every f32 instruction into its own branch costs far more to compile, and the cost grows faster than the shader. Your +77% on the RTX 3090 is in line with that. Branching only for products, so add and sub stay straight-line, still costs +18% at 1,024 ops and +77% at 4,096. The run-time gain shows up only in f32-dense code, and that doesn't justify doubling pipeline creation or failing cold runs on RADV.
#4539 doesn't need this PR. Its rules (the tiny-product flush, the NaN select and the constant factor) prove a multiply's result denormal-free on the straight-line code too. I'm rebasing it onto #4287 alone and will update it there.
The size of the fixups themselves, about 115 SPIR-V words per f32 instruction even straight-line, is the bigger compile-time lever. I'll look at shrinking that next.
Pull request closed