Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
Verified on Windows 11 (Python 3.12 / 3.15):
- Tested
tools/rdna2spirv.py,tools/lane_masks.py, andtools/shader_meta.pybase_opmemoization:- Validated stripping across
_e32,_e64,_sdwa,_dppsuffixes and identity on non-suffixed ops. - Verified bounded dictionary cache limit (
1 << 16) protection against unbound memory growth.
- Validated stripping across
- Ran tests:
test_rdna2spirv_cli.py: passed (35/35).test_shader_checks.py: passed (84/84).test_run_semantic_tests.pyandtest_semantic_translator_use.py: passed.
- Whitespace clean (
git diff HEAD~1..HEAD --check).
|
Independent Windows check of
|
|
Tested abab251 merged with main 8575d36 on Linux/Python 3.12.16: concern Commands (repo .venv Python 3.12.16,
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. |
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.
|
Fair point — and a test that only compared outputs would have passed on
|
|
Tested
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. |
|
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:
AI-assisted (OpenCode) |
Claimed in the coordination issue: #7049
What and why
base_op()— the four-suffix strip (_e32,_e64,_sdwa,_dpp) — exists three times, inrdna2spirv.py,shader_meta.pyandlane_masks.py, and it is on the hottest path in the translator: parsing an instruction asks forthe base opcode once per operand. A profile of a 3,203-instruction listing shows 440k
str.endswithcalls, ~23base_op()calls per instruction.A build translates 26,503 shaders, and a program made only of
s_nopcosts 14 µs per instruction, so roughly half ofthe 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_metacannot importrdna2spirvwithout 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.pyfrom #7056 (not merged yet; the numbers below are from my branch), samemachine, same commit otherwise,
--runs 5, Windows 11, Python 3.12.4, Ryzen 9:s_nop, 2,004 instructionsThe
s_noprow is the one that says what this is: instructions that do no work at all now cost 39% less, which is thefixed per-instruction cost the translator pays before it starts generating anything. The ALU row is what a real shader
sees.
Equivalence
tests/semantic/fixtures/disasmtranslate to the sameSPIR-V assembly text before and after: SHA-256 over name + output is
157189dcc0158f00…in both runs.python -I scripts/run_semantic_tests.py tests/semanticon cleanmainand withthis change both report 433 files, 5,821 tests, one red file:
test_float_nan_fract, three asserts about the NaNpayload of
v_min3_f32/v_max3_f32/v_med3_f32. That failure is onmainwithout this change (verified bystashing it and re-running the file), so it is not from here.
base_opis a pure function of the string — same four suffixes, same slicing — so a cached answer cannot differ froma recomputed one.
Tested
Windows 11, Python 3.12.4, CPU only, no game data:
python -I tests/semantic/test_float_nan_fract.pyand the full semantic runner, on cleanmainand after: sameresults (the one pre-existing failure above).
python -m compileall -q toolsclean.python -I tests/test_text_write_line_endings.py,tests/test_ci_registration.py,tests/test_semantic_translator_use.pypass.cc @ItsNotRexed @KoertLichtendonk.
Checklist
repository's own hand-written fixtures.
docs/design.md, with how it was confirmed. (Not applicable:no rule changes; the emitted SPIR-V is byte-identical.)