perf(shader): skip the tiny product check that a constant factor settles #2020
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2020"
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-09T05:13:19Z.
What
Follow-up to #1850 (#981). With f32 denormals flushed (
FLOAT_MODE0xc0, or no known mode), every f32 multiply and fma checks whether the exact product is below 2^-126, so that a product which rounds up to 2^-126 still flushes as on RDNA: two exponent extracts, aUMulHiof the significands and a chain of compares and selects influshTinyProduct. NVIDIA reports neithershaderDenormFlushToZeroFloat32norshaderDenormPreserveFloat32, so this stays in every shader there.When one factor is a literal or an inline constant, its exponent settles the check during translation:
|c| >= 1): the check needs both factors finite and nonzero, and for every normalx,e_x + e_c + carry >= 1 + 127 = 128. The product is never tiny, so the check is not emitted.UMulHi(sig_x, 2^23) = sig_x >> 9 <= 0x7fffand the carry is always 0. TheUMulHi, its compare and its select are not emitted; the exponent test stays.readOperandfolds the abs, neg and input denormal flush of an immediate source into one constant, so the factor reachesflushTinyProductasBitCastF32(constant).flushF32Denormalfolds an immediate the same way. A plain read keeps its IR shape (vector_fused_multiply_addpasses unchanged).Each removed part evaluates to the same constant the current code computes at run time, so results are bit-identical. Registers and other sources are unchanged.
Tested
Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
ANYPS5_ENABLE_SPIRV_TOOLS=ON. GPU tests on an NVIDIA GeForce RTX 4060 Laptop GPU (591.86, subgroup 32) and on lavapipe (Mesa 26.2.4 for Windows, llvmpipe,GALLIUM_OVERRIDE_CPU_CAPS=avxas in CI).New
vector_constant_factor_tininess: translatesv_mul_f32,v_mul_legacy_f32,v_fmamk_f32,v_fmaak_f32and VOP3v_fma_f32with register, inline and literal factors underFLOAT_MODE0xc0 and 0xf0, and counts the tiny-product compares andUMulHis: none for 2.0, -4.0, 1.0, inf, a denormal literal and integer inline 1; exponents only for 0.5, 0.25 and-|0.5|; the full check for 0x3f7fffff, 1/(2pi), a register pair and thev_fmaak_f32literal addend. It also checks the folded constant of-|2.0|and of flushed denormal literals. On main it fails (v_mul_f32 v5, 2.0, v7 emits 1 tiny product checks, expected 0).New
agc_driver_f32_constant_factor_tininess: eight instructions, each run once with a constant factor and once with the same value in a VGPR (the path #1850 pins against hardware), over 32 rows around 2^-126, specials and ±0 addends, with no float mode, 0xc0, 0x00 (IEEE=1) and 0xf0. Every pair must match bit for bit. Six cells whose exact product is below 2^-126 and stays below it when rounded to 24 bits are pinned to the flushed zero:0x3f7ffffe * 0x00800000,0.5 * 0x00ffffff,0.5 * 0x80ffffff, andv_fmamk_f32by 0.25 with +0, -0 and a flushed denormal addend. Lane 2,0x3f7ffffe * 0x00800001= 2^-126 * (1 - 2^-46), is only compared between the two forms: hardware keeps 0x00800000 there (rounded to 24 bits it is 2^-126, measured by @CouchRiv below), whilemain's check from #1850 flushes it, and both forms here followmain. The row still shows a skipped check, because a host that flushes after rounding keeps the product with the constant whilemain's check flushes it with the register. It passes on main too.Negative checks, each reverted:
v_mul_f32 v5, 0.5, v7 emits 0 tiny product checks, expected 1andlane 2 no float mode v_mul_f32 0x3f7ffffe is 0x00800000 with a constant and 0x00000000 with a register.v_mul_f32 v5, 0x3f7fffff, v7 emits 0 significand products, expected 1andlane 3 no float mode v_mul_f32 0x3f7ffffe is 0x00000000 with a constant and 0x00fffffd with a register.v_fma_f32 v5, v6, -|2.0|, v7 source 1 is folded to the wrong constantandlane 0 no float mode v_fma_f32 -|2.0| is 0x01000000 with a constant and 0x81000000 with a register.Each fails both new tests on both devices.
agc_driver_f32_denormal_flush,agc_driver_f32_output_modifier,agc_driver_f32_min_max_nanandvector_fused_multiply_addpass unchanged on both devices.Rebased onto current
main(6cc0503d, with #2083): the two new tests,agc_driver_f32_denormal_flush,agc_driver_f32_output_modifier,agc_driver_f32_min_max_nan,agc_driver_f32_arithmetic_nan,vector_fused_multiply_addandvector_mad_legacypass on both devices.agc_driverandagc_shader_fusiondon't build on currentmain(VulkanTestDevice.hppincludesspirv/unified1/spirv.hpp; #2093 and #2099 fix it).Full ctest before the rebase (
f1451396), aftercmake --build buildand--target libs, serial,--timeout 120, RTX 4060: 492 of 493 pass.windows_import_modulesfails on this machine when Python removes its temporary directory (PermissionError: [WinError 5]on a.prx.guest.prxthe test has just run); #2227 traces it to Windows Error Reporting keeping the GUI fixture's image open. It runs only the relinker, whose build contains no recompiler sources.Pipeline statistics (
APS5_PIPELINE_STATS=1, RTX 4060) of a compute program, not committed, with eight constant products typical of shaders (×2.0, ×0.5, ×255.0, ×1/255,v_fmamk_f32×3.0 and ×0.25, ×-1.0, ×4.0) underFLOAT_MODE0xc0:The output is bit-identical (same checksum over all 512 result words). Only ×1/255 keeps the full check.
Touches
OperandAccess.cpplike #1940, in other functions.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 CouchRiv on 2026-10-09T05:54:25Z.
Measured on RDNA2 (gfx1036, Radeon 610M) with
tools/hw-oracle, wave32, using words 5-38 ofF32ConstantFactorTininess'sCodeunchanged and its 32 rows. The constant and register forms are equal in every lane in FLOAT_MODE 0xc0 (IEEE=0), 0x00 (IEEE=1) and 0xf0, and six of the seven pinned cells match. Lane 2 does not:v_mul_f32 0x3f7ffffetimes 0x00800001 is 0x00800000 on hardware, in 0xc0 and in 0x00, with the constant and with the register. That product is 2^-126 * (1 - 2^-46): below 2^-126 before rounding, exactly 2^-126 after rounding to 24 bits.More rows (
v_mul_f32,v_fma_f32with a 0 addend andv_mul_legacy_f32all agree), products 2^-126 * (1 - D/2^47), f32 denormals flushed:So the hardware flushes when the product rounded to 24 bits in the current round mode (unbounded exponent) is below 2^-126, which is tininess after rounding. The 6646451 row rules out "round to a denormal, then flush": as a denormal it rounds to 0x00800000 (it does with denormals kept), yet it is flushed.
main's check from #1850 tests the product before rounding, somainalso gives 0x00000000 for lane 2 and for the nearest-mode rows above that hardware keeps. Your shortcuts are not affected by this: a power-of-two factor gives an exact product, and|c| >= 1never gives a tiny one. Only the lane-2 pin, and the full check it is compared against, disagree with hardware.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-09T06:26:04Z.
Thanks for measuring it. You're right: the lane 2 pin held
main's before-rounding result, not the hardware's. I removed it (a29d2f55). Lane 2 is still compared between the constant and the register form, so a check skipped from exponent 126 still fails it, on the RTX 4060 and on lavapipe (lane 2 no float mode v_mul_f32 0x3f7ffffe is 0x00800000 with a constant and 0x00000000 with a register). The six remaining pins are cases where the product stays below 2^-126 after rounding to 24 bits. The description says this now.The after-rounding rule has a cheap exact form. Scaling one factor by 2^24 is exact and keeps the product out of the denormal range, so
|(x * 2^24) * y| < 2^-102compares the product rounded to 24 bits in the current mode with 2^-126: two multiplies and a compare instead of theUMulHichain. It gives the hardware result for all five nearest-mode rows in your table (0x0c800000for the three kept,0x0c7ffffffor the two flushed). I'd open that as a separatefix(shader)PR on top of this one, citing your measurements, unless you or @adriadam10 already have it in progress.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:08:02Z.
Checked on Linux (merged onto current main): build + ctest green for
vector_constant_factor_tininess,agc_driver_f32_constant_factor_tininess,agc_driver_f32_denormal_flushandvector_fused_multiply_add. The unit test fails without theOperandAccess.cppchange. The execution test passes without it (it only compares constant vs register form), which is expected for a bit-identical optimization. The exponent/significand argument holds (e_x + e_c + carry >= 128fore_c >= 127; significand 2^23 givesUMulHi <= 0x7fff). Hardware equality of constant and register forms was measured by @CouchRiv on gfx1036. Merging.