perf(shader): skip masked-select families that have no candidate select #7491

Merged
milescoviello merged 1 commit from upstream-pr/7491 into main 2026-10-19 22:08:44 +00:00
milescoviello commented 2026-10-18 09:22:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T09:22:02Z.


What

Every round of MaskedSelectEliminator::Eliminate runs UnreadLanes::Remove for each of the four mask families. Each Remove first walks every instruction of the program, newest block first, to find the selects whose mask belongs to its family. If it finds none, it returns 0 without changing anything, but it has already walked the whole program.

In the largest compute shader of PPSA31162 (about 152,000 IR instructions when the pass runs), each Eliminate call runs two rounds. Only one family ever has candidates: masks that are constants or are defined in a block that runs once, and that are not whole-quad masks. It has 20,614 selects, and 308 are removed in the first round. The other three families find nothing in either round. With a temporary timer, each of those empty walks took 4.7 to 8.9 ms.

removeFullWaveSelects already walks every instruction at the start of each round. It now also collects the selects of Once-region blocks that it keeps. A family runs only when one of those selects is still in its block, has uses and has a mask of that family. That is exactly the test Remove starts with. It is checked when the family's turn comes, not at the start of the round, because removing a select can change another select's mask (a SelectU1 can be a mask, and familyOf looks at the mask's sources). Removed selects have no parent, and no pass in the round adds selects. So a family is skipped only when Remove would have returned 0 without touching the program or steps, and the output is unchanged.

There's no new test, because the output doesn't change. agc_masked_select already covers loop, whole-quad and first-lane masks.

Tested

Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02.

  • ctest -R "^agc_|^shader_ir" on ccb0c09268: 708/708 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export). After rebasing onto cb1d6e5bd6: agc_masked_select, agc_shader_memory and agc_shader_srt_read_patching pass.
  • agc_shader_replay --spv (Release, ANYPS5_ENABLE_SPIRV_TOOLS off, ANYPS5_NO_SHADER_CACHE=1) on 420 shader requests I had dumped with APS5_DUMP_SHADERS: 355 from MXGP 26 (PPSA30839, two dumps), 61 from PPSA31162 and 4 from another app. On main ccb0c09268 and with this PR, all 419 .spv files that are produced have the same SHA-256, and the replay output is identical, including the one request that fails on both.

Pass time. A temporary timer around Eliminate (not part of this PR), on the replay of PPSA31162's largest compute shader (1,056,287 SPIR-V words), summed over its 2 calls, 20 runs per build, interleaved:

Build mean ± sd median
main ccb0c09268 219.5 ± 21.2 ms 215.5 ms
this PR 156.4 ± 9.2 ms 154.0 ms

The whole replay takes about 1.2 s of CPU time, so that is about 5% of it. Whole-replay task-clock (40 runs each) was 1234.0 ± 123.5 ms on main and 1182.7 ± 124.6 ms with this PR, with equal medians: another workload was using about 8 cores of the host, and the whole-replay spread is larger than the gain. I'm only claiming the pass time.

Title level: no speed claim. This is background shader preparation. I didn't run a title for it.

This is independent of #7431, which changes PlanBuilder::PatchReads in the same shader preparation.

tools/check_conventions.py --base upstream/main passes. tools/pr_overlap.py lists only #4072, which it could not compare. That PR adds one opcode to isLaneLocal in this file and doesn't touch Eliminate or removeFullWaveSelects.

Title capture

As the perf label asks, these are captures of one title and scene on one machine. MXGP26 (PPSA30839) ran from boot with APS5_TRACE_SECONDS=0:60, ANYPS5_NO_SHADER_CACHE=1 and a fresh run directory (cold AnyPS5 and NVIDIA caches), so the shaders compile inside the window. NVIDIA RTX 4070 Ti (595.99.02), Ubuntu 24.04, release libs, Xvfb display, no input. main dbac9fa15b was captured five times: before the PR captures, after them, and three more times in a row after that. Each PR capture and the first two main captures followed a rebuild of the release libraries; the last three main captures did not, and they came out lower. The change is main with this PR merged on top as a local merge commit (a40ccbfbb85a). The time is the wall time of the Shader.PrepareArtifact slices on the shader prepare threads in each trace.

