diff --git a/tests/test_dpp_hw_probe.py b/tests/test_dpp_hw_probe.py new file mode 100644 index 0000000000..83b69787ba --- /dev/null +++ b/tests/test_dpp_hw_probe.py @@ -0,0 +1,295 @@ +"""CPU-only unit tests for tools/dpp_hw_probe.py (TOOL-DPP-HW-PROBE). No GPU and no game files. + +The encoding test runs AMD clang's llvm-mc when LEONIDA_AMD_CLANG points at a clang.exe with llvm-mc beside it, and +is skipped otherwise; the same words are pinned in the probe's --self-test. +""" +import io +import json +import os +import re +import shutil +import subprocess +import sys +import tempfile +import types +import unittest +from unittest import mock + +REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +TOOLS = os.path.join(REPO, "tools") +PROBE = os.path.join(TOOLS, "dpp_hw_probe.py") +if TOOLS not in sys.path: + sys.path.insert(0, TOOLS) + +import dpp_hw_probe as probe # noqa: E402 + + +class _FakeUnavailable(Exception): + pass + + +def _fake_hw_oracle(open_side_effect=None, family="gfx11"): + def fam(arch): + if family is None: + raise ValueError(arch) + return family + asm_mod = types.SimpleNamespace(family=fam, Kernel=lambda *a, **k: (a, k)) + open_oracle = mock.Mock(side_effect=open_side_effect or _FakeUnavailable("no gpu")) + runner_mod = types.SimpleNamespace(Unavailable=_FakeUnavailable, open_oracle=open_oracle) + pkg = types.SimpleNamespace(asm=asm_mod, runner=runner_mod) + return {"hw_oracle": pkg, "hw_oracle.asm": asm_mod, "hw_oracle.runner": runner_mod}, open_oracle + + +def _neg_words(wave, model, flip=None): + """The words a GPU following `model` stores for group neg; flip=(case index, lane) breaks one word.""" + n = len(probe.NEG_CASES) + words = [0] * (wave * n) + for j, case in enumerate(probe.NEG_CASES): + for lane in range(wave): + words[lane * n + j] = probe.neg_model(case, lane, model) + if flip: + j, lane = flip + words[lane * n + j] ^= 0x1 + return words + + +def _vopc_words(wave, model): + runs = probe.vopc_runs() + per = [] + for run in runs: + mask = sum(probe.vopc_model(run, lane, model) << lane for lane in range(wave)) + per += [mask & 0xFFFFFFFF, mask >> 32] + return per * wave + + +class Models(unittest.TestCase): + def test_self_test(self): + proc = subprocess.run([sys.executable, "-I", PROBE, "--self-test"], capture_output=True, text=True, + check=False) + self.assertEqual(proc.returncode, 0, proc.stderr or proc.stdout) + self.assertIn("self-test OK", proc.stdout) + + def test_neg_models_differ_only_on_negated_invalid_lanes(self): + for case in probe.NEG_CASES: + for wave in (32, 64): + with self.subTest(case=case[0], wave=wave): + diff = [l for l in range(wave) if probe.neg_model(case, l, "mods_after_zero") + != probe.neg_model(case, l, "mods_before_zero")] + invalid = [l for l in range(wave) if probe.lane_active(l, case[4]) + and not probe.source_valid(l, case[3], case[4])] + self.assertEqual(diff, invalid if case[2] == "neg" else []) + for l in diff: + self.assertEqual(probe.neg_model(case, l, "mods_after_zero"), 0x80000000) + self.assertEqual(probe.neg_model(case, l, "mods_before_zero"), 0) + + def test_each_vopc_model_is_singled_out_by_some_run(self): + """For every pair of models some run gives a lane a different bit, so a GPU matches at most one.""" + for wave in (32, 64): + for a in probe.VOPC_MODELS: + for b in probe.VOPC_MODELS: + if a < b: + with self.subTest(wave=wave, models=(a, b)): + self.assertTrue(any(probe.vopc_model(r, l, a) != probe.vopc_model(r, l, b) + for r in probe.vopc_runs() for l in range(wave))) + + def test_lane_categories(self): + row_mask = probe.Ctrl("quad_perm", (0, 1, 2, 3), row_mask=0x5, bound_ctrl=True) + bank_mask = probe.Ctrl("quad_perm", (0, 1, 2, 3), bank_mask=0x5, bound_ctrl=True) + swap = probe.Ctrl("quad_perm", (1, 0, 3, 2)) + self.assertEqual([probe.lane_category(l, row_mask, "all") for l in (0, 15, 16, 31, 32, 48)], + ["valid", "valid", "masked", "masked", "valid", "masked"]) + self.assertEqual([probe.lane_category(l, bank_mask, "all") for l in (0, 3, 4, 7, 8, 12)], + ["valid", "valid", "masked", "masked", "valid", "masked"]) + self.assertEqual([probe.lane_category(l, swap, "even") for l in (0, 1, 2)], + ["unwritten", "inactive", "unwritten"]) + shr = probe.Ctrl("row_shr", 1, bound_ctrl=True) + self.assertEqual([probe.lane_category(l, shr, "all") for l in (0, 1, 16, 17)], + ["zero_src", "valid", "zero_src", "valid"]) + + +class Kernels(unittest.TestCase): + def test_vopc_compares_are_long_words_with_no_wait_state_between_them(self): + for fam in probe.FAMILIES: + for wave in (32, 64): + with self.subTest(fam=fam, wave=wave): + lines = probe.vopc_lines(wave, fam) + longs = [ln for ln in lines if ln.startswith(".long")] + self.assertEqual(len(longs), len(probe.vopc_runs())) + for ln, (_, op, ctrl, _, _) in zip(longs, probe.vopc_runs()): + words = tuple(int(w, 16) for w in re.findall(r"0x([0-9a-f]{8})", ln.split(";")[0])) + self.assertEqual(words, probe.vopc_dpp_words(fam, op, 1, 2, ctrl)) + # no compare mnemonic outside the .long comments: gfx10 cannot assemble one + code = [ln.split(";")[0] for ln in lines] + self.assertFalse([c for c in code if re.match(r"\s*v_cmpx?_", c)]) + + def test_kernels_restore_exec_and_end(self): + for wave in (32, 64): + for lines in (probe.neg_lines(wave), probe.vopc_lines(wave, "gfx11")): + with self.subTest(wave=wave): + self.assertEqual(lines[-1], "s_endpgm") + stores = [i for i, ln in enumerate(lines) if ln.startswith("global_store_b32")] + self.assertTrue(stores) + last_exec = max(i for i, ln in enumerate(lines[:stores[0]]) if "exec_lo" in ln and + ln.startswith("s_mov_b32 exec_lo")) + self.assertEqual(lines[last_exec], "s_mov_b32 exec_lo, -1") + + def test_neg_kernel_keeps_the_multiplier_register(self): + lines = probe.neg_lines(32) + dsts = {int(m) for ln in lines for m in re.findall(r"^v_(?:add|mul)_f32_dpp v(\d+)", ln)} + self.assertEqual(len(dsts), len(probe.NEG_CASES)) + self.assertNotIn(7, dsts) + self.assertNotIn(1, dsts) + self.assertNotIn(2, dsts) + + def test_store_offsets_fit_gfx10(self): + # GFX10 global instructions take a 12-bit signed offset + for wave in (32, 64): + for lines in (probe.neg_lines(wave), probe.vopc_lines(wave, "gfx10")): + offs = [int(m) for ln in lines for m in re.findall(r"offset:(\d+)", ln)] + self.assertLessEqual(max(offs), 2047) + + def test_encoding_rejects_bad_fields(self): + with self.assertRaises(ValueError): + probe.vopc_dpp_words("gfx12", "v_cmp_gt_f32", 1, 2, probe.Ctrl("row_shr", 1)) + with self.assertRaises(ValueError): + probe.Ctrl("row_shr", 0) + with self.assertRaises(ValueError): + probe.Ctrl("quad_perm", (0, 1, 2, 4)) + with self.assertRaises(ValueError): + probe.Ctrl("row_ror", 1) + with self.assertRaises(ValueError): + probe.dpp_word(256, probe.Ctrl("row_shr", 1)) + + +@unittest.skipUnless(os.environ.get("LEONIDA_AMD_CLANG") and shutil.which( + os.path.join(os.path.dirname(os.environ.get("LEONIDA_AMD_CLANG", "")), "llvm-mc")), + "needs LEONIDA_AMD_CLANG with llvm-mc beside it") +class LlvmMcEncodings(unittest.TestCase): + """The gfx11 words the probe emits are the ones AMD clang's llvm-mc encodes for the same text.""" + + def test_gfx11_vopc_dpp_words(self): + mc = shutil.which(os.path.join(os.path.dirname(os.environ["LEONIDA_AMD_CLANG"]), "llvm-mc")) + for op in probe.VOPC_OPS: + for _, ctrl, _ in probe.VOPC_CTRLS: + dst = "" if op.startswith("v_cmpx_") else "vcc_lo, " + text = f"{op}_dpp {dst}v1, v2 {ctrl.text()}\n" + with self.subTest(text=text.strip()): + r = subprocess.run([mc, "-arch=amdgcn", "-mcpu=gfx1101", "--show-encoding"], input=text, + capture_output=True, text=True, check=False) + self.assertEqual(r.returncode, 0, r.stderr) + enc = re.search(r"encoding: \[([^\]]+)\]", r.stdout) + self.assertIsNotNone(enc, r.stdout) + b = bytes(int(x, 16) for x in enc.group(1).split(",")) + got = (int.from_bytes(b[0:4], "little"), int.from_bytes(b[4:8], "little")) + self.assertEqual(got, probe.vopc_dpp_words("gfx11", op, 1, 2, ctrl)) + + +class Judging(unittest.TestCase): + def test_judge_neg_names_the_matching_model(self): + for model in probe.NEG_MODELS: + judged = probe.judge_neg(32, _neg_words(32, model)) + with self.subTest(model=model): + self.assertTrue(all(model in c["models"] for c in judged.values())) + self.assertEqual(probe.summary({"neg": {"32": judged}})["neg wave32"]["consistent_models"], + [model]) + + def test_judge_neg_reports_a_case_no_model_matches(self): + judged = probe.judge_neg(32, _neg_words(32, "mods_after_zero", flip=(3, 5))) + s = probe.summary({"neg": {"32": judged}})["neg wave32"] + self.assertEqual(s["unmatched_cases"], [probe.NEG_CASES[3][0]]) + + def test_judge_vopc_names_the_matching_model(self): + for wave in (32, 64): + for model in probe.VOPC_MODELS: + with self.subTest(wave=wave, model=model): + judged = probe.judge_vopc(wave, _vopc_words(wave, model)) + s = probe.summary({"vopc": {str(wave): judged}})[f"vopc wave{wave}"] + self.assertEqual(s["consistent_models"], [model]) + self.assertEqual(s["unmatched_cases"], []) + + def test_judge_vopc_rejects_lanes_that_disagree(self): + words = _vopc_words(32, "unwritten_gets_0") + words[-1] ^= 1 + with self.assertRaises(RuntimeError): + probe.judge_vopc(32, words) + + +class Cli(unittest.TestCase): + def run_main(self, argv, modules): + stdout, stderr = io.StringIO(), io.StringIO() + with mock.patch.dict(sys.modules, modules), mock.patch.object(sys, "stdout", stdout), \ + mock.patch.object(sys, "stderr", stderr): + rc = probe.main(argv) + return rc, stdout.getvalue(), stderr.getvalue() + + def test_missing_oracle_exits_77(self): + env = {k: v for k, v in os.environ.items() if not k.startswith("LEONIDA_HW_") and k != "LEONIDA_AMD_CLANG"} + env["LEONIDA_AMD_CLANG"] = os.path.join(REPO, "does_not_exist_clang") + proc = subprocess.run([sys.executable, "-I", PROBE], env=env, capture_output=True, text=True, check=False) + self.assertEqual(proc.returncode, 77, proc.stderr or proc.stdout) + + def test_bad_arguments_are_rejected_before_the_oracle_opens(self): + modules, open_oracle = _fake_hw_oracle() + for argv in (["--group", ""], ["--group", "neg,abs"], ["--waves", "16"], ["--waves", "32,x"], + ["--json", ""], ["--json", "bad\x00.json"]): + with self.subTest(argv=argv): + rc, _, err = self.run_main(argv, modules) + self.assertEqual(rc, 2) + self.assertIn("error:", err) + open_oracle.assert_not_called() + + def test_json_path_that_is_a_directory_is_rejected(self): + modules, open_oracle = _fake_hw_oracle() + with tempfile.TemporaryDirectory() as tmp: + rc, _, err = self.run_main(["--json", tmp], modules) + self.assertEqual(rc, 2) + open_oracle.assert_not_called() + + def test_other_families_skip_with_77_and_close_the_oracle(self): + for family in ("gfx12", None): + oracle = types.SimpleNamespace(arch="gfx1201", close=mock.Mock()) + modules, _ = _fake_hw_oracle(open_side_effect=lambda: oracle, family=family) + with self.subTest(family=family): + rc, _, err = self.run_main([], modules) + self.assertEqual(rc, 77) + self.assertIn("SKIP", err) + oracle.close.assert_called_once() + + def test_report_and_exit_code_follow_the_judged_cases(self): + oracle = types.SimpleNamespace(arch="gfx1101", close=mock.Mock()) + modules, _ = _fake_hw_oracle(open_side_effect=lambda: oracle) + good = {"neg": {"32": probe.judge_neg(32, _neg_words(32, "mods_after_zero"))}} + bad = {"neg": {"32": probe.judge_neg(32, _neg_words(32, "mods_after_zero", flip=(0, 0)))}} + with tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "sub", "report.json") + with mock.patch.object(probe, "probe", return_value=good): + rc, stdout, _ = self.run_main(["--group", "neg", "--waves", "32", "--json", out], modules) + self.assertEqual(rc, 0) + self.assertIn("every case matches mods_after_zero", stdout) + with open(out, "rb") as f: + raw = f.read() + self.assertNotIn(b"\r\n", raw) + self.assertEqual(json.loads(raw)["arch"], "gfx1101") + with mock.patch.object(probe, "probe", return_value=bad): + rc, stdout, _ = self.run_main(["--group", "neg", "--waves", "32"], modules) + self.assertEqual(rc, 1) + self.assertIn("no model matches", stdout) + + def test_a_failed_kernel_is_a_harness_error(self): + oracle = types.SimpleNamespace(arch="gfx1101", close=mock.Mock(), + run=lambda kernels: ["assembler rejected: x"] * len(kernels)) + modules, _ = _fake_hw_oracle(open_side_effect=lambda: oracle) + rc, _, err = self.run_main(["--group", "vopc", "--waves", "32"], modules) + self.assertEqual(rc, 2) + self.assertIn("assembler rejected", err) + oracle.close.assert_called_once() + + def test_write_json_report_error_is_one_line(self): + with tempfile.TemporaryDirectory() as tmp: + with mock.patch("builtins.open", side_effect=PermissionError(13, "denied")): + err = probe.write_json_report(os.path.join(tmp, "out.json"), {}) + self.assertIn("cannot write --json", err) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/dpp_hw_probe.py b/tools/dpp_hw_probe.py new file mode 100644 index 0000000000..fe4a9dce7c --- /dev/null +++ b/tools/dpp_hw_probe.py @@ -0,0 +1,473 @@ +r"""Measure two DPP edge cases natively through `tools/hw_oracle` (TOOL-DPP-HW-PROBE). + +Group `neg`: does DPP apply src0's `neg` / `abs` to the 0 that `bound_ctrl:1` gives a lane whose source is invalid +(out of its row, or off in EXEC with `fi:0`)? `v_add_f32_dpp` / `v_mul_f32_dpp` with `-src0`, `src0` and `|src0|`; +each lane is compared with two models: the modifiers come after the zero (the invalid lane reads -0), or before it +(+0, what `Gen.dpp_insn` in `tools/rdna2spirv.py` emits). Measured on an RX 7800 XT (gfx1101): after. + +Group `vopc`: what does a VOPC compare in DPP form (`v_cmp_*_dpp` into VCC, `v_cmpx_*_dpp` into EXEC) do to the bit +of a lane DPP leaves unwritten (an invalid source with `bound_ctrl:0`, or a lane `row_mask` / `bank_mask` turn +off)? VCC is preset to 0 and to all ones, and each lane is compared with three models: the bit is written 0, kept, +or the compare of a 0 source. Measured on an RX 7800 XT (gfx1101): written 0. This is the open point of the +"DPP on a VOPC compare" row of docs/design.md. + +LLVM has no gfx10 encoding for a VOPC compare in DPP form ("dpp variant of this instruction is not supported" for +gfx1030), so the compares are emitted as `.long` words: the VOPC word with src0 = 250 (DPP16) and the DPP word, laid +out as AMD clang's llvm-mc encodes them for gfx1101, with the gfx10 VOPC opcodes of its gfx1030 e32 encodings. + +Usage: + python -I tools/dpp_hw_probe.py [--group neg,vopc] [--waves 32,64] [--json out.json] + python -I tools/dpp_hw_probe.py --self-test # model and encoding checks, no GPU + +Prints, per case and wave size, which models every lane matches. Exit 0 when every case matches at least one model +(or --self-test passed), 1 when a case matches none or the report cannot be written, 2 on a harness or CLI error, +77 when tools/hw_oracle is unavailable or the GPU is not gfx10 / gfx11. Standard library only; nothing here reads +game files. +""" +import argparse +import json +import os +import struct +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.dont_write_bytecode = True +if HERE not in sys.path: + sys.path.insert(0, HERE) + +SENTINEL = 0xDEADBEEF +FAMILIES = ("gfx10", "gfx11") +GROUPS = ("neg", "vopc") + +# VOPC opcodes (bits [24:17] of the VOPC word). gfx10 from llvm-mc -mcpu=gfx1030 e32 encodings, gfx11 from +# llvm-mc -mcpu=gfx1101 DPP encodings (AMD LLVM 23.0.0git, rocm-sdk-core 10.0.0). +VOPC_OPCODES = { + "gfx10": {"v_cmp_lt_f32": 0x01, "v_cmp_gt_f32": 0x04, "v_cmpx_lt_f32": 0x11, "v_cmpx_gt_f32": 0x14}, + "gfx11": {"v_cmp_lt_f32": 0x11, "v_cmp_gt_f32": 0x14, "v_cmpx_lt_f32": 0x91, "v_cmpx_gt_f32": 0x94}, +} +VOPC_ENC = 0x3E # bits [31:25] of a VOPC word +SRC0_DPP16 = 250 # src0 field value that says "a DPP16 word follows" + + +class Ctrl: + """One DPP16 control: ("row_shr", n) or ("quad_perm", (a, b, c, d)), the masks, bound_ctrl and fi.""" + + def __init__(self, kind, arg, row_mask=0xF, bank_mask=0xF, bound_ctrl=False, fi=False): + if kind not in ("row_shr", "quad_perm"): + raise ValueError(f"unsupported DPP control {kind!r}") + if kind == "row_shr" and not 1 <= arg <= 15: + raise ValueError(f"row_shr:{arg} is not in 1..15") + if kind == "quad_perm" and (len(arg) != 4 or any(not 0 <= x <= 3 for x in arg)): + raise ValueError(f"quad_perm:{list(arg)} needs four selects in 0..3") + if not (0 <= row_mask <= 0xF and 0 <= bank_mask <= 0xF): + raise ValueError("row_mask and bank_mask are 4-bit") + self.kind, self.arg, self.row_mask, self.bank_mask = kind, arg, row_mask, bank_mask + self.bound_ctrl, self.fi = bound_ctrl, fi + + def dpp_ctrl(self): + """The 9-bit DPP_CTRL field: quad_perm 0x000-0x0FF, row_shr:n 0x110 + n.""" + if self.kind == "quad_perm": + a, b, c, d = self.arg + return a | b << 2 | c << 4 | d << 6 + return 0x110 + self.arg + + def text(self): + """The control as assembler syntax (bound_ctrl:0 is written by leaving it out).""" + sel = f"row_shr:{self.arg}" if self.kind == "row_shr" else "quad_perm:[" + ",".join(map(str, self.arg)) + "]" + out = f"{sel} row_mask:0x{self.row_mask:x} bank_mask:0x{self.bank_mask:x}" + return out + (" bound_ctrl:1" if self.bound_ctrl else "") + (" fi:1" if self.fi else "") + + def source(self, lane): + """The lane this lane reads, or None when that is out of its row of 16.""" + if self.kind == "row_shr": + return lane - self.arg if lane % 16 >= self.arg else None + return (lane & ~3) + self.arg[lane & 3] + + def masked(self, lane): + """True when row_mask or bank_mask turn this lane off (DPP does not write it).""" + return not (self.row_mask >> (lane // 16) & 1 and self.bank_mask >> ((lane % 16) // 4) & 1) + + +def dpp_word(src0, ctrl, src0_neg=False, src0_abs=False, src1_neg=False, src1_abs=False): + """The DPP16 word: src0 [7:0], DPP_CTRL [16:8], FI [18], BOUND_CTRL [19], src0/src1 neg [20]/[22] and abs + [21]/[23], BANK_MASK [27:24], ROW_MASK [31:28].""" + if not 0 <= src0 <= 255: + raise ValueError(f"src0 v{src0} is not a VGPR") + return (src0 | ctrl.dpp_ctrl() << 8 | ctrl.fi << 18 | ctrl.bound_ctrl << 19 | src0_neg << 20 | src0_abs << 21 + | src1_neg << 22 | src1_abs << 23 | ctrl.bank_mask << 24 | ctrl.row_mask << 28) + + +def vopc_dpp_words(fam, op, src0, vsrc1, ctrl): + """The two words of `op` (VOPC, f32) in DPP16 form: compare v of the selected lane with this lane's + v.""" + if fam not in VOPC_OPCODES: + raise ValueError(f"no VOPC opcodes for {fam!r} (expected one of {', '.join(FAMILIES)})") + if not 0 <= vsrc1 <= 255: + raise ValueError(f"vsrc1 v{vsrc1} is not a VGPR") + word0 = VOPC_ENC << 25 | VOPC_OPCODES[fam][op] << 17 | vsrc1 << 9 | SRC0_DPP16 + return word0, dpp_word(src0, ctrl) + + +def f32(x): + return struct.unpack(" 0.5), v2 = 0.5 +VOPC_CTRLS = [ + ("row_shr_bc0", Ctrl("row_shr", 1), "all"), + ("row_shr_bc1", Ctrl("row_shr", 1, bound_ctrl=True), "all"), + ("row_mask5_bc1", Ctrl("quad_perm", (0, 1, 2, 3), row_mask=0x5, bound_ctrl=True), "all"), + ("bank_mask5_bc1", Ctrl("quad_perm", (0, 1, 2, 3), bank_mask=0x5, bound_ctrl=True), "all"), + ("quad_even_fi0_bc0", Ctrl("quad_perm", (1, 0, 3, 2)), "even"), + ("quad_even_fi0_bc1", Ctrl("quad_perm", (1, 0, 3, 2), bound_ctrl=True), "even"), +] +VOPC_OPS = ("v_cmp_gt_f32", "v_cmp_lt_f32", "v_cmpx_gt_f32", "v_cmpx_lt_f32") +VOPC_MODELS = ("unwritten_gets_0", "unwritten_keeps_bit", "unwritten_compares_0") + + +def vopc_runs(): + """(name, op, control, EXEC pattern, VCC preset or None) for every run; v_cmpx has no preset (EXEC is its + destination and starts as the case's pattern).""" + runs = [] + for op in VOPC_OPS: + for name, ctrl, ex in VOPC_CTRLS: + for preset in ((0, 1) if op.startswith("v_cmp_") else (None,)): + runs.append((f"{op}_{name}" + ("" if preset is None else f"_preset{preset}"), op, ctrl, ex, preset)) + return runs + + +def lane_category(lane, ctrl, ex): + """inactive / masked / valid / zero_src (bound_ctrl:1 zero) / unwritten (bound_ctrl:0, invalid source).""" + if not lane_active(lane, ex): + return "inactive" + if ctrl.masked(lane): + return "masked" + if source_valid(lane, ctrl, ex): + return "valid" + return "zero_src" if ctrl.bound_ctrl else "unwritten" + + +def vopc_model(run, lane, model): + """The bit lane `lane` holds after a vopc run under `model` (VCC for v_cmp, EXEC for v_cmpx).""" + _, op, ctrl, ex, preset = run + gt = op.endswith("gt_f32") + cat = lane_category(lane, ctrl, ex) + if cat == "inactive": + return 0 + if cat == "valid": + return int(gt) # lane + 1.0 > 0.5 + if cat == "zero_src": + return int(not gt) # 0 < 0.5 + if model == "unwritten_gets_0": + return 0 + if model == "unwritten_keeps_bit": + return 1 if preset is None else preset # v_cmpx: the lane was on in EXEC + return int(not gt) # unwritten_compares_0 + + +# ---- kernels ---------------------------------------------------------------------------------------------------- + +def _set32(lo, hi, value, wave): + return [f"s_mov_b32 {lo}, {value}"] + ([f"s_mov_b32 {hi}, {value}"] if wave == 64 else []) + + +def _exec(pattern, wave): + return _set32("exec_lo", "exec_hi", -1 if pattern == "all" else "0x55555555", wave) + + +def _guard(lines): + """s_nop 7 after every instruction: the VALU -> SALU reads of VCC / EXEC need wait states, and the model has + none (as crosslane_fuzz's guard).""" + out = [] + for ln in lines: + out.append(ln) + if ln != "s_endpgm": + out.append("s_nop 7") + return out + + +def neg_lines(wave): + """Kernel body for group neg: one word per case and lane.""" + dsts = [d for d in range(3, 4 + len(NEG_CASES)) if d != 7][:len(NEG_CASES)] # v7 holds the multiplier + body = ["v_cvt_f32_u32_e32 v1, v0", "v_add_f32_e32 v1, 1.0, v1", "v_mov_b32_e32 v2, 0x80000000", + "v_mov_b32_e32 v7, 1.0"] + body +=[f"v_mov_b32_e32 v{d}, 0x{SENTINEL:08x}" for d in dsts] + for case, d in zip(NEG_CASES, dsts): + body += _exec(case[4], wave) + [neg_text(case, d)] + _exec("all", wave) + body.append(f"v_mul_u32_u24_e32 v20, {4 * len(NEG_CASES)}, v0") + body += [f"global_store_b32 v20, v{d}, s[2:3] offset:{4 * j}" for j, d in enumerate(dsts)] + body.append("s_endpgm") + return _guard(body) + + +def vopc_lines(wave, fam): + """Kernel body for group vopc: two words (the mask) per run; every lane stores the same words.""" + runs = vopc_runs() + body = ["v_cvt_f32_u32_e32 v1, v0", "v_add_f32_e32 v1, 1.0, v1", "v_mov_b32_e32 v2, 0.5", + f"v_mul_u32_u24_e32 v20, {8 * len(runs)}, v0"] + for i, (_, op, ctrl, ex, preset) in enumerate(runs): + if preset is not None: + body += _set32("vcc_lo", "vcc_hi", -preset, wave) # 0 or -1 + body += _exec(ex, wave) + w0, w1 = vopc_dpp_words(fam, op, 1, 2, ctrl) + body.append(f".long 0x{w0:08x}, 0x{w1:08x} ; {op}_dpp {ctrl.text()}") + mask = "exec" if op.startswith("v_cmpx_") else "vcc" + body += ["s_mov_b32 s10, " + mask + "_lo", "s_mov_b32 s11, " + (mask + "_hi" if wave == 64 else "0")] + body += _exec("all", wave) + body += ["v_mov_b32_e32 v3, s10", "v_mov_b32_e32 v4, s11", + f"global_store_b32 v20, v3, s[2:3] offset:{8 * i}", + f"global_store_b32 v20, v4, s[2:3] offset:{8 * i + 4}"] + body.append("s_endpgm") + return _guard(body) + + +# ---- comparison ------------------------------------------------------------------------------------------------- + +def judge_neg(wave, words): + """{case: {"models": [...matching], "lanes": {lane: word} for lanes 0, 1, 2, 15, 16, 17}} for group neg.""" + n = len(NEG_CASES) + out = {} + for j, case in enumerate(NEG_CASES): + got = [words[lane * n + j] for lane in range(wave)] + models = [m for m in NEG_MODELS if all(got[l] == neg_model(case, l, m) for l in range(wave))] + out[case[0]] = {"instruction": neg_text(case, "D"), "exec": case[4], "models": models, + "lanes": {str(l): f"0x{got[l]:08x}" for l in (0, 1, 2, 15, 16, 17)}} + return out + + +def judge_vopc(wave, words): + """{run: {"models": [...matching], "mask": hex}} for group vopc, from lane 0's words; also checks that every + lane stored the same words.""" + runs = vopc_runs() + per = 2 * len(runs) + if any(words[l * per:(l + 1) * per] != words[:per] for l in range(wave)): + raise RuntimeError(f"wave{wave}: lanes stored different mask words") + out = {} + for i, run in enumerate(runs): + mask = words[2 * i] | words[2 * i + 1] << 32 + models = [m for m in VOPC_MODELS if all((mask >> l & 1) == vopc_model(run, l, m) for l in range(wave))] + out[run[0]] = {"control": run[2].text(), "exec": run[3], "models": models, + "mask": f"0x{mask:0{wave // 4}x}"} + return out + + +def probe(oracle, asm_mod, fam, groups, waves): + report = {} + kernels, keys = [], [] + for wave in waves: + if "neg" in groups: + kernels.append(asm_mod.Kernel(neg_lines(wave), wave, wave, wave * len(NEG_CASES), raw=True)) + keys.append(("neg", wave)) + if "vopc" in groups: + kernels.append(asm_mod.Kernel(vopc_lines(wave, fam), wave, wave, wave * 2 * len(vopc_runs()), raw=True)) + keys.append(("vopc", wave)) + for (group, wave), got in zip(keys, oracle.run(kernels)): + if isinstance(got, str): + raise RuntimeError(f"{group} wave{wave}: {got}") + judged = judge_neg(wave, got) if group == "neg" else judge_vopc(wave, got) + report.setdefault(group, {})[str(wave)] = judged + return report + + +def summary(report): + """Per group and wave: the models every case matches, and the cases that match none.""" + out = {} + for group, by_wave in report.items(): + models = NEG_MODELS if group == "neg" else VOPC_MODELS + for wave, cases in by_wave.items(): + common = [m for m in models if all(m in c["models"] for c in cases.values())] + none = sorted(k for k, c in cases.items() if not c["models"]) + out[f"{group} wave{wave}"] = {"consistent_models": common, "unmatched_cases": none} + return out + + +# ---- self-test and CLI -------------------------------------------------------------------------------------------- + +def self_test(): + # DPP words as AMD clang's llvm-mc encodes them for gfx1101 and gfx1030 (v_max_f32_dpp carries the same word) + assert dpp_word(1, Ctrl("row_shr", 1)) == 0xFF011101 + assert dpp_word(3, Ctrl("quad_perm", (1, 0, 3, 2), row_mask=5, bank_mask=0xA, bound_ctrl=True, fi=True)) == 0x5A0CB103 + assert dpp_word(3, Ctrl("quad_perm", (2, 3, 0, 1)), src0_neg=True, src1_abs=True) == 0xFF904E03 + # gfx11 VOPC DPP words (llvm-mc -mcpu=gfx1101 --show-encoding) + assert vopc_dpp_words("gfx11", "v_cmp_lt_f32", 1, 2, Ctrl("row_shr", 1)) == (0x7C2204FA, 0xFF011101) + assert vopc_dpp_words("gfx11", "v_cmp_gt_f32", 1, 2, Ctrl("row_shr", 1))[0] == 0x7C2804FA + assert vopc_dpp_words("gfx11", "v_cmpx_lt_f32", 1, 2, Ctrl("row_shr", 1))[0] == 0x7D2204FA + assert vopc_dpp_words("gfx11", "v_cmpx_gt_f32", 1, 2, Ctrl("row_shr", 1))[0] == 0x7D2804FA + # gfx10 opcodes: the e32 words llvm-mc -mcpu=gfx1030 prints, with src0 = 250 in place of v1 + for op, e32 in (("v_cmp_lt_f32", 0x7C020501), ("v_cmp_gt_f32", 0x7C080501), ("v_cmpx_lt_f32", 0x7C220501), + ("v_cmpx_gt_f32", 0x7C280501)): + assert vopc_dpp_words("gfx10", op, 1, 2, Ctrl("row_shr", 1))[0] == (e32 & ~0x1FF) | SRC0_DPP16, op + # the neg models differ exactly on the invalid lanes of the negated cases + case = NEG_CASES[0] + diff = [l for l in range(32) if neg_model(case, l, "mods_after_zero") != neg_model(case, l, "mods_before_zero")] + assert diff == [0, 16], diff + assert neg_model(case, 0, "mods_after_zero") == 0x80000000 and neg_model(case, 0, "mods_before_zero") == 0 + assert neg_model(NEG_CASES[2], 0, "mods_after_zero") == 0 # |0| + -0 + # vopc: the three models are told apart across the two presets and the two compares + runs = {r[0]: r for r in vopc_runs()} + lane0 = lambda name, m: vopc_model(runs[name], 0, m) + assert [lane0("v_cmp_gt_f32_row_shr_bc0_preset1", m) for m in VOPC_MODELS] == [0, 1, 0] + assert [lane0("v_cmp_lt_f32_row_shr_bc0_preset0", m) for m in VOPC_MODELS] == [0, 0, 1] + assert vopc_model(runs["v_cmp_gt_f32_row_mask5_bc1_preset1"], 16, "unwritten_keeps_bit") == 1 + assert lane_category(16, VOPC_CTRLS[2][1], "all") == "masked" + assert lane_category(1, VOPC_CTRLS[4][1], "even") == "inactive" + assert lane_category(0, VOPC_CTRLS[4][1], "even") == "unwritten" + print("self-test OK") + + +def parse_list(text, allowed, what): + tokens = [t.strip() for t in text.split(",") if t.strip()] + if not tokens: + raise ValueError(f"--{what} must not be empty") + out = [] + for tok in tokens: + if tok not in allowed: + raise ValueError(f"invalid {what[:-1]} {tok!r}; expected {' or '.join(allowed)}") + if tok not in out: + out.append(tok) + return out + + +def validate_json_path(path): + """An error string if `path` cannot be a --json output file, or None.""" + if not path or not path.strip(): + return "--json path must not be empty" + if "\x00" in path: + return "--json path contains a NUL byte" + if path.endswith(os.sep) or (os.altsep and path.endswith(os.altsep)) or os.path.isdir(path): + return f"--json path is a directory: {path}" + cur = os.path.dirname(os.path.abspath(path)) + while cur: + if os.path.isdir(cur): + break + if os.path.lexists(cur): + return f"--json parent is not a directory: {cur}" + nxt = os.path.dirname(cur) + if nxt == cur: + break + cur = nxt + return None + + +def write_json_report(path, data): + """Create missing parent directories and write `data` as LF-terminated JSON, or return an error string.""" + parent = os.path.dirname(os.path.abspath(path)) + try: + os.makedirs(parent, exist_ok=True) + with open(path, "w", encoding="utf-8", newline="\n") as f: + json.dump(data, f, indent=2) + f.write("\n") + except OSError as e: + return f"cannot write --json {path}: {e.strerror or e}" + return None + + +def main(argv=None): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument("--group", default="neg,vopc", help="case groups to run: neg, vopc (default: both)") + ap.add_argument("--waves", default="32,64", help="wave sizes to run (default: 32,64)") + ap.add_argument("--json", dest="json_out", help="write the full report to this path") + ap.add_argument("--self-test", action="store_true", help="check the models and encodings without a GPU") + args = ap.parse_args(argv) + if args.self_test: + self_test() + return 0 + try: + groups = parse_list(args.group, GROUPS, "groups") + waves = [int(w) for w in parse_list(args.waves, ("32", "64"), "waves")] + except ValueError as exc: + print(f"error: {exc}", file=sys.stderr) + return 2 + if args.json_out is not None: + err = validate_json_path(args.json_out) + if err: + print(f"error: {err}", file=sys.stderr) + return 2 + from hw_oracle import asm, runner + try: + oracle = runner.open_oracle() + except runner.Unavailable as exc: + print(f"SKIP: {exc}", file=sys.stderr) + return 77 + try: + try: + fam = asm.family(oracle.arch) + except ValueError: + fam = None + if fam not in FAMILIES: + print(f"SKIP: {oracle.arch} is not gfx10 or gfx11 (no VOPC opcodes for it here)", file=sys.stderr) + return 77 + try: + report = probe(oracle, asm, fam, groups, waves) + except RuntimeError as exc: + print(f"error: {exc}", file=sys.stderr) + return 2 + finally: + close = getattr(oracle, "close", None) + if close: + close() + summ = summary(report) + for key, s in summ.items(): + models = ", ".join(s["consistent_models"]) or "none" + print(f"{key} ({oracle.arch}): every case matches {models}" + + (f"; no model matches {', '.join(s['unmatched_cases'])}" if s["unmatched_cases"] else "")) + if args.json_out is not None: + err = write_json_report(args.json_out, {"arch": oracle.arch, "summary": summ, "cases": report}) + if err: + print(f"error: {err}", file=sys.stderr) + return 1 + return 1 if any(s["unmatched_cases"] for s in summ.values()) else 0 + + +if __name__ == "__main__": + if hasattr(sys.stdout, "reconfigure"): + sys.stdout.reconfigure(errors="replace") + raise SystemExit(main())