perf(shader): emit the f32 NaN repair without its own selection #28820

Open
oneandonlydean wants to merge 1 commit from upstream-pr/28820 into main
oneandonlydean commented 2026-12-03 23:59:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-12-03T23:59:03Z.


What

EmitNanPropagateF32 opened one DontFlatten selection per exact f32 result: branch on "the host result is a NaN", pick the source NaN inside, phi after. In large compute programs that is hundreds to thousands of small selections in one function, and the GPU vendor's pipeline compiler pays for each of them.

Now the repair is emitted without its own selection: the same selects pick the source NaN, they always run, and one more OpSelect on the same NaN test chooses between the repaired bits and the host result. The value is the same in every case: the instructions that moved out of the branch are bit casts, integer compares, selects and one OR, none with a side effect. The form without possible NaN sources was already a single select and is unchanged, and so is the grouped region path (EmitNanRegion), which keeps its one entry or exit test for a whole region.

Why: on #27914 a reporter's boots spend about a minute of vendor compile per new compute program, with one function holding 90% or more of each module. I measured where that time goes on a title I hold and found this branch per result to be the largest single part I could remove without changing results.

The debt note of #17733 said the source repair branches on a NaN host result; its sentence is corrected in the pending fragment that adds the entry.

Tested

Linux (CachyOS, GCC 16, Release), NVIDIA GeForce RTX 4090, driver 610.57.04.

On this head:

  • GPU execution tests agc_driver_f32_arithmetic_nan, agc_driver_f32_arithmetic_nan_fast and agc_driver_fma_mix_f32_nan_sources (they run v_add_f32, v_sub_f32, v_subrev_f32, v_mul_f32, v_fma_f32 and v_fma_mix_f32 forms with NaN sources on the GPU and compare the result bits, payloads included): pass.
  • agc_nan_payload_elimination, agc_shader_memory, agc_spirv_branch_targets: pass.

Measured with a build of main 75021708bf that carries this same change behind a local switch (one binary, switch off and on; the switched-on path emits exactly what this PR emits):

Vendor pipeline creation time, offline. The 13 compute programs over 100000 SPIR-V words that PPSA21564 creates in one level, captured once per arm and replayed with agc_compute_pipeline_replay, one process per replay, fresh application cache, driver disk cache off, two passes. Times are the tool's fresh pipeline creation time in ms, on this host's E cores.

program words before selections before ms pass 1 ms pass 2 words after selections after ms pass 1 ms pass 2 after / before
0x50051e600 207446 751 4664 4836 201065 327 2700 2579 55.6%
0x500525200 138459 473 1001 1010 132041 52 718 733 72.2%
0x500571000 269476 1194 6891 7248 261184 639 4137 4020 57.7%
0x500586800 333603 1461 8740 9335 321251 642 5768 5933 64.7%
0x50059cd00 292684 1310 7946 7619 282906 650 4772 4599 60.2%
0x5005b3f00 358136 1604 10395 10474 344196 675 6638 6591 63.4%
0x5005cc100 307663 1321 7887 8296 297851 654 5129 4545 59.8%
0x5005e4a00 372913 1619 10422 10823 358883 681 6415 7040 63.3%
0x5005fdb00 327956 1435 9537 9871 316606 659 5576 5611 57.6%
0x500617500 393268 1733 11430 12221 377712 686 7019 8933 67.4%
0x500666700 149521 691 1062 1002 141361 167 612 618 59.6%
0x555f41f00 555668 2436 9691 9793 536622 1095 7534 7256 75.9%
0x555f4f500 312386 1292 3983 4293 306732 899 3081 2933 72.7%

All 13 together: pass 1 93648 ms to 60100 ms (64.2%), pass 2 96821 ms to 61391 ms (63.4%). Words 96.5%, selections 45.2%. No program got slower. The two passes of one capture differ by 3.8% in the median, 24% at most.

Frame time, PPSA21564, same scene, one perf window, warm shader caches (so this pair shows the run-time cost of always running the selects, not the compile gain): p50 over 180 to 330 s is 205.8 ms before and 208.6 ms after (+2.8 ms, +1.4%); the noise floor of this scene is 2 to 4 ms, so neutral. Presents per 10 s 49 and 48, GPU busy per present 81.8 ms and 80.8 ms. No crash, the level loaded and the frames show the same pictures in both arms.

