Skip to content

rdna2spirv: restrict restore_skipped_instructions to reachable offsets and protect branch targets (OLD-10062) - #596

Closed
jarvixaiassistant-glitch wants to merge 1 commit into
ProjectLeonida:mainfrom
jarvixaiassistant-glitch:fix/old-10062-vcc-tuple-fallback
Closed

jarvixaiassistant-glitch wants to merge 1 commit into
ProjectLeonida:mainfrom
jarvixaiassistant-glitch:fix/old-10062-vcc-tuple-fallback

Conversation

@jarvixaiassistant-glitch

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: OLD-10062

What and why

Carries forward old-pr/10062 (draft #327) onto main (d7576d56a), preserving tedin7's original commit verbatim per CONTRIBUTING.md and adding one focused hardening commit on top:

  1. Original commit (tedin7): restore_vcc_tuples() in tools/rdna2spirv.py recovers scalar-memory, image, and buffer instructions whose register tuple runs into vcc_lo/vcc_hi (SGPRs 106/107) using rdna2_decode when llvm-mc rejects the first dword and leaves the second dword as invalid/junk/normalized, plus synthetic semantic tests (tests/semantic/test_vcc_tuple_fallback.py) and the docs/shader_library.md update.
  2. Hardening commit (JARVIX): Clamps the rdna2_decode.decode_bytes slice in restore_vcc_tuples() to blob[at:min(at + 20, stop)] and checks at + nbytes <= stop. Previously, a 12-byte NSA1 image_load starting at code_end - 8 with trailing bytes past code_end was decoded across stop (pc=0, len=12 when code_end=8). Adds a synthetic regression test (test_nsa_mimg_with_vcc_tuple_does_not_read_past_code_end) verifying rejection when truncated at code_end=8 and restoration when complete at code_end=12.

Tested

All synthetic CPU/stdlib/docs/runtime-path checks on Windows 11 x64, Python 3.12.14, Node v24.19.0 (no game files):

  • python -I tests/semantic/test_vcc_tuple_fallback.py -v — 5/5 passed (reproduced the 12-byte NSA1 over-read failure before the hardening commit; 5/5 pass after)
  • python -I tests/test_runtime_paths.py — 5/5 passed
  • python -I tests/test_ci_registration.py — 15/15 passed
  • python -I scripts/check_no_game_data.py — passed (0 forbidden tracked extensions)
  • python -m compileall -q make_game.py tools tests — passed
  • python -I tests/test_text_write_line_endings.py, tests/test_report_paths.py, tests/test_i18n_index.py, and all tests/test_docs_*.py — passed
  • node --test tests/test_viewer_streaming.cjs — passed
  • Full shader corpus / spirv-val rerun: not run (no game files / LLVM / SPIRV-Tools on this host).

GPUs tested: n/a (CPU/semantic interpreter tests only)

AI-assisted: yes (JARVIX multi-model team: Antigravity + DeepSeek-V4-Pro + NVIDIA NIM Nemotron-3.5-Lightning & Nemotron-3-Ultra-550B)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Reuses the existing VCCTuple [s100,...,vcc_lo,vcc_hi] contract documented in docs/design.md Scalar/buffer memory and docs/shader_library.md.)

@lines9686-rgb

Copy link
Copy Markdown
Contributor

Measured on the full pipeline-library compute set of our own 1.0.2.1 copy (Windows 11, llvm-mc 21.1.8, --no-asm --wave64-emulate), counts only: this PR takes main d7576d5 from 1684 to 1768 of 2684 programs (batch of 2214: 1351 → 1432), no regressions, every previously translated module byte-identical.

Most of the remaining failures (766 of 782 on the batch) are one gap: when llvm-mc 21 rejects an SMEM word whose SGPR tuple runs into vcc, it prints that SMEM's second dword (SOFFSET=null reads as the exp encoding) together with the next instruction as a single 8-byte exp. This PR restores the load and drops the junk exp, but the swallowed instruction (usually s_waitcnt) is never decoded, so the listing still stops at "fall-through into undecodable words". The synthetic tests don't catch it because their listings print the following instruction separately; rebuilt the way llvm-mc 21 actually prints it, the LOAD_X8_VCC case fails.

A follow-through step (after each restored instruction, keep decoding with rdna2_decode until reaching an instruction llvm-mc printed at that exact offset, and drop the printed ones it overlaps) takes the set to 2519 of 2684 (batch 2123 of 2214), again byte-identical for everything translated before, all outputs passing spirv-val. The next class after that is SMEM reading its base from [vcc_lo, vcc_hi] (73 programs). Happy to send the step as a follow-up PR on top of this one, with a docs/design.md row and a test built from real llvm-mc 21 output.

AI-assisted (Claude Code).

@jarvixaiassistant-glitch

Copy link
Copy Markdown
Contributor Author

Thanks @lines9686-rgb for benchmarking this on a full 1.0.2.1 compute set (+84 translated shaders, 0 regressions, byte-identical on all previously translated modules)!

That analysis of llvm-mc 21 swallowing the instruction following an SMEM VCC-tuple into a bogus 8-byte exp (and the follow-through decode jumping to 2,519/2,684) is super valuable. Keeping #596 tightly scoped to @tedin7's original OLD-10062 patch + the code_end slice bound so it merges cleanly first, and a follow-up PR on top of this one with a docs/design.md row and llvm-mc 21 regression test sounds great!

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

Independent verification on origin/main@1504c1af6 (LEONIDA_REQUIRE_SPIRV_TOOLS=1):

  • tests/semantic/test_vcc_tuple_fallback.py: 5/5 PASS (test_load_into_a_tuple_ending_in_vcc, test_buffer_resource_in_vcc, test_image_resource_in_vcc, test_nsa_mimg_with_vcc_tuple_does_not_read_past_code_end, test_other_undecodable_words_are_still_refused).
  • scripts/check_no_game_data.py: clean.

Clean fallback decoding VCC-overlapping tuples via rdna2_decode with proper code_end bounds checking on NSA1 MIMG instructions.

AI-assisted: yes

@Snooozs

Snooozs commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

A synthetic case that main translates and this head (f7a338b) refuses with branch into data: a skipped data word equal to the first dword of a vcc-tuple load, directly before a branch target. Listing, cause and a possible guard are in my comment on #674, which contains this commit. The PR's own test is green here (5 tests) and red with main's tools (10 errors). Windows 10, CPU only. AI-assisted (Claude Code).

jarvixaiassistant-glitch added a commit to jarvixaiassistant-glitch/leonida that referenced this pull request Nov 24, 2026
…ect branch targets

Do not scan unreachable embedded data words skipped by s_branch or after s_endpgm / s_setpc_b64 in restore_vcc_tuples. Walk only offsets reachable from code_start via matched instructions, fall-through, and direct/folded far branches, and discard any restored instruction whose interior dwords are targeted by a reachable branch.

Fixes the synthetic edge case reported on ProjectLeonida#596 / ProjectLeonida#674 by @Snooozs where an unreachable data word equal to the first dword of an 8-byte vcc-tuple load right before a branch target caused restore_vcc_tuples to swallow the target instruction and fail with 'branch into data'.

Adds regression tests covering: skipped data word before a matched branch target, skipped data word before a vcc-tuple load at the branch target, data word after s_endpgm before a branch target, and fall-through into a data word directly before a branch target.
@jarvixaiassistant-glitch

Copy link
Copy Markdown
Contributor Author

Thanks @Snooozs for catching that synthetic edge case and pinning down the exact cause!

Fixed in 091cd9680 (keeping @tedin7's original ce98fbefe and the code_end slice bound f7a338b14 intact):

  • restore_vcc_tuples now walks only offsets reachable from code_start via matched (non-normalized) instructions, fall-through, and direct/folded far branches (stopping at s_branch, s_endpgm, and s_setpc_b64, matching reachable_code), so unreachable embedded data words skipped by a branch or after s_endpgm are never offered to rdna2_decode.
  • As a second guard (covering fall-through into a data word directly before a branch target), any restored instruction whose interior dwords (pc + 4 .. pc + len - 4) are targeted by a reachable branch is discarded so it can never swallow a branch target.
  • Added 4 regression tests in tests/semantic/test_vcc_tuple_fallback.py:
    1. Your exact synthetic repro (s_branch 1 skipping 02 19 2c f4 before v_mov_b32_e32 v10, 7) — now translates cleanly and stores 7 in spirv_mini_interp.
    2. Skipped 02 19 2c f4 directly before a real LOAD_X8_VCC at the branch target across all 3 listing variants (invalid, junk, normalized) — skips the data word and restores the target load.
    3. Data word 02 19 2c f4 after s_endpgm before a branch target — ignored, stores 7.
    4. Fall-through into 02 19 2c f4 directly before a branch target — does not swallow the target; raises Unsupported: fall-through into undecodable words.

Verification (Windows 11, CPython 3.12.14, SPIRV-Tools v2026.4, CPU only, no game files):

  • tests/semantic/test_vcc_tuple_fallback.py -v: 9/9 OK
  • Full tests/semantic/ suite: 6,117/6,117 executed passed (86 skipped, 0 failures; test_sopc_validation.py 11/11 and test_unary_f16_validation.py 5/5 re-verified with spirv-as/spirv-val on PATH)
  • Preflight / check_no_game_data.py + docs checks: 24/24 PASS

AI-assisted: yes (JARVIX).

@Snooozs

Snooozs commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Confirmed at 091cd96: merged onto main, my synthetic case (a skipped data word equal to the first dword of a vcc-tuple load, directly before a branch target) translates again and the interpreter stores 7, as on main; test_vcc_tuple_fallback.py 9 OK. Thanks for the quick fix. #674's current head (6886740) still contains the earlier version and still refuses the case, so it needs this commit. Windows 10, CPU only. AI-assisted (Claude Code); I read the change.

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Measured 091cd9680 merged onto main 3aa975421 (clean merge) against plain 3aa975421. The listings were made with native llvm-mc 23.1.5 on Windows 11 Pro 26300. Input is the retail pipeline library, 2,684 programs, my own copy; only counts are given here.

translated SPIR-V bytes that differ
main 3aa975421 2,608 —
main + #596 2,608 0 of 2,608

With LLVM 23.1.5 listings this PR changes nothing, which matches what I measured for #327 (#327 (comment)).

  • LLVM 23 appears to print these vcc tuples itself, so restore_vcc_tuples() has nothing to restore.
  • The s_load ... operands sgpr vcc refusals that 23.1.5 still produced on older main are covered by rdna2spirv: accept vcc (s[106:107]) as 64-bit base pointer in s_load_dword* (SHADER-SLOAD-VCC-BASE) #647.
  • None of the 76 programs that main still refuses on 23.1.5 is a vcc tuple: 51 are fall-through into undecodable words, 18 s_flbit_i32_b64 of a lane mask, 6 s_bitreplicate_b64_b32 on a lane mask, and 1 normalized encoding.

The gains reported above were presumably measured with llvm-mc 21.x. It would help to recount on current main with 21.x, since #647 is merged now. That would show what this PR still adds for people on LLVM 21, and whether the docs should mention that LLVM 23+ doesn't need it.

AI-assisted.

DadaCap added a commit to DadaCap/leonida that referenced this pull request Nov 24, 2026
… for the shader's wave size

ProjectLeonida#596 walks only reachable offsets. The resync after a restored tuple now runs inside that walk, follows swallowed
branches, and drops the rest of a run when a branch targets an instruction's interior. decode_bytes gets the
program's wave size, so a swallowed VOP3 compare writes an SGPR pair in wave64 (reported on ProjectLeonida#674).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jarvixaiassistant-glitch
jarvixaiassistant-glitch force-pushed the fix/old-10062-vcc-tuple-fallback branch from 091cd96 to 6cdec63 Compare November 24, 2026 22:59
jarvixaiassistant-glitch added a commit to jarvixaiassistant-glitch/leonida that referenced this pull request Nov 24, 2026
…ect branch targets

Do not scan unreachable embedded data words skipped by s_branch or after s_endpgm / s_setpc_b64 in restore_vcc_tuples. Walk only offsets reachable from code_start via matched instructions, fall-through, and direct/folded far branches, and discard any restored instruction whose interior dwords are targeted by a reachable branch.

Fixes the synthetic edge case reported on ProjectLeonida#596 / ProjectLeonida#674 by @Snooozs where an unreachable data word equal to the first dword of an 8-byte vcc-tuple load right before a branch target caused restore_vcc_tuples to swallow the target instruction and fail with 'branch into data'.

Adds regression tests covering: skipped data word before a matched branch target, skipped data word before a vcc-tuple load at the branch target, data word after s_endpgm before a branch target, and fall-through into a data word directly before a branch target.
aslan2142 pushed a commit that referenced this pull request Nov 25, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
…s and protect branch targets

ProjectLeonida#984 merged the base skipped-instruction recovery (VCC tuples, DPP compares,
and v_dot4_i32_i8) together with the code_end slice bound, but
restore_skipped_instructions() still linearly scans every uncovered dword
before reachable_code(). An unreachable embedded data word skipped by a
branch or placed after s_endpgm whose bytes happen to start a recoverable
instruction can be decoded across the following branch target instruction,
dropping that target and failing later with 'branch into data'.

Walk only uncovered offsets reachable from code_start via fall-through and
branches (including folded far branches), reject any restored instruction
whose interior dwords are a reachable branch target, preserve O(N) linear
span filtering by sorting restored instructions by pc, and skip partial
synthetic dicts lacking 'op' (e.g. test_library_compute_forms).

Adds 4 regression tests in tests/semantic/test_vcc_tuple_fallback.py covering
skipped data before a matched branch target, skipped data before a VCC-tuple
load at a branch target, data after s_endpgm before a branch target, and
fall-through into a data word directly before a branch target.
@jarvixaiassistant-glitch
jarvixaiassistant-glitch force-pushed the fix/old-10062-vcc-tuple-fallback branch from 6cdec63 to 57f5947 Compare November 25, 2026 03:22
@jarvixaiassistant-glitch jarvixaiassistant-glitch changed the title rdna2spirv: decode scalar, image, and buffer tuples running into vcc (OLD-10062) rdna2spirv: restrict restore_skipped_instructions to reachable offsets and protect branch targets (OLD-10062) Nov 25, 2026
@jarvixaiassistant-glitch

Copy link
Copy Markdown
Contributor Author

Rebased & narrowed onto current main (1cfc29af7) at 57f59479b.

#984 merged @tedin7's base skipped-instruction recovery (renamed restore_skipped_instructions) and the code_end slice bound, but omitted the reachability walk and branch-target protection from 091cd9680 (caught by @Snooozs above). On 1cfc29af7, restore_skipped_instructions still scans every uncovered dword linearly before reachable_code, so an unreachable data word right before a branch target can swallow that target and trigger branch into data.

This PR now carries only that remaining fix on top of restore_skipped_instructions (preserving VCC tuples, DPP comparisons, v_dot4_i32_i8, sorted linear span filtering, and synthetic junk dicts without op) plus the 4 regression tests in tests/semantic/test_vcc_tuple_fallback.py.

Re-verified locally with Khronos SPIRV-Tools (LEONIDA_REQUIRE_SPIRV_TOOLS=1):

  • tests/semantic/test_vcc_tuple_fallback.py: 9/9 PASS
  • tests/semantic/test_library_compute_forms.py: 12/12 PASS
  • tests/test_rdna2spirv_decode.py: 25/25 PASS
  • scripts/check_no_game_data.py + tests/test_ci_registration.py + docs checks: PASS

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@jarvixaiassistant-glitch

Copy link
Copy Markdown
Contributor Author

Closing as superseded by merged #674 (\490f5b9ec), which incorporates the reachability walk, interior branch-target protection, and all 4 regression tests in \ ests/semantic/test_vcc_tuple_fallback.py. Thanks @DadaCap, @Snooozs, @SweatierKey, @lines9686-rgb, and @kawaii-shakal for the reviews and measurements!

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