Skip to content

rdna2_decode: stream the text listing instead of retaining every instruction - #4963

Closed
tupacfan04 wants to merge 1 commit into
ProjectLeonida:mainfrom
tupacfan04:fix/rdna2-decode-stream-text
Closed

tupacfan04 wants to merge 1 commit into
ProjectLeonida:mainfrom
tupacfan04:fix/rdna2-decode-stream-text

Conversation

@tupacfan04

@tupacfan04 tupacfan04 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as #4932 (claim).

What and why

rdna2_decode.disasm_bytes() called decode_bytes(), kept every decoded instruction dictionary, built a second list of formatted lines, and joined it. For a text-only caller that doubled the retained Python objects until the final string existed.

  • Added _iter_decoded(), which yields each instruction (or None for an invalid word) as soon as it is decoded.
  • decode_bytes() keeps collecting the structured result its callers expect.
  • disasm_bytes() formats each instruction and writes it to a StringIO immediately, so the instruction dict and its formatted line are released before the next decode.
  • Output, code_start handling, the trailing newline and the invalid-word count are unchanged.

Measured on Windows 11 with CPython 3.14.7, using bytes.fromhex("000040d501050e04") * 8192 + b"\xff" and tracemalloc started immediately before the call:

Peak traced Python allocations
Before 11.67 MiB
After 1.86 MiB

Both return the same 778,240-character listing and report one invalid word. This measures Python allocations only, not RSS or throughput.

Tested

  • py -3 -I -m pytest tests/test_rdna2_decode.py tests/test_rdna2spirv_decode.py tests/test_shader_disasm.py -q: 57 passed, 1 skipped, 72 subtests passed.
  • py -3 -m compileall -q tools/rdna2_decode.py: exit 0.
  • Byte-identical output against the old path on seven cases (empty input, one instruction, two instructions, trailing invalid byte, non-zero code_start, wave32=False, and the 2,048-instruction repeated input), compared by SHA-256 of the full listing plus the invalid count.
  • No GPU, no game data and no external tools are involved.

AI-assisted: the change, tests and this description were written by an AI coding agent running in OpenCode and checked by the commands above.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not a translator change.)

Depends on: nothing.

@tupacfan04
tupacfan04 force-pushed the fix/rdna2-decode-stream-text branch 2 times, most recently from 7786790 to 595c5dc Compare November 23, 2026 06:57
@tupacfan04
tupacfan04 force-pushed the fix/rdna2-decode-stream-text branch from 595c5dc to e54f1cf Compare November 23, 2026 07:00
@Atomic258

Copy link
Copy Markdown
Contributor

Independent check of e54f1cf1 — pass.

Environment: Windows 11, CPython 3.15.2, CPU only. Synthetic inputs, no GPU, no game files.

  • python -I tests/test_rdna2_decode.py → 14 tests, 13 pass / 1 skip (platform-gated)
  • Negative control: PR tests against main's tools/rdna2_decode.py → 2 failures (disasm_bytes used decode_bytes — the streaming path isn't taken on the old code), so the new tests pin the change
  • compileall clean, git diff --check main...HEAD clean

Read the diff: text listing now streams per-instruction instead of accumulating — the memory win the title claims, with the byte path preserved for the binary listing. No issues found.

AI-assisted (Devin).

@SphyserX

Copy link
Copy Markdown

Tested e54f1cf on Arch Linux (in a VM), CPython 3.14, LLVM 23.1.5: pass.

@kapitche

Copy link
Copy Markdown
Collaborator

Tested e54f1cf merged with main 3faac55 on Linux (WSL2), CPython 3.12.16, CPU only: pass

  • python -I tests/test_rdna2_decode.py: 14 run, 13 passed, 0 failed, 1 skipped (llvm-mc parity test, llvm-mc not installed here).
  • Negative control: tools/rdna2_decode.py restored from main, same test file: 14 run, 2 failures (disasm_bytes used decode_bytes), so the streaming tests do fail without the change.
  • Read the diff: decode_bytes and disasm_bytes now share _iter_decoded, the invalid-word step (max(4, nbytes)) is unchanged, and the empty-input and invalid-word cases are covered by the new tests. No issue found.

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed at \e54f1cf1. Tested on Windows 11:

  • \python -m unittest tests/test_rdna2_decode.py\ -> 14 passed, OK (1 skipped, 1.50s).
  • _iter_decoded\ preserves exact PC stepping and invalid-word stepping for \decode_bytes.
  • \disasm_bytes\ streams into \io.StringIO\ with identical output and trailing newline semantics across empty, invalid, and mixed streams.
  • Peak memory bound verified (< 6 MiB for 8,192 instructions).
  • \git diff --check\ clean. No game files. AI-assisted review (Gemini).

@tahffo68hk-hash

Copy link
Copy Markdown
Contributor

Integration check on Windows: e54f1cf1 conflicts with the current target-aware parent #4928 (200a4c3f) in the shared decode loop. I resolved it locally by retaining the streaming iterator, moving the existing supports_target(mcpu) check into that iterator, and forwarding mcpu from both APIs to _decode_one. The local combination also included the GFX10.1 opcode follow-up in bobcatchris#3.

The combined decoder suite had 18 passes and one LLVM-dependent skip. All 22 shader-disassembly tests passed. Against the target-aware non-streaming version, 520 combinations of synthetic buffers, GFX10.1/GFX10.3, and wave32/wave64 matched in both text and structured output, including the invalid count. No branch changes pushed. CPU only, synthetic inputs, AI-assisted.

在 Windows 上做了接入验证。当前 e54f1cf1 与支持目标架构的父改动 #4928(200a4c3f)在解码循环中冲突。我在本地保留流式迭代器,将现有 supports_target(mcpu) 检查移入其中,并让两个接口经迭代器把 mcpu 传给 _decode_one。本地组合也包含 bobcatchris#3 的 GFX10.1 操作码补丁。

组合后的解码测试 18 项通过、1 项因依赖 LLVM 跳过,22 项着色器反汇编测试全部通过。与支持目标架构的非流式版本相比,合成缓冲区、GFX10.1/GFX10.3 和 wave32/wave64 的 520 个组合在文本输出、结构化输出及非法指令计数上均一致。未推送分支改动。仅 CPU 和合成输入,AI 辅助。

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

At e54f1cf, 165 stage-stratified cached modules matched parent text, complete structured decoder output and invalid counts. All production ranges were clean; six deliberately extended ranges separately exercised 20 invalid words. On the largest sample, peak traced Python allocation fell from 106,416,576 to 18,399,556 bytes (one measurement per revision). Windows CPython 3.13.15. AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested e54f1cf on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_rdna2_decode.py -> Ran 14 tests, OK (skipped=1)
  • Negative control (tools/rdna2_decode.py swapped to upstream/main): FAILED (failures=2)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@tupacfan04

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #4943 ("Stream Python shader text disassembly to lower peak memory", merged as 891358bb). It makes the same change in tools/rdna2_decode.py: disasm_bytes() streams each decoded instruction into a StringIO instead of building decode_bytes()'s full list first. It also forwards mcpu through the shared iterator, which resolves the target-aware conflict @tahffo68hk-hash noted with #4928.

Checked on Windows 11, py -3.13 -I, CPU only, no game files:

  • e54f1cf1 no longer merges onto main 4e42451d: both tools/rdna2_decode.py and tests/test_rdna2_decode.py conflict.
  • This PR's own tests/test_rdna2_decode.py from e54f1cf1, run against main's tools/rdna2_decode.py, gives 14 tests OK (1 skipped, the llvm-mc parity test). That includes test_disasm_bytes_streams_without_building_decode_bytes and the tracemalloc peak bound. Nothing in this PR is left unmerged.

Thanks @Atomic258, @SphyserX, @kapitche and @tahffo68hk-hash for testing it.

AI-assisted (Claude Code).

@tupacfan04 tupacfan04 closed this Nov 23, 2026
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.

7 participants