Not claimed: that this unlocks PPSA38500 (#27914). It shortens the compile of each new large compute program there by an amount I measured only on PPSA21564's programs; whether that is enough for the reporter's boots is for a run on this head to show.

Windows: built and tested by CI only. No Windows host here.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4090, driver 610.57.04, Linux
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Updated shader handling to select the repaired floating-point result when the host calculation produces NaN. Existing fallback behavior for cases without a possible NaN source remains unchanged.
  • Documentation
    • Clarified how floating-point result repair and selection work, including existing fallback behavior and compatibility caveats.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5637320069 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28820). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-12-03T23:59:03Z. --- ### What `EmitNanPropagateF32` opened one `DontFlatten` selection per exact f32 result: branch on "the host result is a NaN", pick the source NaN inside, phi after. In large compute programs that is hundreds to thousands of small selections in one function, and the GPU vendor's pipeline compiler pays for each of them. Now the repair is emitted without its own selection: the same selects pick the source NaN, they always run, and one more `OpSelect` on the same NaN test chooses between the repaired bits and the host result. The value is the same in every case: the instructions that moved out of the branch are bit casts, integer compares, selects and one OR, none with a side effect. The form without possible NaN sources was already a single select and is unchanged, and so is the grouped region path (`EmitNanRegion`), which keeps its one entry or exit test for a whole region. Why: on #27914 a reporter's boots spend about a minute of vendor compile per new compute program, with one function holding 90% or more of each module. I measured where that time goes on a title I hold and found this branch per result to be the largest single part I could remove without changing results. The debt note of #17733 said the source repair branches on a NaN host result; its sentence is corrected in the pending fragment that adds the entry. ### Tested Linux (CachyOS, GCC 16, Release), NVIDIA GeForce RTX 4090, driver 610.57.04. On this head: - GPU execution tests `agc_driver_f32_arithmetic_nan`, `agc_driver_f32_arithmetic_nan_fast` and `agc_driver_fma_mix_f32_nan_sources` (they run `v_add_f32`, `v_sub_f32`, `v_subrev_f32`, `v_mul_f32`, `v_fma_f32` and `v_fma_mix_f32` forms with NaN sources on the GPU and compare the result bits, payloads included): pass. - `agc_nan_payload_elimination`, `agc_shader_memory`, `agc_spirv_branch_targets`: pass. Measured with a build of `main` 75021708bf2 that carries this same change behind a local switch (one binary, switch off and on; the switched-on path emits exactly what this PR emits): **Vendor pipeline creation time, offline.** The 13 compute programs over 100000 SPIR-V words that PPSA21564 creates in one level, captured once per arm and replayed with `agc_compute_pipeline_replay`, one process per replay, fresh application cache, driver disk cache off, two passes. Times are the tool's fresh pipeline creation time in ms, on this host's E cores. | program | words before | selections before | ms pass 1 | ms pass 2 | words after | selections after | ms pass 1 | ms pass 2 | after / before | |---|---|---|---|---|---|---|---|---|---| | 0x50051e600 | 207446 | 751 | 4664 | 4836 | 201065 | 327 | 2700 | 2579 | 55.6% | | 0x500525200 | 138459 | 473 | 1001 | 1010 | 132041 | 52 | 718 | 733 | 72.2% | | 0x500571000 | 269476 | 1194 | 6891 | 7248 | 261184 | 639 | 4137 | 4020 | 57.7% | | 0x500586800 | 333603 | 1461 | 8740 | 9335 | 321251 | 642 | 5768 | 5933 | 64.7% | | 0x50059cd00 | 292684 | 1310 | 7946 | 7619 | 282906 | 650 | 4772 | 4599 | 60.2% | | 0x5005b3f00 | 358136 | 1604 | 10395 | 10474 | 344196 | 675 | 6638 | 6591 | 63.4% | | 0x5005cc100 | 307663 | 1321 | 7887 | 8296 | 297851 | 654 | 5129 | 4545 | 59.8% | | 0x5005e4a00 | 372913 | 1619 | 10422 | 10823 | 358883 | 681 | 6415 | 7040 | 63.3% | | 0x5005fdb00 | 327956 | 1435 | 9537 | 9871 | 316606 | 659 | 5576 | 5611 | 57.6% | | 0x500617500 | 393268 | 1733 | 11430 | 12221 | 377712 | 686 | 7019 | 8933 | 67.4% | | 0x500666700 | 149521 | 691 | 1062 | 1002 | 141361 | 167 | 612 | 618 | 59.6% | | 0x555f41f00 | 555668 | 2436 | 9691 | 9793 | 536622 | 1095 | 7534 | 7256 | 75.9% | | 0x555f4f500 | 312386 | 1292 | 3983 | 4293 | 306732 | 899 | 3081 | 2933 | 72.7% | All 13 together: pass 1 93648 ms to 60100 ms (64.2%), pass 2 96821 ms to 61391 ms (63.4%). Words 96.5%, selections 45.2%. No program got slower. The two passes of one capture differ by 3.8% in the median, 24% at most. **Frame time, PPSA21564, same scene, one perf window, warm shader caches** (so this pair shows the run-time cost of always running the selects, not the compile gain): p50 over 180 to 330 s is 205.8 ms before and 208.6 ms after (+2.8 ms, +1.4%); the noise floor of this scene is 2 to 4 ms, so neutral. Presents per 10 s 49 and 48, GPU busy per present 81.8 ms and 80.8 ms. No crash, the level loaded and the frames show the same pictures in both arms. Not claimed: that this unlocks PPSA38500 (#27914). It shortens the compile of each new large compute program there by an amount I measured only on PPSA21564's programs; whether that is enough for the reporter's boots is for a run on this head to show. Windows: built and tested by CI only. No Windows host here. ### 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) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4090, driver 610.57.04, Linux - [x] AI-assisted: yes <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Updated shader handling to select the repaired floating-point result when the host calculation produces NaN. Existing fallback behavior for cases without a possible NaN source remains unchanged. * **Documentation** * Clarified how floating-point result repair and selection work, including existing fallback behavior and compatibility caveats. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-12-04 01:35:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T01:35:52Z.

