Skip to content

shader_disasm: decode scalar tuples into vcc with rdna2_decode when llvm-mc 21/22 rejects them - #7104

Open
quackottu wants to merge 3 commits into
ItsNotRexed:mainfrom
quackottu:disasm-vcc-tuple-fallback
Open

quackottu wants to merge 3 commits into
ItsNotRexed:mainfrom
quackottu:disasm-vcc-tuple-fallback

Conversation

@quackottu

@quackottu quackottu commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Item: SHADER-1108

llvm-mc 21 and 22 reject scalar register tuples that run into vcc (SGPRs 106-107): SMEM data s[100:107], s[104:107], s[92:107], and an image's resource or sampler or a buffer's resource on those registers. llvm-mc then decodes the words after them misaligned, and the translator refuses the program with "reachable instruction with normalized encoding" (docs/shader_library.md §5: the largest refusal class in the pipeline-library corpus). 21.1.8 is still scripts/build_llvm_mc.ps1's default. LLVM 23 decodes these itself (#6813's table: 15,551 vs 16,699 of 21,521 library programs pass, 21.1.8 vs 23.1.5).

tools/rdna2_decode.py already decodes these words as the [s100,...,vcc_lo,vcc_hi] list that shader_ir parses and the translator reads (design.md, Scalar/buffer memory), the same form LLVM 23 prints. Change in shader_disasm.disasm (used by shader_disasm.py, fspx_unpack.py --disasm and shader_library.py --disasm):

  • When llvm-mc reports invalid words, the Python decoder finds the instructions whose register list ends in vcc_lo,vcc_hi].
  • Those bytes go to llvm-mc as s_nop 0 words, and llvm-mc decodes everything else as before.
  • The Python lines are spliced back in at their offsets. Their encoding comment holds the original bytes, so the translator's printed-encoding check still compares them.
  • If no such instruction exists, or the offsets don't line up (counted from the printed encodings plus 4 bytes per invalid word in llvm-mc's stderr), the llvm-mc output is returned unchanged.

Tests

tests/test_shader_disasm.py, VccTupleFallbackTest. It uses one synthetic program: s_load_dwordx8 into s[100:107], s_load_dwordx4 into s[104:107], image_sample with resource s[100:107] and sampler s[104:107], buffer_load_dword with resource s[104:107], then v_mov_b32 and s_endpgm.

  • With a fake llvm-mc (runs everywhere), it checks: 0 invalid words, each tuple printed as the list form, shader_ir parses an slist, and the encodings concatenate back to the input bytes.
  • The same check runs with the real llvm-mc when LLVM_MC is set.
  • Both fail on main.

With llvm-mc 21.1.8 on Windows: test_shader_disasm (31), test_fspx_unpack, test_shader_library, test_rdna2spirv_decode pass. test_rdna2_decode has the same one failure on main (op_sel parity with llvm-mc 21). scripts/check_no_game_data.py is clean on the changed files.

Corpus counts for the affected library programs (main vs this branch, llvm-mc 21.1.8) follow as a comment. The FSSM corpus has not been re-run with this change.

AI-assisted.

…lvm-mc rejects them

llvm-mc 21 and 22 (21.1.8 is scripts/build_llvm_mc.ps1's default) reject
SMEM data, MIMG resource/sampler and MUBUF resource tuples that run into
vcc (s[100:107], s[104:107], s[92:107]) and then decode the words after
them misaligned, so the translator refuses the program with "reachable
instruction with normalized encoding". rdna2_decode prints those words as
the [s100,...,vcc_lo,vcc_hi] list the translator already reads (as LLVM
23 does): give llvm-mc s_nop words in their place and splice the Python
lines back in at the same offsets. llvm-mc keeps every other word.
@kapitche

Copy link
Copy Markdown
Contributor

Tested acab5ca merged with main 8575d36 on Linux/Python 3.12.16: pass

Commands run (merged worktree, -I isolated mode):

  • python -I tests/test_shader_disasm.py -> Ran 31 tests, OK (skipped=1: test_with_real_llvm_mc, since LLVM_MC is not set on Linux)
  • python -I tests/test_rdna2_decode.py -> OK; python -I tests/test_shader_ir.py -> OK
  • Negative control: git checkout upstream/main -- tools/shader_disasm.py, rerun test_shader_disasm.py -> FAILED (failures=1): test_tuples_into_vcc_take_the_python_decoder_line fails at assertEqual(bad, 0) with 8 != 0. Restored with git checkout HEAD -- tools/shader_disasm.py.
  • Windows/Python 3.13.1 (native, scratch worktree with the merged tools/ and test file), LLVM_MC=...\llvm-mc.exe (LLVM version 23.1.6): python -I tests\test_shader_disasm.py -> Ran 31 tests, OK, with test_with_real_llvm_mc running (not skipped). I did not run that real-llvm-mc test against main's shader_disasm.py, so I cannot say whether llvm-mc 23.1.6 still needs the fallback; the Linux test uses a fake llvm-mc that rejects those words and that is what the negative control exercises.

Diff read: after llvm-mc reports invalid words, the instructions whose Python decode contains vcc_lo,vcc_hi] are replaced with s_nop words, llvm-mc is rerun, and the Python lines are spliced back at the right byte offsets; it returns None (keeping the old output) unless every spliced line is placed and the byte count matches the input.

@quackottu

