"""op_reach: shaders and instructions per base instruction name, on hand-written listings. python -I tests/test_op_reach.py Every input is built here (five tiny .s listings, an index.json and FSSM headers with only the magic and the wave flag). No game data. """ import io import json import os import posixpath import struct import subprocess import sys import tempfile import unittest from contextlib import redirect_stderr, redirect_stdout from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "op_reach.py") sys.path.insert(0, os.path.join(ROOT, "tools")) import op_reach # noqa: E402 import replace_retry # noqa: E402 import report_paths # noqa: E402 LISTINGS = { # both encodings of one instruction: one shader, two instructions "a_ps": "; a_ps code 0x0..0x10, gfx1013, 0 invalid words\n" "\tv_cndmask_b32_e32 v0, v1, v2, vcc\n\tv_cndmask_b32_e64 v3, v1, v2, s[0:1]\n\ts_endpgm\n", # an SDWA and a DPP form; a comment line naming an instruction is not counted "b_vs": "; v_rcp_f64 in a comment\n" "\tv_add_f16_sdwa v0, v1, v2 dst_sel:WORD_1\n\tv_add_f16_dpp v0, v1, v2 quad_perm:[0,1,2,3]\n" "\tv_cndmask_b32_e32 v0, v1, v2, vcc\n\ts_endpgm\n", # a name that merely ends like a suffix keeps it: v_cvt_pkrtz_f16_f32 has no encoding suffix "c_cs": "\tv_cvt_pkrtz_f16_f32_e32 v0, v1, v2\n\ts_endpgm\n", # two programs, then data words decoded as instructions after the last s_endpgm: those are not counted "d_cs": "\tv_exp_f16_e32 v0, v1\n\ts_endpgm\n\tv_cos_f16_e32 v0, v1\n\ts_endpgm\n" "\ts_cmov_b32 s0, s1\n\tv_mac_f32_e32 v0, v1, v2\n", # no s_endpgm at all: the whole listing counts "e_cs": "\tv_cvt_f16_f32_e32 v0, v1\n", } def build(root): disasm = os.path.join(root, "disasm") os.makedirs(disasm) for stem, text in LISTINGS.items(): with open(os.path.join(disasm, stem + ".s"), "w", encoding="utf-8", newline="\n") as f: f.write(text) index = os.path.join(root, "index.json") with open(index, "w", encoding="utf-8") as f: json.dump([{"name": s, "file": s + ".dxbc", "stage": s.split("_")[1]} for s in LISTINGS], f) return disasm, index # Compiled wave size per listing; e_cs has no sidecar header. WAVES = {"a_ps": 64, "b_vs": 64, "c_cs": 32, "d_cs": 64} def build_fssm(root): fssm = os.path.join(root, "fssm") os.makedirs(fssm) for stem, wave in WAVES.items(): header = bytearray(0x100) header[:4] = b"FSSM" struct.pack_into("