perf(shader): look up opcode access traits in a table built at compile time #12002

Merged
milescoviello merged 2 commits from upstream-pr/12002 into main 2026-11-14 17:33:21 +00:00
milescoviello commented 2026-10-27 16:29:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-27T16:29:03Z.


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 are below.

BufferAccessOf, SharedAccessOf, AddressOpcodeInfoOf, ImageOpcodeInfoOf and IrOpcodeHasSideEffects each run a switch over the opcode. IrOpcodeHasSideEffects runs four of them before its own. Passes call them per instruction and per use, at 61 call sites in 17 files: dead code elimination's MayHaveSideEffects, ValidateProgram, the memory and resource passes. In a replay profile with #11798, #11820 and #11967 applied, AddressOpcodeInfoOf alone is 2.7 % of samples.

This turns the five switches into constexpr functions and fills a table with all five traits for every IrOpcode at compile time. The five public functions become one indexed load. An opcode outside the enum gets the same defaults the switches' default: cases return: None accesses, value-initialized info structs, no side effects.

Measured with the shader replay over the 152 Crimson Desert (PPSA25286) pipeline requests in --prepare mode, perf stat -e task-clock, release builds, ANYPS5_NO_SHADER_CACHE=1:

Comparison Before After Change Rounds lower
on #11798 + #11820 + #11967 8.02 s mean (8.02 median) 7.83 s mean (7.83 median) −2.3 % 9 of 10 (one tie)
on main eece5da035 alone 15.60 s mean (15.59 median) 15.71 s mean (15.46 median) +0.7 % mean / −0.8 % median 6 of 10

On main eece5da035 alone the gain was inside the run-to-run noise: one 17.63 s round sets the mean. The three allocation PRs have since merged (#11798, #11820, #11967), so the gain now shows on main itself.

Current main (11-09): main 52c33518f8 against this PR merged onto it (354249cff1), release builds with SPIRV-Tools off, ANYPS5_NO_SHADER_CACHE=1, --spv over the 152 Crimson Desert and 305 MXGP 26 requests (457), perf stat -e task-clock, 10 interleaved rounds:

Comparison Before After Change Rounds lower
on main 52c33518f8 17.05 s mean (17.07 median) 16.72 s mean (16.66 median) −1.9 % 10 of 10

The SPIR-V is byte-identical there too: 0 of the 65 Crimson Desert modules and 0 of the 304 MXGP modules differ. The SPIR-V is byte-identical: all 65 modules from the Crimson Desert requests and all 302 from MXGP 26's (PPSA30839) dump, both on main and on the stack.

How

  • IrOpcode.cpp only: computeBufferAccess, computeSharedAccess, computeAddressOpcodeInfo, computeImageOpcodeInfo and computeHasSideEffects are the former bodies, now static constexpr. TraitTable is a constexpr std::array<OpcodeTraits, IrOpcode::Count> built from them.
  • The public functions keep their signatures and read the table. traitsOf returns a defaulted OpcodeTraits for an index past the table, matching the switches' default: cases.
  • No header changes and no behaviour changes.

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 (f0595a79ba).
  • 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 this head: 26 of 26 pass in all 3 runs on both sides.

tools/pr_overlap.py: #7410 (wachi35676) edits IrOpcode.cpp but already conflicts with main. It adds an opcode to the OpcodeMeta table around line 323, away from these hunks. A new opcode gets its traits from the same constexpr functions, so the table needs no separate update.

Timing method: 10 rounds per comparison in alternating or rotating order, nothing else running.

Title captures: Super Meat Boy 3D, main 9af563f33d + #11580 (608dd76843) against the same + this PR (a1a31f6512), 5 interleaved rounds, recompiler cache off: no gain. Over each run's first 400 worker artifacts the worker time is 8,855 ms → 8,997 ms (+1.6 %, lower in 3 of 5) and over the first 350 title-thread artifacts 5,579 ms → 6,396 ms (+14.6 %, lower in 1 of 5); this PR is a draft until #11798, #11820 and #11967 land and it is measured again on them (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":60158.233},"presents":420,"frame_ms":{"p50":124.781,"p90":139.667,"p99":1419.083,"max":4015.253},"gpu_busy_ms_per_present":88.033,"worker_busy_pct":{"queue worker 0x0":85.732,"queue worker 0x20":84.407},"top_phases_ms_per_frame":{"Driver.Packet":243.633,"Driver.Packet.memory_wait":121.004,"Driver.Packet.dispatch":57.563,"Driver.Dispatch":57.420,"Shader.PrepareArtifact":42.312,"Recorder.Sync":40.213,"Driver.Packet.pipeline_drain":39.650,"GpuMutex.wait.hook":38.101},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":39.549,"events_lost":320829,"totals_full":false}},"trace":"aps5-trace-608dd7684303-3.json.gz"}
{"commit":"a1a31f651200","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":60078.578},"presents":428,"frame_ms":{"p50":123.465,"p90":139.994,"p99":1351.894,"max":4114.469},"gpu_busy_ms_per_present":86.035,"worker_busy_pct":{"queue worker 0x0":86.739,"queue worker 0x20":83.681},"top_phases_ms_per_frame":{"Driver.Packet":239.154,"Driver.Packet.memory_wait":116.715,"Driver.Packet.dispatch":56.870,"Driver.Dispatch":56.693,"Shader.PrepareArtifact":43.577,"Driver.Packet.pipeline_drain":39.284,"Recorder.Sync":38.871,"GpuMutex.wait.hook":36.450},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":38.668,"events_lost":345148,"totals_full":false}},"trace":"aps5-trace-a1a31f651200-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, #11820 and #11967; the second timing row has all three applied)
  • 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:5037484209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12002). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-27T16:29:03Z. --- ### 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 are below. `BufferAccessOf`, `SharedAccessOf`, `AddressOpcodeInfoOf`, `ImageOpcodeInfoOf` and `IrOpcodeHasSideEffects` each run a `switch` over the opcode. `IrOpcodeHasSideEffects` runs four of them before its own. Passes call them per instruction and per use, at 61 call sites in 17 files: dead code elimination's `MayHaveSideEffects`, `ValidateProgram`, the memory and resource passes. In a replay profile with #11798, #11820 and #11967 applied, `AddressOpcodeInfoOf` alone is 2.7 % of samples. This turns the five `switch`es into `constexpr` functions and fills a table with all five traits for every `IrOpcode` at compile time. The five public functions become one indexed load. An opcode outside the enum gets the same defaults the `switch`es' `default:` cases return: `None` accesses, value-initialized info structs, no side effects. Measured with the shader replay over the 152 Crimson Desert (PPSA25286) pipeline requests in `--prepare` mode, `perf stat -e task-clock`, release builds, `ANYPS5_NO_SHADER_CACHE=1`: | Comparison | Before | After | Change | Rounds lower | |---|---|---|---|---| | on #11798 + #11820 + #11967 | 8.02 s mean (8.02 median) | 7.83 s mean (7.83 median) | −2.3 % | 9 of 10 (one tie) | | on `main` `eece5da035` alone | 15.60 s mean (15.59 median) | 15.71 s mean (15.46 median) | +0.7 % mean / −0.8 % median | 6 of 10 | On `main` `eece5da035` alone the gain was inside the run-to-run noise: one 17.63 s round sets the mean. The three allocation PRs have since merged (#11798, #11820, #11967), so the gain now shows on `main` itself. **Current `main` (11-09):** `main` `52c33518f8` against this PR merged onto it (`354249cff1`), release builds with SPIRV-Tools off, `ANYPS5_NO_SHADER_CACHE=1`, `--spv` over the 152 Crimson Desert and 305 MXGP 26 requests (457), `perf stat -e task-clock`, 10 interleaved rounds: | Comparison | Before | After | Change | Rounds lower | |---|---|---|---|---| | on `main` `52c33518f8` | 17.05 s mean (17.07 median) | 16.72 s mean (16.66 median) | −1.9 % | 10 of 10 | The SPIR-V is byte-identical there too: 0 of the 65 Crimson Desert modules and 0 of the 304 MXGP modules differ. The SPIR-V is byte-identical: all 65 modules from the Crimson Desert requests and all 302 from MXGP 26's (PPSA30839) dump, both on `main` and on the stack. ### How - `IrOpcode.cpp` only: `computeBufferAccess`, `computeSharedAccess`, `computeAddressOpcodeInfo`, `computeImageOpcodeInfo` and `computeHasSideEffects` are the former bodies, now `static constexpr`. `TraitTable` is a `constexpr std::array<OpcodeTraits, IrOpcode::Count>` built from them. - The public functions keep their signatures and read the table. `traitsOf` returns a defaulted `OpcodeTraits` for an index past the table, matching the `switch`es' `default:` cases. - No header changes and no behaviour changes. ### 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 (`f0595a79ba`). - 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 this head: 26 of 26 pass in all 3 runs on both sides. `tools/pr_overlap.py`: #7410 (wachi35676) edits `IrOpcode.cpp` but already conflicts with `main`. It adds an opcode to the `OpcodeMeta` table around line 323, away from these hunks. A new opcode gets its traits from the same `constexpr` functions, so the table needs no separate update. Timing method: 10 rounds per comparison in alternating or rotating order, nothing else running. **Title captures:** Super Meat Boy 3D, `main` `9af563f33d` + #11580 (`608dd76843`) against the same + this PR (`a1a31f6512`), 5 interleaved rounds, recompiler cache off: no gain. Over each run's first 400 worker artifacts the worker time is 8,855 ms → 8,997 ms (+1.6 %, lower in 3 of 5) and over the first 350 title-thread artifacts 5,579 ms → 6,396 ms (+14.6 %, lower in 1 of 5); this PR is a draft until #11798, #11820 and #11967 land and it is measured again on them ([table and traces](https://github.com/milescoviello/AnyPS5/releases/tag/pr12002-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":60158.233},"presents":420,"frame_ms":{"p50":124.781,"p90":139.667,"p99":1419.083,"max":4015.253},"gpu_busy_ms_per_present":88.033,"worker_busy_pct":{"queue worker 0x0":85.732,"queue worker 0x20":84.407},"top_phases_ms_per_frame":{"Driver.Packet":243.633,"Driver.Packet.memory_wait":121.004,"Driver.Packet.dispatch":57.563,"Driver.Dispatch":57.420,"Shader.PrepareArtifact":42.312,"Recorder.Sync":40.213,"Driver.Packet.pipeline_drain":39.650,"GpuMutex.wait.hook":38.101},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":39.549,"events_lost":320829,"totals_full":false}},"trace":"aps5-trace-608dd7684303-3.json.gz"} {"commit":"a1a31f651200","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":60078.578},"presents":428,"frame_ms":{"p50":123.465,"p90":139.994,"p99":1351.894,"max":4114.469},"gpu_busy_ms_per_present":86.035,"worker_busy_pct":{"queue worker 0x0":86.739,"queue worker 0x20":83.681},"top_phases_ms_per_frame":{"Driver.Packet":239.154,"Driver.Packet.memory_wait":116.715,"Driver.Packet.dispatch":56.870,"Driver.Dispatch":56.693,"Shader.PrepareArtifact":43.577,"Driver.Packet.pipeline_drain":39.284,"Recorder.Sync":38.871,"GpuMutex.wait.hook":36.450},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":38.668,"events_lost":345148,"totals_full":false}},"trace":"aps5-trace-a1a31f651200-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, #11820 and #11967; the second timing row has all three applied) - [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!12002
No description provided.