diff --git a/tests/semantic/test_vcc_tuple_fallback.py b/tests/semantic/test_vcc_tuple_fallback.py index 8df6e2a9cc..dd1949af64 100644 --- a/tests/semantic/test_vcc_tuple_fallback.py +++ b/tests/semantic/test_vcc_tuple_fallback.py @@ -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 @@ def test_nsa_mimg_with_vcc_tuple_does_not_read_past_code_end(self): 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_skipped_instructions must not swallow the branch target; reachable_code refuses it. + 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() diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 11bd9cee0d..938be35270 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -17550,36 +17550,69 @@ def restore_skipped_instructions(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. Recover VCC tuples, DPP comparisons and the measured signed-byte dot half swap. - Instructions printed inside the words of a restored one are dropped. The instructions already carry + 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; recover VCC tuples, DPP + comparisons and the measured signed-byte dot half swap only when no reachable branch targets an interior + dword. 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([ins for ins in insns if "op" in ins]) + 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 ( - (found[0]["op"].startswith("v_cmp_") and found[0]["op"].endswith("_f32_dpp")) - # tedin7 (upstream PR #281): LLVM skips this VOP3P form. Admit only opcode 0x16, - # op_sel=2, op_sel_hi=5, no clamp or neg, with all reserved encoding bits clear. - or (found[0]["op"] == "v_dot4_i32_i8" and - int.from_bytes(blob[at:at + 4], "little") >> 8 == 0xCC1650 and blob[at + 7] >> 3 == 1) - or 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 ( + (found[0]["op"].startswith("v_cmp_") and found[0]["op"].endswith("_f32_dpp")) + # tedin7 (upstream PR #281): LLVM skips this VOP3P form. Admit only opcode 0x16, + # op_sel=2, op_sel_hi=5, no clamp or neg, with all reserved encoding bits clear. + or (found[0]["op"] == "v_dot4_i32_i8" and + int.from_bytes(blob[at:at + 4], "little") >> 8 == 0xCC1650 and blob[at + 7] >> 3 == 1) + or 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 + restored.sort(key=lambda r: r["pc"]) # Both streams are in program order. A shader can contain thousands of recovered tuples. spans = iter((r["pc"], r["pc"] + len(r["enc"]) // 2) for r in restored) span = next(spans, None)