From 063f14dcd944170efb6cf907cdb9cd5ca57d8da9 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 00:35:11 +0100 Subject: [PATCH] fix: rdna2spirv decodes scalar, image and buffer tuples that run into 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. --- docs/shader_library.md | 8 +- tests/semantic/test_vcc_tuple_fallback.py | 149 ++++++++++++++++++++++ tools/rdna2spirv.py | 36 ++++++ 3 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 tests/semantic/test_vcc_tuple_fallback.py diff --git a/docs/shader_library.md b/docs/shader_library.md index 891a740c10..193e4714c9 100644 --- a/docs/shader_library.md +++ b/docs/shader_library.md @@ -150,9 +150,11 @@ What stops the rest, by the translator's message: run into `vcc` (SGPRs 106 and 107): `s_load_dwordx8`/`x4`/`x16` writing `s[100:107]`, `s[104:107]` or `s[92:107]`, and image instructions whose resource is `s[100:107]`. In the compute programs alone that is 37,952 SMEM, 22,248 MIMG and 2,298 MUBUF words. The same words with the register field one tuple lower decode; with - `gfx1030` they don't either. The FSSM shaders don't use these tuples; the pipeline builds use them often. A decoder - fallback for these encodings would cover most of the remaining programs. The translator already reads - `[s100,...,vcc_lo,vcc_hi]` lists (design.md *Scalar/buffer memory*). + `gfx1030` they don't either. The FSSM shaders don't use these tuples; the pipeline builds use them often. The translator + now decodes these words from the blob with `rdna2_decode` (`restore_vcc_tuples`) and reads them as the + `[s100,...,vcc_lo,vcc_hi]` lists it already takes (design.md *Scalar/buffer memory*); synthetic cases are in + `tests/semantic/test_vcc_tuple_fallback.py`. The count above was measured before this change; it has not been + re-run on the real library. * **443: an instruction llvm-mc prints with another encoding than the blob's** (`v_fma_mix*`, `v_interp_p2_f16`): the existing `check_printed_encodings` refusal. * **90 CS: "CS thread-group-size SGPR"**: RSRC2 `TG_SIZE_EN`, which no FSSM compute shader sets. diff --git a/tests/semantic/test_vcc_tuple_fallback.py b/tests/semantic/test_vcc_tuple_fallback.py new file mode 100644 index 0000000000..dd02b4509a --- /dev/null +++ b/tests/semantic/test_vcc_tuple_fallback.py @@ -0,0 +1,149 @@ +"""A scalar tuple that runs into vcc is translated from the blob when llvm-mc cannot decode it. + +llvm-mc rejects s_buffer_load_dwordx8 into s[100:107], a buffer resource in s[104:107] and an image resource in +s[100:107] (SGPRs 106 and 107 are vcc). It skips the first word as invalid and leaves the second word as junk, so +translate() refused every such shader ("reachable instruction with normalized encoding", or "fall-through into +undecodable words"; docs/shader_library.md section 5), or, when the junk matched the blob, translated a program +with the load missing. The words are now decoded from the blob as the VCCTuple lists the translator takes. + +Hand-built listings and blobs of synthetic instructions (encodings from llvm-mc 19, the three tuple words patched in +their register field), no game data. + + python -I tests/semantic/test_vcc_tuple_fallback.py -v +""" +import os +import struct +import sys +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +if HERE not in sys.path: + sys.path.insert(0, HERE) + +from support import rdna2spirv, translate_listing # noqa: E402 +import fssm_header # noqa: E402 +import spirv_mini_interp as si # noqa: E402 + +NAME = "cs_vcc_tuple" +CODE_START = 0x148 +BASE, OUT = 0x1000, 0x3000 +INVALID = "; warning: invalid instruction encoding" + +SET_SGPR_V = [ # s4..s7 = a V# over BASE (32 bytes), as tests/semantic/test_fspx_rules.py builds it + ("s_mov_b32 s4, 0x1000", "ff 03 84 be 00 10 00 00"), + ("s_mov_b32 s5, 0", "80 03 85 be"), + ("s_mov_b32 s6, 32", "a0 03 86 be"), + ("s_mov_b32 s7, 0x30000000", "ff 03 87 be 00 00 00 30"), +] +WAIT = ("s_waitcnt lgkmcnt(0)", "7f c0 8c bf") +WAIT_V = ("s_waitcnt vmcnt(0)", "70 3f 8c bf") +STORE_X4 = [ + ("v_mov_b32_e32 v6, 0x3000", "ff 02 0c 7e 00 30 00 00"), + ("global_store_dwordx4 v6, v[10:13], s[0:1]", "00 80 78 dc 06 0a 00 00"), + ("s_endpgm", "00 00 81 bf"), +] +# the words llvm-mc cannot decode (each is two dwords): the llvm-mc 19 encoding of the same instruction one tuple +# lower, with its register field patched into vcc +LOAD_X8_VCC = "02 19 2c f4 00 00 00 fa" # s_buffer_load_dwordx8 [s100..s105,vcc_lo,vcc_hi], s[4:7], 0x0 +LOAD_X4_VCC = "02 1a 28 f4 00 00 00 fa" # s_buffer_load_dwordx4 [s104,s105,vcc_lo,vcc_hi], s[4:7], 0x0 +LOAD_DW_FROM_VCC = "b4 02 20 f4 00 00 00 fa" # s_buffer_load_dword s10, [s104,s105,vcc_lo,vcc_hi], 0x0 +BUFFER_LOAD_VCC = "00 10 30 e0 01 0b 1a 80" # buffer_load_dword v11, v1, [s104,s105,vcc_lo,vcc_hi], 0 offen +IMAGE_LOAD_VCC = "08 1f 00 f0 01 04 19 00" # image_load v[4:7], v[1:2], [s100..s105,vcc_lo,vcc_hi] 2D unorm +PROGRAM_VARIANTS = ("invalid", "junk", "normalized") + + +def encoding(raw): + return "; encoding: [" + ",".join(f"0x{b:02x}" for b in bytes.fromhex(raw)) + "]" + + +def listing_and_blob(program, variant="invalid"): + """program: (text, hex) pairs; a pair whose text is None is a word llvm-mc rejects, listed as the variant says.""" + code = b"".join(bytes.fromhex(raw) for _, raw in program) + lines = [] + for text, raw in program: + if text is not None: + lines.append(f"\t{text:<40}{encoding(raw)}") + continue + second = bytes.fromhex(raw)[4:8] + lines.append(INVALID) + if variant == "invalid": + lines.append(INVALID) + elif variant == "junk": # the second word decodes as an instruction of its own, bytes as stored + lines.append(f"\ts_nop 0{'':<33}" + encoding(second.hex())) + else: # printed with another encoding than the blob holds + lines.append("\texp mrt0 v0, v0, v0, v0 done vm " + encoding("0f 18 00 f8 00 00 00 00")) + head = f"; {NAME} code 0x{CODE_START:x}..0x{CODE_START + len(code):x} (scan), gfx1013, 2 invalid words" + header = bytearray(CODE_START) + struct.pack_into("<4I", header, 0x138, 0x88, 32, 1, 1) + struct.pack_into(" lo for lo, hi in spans)] + return sorted(kept + restored, key=lambda i: i["pc"]) + + def output_signature(path, name): """OSG1 elements as (register, system value), one per register (first element wins).""" d = shader_container(path, name) @@ -17258,6 +17293,7 @@ def translate(path): ins["normalized"] = True ins["pc"] = at pos = at + len(enc) + insns = restore_vcc_tuples(insns, blob, info["code_start"], info.get("code_end")) insns = fold_far_branches(insns) insns = reachable_code(insns, info.get("code_end")) if any(i.get("normalized") for i in insns):