perf(shader): mark live values of a reclaimed program without a hash set #7503

Merged
milescoviello merged 1 commit from upstream-pr/7503 into main 2026-10-19 22:17:56 +00:00
milescoviello commented 2026-10-18 09:50:30 +00:00 (Migrated from github.com)

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


What

IrProgram::ReclaimUnusedValues runs once for each resource program (ResourceProgram in Recompiler.cpp). It marked every live value by inserting it into an std::unordered_set<IrValue*>, which costs one node allocation per value plus rehashes, and then looked every value of the program up in that set three more times. In a profile of a Release replay of 355 shader requests from MXGP 26 (PPSA30839), it was 7.1% of all samples (inclusive). Most of that was the set: its lookups and inserts, and the malloc and free of its nodes.

Values enter values only through CreateValue, which numbers them with nextValueId. So an id below nextValueId names at most one value of the program. The marks of those values now live in an id-indexed std::vector<bool>, after a check that values holds the same pointer under that id. This is the scheme IdIndex in the same file already uses for ValidateProgram. A value the program does not own still goes into a set as before. That covers the resource plan's valueStorage, which can reuse an id (agc_ir_reclaim does that on purpose). The roots, the walk over arguments, the error for a removed value with a user, and the values kept and removed are unchanged.

Tested

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

  • ctest -R "^agc_|^shader_ir" on 9caecfd2d5: 709/709 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export).
  • agc_ir_reclaim passes. It covers reclaiming detached and cyclic values, keeping values that only metadata or the resource plan holds (a valueStorage value with a reused id among them), the error for an untracked external user, and moves of the program.
  • 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 PPSA30839 (two dumps), 61 from PPSA31162 and 4 from another app. All 419 .spv files that are produced have the same SHA-256 as on main 9caecfd2d5. The replay output also matches main cb1d6e5bd6 line for line, including the one request that fails on both. (Against 9caecfd2d5 I only compared it with the timer below built in, and that run's output differs only by the timer's own lines.)

Pass time, from a temporary timer in ReclaimUnusedValues that is not part of this PR, on main 9caecfd2d5 and with this PR, interleaved:

Replay main this PR
the 355 PPSA30839 requests (320 calls), 3 runs each 1,802.4, 1,779.3, 1,807.0 ms 883.7, 877.9, 876.4 ms
PPSA31162's largest compute shader (1 call), 5 runs each 81.7 to 83.2 ms 28.4 to 29.4 ms

Whole-replay task-clock of the 355 requests was 25,153 and 25,115 ms on main and 24,420 and 25,000 ms with this PR. That run-to-run spread is about as large as the gain, so 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, #7491 and #7494, which change other passes of the same shader preparation.

