"""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 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 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("