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

2 participants