Skip to content

fix: rdna2spirv decodes scalar, image and buffer tuples that run into vcc, so the pipeline-library shaders translate - #327

Closed
Andretuta wants to merge 1 commit into
mainfrom
old-pr/10062
Closed

Andretuta wants to merge 1 commit into
mainfrom
old-pr/10062

Conversation

@Andretuta

Copy link
Copy Markdown
Collaborator

Carried over from old PR 10062 by tedin7 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/10062, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
tedin7: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

What

tools/rdna2spirv.py: a scalar-memory, image or buffer instruction whose register tuple runs into vcc (SGPRs 106 and 107) is now decoded from the shader blob when llvm-mc rejects it. New restore_vcc_tuples reads the blob dwords of the code range that no matched instruction covers with the existing pure-Python rdna2_decode, keeps an instruction only if one of its operands is a [s100,...,vcc_lo,vcc_hi] list (the form the translator already takes), and drops the junk instructions llvm-mc printed inside its two words. Every other undecodable word behaves as before.

Why

docs/shader_library.md section 5 lists this as the largest refusal in the pipeline library: "reachable instruction with normalized encoding", s_load_dwordx8/x4/x16 into s[100:107]/s[104:107]/s[92:107], and image or buffer resources in those tuples (about 2,533 programs per that document; I cannot measure the real shaders.rpf and did not try). llvm-mc skips the first word as invalid and prints the second as junk, so translate() refused the shader, or, when the junk matched the blob, translated it with the load silently missing.

Tested

Synthetic words only, no game files. tests/semantic/test_vcc_tuple_fallback.py (new): llvm-mc 19 encodings with the register field patched into vcc, run on the CPU interpreter. Three shapes, each with the three ways the listing can show the rejected words (both invalid, second word printed as a matching junk instruction, second word printed with another encoding):

  • s_buffer_load_dwordx8 [s100..s105,vcc_lo,vcc_hi]: the store of s100, s105, vcc_lo, vcc_hi gives the four expected words (9 sub-cases across the file, 4 tests).
  • V# in [s104,s105,vcc_lo,vcc_hi] read by s_buffer_load_dword and by buffer_load_dword: loads 0xCAFE1234 and 0xBEEF5678.
  • image_load with the T# in [s100..vcc_hi]: translates through the descriptor lookup.
  • A rejected word that is not a tuple into vcc is still refused ("fall-through into undecodable words").

Results: the new file 4 tests, 9 translations also pass spirv-as + spirv-val --target-env vulkan1.2 (0 errors). Neighbours: test_fspx_rules.py 9 OK, test_printed_encoding.py 7 OK, tests/test_rdna2spirv_decode.py 25 OK, tests/test_rdna2_decode.py 17 OK (1 skipped), test_fma_mix_literal.py 18 OK, tests/test_shader_library.py 13 OK, five test_docs_* files OK.

Negative control: with the one-line call to restore_vcc_tuples removed (that is the behaviour of main), 9 sub-cases fail: "fall-through into undecodable words" for the two listings with no or matching junk and "reachable instruction with normalized encoding" for the third, the message the doc reports.

Not verified: llvm-mc 23 is not available here (only 19, which rejects these words as invalid), so a listing where llvm-mc 23 prints the tuple one lower with another encoding (not "invalid") goes through check_printed_encodings and is not covered by this PR. The translated shaders were not run on a GPU. The real library counts were not re-measured.

Lane: tools/rdna2spirv.py and a doc sentence only.

… vcc from the blob when llvm-mc rejects them

llvm-mc skips the first word of s_buffer_load_dwordx8 into s[100:107], an image resource in s[100:107] or a buffer resource in s[104:107] and leaves the second word as junk, so translate() refused those shaders (reachable instruction with normalized encoding) or translated them without the load. restore_vcc_tuples reads the uncovered words of the code range with rdna2_decode and keeps only instructions whose operands hold a VCCTuple list, the form the translator already takes. Other undecodable words are unchanged.
@Sigtryggur-Mani-Gudmundsson

Copy link
Copy Markdown

I compared six synthetic gfx1013 FSPX programs with real llvm-mc 19.1.7 and 23.1.5. LLVM 23 printed the explicit VCC register lists and preserved their bytes. LLVM 19 exposed a missing case in the submitted patch. After rejecting a scalar load, it can decode the second dword as exp and consume the first dword of the following instruction. Restoring the load alone leaves a gap. All six LLVM 19 listings were refused on the submitted code, including a load followed by a literal move that supplies the output address.

I published a follow-up that recovers that successor and keeps variable-length image recovery inside the declared code range. All six modules validated for Vulkan 1.2 under both toolchains. The five buffer cases returned the expected words on an RTX 4070 Ti. The image case verified descriptor lookup and module validity. I did not measure the retail corpus.

Fixture, measured outputs and patch instructions. The second mailbox commit applies on top of tedin7’s change.

@Tyrannys

Copy link
Copy Markdown
Contributor

Measured on a real shaders.rpf (own 1.0.2.1 copy). The pipeline-library stage from #19/#321 wrote 2,684 compute programs. Their listings came from llvm-mc 21.1.8, -mcpu=gfx1013, on Windows 11. I then translated <work>/shaders/library/disasm with this branch at its current head (063f14d): rdna2spirv.py --wave64-emulate.

translated (all pass spirv-val)
#321 head e0d4fd2b8 translator (no #327) 1,684 / 2,684
this branch 1,629 / 2,684

This branch is based on main, which lacks some translator fixes that #321 has, so the totals aren't directly comparable. The refusals are what matter here. On this branch:

881  fall-through into undecodable words
 91  CS thread-group-size SGPR
 27  ds_append
 26  s_bitreplicate_b64_b32 on a lane mask
 10  v_cmp destination
  9  undecodable words (cannot re-anchor)
  6  mask destination
  3  s_flbit_i32_b64 of a lane mask
  1  s_load_dwordx4 operands sgpr vcc
  1  reachable instruction with normalized encoding

976 of the 2,684 listings have invalid words. In a sample of 40 refused programs, the invalid words I decoded by hand are SMEM loads whose tuple runs into vcc: s_load_dwordx8 into s[100:107], s_load_dwordx4/s_buffer_load_dword* into or from s[104:107]. That's the case this PR targets. Yet nearly all of them are still refused as fall-through into undecodable words, so restore_vcc_tuples doesn't seem to reach that path. Only one program got as far as the operands sgpr vcc check. In the #321 runtime, 64 DeserializeComputePipelineStateX (slot 67) calls fail with "no translated compute shader", so these programs are on the boot path.

I'm happy to rerun on the real corpus if you push changes. AI-assisted (Claude Code).

@Tyrannys

Copy link
Copy Markdown
Contributor

Follow-up on top of this PR, for you to take if useful: Tyrannys@07f4dd738 (branch Tyrannys:pr327-vcc-followup, one commit on 063f14dcd).

  • forward-decode after a vcc tuple restored from the blob, skipping junk words marked "normalized";
  • wave32: vcc_hi is SGPR 107 (vcc_hi_as_sgpr, Gen.vcc_hi_sgpr makes %vcc_hi the %sgpr[107] element, and emit() drops the cached SGPR 107 state on a direct %vcc_hi store).

Numbers, 1.0.2.1 pipeline library (2,684 programs, --wave64-emulate): 2,396 translated and spirv-val-valid with it. Of the 25 compute programs the retail boot asked for and lacked, 21 now translate; the other 4 were a separate unpack bug (#583).

Suite: run_semantic_tests.py tests/semantic 460 files / 6,050 tests; 3 files fail (test_exec_computed, test_image_atomics_guard, test_image_cube_store_glc) and they fail the same at 063f14dcd without the commit.

Left in the library after both: 91 "CS thread-group-size SGPR", 73 "s_load_dwordx4 operands sgpr vcc", 51 "fall-through into undecodable words", 28 ds_append, 26 s_bitreplicate_b64_b32 on a lane mask, 9 "cannot re-anchor", 6 s_load_dwordx8 vcc, 3 s_flbit_i32_b64 of a lane mask.

AI-assisted (Claude Code).

@oscarlosu-lab

Copy link
Copy Markdown
Contributor

Tested 063f14dcd on Windows 11 (Python 3.15, llvm-mc 21.1.8, SPIRV-Tools v2026.4):

  • tests/semantic/test_vcc_tuple_fallback.py: 4/4 passed (0.317s).
  • Pipeline-library compute shaders (--stages cs --wave64-emulate): increased translated & spirv-val-passing programs from 1,684 / 2,684 to 1,768 / 2,684 (+84 programs).
  • Remaining 916 rejections in the library set: 881 fall-through into undecodable words, 18 s_flbit_i32_b64 of a lane mask, 9 undecodable words (cannot re-anchor), 6 s_bitreplicate_b64_b32 on a lane mask, 1 s_load_dwordx4 operands sgpr vcc, 1 reachable instruction with normalized encoding.

@oscarlosu-lab

Copy link
Copy Markdown
Contributor

A follow-up measured on top of this branch (063f14d), Windows 11, llvm-mc 21.1.8, --wave64-emulate, on the retail pipeline library (no game data here, counts only).

What still fails, and why. Of the library programs this branch still refuses, nearly all report fall-through into undecodable words. The rejected words are almost all the vcc-tuple forms this PR targets (SMEM s_load_dwordx8/x4 into s[100:107]/s[104:107], s_buffer_load_dwordx16 into s[92:107], MIMG with srsrc/ssamp reaching s106/s107). In many of them the instruction is restored, but llvm-mc's junk from the second word was a 64-bit instruction that ran past it and swallowed the first word of the next real instruction. restore_vcc_tuples drops that junk, but its words were in covered, so the swallowed instruction is never offered to the decoder and the path falls into a hole right after the restored one.

Suggested change: after restoring, decode from the end of each restored instruction with rdna2_decode until the stream meets the start (or any word) of a kept instruction again:

diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py
index 71317aeac..056584e55 100644
--- a/tools/rdna2spirv.py
+++ b/tools/rdna2spirv.py
@@ -17025,7 +17025,26 @@ def restore_vcc_tuples(insns, blob, code_start, code_end):
         return insns
     spans = [(r["pc"], r["pc"] + len(r["enc"]) // 2) for r in restored]
     kept = [i for i in insns if not any(i["pc"] < hi and i["pc"] + len(i["enc"]) // 2 > lo for lo, hi in spans)]
-    return sorted(kept + restored, key=lambda i: i["pc"])
+    # The junk llvm-mc printed from a restored instruction's second word can run past it and swallow the next real
+    # instruction's words; those words were "covered" only by the dropped junk. Decode from the end of each restored
+    # instruction until the stream meets the start of a kept instruction again.
+    starts = {i["pc"] for i in kept}
+    kept_words = set()
+    for i in kept:
+        kept_words.update(range(i["pc"], i["pc"] + len(i["enc"]) // 2, 4))
+    refill = []
+    restored_pcs = {r["pc"] for r in restored}
+    for lo, hi in spans:
+        at = hi
+        while at + 4 <= stop and at not in starts and at not in kept_words and at not in restored_pcs:
+            got = rdna2_decode.decode_bytes(blob[at:at + 20])["insns"][:1]
+            if not got or got[0]["pc"] != 0:
+                break
+            ins = got[0]
+            ins["pc"] = at
+            refill.append(ins)
+            at += len(ins["enc"]) // 2
+    return sorted(kept + restored + refill, key=lambda i: i["pc"])
 
 
 def output_signature(path, name):

Measured: two random samples of the programs this branch still refuses, 40 and 120 programs: 8/40 and 11/120 now translate, all passing spirv-val (19/160, about 12%). tests/semantic/test_vcc_tuple_fallback.py still passes.

What the rest is. Every remaining failure in both samples falls through right after an exp that llvm-mc (and rdna2_decode too) prints from a word like 0xf8000d60 (EXP, target 22, done compr, no channels) inside a compute program. Its second dword decodes cleanly on its own as s_mov_b32 exec_lo, s32, so I suspect a 32-bit console instruction in the EXP encoding space rather than a real 64-bit export, but I have not confirmed that; it is a separate question from this PR.

AI-assisted (Claude Code); measured on our copy, nothing of the game posted.

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Tested 063f14dcd with a newer llvm-mc than earlier runs here. Setup: Windows 11 Pro 26300, native llvm-mc 23.1.5, --wave64-emulate, on the retail pipeline library (2,684 programs, my own copy). Only counts are given here, no game data.

base without #327 with #327 SPIR-V bytes that differ
the PR's merge base de4c37383 1,773 1,773 0 of 1,773
current main 3aa975421 2,608 2,608 (merged clean) 0 of 2,608

With LLVM 23.1.5 listings the change has no effect on either base. The listings are identical and so is the output.

On 23.1.5, main still refuses 76 programs, and none of the refusals is a vcc tuple.

So whatever this PR still adds would show up only with older llvm-mc (21.x, as in the runs above). I have not rerun 21.x on current main. Someone with 21.1.8 could rebase onto main and recount, which would show what is left after #647. If nothing is left, a note in the docs that LLVM 23+ prints these tuples may be enough.

AI-assisted.

@aslan2142

Copy link
Copy Markdown
Collaborator

This carry-over of old upstream PR 10062 has been taken over in #596, so I'm closing this draft as #17 describes. The branch old-pr/10062 stays.

AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142 aslan2142 closed this Nov 24, 2026
QemmHD pushed a commit that referenced this pull request Nov 24, 2026
After undecodable words, translate() put each printed instruction back at its
blob offset by searching forward for its printed bytes. A data word llvm-mc
printed as a copy of a later instruction (printed bytes are not the bytes
read) took that later instruction's place, and every instruction after it
lost its place: "undecodable words (cannot re-anchor)".

shader_ir.parse_file now reads the byte offset of each rejected word from the
llvm-mc messages (shader_disasm.py feeds one line of "0xNN " bytes, five
characters a byte) as info["invalid_at"]. When there is one offset per
rejected word, the translator skips exactly those words and searches nothing;
a listed word whose bytes are the next instruction is not skipped, so a
listing with wrong or hand-written messages reads as before.

On the 1.0.2.1 pipeline library, with #327's vcc-tuple decoding, 36 compute
programs that stopped at "cannot re-anchor" now translate and pass
spirv-val; none that translated before is lost.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

7 participants