diff options
Diffstat (limited to 'tools/mem_diff.py')
| -rw-r--r-- | tools/mem_diff.py | 204 |
1 files changed, 199 insertions, 5 deletions
diff --git a/tools/mem_diff.py b/tools/mem_diff.py index 1c7f60377..b6bae05fa 100644 --- a/tools/mem_diff.py +++ b/tools/mem_diff.py | |||
| @@ -19,6 +19,11 @@ below num_records for raw ones), is dword aligned, and uses a word3 with no form | |||
| 19 | s_buffer_load alignment truncation and swizzling depend on descriptor fields docs/design.md lists as not modelled, | 19 | s_buffer_load alignment truncation and swizzling depend on descriptor fields docs/design.md lists as not modelled, |
| 20 | so they are left to a GPU-confirmed follow-up instead of being guessed here. | 20 | so they are left to a GPU-confirmed follow-up instead of being guessed here. |
| 21 | 21 | ||
| 22 | Typed buffer loads (buffer_load_format_*, tbuffer_load_format_*) are decoded from the GFX10 format names in | ||
| 23 | tools/gfx10_formats.json, not from the translator's table. Two outcomes awaiting a hardware measurement are | ||
| 24 | counted apart instead of as mismatches: 'norm-ulp1' (a UNORM/SNORM result one ULP from the correctly rounded | ||
| 25 | value) and 'missing-w32' (the missing W of a 32-bit format: the translator returns 0, every other format 1). | ||
| 26 | |||
| 22 | Usage: | 27 | Usage: |
| 23 | python -I tools/mem_diff.py [--programs N] [--seed S] [--max-ops N] | 28 | python -I tools/mem_diff.py [--programs N] [--seed S] [--max-ops N] |
| 24 | python -I tools/mem_diff.py --self-test | 29 | python -I tools/mem_diff.py --self-test |
| @@ -67,6 +72,85 @@ class Program: | |||
| 67 | def __init__(self): | 72 | def __init__(self): |
| 68 | self.lines = [] | 73 | self.lines = [] |
| 69 | self.memory = {} | 74 | self.memory = {} |
| 75 | self.norm_regs = set() # VGPRs that hold a UNORM/SNORM result (rounding differences are counted apart) | ||
| 76 | self.missing_w32_regs = set() # VGPRs that select the missing W of a 32-bit format (counted apart) | ||
| 77 | |||
| 78 | |||
| 79 | # ---------------------------------------------------------------- typed-buffer formats (reference decode) | ||
| 80 | |||
| 81 | def _load_format_names(): | ||
| 82 | path = os.path.join(HERE, "gfx10_formats.json") | ||
| 83 | import json | ||
| 84 | with open(path, encoding="utf-8") as f: | ||
| 85 | return {int(k): v for k, v in json.load(f).items()} | ||
| 86 | |||
| 87 | |||
| 88 | FORMAT_NAMES = _load_format_names() # GFX10 FORMAT code -> name (Mesa gfx10-rsrc.json), data only | ||
| 89 | ONE_F32 = 0x3F800000 | ||
| 90 | |||
| 91 | |||
| 92 | def parse_format(name): | ||
| 93 | """'GFX10_FORMAT_16_16_SNORM' -> ([16, 16] for X, Y from bit 0, 'SNORM'). The name lists the channel widths | ||
| 94 | from the top bit down (2_10_10_10 is X10 Y10 Z10 W2 from bit 0), so the list is reversed. Returns None for | ||
| 95 | names this model does not decode (packed floats, scaled, sRGB, block formats).""" | ||
| 96 | parts = name.replace("GFX10_FORMAT_", "").split("_") | ||
| 97 | kind = parts[-1] | ||
| 98 | try: | ||
| 99 | widths = [int(p) for p in parts[:-1]] | ||
| 100 | except ValueError: | ||
| 101 | return None | ||
| 102 | if kind not in ("UNORM", "SNORM", "UINT", "SINT", "FLOAT") or not widths: | ||
| 103 | return None | ||
| 104 | if kind == "FLOAT" and any(w not in (16, 32) for w in widths): | ||
| 105 | return None | ||
| 106 | if sum(widths) % 8: | ||
| 107 | return None | ||
| 108 | return list(reversed(widths)), kind | ||
| 109 | |||
| 110 | |||
| 111 | def f32_round(q): | ||
| 112 | """Nearest-even binary32 of an exact rational (Fraction), as bits.""" | ||
| 113 | from fractions import Fraction | ||
| 114 | if q == 0: | ||
| 115 | return 0 | ||
| 116 | d = float(q) | ||
| 117 | cands = set() | ||
| 118 | for scale in (1.0, 1 + 2 ** -26, 1 - 2 ** -26): | ||
| 119 | try: | ||
| 120 | cands.add(struct.unpack("<I", struct.pack("<f", d * scale))[0]) | ||
| 121 | except OverflowError: | ||
| 122 | pass | ||
| 123 | def err(b): | ||
| 124 | return abs(Fraction(struct.unpack("<f", struct.pack("<I", b))[0]) - q) | ||
| 125 | return min(cands, key=lambda b: (err(b), b & 1)) | ||
| 126 | |||
| 127 | |||
| 128 | def decode_element(raw, widths, kind): | ||
| 129 | """Decode one element (an int of sum(widths) bits) -> 4 result words, missing channels 0, 0, 0, 1.""" | ||
| 130 | from fractions import Fraction | ||
| 131 | out, bit = [], 0 | ||
| 132 | for w in widths: | ||
| 133 | v = (raw >> bit) & ((1 << w) - 1) | ||
| 134 | bit += w | ||
| 135 | if kind == "UNORM": | ||
| 136 | out.append(f32_round(Fraction(v, (1 << w) - 1))) | ||
| 137 | elif kind == "SNORM": | ||
| 138 | s = v - (1 << w) if v >> (w - 1) else v | ||
| 139 | q = max(Fraction(s, (1 << (w - 1)) - 1), Fraction(-1)) | ||
| 140 | bits_ = f32_round(abs(q)) | ||
| 141 | out.append(bits_ | (0x80000000 if q < 0 else 0)) | ||
| 142 | elif kind == "UINT": | ||
| 143 | out.append(v) | ||
| 144 | elif kind == "SINT": | ||
| 145 | out.append((v - (1 << w) if v >> (w - 1) else v) & MASK32) | ||
| 146 | elif w == 32: | ||
| 147 | out.append(v) | ||
| 148 | else: # FLOAT 16 -> f32 bits (exact) | ||
| 149 | out.append(struct.unpack("<I", struct.pack("<f", struct.unpack("<e", struct.pack("<H", v))[0]))[0]) | ||
| 150 | one = 1 if kind in ("UINT", "SINT") else ONE_F32 | ||
| 151 | while len(out) < 4: | ||
| 152 | out.append(0 if len(out) < 3 else one) | ||
| 153 | return out | ||
| 70 | 154 | ||
| 71 | 155 | ||
| 72 | # ---------------------------------------------------------------- reference model | 156 | # ---------------------------------------------------------------- reference model |
| @@ -149,6 +233,35 @@ class RefMachine: | |||
| 149 | self.v[vdata + i] = self.word(addr + 4 * i) | 233 | self.v[vdata + i] = self.word(addr + 4 * i) |
| 150 | else: | 234 | else: |
| 151 | self.put(addr + 4 * i, self.v.get(vdata + i, 0)) | 235 | self.put(addr + 4 * i, self.v.get(vdata + i, 0)) |
| 236 | elif op.startswith(("buffer_load_format_", "tbuffer_load_format_")): | ||
| 237 | ncomp = len(op.rsplit("_", 1)[1]) | ||
| 238 | vdata, rsrc = reg_first(args[0]), reg_first(args[2]) | ||
| 239 | vaddr = None if args[1] == "off" else reg_first(args[1]) | ||
| 240 | d0, d1, d3 = self.s.get(rsrc, 0), self.s.get(rsrc + 1, 0), self.s.get(rsrc + 3, 0) | ||
| 241 | base, stride = (d1 & 0xFFFF) << 32 | d0, (d1 >> 16) & 0x3FFF | ||
| 242 | offset = self.scalar(args[3]) + sum(int(m.split(":", 1)[1], 0) for m in mods if m.startswith("offset:")) | ||
| 243 | regs = [] if vaddr is None else [vaddr, vaddr + 1] | ||
| 244 | if "idxen" in mods: | ||
| 245 | offset += self.v.get(regs.pop(0), 0) * stride | ||
| 246 | if "offen" in mods: | ||
| 247 | offset += self.v.get(regs.pop(0), 0) | ||
| 248 | addr = base + (offset & MASK32) | ||
| 249 | fm = next((m for m in mods if m.startswith("format:")), None) | ||
| 250 | if op.startswith("tbuffer"): | ||
| 251 | name = "GFX10_FORMAT_" + fm.split("BUF_FMT_")[-1].rstrip("]") | ||
| 252 | else: | ||
| 253 | name = FORMAT_NAMES[(d3 >> 12) & 0x7F] | ||
| 254 | widths, kind = parse_format(name) | ||
| 255 | nbytes = sum(widths) // 8 | ||
| 256 | raw = 0 | ||
| 257 | for i in range(nbytes): # little-endian bytes from dword memory | ||
| 258 | a = addr + i | ||
| 259 | raw |= ((self.word(a & ~3) >> (8 * (a & 3))) & 0xFF) << (8 * i) | ||
| 260 | comps = decode_element(raw, widths, kind) | ||
| 261 | one = 1 if kind in ("UINT", "SINT") else ONE_F32 | ||
| 262 | for c in range(ncomp): | ||
| 263 | sel = (d3 >> (3 * c)) & 7 | ||
| 264 | self.v[vdata + c] = {0: 0, 1: one, 4: comps[0], 5: comps[1], 6: comps[2], 7: comps[3]}.get(sel, 0) | ||
| 152 | elif op.startswith("buffer_atomic_"): | 265 | elif op.startswith("buffer_atomic_"): |
| 153 | kind = op[len("buffer_atomic_"):] | 266 | kind = op[len("buffer_atomic_"):] |
| 154 | vdata, rsrc = reg_first(args[0]), reg_first(args[2]) | 267 | vdata, rsrc = reg_first(args[0]), reg_first(args[2]) |
| @@ -416,6 +529,66 @@ def gen_global(rng, prog): | |||
| 416 | return [("v", VDATA + i) for i in range(n)] | 529 | return [("v", VDATA + i) for i in range(n)] |
| 417 | 530 | ||
| 418 | 531 | ||
| 532 | # The five formats tbuffer loads use in the 1.0.2.1 corpus (counted over all 26,503 shaders). | ||
| 533 | TBUFFER_FORMATS = ("16_16_SNORM", "8_8_8_8_UNORM", "8_8_8_8_UINT", "2_10_10_10_UNORM", "16_16_16_16_UINT") | ||
| 534 | XYZW = 4 | 5 << 3 | 6 << 6 | 7 << 9 | ||
| 535 | |||
| 536 | |||
| 537 | def _typed_codes(): | ||
| 538 | """Formats buffer_load_format is generated with: the ones the translator claims to decode (its scope), each | ||
| 539 | checked here against the reference decode of its GFX10 name.""" | ||
| 540 | return sorted(c for c in rdna2spirv.Gen.FORMATS if parse_format(FORMAT_NAMES[c])) | ||
| 541 | |||
| 542 | |||
| 543 | def gen_buffer_format(rng, prog): | ||
| 544 | ncomp = rng.choice((1, 2, 3, 4)) | ||
| 545 | suffix = "xyzw"[:ncomp] | ||
| 546 | tbuffer = rng.random() < 0.5 | ||
| 547 | if tbuffer: | ||
| 548 | fname = rng.choice(TBUFFER_FORMATS) | ||
| 549 | widths, kind = parse_format("GFX10_FORMAT_" + fname) | ||
| 550 | word3 = XYZW # tbuffer: format in the instruction, identity swizzle | ||
| 551 | else: | ||
| 552 | code = rng.choice(_typed_codes()) | ||
| 553 | widths, kind = parse_format(FORMAT_NAMES[code]) | ||
| 554 | sels = [rng.choice((0, 4, 5, 6, 7)) for _ in range(4)] # no SEL_1: its integer value is unconfirmed | ||
| 555 | word3 = code << 12 | sels[0] | sels[1] << 3 | sels[2] << 6 | sels[3] << 9 | ||
| 556 | size = sum(widths) // 8 | ||
| 557 | base = _buffer_base(rng) | ||
| 558 | _seed_region(rng, prog.memory, base) | ||
| 559 | structured = rng.random() < 0.5 | ||
| 560 | if structured: | ||
| 561 | stride = size * rng.choice((1, 2, 3, 4)) | ||
| 562 | stride += -stride % 4 | ||
| 563 | records = REGION // stride | ||
| 564 | index = rng.randrange(0, records) | ||
| 565 | _emit_vsharp(rng, prog, base, stride, records) | ||
| 566 | prog.lines[-1] = f"s_mov_b32 s{VSHARP + 3}, {word3:#x}" | ||
| 567 | prog.lines.append(f"v_mov_b32 v{VADDR}, {index:#x}") | ||
| 568 | operands = f"v{VADDR}, s[{VSHARP}:{VSHARP + 3}], 0" | ||
| 569 | addr_mods = "idxen" | ||
| 570 | else: | ||
| 571 | voff = rng.randrange(0, (REGION - 16) // size) * size | ||
| 572 | _emit_vsharp(rng, prog, base, 0, REGION) | ||
| 573 | prog.lines[-1] = f"s_mov_b32 s{VSHARP + 3}, {word3:#x}" | ||
| 574 | prog.lines.append(f"v_mov_b32 v{VADDR}, {voff:#x}") | ||
| 575 | operands = f"v{VADDR}, s[{VSHARP}:{VSHARP + 3}], 0" | ||
| 576 | addr_mods = "offen" | ||
| 577 | vdata = reg_range("v", VDATA, ncomp) | ||
| 578 | if tbuffer: | ||
| 579 | prog.lines.append(f"tbuffer_load_format_{suffix} {vdata}, {operands} format:[BUF_FMT_{fname}] {addr_mods}") | ||
| 580 | else: | ||
| 581 | prog.lines.append(f"buffer_load_format_{suffix} {vdata}, {operands} {addr_mods}") | ||
| 582 | prog.lines.append("s_waitcnt vmcnt(0)") | ||
| 583 | if kind in ("UNORM", "SNORM"): | ||
| 584 | prog.norm_regs.update(VDATA + c for c in range(ncomp)) | ||
| 585 | if set(widths) == {32} and len(widths) < 4: | ||
| 586 | for c in range(ncomp): | ||
| 587 | if (word3 >> (3 * c)) & 7 == 7: | ||
| 588 | prog.missing_w32_regs.add(VDATA + c) | ||
| 589 | return [("v", VDATA + c) for c in range(ncomp)] | ||
| 590 | |||
| 591 | |||
| 419 | GENERATORS = { | 592 | GENERATORS = { |
| 420 | "s_load": gen_s_load, | 593 | "s_load": gen_s_load, |
| 421 | "s_buffer_load": gen_s_buffer_load, | 594 | "s_buffer_load": gen_s_buffer_load, |
| @@ -423,6 +596,7 @@ GENERATORS = { | |||
| 423 | "buffer_store": gen_buffer_store, | 596 | "buffer_store": gen_buffer_store, |
| 424 | "buffer_atomic": gen_buffer_atomic, | 597 | "buffer_atomic": gen_buffer_atomic, |
| 425 | "global": gen_global, | 598 | "global": gen_global, |
| 599 | "buffer_format": gen_buffer_format, | ||
| 426 | } | 600 | } |
| 427 | 601 | ||
| 428 | 602 | ||
| @@ -434,9 +608,13 @@ def generate_program(rng, n_ops, families=None): | |||
| 434 | for _ in range(n_ops): | 608 | for _ in range(n_ops): |
| 435 | family = rng.choice(names) | 609 | family = rng.choice(names) |
| 436 | prog.lines.append(f"; {family}") | 610 | prog.lines.append(f"; {family}") |
| 611 | prog.norm_regs = set() # only the last operation's results are exported | ||
| 612 | prog.missing_w32_regs = set() | ||
| 437 | results = GENERATORS[family](rng, prog) or results | 613 | results = GENERATORS[family](rng, prog) or results |
| 438 | picks = [results[min(i, len(results) - 1)] for i in range(4)] if results else [] | 614 | picks = [results[min(i, len(results) - 1)] for i in range(4)] if results else [] |
| 439 | for dst, (kind, reg) in zip(EXPORT, picks): | 615 | prog.export_sources = {} |
| 616 | for k, (dst, (kind, reg)) in enumerate(zip(EXPORT, picks)): | ||
| 617 | prog.export_sources[k] = reg if kind == "v" else None | ||
| 440 | if not (kind == "v" and reg == dst): | 618 | if not (kind == "v" and reg == dst): |
| 441 | prog.lines.append(f"v_mov_b32 v{dst}, {kind}{reg}") | 619 | prog.lines.append(f"v_mov_b32 v{dst}, {kind}{reg}") |
| 442 | return prog | 620 | return prog |
| @@ -477,9 +655,21 @@ def run_one(name, prog): | |||
| 477 | return "skip", f"mini_interp unsupported: {type(e).__name__}: {e}" | 655 | return "skip", f"mini_interp unsupported: {type(e).__name__}: {e}" |
| 478 | got = list(out.get("%out_mrt0", ())) | 656 | got = list(out.get("%out_mrt0", ())) |
| 479 | if got != want: | 657 | if got != want: |
| 480 | i = next(k for k in range(4) if k >= len(got) or got[k] != want[k]) | 658 | bad = [k for k in range(4) if k >= len(got) or got[k] != want[k]] |
| 659 | i = bad[0] | ||
| 481 | g = f"{got[i]:#010x}" if i < len(got) else "missing" | 660 | g = f"{got[i]:#010x}" if i < len(got) else "missing" |
| 482 | return "mismatch", f"export word {i}: expected {want[i]:#010x}, translated {g}" | 661 | detail = f"export word {i}: expected {want[i]:#010x}, translated {g}" |
| 662 | # A UNORM/SNORM result one ULP from the correctly rounded value is a rounding-convention question (the | ||
| 663 | # translator multiplies by a rounded reciprocal), kept apart so it can't hide a real decode error. | ||
| 664 | sources = prog.export_sources if hasattr(prog, "export_sources") else {} | ||
| 665 | if all(k < len(got) and sources.get(k) in prog.norm_regs and abs(got[k] - want[k]) == 1 for k in bad): | ||
| 666 | return "norm-ulp1", detail | ||
| 667 | # The missing W of a 32-bit format: the translator gives 0, every other format (and this model) 1 / 1.0. | ||
| 668 | # Posted as a finding awaiting a hardware measurement; counted apart until then. | ||
| 669 | if all(k < len(got) and sources.get(k) in prog.missing_w32_regs and got[k] == 0 and want[k] in (1, ONE_F32) | ||
| 670 | for k in bad): | ||
| 671 | return "missing-w32", detail | ||
| 672 | return "mismatch", detail | ||
| 483 | if memory != ref.mem: | 673 | if memory != ref.mem: |
| 484 | bad = sorted(a for a in set(memory) | set(ref.mem) if memory.get(a, 0) != ref.mem.get(a, 0)) | 674 | bad = sorted(a for a in set(memory) | set(ref.mem) if memory.get(a, 0) != ref.mem.get(a, 0)) |
| 485 | a = bad[0] | 675 | a = bad[0] |
| @@ -489,7 +679,7 @@ def run_one(name, prog): | |||
| 489 | 679 | ||
| 490 | 680 | ||
| 491 | def run_suite(programs, seed, max_ops, families=None): | 681 | def run_suite(programs, seed, max_ops, families=None): |
| 492 | counts = {"pass": 0, "mismatch": 0, "skip": {}, "harness-error": 0} | 682 | counts = {"pass": 0, "mismatch": 0, "norm-ulp1": 0, "missing-w32": 0, "skip": {}, "harness-error": 0} |
| 493 | first = None | 683 | first = None |
| 494 | rng = random.Random(seed) | 684 | rng = random.Random(seed) |
| 495 | for i in range(programs): | 685 | for i in range(programs): |
| @@ -503,6 +693,8 @@ def run_suite(programs, seed, max_ops, families=None): | |||
| 503 | continue | 693 | continue |
| 504 | if status == "match": | 694 | if status == "match": |
| 505 | counts["pass"] += 1 | 695 | counts["pass"] += 1 |
| 696 | elif status in ("norm-ulp1", "missing-w32"): | ||
| 697 | counts[status] += 1 | ||
| 506 | elif status == "mismatch": | 698 | elif status == "mismatch": |
| 507 | counts["mismatch"] += 1 | 699 | counts["mismatch"] += 1 |
| 508 | if first is None: | 700 | if first is None: |
| @@ -556,7 +748,9 @@ def main(argv=None): | |||
| 556 | counts, first = run_suite(a.programs, a.seed, a.max_ops, a.family) | 748 | counts, first = run_suite(a.programs, a.seed, a.max_ops, a.family) |
| 557 | skips = sum(counts["skip"].values()) | 749 | skips = sum(counts["skip"].values()) |
| 558 | print(f"{a.programs} programs (seed {a.seed}): {counts['pass']} pass, {counts['mismatch']} mismatch, " | 750 | print(f"{a.programs} programs (seed {a.seed}): {counts['pass']} pass, {counts['mismatch']} mismatch, " |
| 559 | f"{skips} skipped, {counts['harness-error']} harness errors") | 751 | f"{counts['norm-ulp1']} UNORM/SNORM 1-ULP rounding differences, " |
| 752 | f"{counts['missing-w32']} missing-W-of-a-32-bit-format differences, {skips} skipped, " | ||
| 753 | f"{counts['harness-error']} harness errors") | ||
| 560 | for reason, n in sorted(counts["skip"].items()): | 754 | for reason, n in sorted(counts["skip"].items()): |
| 561 | print(f" skip x{n}: {reason}") | 755 | print(f" skip x{n}: {reason}") |
| 562 | if first: | 756 | if first: |