Skip to content

spirv_opt: gate rules by opcode and scan in bulk, about 28% less CPU, byte-identical output - #7060

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/spirv-opt-hotpath
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/spirv-opt-hotpath

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted (Claude Code). Claims the item in #6910 ("claiming: new BUILD item — tools/spirv_opt.py hot path").

#4760 turns --opt on for all 26,503 shaders, so tools/spirv_opt.py is on the whole shader build's critical path. This makes optimize() about 28% cheaper in CPU time with byte-identical output and no peak-memory change. It adds no cache, so nothing needs bounding.

What changed (all in tools/spirv_opt.py)

Measurements

Corpus: the 221 synthetic shaders of the GPU audit set (gpu-current-shader-audit/disasm), translated with rdna2spirv.py --assembler python (all 221 translate; spirv-as/spirv-val aren't installed on this Windows machine, so assembly and validation were stubbed out of the harness). That is 15 MB of .spvasm, 311,534 lines before and 222,329 after. Python 3.12.10, Windows 11, time.process_time(), optimize() over all 221 modules, base and new alternated in one process:

base (1cc6e3c12) this PR
best of N 2.73-3.30 s 2.08-2.41 s
median of N 3.34-3.59 s 2.38-2.77 s
per-round speedup, median 27-29% (four runs of 12-20 rounds)
tracemalloc peak, worst module 8.3 MB 8.3 MB

The machine is noisy (the base alone varied 2.73-3.30 s between runs), so the table gives the ranges seen. The worst single round of the cleanest run was a 22.7% speedup; a few noisier rounds fell lower. The cProfile view agrees: _optimize_function plus process_block self time drops from 6.6 s to 2.2 s under the profiler.

Byte-identical output

  • SHA-256 of every one of the 221 outputs is the same as on main, at the default max_passes=4.
  • Also identical for max_passes 1, 2 and 8, and when the optimizer is applied a second time to its own output.
  • A hand-written module is pinned in tests/test_spirv_opt_hotpath.py.

Tests

  • New tests/test_spirv_opt_hotpath.py (11 tests, hand-written module, no game data). It checks that every opcode quoted in process_block, the simplify block, _fold_int_op and the DCE candidate test is in the set that gates it, so a new rule (for example the vector folds in BUILD-1578: Preserve vector shape and signedness in SPIR-V folds #5150) that forgets the gate fails the test and doesn't silently stop firing. It also probes _fold_int_op for any opcode outside _FOLD_OPS that folds, covers the fast-reject edge cases, and pins the optimized text of a module with store forwarding across a block edge, an escaped variable, folding, CSE, copy removal and DCE.
  • python -m compileall -q tools/spirv_opt.py tests: clean.
  • python -I tests/test_spirv_opt.py (9 tests) and python -I tests/test_spirv_opt_hotpath.py (11 tests): OK.
  • Not run: tests/semantic (as asked), and anything needing spirv-as/spirv-val or a GPU. No GPU involved.

🤖 Generated with Claude Code

… byte-identical output

process_block now skips instructions no rule can touch (_BLOCK_OPS, _SIMPLIFY_OPS,
_FOLD_OPS), _fold_int_op rejects a 3-operand op with no constant and no repeated
operand up front, the escape / discover / DCE / dead-store scans walk only the
opcodes they act on (list comprehensions and a C-level set intersection), the
CFG reads its terminators once, alias resolution is skipped for instructions
that name no alias, id_type no longer copies const_type per pass, and the module
scan no longer parses every body line a second time to find OpFunctionEnd.

Output is byte-identical on 221 translated shaders (max_passes 1, 2, 4, 8), and
tracemalloc peak is unchanged. tests/test_spirv_opt_hotpath.py pins each opcode
set to the rules it gates and pins a hand-written module's output.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@kapitche

Copy link
Copy Markdown
Contributor

Tested b4b411d merged with main 8575d36 on Linux/Python 3.12.16: pass

Commands run (merged worktree, -I isolated mode):

  • python -I tests/test_spirv_opt_hotpath.py -> Ran 11 tests, OK
  • python -I tests/test_spirv_opt.py -> Ran 9 tests, OK
  • Negative control: git checkout upstream/main -- tools/spirv_opt.py, rerun test_spirv_opt_hotpath.py -> FAILED (errors=8; ValueError: substring not found, the tests slice the new opcode-set code out of the source). Restored with git checkout HEAD -- tools/spirv_opt.py. As this control only shows the new tests need the new code, the equivalence check below is the part that tests behavior.
  • Equivalence of the optimizer output (a throwaway script, not part of the PR): I ran the semantic suite's translator inputs (tests/semantic files matching test_[sli]*.py) with rdna2spirv.assemble captured, and ran every distinct captured module through spirv_opt.optimize from main and from the PR, comparing the text. 7,613 distinct modules (8,221 captured): 7,613 identical outputs, 0 differences; total optimize time 25.7 s on main versus 20.4 s with the PR. The semantic files starting with other letters (alu, valu and others, which are most of the suite) were not included because the full suite takes over 20 minutes here, so this is a partial corpus.

Diff read: the optimizer's passes are gated by per-pass opcode sets, loops over all instructions are replaced by filtered iterations, the use-count and alias checks are shortened, and _fold_int_op returns early for 3-operand forms with no constant and no repeated operand. I did not review each gate by hand; the claim rests on the tests and the identical outputs above.

@rojasruiz

Copy link
Copy Markdown

Tested b4b411d merged onto main 5cf3daf (clean merge) on Linux 7.2.15, CPython 3.14, with the two checks the PR lists as not run (real spirv-as/spirv-val, and tests/semantic): pass, byte-identical output confirmed.

  • Byte-identical, with validation. Inputs: the 268 .spvasm modules runtime/test/shaders/build.py writes for the synthetic GPU fixtures. Each one went through optimize() from main and from this branch, loaded side by side in one process. All 268/268 are identical. The branch's output, assembled with spirv-as and checked with spirv-val --target-env vulkan1.3 (SPIRV-Tools v2026.4), is valid for 265. The other 3 (ps_texoff, ps_texoff_forms, ps_texoff_implicit) are the maintenance8 texel-offset fixtures: their un-optimized input fails the same VUID-RuntimeSpirv-Offset-10213 without --allow-offset-texture-operand, so nothing changes there.
  • CPU time over those 268 modules (time.process_time(), one pass each): main 2.25 s, this branch 1.72 s, about 24% less, in line with your 27-29%.
  • tests/test_spirv_opt_hotpath.py: 11 OK.
  • scripts/run_semantic_tests.py tests/semantic: 443 files / 5,889 tests, the same as main. Only the 2 files that already fail there fail here, with the same counts (test_image_atomics_guard 47, see tests: the image atomics guard accepts the folded %true when no lane can be inactive #7292; test_image_store_d16 1, see test: cover supported d16 cube image stores #7194 / docs/design.md: the typed-image-store row names the 1D and cube dims, and a test keeps it in step with STORE_DIMS #7461).
  • scripts/run_gpu_tests.py on an RTX 3080 (NVIDIA 615.78.08): 190/239 passed, 48 skipped, identical to main (the one failure is the known ms_wave_info_s3).
  • check_no_game_data.py on both changed files: clean.

AI-assisted (Claude Code).

@airp1ane

Copy link
Copy Markdown
Contributor

Tested b4b411d on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_spirv_opt_hotpath.py - all exit 0.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants