Skip to content

rdna2spirv/fspx_unpack/make_game: direct binary RDNA2 decode backend (#1901) - #2323

Open
SweatierKey wants to merge 4 commits into
ItsNotRexed:mainfrom
SweatierKey:rdna2-direct-translate
Open

SweatierKey wants to merge 4 commits into
ItsNotRexed:mainfrom
SweatierKey:rdna2-direct-translate

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Depends on #2038 (stacked on rdna2-direct-decoder). Completes the pure-Python RDNA2 decode integration for #1901 (claim: #1902 (comment)).

What this adds

  • tools/rdna2spirv.py: adds decode_binary_file() so translate() and the CLI accept native .fssm and fspx/*.code binaries directly (decoding straight into shader_ir via tools/rdna2_decode.py without spawning llvm-mc or writing/parsing .s text files). Direct decode preserves exact byte PCs across embedded undecodable data words, skipping the same_encoding re-anchoring scan and pruning unreachable data via fold_far_branches + reachable_code.
  • tools/fspx_unpack.py: adds --backend {llvm-mc,python} (default llvm-mc) forwarding wave32 to shader_disasm.disasm(), enabling zero-llvm-mc disassembly of all FSPX shaders including the v_dot4_i32_i8 op_sel:[0,1,0] op_sel_hi:[1,0,1] words llvm-mc rejects.
  • make_game.py: adds --disasm-backend {llvm-mc,python} (default llvm-mc). Under --disasm-backend python, preflight() does not require llvm-mc, disasm.done records a SHA-256 fingerprint of rdna2_decode.py + rdna2_isa_tables.py, and shaders() passes --backend python to shader_disasm.py.

Verification

  • python3 -m unittest tests/test_rdna2_direct_translate.py -v: 4/4 OK (direct .fssm and .code translation matches the .s text path byte-for-byte, embedded data word pruning, fspx_unpack --disasm --backend python decodes v_dot4_i32_i8 op_sel with 0 invalid words, make_game.py preflight succeeds without llvm-mc, spirv-val --target-env vulkan1.2 clean).
  • python3 -m unittest tests/test_rdna2_decode.py tests/test_fspx_unpack.py tests/test_shader_disasm.py tests/test_text_write_line_endings.py: 45/45 OK.
  • Synthetic fixtures only; no game files.

AI-assisted.

@Yourrespawn

Copy link
Copy Markdown
Contributor

Tested ca692f62 on NVIDIA GeForce RTX 4080 (Ada, subgroup 32..32), driver 617.14, Vulkan API 1.4.351, Windows 11 Pro 10.0.26200: pass.

  • New decode tests: test_rdna2_direct_translate OK (direct .fssm/.code translation matches the text path), test_rdna2_decode + test_fspx_unpack 30/30 (Python 3.12.10, llvm-mc 21.1.8 also present for the A/B)
  • Semantic suite: 2567 OK — the suite grew by ~900 tests with this branch's fixtures and everything passes
  • run_gpu_tests.py on the default path (llvm-mc, unchanged behaviour): 139/185 passed, 46 skipped (native wave64, expected on this 32-lane GPU)
  • valu_fuzz --family mem --programs 150 --seed 20 --emulate against the branch: 300/300 programs match, 0 mismatches

The default stays llvm-mc as described, so this lands zero behavioural change for existing setups while the --backend python path removes the dependency for the formats llvm-mc rejects (the v_dot4_i32_i8 op_sel case verified in the new tests).

AI-assisted (ZCode agent): builds and runs executed by a coding agent on this machine.

@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

Tested ca692f6 on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 170/185 passed, 15 skipped, 0 failed on both GPUs. Compared with the same suite at the merge base 4947579 on the same machine, every test has the same result.
  • python -m unittest discover -s tests/semantic: 2567 OK
  • tests/test_rdna2_decode.py: 8 OK
  • tests/test_rdna2_direct_translate.py: 4 OK

No game files. AI-assisted.

@zneel

zneel commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 5e517fb7 on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 26.11.2, Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D CPU (64 GB RAM), Python 3.12.10: GPU suite passes. One decoder parity test fails..

Runtime and test programs built from this head (MSVC 14.51, Ninja, RelWithDebInfo), native Windows llvm-mc 21.1.8, Vulkan SDK 1.4.363.0. No game files.

run result
run_gpu_tests.py (whole suite, native wave64) 185/199, 14 skipped, 0 failed (main 49475796: 170/185)
same with LEONIDA_W64_EMULATE=1 185/199, 14 skipped, 0 failed
pytest tests/test_make_game_argparse.py tests/test_rdna2_decode.py tests/test_rdna2_direct_translate.py tests/test_translate.py 1 failed, 609 passed, 4 skipped, 13 subtests passed in 99.55s (0:01:39)

The failure is tests/test_rdna2_decode.py::test_parity_with_llvm_mc_on_shader_fixtures, on a fixture that came from main after this branch was written: runtime/test/shaders/ps_sdwa_f16_const.asm (d78546f, SDWA with an inline float constant).

ps_sdwa_f16_const.asm: v_add_f16_sdwa
  decoder:  [('vgpr', 2, ...), ('raw', 'sreg_242', False, False), ('vgpr', 1, ...)]
  llvm-mc:  [('vgpr', 2, ...), ('fimm', 1.0, False, False),       ('vgpr', 1, ...)]

For v_add_f16_sdwa v2, 1.0, v1 ... src0_sel:WORD_1, the Python decoder doesn't map an SDWA src0 with the S0 (scalar) bit set and value 242 to the inline constant 1.0, while llvm-mc does. The test stops at the first mismatch, so I don't know whether the fixture's s8 case decodes correctly. SDWA scalar sources probably need the same inline-constant table (128–208 integers, 240–248 floats) the other encodings use.

The GPU suite (default llvm-mc path) passes. I didn't run the --backend python / --disasm-backend python paths on the GPU.

AI-assisted (Claude Code); I checked the results.

@SweatierKey
SweatierKey force-pushed the rdna2-direct-translate branch from 5e517fb to 1487c0f Compare November 22, 2026 21:08
@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thank you @zneel for the RX 9070 XT GPU check and for pinpointing that exact SDWA inline-constant mismatch on ps_sdwa_f16_const.asm!

Fixed in the base decoder PR #2038 (998128ec) and restacked here (1487c0fd):

  • Root cause: the VOP1, VOPC, and VOP2 SDWA decoders in tools/rdna2_decode.py called _sreg(..., 1) instead of _src(..., 1, None) when s0_is_sgpr / s1_is_sgpr was set, so codes >= 128 (inline integer constants 128..208 and inline float constants 240..248, including 1.0 = 242 and 2.0 = 244) fell through to raw sreg_<N>.
  • Fix: switched all three SDWA scalar-source decodes to _src(..., 1, None) and added test_sdwa_inline_constants_and_sgpr in tests/test_rdna2_decode.py covering all four SDWA instructions from ps_sdwa_f16_const.asm (v_add_f16_sdwa with 1.0 and s8, v_mul_f16_sdwa with 2.0, v_cvt_f16_u16_sdwa with 1.0) plus VOPC SDWA inline integers and disasm_bytes round-trip.
  • Verified: test_rdna2_decode.py (9/9 OK), test_rdna2_direct_translate.py (4/4 OK), test_text_write_line_endings.py (2/2 OK).

@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested f4ae9bd6 on NVIDIA GeForce RTX 3060 Ti (subgroup 32..32), driver 580.173.02, Linux (kernel 6.18), Python 3.14, llvm-mc 21.1.8 (Ubuntu, /usr/lib/llvm-21): GPU suite passes, one decoder parity test still fails.

  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed. ctest: 47/47.
  • pytest tests/test_make_game_argparse.py tests/test_rdna2_decode.py tests/test_rdna2_direct_translate.py: 1 failed, 47 passed, 1 skipped (base tools/: 5 failed).

The failure is still test_parity_with_llvm_mc_on_shader_fixtures on ps_sdwa_f16_const.asm, but one instruction later than @zneel's. v_add_f16_sdwa decodes now; it stops at v_cvt_f16_u16_sdwa v5, 1.0 ... src0_sel:WORD_1:

AssertionError: 'f9a00a7ef2148500' != 'f9a00a7eff148500003c0000'   (tests/test_rdna2_decode.py:263, di["enc"] vs li["enc"])

The decoder is right here: f9a00a7ef2148500 is the 8 bytes llvm-mc itself assembled. The other side is llvm-mc's disassembler output, which doesn't round-trip this one:

$ echo "0xf9 0xa0 0x0a 0x7e 0xf2 0x14 0x85 0x00" | llvm-mc -disassemble -triple=amdgcn--amdpal -mcpu=gfx1013 -show-encoding
	v_cvt_f16_u16_sdwa v5, 0x3c00 dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE src0_sel:WORD_1 ; encoding: [0xf9,0xa0,0x0a,0x7e,0xff,0x14,0x85,0x00,0x00,0x3c,0x00,0x00]

It prints inline constant 242 on this 16-bit integer op as the literal 0x3c00, then re-encodes it as 0xff plus a literal dword. SDWA has no literal, and the result is 12 bytes, not 8, so every pc after it in that fixture is also 4 bytes off.

Two possible fixes in the test:

  • Compare di["enc"] with the per-instruction encodings from the assembly pass. You already parse them to build raw, and the pc values would come from them too.
  • Or special-case SDWA v_cvt_f16_{u16,i16} with an inline float constant, where llvm-mc prints 0x3c00 and the decoder 1.0. norm_arg would hit the same thing right after enc.

No game files. AI-assisted (Claude Code).

@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thanks @TreXito for the RTX 3060 Ti check and for isolating that llvm-mc 21 disassembler round-trip bug on v_cvt_f16_u16_sdwa v5, 1.0!

Fixed in base PR #2038 (3060b987) and synced here (ccab6c7e):

  • test_parity_with_llvm_mc_on_shader_fixtures now records each instruction parsed from the assembly pass (asm_insns) and always checks di["enc"] == ai["enc"] against the real assembled bytes (never relying on llvm-mc -disassemble's re-encoded length).
  • When llvm-mc -disassemble fails to round-trip its own assembled bytes (li["enc"] != ai["enc"], as with LLVM 21 printing 0x3c00 + bogus 12-byte 0xff literal for v_cvt_f16_u16_sdwa v5, 1.0), operand/op/modifier parity compares against ai (the assembled instruction) while keeping full llvm-mc -disassemble comparison for every round-tripping instruction.
  • Verified: test_rdna2_decode.py (9/9 OK), test_rdna2_direct_translate.py (4/4 OK), test_make_game_argparse.py (35/35 OK, 1 Windows-only skipped).

@SweatierKey
SweatierKey force-pushed the rdna2-direct-translate branch from ccab6c7 to eac43b1 Compare November 23, 2026 03:56
@SweatierKey

Copy link
Copy Markdown
Contributor Author

Rebased onto main (2d3369a4, head eac43b14) after the base decoder (#2038) merged into main:

  • Resolved tools/fspx_unpack.py to pass backend=a.backend, wave32=meta.get("wave32", True) in main's pre-write disasm.disasm() call.
  • Included the exp null k="special" fix for ps_w64_a_discard.asm.
    Verified: test_rdna2_decode.py + test_rdna2_direct_translate.py + test_make_game_argparse.py + test_fspx_unpack.py 76/76 OK (2 Windows-only skipped).

@CuteBooty

Copy link
Copy Markdown

Retail FSPX backend comparison at eac43b1

On Xbox package 1.0.2.1, all 29 compute programs translated and passed Vulkan 1.2 validation through the Python listing backend. The LLVM listing backend passed 26 of the same 29 at the same PR revision.

Result LLVM (llvm-mc) Python
Listings produced 29 29
Listings reporting zero invalid words 21 29
Translated and independently validated 26 29

I ran fspx_unpack.py --disasm with each backend and checked that both runs used identical program bytes, dispatch state and metadata. I translated each set of listings with rdna2spirv.py and checked accepted modules with spirv-val --target-env vulkan1.2. Of the 26 programs both accepted, 17 produced byte-identical SPIR-V and nine differed. The remaining three passed only through Python. This measures the listing-to-SPIR-V path. GPU execution and semantic equivalence were outside this test.

Windows, Ryzen 5 3600, CPython 3.14.10, LLVM 23.1.5 (gfx1030), SPIRV-Tools 2026.4. Codex assisted with measurement and verification.

@Flyander Flyander 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.

The direct backend needs to fail closed when it encounters an unsupported multiword encoding. The decoder currently advances only four bytes for an unknown VOP3P opcode, so trusting its PCs can silently misparse the rest of the shader.

Comment thread tools/rdna2spirv.py
if any(i.get("normalized") for i in insns):
raise Unsupported("reachable instruction with normalized encoding")
else:
if info.get("direct"):

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.

[P2] Reject or preserve the complete size of unknown VOP3P instructions before trusting direct PCs. decode_bytes advances by the length returned from _decode_one; the unknown-opcode VOP3P path returns (None, 4) after reading its 8-byte base encoding (and it may also have a literal). The next dword is then decoded as a new instruction, and this direct path trusts those shifted PCs and can translate bogus control flow for retail shaders containing a VOP3P opcode absent from the table. Preserve the full encoding length, including literal handling, or reject the direct stream instead of translating it.

@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thanks @Flyander for catching that! Pushed both parts of the fix in 18ffaf06:

  1. Decoder length preservation (tools/rdna2_decode.py): unknown VOP3P and VOP3 opcodes in _decode_one() now return their full 8-byte length (or 12 bytes when any of src0/src1/src2 is 255 and 12 bytes are available) instead of 4, so w1 / literals are not decoded as separate instructions in linear sweep.
  2. Fail-closed direct binary translation (tools/rdna2spirv.py): added decode_reachable_direct() for the direct binary path when info["invalid"] > 0. Instead of trusting linear-sweep PCs across undecodable words, it performs recursive-descent decoding from PC 0 (code_start), following exact instruction sizes, relative branches, and s_getpc_b64/s_setpc_b64 far branches:
    • Any undecodable instruction reachable from entry (including unknown 8-byte/12-byte VOP3P/VOP3 encodings at entry, mid-block, or branch targets) raises Unsupported immediately.
    • Any fallthrough past end of code or overlapping decoded instructions raises Unsupported.
    • Unreachable embedded data tables jumped over by branches (even if a data word starts with a VOP3P prefix like 0xCC7F0000) are never decoded and never swallow subsequent branch targets.
  3. Tests: added test_unknown_vop3p_and_vop3_preserve_full_instruction_size in tests/test_rdna2_decode.py and test_direct_translate_fails_closed_on_reachable_unknown_vop3p + test_direct_translate_skips_unreachable_data_resembling_vop3p in tests/test_rdna2_direct_translate.py.
  4. Verified: test_rdna2_decode.py (10/10) + test_rdna2_direct_translate.py (6/6) + test_make_game_argparse.py + test_fspx_unpack.py → 79/79 OK (2 Windows-only skipped) with spirv-val.

@Flyander Flyander 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.

The follow-up addresses my report: unknown VOP3P/VOP3 encodings now preserve their instruction width, the direct path fails closed on reachable unknown encodings, and targeted cases cover literals and unreachable embedded data. Thanks for the quick correction.

@CuteBooty

Copy link
Copy Markdown

RTX 3070 readback on one of the nine differing FSPX listing pairs

I ran the LLVM and Python translations of one retail wave32 compute shader from Xbox package 1.0.2.1 on the same synthetic input. Both versions preserved the entire 64 KiB buffer, including a nonzero sentinel, on two independent dispatches each (4/4 exact readbacks). Leonida’s full-workgroup interpreter predicted 128 aligned reads across two distinct words and zero stores per version. This establishes agreement on an inactive input. Active output and in-game image quality remain unverified.

Across all nine pairs, a traversal from the compute entry point found external buffer or image writes in 3/9 LLVM modules and 9/9 Python modules. In six pairs only Python had a reachable output write. Whether the shader executes that write depends on the input.

The GPU ran the original modules from eac43b14. Repeating fspx_unpack.py --disasm and the listing-to-SPIR-V translation at 18ffaf06 reproduced every accepted binary within each backend (26 LLVM, 29 Python). For readback I used vk_compute_test with a local extension to supply the scalar registers used by the interpreter, then compared the full initialized buffer after each dispatch.

Windows, Ryzen 5 3600, GeForce RTX 3070, NVIDIA 617.14, subgroup 32, CPython 3.14.10, LLVM 23.1.5 (gfx1030), SPIRV-Tools 2026.4. Codex assisted with testing and verification.

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 18ffaf06 (merge base 2d3369a4) on an NVIDIA GeForce RTX 5070 Ti (Blackwell, subgroup 32..32), driver 616.56, Windows 11 10.0.26300. Translator and fixtures came from this head; runtime test programs were built from main 98fa503f (MSVC 19.44, Vulkan-Headers fetched by CMake, loader from the NVIDIA driver); spirv-as/spirv-val and llvm-mc 21 came from WSL Ubuntu. Result: pass.

  • scripts/run_gpu_tests.py: 170/217 passed, 47 skipped (wave64 programs on a 32-wide GPU), 0 failed.
  • Compared test by test with main 98fa503f run through the fixed runner from run_gpu_tests: record cs_tbuffer_store's result; fail a run that records a test twice #4784 (170/217): every result is the same, except that this head does not list cs_tbuffer_store. That is main's own runner bug (its results.append sits under cs_fmt_d16_xyz), not this PR.
  • This PR adds no GPU fixture.

Not run: LEONIDA_W64_EMULATE=1, other vendors, Linux. No game files.

AI-assisted (Claude Code).

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown

Tested 18ffaf0 (merge base 7b7d73f) on NVIDIA GeForce RTX 5080, Linux 580.65.06: pass

Tested the PR head merged with current main 7b7d73f8 (clean merge, not pushed). Runtime harnesses built from main 7b7d73f8, llvm-mc 21, Python 3.13, Vulkan SDK 1.4.363, Linux native.

Check main 7b7d73f8 this PR
run_gpu_tests.py native 170/217 passed, 47 skipped 170/217 passed, 47 skipped
run_gpu_tests.py with LEONIDA_W64_EMULATE=1 201/217 passed, 16 skipped 201/217 passed, 16 skipped
failed (native / emulated) 0 / 0 0 / 0
tests/semantic/regen_fixtures.py --check ok ok
pytest tests/test_make_game_argparse.py tests/test_rdna2_decode.py tests/test_rdna2_direct_translate.py tests/test_shader_checks.py tests/test_run_gpu_tests.py tests/test_cf_diff.py 162 passed, 315 subtests passed (3 shared files) 213 passed, 1 skipped, 331 subtests passed in 14.47s

Line-by-line against my main run (results that differ from main):

  • native:
    • none
  • emulated:
    • none

Not run: the full unittest discover -s tests/semantic (about 6 minutes, and main alone has failures there); only the test files this PR touches plus the shared checker tests ran. No other GPU vendor, no Windows. No game files.

AI-assisted (Claude Code).

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.

9 participants