Skip to content

rdna2_decode: pure-Python RDNA2 binary ISA decoder (#1901) - #2038

Open
SweatierKey wants to merge 2 commits into
ItsNotRexed:mainfrom
SweatierKey:rdna2-direct-decoder
Open

SweatierKey wants to merge 2 commits into
ItsNotRexed:mainfrom
SweatierKey:rdna2-direct-decoder

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Implements #1901 (claim: #1902 (comment)): a zero-dependency, standard-library-only pure-Python RDNA2 binary ISA decoder that removes the hard requirement on llvm-mc / LLVM 21+ / WSL for shader disassembly.

Changes

  • tools/rdna2_isa_tables.py: complete RDNA2 (GFX10.1/GFX10.3) opcode tables across all 20 encoding groups (SOP1, SOP2, SOPC, SOPK, SOPP, SMEM, VOP1, VOP2, VOPC, VINTRP, VOP3, VOP3B, VOP3P, DS, MUBUF, MTBUF, MIMG, FLAT, GLOBAL, SCRATCH), derived from AMD's MIT-licensed machine-readable RDNA2 ISA XML (amdgpu_isa_rdna2.xml), plus the seven gfx1013 legacy/waveid instructions documented in docs/isa_coverage.md and the five literal-only instructions (s_setreg_imm32_b32, v_fmamk_f32/f16, v_fmaak_f32/f16).
  • tools/rdna2_decode.py: decodes raw RDNA2 machine code directly into:
    • decode_bytes() → exact shader_ir.parse_file() dict layout (op, args, mods, enc, pc, invalid), and
    • disasm_bytes() / format_insn() → LLVM-compatible .s text with ; encoding: [...].
    • Covers all RDNA2 encodings including DPP16, DPP8, SDWA, MIMG NSA (1–3 extra dwords) and BVH raytracing (image_bvh*_intersect_ray), LLVM VCCTuple scalar register lists ending in vcc_lo,vcc_hi, ds_swizzle_b32, s_waitcnt, s_setreg/s_getreg, EXP (incl. compr), and VOP3P v_dot4_i32_i8 with non-zero op_sel / op_sel_hi (e.g. 0xCC165008 0x0C0E0501, which llvm-mc rejects).
  • tools/shader_disasm.py: adds optional --backend {llvm-mc,python} (default remains llvm-mc, so existing workflows are unchanged; --backend python skips probe() and runs purely in Python with no LLVM dependency).
  • tests/test_rdna2_decode.py: stdlib unit tests covering all RDNA2 encoding families, v_dot4_i32_i8 op_sel, VCCTuple, binary→SPIR-V→spirv_mini_interp round-trip, and an optional full parity check against llvm-mc across all runtime/test/shaders/*.asm.

Testing (CPU / synthetic only, no game files, no GPU)

  • python3 -m compileall -q make_game.py tools tests: OK
  • python3 -I tests/test_rdna2_decode.py -v: 8/8 OK — including 7,289 / 7,289 instructions across all 204 shaders in runtime/test/shaders/*.asm matching llvm-mc (gfx1013) with 0 invalid words and 100.00% exact length/opcode/operand/modifier parity + text round-trip parity.
  • tests/test_shader_disasm.py (13/13), tests/test_tool_startup.py, tests/test_text_write_line_endings.py, tests/test_docs_links.py: all OK.

AI-assisted.

@borislisandro

Copy link
Copy Markdown

Tested 77b1103c on Linux x86_64 (Python 3.x, llvm-mc 22.1.8; CPU only, no game files): pytest tests/test_rdna2_decode.py: 8 passed.

I also checked the decoder against LLVM on the repository's own hand-written inputs: I assembled the 204 runtime/test/shaders/*.asm GPU fixtures with llvm-mc -arch=amdgcn -mcpu=gfx1030 -mattr=+wavefrontsize32 -show-encoding (files that fail to assemble for that target are skipped), which gives 6,023 instructions, and fed each encoding to decode_bytes and disasm_bytes.

  • Opcode: all 6,023 decode to one instruction with the same opcode name as LLVM (0 exceptions, 0 wrong lengths).
  • Text: 5,770 (95.8%) match LLVM's text after whitespace normalisation. The 253 that differ are formatting, not meaning, in four patterns: literals printed in decimal where LLVM prints hex (s_buffer_load_dword s0, s[4:7], 16 for 0x10; v_mov_b32_e32 v6, 99 for 0x63); null where LLVM prints 0x0 for an SMEM load with SOFFSET null and imm 0 (the IR then holds {'k': 'special', 't': 'null'} instead of imm 0; I checked rdna2spirv.translate gives identical SPIR-V for null and 0x0 offsets); the _e32 suffix dropped on v_interp_p1/p2_f32; and branch offsets printed unsigned (s_cbranch_scc1 0xfff0 where LLVM prints -16).
  • The last one matters for consumers. decode_bytes returns {'k': 'imm', 'v': 65520} for a backward branch, where the text parser gives -16, and test_sopp_endpgm_branch_and_waitcnt pins 0xFFFC. The translator masks the branch immediate with & 0xFFFF and sign-extends it before computing the target (rdna2spirv.py, in reachable_code), so it accepts both, but a tool that reads imm directly would see a forward jump of 65,520 words. The same signed treatment as s_movk_i32 (_s16) would remove the difference. Also, the docstring calls the text "LLVM-compatible"; with the decimal and null differences that holds for the instruction stream but not byte for byte.

Not checked: wave64, the gfx1013 legacy opcodes, anything from the game's shaders (the corpus equality, if claimed in the description, needs a game copy), and the table derivation from the AMD XML.

AI-assisted review.

@UNYTIX

UNYTIX commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 77b1103c9b2d. Command python -B -I tests/test_rdna2_decode.py -k test_sopp -k test_sop1 -k test_vop1 -k test_vop3p -k test_smem -k test_mubuf -v.

Six pure-Python opcode decode tests passed. This excludes the file-based round trip and LLVM fixture-parity checks.

Independent Windows 11 verification, Python 3.15.2. AI-assisted.

SweatierKey added a commit to SweatierKey/Leonida that referenced this pull request Nov 22, 2026
…negatives as decimal

Addresses review feedback on PR ItsNotRexed#2038 (@borislisandro):
- Sign-extend SOPP branch simm16 offsets with _s16(simm16) so backward branches decode
  as negative integers (e.g. -4, -20) matching shader_ir/llvm-mc 21/22 instead of 0xfffc.
- Format negative immediates (< 0) as decimal rather than broken '0x-...' hex.
- Clarify module docstring (shader_ir-compatible .s text) and update unit tests
  (normalizing SOPP branch immediates mod 16-bit for LLVM 18 vs 21/22 parity).
@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thank you @borislisandro and @UNYTIX for the thorough independent verification!

Addressed @borislisandro's feedback in c329207f:

  • SOPP branch offsets (_SOPP_BRANCHES) are now decoded with _s16(simm16) (so backward branches carry signed negative integers like -4 / -16, matching shader_ir and llvm-mc 21/22).
  • Fixed negative immediate formatting in _format_operand (negative values now print as decimal instead of 0x-...).
  • Clarified the module docstring (shader_ir-compatible .s text) and updated tests/test_rdna2_decode.py (8/8 OK, normalizing SOPP branch immediates mod 16-bit for LLVM 18 vs 21/22 parity).

SweatierKey added a commit to SweatierKey/Leonida that referenced this pull request Nov 22, 2026
…negatives as decimal

Addresses review feedback on PR ItsNotRexed#2038 (@borislisandro):
- Sign-extend SOPP branch simm16 offsets with _s16(simm16) so backward branches decode
  as negative integers (e.g. -4, -20) matching shader_ir/llvm-mc 21/22 instead of 0xfffc.
- Format negative immediates (< 0) as decimal rather than broken '0x-...' hex.
- Clarify module docstring (shader_ir-compatible .s text) and update unit tests
  (normalizing SOPP branch immediates mod 16-bit for LLVM 18 vs 21/22 parity).
SweatierKey added a commit to SweatierKey/Leonida that referenced this pull request Nov 22, 2026
…negatives as decimal

Addresses review feedback on PR ItsNotRexed#2038 (@borislisandro):
- Sign-extend SOPP branch simm16 offsets with _s16(simm16) so backward branches decode
  as negative integers (e.g. -4, -20) matching shader_ir/llvm-mc 21/22 instead of 0xfffc.
- Format negative immediates (< 0) as decimal rather than broken '0x-...' hex.
- Clarify module docstring (shader_ir-compatible .s text) and update unit tests
  (normalizing SOPP branch immediates mod 16-bit for LLVM 18 vs 21/22 parity).
kacper-serewis added a commit to kacper-serewis/Leonida that referenced this pull request Nov 22, 2026
SweatierKey added a commit to SweatierKey/Leonida that referenced this pull request Nov 22, 2026
…negatives as decimal

Addresses review feedback on PR ItsNotRexed#2038 (@borislisandro):
- Sign-extend SOPP branch simm16 offsets with _s16(simm16) so backward branches decode
  as negative integers (e.g. -4, -20) matching shader_ir/llvm-mc 21/22 instead of 0xfffc.
- Format negative immediates (< 0) as decimal rather than broken '0x-...' hex.
- Clarify module docstring (shader_ir-compatible .s text) and update unit tests
  (normalizing SOPP branch immediates mod 16-bit for LLVM 18 vs 21/22 parity).
@SweatierKey

Copy link
Copy Markdown
Contributor Author

Update (998128ec): fixed SDWA scalar inline-constant decoding caught by @zneel on stacked #2323 (runtime/test/shaders/ps_sdwa_f16_const.asm): VOP1/VOPC/VOP2 SDWA scalar sources (s0_is_sgpr/s1_is_sgpr) now decode via _src(..., 1, None) instead of _sreg(..., 1), mapping inline integers (128..208) and floats (240..248, e.g. 1.0 = 242, 2.0 = 244) properly. Added regression test test_sdwa_inline_constants_and_sgpr (test_rdna2_decode.py: 9/9 OK).

SweatierKey added a commit to SweatierKey/Leonida that referenced this pull request Nov 22, 2026
…negatives as decimal

Addresses review feedback on PR ItsNotRexed#2038 (@borislisandro):
- Sign-extend SOPP branch simm16 offsets with _s16(simm16) so backward branches decode
  as negative integers (e.g. -4, -20) matching shader_ir/llvm-mc 21/22 instead of 0xfffc.
- Format negative immediates (< 0) as decimal rather than broken '0x-...' hex.
- Clarify module docstring (shader_ir-compatible .s text) and update unit tests
  (normalizing SOPP branch immediates mod 16-bit for LLVM 18 vs 21/22 parity).
@SweatierKey

Copy link
Copy Markdown
Contributor Author

Rebased onto main (2d3369a4, head a8c1473c). Since the core decoder commits (1ca544a3, e4a2233a, 998128ec) are now merged into main, this PR is now a small 2-commit follow-up:

  • Compare against assembled encodings in test_parity_with_llvm_mc_on_shader_fixtures when llvm-mc 21 -disassemble fails to round-trip SDWA float inline constants on v_cvt_f16_u16_sdwa.
  • Emit k="special" (matching shader_ir.py) for exp null (caught by the newly merged ps_w64_a_discard.asm fixture).
    Verified: test_rdna2_decode.py 9/9 OK.

@DenRakEiw

Copy link
Copy Markdown

Tested �8c1473c on Windows 11, Python 3.12.10 (CPU only, no llvm-mc, no game files): pass.

  • python -m compileall -q tools/rdna2_decode.py tests/test_rdna2_decode.py: clean.
  • python -I tests/test_rdna2_decode.py -v: 8 OK, 1 skipped ( est_parity_with_llvm_mc_on_shader_fixtures skipped without llvm-mc).
  • Direct parity check for exp null:
    dna2_decode.decode_bytes(bytes.fromhex('901800f800000000')) emits {'k': 'special', 't': 'null'}, matching shader_ir.parse_line('exp null off, off, off, off done vm').
  • Code read of the llvm-mc 21 SDWA round-trip fallback in ests/test_rdna2_decode.py looks sound (
    ef = li if li[enc] == ai[enc] else ai).

AI-assisted review (Gemini).

@kapitche

Copy link
Copy Markdown
Contributor

Tested a8c1473 (= main 2d3369a plus the two commits, no merge needed) on Linux (Python 3.12.16) and Windows 11 (Python 3.13.1, llvm-mc 23.1.6): pass.

  • Linux: -I tests/test_rdna2_decode.py: 9 tests, 8 OK, 1 skipped (llvm-mc not installed there, so the parity test did not run). Windows with LLVM_MC set: 9 OK in 17 s, and the parity test ran (not skipped).
  • Negative control: tools/rdna2_decode.py from main with the new test: parity fails on the exp null operand kind (target vs special), 1 failure.

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.

5 participants