Skip to content

rdna2spirv: a reachable fall-through into undecodable words is refused, not a silent end of the path (SHADER-813) - #4595

Open
tedin7 wants to merge 3 commits into
ItsNotRexed:mainfrom
tedin7:shader-813
Open

tedin7 wants to merge 3 commits into
ItsNotRexed:mainfrom
tedin7:shader-813

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue as: SHADER-813 (#4571 (comment))

Why

reachable_code in tools/rdna2spirv.py followed fall-through with k = by_pc.get(nxt) and simply stopped when no instruction started at the next address. In a listing with words llvm-mc could not decode (it prints ; invalid instruction encoding and skips them), an instruction that runs into such a gap therefore ended the reachable path there. Everything after it (exports, buffer and image stores) was dropped, and the module still translated and passed spirv-val: a shader that runs and writes nothing, with no error. @aslan2142 found it on the FidelityFX FSPX programs, whose v_dot4_i32_i8 ... op_sel words LLVM 23 cannot decode (finding: #4421 (comment)).

What

  • reachable_code raises Unsupported("fall-through into undecodable words") when an instruction that can fall through has no instruction at its next address and that address is still inside the code (before the end of the last instruction). The end of the code is unchanged: falling off the last instruction still ends the path, as before.
  • Branch targets were already checked (branch into data); this closes the fall-through side.
  • docs/design.md (Code-embedded data) and docs/fspx.md (Translation status) say so; the FSPX counts there predate this and are marked as such, since a recount needs the package.
  • Decoding those words is rdna2_decode: pure-Python RDNA2 binary ISA decoder (#1901) #2038 / rdna2spirv/fspx_unpack/make_game: direct binary RDNA2 decode backend (#1901) #2323; this only makes the count honest until then.

Tested

  • New test_fall_through_into_undecodable_words_is_refused in tests/test_rdna2spirv_decode.py: the finding's synthetic listing (two v_mov_b32, the word 00 50 16 cc 01 05 0e 0c llvm-mc skips, exp mrt0 ... done vm, s_endpgm) with a matching FSSM blob. Fails on main (translates without the export), passes with the fix. The file: 25 tests OK.
  • scripts/run_semantic_tests.py over tests/semantic (365 files, 7,444 tests) before and after on Linux, Python 3.13: identical, the same 19 files not ok on both (pre-existing on this pod).
  • Reviewed by an independent model (DeepSeek): no false refusal found for branch targets, wave64, re-anchored pcs, s_getpc tables after s_endpgm, or listings without invalid words.
  • Not covered: undecodable words at the very start of the code (before the first instruction llvm-mc printed); reachable_code starts at that first instruction and does not see the code start. Not seen in any finding.
  • Not run: the game or FSPX package (no game files here).

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

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

Ran this at 3468d024 on Windows 11: tests/test_rdna2spirv_decode.py -> 25 tests, OK; test_docs_links.py 6 OK; git diff --check clean.

reachable_code now distinguishes "fall-through past the last decoded instruction" (allowed; k stays None and the walk ends) from "fall-through into a hole before later decoded code" (nxt < code_end, raises Unsupported("fall-through into undecodable words")). That is the right split: with no later instruction there is nothing to drop, and with later code the new refusal prevents silently losing its stores and exports. The added decode cases pass. No blocking issue.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 3468d02 merged with main d0835dd (already up to date) on Linux (WSL2) Python 3.12.16 and native Windows 11 Python 3.13.1: pass.

  • tests/test_rdna2spirv_decode.py: 25/25 OK on both. tests/test_docs_*.py (7 files): OK on Linux.
  • Negative control: tools/rdna2spirv.py from main, same tests: 1 failure (the new test_fall_through_into_undecodable_words_is_refused).
  • Read the diff: the refusal fires only when the instruction after a fall-through is missing from the instruction list but still inside the decoded range (nxt < code_end); a path that simply ends at the last instruction behaves as before, and the loop already stops on k is None. The fixture is hand-built (a v_mov/exp/s_endpgm listing with an invalid instruction encoding gap), no game data. The docs/fspx.md note says this refuses FSPX programs 04, 05, 06, 09, 12 and 15 of package 1.0.2.1; I could not recount that without the package.

@CuteBooty

Copy link
Copy Markdown

Retail FSPX recount for Xbox package 1.0.2.1

Of the same 29 compute programs, 26 translated and passed Vulkan 1.2 validation on d0835dd9f9ec (before the fix), and 20 passed on PR head 3468d024d152. All 20 accepted by both revisions produced byte-identical SPIR-V. Six passed only before the fix, zero passed only at head, and three were rejected by both. This gives docs/fspx.md a measured post-change count of 20/29 for the LLVM gfx1030 path.

I compared both rdna2spirv.py revisions against one set of listings from fspx_unpack.py --disasm, then ran spirv-val --target-env vulkan1.2 on each accepted output. Across the nine head rejections, the translator reported eight fall-through refusals and one other unsupported condition. This was an offline compiler check on Windows (Ryzen 5 3600, CPython 3.14.10, LLVM 23.1.5, SPIRV-Tools 2026.4). GPU execution and image quality remain untested.

AI-assisted with Codex for measurement and verification and Claude Opus for test design.

@theswiss24

Copy link
Copy Markdown

Tested 3468d024 (contains main d0835dd9) on four GPUs, three machines, with test programs built from main d0835dd9 on each (this PR changes no C++): pass on all four. Every test has the same result as main on each device.

GPU driver, OS run_gpu_tests.py (LEONIDA_W64_EMULATE=1)
RTX 2060 SUPER (Turing) NVIDIA 580.178.04, Ubuntu 24.04.5 195/210 passed, 15 skipped, 0 failed
GTX 1650 Mobile (Turing) NVIDIA 595.99.02, Ubuntu 24.04.5 195/210 passed, 15 skipped, 0 failed
MX150 (Pascal) NVIDIA 580.178.04, Linux Mint 22.3 185/210 passed, 25 skipped, 0 failed (no mesh shaders or fragment barycentrics)
Intel UHD 630 (Gen9.5) Mesa ANV 25.2.8, Ubuntu 24.04.5 181/210 passed, 27 skipped, 2 failed, the same 2 as main (cs_fmt_int_narrow, my finding in #4571, and cs_signed_zero_nan, the known Mesa 25.2 NIR issue)

llvm-mc 20.1.2 on all. No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

@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 internal-hole regression is useful, but this bound misses reachable undecodable bytes at the end of the declared code range. The assembly header already carries the range end, and translate() has the native blob, so the actual code extent is available to the caller.

Comment thread tools/rdna2spirv.py
k = by_pc.get(nxt)
if k is None and op not in ("s_endpgm",):
break
if k is None and nxt < code_end:

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] Check against the declared code end, not the last decoded instruction. code_end is computed from insns, so it is always the end of the last instruction LLVM decoded. If that reachable instruction falls through into an undecodable span at the end of the declared code range, nxt == code_end and this condition permits the path to end silently. The header parsed by shader_ir.HDR has the real code end, and translate() already has native_blob; pass the actual extent into reachable_code() and add a synthetic trailing-gap case. Otherwise the claimed refusal still drops a reachable final instruction with no later decoded instruction to extend code_end.

@Atomic258

Copy link
Copy Markdown

Tested head 3468d024 locally (CPU only, synthetic fixtures, no game files).

Environment: Linux, Python 3.10.12, no spirv-tools/GPU.

Commands:

python3 -I tests/test_rdna2spirv_decode.py
python3 -m unittest discover -s tests/semantic

Results:

  • test_rdna2spirv_decode.py: OK (the PR's new decode/refusal cases pass).
  • Full semantic suite: 7456 tests, FAILED (failures=7, errors=3, skipped=7, expected failures=1).
  • Control: the same suite on fresh origin/main gives byte-identical failures — same 7 failures + 3 errors, same test IDs (test_cmp_class_f64.test_rejected_forms ×2, test_loop_ballot_barrier divergent-loop ×3 + test_bug4b_loop_carried_split_sgpr_pair_ballot/test_nested_divergent_loops_emulate errors, test_mutations ×2, test_vcmp16_opsel.test_spirv_val_compliance — the last needs spirv-as, absent here). None are introduced by this PR; the fall-through refusal change adds no new failures.

AI-assisted verification (Devin), no game files, no GPU.

…o undecodable words after the last printed instruction are caught too (review: Flyander); fspx.md: measured 20/29
@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks @Flyander, right: the bound was the end of the last printed instruction, so undecodable words between it and the end of the declared range were missed. Fixed in 18f0b74:

  • shader_ir.parse_file keeps the end of the code range from the listing header (; name code 0x..0x<end>, exclusive, as shader_disasm.py writes it) as info["code_end"], and translate passes it to reachable_code, which now takes the larger of that and the end of the last instruction. Listings without a header, and the synthetic code 0x0..0x0 ones, behave as before.
  • New test_fall_through_into_undecodable_words_at_the_end_of_the_range_is_refused: two v_mov_b32 and then 8 bytes llvm-mc skips, inside a declared range 0x0..0x10. Fails on the previous head (translates), passes now. tests/test_rdna2spirv_decode.py: 26 OK.
  • tests/semantic (365 files, 7,444 tests) before and after: the same list of not-ok files (identical checksum of the list; pre-existing on this machine).

Thanks @CuteBooty for the retail recount: docs/fspx.md now gives your measured 20/29 after the change (26/29 before, byte-identical SPIR-V for the 20 kept), credited. Thanks also @kapitche, @Kam471, @theswiss24 and @Atomic258 for the runs and the negative control.

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

@CuteBooty

Copy link
Copy Markdown

Retail recount at 18f0b74

Confirmed the documented 20/29 on 18f0b74a6abc after the code-range update. I ran rdna2spirv.py on the same 29 Xbox 1.0.2.1 FSPX compute programs at this commit and its parent 3468d024d152. Both accepted the same 20 and rejected the same nine. All 20 accepted SPIR-V binaries matched byte for byte across revisions and passed a separate spirv-val --target-env vulkan1.2 check.

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

@lunarminus

Copy link
Copy Markdown

Recount of 18f0b74a (merged onto main d0835dd9) on the 29 FSPX programs from my own 1.0.2.1 amd_fidelityfx_upscaler_xbox.dll, with Linux LLVM 23.1.5 and --no-asm as the shaders stage runs it: 20/29 translated and valid, matching the count above. This adds direct evidence that the 6 newly refused programs were empty. Counts only.

AI-assisted (Claude Code).

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Tested 18f0b74 (a re-test at the new head) 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): 196/210 passed, 14 skipped, 0 failed on both GPUs, with every test's result the same as at the merge base d0835dd.
  • tests/test_rdna2spirv_decode.py: 26 run, OK.
  • python -m unittest discover -s tests/semantic: 7456 run, 7 failures and 2 errors. That is main's known set at d0835dd (test_cmp_class_f64, test_mutations, test_loop_ballot_barrier, all since fixed or with fixes open). The PR adds none.
  • Note: main has moved 912 commits past that merge base, so this doesn't cover the branch merged onto current main.

No game files. AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 18f0b74 on Intel UHD 630 (CFL GT2), Mesa 25.2.8 ANV, Ubuntu 24.04: the PR's tests pass (tests/test_rdna2spirv_decode.py, 26 tests, including both new fall-through cases; tests/test_semantic_translator_use.py, 3 tests). run_gpu_tests.py on the Intel device: 148 pass, 2 fail (cs_fmt_int_narrow, cs_signed_zero_nan); the same two fail with the same values on main 98fa503 on this GPU, so they are not from this PR. AI-assisted.

@lunarminus

Copy link
Copy Markdown

Tested 18f0b74a merged onto main at d0835dd9 (the PR's base; main has moved on since) on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • The merge onto d0835dd9 is a fast-forward. The PR changes no C++, so the test programs come from a d0835dd9 build.
  • tests/test_rdna2spirv_decode.py: 26/26 OK.
    • Negative control: the same test file against main's tools/ fails exactly the two new tests (test_fall_through_into_undecodable_words_is_refused and ..._at_the_end_of_the_range_is_refused).
  • Other tests: tests/test_shader_ir.py 37 OK, tests/test_docs_*.py (7 files) OK.
  • python -m unittest discover -s tests/semantic: 7,456 tests, failures=7, errors=2, skipped=1, expected failures=1, the same as d0835dd9 without the PR. The lists of failing tests are identical (test_cmp_class_f64 x2, test_loop_ballot_barrier x5, test_mutations x2), so none of them comes from this PR.
  • scripts/run_gpu_tests.py:
    • native: 196/210 passed, 14 skipped, 0 failed.
    • LEONIDA_W64_EMULATE=1: 196/210 passed, 14 skipped, 0 failed.
    • Per test, every one of the 210 results matches d0835dd9 without the PR in both modes. The PR adds no GPU fixtures.

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.

10 participants