Skip to content

tools: memoise base_op() in its three copies (17% off the ALU path, 39% off the per-instruction floor) - #7127

Open
pes7 wants to merge 2 commits into
ItsNotRexed:mainfrom
pes7:perf/base-op-memo
Open

pes7 wants to merge 2 commits into
ItsNotRexed:mainfrom
pes7:perf/base-op-memo

Conversation

@pes7

@pes7 pes7 commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue: #7049

What and why

base_op() — the four-suffix strip (_e32, _e64, _sdwa, _dpp) — exists three times, in rdna2spirv.py,
shader_meta.py and lane_masks.py, and it is on the hottest path in the translator: parsing an instruction asks for
the base opcode once per operand. A profile of a 3,203-instruction listing shows 440k str.endswith calls, ~23
base_op() calls per instruction.

A build translates 26,503 shaders, and a program made only of s_nop costs 14 µs per instruction, so roughly half of
the per-instruction cost is this dispatch, not code generation. This takes most of it out: the answer is kept per
string in a dict, straight-line, and the same three-line change goes into each copy (the three copies predate this
change; merging them is a separate edit because shader_meta cannot import rdna2spirv without a cycle).

The cache is capped at 1<<16 entries and cleared when it reaches the cap, so a process that translates a whole corpus
in one run (rdna2spirv.py <dir>) cannot grow it without bound.

Numbers

Measured with scripts/bench_translate.py from #7056 (not merged yet; the numbers below are from my branch), same
machine, same commit otherwise, --runs 5, Windows 11, Python 3.12.4, Ryzen 9:

input before after change
corpus: 52 semantic fixtures, 4,475 instructions 55.4 µs/instr (0.248 s) 45.5 µs/instr (0.204 s) −18%
synthetic ALU, 2,004 instructions 32.3 µs/instr 26.7 µs/instr −17%
synthetic s_nop, 2,004 instructions 14.0 µs/instr 8.5 µs/instr −39%

The s_nop row is the one that says what this is: instructions that do no work at all now cost 39% less, which is the
fixed per-instruction cost the translator pays before it starts generating anything. The ALU row is what a real shader
sees.

Equivalence

  • Byte-identical output. All 52 pixel and vertex fixtures in tests/semantic/fixtures/disasm translate to the same
    SPIR-V assembly text before and after: SHA-256 over name + output is 157189dcc0158f00… in both runs.
  • The semantic suite is unchanged. python -I scripts/run_semantic_tests.py tests/semantic on clean main and with
    this change both report 433 files, 5,821 tests, one red file: test_float_nan_fract, three asserts about the NaN
    payload of v_min3_f32/v_max3_f32/v_med3_f32. That failure is on main without this change (verified by
    stashing it and re-running the file), so it is not from here.
  • base_op is a pure function of the string — same four suffixes, same slicing — so a cached answer cannot differ from
    a recomputed one.

Tested

Windows 11, Python 3.12.4, CPU only, no game data:

  • python -I tests/semantic/test_float_nan_fract.py and the full semantic runner, on clean main and after: same
    results (the one pre-existing failure above).
  • The 52-fixture digest above, twice.
  • python -m compileall -q tools clean.
  • python -I tests/test_text_write_line_endings.py, tests/test_ci_registration.py,
    tests/test_semantic_translator_use.py pass.

cc @ItsNotRexed @KoertLichtendonk.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here: the inputs are the
    repository's own hand-written fixtures.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable:
    no rule changes; the emitted SPIR-V is byte-identical.)

The translator asks for the base opcode once per operand, ~23 times per instruction (440k str.endswith calls for a
3,203-instruction listing), and base_op() walks four suffixes every time. The answer depends only on the string, so
it is kept per string in a dict capped at 1<<16 entries and cleared at the cap.