capture presents frame p50 / p90 artifacts prepared prepare time, total per artifact, mean / median
main 1 194 16.7 / 32.8 ms 935 87.3 s 93.4 / 27.7 ms
main 2 195 16.7 / 34.0 ms 956 91.4 s 95.6 / 29.6 ms
main 3 166 16.7 / 28.4 ms 961 82.1 s 85.4 / 27.3 ms
main 4 161 16.7 / 27.9 ms 946 77.6 s 82.1 / 24.7 ms
main 5 183 16.7 / 22.7 ms 939 78.4 s 83.5 / 22.8 ms
this PR 183 16.7 / 28.4 ms 956 89.0 s 93.0 / 28.1 ms

The five main captures span 17.7% in total prepare time, and the 13 prepare threads run in parallel with the title, so a title capture can't resolve a recompiler change of a few percent. The replay measurement above is this change's measurement. This PR's capture falls inside the range of the five main captures.

main 1 trace, digest, main 2 trace, digest, main 3 trace, digest, main 4 trace, digest, main 5 trace, digest, this PR trace, digest

{"commit":"dbac9fa15b61","dirty":false,"env":{"APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 595.99.02","os":"Linux 6.8.0-147-generic","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":120148.301},"presents":194,"frame_ms":{"p50":16.686,"p90":32.765,"p99":2921.590,"max":112847.624},"gpu_busy_ms_per_present":6.483,"worker_busy_pct":{"queue worker 0x0":88.481,"queue worker 0x20":73.127},"top_phases_ms_per_frame":{"Driver.Packet":1000.862,"Driver.Packet.dispatch":503.561,"Driver.Dispatch":503.451,"Shader.PrepareArtifact":450.125,"Driver.Packet.memory_wait":424.003,"Vulkan.GraphicsPipeline":132.519,"Vulkan.GraphicsPipelineLibrary":126.497,"Vulkan.GraphicsPipelinePrebuild":77.312},"trace_dropped":false,"trace":"aps5-trace-dbac9fa15b61-3.json.gz"}
{"commit":"a40ccbfbb85a","dirty":false,"env":{"APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 595.99.02","os":"Linux 6.8.0-147-generic","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":114331.202},"presents":183,"frame_ms":{"p50":16.686,"p90":28.413,"p99":2002.790,"max":108334.743},"gpu_busy_ms_per_present":4.436,"worker_busy_pct":{"queue worker 0x0":88.840,"queue worker 0x20":72.209},"top_phases_ms_per_frame":{"Driver.Packet":1006.162,"Driver.Packet.dispatch":511.685,"Driver.Dispatch":511.620,"Shader.PrepareArtifact":486.076,"Driver.Packet.memory_wait":421.945,"Vulkan.GraphicsPipeline":128.324,"Vulkan.GraphicsPipelineLibrary":122.663,"Vulkan.GraphicsPipelinePrebuild":75.431},"trace_dropped":false,"trace":"aps5-trace-a40ccbfbb85a-3.json.gz"}

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 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 4070 Ti, driver 595.99.02, Ubuntu 24.04 (Linux 6.8)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4907918810 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7491). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-18T09:22:02Z. --- ### What Every round of `MaskedSelectEliminator::Eliminate` runs `UnreadLanes::Remove` for each of the four mask families. Each `Remove` first walks every instruction of the program, newest block first, to find the selects whose mask belongs to its family. If it finds none, it returns 0 without changing anything, but it has already walked the whole program. In the largest compute shader of PPSA31162 (about 152,000 IR instructions when the pass runs), each `Eliminate` call runs two rounds. Only one family ever has candidates: masks that are constants or are defined in a block that runs once, and that are not whole-quad masks. It has 20,614 selects, and 308 are removed in the first round. The other three families find nothing in either round. With a temporary timer, each of those empty walks took 4.7 to 8.9 ms. `removeFullWaveSelects` already walks every instruction at the start of each round. It now also collects the selects of `Once`-region blocks that it keeps. A family runs only when one of those selects is still in its block, has uses and has a mask of that family. That is exactly the test `Remove` starts with. It is checked when the family's turn comes, not at the start of the round, because removing a select can change another select's mask (a `SelectU1` can be a mask, and `familyOf` looks at the mask's sources). Removed selects have no parent, and no pass in the round adds selects. So a family is skipped only when `Remove` would have returned 0 without touching the program or `steps`, and the output is unchanged. There's no new test, because the output doesn't change. `agc_masked_select` already covers loop, whole-quad and first-lane masks. ### Tested Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02. - `ctest -R "^agc_|^shader_ir"` on ccb0c09268: 708/708 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export). After rebasing onto cb1d6e5bd6: `agc_masked_select`, `agc_shader_memory` and `agc_shader_srt_read_patching` pass. - `agc_shader_replay --spv` (Release, `ANYPS5_ENABLE_SPIRV_TOOLS` off, `ANYPS5_NO_SHADER_CACHE=1`) on 420 shader requests I had dumped with `APS5_DUMP_SHADERS`: 355 from MXGP 26 (PPSA30839, two dumps), 61 from PPSA31162 and 4 from another app. On `main` ccb0c09268 and with this PR, all 419 `.spv` files that are produced have the same SHA-256, and the replay output is identical, including the one request that fails on both. **Pass time.** A temporary timer around `Eliminate` (not part of this PR), on the replay of PPSA31162's largest compute shader (1,056,287 SPIR-V words), summed over its 2 calls, 20 runs per build, interleaved: | Build | mean ± sd | median | |---|---|---| | `main` ccb0c09268 | 219.5 ± 21.2 ms | 215.5 ms | | this PR | 156.4 ± 9.2 ms | 154.0 ms | The whole replay takes about 1.2 s of CPU time, so that is about 5% of it. Whole-replay `task-clock` (40 runs each) was 1234.0 ± 123.5 ms on `main` and 1182.7 ± 124.6 ms with this PR, with equal medians: another workload was using about 8 cores of the host, and the whole-replay spread is larger than the gain. I'm only claiming the pass time. **Title level: no speed claim.** This is background shader preparation. I didn't run a title for it. This is independent of #7431, which changes `PlanBuilder::PatchReads` in the same shader preparation. `tools/check_conventions.py --base upstream/main` passes. `tools/pr_overlap.py` lists only #4072, which it could not compare. That PR adds one opcode to `isLaneLocal` in this file and doesn't touch `Eliminate` or `removeFullWaveSelects`. ### Title capture As the `perf` label asks, these are captures of one title and scene on one machine. MXGP26 (PPSA30839) ran from boot with `APS5_TRACE_SECONDS=0:60`, `ANYPS5_NO_SHADER_CACHE=1` and a fresh run directory (cold AnyPS5 and NVIDIA caches), so the shaders compile inside the window. NVIDIA RTX 4070 Ti (595.99.02), Ubuntu 24.04, release libs, Xvfb display, no input. `main` dbac9fa15b61 was captured five times: before the PR captures, after them, and three more times in a row after that. Each PR capture and the first two `main` captures followed a rebuild of the release libraries; the last three `main` captures did not, and they came out lower. The change is `main` with this PR merged on top as a local merge commit (a40ccbfbb85a). The time is the wall time of the `Shader.PrepareArtifact` slices on the `shader prepare` threads in each trace. | capture | presents | frame p50 / p90 | artifacts prepared | prepare time, total | per artifact, mean / median | |---|---:|---:|---:|---:|---:| | `main` 1 | 194 | 16.7 / 32.8 ms | 935 | 87.3 s | 93.4 / 27.7 ms | | `main` 2 | 195 | 16.7 / 34.0 ms | 956 | 91.4 s | 95.6 / 29.6 ms | | `main` 3 | 166 | 16.7 / 28.4 ms | 961 | 82.1 s | 85.4 / 27.3 ms | | `main` 4 | 161 | 16.7 / 27.9 ms | 946 | 77.6 s | 82.1 / 24.7 ms | | `main` 5 | 183 | 16.7 / 22.7 ms | 939 | 78.4 s | 83.5 / 22.8 ms | | this PR | 183 | 16.7 / 28.4 ms | 956 | 89.0 s | 93.0 / 28.1 ms | The five `main` captures span 17.7% in total prepare time, and the 13 prepare threads run in parallel with the title, so a title capture can't resolve a recompiler change of a few percent. The replay measurement above is this change's measurement. This PR's capture falls inside the range of the five `main` captures. [`main` 1 trace](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main-aps5-trace-dbac9fa15b61-3.json.gz), [digest](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main-digest.txt), [`main` 2 trace](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main2-aps5-trace-dbac9fa15b61-3.json.gz), [digest](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main2-digest.txt), [`main` 3 trace](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main3-aps5-trace-dbac9fa15b61-3.json.gz), [digest](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main3-digest.txt), [`main` 4 trace](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main4-aps5-trace-dbac9fa15b61-3.json.gz), [digest](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main4-digest.txt), [`main` 5 trace](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main5-aps5-trace-dbac9fa15b61-3.json.gz), [digest](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-main5-digest.txt), [this PR trace](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-7491-aps5-trace-a40ccbfbb85a-3.json.gz), [digest](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-7491-digest.txt) ```json {"commit":"dbac9fa15b61","dirty":false,"env":{"APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 595.99.02","os":"Linux 6.8.0-147-generic","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":120148.301},"presents":194,"frame_ms":{"p50":16.686,"p90":32.765,"p99":2921.590,"max":112847.624},"gpu_busy_ms_per_present":6.483,"worker_busy_pct":{"queue worker 0x0":88.481,"queue worker 0x20":73.127},"top_phases_ms_per_frame":{"Driver.Packet":1000.862,"Driver.Packet.dispatch":503.561,"Driver.Dispatch":503.451,"Shader.PrepareArtifact":450.125,"Driver.Packet.memory_wait":424.003,"Vulkan.GraphicsPipeline":132.519,"Vulkan.GraphicsPipelineLibrary":126.497,"Vulkan.GraphicsPipelinePrebuild":77.312},"trace_dropped":false,"trace":"aps5-trace-dbac9fa15b61-3.json.gz"} {"commit":"a40ccbfbb85a","dirty":false,"env":{"APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 595.99.02","os":"Linux 6.8.0-147-generic","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":114331.202},"presents":183,"frame_ms":{"p50":16.686,"p90":28.413,"p99":2002.790,"max":108334.743},"gpu_busy_ms_per_present":4.436,"worker_busy_pct":{"queue worker 0x0":88.840,"queue worker 0x20":72.209},"top_phases_ms_per_frame":{"Driver.Packet":1006.162,"Driver.Packet.dispatch":511.685,"Driver.Dispatch":511.620,"Shader.PrepareArtifact":486.076,"Driver.Packet.memory_wait":421.945,"Vulkan.GraphicsPipeline":128.324,"Vulkan.GraphicsPipelineLibrary":122.663,"Vulkan.GraphicsPipelinePrebuild":75.431},"trace_dropped":false,"trace":"aps5-trace-a40ccbfbb85a-3.json.gz"} ``` ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 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 4070 Ti, driver 595.99.02, Ubuntu 24.04 (Linux 6.8) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-18 09:30:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T09:30:43Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4072 core/shader/recompiler/Optimization/src/MaskedSelectEliminator.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6248263680 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7491#issuecomment-6248263680). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T09:30:43Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 4072](https://www.github.com/boykopovar/AnyPS5/pull/4072) | `core/shader/recompiler/Optimization/src/MaskedSelectEliminator.cpp` |
milescoviello commented 2026-10-19 21:35:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T21:35:35Z.


The title capture the perf label asks for is now in the description, with both summary lines, the traces and the digests: MXGP26 from boot, a cold shader cache, main dbac9fa15b against main + this PR (a40ccbfbb85a). The five main captures span 17.7% in total shader prepare time, so the capture can't resolve this change, and the replay measurement in the description is still its measurement. This PR's capture falls inside the range of the five main captures.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6277886098 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7491#issuecomment-6277886098). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-19T21:35:35Z. --- The title capture the `perf` label asks for is now in the description, with both summary lines, the traces and the digests: MXGP26 from boot, a cold shader cache, `main` dbac9fa15b61 against `main` + this PR (a40ccbfbb85a). The five `main` captures span 17.7% in total shader prepare time, so the capture can't resolve this change, and the replay measurement in the description is still its measurement. This PR's capture falls inside the range of the five `main` captures. AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-19 22:09:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T22:09:50Z.


Checked that hasUnreadCandidate matches the early-out test in UnreadLanes::Remove (Once-region select with 3 args, uses, same mask family), evaluated at each family's turn so mask changes between families are seen; output unchanged. merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6278475567 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7491#issuecomment-6278475567). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T22:09:50Z. --- Checked that `hasUnreadCandidate` matches the early-out test in `UnreadLanes::Remove` (Once-region select with 3 args, uses, same mask family), evaluated at each family's turn so mask changes between families are seen; output unchanged. merged.
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!7491
No description provided.