Skip to content

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

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
milescoviello:perf/ir-opcode-traits
Nov 14, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
milescoviello:perf/ir-opcode-traits

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

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

🤖 Generated with Claude Code

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Oct 27, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@milescoviello

Copy link
Copy Markdown
Contributor Author

Title captures for this change, the same setup as the captures on #12039 and #11967: Super Meat Boy 3D (PPSA31162 02.000.001), release libs, 100 s at a 12 GB cap with the same input script, recompiler cache off (ANYPS5_NO_SHADER_CACHE=1), a warm driver cache and APS5_TRACE_SECONDS=0:60, with 5 rounds alternating which side goes first. Linux, RTX 4070 Ti, 615.71.09. The base is 608dd76843 (main 9af563f33d + #11580). The PR side is this head (f0595a79ba) merged on top: a1a31f6512 for rounds 1 to 3 and 74c167a986, the same tree, for rounds 4 and 5.

Shader.PrepareArtifact, summed over the trace window (total ms / slices):

Round main workers this PR workers main title thread this PR title thread
1 (main first) 12,280 / 488 12,164 / 491 5,457 / 383 5,779 / 381
2 (PR first) 12,196 / 488 12,026 / 496 5,574 / 381 6,624 / 391
3 (main first) 12,079 / 494 12,187 / 493 5,614 / 392 6,525 / 387
4 (PR first) 11,731 / 483 15,008 / 497 6,257 / 392 8,540 / 396
5 (main first) 12,303 / 492 12,288 / 507 5,977 / 377 5,754 / 382
mean 12,118 12,734 (+5.1 %) 5,776 6,644 (+15.0 %)

No run reached the later scene that skewed some other PRs' captures (483 to 507 worker artifacts in all ten). Over each run's first 400 worker and first 350 title-thread artifacts, the same boot work (total ms):

Round main workers this PR workers main title thread this PR title thread
1 (main first) 9,003 8,955 5,248 5,586
2 (PR first) 9,122 8,378 5,413 6,403
3 (main first) 8,228 9,147 5,370 6,331
4 (PR first) 8,890 10,023 6,034 8,054
5 (main first) 9,031 8,483 5,831 5,607
mean 8,855 8,997 (+1.6 %) 5,579 6,396 (+14.6 %)

The title captures show no gain from this change. Over the same work the workers are 1.6 % higher (lower in 3 of 5 rounds) and the title thread 14.6 % higher (lower in 1 of 5). The median worker slice is also higher on this side in all 5 rounds (by 0 to 29 %), and round 4's PR run was slow throughout (worker median 12.3 ms against 8.1 to 9.9 ms in the other nine runs). This change only replaces four switches with a table built at compile time, and I have no explanation for a cost of this size. I don't read it as a measured regression, but there is no improvement here either. The replay task-clock measurement in the description already called it noise on main alone, and showed -2.3 % only on top of #11798, #11820 and #11967.

So I'm marking this PR as a draft. Once those three are in main I'll measure it again on top of them, and close it if it still shows nothing.

The summary lines of round 2:

{"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"}

All ten traces, summary lines and tools/trace_digest.py outputs are release assets on my fork: pr12002-smb3d-captures.

AI-assisted: yes (Claude Code)

@milescoviello
milescoviello marked this pull request as draft October 28, 2026 05:33
@github-actions github-actions Bot removed the perf numbers missing perf pull request without a before and after summary line label Oct 28, 2026
@milescoviello
milescoviello marked this pull request as ready for review November 9, 2026 17:15
@oneandonlydean oneandonlydean self-assigned this Nov 9, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi @milescoviello, thanks for marking this ready again. Reviewed at f0595a7.

#11798, #11820 and #11967 are all in main now, the last since 2 Nov. Your 28 Oct note said you would measure again on top of them. Could you post that run on current main before this merges? The paired replay task-clock from the description (the one that gave -2.3 % on the stack) would do, or the title captures if you prefer them. The code itself is fine: the SPIR-V is byte-identical and nothing else changes. The only open question is whether the change still gains anything with those three in place, since the 28 Oct captures showed none.

@milescoviello

Copy link
Copy Markdown
Contributor Author

Re-measured on current main as you asked, now that #11798, #11820 and #11967 are all in.

Setup. Base f57c971559 (current main), head = that with this PR applied (0440aa6767, a clean cherry-pick of the PR head f0595a79ba onto current main, same +52/-9 diff; GitHub reports the PR mergeable/clean against main). Paired replay task-clock, the same method as the description: agc_shader_replay --spv over the full Crimson Desert (152 requests) + MXGP 26 (305 requests) corpus with ANYPS5_NO_SHADER_CACHE=1 so every shader recompiles, release libs (SPIRV-Tools off), warm OS file cache, 10 rounds alternating which side runs first. Linux, RTX 4070 Ti, 615.71.09.

SPIR-V still byte-identical on current main: 65/65 Crimson Desert modules and 304/304 MXGP modules match, 0 differing.

Task-clock over all 457 requests:

Round (first) main this PR delta
1 (main) 17.05 s 16.64 s -2.42 %
2 (PR) 17.16 s 16.64 s -3.05 %
3 (main) 17.00 s 16.79 s -1.24 %
4 (PR) 17.10 s 16.72 s -2.21 %
5 (main) 16.97 s 16.70 s -1.59 %
6 (PR) 17.02 s 16.62 s -2.37 %
7 (main) 16.81 s 16.53 s -1.64 %
8 (PR) 16.83 s 16.59 s -1.41 %
9 (main) 16.93 s 16.57 s -2.15 %
10 (PR) 16.95 s 16.57 s -2.25 %
mean 16.98 s 16.64 s -2.03 %

The table is faster on this side in 10 of 10 rounds (mean -2.03 %, median 16.98 s vs 16.63 s). So with the three prerequisites in main the change does still pay for itself: the per-call switches are replaced by a single compile-time table read, and the recompile work over the corpus drops by about 2.0 %. Raw perf stat files, the per-round numbers and the SPIR-V hashes are reproducible from the same corpus.

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

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 5925 core/shader/recompiler/CMakeLists.txt
PR 7642 core/shader/recompiler/CMakeLists.txt
PR 9356 core/shader/recompiler/CMakeLists.txt
PR 9387 core/shader/recompiler/CMakeLists.txt
PR 10193 core/shader/recompiler/CMakeLists.txt
PR 12324 core/shader/recompiler/CMakeLists.txt
PR 12828 core/shader/recompiler/CMakeLists.txt
PR 13928 core/shader/recompiler/CMakeLists.txt
PR 13929 core/shader/recompiler/CMakeLists.txt
PR 14217 core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp
PR 15548 core/shader/recompiler/CMakeLists.txt
PR 17611 core/shader/recompiler/CMakeLists.txt
PR 18139 core/shader/recompiler/CMakeLists.txt
PR 18185 core/shader/recompiler/CMakeLists.txt
PR 18369 core/shader/recompiler/CMakeLists.txt

@Jirka135

Copy link
Copy Markdown
Contributor

Reviewed opcode access-trait table (IrOpcode.cpp) — no blocking findings

The five trait lookups return exactly what the old switches returned, by construction; the one thing the PR does not add is a test that pins this.

What I read

