Repository navigation
rdna2_decode: ~30% faster listing and ~20% faster decode_bytes, output unchanged (PERF-RDNA2-DECODE) - #7044
rdna2_decode: ~30% faster listing and ~20% faster decode_bytes, output unchanged (PERF-RDNA2-DECODE)#7044TG0000 wants to merge 1 commit into
Conversation
|
Run this probe from the repository root with import gc, sys, threading
sys.path.insert(0, "tools")
import rdna2_decode as d
entered, resume = threading.Event(), threading.Event()
class Sample(bytes):
def __len__(self):
entered.set()
assert resume.wait(5)
return super().__len__()
gc.enable()
worker = threading.Thread(target=d.decode_bytes, args=(Sample(bytes.fromhex("000081bf")),))
worker.start()
assert entered.wait(5)
gc.disable() # The application requests manual collection from here on.
resume.set()
worker.join()
print("GC enabled after join:", gc.isenabled())
gc.enable()Expected: The two decoder test files run 28 tests. 27 pass and one skips. AI-assisted. |
|
Reviewed I compared the parent's Tests on this branch: Speed on 3.15 (60k-instruction forced stream, best of 3): On the GC pause: I'd also drop it. In this repo AI-assisted. Synthetic machine code only, no game files. |
|
Independent Windows validation of
This validates functional equivalence and error-path GC restoration; it is not an independent performance measurement. Synthetic inputs only. AI-assisted. |
89db4cd to
cdae3d4
Compare
…put (PERF-RDNA2-DECODE) disasm_bytes formats each instruction through format_insn; decode_bytes builds one dict tree per instruction. Both spent most of their time on per-call work that does not depend on the instruction: - format_insn: the `; encoding:` bytes come from a 256-entry text table through map(), operands through map() instead of a generator (a generator only for s_ttracedata_imm, whose immediate prints in hex), and an inline float constant's text from a dict instead of a scan. - _decode_one: the per-call `need`/`finish` closures are module-level code, 32-bit words are read with a pre-built Struct, a spec's source count and "has an offset operand" are computed once per table entry (bounded, keyed by the spec's identity and holding the spec so an id is never reused), the VOP3 mnemonic and the MIMG address/data counts come from lru_caches, and VOP3/VOP3P pass modifier bits straight to _src instead of through bool() and keyword arguments. - _src: scalar and VGPR sources, the common ones, are built without the generic if-chain. decode_bytes does not touch the garbage collector: the collector setting belongs to the interpreter, and a library call that toggled it could re-enable it after another thread had disabled it. Output is unchanged: tests/test_rdna2_decode_equivalence.py compares each shortcut with the formulation it replaced and the SHA-256 of a hand-built corpus (every table opcode, several field patterns, both wave sizes, gfx1013 and gfx1030, every truncation) with the one measured on origin/main 896b56c. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
cdae3d4 to
e6c7cb3
Compare
|
Rebased onto Thanks @shdann, @hiddenblur and @gut0to for the reviews. I took the GC finding: The main changes since AI-assisted. |
|
Tested e6c7cb3 merged with main 8575d36 on Linux/Python 3.12.16: pass Commands run (merged worktree,
I did not review the decoder diff line by line and did not time the speed-up; the verdict rests on the tests and the digest comparison above. |
Item: PERF-RDNA2-DECODE (claimed in #6910). AI-assisted (Claude). Depends on: none.
tools/rdna2_decode.pyis the pathshader_disasmtakes from machine code to the; encoding:listing (disasm_bytes), anddecode_bytesbuilds the same instructions as dicts. Both spent most of their time on work that does not depend on the instruction. This pull request removes that work. Output is unchanged, the new caches are bounded, and it is stdlib only.What changed
format_insn(the listing): the; encoding: [...]bytes come from a 256-entry text table throughmap(), operands are joined throughmap()instead of a generator, and an inline float constant's text comes from a dict instead of a scan of_INLINE_FLOAT_STR(falls back toreprfor anything not in the table, unhashable values included)._decode_one:need/finishwere closures built on every call; they are nowpc + n <= len(code)and a module-level_finish. Words are read with a pre-builtstruct.Struct("<I"). A spec's source count and "has an offset operand" are computed once per table entry (_spec_info: bounded at 4096, keyed by the spec's identity, and the value keeps the spec alive, so anidis never reused for another spec). The VOP3 mnemonic (_vop3_op_name) and the MIMG address/data counts arelru_cached (maxsize 2048/4096/8192, pure functions). VOP3/VOP3P pass modifier bits straight to_srcinstead of throughbool()and keyword arguments, and VOP3P builds anop_sel/neglist only when one of the real source bits is set._src: scalar and VGPR sources, the common ones, skip the generic if-chain (same dicts as before;neg/absmcan now also be passed positionally).decode_bytesdoes not touch the garbage collector. An earlier revision paused it while building the list; review (thanks @shdann, @hiddenblur) showed the setting belongs to the interpreter and a library call could re-enable it after another thread had disabled it, so that part is dropped._decode_one,format_insnand_iter_insnskeep their names and call order (test_disasm_formats_each_instruction_before_decoding_the_nextpatches them).Benchmark
Synthetic, hand-built corpus, no game data: the opcode-table corpus of
tests/test_rdna2_decode_equivalence.py(87 292 bytes: every table opcode with several field patterns,wave32,gfx1013). Python 3.12.10, Windows 11, CPU only,time.perf_counter,origin/mainand this branch run alternately in one process, 9 rounds.disasm_bytesdecode_bytesAn independent run by @hiddenblur on Python 3.15 (60k-instruction forced stream) measured 21 % for
disasm_bytesand 23 % fordecode_bytes. Timings on this machine move by a few percent from run to run.Tests
tests/test_rdna2_decode_equivalence.py(13 tests, ~9 s):_srcfor every code 0..519 x width x literal x neg/abs (and truthy-int modifiers),_spec_infofor every spec in every table, the MIMG counts and_vop3_op_namefor every opcode, the VOP3Pop_sel/op_sel_hi/neg_lo/neg_hibits for every opcode and all bit combinations,format_insnand the inline-float text (including NaN,-0.0,1, strings, lists);off/nullsaddr, literals, NSA dwords, every truncation of a multi-word instruction, a stream ending inside a word,code_start0 and 0x40, raw junk words) is decoded and listed forgfx1013andgfx1030atwave32andwave64, and the SHA-256 is compared with the one measured on origin/main896b56c6a(the same corpus gives identical digests on both trees). If a later change alters decoding on purpose,python -I tests/test_rdna2_decode_equivalence.py --print-digestsprints the new ones;_SPEC_INFOstays bounded and never serves another spec; the MIMG caches report a finitemaxsize;decode_bytes/disasm_bytesleavegcas they found it, including when another thread disables it mid-call.python -m compileall -q tools/rdna2_decode.py tests;python -I tests/test_rdna2_decode_equivalence.py;python -I tests/test_rdna2_decode.py(14 passed, thellvm-mcparity test skips without LLVM);tests/semantic/test_printed_encoding.py,tests/semantic/test_global_mem_offset.pyandtests/test_fssm_crosscheck.pyas scripts. The fulltests/semanticsuite was not run. No GPU involved.Open pull requests on this file
Nothing else on the list speeds this file up; #6795, #4928, #5115, #5698, #6036, #4697, #5013 and #2038 change what it decodes or prints. I kept my edits off the lines they change where I could (the
_srcliteral branch, theimmbranch of_format_operand, the0x140target override, the VOP3op_selblock,_iter_insns,disasm_bytes), but thereturn finish(->return _finish(code, pc,rewrite touches every return in_decode_one, so whichever of these lands second needs a rebase; I will do mine if this one is the second.