tools/check_conventions.py --base upstream/main passes. tools/pr_overlap.py reports no overlap with open pull requests.

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 (d0211cb37d95). 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 182 16.7 / 31.9 ms 959 91.4 s 95.3 / 30.5 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 is above all five main captures in total and median prepare time. With one capture of the change I can't tell that apart from noise, and the replay measurement above points the other way. I can repeat the capture if that would help.

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":"d0211cb37d95","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":118522.766},"presents":182,"frame_ms":{"p50":16.690,"p90":31.935,"p99":1701.050,"max":112801.025},"gpu_busy_ms_per_present":4.772,"worker_busy_pct":{"queue worker 0x0":89.569,"queue worker 0x20":71.093},"top_phases_ms_per_frame":{"Driver.Packet":1046.256,"Driver.Packet.dispatch":539.847,"Driver.Dispatch":539.692,"Shader.PrepareArtifact":502.129,"Driver.Packet.memory_wait":432.225,"Vulkan.GraphicsPipeline":130.469,"Vulkan.GraphicsPipelineLibrary":124.477,"Vulkan.GraphicsPipelinePrebuild":77.830},"trace_dropped":false,"trace":"aps5-trace-d0211cb37d95-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:4908097192 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7503). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-18T09:50:30Z. --- ### What `IrProgram::ReclaimUnusedValues` runs once for each resource program (`ResourceProgram` in `Recompiler.cpp`). It marked every live value by inserting it into an `std::unordered_set<IrValue*>`, which costs one node allocation per value plus rehashes, and then looked every value of the program up in that set three more times. In a profile of a Release replay of 355 shader requests from MXGP 26 (PPSA30839), it was 7.1% of all samples (inclusive). Most of that was the set: its lookups and inserts, and the `malloc` and `free` of its nodes. Values enter `values` only through `CreateValue`, which numbers them with `nextValueId`. So an id below `nextValueId` names at most one value of the program. The marks of those values now live in an id-indexed `std::vector<bool>`, after a check that `values` holds the same pointer under that id. This is the scheme `IdIndex` in the same file already uses for `ValidateProgram`. A value the program does not own still goes into a set as before. That covers the resource plan's `valueStorage`, which can reuse an id (`agc_ir_reclaim` does that on purpose). The roots, the walk over arguments, the error for a removed value with a user, and the values kept and removed are unchanged. ### Tested Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02. - `ctest -R "^agc_|^shader_ir"` on 9caecfd2d5: 709/709 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export). - `agc_ir_reclaim` passes. It covers reclaiming detached and cyclic values, keeping values that only metadata or the resource plan holds (a `valueStorage` value with a reused id among them), the error for an untracked external user, and moves of the program. - `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 PPSA30839 (two dumps), 61 from PPSA31162 and 4 from another app. All 419 `.spv` files that are produced have the same SHA-256 as on `main` 9caecfd2d5. The replay output also matches `main` cb1d6e5bd6 line for line, including the one request that fails on both. (Against 9caecfd2d5 I only compared it with the timer below built in, and that run's output differs only by the timer's own lines.) **Pass time**, from a temporary timer in `ReclaimUnusedValues` that is not part of this PR, on `main` 9caecfd2d5 and with this PR, interleaved: | Replay | `main` | this PR | |---|---|---| | the 355 PPSA30839 requests (320 calls), 3 runs each | 1,802.4, 1,779.3, 1,807.0 ms | 883.7, 877.9, 876.4 ms | | PPSA31162's largest compute shader (1 call), 5 runs each | 81.7 to 83.2 ms | 28.4 to 29.4 ms | Whole-replay `task-clock` of the 355 requests was 25,153 and 25,115 ms on `main` and 24,420 and 25,000 ms with this PR. That run-to-run spread is about as large as the gain, so 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, #7491 and #7494, which change other passes of the same shader preparation. `tools/check_conventions.py --base upstream/main` passes. `tools/pr_overlap.py` reports no overlap with open pull requests. ### 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 (d0211cb37d95). 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 | 182 | 16.7 / 31.9 ms | 959 | 91.4 s | 95.3 / 30.5 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 is above all five `main` captures in total and median prepare time. With one capture of the change I can't tell that apart from noise, and the replay measurement above points the other way. I can repeat the capture if that would help. [`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-7503-aps5-trace-d0211cb37d95-3.json.gz), [digest](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-7503-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":"d0211cb37d95","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":118522.766},"presents":182,"frame_ms":{"p50":16.690,"p90":31.935,"p99":1701.050,"max":112801.025},"gpu_busy_ms_per_present":4.772,"worker_busy_pct":{"queue worker 0x0":89.569,"queue worker 0x20":71.093},"top_phases_ms_per_frame":{"Driver.Packet":1046.256,"Driver.Packet.dispatch":539.847,"Driver.Dispatch":539.692,"Shader.PrepareArtifact":502.129,"Driver.Packet.memory_wait":432.225,"Vulkan.GraphicsPipeline":130.469,"Vulkan.GraphicsPipelineLibrary":124.477,"Vulkan.GraphicsPipelinePrebuild":77.830},"trace_dropped":false,"trace":"aps5-trace-d0211cb37d95-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)
milescoviello commented 2026-10-19 21:35:30 +00:00 (Migrated from github.com)

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


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 (d0211cb37d95). 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 is above all five main captures in total and median prepare time. With one capture of the change I can't tell that apart from noise, and the replay measurement above points the other way. I can repeat the capture if that would help.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6277883732 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7503#issuecomment-6277883732). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-19T21:35:30Z. --- 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 (d0211cb37d95). 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 is above all five `main` captures in total and median prepare time. With one capture of the change I can't tell that apart from noise, and the replay measurement above points the other way. I can repeat the capture if that would help. AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-19 22:21:26 +00:00 (Migrated from github.com)

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


Checked the diff: values is only filled through CreateValue with nextValueId, so ids of owned values are unique and the pointer check keeps foreign values (reused ids in the resource plan) in the fallback set. Roots, argument walk, the external-user error and the removal set are unchanged; agc_ir_reclaim covers the reused-id case. Merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6278650409 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7503#issuecomment-6278650409). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T22:21:26Z. --- Checked the diff: `values` is only filled through `CreateValue` with `nextValueId`, so ids of owned values are unique and the pointer check keeps foreign values (reused ids in the resource plan) in the fallback set. Roots, argument walk, the external-user error and the removal set are unchanged; `agc_ir_reclaim` covers the reused-id case. 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!7503
No description provided.