Head reviewed: f0595a79ba28ee024714af3852ab6c956cec5e6f (base main). I read the description, the thread (milescoviello's two measurement comments and oneandonlydean's request to re-measure, which the author's later measurement comment answers), the single commit, the full diff (one file, +52/-9), and IrOpcode.cpp and IrOpcode.hpp at head. I also searched the head tree for tests that call these functions.

No build or test was run here; this review is from reading the code only. The test results and timings in the description and comments are the author's.

Results

Check Result Note
Old vs new traits, case by case passed (by reading) The diff changes only the five function headers to static constexpr computeX and the four call sites inside computeHasSideEffects (:871, :875, :879, :883); no case label or return value is touched (IrOpcode.cpp:556, :646, :748, :814, :870).
Table covers every opcode passed (by reading) IrOpcode has no explicit values and ends in Count (IrOpcode.hpp:11-487); TraitTable loops 0..Count-1 (IrOpcode.cpp:939-946).
Out-of-range opcode passed (by reading) traitsOf returns a default OpcodeTraits (IrOpcode.cpp:948-952), which equals every old default: result (None, {}, false).
Side-effect composition passed (by reading) sideEffects is computed from the same compute* functions plus the unchanged opcode list (IrOpcode.cpp:870-929).
Test that pins the traits none found No test file at head references BufferAccessOf, SharedAccessOf, AddressOpcodeInfoOf, ImageOpcodeInfoOf or IrOpcodeHasSideEffects.
Merge with upstream/main passed git merge-tree --write-tree produced a tree with no conflict output.
Build, tests, GPU not run

Findings

Blocking

None.

Non-blocking

  1. Nothing in the PR pins the old-versus-new equivalence. The description's claim "no behaviour changes" rests on the SPIR-V being byte-identical over two corpora (the author's runs) plus the existing suites. A small test that loops 0..IrOpcode::Count and compares the public functions against a hand-written expectation for a few representative opcodes would pin the table to the old results, so a later change to the table construction cannot silently remap traits; my suggestion, not required.
  2. struct OpcodeTraits (IrOpcode.cpp:931) is declared after the anonymous namespace has closed, so unlike the static helpers around it, it has external linkage in ShaderRecompiler. Nothing clashes today; moving it into an anonymous namespace would rule out a future ODR clash with a same-named type elsewhere. Low severity.

Not verified

  • Compilation: computeX being valid in a constant expression (no non-constexpr calls in the bodies) is only inferred from reading; I did not compile it.
  • The performance numbers, the ASan runs and the Windows VM runs in the description.
  • AMD Radeon 8060S (gfx1151, AMD Windows driver) and any other vendor; the change is CPU-only, so I do not expect a difference.

To get this to a good state

Nothing is required. Optionally add the table-equivalence test from non-blocking item 1.

AI-assisted: yes (code read with Claude Code; no build or test run).

@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks for reading it closely. You're right that the table is the old switches by construction, so I'll add a test that pins a few representative opcodes through the public accessors — e.g. BufferAccessOf(LoadBufferU32) == Read, StoreBufferU32 == Write, BufferAtomicSwap32 == Atomic, a plain ALU op == None, plus one each for SharedAccessOf, AddressOpcodeInfoOf, ImageOpcodeInfoOf and IrOpcodeHasSideEffects — so a future change to a compute* function that doesn't match is caught. Will push it shortly.

AI-assisted: yes (Claude Code)

…e time

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milescoviello

Copy link
Copy Markdown
Contributor Author

Added in 4b8f060c2b (and rebased the branch onto current main while here): ir_opcode_traits pins the five accessors against representative opcodes — BufferAccessOf Read/Write/Atomic (LoadBufferU32, StoreBufferU32, BufferAtomicSwap32) and None (IAdd32); SharedAccessOf Read/Write/None; AddressOpcodeInfoOf/ImageOpcodeInfoOf default to None for a plain ALU op; and IrOpcodeHasSideEffects true for SetRegister/Discard, false for IAdd32. It builds against IrOpcode.cpp directly (that TU is self-contained) and passes 1/1 locally (ctest -R ir_opcode_traits). It's a forward guard: a later compute*() change the table no longer matches is now caught.

AI-assisted: yes (Claude Code)

Add ir_opcode_traits: asserts BufferAccessOf / SharedAccessOf / AddressOpcodeInfoOf /
ImageOpcodeInfoOf / IrOpcodeHasSideEffects return the expected trait for a spread of
opcodes, so a later change to a compute*() function the TraitTable is built from is
caught even though the table equals the functions by construction (requested by Jirka135).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@oneandonlydean
oneandonlydean merged commit 97c7f3b into boykopovar:main Nov 14, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf Performance change shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants