perf(shader): reuse a repeated NaN bit test in a block when specializing #28911

Open
oneandonlydean wants to merge 1 commit from upstream-pr/28911 into main
oneandonlydean commented 2026-12-04 15:27:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-12-04T15:27:11Z.


What

SpecializeSpirv keeps a per-block table that turns a repeated OpBitcast and a repeated OpIsNan (or OpFUnordNotEqual x x) of one value into a copy of the first. This adds the same NaN test written on the bits to that table:

  • OpBitwiseAnd x 0x7fffffff
  • OpUGreaterThan y 0x7f800000
  • OpSelect c x 0xffc00000 (the NaN source or the default NaN)

Nothing else in the pass changes: other masks, limits and selects are left alone, a decorated result is never shared, and nothing is shared across a label.

Why here and not in the emitter: the f32 NaN repair and the translator's NaN selection each bit-cast their float source again before testing it, so at emission the repeated tests have different operand ids. They only get the same id inside this pass, when it folds a bit cast back to the type its source was cast from and propagates the copy. With the SPIR-V tools on, the optimizer removes the repeats afterwards. With the tools off (the default build, every Windows build) they stay in the module the driver gets.

Refs #28820. That PR removes the selection around the repair, which puts the repairs of one basic block into one SPIR-V block; agc_redundant_value_elimination counts repeats per block and then fails in a build without the SPIR-V tools (6 repeats in its wave64 module). main holds the same repeats today, spread over the blocks the selections open (0 per block, 9 over the function in that module). #28820 should merge after this one.

Tested

Linux (CachyOS, Release), NVIDIA GeForce RTX 4090, driver 610.57.04. Base main 922efd5473.

  • SPIR-V tools ON (GCC): the 161 CPU tests that link the recompiler pass; GPU execution tests agc_driver_f32_arithmetic_nan, agc_driver_f32_arithmetic_nan_fast, agc_driver_fma_mix_f32_nan_sources pass.
  • SPIR-V tools OFF (clang, as the Windows jobs build): the 155 CPU tests that link the recompiler pass.
  • New test in agc_spirv_module_tests: one block shares the And, the compare and the select; different inputs, sibling branches, other mask and limit constants, and a select with another fallback value are not shared.
  • #28820's head f844ec4a17 with this change, tools OFF: agc_redundant_value_elimination passes, 0 repeats in a block, the wave64 module goes from 1713 to 1641 words. With only the And and the compare in the table 2 repeats stay (the selects), which is why the select is in.

How many modules change. Counted with a local build that runs the pass with and without the new entries on every module it specializes, then runs the optimizer on both (the counting code is not in this PR).

set modules words change, tools OFF words change, tools ON
20 saved compute requests (agc_shader_replay) 20 15 0
PPSA21564, one cold boot to the pirate island level, 360 s 392 219 6

Tools OFF: all 219 get smaller, 15995 words of 16.4 million in those modules (0.1%). This is a small size change, not a frame-time claim; I measured no frame times for it.

Tools ON: the 6 modules keep their word count and their opcode counts. In each, the optimizer keeps one repeated instruction (OpBitwiseAnd in five, OpUGreaterThan in one) at the position and id of the first copy where it kept a later copy before; after renaming that one id the two modules hold the same instructions. A second boot gave the same three numbers.

Gate: with the tools on, 6 of the 392 modules of a working title change words, so I count this as altering a draw path a working title runs (gate YES), even though those 6 compute the same values.

Frames of the count boot: black while loading, the level drawing without artifacts in the last frames. The boot was cold and ran the optimizer twice per module, so it reached the level only near the end of the 360 s; modules created later in the level are not counted.

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
  • Depends on: none
  • Fixes: none
  • Unblocks: #28820
  • GPUs tested: NVIDIA GeForce RTX 4090, driver 610.57.04, Linux
  • AI-assisted: yes

Summary by CodeRabbit

  • Performance
    • Repeated shader calculations can now be reused within the same block, reducing redundant processing. Reuse is limited to matching inputs and conditions, so calculations in separate branches or with differing values remain distinct.
  • Tests
    • Added coverage for when repeated calculations are reused and when differences in inputs, conditions, or fallback values prevent reuse.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5649247967 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28911). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-12-04T15:27:11Z. --- ### What `SpecializeSpirv` keeps a per-block table that turns a repeated `OpBitcast` and a repeated `OpIsNan` (or `OpFUnordNotEqual x x`) of one value into a copy of the first. This adds the same NaN test written on the bits to that table: - `OpBitwiseAnd x 0x7fffffff` - `OpUGreaterThan y 0x7f800000` - `OpSelect c x 0xffc00000` (the NaN source or the default NaN) Nothing else in the pass changes: other masks, limits and selects are left alone, a decorated result is never shared, and nothing is shared across a label. Why here and not in the emitter: the f32 NaN repair and the translator's NaN selection each bit-cast their float source again before testing it, so at emission the repeated tests have different operand ids. They only get the same id inside this pass, when it folds a bit cast back to the type its source was cast from and propagates the copy. With the SPIR-V tools on, the optimizer removes the repeats afterwards. With the tools off (the default build, every Windows build) they stay in the module the driver gets. Refs #28820. That PR removes the selection around the repair, which puts the repairs of one basic block into one SPIR-V block; `agc_redundant_value_elimination` counts repeats per block and then fails in a build without the SPIR-V tools (6 repeats in its wave64 module). `main` holds the same repeats today, spread over the blocks the selections open (0 per block, 9 over the function in that module). #28820 should merge after this one. ### Tested Linux (CachyOS, Release), NVIDIA GeForce RTX 4090, driver 610.57.04. Base `main` 922efd5473. - SPIR-V tools ON (GCC): the 161 CPU tests that link the recompiler pass; GPU execution tests `agc_driver_f32_arithmetic_nan`, `agc_driver_f32_arithmetic_nan_fast`, `agc_driver_fma_mix_f32_nan_sources` pass. - SPIR-V tools OFF (clang, as the Windows jobs build): the 155 CPU tests that link the recompiler pass. - New test in `agc_spirv_module_tests`: one block shares the And, the compare and the select; different inputs, sibling branches, other mask and limit constants, and a select with another fallback value are not shared. - #28820's head f844ec4a176 with this change, tools OFF: `agc_redundant_value_elimination` passes, 0 repeats in a block, the wave64 module goes from 1713 to 1641 words. With only the And and the compare in the table 2 repeats stay (the selects), which is why the select is in. **How many modules change.** Counted with a local build that runs the pass with and without the new entries on every module it specializes, then runs the optimizer on both (the counting code is not in this PR). | set | modules | words change, tools OFF | words change, tools ON | |---|---|---|---| | 20 saved compute requests (`agc_shader_replay`) | 20 | 15 | 0 | | PPSA21564, one cold boot to the pirate island level, 360 s | 392 | 219 | 6 | Tools OFF: all 219 get smaller, 15995 words of 16.4 million in those modules (0.1%). This is a small size change, not a frame-time claim; I measured no frame times for it. Tools ON: the 6 modules keep their word count and their opcode counts. In each, the optimizer keeps one repeated instruction (`OpBitwiseAnd` in five, `OpUGreaterThan` in one) at the position and id of the first copy where it kept a later copy before; after renaming that one id the two modules hold the same instructions. A second boot gave the same three numbers. Gate: with the tools on, 6 of the 392 modules of a working title change words, so I count this as altering a draw path a working title runs (gate YES), even though those 6 compute the same values. Frames of the count boot: black while loading, the level drawing without artifacts in the last frames. The boot was cold and ran the optimizer twice per module, so it reached the level only near the end of the 360 s; modules created later in the level are not counted. 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] Depends on: none - [x] Fixes: none - [x] Unblocks: #28820 - [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 * **Performance** * Repeated shader calculations can now be reused within the same block, reducing redundant processing. Reuse is limited to matching inputs and conditions, so calculations in separate branches or with differing values remain distinct. * **Tests** * Added coverage for when repeated calculations are reused and when differences in inputs, conditions, or fallback values prevent reuse. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Plyxen commented 2026-12-04 15:57:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-04T15:57:20Z.

Review: APPROVED


Verified head cf4d991b10 on Windows 11 with GCC 15.2 and ANYPS5_ENABLE_SPIRV_TOOLS=OFF, applied to main 484b0368d3. This covers the Windows gap in the PR's Tested section.

Tests: agc_spirv_module, agc_driver_f32_arithmetic_nan, agc_driver_f32_arithmetic_nan_fast, agc_driver_fma_mix_f32_nan_sources and agc_redundant_value_elimination, run with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer (layer 1.4.363).

main this PR
RTX 5080 (621.24) 5/5 pass 5/5 pass
AMD Radeon iGPU (26.11.2) 5/5 pass 5/5 pass

Each log shows the loader inserting the validation layer and none contains a VUID-.

Negative check: with the new test kept and SpirvSpecialization.cpp reverted to main, agc_spirv_module fails with "2 check(s) failed", so the test covers the new table entries.

I did not run it against #28820's head, nor count module sizes on a title.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6192167502 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28911#pullrequestreview-6192167502). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-12-04T15:57:20Z. **Review:** APPROVED --- Verified head cf4d991b10 on Windows 11 with GCC 15.2 and `ANYPS5_ENABLE_SPIRV_TOOLS=OFF`, applied to main 484b0368d3. This covers the Windows gap in the PR's Tested section. Tests: `agc_spirv_module`, `agc_driver_f32_arithmetic_nan`, `agc_driver_f32_arithmetic_nan_fast`, `agc_driver_fma_mix_f32_nan_sources` and `agc_redundant_value_elimination`, run with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and `VK_LOADER_DEBUG=layer` (layer 1.4.363). | | main | this PR | |---|---|---| | RTX 5080 (621.24) | 5/5 pass | 5/5 pass | | AMD Radeon iGPU (26.11.2) | 5/5 pass | 5/5 pass | Each log shows the loader inserting the validation layer and none contains a `VUID-`. Negative check: with the new test kept and `SpirvSpecialization.cpp` reverted to main, `agc_spirv_module` fails with "2 check(s) failed", so the test covers the new table entries. I did not run it against #28820's head, nor count module sizes on a title. AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-12-04 16:32:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T16:32:38Z.

Review: APPROVED


Reviewed — looks good

The change reuses matching NaN bit tests within a block, and the changed implementation and tests support the stated behavior.

What I ran

Reviewed head cf4d991b10ad109e657c96af321e050ffb34a870 against base main; no merged-preview check was run. In the review container, I used Python 3 and the pinned repo_read.py reader. No compiler or tests were run.

python3 repo_read.py cat core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp 480 615
python3 repo_read.py cat core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp 615 690
python3 repo_read.py cat core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp 920 984
python3 repo_read.py cat core/libs/prx/libSceAgcDriver/tests/SpirvModule.cpp 575 735
python3 repo_read.py search blockNanTests core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp
python3 repo_read.py search SpecializeSpirv core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp

Results

Check Result Note
Changed-source review passed Both changed files read
Tests not run No test execution performed
Prepared review inputs passed No excluded files

Findings

Blocking

None.

Non-blocking

None.

Sources

  • The specialization clears its reuse table at function and label boundaries, excludes decorated results, and keys each candidate by opcode, result type, operands, and the select's true value when applicable. Pinned specialization source
  • The added tests cover identical inputs, different inputs, sibling blocks, differing mask/limit constants, and a different select fallback. Pinned test source

Not verified

No tests were run, and the Linux/GPU and Windows results in the description were not independently verified. Read core/libs/prx/libSceAgcDriver/tests/SpirvModule.cpp and core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp; files.json lists no excluded files.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6192730698 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28911#pullrequestreview-6192730698). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-12-04T16:32:38Z. **Review:** APPROVED --- # Reviewed — looks good The change reuses matching NaN bit tests within a block, and the changed implementation and tests support the stated behavior. ## What I ran Reviewed head `cf4d991b10ad109e657c96af321e050ffb34a870` against base `main`; no merged-preview check was run. In the review container, I used Python 3 and the pinned `repo_read.py` reader. No compiler or tests were run. ```text python3 repo_read.py cat core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp 480 615 python3 repo_read.py cat core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp 615 690 python3 repo_read.py cat core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp 920 984 python3 repo_read.py cat core/libs/prx/libSceAgcDriver/tests/SpirvModule.cpp 575 735 python3 repo_read.py search blockNanTests core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp python3 repo_read.py search SpecializeSpirv core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp ``` ## Results | Check | Result | Note | |---|---|---| | Changed-source review | passed | Both changed files read | | Tests | not run | No test execution performed | | Prepared review inputs | passed | No excluded files | ## Findings ### Blocking None. ### Non-blocking None. ## Sources - The specialization clears its reuse table at function and label boundaries, excludes decorated results, and keys each candidate by opcode, result type, operands, and the select's true value when applicable. [Pinned specialization source](https://github.com/boykopovar/AnyPS5/blob/cf4d991b10ad109e657c96af321e050ffb34a870/core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp#L518-L589) - The added tests cover identical inputs, different inputs, sibling blocks, differing mask/limit constants, and a different select fallback. [Pinned test source](https://github.com/boykopovar/AnyPS5/blob/cf4d991b10ad109e657c96af321e050ffb34a870/core/libs/prx/libSceAgcDriver/tests/SpirvModule.cpp#L636-L710) ## Not verified No tests were run, and the Linux/GPU and Windows results in the description were not independently verified. Read `core/libs/prx/libSceAgcDriver/tests/SpirvModule.cpp` and `core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp`; `files.json` lists no excluded files. ## To get this to a good state Nothing is required.
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/28911:upstream-pr/28911'
git switch 'upstream-pr/28911'

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