Measured with scripts/bench_translate.py from ItsNotRexed#7056, --runs 5, Windows 11, Python 3.12.4: corpus of 52 semantic
fixtures 55.4 -> 45.5 us/instruction (-18%), synthetic ALU 32.3 -> 26.7 (-17%), synthetic s_nop 14.0 -> 8.5 (-39%).
All 52 pixel and vertex fixtures translate to byte-identical SPIR-V assembly before and after.

@devnull34 devnull34 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Windows 11 (Python 3.12 / 3.15):

  • Tested tools/rdna2spirv.py, tools/lane_masks.py, and tools/shader_meta.py base_op memoization:
    • Validated stripping across _e32, _e64, _sdwa, _dpp suffixes and identity on non-suffixed ops.
    • Verified bounded dictionary cache limit (1 << 16) protection against unbound memory growth.
  • Ran tests:
    • test_rdna2spirv_cli.py: passed (35/35).
    • test_shader_checks.py: passed (84/84).
    • test_run_semantic_tests.py and test_semantic_translator_use.py: passed.
  • Whitespace clean (git diff HEAD~1..HEAD --check).

@gut0to

gut0to commented Nov 23, 2026

Copy link
Copy Markdown

Independent Windows check of abab251:

  • python -I tests/test_rdna2spirv_decode.py: 26/26 passed.
  • python -I tests/test_rdna2spirv_pixelinput.py: 6/6 passed.
  • python -I tests/test_rdna2spirv_xdxr_rules.py: 6/6 passed.
  • python -I tests/test_semantic_translator_use.py: 3/3 passed.
  • python -I tests/semantic/test_printed_encoding.py: 7/7 passed.
  • Direct cases covering all four suffixes and an unsuffixed opcode passed for all three base_op() copies; compileall and git diff --check are clean.

tests/test_rdna2spirv_cli.py is not runnable on this host's Python 3.10 because its existing tests call TestCase.enterContext, which was introduced in Python 3.11. I therefore do not attribute its 19 setup errors to this PR. No performance claim from this environment. Synthetic inputs only. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested abab251 merged with main 8575d36 on Linux/Python 3.12.16: concern

Commands (repo .venv Python 3.12.16, -I, worktree of main with this PR merged in):

  • python -I tests/test_rdna2spirv_decode.py: 26 tests OK
  • python -I tests/test_rdna2spirv_pixelinput.py: exit 0
  • python -I tests/test_rdna2spirv_xdxr_rules.py: 6 tests OK
  • python -I tests/test_rdna2spirv_cli.py: OK (2 skipped)
  • python -I tests/test_semantic_translator_use.py: 3 tests OK
  • python -I tests/test_shader_meta.py: 27 OK; tests/test_shader_meta_operands.py: OK; tests/test_lane_masks.py: ok
  • python -I -m unittest discover -s tests/semantic -p test_printed_encoding.py: 7 tests OK
  • An ad-hoc script (not in the PR) comparing each of the three base_op copies with the old suffix loop on v_add_f32_e32, _e64, _sdwa, _dpp, an unsuffixed op, an empty string and double-suffixed names, repeated for cache hits, and with _BASE_OP_CACHE_LIMIT set to 3 to check the clear path: all equal, cache stays at or below the limit.

Reason for concern: the PR adds no test, so there is no negative control; every test above also passes on plain main, so none of them shows the change. The diff itself is a behaviour-preserving memoisation (three identical copies of the same cache), and I found no incorrectness. If a regression test is wanted, something like the ad-hoc check above (equivalence against the uncached loop plus the cache-limit clear) would discriminate.

@pes7

pes7 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Claimed in the coordination thread (opened in #7049, carried to #7333 and #7496; the tables there are rebuilt from open PRs). The numbers and how they were measured are in the description above.

Review on ItsNotRexed#7127 asked for a test, and a test that only compares outputs would pass on main as well, where the strip is
not cached. tests/test_base_op_cache.py reads each module's table, so it fails on main with 4 errors and passes with
the cache: every copy answers what the plain suffix loop answered, a repeated call is a hit with the same answer, the
table clears at its limit and still answers, and each module keeps its own table.
@pes7

pes7 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Fair point — and a test that only compared outputs would have passed on main as well, where the strip is not cached, so it would have shown nothing. tests/test_base_op_cache.py is a negative control instead:

  • it reads each module's own table (_BASE_OP_CACHE) and its limit (_BASE_OP_CACHE_LIMIT), so on main it fails with 4 errors and with this PR it passes;
  • every copy (rdna2spirv, shader_meta, lane_masks) is checked separately against a reference implementation of the uncached strip, including double-suffixed names (v_add_f32_e32_dpp), an unsuffixed op, the empty string, and "_e32" on its own;
  • a repeated call must be a hit and the same answer (this is the case your ad-hoc script covered with cache hits);
  • with the limit set to 3 the table clears and every answer is still right (your _BASE_OP_CACHE_LIMIT = 3 path);
  • each module keeps its own table, so one copy's hits cannot answer for another.

python -I tests/test_base_op_cache.py: 4 tests, OK with the PR; 4 errors against main's tools/, which is the control.

@leozinhoszg

Copy link
Copy Markdown
Contributor

Tested abab251d (applied cleanly onto main 3ad6618e) on Windows 11 Pro (26200), CPython 3.14.7, CPU only, no game files: duplicate. The new head 22f1a5d2 only adds tests/test_base_op_cache.py, which I ran separately.

  • Ran the full tests/semantic discover on main and with this PR. Both give 5,889 tests, 48 failures and 32 skips, with the same failures (test_image_atomics_guard and one test_image_store_d16 case, which also fail on main). All 56,456 distinct translate() inputs the suite makes give the same SPIR-V assembly text on both trees. The tests for the touched files, compileall and git diff --check are clean. I had no spirv-val and no GPU.
  • Negative control: tests/test_base_op_cache.py gives 4 OK on 22f1a5d2 and 4 errors when copied onto main dde82f27. Before that test existed, I also made the cache store the name unstripped: tests/test_rdna2spirv_decode.py gets 2 failures and test_alu_cndmask_mix gets 4 errors, so the existing tests reach the cached path too.
  1. Each of the three hunks is already in an earlier open PR, and each one conflicts with it. git merge-tree of the earlier PR with 22f1a5d2 reports a conflict for all three:

    file earlier open PR
    tools/rdna2spirv.py:235 rdna2spirv: add bounded opcode cache for base_op() #6413, claimed in Coordinating who works on what (36) (moved to #6414) #6214 at 14:08 UTC (this one: Coordinating who works on what (41) (moved to #7231) #7049, 15:59 UTC), already checked by two reviewers
    tools/lane_masks.py:62 lane_masks: cache opcode dispatch, skip dead fixpoint passes, and reuse snapshots (-76.3%) #6086 (same base_op cache plus an _op_info dispatch cache)
    tools/shader_meta.py:66 shader_meta: decode each block's operands once in analyse_instructions (-63.9%) #5862
  2. I could not reproduce the speed-up. base_op itself goes from about 105 ns to 38 ns per call. translate() calls it 30 times per instruction on a synthetic 2,007-instruction s_nop PS and 33.5 times on an ALU mix. I ran an interleaved A/B in one process, best of 60: s_nop went from 15.9 to 15.2 µs/instr (−4%), ALU from 45.9 to 45.3 (−1%). Even a base_op that cost nothing would save at most 3.0 and 3.7 µs here (≤19% and ≤8%), which is less than the 39% and 17% claimed. Part of the gap may be Python 3.12.4 vs 3.14.7. My machine was not idle, so take the A/B as rough.

Suggestion: close this PR in favour of #6413, #6086 and #5862; the new test could move to whichever of them lands. If one PR for all three copies is wanted, settle that with the earlier authors first.

AI-assisted.

@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested 22f1a5d on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass

Command:

  • PYTHONPATH=. python3 -m pytest tests/test_base_op_cache.py -q (4 passed, 36 subtests passed)

AI-assisted (OpenCode)

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.

6 participants