perf(shader): reuse a repeated NaN bit test in a block when specializing #28911
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/28911"
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 oneandonlydean on 2026-12-04T15:27:11Z.
What
SpecializeSpirvkeeps a per-block table that turns a repeatedOpBitcastand a repeatedOpIsNan(orOpFUnordNotEqual 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 0x7fffffffOpUGreaterThan y 0x7f800000OpSelect 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_eliminationcounts repeats per block and then fails in a build without the SPIR-V tools (6 repeats in its wave64 module).mainholds 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
main922efd5473.agc_driver_f32_arithmetic_nan,agc_driver_f32_arithmetic_nan_fast,agc_driver_fma_mix_f32_nan_sourcespass.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.f844ec4a17with this change, tools OFF:agc_redundant_value_eliminationpasses, 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).
agc_shader_replay)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 (
OpBitwiseAndin five,OpUGreaterThanin 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-04T15:57:20Z.
Review: APPROVED
Verified head
cf4d991b10on Windows 11 with GCC 15.2 andANYPS5_ENABLE_SPIRV_TOOLS=OFF, applied to main484b0368d3. 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_sourcesandagc_redundant_value_elimination, run withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationandVK_LOADER_DEBUG=layer(layer 1.4.363).Each log shows the loader inserting the validation layer and none contains a
VUID-.Negative check: with the new test kept and
SpirvSpecialization.cppreverted to main,agc_spirv_modulefails 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)
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
cf4d991b10ad109e657c96af321e050ffb34a870against basemain; no merged-preview check was run. In the review container, I used Python 3 and the pinnedrepo_read.pyreader. No compiler or tests were run.Results
Findings
Blocking
None.
Non-blocking
None.
Sources
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.cppandcore/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp;files.jsonlists no excluded files.To get this to a good state
Nothing is required.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.