perf(shader): reserve an IR value's arguments for its opcode at creation #7518

Merged
milescoviello merged 1 commit from upstream-pr/7518 into main 2026-10-19 22:36:27 +00:00
milescoviello commented 2026-10-18 11:13:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T11:13:09Z.


What

An IrValue grows its arguments vector one AddArgument at a time, starting empty. A value with two arguments allocates twice (capacity 1, then 2), and one with three allocates three times (1, 2, then 4), freeing the earlier blocks each time. In a profile of a Release replay of 355 MXGP 26 (PPSA30839) shader requests on main, the allocator (malloc, free and their internals) was 20% of all samples. Its largest callers were the destruction of IR programs, IrProgram::CreateValue and IrValue::AddArgument.

The constructor now reserves IrOpcodeOperandCount(opcode) arguments. That is the count IrBuilder::Emit already requires for each opcode, so a value given its opcode's arguments allocates once. Opcodes without a fixed argument list have a count of 0 and reserve nothing as before; that covers Phi, and Void, which constants and register operands use. A later AddArgument past the reserved count grows the vector as before. Nothing else changes: the IR, the order of values and the SPIR-V are the same.

Tested

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

  • ctest -R "^agc_|^shader_ir" on 9f25d0bf5b: 709/709 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export).
  • agc_shader_replay --spv (Release, ANYPS5_ENABLE_SPIRV_TOOLS off, ANYPS5_NO_SHADER_CACHE=1) on 466 shader requests I had dumped with APS5_DUMP_SHADERS: 355 from PPSA30839, 107 from PPSA31162 and 4 from another app. All 462 .spv files (a few names repeat across the dumps) have the same SHA-256 on main 9f25d0bf5b and with this PR, and the replay output is identical.

Replay CPU time (perf stat -e task-clock), interleaved:

Replay main 9f25d0bf5b this PR
PPSA31162 0x1e401c0000 (1,056,287 SPIR-V words), 15 runs each 1144.6 ± 16.6 ms 1095.3 ± 18.2 ms
PPSA30839 0x10f9f30000 (1,130,170 words), 15 runs each 1279.4 ± 24.4 ms 1215.6 ± 17.2 ms
the 355 PPSA30839 requests in one replay, 3 runs each 25,285, 25,308, 25,133 ms 24,730, 24,864, 24,907 ms

That is about 4 to 5% on the two large shaders and about 1.7% over the whole set.

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

This is independent of #7431, #7491, #7494, #7503 and #7516, which change single 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 (3c5679c7609e). 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 205 16.7 / 35.3 ms 948 99.6 s 105.0 / 35.0 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":"3c5679c7609e","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":116868.649},"presents":205,"frame_ms":{"p50":16.697,"p90":35.346,"p99":274.884,"max":110423.149},"gpu_busy_ms_per_present":5.290,"worker_busy_pct":{"queue worker 0x0":88.448,"queue worker 0x20":71.951},"top_phases_ms_per_frame":{"Driver.Packet":914.393,"Shader.PrepareArtifact":485.626,"Driver.Packet.dispatch":461.449,"Driver.Dispatch":461.393,"Driver.Packet.memory_wait":385.576,"Vulkan.GraphicsPipeline":114.363,"Vulkan.GraphicsPipelineLibrary":109.242,"Vulkan.GraphicsPipelinePrebuild":66.968},"trace_dropped":false,"trace":"aps5-trace-3c5679c7609e-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:4908587567 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7518). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-18T11:13:09Z. --- ### What An `IrValue` grows its `arguments` vector one `AddArgument` at a time, starting empty. A value with two arguments allocates twice (capacity 1, then 2), and one with three allocates three times (1, 2, then 4), freeing the earlier blocks each time. In a profile of a Release replay of 355 MXGP 26 (PPSA30839) shader requests on `main`, the allocator (`malloc`, `free` and their internals) was 20% of all samples. Its largest callers were the destruction of IR programs, `IrProgram::CreateValue` and `IrValue::AddArgument`. The constructor now reserves `IrOpcodeOperandCount(opcode)` arguments. That is the count `IrBuilder::Emit` already requires for each opcode, so a value given its opcode's arguments allocates once. Opcodes without a fixed argument list have a count of 0 and reserve nothing as before; that covers `Phi`, and `Void`, which constants and register operands use. A later `AddArgument` past the reserved count grows the vector as before. Nothing else changes: the IR, the order of values and the SPIR-V are the same. ### Tested Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02. - `ctest -R "^agc_|^shader_ir"` on 9f25d0bf5b: 709/709 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export). - `agc_shader_replay --spv` (Release, `ANYPS5_ENABLE_SPIRV_TOOLS` off, `ANYPS5_NO_SHADER_CACHE=1`) on 466 shader requests I had dumped with `APS5_DUMP_SHADERS`: 355 from PPSA30839, 107 from PPSA31162 and 4 from another app. All 462 `.spv` files (a few names repeat across the dumps) have the same SHA-256 on `main` 9f25d0bf5b and with this PR, and the replay output is identical. **Replay CPU time** (`perf stat -e task-clock`), interleaved: | Replay | `main` 9f25d0bf5b | this PR | |---|---|---| | PPSA31162 `0x1e401c0000` (1,056,287 SPIR-V words), 15 runs each | 1144.6 ± 16.6 ms | 1095.3 ± 18.2 ms | | PPSA30839 `0x10f9f30000` (1,130,170 words), 15 runs each | 1279.4 ± 24.4 ms | 1215.6 ± 17.2 ms | | the 355 PPSA30839 requests in one replay, 3 runs each | 25,285, 25,308, 25,133 ms | 24,730, 24,864, 24,907 ms | That is about 4 to 5% on the two large shaders and about 1.7% over the whole set. **Title level: no speed claim.** This is background shader preparation. I didn't run a title for it. This is independent of #7431, #7491, #7494, #7503 and #7516, which change single 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 (3c5679c7609e). 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 | 205 | 16.7 / 35.3 ms | 948 | 99.6 s | 105.0 / 35.0 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-7518-aps5-trace-3c5679c7609e-3.json.gz), [digest](https://github.com/milescoviello/AnyPS5/releases/download/perf-mxgp-2026-10-19/mxgp-7518-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":"3c5679c7609e","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":116868.649},"presents":205,"frame_ms":{"p50":16.697,"p90":35.346,"p99":274.884,"max":110423.149},"gpu_busy_ms_per_present":5.290,"worker_busy_pct":{"queue worker 0x0":88.448,"queue worker 0x20":71.951},"top_phases_ms_per_frame":{"Driver.Packet":914.393,"Shader.PrepareArtifact":485.626,"Driver.Packet.dispatch":461.449,"Driver.Dispatch":461.393,"Driver.Packet.memory_wait":385.576,"Vulkan.GraphicsPipeline":114.363,"Vulkan.GraphicsPipelineLibrary":109.242,"Vulkan.GraphicsPipelinePrebuild":66.968},"trace_dropped":false,"trace":"aps5-trace-3c5679c7609e-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:26 +00:00 (Migrated from github.com)

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


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 (3c5679c7609e). 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:6277881624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7518#issuecomment-6277881624). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-19T21:35:26Z. --- 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 (3c5679c7609e). 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:39:24 +00:00 (Migrated from github.com)

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


Checked that the reserved count is the same IrOpcodeOperandCount that IrBuilder::Emit already enforces, that variable-arity opcodes (Phi, Void) reserve nothing, and that AddArgument past the reserved count grows as before, so the IR is unchanged; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6278927688 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7518#issuecomment-6278927688). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T22:39:24Z. --- Checked that the reserved count is the same `IrOpcodeOperandCount` that `IrBuilder::Emit` already enforces, that variable-arity opcodes (Phi, Void) reserve nothing, and that `AddArgument` past the reserved count grows as before, so the IR is 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!7518
No description provided.