perf(shader): drop the output flush of f32 products #4539
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4539"
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-12T15:05:20Z.
What
DenormalFlushEliminatornow recognizes av_mul_f32orv_mul_legacy_f32result as never being an f32 denormal, so the output flush thatwriteOperandadds to it is removed. Results are unchanged.This is stacked on #4287, whose NaN select it reads. Only the last commit is new. #4490 and #4737 add other cases to the same switch, and whichever lands second rebases. It first sat on #4322, which is closed; this version doesn't need it.
Background. In a
FLOAT_MODEthat flushes f32 denormals,writeOperandflushes every f32 result: a select on the exponent bits. #2103 removes such a flush when the value provably can't be a denormal, but it had no rule for products. In av_mul_f32, the flush that a tiny product needs (#2839) is already done before the result is written, so the output flush is redundant. Yet it stayed on every multiply, the most common instruction in titles: 13.4% of the instructions in fma-rc's 11-title census, and 17% of Demon's Souls' by Raghav314's count in #4287.How. Three facts, each local to one value:
flushTinyProduct, round to nearest even, no addend) returns a signed zero or a product that isn't tiny after rounding. That product is at least 2^-126 in magnitude, or ±0 when the host flushes denormal results itself. Either way it's never a denormal. The select is flaggedSelectFlags::notDenormal. An fma doesn't get the flag, because its addend can cancel the result into a denormal.keepsProductOutOfTinyRange). If the other factor is denormal-free, the rounded product is at least that factor's magnitude, or ±0, infinity or NaN, so it isn't a denormal either.nanResultF32,select(isnan(c), c or c | quiet bit, fallback), takes its first arm only when that arm is a NaN. So it's as denormal-free as its fallback. This is recognized from the IR pattern, with no flag.With these, a multiply's result is denormal-free, and its output flush is removed like any other redundant flush.
Before / after on an RTX 4060 Laptop (Ada, driver 591.86),
FLOAT_MODE0xc0, compared with #4287. A temporary switch, not part of the PR, turned the new rules off for the comparison.A dependent chain of 2048 instructions, 16384 workgroups, median of interleaved runs in one process:
v_mul_f32v_mul_f32, NaN inputsv_fma_f32v_add_f32Only the multiply's SPIR-V changes; the fma and add rows are noise. In NVIDIA's compiled code (
VK_KHR_pipeline_executable_properties), av_mul_f32goes from 16.5 to 14.5 SASS instructions, andv_mul_legacy_f32from 20.0 to 18.0. The other 88 ops in the sweep compile to the same size.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_denormal_flush_eliminationhas a case for each rule: the flagged tiny flush, the NaN select, and the constant factor. Each case has a negative counterpart that must keep its flush:agc_driver_f32_denormal_flushgets rows where a nonzero addend cancels a normal product into an exact denormal, for example0x00800001 × 1.0 − 2^-126 = 2^-149. In every mode that flushes f32 denormals, AMD returns a signed zero forv_fma_f32,v_fmac_f32,v_fma_mix_f32,v_mad_legacy_f32andv_mac_legacy_f32. No existing row covered this.notDenormaltoo, so the fma loses its output flush, makes lavapipe fail those rows:v_fma_f32 v12, v4, v5, v6 is 0x00000001, expected 0x00000000. lavapipe keeps f32 denormal results. The RTX 4060 and the Intel UHD flush them in hardware in this mode, so only lavapipe catches it. Before the new rows, the 41 f32 and modifier tests all passed that broken build.agc_driver_f32_*,agc_driver_fma_*,agc_driver_vop*,agc_denormal*,agc_driver_division_*and others) pass on the RTX 4060.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_depth_fast_clear_submitsegfault that also happens onmain.agc_driver_f32_denormal_flushrows pass on all three devices.Why results don't change. A flush is removed only when its input can't be a denormal, which is the eliminator's existing contract. Each of the three facts depends only on one value and its operands, and holds whether the host keeps or flushes denormal results.
Limits.
v_fma_f32andv_add_f32keep their output flush; both can produce exact denormals.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 karanvora2599 on 2026-10-12T22:42:32Z.
Rebased onto #4287 alone, since #4322 is closed (its per-instruction branches cost too much pipeline-creation time; details there).
What changed:
falseArmNotDenormalrule, which read #4322's rarely-true select, and its unit-test case.SelectFlagsnow has onlynotDenormal.v_mul_f32result denormal-free.v_mul_f32chain goes from 5.59 to 5.10 ms (−9%);v_mul_f32goes from 16.5 to 14.5, andv_mul_legacy_f32from 20.0 to 18.0. The other 88 ops in the sweep compile to the same size.Float64tests that fail onmain) and lavapipe 25.2.8 (only the intermittentdepth_fast_clear_submitsegfault). With the fma's flush wrongly flagged, lavapipe still fails the cancelled-sum rows.This version adds no branches, so it doesn't change the block structure the driver compiles, only removes one flush per multiply.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:38:51Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed