perf(shader): allocate a program's instruction list nodes from a pool #11967

Merged
milescoviello merged 1 commit from upstream-pr/11967 into main 2026-10-28 09:53:41 +00:00
milescoviello commented 2026-10-27 14:06:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-27T14:06:35Z.


What

Performance evidence: this is a CPU-only change to shader compilation, and the SPIR-V is byte-identical. oneandonlydean accepted paired replay timings for this class of change instead of title captures (#9132, #9198, #11719); the timings and a profile are below.

Every IrBlock keeps its instructions in a std::list<IrValue*>, so each instruction a pass appends, inserts or erases is one operator new/delete of a 24-byte list node. The nodes end up scattered across the heap. Passes walk these lists over and over, ValidateProgram, dead code elimination, the masked-select and resource passes among them, and the node loads are where much of their time goes. In a profile of the shader replay, the hottest instructions in ValidateProgram were the loads of node->value while walking a block.

This keeps each program's list nodes in a pool owned by the program. IrBlock holds a std::pmr::list<IrValue*> (IrInstructionList). IrProgram::CreateBlock gives each block a shared std::pmr::unsynchronized_pool_resource, so a block's nodes are allocated next to each other and freed into the pool's free lists instead of the heap.

Measured with the shader replay over the 152 Crimson Desert (PPSA25286) pipeline requests in --prepare mode, the same corpus as #11798, #11820 and #11719:

Build Mean task-clock Median Rounds lower Max RSS
main eece5da035 15.60 s 15.59 s 265.0 MB
this PR 10.00 s (−35.9 %) 9.93 s 10 of 10 266.2 MB
#11798 + #11820 10.59 s 10.49 s 257.1 MB
#11798 + #11820 + this PR 8.07 s (−23.7 % vs the two) 8.04 s 10 of 10 258.4 MB

Together with #11798 and #11820, the replay takes 48.2 % less time than on main. Max RSS grows by about 1 MB (+0.5 %), because the pool keeps freed nodes for reuse until the program is destroyed.

Where the time goes, from perf record -F 499 of one replay of the same corpus on each side (samples, not percentages):

Function main this PR
whole run 9,921 5,742
allocator (malloc, free and their internals) 2,613 1,498
ValidateProgram 583 291
ConstantFolder::Fold 430 54
IrValue::HasUses 403 168
IrValue::Parent 268 154
RemoveRegisterStatePseudos 185 26
DeadCodeEliminator::RemoveIdentities 172 50

The allocator saves about 1,100 samples. The passes that walk the instruction lists save more again, because their nodes now sit together in memory instead of wherever malloc put them.

The SPIR-V is byte-identical to main's: all 65 modules from the Crimson Desert requests and all 302 from MXGP 26's (PPSA30839) dump. The replay logs match too. This only changes where list nodes live; instruction order, iteration and erase semantics are those of std::list.

How

  • IrBlock.hpp: using IrInstructionList = std::pmr::list<IrValue*>. Instructions() returns it. A new constructor takes the shared resource. The resource's shared_ptr is declared before the list, so it outlives the list's nodes. IrBlock(id) without a resource keeps the default (new/delete) resource, which is what blocks made outside a program use (resource plans, the disk cache, tests).
  • IrProgram: one shared_ptr<std::pmr::memory_resource>, created on the first CreateBlock. Each block holds a reference, so the pool lives until the last block is gone. That keeps the implicit move constructor and move assignment correct: a moved-from or reassigned program's blocks still own their pool, and nothing needs ordering.
  • An unsynchronized_pool_resource isn't thread-safe. A program's blocks are only changed by the thread compiling it, as they already must be for std::list. No pass shares a program's lists across threads.
  • No other file names the list type. Callers use auto& and iterators, and no code splices or swaps instruction lists between blocks, which pmr lists with different resources wouldn't allow.
  • std::pmr::unsynchronized_pool_resource needs a libc++ with <memory_resource> (macOS 14 deployment target or newer). The repository sets no deployment target, and macOS CI builds only the relinker.

Tested

Linux (Ubuntu 26.04, GCC 15.2):

  • The 46 test targets whose sources use the IR API, ctest 3 runs: all 75 entries pass in every run at this head (007c0bf37e).
  • AddressSanitizer: agc_ir_reclaim, agc_shader_memory and agc_validate_program, 3 runs: all pass, with no AddressSanitizer or LeakSanitizer report.
  • Windows 11 VM (GeForce GT 1030, WinLibs GCC, Release), 20 IR test targets (26 ctest entries), 3 runs each on main and on 9f01a1aedf (this change plus an #include <memory_resource> in IrProgram.hpp, since dropped because IrBlock.hpp includes it): 26 of 26 pass in all 3 runs on both sides.

Title captures, Black Ops II with a cold recompiler cache, 3 interleaved runs per side, each side with #9656 (details and summary lines). Prepared artifacts take about 12 % less time on the prepare workers and about 14 % less on the title thread. The worst frame, a synchronous shader-preparation hitch at boot, is about 120 ms shorter in all three pairs. No frame-rate claim.

Timing method: release agc_shader_replay builds (SPIR-V Tools off, as users build) of each side, perf stat -e task-clock and /usr/bin/time max RSS. 10 rounds of the five builds in a rotating order, ANYPS5_NO_SHADER_CACHE=1, nothing else running.

Title captures: Super Meat Boy 3D, main 9af563f33d + #11580 (608dd76843) against the same + this PR (94d925a0c8), 3 interleaved rounds, recompiler cache off. Shader.PrepareArtifact worker time is 23,694 ms → 13,377 ms (-43.5 %, lower in 3 of 3); over each run's first 400 worker artifacts 9,331 ms → 5,841 ms (-37.4 %) and over the first 350 title-thread artifacts 5,789 ms → 3,860 ms (-33.3 %), 3 of 3 each (table and traces). Round 2's summary lines:

{"commit":"608dd7684303","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.0.0-38-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":30993,"title_id":"PPSA31162","title":"Super Meat Boy 3D","content_version":"02.000.001","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":60067.963},"presents":364,"frame_ms":{"p50":120.787,"p90":212.200,"p99":1795.082,"max":4033.987},"gpu_busy_ms_per_present":72.093,"worker_busy_pct":{"queue worker 0x0":78.182,"queue worker 0x20":79.056},"top_phases_ms_per_frame":{"Driver.Packet":259.344,"Driver.Packet.memory_wait":127.655,"Shader.PrepareArtifact":111.759,"Driver.Packet.dispatch":52.951,"Driver.Dispatch":52.549,"Driver.Packet.pipeline_drain":40.325,"Graphics.Draw":36.188,"Graphics.Draw.draw_and_resource_release":32.884},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":39.531,"events_lost":368447,"totals_full":false}},"trace":"aps5-trace-608dd7684303-3.json.gz"}
{"commit":"94d925a0c88f","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.0.0-38-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":30993,"title_id":"PPSA31162","title":"Super Meat Boy 3D","content_version":"02.000.001","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":60014.936},"presents":402,"frame_ms":{"p50":116.878,"p90":203.910,"p99":1378.808,"max":4604.644},"gpu_busy_ms_per_present":72.413,"worker_busy_pct":{"queue worker 0x0":85.499,"queue worker 0x20":83.292},"top_phases_ms_per_frame":{"Driver.Packet":251.909,"Driver.Packet.memory_wait":120.786,"Shader.PrepareArtifact":51.943,"Driver.Packet.dispatch":49.983,"Driver.Dispatch":49.788,"Driver.Packet.pipeline_drain":40.683,"Graphics.Draw":37.030,"Graphics.Draw.draw_and_resource_release":33.917},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":39.970,"events_lost":442602,"totals_full":false}},"trace":"aps5-trace-94d925a0c88f-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 (merges cleanly with #11798 and #11820)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (CPU-only shader compilation; SPIR-V identical)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5035261598 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11967). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-27T14:06:35Z. --- ### What **Performance evidence:** this is a CPU-only change to shader compilation, and the SPIR-V is byte-identical. oneandonlydean accepted paired replay timings for this class of change instead of title captures (#9132, #9198, #11719); the timings and a profile are below. Every `IrBlock` keeps its instructions in a `std::list<IrValue*>`, so each instruction a pass appends, inserts or erases is one `operator new`/`delete` of a 24-byte list node. The nodes end up scattered across the heap. Passes walk these lists over and over, `ValidateProgram`, dead code elimination, the masked-select and resource passes among them, and the node loads are where much of their time goes. In a profile of the shader replay, the hottest instructions in `ValidateProgram` were the loads of `node->value` while walking a block. This keeps each program's list nodes in a pool owned by the program. `IrBlock` holds a `std::pmr::list<IrValue*>` (`IrInstructionList`). `IrProgram::CreateBlock` gives each block a shared `std::pmr::unsynchronized_pool_resource`, so a block's nodes are allocated next to each other and freed into the pool's free lists instead of the heap. Measured with the shader replay over the 152 Crimson Desert (PPSA25286) pipeline requests in `--prepare` mode, the same corpus as #11798, #11820 and #11719: | Build | Mean task-clock | Median | Rounds lower | Max RSS | |---|---|---|---|---| | `main` `eece5da035` | 15.60 s | 15.59 s | | 265.0 MB | | this PR | 10.00 s (−35.9 %) | 9.93 s | 10 of 10 | 266.2 MB | | #11798 + #11820 | 10.59 s | 10.49 s | | 257.1 MB | | #11798 + #11820 + this PR | 8.07 s (−23.7 % vs the two) | 8.04 s | 10 of 10 | 258.4 MB | Together with #11798 and #11820, the replay takes 48.2 % less time than on `main`. Max RSS grows by about 1 MB (+0.5 %), because the pool keeps freed nodes for reuse until the program is destroyed. Where the time goes, from `perf record -F 499` of one replay of the same corpus on each side (samples, not percentages): | Function | `main` | this PR | |---|---|---| | whole run | 9,921 | 5,742 | | allocator (`malloc`, `free` and their internals) | 2,613 | 1,498 | | `ValidateProgram` | 583 | 291 | | `ConstantFolder::Fold` | 430 | 54 | | `IrValue::HasUses` | 403 | 168 | | `IrValue::Parent` | 268 | 154 | | `RemoveRegisterStatePseudos` | 185 | 26 | | `DeadCodeEliminator::RemoveIdentities` | 172 | 50 | The allocator saves about 1,100 samples. The passes that walk the instruction lists save more again, because their nodes now sit together in memory instead of wherever `malloc` put them. The SPIR-V is byte-identical to `main`'s: all 65 modules from the Crimson Desert requests and all 302 from MXGP 26's (PPSA30839) dump. The replay logs match too. This only changes where list nodes live; instruction order, iteration and erase semantics are those of `std::list`. ### How - `IrBlock.hpp`: `using IrInstructionList = std::pmr::list<IrValue*>`. `Instructions()` returns it. A new constructor takes the shared resource. The resource's `shared_ptr` is declared before the list, so it outlives the list's nodes. `IrBlock(id)` without a resource keeps the default (`new`/`delete`) resource, which is what blocks made outside a program use (resource plans, the disk cache, tests). - `IrProgram`: one `shared_ptr<std::pmr::memory_resource>`, created on the first `CreateBlock`. Each block holds a reference, so the pool lives until the last block is gone. That keeps the implicit move constructor and move assignment correct: a moved-from or reassigned program's blocks still own their pool, and nothing needs ordering. - An `unsynchronized_pool_resource` isn't thread-safe. A program's blocks are only changed by the thread compiling it, as they already must be for `std::list`. No pass shares a program's lists across threads. - No other file names the list type. Callers use `auto&` and iterators, and no code splices or swaps instruction lists between blocks, which `pmr` lists with different resources wouldn't allow. - `std::pmr::unsynchronized_pool_resource` needs a libc++ with `<memory_resource>` (macOS 14 deployment target or newer). The repository sets no deployment target, and macOS CI builds only the relinker. ### Tested Linux (Ubuntu 26.04, GCC 15.2): - The 46 test targets whose sources use the IR API, `ctest` 3 runs: all 75 entries pass in every run at this head (`007c0bf37e`). - AddressSanitizer: `agc_ir_reclaim`, `agc_shader_memory` and `agc_validate_program`, 3 runs: all pass, with no AddressSanitizer or LeakSanitizer report. - Windows 11 VM (GeForce GT 1030, WinLibs GCC, Release), 20 IR test targets (26 ctest entries), 3 runs each on `main` and on `9f01a1aedf` (this change plus an `#include <memory_resource>` in `IrProgram.hpp`, since dropped because `IrBlock.hpp` includes it): 26 of 26 pass in all 3 runs on both sides. Title captures, Black Ops II with a cold recompiler cache, 3 interleaved runs per side, each side with #9656 ([details and summary lines](https://github.com/boykopovar/AnyPS5/pull/11967#issuecomment-6445322283)). Prepared artifacts take about 12 % less time on the prepare workers and about 14 % less on the title thread. The worst frame, a synchronous shader-preparation hitch at boot, is about 120 ms shorter in all three pairs. No frame-rate claim. Timing method: release `agc_shader_replay` builds (SPIR-V Tools off, as users build) of each side, `perf stat -e task-clock` and `/usr/bin/time` max RSS. 10 rounds of the five builds in a rotating order, `ANYPS5_NO_SHADER_CACHE=1`, nothing else running. **Title captures:** Super Meat Boy 3D, `main` `9af563f33d` + #11580 (`608dd76843`) against the same + this PR (`94d925a0c8`), 3 interleaved rounds, recompiler cache off. `Shader.PrepareArtifact` worker time is 23,694 ms → 13,377 ms (-43.5 %, lower in 3 of 3); over each run's first 400 worker artifacts 9,331 ms → 5,841 ms (-37.4 %) and over the first 350 title-thread artifacts 5,789 ms → 3,860 ms (-33.3 %), 3 of 3 each ([table and traces](https://github.com/milescoviello/AnyPS5/releases/tag/pr11967-smb3d-captures)). Round 2's summary lines: ``` {"commit":"608dd7684303","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.0.0-38-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":30993,"title_id":"PPSA31162","title":"Super Meat Boy 3D","content_version":"02.000.001","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":60067.963},"presents":364,"frame_ms":{"p50":120.787,"p90":212.200,"p99":1795.082,"max":4033.987},"gpu_busy_ms_per_present":72.093,"worker_busy_pct":{"queue worker 0x0":78.182,"queue worker 0x20":79.056},"top_phases_ms_per_frame":{"Driver.Packet":259.344,"Driver.Packet.memory_wait":127.655,"Shader.PrepareArtifact":111.759,"Driver.Packet.dispatch":52.951,"Driver.Dispatch":52.549,"Driver.Packet.pipeline_drain":40.325,"Graphics.Draw":36.188,"Graphics.Draw.draw_and_resource_release":32.884},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":39.531,"events_lost":368447,"totals_full":false}},"trace":"aps5-trace-608dd7684303-3.json.gz"} {"commit":"94d925a0c88f","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.0.0-38-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":30993,"title_id":"PPSA31162","title":"Super Meat Boy 3D","content_version":"02.000.001","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":60014.936},"presents":402,"frame_ms":{"p50":116.878,"p90":203.910,"p99":1378.808,"max":4604.644},"gpu_busy_ms_per_present":72.413,"worker_busy_pct":{"queue worker 0x0":85.499,"queue worker 0x20":83.292},"top_phases_ms_per_frame":{"Driver.Packet":251.909,"Driver.Packet.memory_wait":120.786,"Shader.PrepareArtifact":51.943,"Driver.Packet.dispatch":49.983,"Driver.Dispatch":49.788,"Driver.Packet.pipeline_drain":40.683,"Graphics.Draw":37.030,"Graphics.Draw.draw_and_resource_release":33.917},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":39.970,"events_lost":442602,"totals_full":false}},"trace":"aps5-trace-94d925a0c88f-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 (merges cleanly with #11798 and #11820) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a (CPU-only shader compilation; SPIR-V identical) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!11967
No description provided.