perf(shader): drop denormal flushes of values that can't be denormal #2103
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2103"
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-09T08:38:43Z.
What
With f32 denormals flushed (
FLOAT_MODE0xc0, or no known mode), #1850 flushes every f32 result when it is written and every f32 source when it is read. A source that is the result of an earlier flushing op is flushed twice, and the second flush can't change it: a flush isexponent == 0 ? sign : bits, which returns any value that is not a denormal unchanged (a zero keeps its sign). After SSA the second flush reads the first one directly, and the driver doesn't remove it (it doesn't know the first select already cleared denormals).New
DenormalFlushEliminatorpass, afterMaskedSelectEliminator: it finds the flush selects (SelectU32(IEqual32(BitwiseAnd32(x, 0x7f800000), 0), BitwiseAnd32(x, 0x80000000), x)) and replaces one whose source can't be a denormal with the source. A value can't be a denormal when it is:BitwiseXor32with 0x80000000), absolute value (BitwiseAnd32with 0x7fffffff),FPAbs32,FPNeg32,FPSaturate32or bit cast of such a value;ConvertF32U32/S32,ConvertIToF32,ConvertUToF32,ConvertF32F16), which gives zero or at least 2^-24;Anything else keeps its flush: loads, interpolants, arithmetic results before their output flush, and the output modifier, which can make a flushed result denormal again. Results are bit-identical.
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,GALLIUM_OVERRIDE_CPU_CAPS=avx).New
agc_denormal_flush_elimination(IR only): a flush of a flush goes and the first stays; a flush of an unknown value stays; sign change and absolute value; a select of two flushes against one with an unknown arm; normal, -0 and denormal constants; integer and f16 conversions; a clamp of a flush against a product; a phi of two flushes; a loop phi whose back edge is a flush against one whose back edge isn't.Negative check: counting every value as free of denormals fails the new test and
agc_driver_f32_denormal_flushon both devices (lane 0 no float mode v_min_f32 v16, v4, v5 is 0x00000001, expected 0x00000000).agc_driver_f32_denormal_flush,agc_driver_f32_output_modifier,agc_driver_f32_min_max_nan(rows measured on hardware) andvector_fused_multiply_addpass on both devices.On
mainatf1451396(before rebasing), with a temporary counter in the pass (stderr only, not committed) over a full ctest run, my measurement programs left out: 1155 recompiles, 199 with flushes, 1436 flush selects, 39 removed, in 5 tests. The execution tests mostly run one instruction on loaded sources, so few sources come from an earlier op. The pass applies to chained ALU code: in the per-op chains below, one of the two sources of each op is the previous result.Full ctest on current
main(6cc0503d) aftercmake --build build -- -k 0and--target libs, serial,--timeout 120, RTX 4060: 494 of 496 built tests pass.agc_driverandagc_shader_fusiondon't build on currentmain(VulkanTestDevice.hppincludesspirv/unified1/spirv.hpp; #2093 and #2099 fix it), so they aren't counted.windows_import_modulesfails on this machine because Windows Error Reporting keeps its GUI fixture's image open at cleanup (#2227), andtls_function_coveragebecause Windows Defender removes an executable it writes to the temporary directory (Defender logged detections ofload-register-1.exe); both run only the relinker, whose build contains no recompiler sources.Per op in
FLOAT_MODE0xc0 on the RTX 4060, on currentmain, pipeline statistics of a compute program with a dependent chain of 8 ops whose other source is a buffer load, not committed (instructions per op, main to this PR):v_add_f3214 to 12,v_mul_f3230 to 29,v_fma_f3238 to 36,v_min/max_f3215/16 to 13/14,v_med3_f3248 to 46,v_fract_f324 to 2,v_sin_f3215 to 13. Only the chained source loses its flush; the loaded one keeps it.Throughput on the RTX 4060, on current
main, not committed: a dependent chain of 1024 ops on registers, 65535 workgroups of 32, timed per dispatch including submit and wait, main and this branch alternated over 4 runs (best / run medians):FLOAT_MODE0xc0v_add_f32v_mul_f32v_fma_f32The medians vary a lot on this laptop; the best times are the steadier comparison. The outputs are bit-identical (same checksum).
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 adriadam10 on 2026-10-10T06:13:12Z.
Checked: the pass logic against each rule (f16→f32 ≥ 2^-24, int→f32 zero or ≥ 1, loop phis count as possibly denormal), build + ctest of libSceAgc/libSceAgcDriver green locally,
agc_denormal_flush_eliminationpasses and fails when "can't be denormal" is made always true. Merging.Note: this touches
Recompiler.cppnext to #1491; whichever lands second needs a trivial rebase.