"""Shared fixtures for the translator tests. Nothing here touches game data: shaders are tiny hand-written assembly programs, assembled with llvm-mc into the same ``; encoding:`` listing format that tools/shader_disasm.py produces, next to a synthetic empty DXBC container (rdna2spirv.translate() looks for ``/../.dxbc`` to read the input signature). """ import glob import os import re import shutil import struct import subprocess import sys import pytest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) sys.path.insert(0, os.path.join(ROOT, "tests", "semantic")) # Before rdna2spirv is imported: support.py installs its piecewise finder first # where the tokenizer needs it (Python 3.12 to 3.14.7), and the finder can only # take the import if it is in sys.meta_path when the module is first asked for. # Without this, pytest's conftest imports it through PathFinder and # test_support_installs_the_finder_only_where_the_tokenizer_is_slow sees the # usual loader; run as `python tests/test_piecewise_parse.py` there is no # conftest, which is why the same test passed one way and not the other. import support # noqa: E402, F401 import mc_command # noqa: E402 import rdna2spirv # noqa: E402 def _repair_stdin_handle(): """Windows, under pytest's fd capture: give the children the tests start a stdin that is open. The capture replaces fd 0 with os.dup2(), which closes the handle fd 0 had, while the process's STD_INPUT_HANDLE keeps that handle's number. subprocess starts from GetStdHandle(), not from fd 0, so a child started without stdin=/input= fails with "[WinError 6] The handle is invalid" or, when the number has been reused meanwhile, gets whatever file now has it. Which one depends on what ran before, so the failures move from run to run. With -s or --capture=sys, and without pytest, the two handles agree and nothing is done. """ if sys.platform != "win32": return import ctypes import msvcrt kernel32 = ctypes.WinDLL("kernel32") kernel32.GetStdHandle.restype = ctypes.c_void_p kernel32.GetStdHandle.argtypes = [ctypes.c_uint32] kernel32.SetStdHandle.argtypes = [ctypes.c_uint32, ctypes.c_void_p] std_input = 0xFFFFFFF6 # STD_INPUT_HANDLE, (DWORD)-10 handle = kernel32.GetStdHandle(std_input) try: fd0 = msvcrt.get_osfhandle(0) except OSError: fd0 = None if not handle or handle == fd0: # no stdin at all (subprocess copes), or not redirected return # A NUL of our own, never closed: fd 0's handle would go stale again the next time the capture swaps fd 0. kernel32.SetStdHandle(std_input, msvcrt.get_osfhandle(os.open(os.devnull, os.O_RDONLY))) _repair_stdin_handle() # before the llvm-mc probe below, which is the first child def _has_amdgpu(mc): """True if this llvm-mc was built with the AMDGPU target (the official Windows release archives are not).""" try: r = subprocess.run(mc + ["--version"], capture_output=True, text=True, timeout=60) except (OSError, subprocess.SubprocessError): return False return "amdgcn" in r.stdout def _find_llvm_mc(): candidates = [] env = os.environ.get("LLVM_MC") if env: candidates.append(mc_command.split_command(env)) found = shutil.which("llvm-mc") if found: candidates.append([found]) # Debian/Ubuntu install it versioned, out of PATH. candidates += [[p] for p in sorted(glob.glob("/usr/lib/llvm-*/bin/llvm-mc"), reverse=True)] # Homebrew's llvm and llvm@N are keg-only, never linked onto PATH. for prefix in ("/opt/homebrew/opt", "/usr/local/opt"): candidates += [[p] for p in sorted(glob.glob(prefix + "/llvm*/bin/llvm-mc"), reverse=True)] # Default location of the official Windows installer, which doesn't add LLVM to PATH unless asked to. candidates += [[p] for p in glob.glob(r"C:\Program Files\LLVM\bin\llvm-mc.exe")] for c in candidates: if c and _has_amdgpu(c): return c return None LLVM_MC = _find_llvm_mc() HAVE_SPIRV = shutil.which("spirv-as") and shutil.which("spirv-val") requires_tools = pytest.mark.skipif( not (LLVM_MC and HAVE_SPIRV), reason=("needs an llvm-mc built with the amdgcn target, and spirv-as/spirv-val on PATH; " f"LLVM_MC={os.environ.get('LLVM_MC')!r}, found llvm-mc: {LLVM_MC!r}, " f"spirv-as/val: {bool(HAVE_SPIRV)}"), ) # 32-byte DXBC header: magic, digest, version, size, chunk count = 0. Gives an empty input signature. EMPTY_DXBC = b"DXBC" + bytes(16) + struct.pack(" 3 else 0 # stream, name offset, semantic index, system value, component type (3 = float), register, masks, padding out += struct.pack("<6I", 0, 0, sem, sysv, 3, reg) + bytes([mask, mask, 0, 0]) + struct.pack("/../fssm/.fssm): word 0x138 is COMPUTE_PGM_RSRC2, words 0x13C/0x140/0x144 the thread-group size. # 0x8088 = 4 user SGPRs, TGID_X, 512 B of LDS; group 64x1x1 (the header the cs_* samples in runtime/test/shaders use). DEFAULT_CS_HEADER = (0x8088, (64, 1, 1)) # The comment line a sample carries when llvm-mc cannot assemble it (cs_dpp_cmp.asm: a VOPC compare in DPP form). # scripts/run_gpu_tests.py has the same line; tests/test_translate.py checks that the two agree. TEXT_ONLY_MARK = "; run_gpu_tests: text only, llvm-mc has no gfx1013 encoding for an instruction here" class Shader: def __init__(self, workdir, name, asm, cs_header=DEFAULT_CS_HEADER, dxbc=EMPTY_DXBC): self.name = name disasm = workdir / "disasm" disasm.mkdir(exist_ok=True) (workdir / f"{name}.dxbc").write_bytes(dxbc) if name.startswith("cs_"): rsrc2, (lx, ly, lz) = cs_header (workdir / "fssm").mkdir(exist_ok=True) (workdir / "fssm" / f"{name}.fssm").write_bytes(bytes(0x138) + struct.pack("<4I", rsrc2, lx, ly, lz)) self.path = disasm / f"{name}.s" self.workdir = workdir if any(ln.strip() == TEXT_ONLY_MARK for ln in asm.splitlines()): # llvm-mc has no gfx1013 encoding for an instruction of this sample, so its text is the listing, as in # assemble_to_s of scripts/run_gpu_tests.py: the translator reads straight-line code from the text alone. lines = [f"; {name} code 0x0..0x0 (synthetic), gfx1013, 0 invalid words"] lines += ["\t" + ln.strip() for ln in asm.splitlines() if ln.strip() and not ln.lstrip().startswith(";")] self.path.write_text("\n".join(lines) + "\n", encoding="utf-8", newline="\n") return cmd = LLVM_MC + ["-triple=amdgcn--amdpal", "-mcpu=gfx1013"] for ln in asm.splitlines(): s = ln.strip() if not s: continue if not s.startswith(";"): break m = re.match(r";\s*mattr:\s*(\S+)", s) if m: cmd.append(f"-mattr={m.group(1)}") break cmd.append("-show-encoding") r = subprocess.run(cmd, input=asm, capture_output=True, text=True) assert r.returncode == 0, r.stderr self.path.write_text(f"; {name} synthetic\n{r.stdout}") def translate(self): return rdna2spirv.translate(str(self.path)) def validate(self, spvasm, known_val_issue=None): """Assemble and run spirv-val. known_val_issue: a substring of a spirv-val error that is a validator problem, not a translator one; the test is then reported as xfail with that text, so it turns into a plain pass once a newer spirv-tools no longer reports it. (spirv-val stops at its first error: later ones stay hidden.)""" sa = self.workdir / f"{self.name}.spvasm" sv = self.workdir / f"{self.name}.spv" sa.write_text(spvasm) r = subprocess.run(["spirv-as", str(sa), "-o", str(sv), "--target-env", "vulkan1.2"], capture_output=True, text=True) assert r.returncode == 0, "spirv-as: " + r.stderr val = ["spirv-val", "--target-env", "vulkan1.2"] r = subprocess.run(val + [str(sv)], capture_output=True, text=True) if r.returncode != 0 and "Offset can only be used with OpImage*Gather" in r.stderr: # image_sample*_o (sample ps_texoff.asm): a non-constant Offset outside gather is VK_KHR_maintenance8, and # rdna2spirv.py validates with this flag (docs/design.md, "Texel offsets"). A spirv-val from before # maintenance8 prints its usage text for the flag it doesn't know: nothing can be validated there. r = subprocess.run(val + ["--allow-offset-texture-operand", str(sv)], capture_output=True, text=True) if r.returncode != 0 and "USAGE" in r.stdout + r.stderr: pytest.skip("this spirv-val has no --allow-offset-texture-operand (VK_KHR_maintenance8)") if r.returncode != 0 and known_val_issue and known_val_issue in r.stderr: pytest.xfail("known spirv-val issue (" + known_val_issue + "): " + r.stderr.strip().splitlines()[0]) assert r.returncode == 0, "spirv-val: " + r.stderr @pytest.fixture def make_shader(tmp_path): """make_shader(asm, name="ps_test") -> Shader. The name's prefix ("ps_", "vs_", "cs_") selects the stage. For cs_ shaders, cs_header=(RSRC2, (x, y, z)) sets the synthetic FSSM header; dxbc= replaces the empty DXBC container (see make_dxbc).""" def make(asm, name="ps_test", **kw): return Shader(tmp_path, name, asm, **kw) return make