upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/pk16_diff.py
blob: 6b75caf286a78d87bf596a13c45ce18bab27cdf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
r"""Differential fuzzing of packed and 16-bit integer VALU (v_pk_* integer ops, 16-bit VOP3 integer ops). Standard
library only.

Generates seeded straight-line programs of 16-bit 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/valu_fuzz.py --family f16 runs some of the same opcodes on the GPU
with VGPR sources; this one adds the packed add/sub/mul/min/max/mad, the signed add/sub, min3/max3/med3 and mad,
clamp, and SGPR, inline-constant and literal sources in every position the encoding allows.

What is modelled, per instruction:
  * v_pk_* (VOP3P, OPS_PK): each result half is the op on the halves its sources select: op_sel bit i picks the
    half of source i for the low result, op_sel_hi bit i for the high result (default op_sel:[0,..],
    op_sel_hi:[1,..]). clamp saturates the exact result of add/sub/mad to [0, 0xffff] or [-32768, 32767];
    without it the result wraps to 16 bits. Shift amounts are their low 4 bits.
  * 16-bit VOP3 ops (OPS_16): op_sel bits 0..n-1 pick source halves, bit n (the last) the destination half. The
    result is written to that half and the other half of vD is kept. clamp as above (v_add_nc_*16, v_sub_nc_*16,
    v_mad_*16). On GFX10 these are VOP3 only: the VOP2 forms of v_mul_lo_u16, v_min/max_u16/i16 and the 16-bit
    shifts do not exist (the assembler refuses v_mul_lo_u16_e32). LLVM prints their VOP3 form without a suffix
    and the translator tells it from a VOP2 write (which zeroes the high half) by the listing's encoding bytes.
    A program here has no encodings, so these are written with _e64.
  * sources: VGPRs v0..v7 and v12..v19, SGPRs s4..s19, integer inline constants -16..64, float inline constants
    (0.5, 1.0, 2.0, 4.0, their negatives, 1/(2*pi)) and literals, at most one literal and two scalar values per
    instruction (the GFX10 constant bus). A constant is its 32-bit value split into halves like a register: -16 is
    0xfff0 / 0xffff, 1.0 is 0x0000 / 0x3f80 (its f32 bits, not the f16 constant of the f16 ops), a literal of a
    16-bit VOP3 op has 16 bits (the assembler rejects a wider one), so its high half is 0.
Every rule was checked against an RDNA2 GPU (AMD Radeon iGPU, gfx1036, Windows driver 26.10.1) with --oracle hw.

Not generated: neg_lo/neg_hi on the integer packed ops (the translator rejects them, their meaning on integers is
unmeasured), clamp on min/max/mul_lo/shifts, DPP and SDWA forms (tools/sdwa_diff.py), v_mad_u32_u16 and the other
16x16->32 ops (tools/vint_diff.py), v_dot* (#2229), f16 ops (tools/valu_fuzz.py --family f16).

Known divergences (KNOWN_DIVERGENCES): none at present. 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). The last one, `pk-const-hi` (a constant in a packed add/sub/mul_lo/min/max/mad
source whose high half `op_sel` or `op_sel_hi` reads, which raised Unsupported until 2026-11-22), is measured and
handled (docs/design.md), so the generator produces it by default.

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/pk16_diff.py [--programs N] [--seed S] [--max-ops N] [--ops v_pk_mad_u16,v_med3_i16]
    python -I tools/pk16_diff.py --no-literals          # sources without literals
    python -I tools/pk16_diff.py --known                # also the forms in KNOWN_DIVERGENCES
    python -I tools/pk16_diff.py --oracle hw            # hardware vs reference only
    python -I tools/pk16_diff.py --oracle translator,hw
    python -I tools/pk16_diff.py --list-ops
    python -I tools/pk16_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)

M16 = 0xFFFF
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
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}
HALVES = (0, 1, 2, 15, 16, 17, 0x7FFF, 0x8000, 0x8001, 0xFFFF, 0xFFFE, 0x00FF, 0x0100, 0x5555, 0xAAAA, 0x1234,
          0xFF00, 0x4000, 0xC000, 0x00F0)


class Skip(Exception):
    pass


class RefError(Exception):
    pass


def sx16(x):
    x &= M16
    return x - 0x10000 if x & 0x8000 else x


def satu(v):
    return max(0, min(M16, v))


def sati(v):
    return max(-32768, min(32767, v))


def med3(a, b, c):
    return sorted((a, b, c))[1]


# name -> (nsrc, fn on 16-bit source halves -> exact integer, clamp fn or None). The result keeps its low 16 bits.
OPS_PK = {
    "v_pk_add_u16": (2, lambda a, b: a + b, satu),
    "v_pk_add_i16": (2, lambda a, b: sx16(a) + sx16(b), sati),
    "v_pk_sub_u16": (2, lambda a, b: a - b, satu),
    "v_pk_sub_i16": (2, lambda a, b: sx16(a) - sx16(b), sati),
    "v_pk_mul_lo_u16": (2, lambda a, b: a * b, None),
    "v_pk_min_u16": (2, min, None),
    "v_pk_max_u16": (2, max, None),
    "v_pk_min_i16": (2, lambda a, b: min(sx16(a), sx16(b)), None),
    "v_pk_max_i16": (2, lambda a, b: max(sx16(a), sx16(b)), None),
    "v_pk_lshlrev_b16": (2, lambda a, b: b << (a & 15), None),
    "v_pk_lshrrev_b16": (2, lambda a, b: b >> (a & 15), None),
    "v_pk_ashrrev_i16": (2, lambda a, b: sx16(b) >> (a & 15), None),
    "v_pk_mad_u16": (3, lambda a, b, c: a * b + c, satu),
    "v_pk_mad_i16": (3, lambda a, b, c: sx16(a) * sx16(b) + sx16(c), sati),
}
OPS_16 = {
    "v_add_nc_u16": (2, lambda a, b: a + b, satu),
    "v_sub_nc_u16": (2, lambda a, b: a - b, satu),
    "v_add_nc_i16": (2, lambda a, b: sx16(a) + sx16(b), sati),
    "v_sub_nc_i16": (2, lambda a, b: sx16(a) - sx16(b), sati),
    "v_mul_lo_u16": (2, lambda a, b: a * b, None),
    "v_min_u16": (2, min, None),
    "v_max_u16": (2, max, None),
    "v_min_i16": (2, lambda a, b: min(sx16(a), sx16(b)), None),
    "v_max_i16": (2, lambda a, b: max(sx16(a), sx16(b)), None),
    "v_lshlrev_b16": (2, lambda a, b: b << (a & 15), None),
    "v_lshrrev_b16": (2, lambda a, b: b >> (a & 15), None),
    "v_ashrrev_i16": (2, lambda a, b: sx16(b) >> (a & 15), None),
    "v_min3_u16": (3, min, None),
    "v_max3_u16": (3, max, None),
    "v_med3_u16": (3, med3, None),
    "v_min3_i16": (3, lambda a, b, c: min(sx16(a), sx16(b), sx16(c)), None),
    "v_max3_i16": (3, lambda a, b, c: max(sx16(a), sx16(b), sx16(c)), None),
    "v_med3_i16": (3, lambda a, b, c: med3(sx16(a), sx16(b), sx16(c)), None),
    "v_mad_u16": (3, lambda a, b, c: a * b + c, satu),
    "v_mad_i16": (3, lambda a, b, c: sx16(a) * sx16(b) + sx16(c), sati),
}
# GFX10 has only the VOP3 encoding of these (VOP2 names on earlier chips). LLVM prints them without a suffix and the
# translator then tells VOP3 from VOP2 by the listing's encoding bytes; a program here has none, so it says _e64.
E64 = ("v_mul_lo_u16", "v_min_u16", "v_max_u16", "v_min_i16", "v_max_i16", "v_lshlrev_b16", "v_lshrrev_b16",
       "v_ashrrev_i16")
ALL_OPS = sorted(OPS_PK) + sorted(OPS_16)

KNOWN = False   # also generate the forms in KNOWN_DIVERGENCES (--known; always on for a hardware-only run)
LITERALS = True
KNOWN_DIVERGENCES = {}   # forms left out by default and put back by --known; none at present


# ---- program generation -------------------------------------------------------------------------------------

def rand_value(rng):
    r = rng.random()
    if r < 0.5:
        return rng.choice(HALVES) | (rng.choice(HALVES) << 16)
    if r < 0.6:
        return rng.randrange(0, 64)
    return rng.getrandbits(32)


def is_const(tok):
    return not re.fullmatch(r"[vs]\d+", tok)


class Gen:
    def __init__(self, rng):
        self.r = rng

    def vreg(self):
        return f"v{self.r.choice(VREGS)}"

    def const(self, lit_ok, lit16):
        r = self.r
        k = r.random()
        if k < 0.25:
            return r.choice(tuple(INLINE_F))
        if k < 0.6 or not lit_ok:
            return str(r.randint(-16, 64))
        v = rand_value(r) & (M16 if lit16 else M32)
        if v <= 64:
            v = 0x1234 if lit16 else 0x12345678
        return f"0x{v:x}"

    def srcs(self, n, lit16, const_ok):
        """n sources: at most one literal and two scalar values (the literal counted among them). const_ok[i]: a
        constant may go in source i."""
        r = self.r
        out, bus, lit = [], set(), False
        for i in range(n):
            k = r.random()
            if k < 0.5:
                out.append(self.vreg())
            elif k < 0.75 or not const_ok[i]:
                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:
                c = self.const(LITERALS and not lit and len(bus) < 2, 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 in OPS_PK:
            n, _, cl = OPS_PK[name]
            sel = [int(r.random() < 0.3) for _ in range(n)]
            hi = [int(r.random() < 0.7) for _ in range(n)]
            # a constant source is its 32-bit dword and op_sel / op_sel_hi index that dword, high half included
            # (measured on gfx1036, docs/design.md), so a constant may go in any source
            srcs = self.srcs(n, False, [True] * n)
            line = f"{name} v{d}, {', '.join(srcs)}"
            if any(sel):
                line += f" op_sel:[{','.join(map(str, sel))}]"
            if not all(hi):
                line += f" op_sel_hi:[{','.join(map(str, hi))}]"
            if cl and r.random() < 0.3:
                line += " clamp"
            return [line]
        n, _, cl = OPS_16[name]
        line = f"{name}{'_e64' if name in E64 else ''} v{d}, {', '.join(self.srcs(n, True, [True] * n))}"
        sel = [int(r.random() < 0.3) for _ in range(n + 1)]
        if any(sel):
            line += f" op_sel:[{','.join(map(str, sel))}]"
        if cl and r.random() < 0.3:
            line += " clamp"
        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):
        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 --------------------------------------------------------------------------------

_MOD = re.compile(r"\s+(op_sel|op_sel_hi):\[([01,]+)\]|\s+(clamp)\b")


def parse(rest):
    """Operands and modifiers of one instruction."""
    mods = {}
    for m in _MOD.finditer(rest):
        if m.group(3):
            mods["clamp"] = True
        else:
            mods[m.group(1)] = [int(x) for x in m.group(2).split(",")]
    rest = _MOD.sub("", rest)
    return [a.strip() for a in rest.split(",")], mods


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+", 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]
    try:
        return int(tok, 0) & M32
    except ValueError:
        raise RefError(f"operand {tok!r}") from None


def half(v, hi):
    return (v >> 16) & M16 if hi else v & M16


def reference(lines):
    """Run the program; returns the four exported words."""
    st = {"v": {}, "s": {}}
    for line in lines:
        op, _, rest = line.partition(" ")
        args, mods = parse(rest)
        if op == "s_mov_b32":
            if len(args) != 2 or not re.fullmatch(r"s\d+", args[0]):
                raise RefError(f"{op}: bad operands {args!r}")
            st["s"][args[0]] = value(args[1], st)
            continue
        if not args or not re.fullmatch(r"v\d+", args[0]):
            raise RefError(f"{op}: bad destination {args[0]!r}" if args else f"{op}: missing destination")
        d = int(args[0][1:])
        vals = [value(a, st) for a in args[1:]]
        if op == "v_mov_b32_e32":
            if len(vals) != 1:
                raise RefError(f"{op}: expected 1 source, got {len(vals)}")
            st["v"][d] = vals[0]
            continue
        if op in OPS_PK:
            n, fn, cl = OPS_PK[op]
            if len(vals) != n:
                raise RefError(f"{op}: expected {n} sources, got {len(vals)}")
            if mods.get("clamp") and cl is None:
                raise RefError(f"{op}: clamp is not modelled")
            sel_lo = mods.get("op_sel", [0] * n)
            sel_hi = mods.get("op_sel_hi", [1] * n)
            if len(sel_lo) != n or len(sel_hi) != n:
                raise RefError(f"{op}: expected {n} op_sel/op_sel_hi entries")
            out = 0
            for k, sel in enumerate((sel_lo, sel_hi)):
                r = fn(*[half(v, sel[i]) for i, v in enumerate(vals)])
                if mods.get("clamp") and cl:
                    r = cl(r)
                out |= (r & M16) << (16 * k)
            st["v"][d] = out
            continue
        op = op[:-4] if op.endswith("_e64") and op[:-4] in E64 else op
        if op not in OPS_16:
            raise RefError(f"{op} is not modelled")
        n, fn, cl = OPS_16[op]
        if len(vals) != n:
            raise RefError(f"{op}: expected {n} sources, got {len(vals)}")
        if mods.get("clamp") and cl is None:
            raise RefError(f"{op}: clamp is not modelled")
        if "op_sel_hi" in mods:
            raise RefError(f"{op}: op_sel_hi is only valid on packed VOP3P ops")
        sel = mods.get("op_sel", [0] * (n + 1))
        if len(sel) != n + 1:
            raise RefError(f"{op}: expected {n + 1} op_sel entries, got {len(sel)}")
        r = fn(*[half(v, sel[i]) for i, v in enumerate(vals)])
        if mods.get("clamp") and cl:
            r = cl(r)
        old = st["v"].get(d, 0)
        st["v"][d] = ((old & M16) | ((r & M16) << 16)) if sel[n] else ((old & 0xFFFF0000) | (r & M16))
    return [st["v"].get(8 + i, 0) for i in range(4)]


# ---- the translator side -----------------------------------------------------------------------------------

def translated(lines, name="ps_pk16"):
    """Translate and execute; returns the four exported words, or raises Skip."""
    import rdna2spirv
    from spirv_mini_interp import run_fragment
    with tempfile.TemporaryDirectory(prefix="pk16-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}  pk16_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, wave=32):
    """A raw kernel for tools/hw_oracle: the program on one lane, then v8..v11 stored at the output pointer. wave 64
    runs it in a wave64 wave with the lane masks as 64-bit pairs."""
    from hw_oracle import asm
    if wave == 64:
        import vk_fuzz_run
        lines = [vk_fuzz_run.wave64_text(ln) for ln in lines]
    return asm.Kernel(list(lines) + ["v_mov_b32 v0, 0", "global_store_b128 v0, v[8:11], s[2:3]", "s_endpgm"],
                      wave=wave, threads=1, words=4, raw=True)


def hw_run(oracle, programs, wave=32):
    if oracle is None:
        raise RuntimeError("hardware oracle required for backend 'hw'")
    res = oracle.run([hw_kernel(p, wave) for p in programs])
    if not isinstance(res, (list, tuple)) or len(res) != len(programs):
        got = len(res) if isinstance(res, (list, tuple)) else type(res).__name__
        raise RuntimeError(f"hardware oracle returned {got} results for {len(programs)} programs")
    return res


# ---- comparison, shrinking, the run --------------------------------------------------------------------------

def _diff(lines, want, got, label):
    if not isinstance(got, (list, tuple)) or len(got) != 4:
        got_desc = f"{len(got)} words" if isinstance(got, (list, tuple)) else type(got).__name__
        raise RuntimeError(f"{label} returned {got_desc} (expected 4 words)")
    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 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
        for i in range(len(body) - 1, -1, -1):
            trial = body[:i] + body[i + 1:]
            if fails(seed + trial + obs):
                body, changed = trial, True
    used = set()
    for line in body:
        used |= set(re.findall(r"\b[vs]\d+\b", line))      # a 16-bit write keeps the other half of vD: it reads vD
    for line in 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, wave=32, build=None, work=None,
              vk_constants=False):
    counts = {"pass": 0, "mismatch": 0, "skip": {}, "vk": {}, "vk_failures": []}
    failures = []
    rng = random.Random(seed)
    progs = [generate(rng, rng.randrange(1, max_ops + 1), ops) for _ in range(programs)]
    hw = hw_run(oracle, progs, wave) if "hw" in backends else [None] * programs
    if "vk" in backends:
        import vk_fuzz_run
        counts["vk"], counts["vk_failures"] = vk_fuzz_run.judge(progs, hw, vk_fuzz_run.exact_verdict, wave=wave,
                                                                  build=build, work_parent=work, opaque=not vk_constants)
    backends = tuple(b for b in backends if b != "vk")
    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{0x00010001 * v:x}" for v in VREGS] + [f"s_mov_b32 s{s}, {s}" for s in SREGS]
    body = ["v_pk_add_u16 v1, v2, s4 op_sel:[1,0] op_sel_hi:[0,0]",    # lo: 2 + 4, hi: 2 + 4
            "v_pk_mad_i16 v3, v3, -1, v5 op_sel_hi:[1,0,1] clamp",     # -1 in both halves: 5 - 3
            "v_mad_u16 v4, v6, 0x8000, v7 op_sel:[0,0,0,1] clamp",     # 6 * 0x8000 + 7 saturates; high half
            "v_max3_i16 v2, v2, -16, s5 op_sel:[0,0,0,0]"]
    lines = base + body + observe([1, 3, 4, 2])
    ok = check(lines) is None
    worked = reference(lines) == [0x00060006, 0x00020002, 0xFFFF0004, 0x00020005]
    try:
        translated(base + ["v_pk_add_u16_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="Packed and 16-bit integer VALU differential fuzzing.")
    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)")
    ap.add_argument("--no-literals", action="store_true", help="no 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)")
    ap.add_argument("--wave", type=int, choices=(32, 64), default=32,
                    help="the wave size of the hardware kernels and of the vk programs (64 needs native 64-wide "
                         "subgroups; lane 0 is judged)")
    ap.add_argument("--build", default=os.path.join(os.path.dirname(HERE), "runtime", "build"),
                    help="with vk: the folder with vk_compute_test")
    ap.add_argument("--vk-constants", action="store_true",
                    help="with vk: keep the seeds as literal v_mov_b32 constants (the driver may fold them)")
    ap.add_argument("--work", default=None, help="with vk: a folder for the temporary files (VK_FUZZ_KEEP=1 keeps them)")
    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")
    if a.hw_arch is not None:
        a.hw_arch = a.hw_arch.strip()
        if not a.hw_arch:
            ap.error("--hw-arch must not be empty")
    if a.build is not None:
        a.build = a.build.strip()
        if not a.build:
            ap.error("--build must not be empty")
    if a.work is not None:
        a.work = a.work.strip()
        if not a.work:
            ap.error("--work must not be empty")
    backends = tuple(dict.fromkeys(t.strip() for t in a.oracle.split(",")))
    if not backends or any(t not in ("translator", "hw", "vk") for t in backends):
        ap.error("--oracle takes translator, hw, vk (with hw) or a comma-separated mix")
    if "vk" in backends and "hw" not in backends:
        ap.error("--oracle vk judges the translated programs against the hardware's words: use --oracle hw,vk")
    if a.list_ops:
        for name in ALL_OPS:
            if name in OPS_PK:
                n, _, cl = OPS_PK[name]
                print(f"{name:18} VOP3P, {n} sources, op_sel/op_sel_hi{', clamp' if cl else ''}")
            else:
                n, _, cl = OPS_16[name]
                print(f"{name:18} VOP3, {n} sources, op_sel (sources and destination half){', clamp' if cl 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}")
    global KNOWN, LITERALS
    prev_known, prev_literals = KNOWN, LITERALS
    KNOWN = a.known or backends == ("hw",)
    LITERALS = not a.no_literals
    try:
        counts, failures = run_suite(a.programs, a.seed, a.max_ops, ops, backends, oracle, a.wave, a.build, a.work,
                                     a.vk_constants)
        vk_bad = counts.pop("vk_failures")
        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}")

        want_vk = "vk" in backends
        if want_vk:
            print(f"translated on the Vulkan GPU (wave{a.wave}) against the hardware: "
                  + ", ".join(f"{k} {n}" for k, n in sorted(counts["vk"].items(), key=lambda kv: kv[0].startswith("skip"))))
            for i, detail, lines in vk_bad[: None if a.all_failures else 3]:
                print(f"VK MISMATCH program {i}: {detail}")
                print("\n".join("\t" + line for line in lines))
        backends = tuple(b for b in backends if b != "vk")

        def check_fn(lines):
            return check(lines, backends, (lambda l: hw_run(oracle, [l], a.wave)[0]) if oracle else None)
        for i, problem, lines in failures[: None if a.all_failures else 1]:
            small = shrink(lines, check_fn)
            try:
                detail = check_fn(small) or problem
            except Skip:
                detail = problem
            print(f"MISMATCH program {i}: {detail}")
            print("\n".join("\t" + line for line in small))
    except (RefError, RuntimeError) as e:
        print(f"harness error: {e}")
        return 2
    finally:
        KNOWN, LITERALS = prev_known, prev_literals
        if oracle:
            oracle.close()
    if failures or vk_bad:
        return 1
    if not counts["pass"]:   # nothing was compared: every program was skipped
        print("no program ran: every one was skipped")
        return 2
    if want_vk:
        import vk_fuzz_run
        if vk_fuzz_run.all_skipped(counts["vk"]):
            print("no Vulkan program ran: every one was skipped")
            return 2
    return 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())