Review: APPROVED


Reviewed — looks good

The reviewed change removes the NaN repair branch while preserving the result selection, and I found no defect in the changed code.

What I ran

Reviewed head f844ec4a1767f2742a44223390eaf8babdd66f76 using the prepared diff and manifest, the changed function’s source range, and the complete changed debt note. No tests or builds were run.

Results

Check Result Note
Changed code review passed Repair selection remains gated by the NaN test
Tests not run No runtime checks performed

Negative control

Not run; no test was run to invert the expected behavior.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

GPU execution, console compatibility, and performance claims in the description were not independently verified. No changed files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6181697453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28820#pullrequestreview-6181697453). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-12-04T01:35:52Z. **Review:** APPROVED --- # Reviewed — looks good The reviewed change removes the NaN repair branch while preserving the result selection, and I found no defect in the changed code. ## What I ran Reviewed head `f844ec4a1767f2742a44223390eaf8babdd66f76` using the prepared diff and manifest, the changed function’s source range, and the complete changed debt note. No tests or builds were run. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Repair selection remains gated by the NaN test | | Tests | not run | No runtime checks performed | ## Negative control Not run; no test was run to invert the expected behavior. ## Findings ### Blocking None. ### Non-blocking None. ## Sources - [Changed NaN repair at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/f844ec4a1767f2742a44223390eaf8babdd66f76/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp#L1477-L1513) - [Updated debt note at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/f844ec4a1767f2742a44223390eaf8babdd66f76/docs/dev/debt/changes/pr17733-shader.md#L1-L6) ## Not verified GPU execution, console compatibility, and performance claims in the description were not independently verified. No changed files were excluded by the manifest. ## To get this to a good state Nothing is required.
Plyxen commented 2026-12-04 13:37:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-04T13:37:36Z.

Review: APPROVED


Verified on Windows 11 (GCC 15.2, Release) on main 484b0368d3, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and the Khronos validation layer 1.4.363. Every run log shows the loader inserting the layer and none shows "was not found". It ran on an RTX 5080 (621.24) and an AMD Radeon iGPU (26.11.2), together with #28802, #28780, #28646 and #28670.

  • The f32 NaN group passed on main and on this change on both GPUs, with no VUID- messages in any log: agc_driver_f32_arithmetic_nan, agc_driver_f32_arithmetic_nan_fast, agc_driver_fma_mix_f32_nan_sources, agc_nan_payload_elimination, agc_shader_memory and agc_spirv_branch_targets.
  • Read the emitter change. The moved instructions are bitcasts, integer compares, selects and one OR, all side-effect free. The final OpSelect on resultNan gives what the old phi gave on both edges.

Not run: the pipeline creation timings and the PPSA21564 frame-time pair, and Linux.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6189955207 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28820#pullrequestreview-6189955207). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-12-04T13:37:36Z. **Review:** APPROVED --- Verified on Windows 11 (GCC 15.2, Release) on main 484b0368d3, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and the Khronos validation layer 1.4.363. Every run log shows the loader inserting the layer and none shows "was not found". It ran on an RTX 5080 (621.24) and an AMD Radeon iGPU (26.11.2), together with #28802, #28780, #28646 and #28670. - The f32 NaN group passed on main and on this change on both GPUs, with no `VUID-` messages in any log: `agc_driver_f32_arithmetic_nan`, `agc_driver_f32_arithmetic_nan_fast`, `agc_driver_fma_mix_f32_nan_sources`, `agc_nan_payload_elimination`, `agc_shader_memory` and `agc_spirv_branch_targets`. - Read the emitter change. The moved instructions are bitcasts, integer compares, selects and one OR, all side-effect free. The final `OpSelect` on `resultNan` gives what the old phi gave on both edges. Not run: the pipeline creation timings and the PPSA21564 frame-time pair, and Linux. AI-assisted: yes (Claude Code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/28820:upstream-pr/28820'
git switch 'upstream-pr/28820'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/28820'
git switch 'upstream-pr/28820'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/28820'
git switch 'upstream-pr/28820'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/28820'
git switch 'main'
git merge --squash 'upstream-pr/28820'
git switch 'main'
git merge --ff-only 'upstream-pr/28820'
git switch 'main'
git merge 'upstream-pr/28820'
git push origin 'main'
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!28820
No description provided.