mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 21:48:37 +00:00
tools: vint_diff.py, a differential fuzzer for 32-bit integer VALU in every encoding and source kind
A separate ISA model for 55 integer opcodes (VOP1/VOP2 as e32 and e64, VOP3-only ops, VOPC e64 + v_cndmask) with VGPR, SGPR, inline, float-inline and literal sources within the encoding and constant-bus rules, compared one lane at a time with rdna2spirv run in tests/semantic/spirv_mini_interp.py, optionally with the hardware through tools/hw_oracle. A mask read as data (#293) only with --known. AI-assisted.
This commit is contained in:
@ -613,6 +613,24 @@ principle, but it needs the loaded code to be 256-byte aligned, which the kernel
|
||||
section above records a 4096-word output cap there, while a 64x64 case stores 16,384 words. Until that is measured
|
||||
on a GFX10 card, treat RDNA2 as unverified.
|
||||
|
||||
### 32-bit integer VALU in every encoding (`tools/vint_diff.py`)
|
||||
|
||||
`tools/shader_diff.py` checks the common integer opcodes with VGPR sources in their e32 form. `tools/vint_diff.py`
|
||||
adds the rest of the 32-bit integer VALU a compiler emits: the VOP1/VOP2 opcodes as e32 and e64, the VOP3-only ones
|
||||
(`v_add3_u32`, `v_lshl_add_u32`, `v_alignbit/alignbyte/perm_b32`, `v_min3/max3/med3`, the 24- and 16-bit
|
||||
multiplies with `op_sel`, `v_bcnt`, `v_bfm`, `v_ffbh/ffbl`, `v_cvt_pk_*16_*32`, ...) and VOPC e64 into a mask with
|
||||
`v_cndmask_b32_e64`, with VGPR, SGPR, integer and float inline-constant and literal sources in every position the
|
||||
encoding allows (one literal and two scalar values per VOP3). It runs one lane through
|
||||
`tests/semantic/spirv_mini_interp.py` against its own ISA model, with the shrinking, skips and exit codes of
|
||||
`salu_diff.py`; a compare's mask read as VALU data (#293) is left out unless `--known`. `--oracle hw` runs the same
|
||||
programs on an AMD GPU; on an RDNA2 iGPU (gfx1036) 1,500 programs, the known form included, matched the reference. A 32-bit literal in a 16-bit source of `v_mad_u32_u16`/`v_mad_i32_i16` does not assemble, so literals there have 16 bits (zero-extended, measured).
|
||||
|
||||
```
|
||||
python -I tools/vint_diff.py --programs 2000 --seed 1
|
||||
python -I tools/vint_diff.py --oracle hw --programs 500 --seed 1
|
||||
python -I tests/test_vint_diff.py
|
||||
```
|
||||
|
||||
### 16-bit and memory ops on the GPU (`tools/valu_fuzz.py`, FUZZ-2, FUZZ-5)
|
||||
|
||||
`tools/valu_fuzz.py --family f16|mem` uses the same runner shape as `lds_fuzz.py`. It generates seeded
|
||||
|
||||
225
tests/test_vint_diff.py
Normal file
225
tests/test_vint_diff.py
Normal file
@ -0,0 +1,225 @@
|
||||
"""tools/vint_diff.py: the integer VALU reference against hand-worked values, a short seeded fuzz run, and canaries.
|
||||
Standard library only; no GPU."""
|
||||
import contextlib
|
||||
import io
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
TOOL = Path(__file__).resolve().parents[1] / "tools" / "vint_diff.py"
|
||||
sys.path.insert(0, str(TOOL.parent))
|
||||
import vint_diff as d # noqa: E402
|
||||
|
||||
|
||||
def prefix(v=None, s=None):
|
||||
return ([f"v_mov_b32_e32 v{r}, 0x{(v or {}).get(r, 0):x}" for r in d.VREGS]
|
||||
+ [f"s_mov_b32 s{r}, 0x{(s or {}).get(r, 0):x}" for r in d.SREGS])
|
||||
|
||||
|
||||
def run(*body, v=None, s=None, regs=(1, 2, 3, 4)):
|
||||
"""The reference's four exported words; v / s map register -> seed value (others are 0)."""
|
||||
return d.reference(prefix(v, s) + list(body) + d.observe(list(regs)))
|
||||
|
||||
|
||||
class Reference(unittest.TestCase):
|
||||
def test_vop3_only_ops(self):
|
||||
self.assertEqual(run("v_add3_u32 v1, v2, s4, 0x10", v={2: 0xFFFFFFF0}, s={4: 4})[0], 4)
|
||||
self.assertEqual(run("v_alignbit_b32 v1, v2, v3, 8", v={2: 0x12345678, 3: 0x9ABCDEF0})[0], 0x789ABCDE)
|
||||
self.assertEqual(run("v_bfm_b32 v1, 5, 4")[0], 0x1F0)
|
||||
self.assertEqual(run("v_bcnt_u32_b32 v1, v2, 3", v={2: 0xF0F0})[0], 11)
|
||||
self.assertEqual(run("v_med3_i32 v1, v2, v3, 5", v={2: 0x80000000, 3: 7})[0], 5)
|
||||
self.assertEqual(run("v_xad_u32 v1, v2, v3, 1", v={2: 0xFF, 3: 0x0F})[0], 0xF1)
|
||||
# saturating packs: (-40000, 40000) -> 0x8000 | 0x7fff << 16; unsigned 0x12345 -> 0xffff
|
||||
self.assertEqual(run("v_cvt_pk_i16_i32 v1, v2, v3", v={2: (-40000) & d.M32, 3: 40000})[0], 0x7FFF8000)
|
||||
self.assertEqual(run("v_cvt_pk_u16_u32 v1, v2, 7", v={2: 0x12345})[0], 0x0007FFFF)
|
||||
|
||||
def test_bitfield_extract_stops_at_bit_31(self):
|
||||
# offset 28, width 8: only bits 28..31 exist, 0x8 sign-extends from bit 3 of the field
|
||||
self.assertEqual(run("v_bfe_i32 v1, v2, 28, 8", v={2: 0x80000000})[0], 0xFFFFFFF8)
|
||||
self.assertEqual(run("v_bfe_u32 v1, v2, 28, 8", v={2: 0x80000000})[0], 0x8)
|
||||
self.assertEqual(run("v_bfe_u32 v1, v2, 4, 0", v={2: M})[0], 0)
|
||||
|
||||
def test_perm_selectors(self):
|
||||
# {src0, src1} = 0x80000000_00008000: 8 -> bit 15 (set), 9 -> bit 31, 10 -> bit 47, 11 -> bit 63 (set)
|
||||
self.assertEqual(run("v_perm_b32 v1, v2, v3, 0x0b0a0908", v={2: 0x80000000, 3: 0x8000})[0], 0xFF0000FF)
|
||||
# 12 -> 0x00, 13 and up -> 0xff, 0..7 -> bytes of src1 then src0
|
||||
self.assertEqual(run("v_perm_b32 v1, v2, v3, 0x070d0c00", v={2: 0xAB000000, 3: 0x11})[0], 0xABFF0011)
|
||||
|
||||
def test_24_and_16_bit_multiplies(self):
|
||||
self.assertEqual(run("v_mul_hi_i32_i24_e64 v1, v2, 2", v={2: 0x800000})[0], M) # -2^23 * 2 < 0
|
||||
self.assertEqual(run("v_mul_u32_u24_e32 v1, v2, v3", v={2: 0xFF000003, 3: 5})[0], 15)
|
||||
# op_sel takes the high halves of the two 16-bit sources: 5 * 2 + 1
|
||||
self.assertEqual(run("v_mad_u32_u16 v1, v2, v3, 1 op_sel:[1,0,0,0]", v={2: 0x00050003, 3: 0x00070002})[0],
|
||||
11)
|
||||
self.assertEqual(run("v_mad_i32_i16 v1, v2, v3, 0", v={2: 0xFFFF, 3: 3})[0], M - 2) # -1 * 3
|
||||
|
||||
def test_find_first_bit(self):
|
||||
self.assertEqual(run("v_ffbh_i32_e64 v1, v2", v={2: 0xFFFFFFFE})[0], 31)
|
||||
self.assertEqual(run("v_ffbh_u32_e32 v1, v2", v={2: 0x00010000})[0], 15)
|
||||
self.assertEqual(run("v_ffbl_b32_e32 v1, 0")[0], M)
|
||||
|
||||
def test_constant_sources(self):
|
||||
# a float inline constant in an integer op is its f32 bits; a literal goes in src0 of an e32 VOP2
|
||||
self.assertEqual(run("v_add_nc_u32_e64 v1, 1.0, 0")[0], 0x3F800000)
|
||||
self.assertEqual(run("v_or_b32_e64 v1, 0.15915494, 0")[0], 0x3E22F983)
|
||||
self.assertEqual(run("v_xor_b32_e32 v1, 0x12345678, v2", v={2: 0xFF})[0], 0x12345687)
|
||||
self.assertEqual(run("v_lshlrev_b32_e64 v1, s4, -16", s={4: 36})[0], 0xFFFFFF00) # shift by 36 & 31
|
||||
|
||||
def test_compare_and_select(self):
|
||||
self.assertEqual(run("v_cmp_lt_i32_e64 s20, v2, v3", "v_cndmask_b32_e64 v1, 1, 2, s20",
|
||||
v={2: 0x80000000, 3: 0})[0], 2)
|
||||
self.assertEqual(run("v_cmp_lt_u32_e64 vcc_lo, v2, v3", "v_cndmask_b32_e64 v1, 1, 2, vcc_lo",
|
||||
v={2: 0x80000000, 3: 0})[0], 1)
|
||||
|
||||
|
||||
M = d.M32
|
||||
|
||||
|
||||
class Generator(unittest.TestCase):
|
||||
def lines(self, known=False, literals=True, n=3000, seed=1):
|
||||
real = d.KNOWN, d.LITERALS
|
||||
d.KNOWN, d.LITERALS = known, literals
|
||||
try:
|
||||
rng = d.random.Random(seed)
|
||||
gen = d.Gen(rng)
|
||||
return [line for _ in range(n) for line in gen.op(d.ALL_OPS)]
|
||||
finally:
|
||||
d.KNOWN, d.LITERALS = real
|
||||
|
||||
def test_every_op_is_generated_in_every_form(self):
|
||||
lines = self.lines()
|
||||
seen = {re.sub(r"_e(32|64)$", "", line.split()[0]) for line in lines}
|
||||
self.assertEqual(set(d.OPS) - seen, set())
|
||||
forms = {line.split()[0] for line in lines}
|
||||
for name, (enc, _, _) in d.OPS.items():
|
||||
if enc != "vop3":
|
||||
self.assertTrue({name + "_e32", name + "_e64"} <= forms, name)
|
||||
self.assertTrue(any(line.startswith("v_cmp_") for line in lines))
|
||||
|
||||
def test_sources_respect_the_encoding_rules(self):
|
||||
for line in self.lines():
|
||||
op, _, rest = line.partition(" ")
|
||||
srcs = [a.strip() for a in rest.split(" op_sel:")[0].split(",")][1:]
|
||||
if op == "v_cndmask_b32_e64":
|
||||
srcs = srcs[:-1]
|
||||
lits = [x for x in srcs if x.startswith("0x")]
|
||||
scalars = set(x for x in srcs if x.startswith("s")) | set(lits)
|
||||
if op.endswith("_e32"):
|
||||
self.assertTrue(len(srcs) == 1 or re.fullmatch(r"v\d+", srcs[1]), line)
|
||||
self.assertLessEqual(len(lits), 1, line)
|
||||
self.assertLessEqual(len(scalars), 2 if not op.startswith("v_cndmask") else 0, line)
|
||||
self.assertFalse(any("0x" in line.split(",", 1)[1] for line in self.lines(literals=False)))
|
||||
|
||||
def test_16bit_sources_take_16bit_literals(self):
|
||||
for line in self.lines():
|
||||
if line.startswith(d.OP_SEL_OPS):
|
||||
srcs = [a.strip() for a in line.split(" op_sel:")[0].split(",")][1:3]
|
||||
self.assertTrue(all(int(x, 16) <= 0xFFFF for x in srcs if x.startswith("0x")), line)
|
||||
# measured on gfx1036: such a literal is zero-extended, a float constant is its f32 bits
|
||||
self.assertEqual(run("v_mad_u32_u16 v1, 0xffff, 1, 0 op_sel:[1,0,0,0]")[0], 0)
|
||||
self.assertEqual(run("v_mad_u32_u16 v1, 0.5, 1, 0 op_sel:[1,0,0,0]")[0], 0x3F00)
|
||||
|
||||
def test_known_divergences_are_left_out_by_default(self):
|
||||
def mask_as_data(lines):
|
||||
for line in lines:
|
||||
op, _, rest = line.partition(" ")
|
||||
srcs = [a.strip() for a in rest.split(",")][1:]
|
||||
if op == "v_cndmask_b32_e64":
|
||||
srcs = srcs[:-1]
|
||||
if any(x in d.MASKS for x in srcs):
|
||||
return True
|
||||
return False
|
||||
self.assertFalse(mask_as_data(self.lines()))
|
||||
self.assertTrue(mask_as_data(self.lines(known=True)))
|
||||
|
||||
|
||||
class Translated(unittest.TestCase):
|
||||
def test_harness_canaries(self):
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
self.assertTrue(d.self_test())
|
||||
|
||||
def test_short_seeded_run_matches(self):
|
||||
counts, failures = d.run_suite(80, 5, 5, d.ALL_OPS)
|
||||
self.assertEqual(failures, [], failures[:1])
|
||||
self.assertEqual(counts["skip"], {})
|
||||
self.assertEqual(counts["pass"], 80)
|
||||
|
||||
def test_hand_cases_match_the_translator(self):
|
||||
seeds = {2: 0x80000000, 3: 0x00008000, 4: 0x00050003, 5: 0x00070002, 6: 0xFFFFFFFE}
|
||||
cases = ["v_bfe_i32 v1, v2, 28, 8", "v_perm_b32 v1, v2, v3, 0x0b0a0908", "v_perm_b32 v1, v2, v3, 0x070d0c00",
|
||||
"v_mad_u32_u16 v1, v4, v5, 1 op_sel:[1,0,0,0]", "v_ffbh_i32_e64 v1, v6",
|
||||
"v_add_nc_u32_e64 v1, 1.0, s4", "v_xor_b32_e32 v1, 0x12345678, v2",
|
||||
"v_lshlrev_b32_e64 v1, s5, -16", "v_cvt_pk_i16_i32 v1, v6, v4", "v_mul_hi_i32_i24_e32 v1, -2, v3",
|
||||
"v_alignbit_b32 v1, v4, v6, 8", "v_mad_u32_u16 v1, 0xffff, 0.5, v3 op_sel:[1,1,0,0]"]
|
||||
for case in cases:
|
||||
with self.subTest(case=case):
|
||||
self.assertIsNone(d.check(prefix(seeds, {4: 7, 5: 36}) + [case] + d.observe([1, 2, 3, 4])))
|
||||
|
||||
def test_a_wrong_translation_is_caught(self):
|
||||
lines = prefix({2: 5}) + ["v_add3_u32 v1, v2, 1, 2"] + d.observe([1, 2, 3, 4])
|
||||
self.assertIsNone(d.check(lines))
|
||||
want = d.reference(lines)
|
||||
real = d.translated
|
||||
try:
|
||||
d.translated = lambda _lines: [want[0] ^ 1] + want[1:]
|
||||
self.assertIn("word 0 (v1)", d.check(lines))
|
||||
finally:
|
||||
d.translated = real
|
||||
|
||||
def test_shrinking_keeps_the_instruction_that_matters(self):
|
||||
lines = d.generate(d.random.Random(2), 5, ["v_add_nc_u32", "v_xor_b32", "cmp"])
|
||||
body = lines[d.N_SEED:-d.N_OBS]
|
||||
target = body[len(body) // 2]
|
||||
if target.startswith("v_cmp"):
|
||||
target = body[len(body) // 2 + 1]
|
||||
real = d.translated
|
||||
try:
|
||||
d.translated = lambda l: [w ^ (1 if target in l else 0) for w in d.reference(l)]
|
||||
small = d.shrink(lines)
|
||||
finally:
|
||||
d.translated = real
|
||||
kept = [line for line in small if not line.startswith(("v_mov_b32_e32", "s_mov_b32"))]
|
||||
self.assertIn(target, kept)
|
||||
self.assertLessEqual(len(kept), 2) # the target (and its compare, for a v_cndmask)
|
||||
|
||||
|
||||
class Cli(unittest.TestCase):
|
||||
def main(self, *argv):
|
||||
out = io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(io.StringIO()):
|
||||
try:
|
||||
rc = d.main(list(argv))
|
||||
except SystemExit as e:
|
||||
rc = e.code
|
||||
return rc, out.getvalue()
|
||||
|
||||
def test_exit_codes(self):
|
||||
rc, out = self.main("--list-ops")
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertIn("v_perm_b32", out)
|
||||
self.assertEqual(self.main("--ops", "v_nope")[0], 2)
|
||||
self.assertEqual(self.main("--oracle", "gpu")[0], 2)
|
||||
rc, out = self.main("--programs", "5", "--seed", "1")
|
||||
self.assertEqual(rc, 0, out)
|
||||
real = d.translated
|
||||
try:
|
||||
d.translated = lambda l: [w ^ 1 for w in d.reference(l)]
|
||||
rc, out = self.main("--programs", "3", "--seed", "1", "--ops", "v_xor_b32")
|
||||
finally:
|
||||
d.translated = real
|
||||
self.assertEqual(rc, 1)
|
||||
self.assertIn("MISMATCH", out)
|
||||
|
||||
def test_hardware_oracle_without_the_tools_exits_77(self):
|
||||
env = dict(os.environ, LEONIDA_AMD_CLANG=str(TOOL.parent / "no-such-clang.exe"))
|
||||
r = subprocess.run([sys.executable, "-I", str(TOOL), "--oracle", "hw", "--programs", "3"],
|
||||
capture_output=True, text=True, env=env)
|
||||
self.assertEqual(r.returncode, 77, r.stdout + r.stderr)
|
||||
self.assertIn("unavailable", r.stdout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
608
tools/vint_diff.py
Normal file
608
tools/vint_diff.py
Normal file
@ -0,0 +1,608 @@
|
||||
r"""Differential fuzzing of 32-bit integer VALU in every encoding and source kind. Standard library only.
|
||||
|
||||
Generates seeded straight-line programs of integer vector instructions, one lane, runs them through
|
||||
|
||||
* the reference below, written from the RDNA2 ISA guide's pseudocode for each opcode, and
|
||||
* tools/rdna2spirv.py, executing the SPIR-V with tests/semantic/spirv_mini_interp.py (lane 0 of a wave32),
|
||||
|
||||
and compares four exported words bit for bit (v8..v11). The reference does not share code with the translator, with
|
||||
tools/cpu_ref.py or with the other fuzzers. tools/shader_diff.py covers many of the same opcodes with VGPR sources in
|
||||
their e32 form; this one puts every legal source kind in every legal position and adds the VOP3-only opcodes.
|
||||
|
||||
What is modelled, per instruction:
|
||||
* encodings: VOP1 and VOP2 opcodes as e32 and as e64, the VOP3-only opcodes (OPS, --list-ops), and VOPC e64
|
||||
into an SGPR mask (s20, s21) or VCC followed by v_cndmask_b32_e64 on it (one lane: the mask is that lane's bit).
|
||||
* sources: VGPRs v0..v7 and v12..v19, SGPRs s4..s19 (seeded with s_mov_b32, so the translator knows them as
|
||||
constants), integer inline constants -16..64, float inline constants (0.5, 1.0, 2.0, 4.0, their negatives and
|
||||
1/(2*pi)) as their f32 bits, and 32-bit literals. An e32 VOP2 has a VGPR in src1 and a literal only in src0; a
|
||||
VOP3 form has at most one literal and at most two scalar values (SGPRs and the literal: the GFX10 constant bus).
|
||||
* v_mad_u32_u16 / v_mad_i32_i16 with op_sel on the two 16-bit sources (the high halves). A literal there has 16
|
||||
bits (zero-extended: its high half is 0); a float inline constant is its f32 bits (high half 0x3f00 for 0.5).
|
||||
* v_bfe_u32 / v_bfe_i32: offset and width are their low 5 bits, and the field stops at bit 31
|
||||
(width = min(width, 32 - offset), as for s_bfe in tools/salu_diff.py).
|
||||
* v_perm_b32: the selector bytes 0..7 pick a byte of {src0, src1}, 8..11 replicate a sign bit, 12 is 0x00,
|
||||
13 and up 0xff.
|
||||
Every rule was checked against an RDNA2 GPU (AMD Radeon iGPU, gfx1036, Windows driver 26.10.1) with --oracle hw.
|
||||
|
||||
Not generated: VOP3 neg/abs/clamp/omod on integer ops, DPP and SDWA forms (tools/sdwa_diff.py), 16-bit and packed
|
||||
ops, carries and anything that depends on other lanes (tools/wint_diff.py), 64-bit ops.
|
||||
|
||||
Known divergences (KNOWN_DIVERGENCES): by default the generator leaves out the forms where the translator is known to
|
||||
differ from the hardware, so that a run shows new problems only; --known puts them back (a hardware-only run always
|
||||
has them):
|
||||
* mask-as-data: a lane mask written by a compare (s20, s21, vcc_lo) read as a 32-bit VALU source. The hardware
|
||||
reads the wave's bits (here 0 or 1), the translator its per-lane view of the mask, 0 or 0xffffffff (#293).
|
||||
|
||||
With --oracle hw the same programs also run natively on an AMD GPU (tools/hw_oracle/), one lane of a wave32 compute
|
||||
dispatch, and the four words are compared with the reference: that validates the reference itself. Without the
|
||||
oracle tools the run exits 77.
|
||||
|
||||
Usage:
|
||||
python -I tools/vint_diff.py [--programs N] [--seed S] [--max-ops N] [--ops v_add3_u32,v_perm_b32,cmp]
|
||||
python -I tools/vint_diff.py --no-literals # sources without 32-bit literals
|
||||
python -I tools/vint_diff.py --known # also the forms in KNOWN_DIVERGENCES
|
||||
python -I tools/vint_diff.py --oracle hw # hardware vs reference only
|
||||
python -I tools/vint_diff.py --oracle translator,hw
|
||||
python -I tools/vint_diff.py --list-ops
|
||||
python -I tools/vint_diff.py --self-test
|
||||
|
||||
Exit 0 when every program matches, 1 on a mismatch (the shortest failing program is printed after removing
|
||||
instructions that do not matter), 2 on a harness error, 77 when the requested hardware oracle is unavailable.
|
||||
Skips are counted with their reasons.
|
||||
"""
|
||||
import argparse
|
||||
import gc
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
import tempfile
|
||||
import warnings
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.dont_write_bytecode = True
|
||||
if HERE not in sys.path:
|
||||
sys.path.insert(0, HERE)
|
||||
SEM_DIR = os.path.join(os.path.dirname(HERE), "tests", "semantic")
|
||||
if SEM_DIR not in sys.path:
|
||||
sys.path.insert(0, SEM_DIR)
|
||||
|
||||
M32 = 0xFFFFFFFF
|
||||
STUB_DXBC = b"DXBC" + bytes(24) + struct.pack("<I", 0) + bytes(4)
|
||||
|
||||
VREGS = tuple(range(0, 8)) + tuple(range(12, 20)) # data VGPRs; v8..v11 are the observation
|
||||
SREGS = tuple(range(4, 20)) # data SGPRs
|
||||
MASKS = ("s20", "s21", "vcc_lo")
|
||||
N_SEED = len(VREGS) + len(SREGS)
|
||||
N_OBS = 4
|
||||
INLINE_F = {"0.5": 0x3F000000, "-0.5": 0xBF000000, "1.0": 0x3F800000, "-1.0": 0xBF800000, "2.0": 0x40000000,
|
||||
"-2.0": 0xC0000000, "4.0": 0x40800000, "-4.0": 0xC0800000, "0.15915494": 0x3E22F983}
|
||||
INTERESTING = (0, 1, 2, 31, 32, 33, 0x7FFFFFFF, 0x80000000, M32, 0xFFFFFFFE, 0xFFFF, 0x10000, 0x8000,
|
||||
0x00FFFFFF, 0x00800000, 0xFF800000, 0x12345678, 0xDEADBEEF, 0x55555555, 0xAAAAAAAA, 0x0C0D0E0F,
|
||||
0x03020100, 0x07060504, 0x0B0A0908, 0x01000302)
|
||||
|
||||
|
||||
class Skip(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class RefError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def s32(x):
|
||||
x &= M32
|
||||
return x - (1 << 32) if x >> 31 else x
|
||||
|
||||
|
||||
def sx(x, bits):
|
||||
x &= (1 << bits) - 1
|
||||
return x - (1 << bits) if x >> (bits - 1) else x
|
||||
|
||||
|
||||
def ffbh_u(a):
|
||||
return M32 if a == 0 else 32 - a.bit_length()
|
||||
|
||||
|
||||
def ffbh_i(a):
|
||||
if a in (0, M32):
|
||||
return M32
|
||||
return 32 - ((~a & M32) if a >> 31 else a).bit_length()
|
||||
|
||||
|
||||
def ffbl(a):
|
||||
return M32 if a == 0 else (a & -a).bit_length() - 1
|
||||
|
||||
|
||||
def bfe(a, off, w, signed):
|
||||
off, w = off & 31, w & 31
|
||||
if w == 0:
|
||||
return 0
|
||||
w = min(w, 32 - off)
|
||||
f = (a >> off) & ((1 << w) - 1)
|
||||
return (sx(f, w) if signed else f) & M32
|
||||
|
||||
|
||||
def perm(s0, s1, sel):
|
||||
x = (s0 << 32) | s1
|
||||
out = 0
|
||||
for i in range(4):
|
||||
b = (sel >> (8 * i)) & 0xFF
|
||||
if b >= 13:
|
||||
v = 0xFF
|
||||
elif b == 12:
|
||||
v = 0
|
||||
elif b >= 8:
|
||||
v = 0xFF if (x >> (16 * (b - 8) + 15)) & 1 else 0 # 8: bit 15, 9: bit 31, 10: bit 47, 11: bit 63
|
||||
else:
|
||||
v = (x >> (8 * b)) & 0xFF
|
||||
out |= v << (8 * i)
|
||||
return out
|
||||
|
||||
|
||||
def sat(v, lo, hi):
|
||||
return max(lo, min(hi, v))
|
||||
|
||||
|
||||
# name -> (encoding, nsrc, fn on 32-bit sources). vop1 / vop2: generated as e32 and e64; vop3: VOP3-only.
|
||||
OPS = {
|
||||
"v_mov_b32": ("vop1", 1, lambda a: a),
|
||||
"v_not_b32": ("vop1", 1, lambda a: ~a & M32),
|
||||
"v_bfrev_b32": ("vop1", 1, lambda a: int(f"{a:032b}"[::-1], 2)),
|
||||
"v_ffbh_u32": ("vop1", 1, ffbh_u),
|
||||
"v_ffbh_i32": ("vop1", 1, ffbh_i),
|
||||
"v_ffbl_b32": ("vop1", 1, ffbl),
|
||||
"v_add_nc_u32": ("vop2", 2, lambda a, b: (a + b) & M32),
|
||||
"v_sub_nc_u32": ("vop2", 2, lambda a, b: (a - b) & M32),
|
||||
"v_subrev_nc_u32": ("vop2", 2, lambda a, b: (b - a) & M32),
|
||||
"v_and_b32": ("vop2", 2, lambda a, b: a & b),
|
||||
"v_or_b32": ("vop2", 2, lambda a, b: a | b),
|
||||
"v_xor_b32": ("vop2", 2, lambda a, b: a ^ b),
|
||||
"v_xnor_b32": ("vop2", 2, lambda a, b: ~(a ^ b) & M32),
|
||||
"v_lshlrev_b32": ("vop2", 2, lambda a, b: (b << (a & 31)) & M32),
|
||||
"v_lshrrev_b32": ("vop2", 2, lambda a, b: b >> (a & 31)),
|
||||
"v_ashrrev_i32": ("vop2", 2, lambda a, b: (s32(b) >> (a & 31)) & M32),
|
||||
"v_min_i32": ("vop2", 2, lambda a, b: min(s32(a), s32(b)) & M32),
|
||||
"v_max_i32": ("vop2", 2, lambda a, b: max(s32(a), s32(b)) & M32),
|
||||
"v_min_u32": ("vop2", 2, min),
|
||||
"v_max_u32": ("vop2", 2, max),
|
||||
"v_mul_i32_i24": ("vop2", 2, lambda a, b: (sx(a, 24) * sx(b, 24)) & M32),
|
||||
"v_mul_hi_i32_i24": ("vop2", 2, lambda a, b: ((sx(a, 24) * sx(b, 24)) >> 32) & M32),
|
||||
"v_mul_u32_u24": ("vop2", 2, lambda a, b: ((a & 0xFFFFFF) * (b & 0xFFFFFF)) & M32),
|
||||
"v_mul_hi_u32_u24": ("vop2", 2, lambda a, b: ((a & 0xFFFFFF) * (b & 0xFFFFFF)) >> 32),
|
||||
"v_add3_u32": ("vop3", 3, lambda a, b, c: (a + b + c) & M32),
|
||||
"v_lshl_add_u32": ("vop3", 3, lambda a, b, c: ((a << (b & 31)) + c) & M32),
|
||||
"v_add_lshl_u32": ("vop3", 3, lambda a, b, c: (((a + b) & M32) << (c & 31)) & M32),
|
||||
"v_lshl_or_b32": ("vop3", 3, lambda a, b, c: ((a << (b & 31)) & M32) | c),
|
||||
"v_and_or_b32": ("vop3", 3, lambda a, b, c: (a & b) | c),
|
||||
"v_or3_b32": ("vop3", 3, lambda a, b, c: a | b | c),
|
||||
"v_xor3_b32": ("vop3", 3, lambda a, b, c: a ^ b ^ c),
|
||||
"v_xad_u32": ("vop3", 3, lambda a, b, c: ((a ^ b) + c) & M32),
|
||||
"v_bfe_u32": ("vop3", 3, lambda a, b, c: bfe(a, b, c, False)),
|
||||
"v_bfe_i32": ("vop3", 3, lambda a, b, c: bfe(a, b, c, True)),
|
||||
"v_bfi_b32": ("vop3", 3, lambda a, b, c: (a & b) | (~a & c & M32)),
|
||||
"v_alignbit_b32": ("vop3", 3, lambda a, b, c: (((a << 32) | b) >> (c & 31)) & M32),
|
||||
"v_alignbyte_b32": ("vop3", 3, lambda a, b, c: (((a << 32) | b) >> (8 * (c & 3))) & M32),
|
||||
"v_perm_b32": ("vop3", 3, perm),
|
||||
"v_min3_i32": ("vop3", 3, lambda a, b, c: min(s32(a), s32(b), s32(c)) & M32),
|
||||
"v_max3_i32": ("vop3", 3, lambda a, b, c: max(s32(a), s32(b), s32(c)) & M32),
|
||||
"v_med3_i32": ("vop3", 3, lambda a, b, c: sorted((s32(a), s32(b), s32(c)))[1] & M32),
|
||||
"v_min3_u32": ("vop3", 3, lambda a, b, c: min(a, b, c)),
|
||||
"v_max3_u32": ("vop3", 3, lambda a, b, c: max(a, b, c)),
|
||||
"v_med3_u32": ("vop3", 3, lambda a, b, c: sorted((a, b, c))[1]),
|
||||
"v_mul_lo_u32": ("vop3", 2, lambda a, b: (a * b) & M32),
|
||||
"v_mul_hi_u32": ("vop3", 2, lambda a, b: (a * b) >> 32),
|
||||
"v_mul_hi_i32": ("vop3", 2, lambda a, b: ((s32(a) * s32(b)) >> 32) & M32),
|
||||
"v_mad_u32_u24": ("vop3", 3, lambda a, b, c: ((a & 0xFFFFFF) * (b & 0xFFFFFF) + c) & M32),
|
||||
"v_mad_i32_i24": ("vop3", 3, lambda a, b, c: (sx(a, 24) * sx(b, 24) + s32(c)) & M32),
|
||||
"v_mad_u32_u16": ("vop3", 3, lambda a, b, c: ((a & 0xFFFF) * (b & 0xFFFF) + c) & M32),
|
||||
"v_mad_i32_i16": ("vop3", 3, lambda a, b, c: (sx(a, 16) * sx(b, 16) + s32(c)) & M32),
|
||||
"v_bcnt_u32_b32": ("vop3", 2, lambda a, b: (bin(a).count("1") + b) & M32),
|
||||
"v_bfm_b32": ("vop3", 2, lambda a, b: (((1 << (a & 31)) - 1) << (b & 31)) & M32),
|
||||
"v_cvt_pk_u16_u32": ("vop3", 2, lambda a, b: min(a, 0xFFFF) | (min(b, 0xFFFF) << 16)),
|
||||
"v_cvt_pk_i16_i32": ("vop3", 2, lambda a, b: (sat(s32(a), -32768, 32767) & 0xFFFF)
|
||||
| ((sat(s32(b), -32768, 32767) & 0xFFFF) << 16)),
|
||||
}
|
||||
OP_SEL_OPS = ("v_mad_u32_u16", "v_mad_i32_i16")
|
||||
CMPS = {
|
||||
"eq_u32": lambda a, b: a == b, "ne_u32": lambda a, b: a != b, "lt_u32": lambda a, b: a < b,
|
||||
"le_u32": lambda a, b: a <= b, "gt_u32": lambda a, b: a > b, "ge_u32": lambda a, b: a >= b,
|
||||
"lt_i32": lambda a, b: s32(a) < s32(b), "le_i32": lambda a, b: s32(a) <= s32(b),
|
||||
"gt_i32": lambda a, b: s32(a) > s32(b), "ge_i32": lambda a, b: s32(a) >= s32(b),
|
||||
"eq_i32": lambda a, b: a == b, "ne_i32": lambda a, b: a != b,
|
||||
"f_u32": lambda a, b: False, "t_u32": lambda a, b: True, "f_i32": lambda a, b: False, "t_i32": lambda a, b: True,
|
||||
}
|
||||
ALL_OPS = sorted(OPS) + ["cmp"] # cmp: a VOPC e64 into a mask, then v_cndmask_b32_e64 on it
|
||||
|
||||
KNOWN = False # also generate the forms in KNOWN_DIVERGENCES (--known; always on for a hardware-only run)
|
||||
LITERALS = True
|
||||
KNOWN_DIVERGENCES = {
|
||||
"mask-as-data": "a compare's lane mask (s20, s21, vcc_lo) read as a 32-bit VALU source: the hardware reads the "
|
||||
"wave's bits, the translator 0 or 0xffffffff per lane (#293)",
|
||||
}
|
||||
|
||||
|
||||
# ---- program generation -------------------------------------------------------------------------------------
|
||||
|
||||
def rand_value(rng):
|
||||
r = rng.random()
|
||||
if r < 0.4:
|
||||
return rng.choice(INTERESTING)
|
||||
if r < 0.55:
|
||||
return rng.randrange(0, 64)
|
||||
return rng.getrandbits(32)
|
||||
|
||||
|
||||
class Gen:
|
||||
def __init__(self, rng):
|
||||
self.r = rng
|
||||
self.masks = set() # mask registers a compare has written (read as data only with KNOWN)
|
||||
|
||||
def vreg(self):
|
||||
return f"v{self.r.choice(VREGS)}"
|
||||
|
||||
def const(self, lit_ok, lit16=False):
|
||||
r = self.r
|
||||
k = r.random()
|
||||
if k < 0.2:
|
||||
return r.choice(tuple(INLINE_F))
|
||||
if k < 0.6 or not lit_ok:
|
||||
return str(r.randint(-16, 64))
|
||||
v = rand_value(r) & (0xFFFF if lit16 else M32)
|
||||
return f"0x{v:x}" if not -16 <= s32(v) <= 64 else ("0x1234" if lit16 else "0x12345678")
|
||||
|
||||
def srcs(self, n, vop3, e32_vop2=False, lit16=0):
|
||||
"""n sources. e32: src0 any kind (a literal only there), src1 a VGPR. VOP3: at most one literal and at most
|
||||
two scalar values, the literal counted among them. The first lit16 sources are 16-bit operands: a literal
|
||||
there must fit 16 bits (the assembler rejects a wider one)."""
|
||||
r = self.r
|
||||
out, bus, lit = [], set(), False
|
||||
for i in range(n):
|
||||
k = r.random()
|
||||
if (e32_vop2 and i == 1) or k < 0.5:
|
||||
out.append(self.vreg())
|
||||
elif k < 0.75:
|
||||
if KNOWN and self.masks and r.random() < 0.25:
|
||||
s = r.choice(sorted(self.masks))
|
||||
else:
|
||||
s = f"s{r.choice(SREGS)}"
|
||||
if s not in bus and len(bus) >= 2:
|
||||
out.append(self.vreg())
|
||||
continue
|
||||
bus.add(s)
|
||||
out.append(s)
|
||||
else:
|
||||
lit_ok = LITERALS and not lit and len(bus) < 2 and (vop3 or i == 0)
|
||||
c = self.const(lit_ok, i < lit16)
|
||||
if c.startswith("0x"):
|
||||
lit = True
|
||||
bus.add("lit")
|
||||
out.append(c)
|
||||
return out
|
||||
|
||||
def op(self, ops):
|
||||
r = self.r
|
||||
name = r.choice(ops)
|
||||
d = r.choice(VREGS)
|
||||
if name == "cmp":
|
||||
c = r.choice(tuple(CMPS))
|
||||
m = r.choice(MASKS)
|
||||
a, b = self.srcs(2, True)
|
||||
# the mask is on the constant bus of the v_cndmask too: its sources are VGPRs or inline constants
|
||||
sel = [x if not x.startswith(("s", "0x")) else self.vreg() for x in self.srcs(2, True)]
|
||||
self.masks.add(m)
|
||||
return [f"v_cmp_{c}_e64 {m}, {a}, {b}", f"v_cndmask_b32_e64 v{d}, {sel[0]}, {sel[1]}, {m}"]
|
||||
enc, n, _ = OPS[name]
|
||||
if enc == "vop1":
|
||||
if r.random() < 0.5:
|
||||
return [f"{name}_e32 v{d}, {self.srcs(1, False)[0]}"]
|
||||
return [f"{name}_e64 v{d}, {self.srcs(1, True)[0]}"]
|
||||
if enc == "vop2":
|
||||
if r.random() < 0.5:
|
||||
return [f"{name}_e32 v{d}, {', '.join(self.srcs(2, False, True))}"]
|
||||
return [f"{name}_e64 v{d}, {', '.join(self.srcs(2, True))}"]
|
||||
line = f"{name} v{d}, {', '.join(self.srcs(n, True, lit16=2 if name in OP_SEL_OPS else 0))}"
|
||||
if name in OP_SEL_OPS and r.random() < 0.5:
|
||||
line += f" op_sel:[{r.randint(0, 1)},{r.randint(0, 1)},0,0]"
|
||||
return [line]
|
||||
|
||||
|
||||
def seeds(rng):
|
||||
return ([f"v_mov_b32_e32 v{v}, 0x{rand_value(rng):x}" for v in VREGS]
|
||||
+ [f"s_mov_b32 s{s}, 0x{rand_value(rng):x}" for s in SREGS])
|
||||
|
||||
|
||||
def observe(regs):
|
||||
return [f"v_mov_b32_e32 v{8 + i}, v{r}" for i, r in enumerate(regs)]
|
||||
|
||||
|
||||
def generate(rng, n_ops, ops):
|
||||
gen = Gen(rng)
|
||||
body = []
|
||||
for _ in range(n_ops):
|
||||
body += gen.op(ops)
|
||||
written = []
|
||||
for line in reversed(body):
|
||||
if line.startswith("v_cmp"):
|
||||
continue
|
||||
dst = int(line.split()[1].rstrip(",")[1:])
|
||||
if dst not in written:
|
||||
written.append(dst)
|
||||
regs = written[:4]
|
||||
regs += [rng.choice(VREGS) for _ in range(4 - len(regs))]
|
||||
return seeds(rng) + body + observe(regs)
|
||||
|
||||
|
||||
# ---- the reference interpreter --------------------------------------------------------------------------------
|
||||
|
||||
_OP_SEL = re.compile(r"\s+op_sel:\[([01,]+)\]\s*$")
|
||||
|
||||
|
||||
def value(tok, st):
|
||||
if re.fullmatch(r"v\d+", tok):
|
||||
return st["v"].get(int(tok[1:]), 0)
|
||||
if re.fullmatch(r"s\d+|vcc_lo", tok):
|
||||
if tok not in st["s"]:
|
||||
raise RefError(f"{tok} read before it is written")
|
||||
return st["s"][tok]
|
||||
if tok in INLINE_F:
|
||||
return INLINE_F[tok]
|
||||
return int(tok, 0) & M32
|
||||
|
||||
|
||||
def reference(lines):
|
||||
"""Run the program; returns the four exported words."""
|
||||
st = {"v": {}, "s": {}}
|
||||
for line in lines:
|
||||
op, _, rest = line.partition(" ")
|
||||
sel = None
|
||||
m = _OP_SEL.search(rest)
|
||||
if m:
|
||||
sel = [int(x) for x in m.group(1).split(",")]
|
||||
rest = rest[:m.start()]
|
||||
args = [a.strip() for a in rest.split(",")]
|
||||
if op == "s_mov_b32":
|
||||
st["s"][args[0]] = value(args[1], st)
|
||||
continue
|
||||
if op.startswith("v_cmp_"):
|
||||
c = op[len("v_cmp_"):-len("_e64")]
|
||||
st["s"][args[0]] = int(CMPS[c](value(args[1], st), value(args[2], st))) # this lane's bit
|
||||
continue
|
||||
if op == "v_cndmask_b32_e64":
|
||||
st["v"][int(args[0][1:])] = value(args[2] if value(args[3], st) & 1 else args[1], st)
|
||||
continue
|
||||
base = op[:-4] if op.endswith(("_e32", "_e64")) else op
|
||||
if base not in OPS:
|
||||
raise RefError(f"{op} is not modelled")
|
||||
vals = [value(a, st) for a in args[1:]]
|
||||
if sel:
|
||||
vals = [v >> 16 if i < 2 and sel[i] else v for i, v in enumerate(vals)]
|
||||
st["v"][int(args[0][1:])] = OPS[base][2](*vals) & M32
|
||||
return [st["v"].get(8 + i, 0) for i in range(4)]
|
||||
|
||||
|
||||
# ---- the translator side -----------------------------------------------------------------------------------
|
||||
|
||||
def translated(lines, name="ps_vint"):
|
||||
"""Translate and execute; returns the four exported words, or raises Skip."""
|
||||
import rdna2spirv
|
||||
from spirv_mini_interp import run_fragment
|
||||
with tempfile.TemporaryDirectory(prefix="vint-diff-") as tmp:
|
||||
dis = os.path.join(tmp, "disasm")
|
||||
os.mkdir(dis)
|
||||
path = os.path.join(dis, name + ".s")
|
||||
with open(path, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write(f"; {name} vint_diff case\n")
|
||||
for line in lines:
|
||||
f.write("\t" + line + "\n")
|
||||
f.write("\texp mrt0 v8, v9, v10, v11 done vm\n\ts_endpgm\n")
|
||||
with open(os.path.join(tmp, name + ".dxbc"), "wb") as f:
|
||||
f.write(STUB_DXBC)
|
||||
try:
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", ResourceWarning)
|
||||
spvasm = rdna2spirv.translate(path)
|
||||
gc.collect()
|
||||
except rdna2spirv.Unsupported as e:
|
||||
raise Skip(f"translator unsupported: {e}") from None
|
||||
try:
|
||||
out = run_fragment(spvasm)
|
||||
except Exception as e: # noqa: BLE001 - an interpreter gap is a counted skip, never a pass
|
||||
raise Skip(f"mini_interp: {type(e).__name__}: {str(e)[:80]}") from None
|
||||
return [w & M32 for w in out["%out_mrt0"]]
|
||||
|
||||
|
||||
def hw_kernel(lines):
|
||||
"""A raw kernel for tools/hw_oracle: the program on one lane, then v8..v11 stored at the output pointer."""
|
||||
from hw_oracle import asm
|
||||
return asm.Kernel(list(lines) + ["v_mov_b32 v0, 0", "global_store_b128 v0, v[8:11], s[2:3]", "s_endpgm"],
|
||||
wave=32, threads=1, words=4, raw=True)
|
||||
|
||||
|
||||
# ---- comparison, shrinking, the run --------------------------------------------------------------------------
|
||||
|
||||
def _diff(lines, want, got, label):
|
||||
bad = [k for k in range(4) if want[k] != got[k]]
|
||||
if not bad:
|
||||
return None
|
||||
i = bad[0]
|
||||
return f"word {i} ({lines[-N_OBS + i].split(', ')[1]}): reference 0x{want[i]:08x}, {label} 0x{got[i]:08x}"
|
||||
|
||||
|
||||
def check(lines, backends=("translator",), hw=None):
|
||||
"""None on a match, else a description. Raises Skip (every backend skipped) / RefError.
|
||||
hw is the hardware result for this program (four words or a str reason), or a callable lines -> that."""
|
||||
want = reference(lines)
|
||||
problems, skips = [], []
|
||||
if "translator" in backends:
|
||||
try:
|
||||
problems.append(_diff(lines, want, translated(lines), "translated"))
|
||||
except Skip as e:
|
||||
skips.append(e)
|
||||
if "hw" in backends:
|
||||
got = hw(lines) if callable(hw) else hw
|
||||
if isinstance(got, str):
|
||||
skips.append(Skip(f"hardware: {got}"))
|
||||
else:
|
||||
problems.append(_diff(lines, want, got, "hardware"))
|
||||
if skips and len(skips) == len(backends):
|
||||
raise skips[0]
|
||||
return "; ".join(p for p in problems if p) or None
|
||||
|
||||
|
||||
def shrink(lines, check_fn=check):
|
||||
"""Observe one register if that still fails, drop instructions (a compare and its v_cndmask together) while the
|
||||
program still mismatches, then the seeds of registers nothing reads."""
|
||||
def fails(trial):
|
||||
try:
|
||||
return bool(check_fn(trial))
|
||||
except (Skip, RefError):
|
||||
return False
|
||||
seed, body, obs = lines[:N_SEED], lines[N_SEED:-N_OBS], lines[-N_OBS:]
|
||||
for line in obs:
|
||||
one = [line.replace(line.split()[1], f"v{8 + i},") for i in range(4)]
|
||||
if fails(seed + body + one):
|
||||
obs = one
|
||||
break
|
||||
changed = True
|
||||
while changed:
|
||||
changed = False
|
||||
i = len(body) - 1
|
||||
while i >= 0:
|
||||
n = 2 if body[i].startswith("v_cndmask") and i and body[i - 1].startswith("v_cmp") else 1
|
||||
trial = body[:i + 1 - n] + body[i + 1:]
|
||||
if fails(seed + trial + obs):
|
||||
body, changed = trial, True
|
||||
i -= n
|
||||
used = set()
|
||||
for line in body + obs:
|
||||
used |= set(re.findall(r"\b[vs]\d+\b", line.partition(" ")[2]))
|
||||
seed = [line for line in seed if line.split()[1].rstrip(",") in used]
|
||||
return seed + body + obs
|
||||
|
||||
|
||||
def run_suite(programs, seed, max_ops, ops, backends=("translator",), oracle=None):
|
||||
counts = {"pass": 0, "mismatch": 0, "skip": {}}
|
||||
failures = []
|
||||
rng = random.Random(seed)
|
||||
progs = [generate(rng, rng.randrange(1, max_ops + 1), ops) for _ in range(programs)]
|
||||
hw = oracle.run([hw_kernel(p) for p in progs]) if "hw" in backends else [None] * programs
|
||||
for i, lines in enumerate(progs):
|
||||
try:
|
||||
problem = check(lines, backends, hw[i])
|
||||
except Skip as e:
|
||||
counts["skip"][str(e)] = counts["skip"].get(str(e), 0) + 1
|
||||
continue
|
||||
if problem is None:
|
||||
counts["pass"] += 1
|
||||
continue
|
||||
counts["mismatch"] += 1
|
||||
failures.append((i, problem, lines))
|
||||
return counts, failures
|
||||
|
||||
|
||||
def self_test():
|
||||
"""Harness canaries: a hand program matches, the reference gives hand-worked values, a bad form is a skip."""
|
||||
base = [f"v_mov_b32_e32 v{v}, 0x{0x01010101 * v:x}" for v in VREGS] + [f"s_mov_b32 s{s}, {s}" for s in SREGS]
|
||||
body = ["v_add3_u32 v1, v2, s4, 0x10", # 0x02020202 + 4 + 0x10
|
||||
"v_perm_b32 v3, v2, v3, 0x0c0a0400", # bytes: v3.b0, v2.b0, sign(bit 47), 0
|
||||
"v_cmp_gt_u32_e64 s20, v1, v2",
|
||||
"v_cndmask_b32_e64 v4, 1, 2, s20"]
|
||||
lines = base + body + observe([1, 3, 4, 2])
|
||||
ok = check(lines) is None
|
||||
worked = reference(lines) == [0x02020216, 0x00000203, 2, 0x02020202]
|
||||
try:
|
||||
translated(base + ["v_add_nc_u32_sdwa_dpp v1, v2, v3"] + observe([1, 2, 3, 4]))
|
||||
skipped = False
|
||||
except Skip:
|
||||
skipped = True
|
||||
print(f"self-test: match {ok}, hand values {worked}, unsupported form skipped {skipped}")
|
||||
return ok and worked and skipped
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(description="32-bit integer VALU differential fuzzing (all encodings and sources).")
|
||||
ap.add_argument("--programs", type=int, default=300)
|
||||
ap.add_argument("--seed", type=int, default=1)
|
||||
ap.add_argument("--max-ops", type=int, default=6)
|
||||
ap.add_argument("--ops", default=None, help="comma-separated opcodes (--list-ops; cmp = compare + v_cndmask)")
|
||||
ap.add_argument("--no-literals", action="store_true", help="no 32-bit literal sources")
|
||||
ap.add_argument("--list-ops", action="store_true")
|
||||
ap.add_argument("--self-test", action="store_true")
|
||||
ap.add_argument("--all-failures", action="store_true", help="print every failing program, not just the first")
|
||||
ap.add_argument("--known", action="store_true",
|
||||
help="also generate the forms where the translator is known to differ (KNOWN_DIVERGENCES; "
|
||||
"always on with --oracle hw alone)")
|
||||
ap.add_argument("--oracle", default="translator",
|
||||
help="comma-separated comparison targets: translator (default), hw (natively on an AMD GPU; see "
|
||||
"tools/hw_oracle/). 'hw' alone compares the hardware with the reference only")
|
||||
ap.add_argument("--hw-arch", default=None, help="GPU architecture to assemble for (default: detected)")
|
||||
a = ap.parse_args(argv)
|
||||
if a.programs < 1 or a.max_ops < 1:
|
||||
ap.error("--programs and --max-ops must be at least 1")
|
||||
backends = tuple(dict.fromkeys(t.strip() for t in a.oracle.split(",")))
|
||||
if not backends or any(t not in ("translator", "hw") for t in backends):
|
||||
ap.error("--oracle takes translator, hw or translator,hw")
|
||||
global KNOWN, LITERALS
|
||||
KNOWN = a.known or backends == ("hw",)
|
||||
LITERALS = not a.no_literals
|
||||
if a.list_ops:
|
||||
for name in ALL_OPS:
|
||||
if name == "cmp":
|
||||
print(f"{'cmp':20} VOPC e64 into s20/s21/vcc_lo + v_cndmask_b32_e64: {', '.join(sorted(CMPS))}")
|
||||
continue
|
||||
enc, n, _ = OPS[name]
|
||||
kind = "VOP3 only" if enc == "vop3" else f"{enc.upper()} e32/e64"
|
||||
print(f"{name:20} {kind}, {n} sources{', op_sel' if name in OP_SEL_OPS else ''}")
|
||||
return 0
|
||||
if a.self_test:
|
||||
return 0 if self_test() else 1
|
||||
if a.ops is not None:
|
||||
tokens = [t.strip() for t in a.ops.split(",")]
|
||||
if not any(tokens) or any(not t for t in tokens):
|
||||
ap.error("--ops must be a comma-separated list of opcodes")
|
||||
ops = sorted(set(tokens))
|
||||
else:
|
||||
ops = list(ALL_OPS)
|
||||
unknown = [o for o in ops if o not in ALL_OPS]
|
||||
if unknown:
|
||||
ap.error("unknown ops: " + ", ".join(unknown))
|
||||
oracle = None
|
||||
if "hw" in backends:
|
||||
from hw_oracle import runner
|
||||
try:
|
||||
oracle = runner.open_oracle(arch=a.hw_arch)
|
||||
except runner.Unavailable as e:
|
||||
print(f"hardware oracle unavailable: {e}")
|
||||
return 77
|
||||
print(f"hardware oracle: {oracle.device}")
|
||||
try:
|
||||
counts, failures = run_suite(a.programs, a.seed, a.max_ops, ops, backends, oracle)
|
||||
skips = sum(counts["skip"].values())
|
||||
print(f"{a.programs} programs (seed {a.seed}, {'+'.join(backends)}{', known' if KNOWN else ''}): "
|
||||
f"{counts['pass']} pass, {counts['mismatch']} mismatch, {skips} skipped")
|
||||
for reason, n in sorted(counts["skip"].items(), key=lambda kv: -kv[1]):
|
||||
print(f" skip x{n}: {reason}")
|
||||
|
||||
def check_fn(lines):
|
||||
return check(lines, backends, (lambda l: oracle.run([hw_kernel(l)])[0]) if oracle else None)
|
||||
for i, problem, lines in failures[: None if a.all_failures else 1]:
|
||||
small = shrink(lines, check_fn)
|
||||
print(f"MISMATCH program {i}: {check_fn(small) or problem}")
|
||||
print("\n".join("\t" + line for line in small))
|
||||
except (RefError, RuntimeError) as e:
|
||||
print(f"harness error: {e}")
|
||||
return 2
|
||||
finally:
|
||||
if oracle:
|
||||
oracle.close()
|
||||
return 1 if failures else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode
|
||||
sys.stdout.reconfigure(errors="replace")
|
||||
except (AttributeError, ValueError, OSError):
|
||||
pass
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user