perf(shader): drop denormal flushes of values that can't be denormal #2103

Merged
karanvora2599 merged 1 commit from upstream-pr/2103 into main 2026-10-10 06:13:15 +00:00
karanvora2599 commented 2026-10-09 08:38:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-09T08:38:43Z.


What

With f32 denormals flushed (FLOAT_MODE 0xc0, 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 is exponent == 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 DenormalFlushEliminator pass, after MaskedSelectEliminator: 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:

  • a flush select;
  • a constant whose exponent field is nonzero, or ±0;
  • a sign change (BitwiseXor32 with 0x80000000), absolute value (BitwiseAnd32 with 0x7fffffff), FPAbs32, FPNeg32, FPSaturate32 or bit cast of such a value;
  • an integer or f16 converted to f32 (ConvertF32U32/S32, ConvertIToF32, ConvertUToF32, ConvertF32F16), which gives zero or at least 2^-24;
  • a select or phi whose values all are. A phi met again while its own inputs are being checked counts as possibly denormal, so a loop phi qualifies only when every value it takes is a flush or one of the above.

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_flush on 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) and vector_fused_multiply_add pass on both devices.

  • On main at f1451396 (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) after cmake --build build -- -k 0 and --target libs, serial, --timeout 120, RTX 4060: 494 of 496 built tests pass. agc_driver and agc_shader_fusion don't build on current main (VulkanTestDevice.hpp includes spirv/unified1/spirv.hpp; #2093 and #2099 fix it), so they aren't counted. windows_import_modules fails on this machine because Windows Error Reporting keeps its GUI fixture's image open at cleanup (#2227), and tls_function_coverage because Windows Defender removes an executable it writes to the temporary directory (Defender logged detections of load-register-1.exe); both run only the relinker, whose build contains no recompiler sources.

  • Per op in FLOAT_MODE 0xc0 on the RTX 4060, on current main, 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_f32 14 to 12, v_mul_f32 30 to 29, v_fma_f32 38 to 36, v_min/max_f32 15/16 to 13/14, v_med3_f32 48 to 46, v_fract_f32 4 to 2, v_sin_f32 15 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_MODE 0xc0 main this PR
    v_add_f32 9.54 ms / 11.3-28.0 ms 7.43 ms / 13.9-27.2 ms
    v_mul_f32 22.7 ms / 28.0-32.1 ms 21.3 ms / 26.7-30.8 ms
    v_fma_f32 27.5 ms / 36.5-39.0 ms 25.1 ms / 27.2-38.5 ms

    The medians vary a lot on this laptop; the best times are the steadier comparison. The outputs are bit-identical (same checksum).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4798104911 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2103). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-09T08:38:43Z. --- ### What With f32 denormals flushed (`FLOAT_MODE` 0xc0, 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 is `exponent == 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 `DenormalFlushEliminator` pass, after `MaskedSelectEliminator`: 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: - a flush select; - a constant whose exponent field is nonzero, or ±0; - a sign change (`BitwiseXor32` with 0x80000000), absolute value (`BitwiseAnd32` with 0x7fffffff), `FPAbs32`, `FPNeg32`, `FPSaturate32` or bit cast of such a value; - an integer or f16 converted to f32 (`ConvertF32U32/S32`, `ConvertIToF32`, `ConvertUToF32`, `ConvertF32F16`), which gives zero or at least 2^-24; - a select or phi whose values all are. A phi met again while its own inputs are being checked counts as possibly denormal, so a loop phi qualifies only when every value it takes is a flush or one of the above. 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_flush` on 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) and `vector_fused_multiply_add` pass on both devices. - On `main` at f1451396 (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) after `cmake --build build -- -k 0` and `--target libs`, serial, `--timeout 120`, RTX 4060: 494 of 496 built tests pass. `agc_driver` and `agc_shader_fusion` don't build on current `main` (`VulkanTestDevice.hpp` includes `spirv/unified1/spirv.hpp`; #2093 and #2099 fix it), so they aren't counted. `windows_import_modules` fails on this machine because Windows Error Reporting keeps its GUI fixture's image open at cleanup (#2227), and `tls_function_coverage` because Windows Defender removes an executable it writes to the temporary directory (Defender logged detections of `load-register-1.exe`); both run only the relinker, whose build contains no recompiler sources. - Per op in `FLOAT_MODE` 0xc0 on the RTX 4060, on current `main`, 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_f32` 14 to 12, `v_mul_f32` 30 to 29, `v_fma_f32` 38 to 36, `v_min/max_f32` 15/16 to 13/14, `v_med3_f32` 48 to 46, `v_fract_f32` 4 to 2, `v_sin_f32` 15 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_MODE` 0xc0 | main | this PR | |---|---|---| | `v_add_f32` | 9.54 ms / 11.3-28.0 ms | 7.43 ms / 13.9-27.2 ms | | `v_mul_f32` | 22.7 ms / 28.0-32.1 ms | 21.3 ms / 26.7-30.8 ms | | `v_fma_f32` | 27.5 ms / 36.5-39.0 ms | 25.1 ms / 27.2-38.5 ms | The medians vary a lot on this laptop; the best times are the steadier comparison. The outputs are bit-identical (same checksum). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-10 06:13:12 +00:00 (Migrated from github.com)

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_elimination passes and fails when "can't be denormal" is made always true. Merging.

Note: this touches Recompiler.cpp next to #1491; whichever lands second needs a trivial rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094540888 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2103#issuecomment-6094540888). Originally posted by [adriadam10](https://github.com/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_elimination` passes and fails when "can't be denormal" is made always true. Merging. Note: this touches `Recompiler.cpp` next to #1491; whichever lands second needs a trivial rebase.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!2103
No description provided.