rdna2spirv: restrict restore_vcc_tuples to reachable offsets and protect 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 #596 / #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.
This commit is contained in:
JARVIX
2026-11-24 17:48:02 +00:00
parent f7a338b14d
commit 091cd9680b
2 changed files with 111 additions and 17 deletions

View File

@ -64,8 +64,11 @@ def listing_and_blob(program, variant="invalid"):
if text is not None:
lines.append(f"\t{text:<40}{encoding(raw)}")
continue
second = bytes.fromhex(raw)[4:8]
raw_bytes = bytes.fromhex(raw)
lines.append(INVALID)
if len(raw_bytes) == 4:
continue
second = raw_bytes[4:8]
if variant == "invalid":
lines.append(INVALID)
elif variant == "junk": # the second word decodes as an instruction of its own, bytes as stored
@ -152,6 +155,66 @@ class VccTupleFallbackTest(unittest.TestCase):
self.assertEqual(len(restored), 1)
self.assertEqual((restored[0]["pc"], len(restored[0]["enc"]) // 2), (0, 12))
def test_skipped_data_word_before_branch_target_is_not_restored(self):
# An unreachable data word skipped by s_branch whose bytes equal the first dword of a vcc-tuple load must
# not be restored across the branch target instruction that immediately follows it.
program = SET_SGPR_V + [
("s_branch 1", "01 00 82 bf"),
(None, "02 19 2c f4"),
("v_mov_b32_e32 v10, 7", "87 02 14 7e"),
("v_mov_b32_e32 v6, 0x3000", "ff 02 0c 7e 00 30 00 00"),
("global_store_dword v6, v10, s[0:1]", "00 80 70 dc 06 0a 00 00"),
("s_endpgm", "00 00 81 bf"),
]
mem = run(translate(program), {})
self.assertEqual(mem.get(OUT), 7)
def test_skipped_data_word_before_branch_target_vcc_load(self):
# Same skipped data word right before a real vcc-tuple load at the branch target: the data word is skipped
# and the real vcc-tuple load at the target is restored cleanly across all three listing variants.
program = SET_SGPR_V + [
("s_branch 1", "01 00 82 bf"),
(None, "02 19 2c f4"),
(None, LOAD_X8_VCC), WAIT,
("v_mov_b32_e32 v10, s100", "64 02 14 7e"), ("v_mov_b32_e32 v11, s105", "69 02 16 7e"),
("v_mov_b32_e32 v12, vcc_lo", "6a 02 18 7e"), ("v_mov_b32_e32 v13, vcc_hi", "6b 02 1a 7e"),
] + STORE_X4
words = [0x22220000 + i for i in range(8)]
for variant in PROGRAM_VARIANTS:
with self.subTest(printed_as=variant):
mem = run(translate(program, variant), {BASE + 4 * i: w for i, w in enumerate(words)})
self.assertEqual([mem.get(OUT + 4 * i) for i in range(4)], [words[0], words[5], words[6], words[7]])
def test_data_word_after_endpgm_before_branch_target_is_not_restored(self):
# An unreachable data word after s_endpgm right before a branch target is likewise ignored.
program = SET_SGPR_V + [
("s_cmp_eq_i32 s4, s4", "04 04 00 bf"),
("s_cbranch_scc1 2", "02 00 85 bf"),
("s_endpgm", "00 00 81 bf"),
(None, "02 19 2c f4"),
("v_mov_b32_e32 v10, 7", "87 02 14 7e"),
("v_mov_b32_e32 v6, 0x3000", "ff 02 0c 7e 00 30 00 00"),
("global_store_dword v6, v10, s[0:1]", "00 80 70 dc 06 0a 00 00"),
("s_endpgm", "00 00 81 bf"),
]
mem = run(translate(program), {})
self.assertEqual(mem.get(OUT), 7)
def test_fallthrough_into_data_word_before_branch_target_refused(self):
# When fall-through reaches a data word equal to the first dword of a vcc-tuple load right before a branch
# target, restore_vcc_tuples must not swallow the branch target; reachable_code refuses the fall-through.
program = SET_SGPR_V + [
("s_cmp_eq_i32 s4, s4", "04 04 00 bf"),
("s_cbranch_scc1 1", "01 00 85 bf"),
(None, "02 19 2c f4"),
("v_mov_b32_e32 v10, 7", "87 02 14 7e"),
("v_mov_b32_e32 v6, 0x3000", "ff 02 0c 7e 00 30 00 00"),
("global_store_dword v6, v10, s[0:1]", "00 80 70 dc 06 0a 00 00"),
("s_endpgm", "00 00 81 bf"),
]
with self.assertRaisesRegex(rdna2spirv.Unsupported, "fall-through into undecodable words"):
translate(program)
if __name__ == "__main__":
unittest.main()

View File

@ -17153,29 +17153,60 @@ def restore_vcc_tuples(insns, blob, code_start, code_end):
s[100:107], a buffer resource in s[104:107]): it skips the first word as invalid and then prints the second word
as an instruction of its own, one that is junk or does not match the blob (docs/shader_library.md section 5).
The pure-Python decoder reads such a word as a VCCTuple list, [s100,...,s105,vcc_lo,vcc_hi], the form the
translator takes for these instructions. Every blob dword in the code range that no matched instruction covers
is offered to it; the instruction is used only if its operands hold such a list, so no other undecodable word
changes. Instructions printed inside the words of a restored one are dropped. The instructions already carry
their blob offsets in "pc"; returns the list sorted by it."""
translator takes for these instructions. Only uncovered offsets reached from code_start by fall-through or
branches (including folded far branches) are offered to it, so unreachable embedded data words skipped by a
branch or after s_endpgm are never decoded across a following branch target; the instruction is used only if
its operands hold such a list and no reachable branch targets its interior dwords. Instructions printed inside
the words of a restored one are dropped. The instructions already carry their blob offsets in "pc"; returns
the list sorted by it."""
import rdna2_decode
covered = set()
for ins in insns:
if not ins.get("normalized"):
covered.update(range(ins["pc"], ins["pc"] + len(ins["enc"]) // 2, 4))
folded = fold_far_branches(insns)
by_pc = {ins["pc"]: ins for ins in folded
if all(o in covered for o in range(ins["pc"], ins["pc"] + len(ins["enc"]) // 2, 4))}
stop = min(len(blob), code_end if code_end is not None else len(blob))
restored, at = [], code_start
while at + 8 <= stop:
found = None if at in covered else rdna2_decode.decode_bytes(blob[at:min(at + 20, stop)])["insns"][:1]
if found and found[0]["pc"] == 0 and any(
a["k"] == "slist" and ("vcc_lo" in a["r"] or "vcc_hi" in a["r"]) for a in found[0]["args"]):
ins = found[0]
nbytes = len(ins["enc"]) // 2
if at + nbytes <= stop:
ins["pc"] = at
restored.append(ins)
at += nbytes
restored, restored_covered, reachable_targets = [], set(), set()
seen, work = set(), [code_start]
while work:
at = work.pop()
while code_start <= at < stop and at not in seen:
seen.add(at)
ins = by_pc.get(at)
if ins is not None:
op = base_op(ins["op"])
nxt = at + len(ins["enc"]) // 2
if op in BRANCHES:
imm = next((x for x in ins["args"] if x["k"] == "imm"), None)
v = (imm["v"] & 0xFFFF) if imm else 0
tgt = ins["target"] if "target" in ins else nxt + 4 * (v - 0x10000 if v & 0x8000 else v)
reachable_targets.add(tgt)
work.append(tgt)
if op == "s_branch":
break
if op in ("s_endpgm", "s_setpc_b64"):
break
at = nxt
continue
at += 4
if at in covered or at in restored_covered or at + 8 > stop:
break
found = rdna2_decode.decode_bytes(blob[at:min(at + 20, stop)])["insns"][:1]
if found and found[0]["pc"] == 0 and any(
a["k"] == "slist" and ("vcc_lo" in a["r"] or "vcc_hi" in a["r"]) for a in found[0]["args"]):
r = found[0]
nbytes = len(r["enc"]) // 2
if (at + nbytes <= stop
and not any(o in restored_covered for o in range(at, at + nbytes, 4))):
r["pc"] = at
restored.append(r)
restored_covered.update(range(at, at + nbytes, 4))
at += nbytes
continue
break
restored = [r for r in restored
if not any(o in reachable_targets for o in range(r["pc"] + 4, r["pc"] + len(r["enc"]) // 2, 4))]
if not restored:
return insns
spans = [(r["pc"], r["pc"] + len(r["enc"]) // 2) for r in restored]