quackottu commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Note: #4843 (tedin7) proposes llvm-mc 23.1.0 as the minimum. LLVM 23 decodes these vcc tuples itself, so if #4843 lands this fallback only matters for anyone still on 21/22 (e.g. a build from an older scripts/build_llvm_mc.ps1). With 23 it only runs on a listing that has other invalid words as well as a vcc_lo,vcc_hi] list; it then swaps LLVM 23's line for the Python decoder's line for the same bytes, the same list form.

The fallback read each warning's byte offset from its column, which holds
only while the program goes to llvm-mc as one line. ItsNotRexed#4210 splits it into
short lines; take the offset from the warning's line and column in the
text that was sent, so the splice works with either input layout.
@quackottu

Copy link
Copy Markdown
Contributor Author

Pushed 9a15309: the invalid words' offsets now come from each warning's line and column in the text sent to llvm-mc, so this works with #4210's short input lines as well as one line (test_input_in_short_lines, fake llvm-mc). Whichever of the two lands second has a one-line conflict where the input text is built.

llvm-mc prints a v_fma_mix* literal cut from its encoding comment (8 bytes
shown, 12 read), so counting the printed bytes put every later
instruction 4 bytes early and the splice gave up. Where llvm-mc and
rdna2_decode name the same instruction at an offset, advance by
rdna2_decode's length.
@wavehs

wavehs commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested b8b2dbc35 on Apple M3 Pro (macOS arm64, CPython 3.15.2, Homebrew LLVM 23.1.5 with the AMDGPU target; CPU only, synthetic inputs, no game files): pass.

  • python3 -I tests/test_shader_disasm.py -> 33 tests, OK (skipped=1: the real-llvm-mc case, LLVM_MC unset).
  • LLVM_MC=/opt/homebrew/opt/llvm@23/bin/llvm-mc python3 -I tests/test_shader_disasm.py -> 33 tests, OK, with test_with_real_llvm_mc running. Caveat worth knowing: llvm-mc 23.1.5 decodes these vcc tuples natively (I fed the test's synthetic program to it directly and got the list form for all four instructions with no warnings), so on 23 that test passes without the fallback — the fallback itself is exercised here by the fake llvm-mc in the other three cases, exactly as on your Linux/LLVM-21 run.
  • Negative controls, two of them because the first alone is weak:
    1. main's tools/shader_disasm.py + head's test file -> FAILED (errors=3, skipped=1), an AttributeError: module 'shader_disasm' has no attribute '_mc_text', i.e. the test fails on main but on the refactor rather than on the behaviour.
    2. head's tool with only the fallback call in disasm() removed (helper and _mc_text refactor kept) -> FAILED (failures=3, skipped=1), each at self.assertEqual(bad, 0) with AssertionError: 8 != 0. So the three tests do test the new behaviour, not just the new helper's existence.
  • Related suites on this head: test_fspx_unpack OK (skipped=1), test_shader_library OK, test_shader_ir OK, test_rdna2_decode OK (skipped=1), test_rdna2spirv_decode OK. I could not reproduce the test_rdna2_decode op_sel failure you see with llvm-mc 21 — it is the LLVM-version difference, not this PR.
  • Merges cleanly onto origin/main dde82f27e; the suite is 33 tests OK (skipped=1) with and without LLVM_MC on the merged tree. python -m compileall -q, python -I scripts/check_no_game_data.py on both changed files and git diff --check are clean.

Code read: the splice is conservative — it returns None (unchanged llvm-mc output) unless every tuple is placed and the byte count plus the remaining invalid words add up to the input length, so a misaligned decode keeps today's behaviour instead of emitting a half-spliced listing. _invalid_offsets counts whitespace tokens per line, which matches both the one-line and the #4210 short-line inputs; NOP * (n // 4) covers the 8-byte forms. I could not run the corpus counts or the FSSM corpus (no game files here), so the 15,551 -> 16,699 library figure is unverified by me.

AI-assisted.

@quackottu

Copy link
Copy Markdown
Contributor Author

Corpus counts: the 1.0.2.1 pipeline library (21,521 programs, default stages), llvm-mc 21.1.8 gfx1013, Windows. Each listing goes through rdna2spirv.translate, then spirv-as and spirv-val (Vulkan 1.2). Counts only.

The 3,566 programs that hold a scalar tuple into vcc. I found them by scanning each program's code with rdna2_decode. Both runs use this branch's translator; only the disassembly differs.

stage programs main's disasm this PR's disasm
VS 39 0 39
MS 741 0 683
PS 1,863 0 323
CS 923 0 160
total 3,566 0 1,205

On main, 3,536 of them stop on "fall-through into undecodable words" and 30 on other refusals. Every program that translates passes spirv-val, on both sides. With this PR, the rest stop on:

Whole library:

VS MS PS CS total of 21,521
main (1cc6e3c12) 1,296 6,744 6,119 1,538 15,697
main + this PR 1,335 7,427 6,442 1,698 16,902

0 spirv-val failures on either side. With this PR, VS, MS and PS match #6813's llvm-mc 23.1.5 row exactly (1,335 / 7,427 / 6,442), and the remaining "fall-through into undecodable words" count, 1,731, matches #6813's LLVM 23 count too.

Two fixes since the first push came out of this run:

The FSSM corpus was not re-run.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested b8b2dbc on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_shader_disasm.py - all exit 0.

AI-assisted (Claude Code).

@rideinstead

Copy link
Copy Markdown

✅ Automated security review: CLEAN (commit b8b2dbc3)

  • No findings.

Scope: static analysis of added diff lines only; no code was executed. Please review findings manually.

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