diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 20:38:54 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 20:38:54 -0500 |
| commit | d11e39bb3967d94de4333c0c0b23e7e847b2e3e5 (patch) | |
| tree | 731c53d4860ec95b4fce0a93ee9661cb5413e84c /tools | |
| parent | fa7f38b45f043408a75e1f0cc5d04d69058c0279 (diff) | |
| parent | 14d81dd093e91c2f9b4a21f74fe0db2d26db1199 (diff) | |
Merge pull request #599 from LuanFreitas
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/mem_diff.py | 45 | ||||
| -rw-r--r-- | tools/rdna2spirv.py | 35 |
2 files changed, 33 insertions, 47 deletions
diff --git a/tools/mem_diff.py b/tools/mem_diff.py index b6bae05fa..0469d763e 100644 --- a/tools/mem_diff.py +++ b/tools/mem_diff.py | |||
| @@ -20,9 +20,8 @@ s_buffer_load alignment truncation and swizzling depend on descriptor fields doc | |||
| 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 | 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 | 23 | tools/gfx10_formats.json, not from the translator's table. Missing channels repeat (*c* mod *n*), UNORM/SNORM |
| 24 | counted apart instead of as mismatches: 'norm-ulp1' (a UNORM/SNORM result one ULP from the correctly rounded | 24 | use correctly rounded division, and DST_SEL 1 is integer 1 for UINT/SINT (SHADER-86, RX 6700). |
| 25 | value) and 'missing-w32' (the missing W of a 32-bit format: the translator returns 0, every other format 1). | ||
| 26 | 25 | ||
| 27 | Usage: | 26 | Usage: |
| 28 | python -I tools/mem_diff.py [--programs N] [--seed S] [--max-ops N] | 27 | python -I tools/mem_diff.py [--programs N] [--seed S] [--max-ops N] |
| @@ -72,8 +71,6 @@ class Program: | |||
| 72 | def __init__(self): | 71 | def __init__(self): |
| 73 | self.lines = [] | 72 | self.lines = [] |
| 74 | self.memory = {} | 73 | 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 | 74 | ||
| 78 | 75 | ||
| 79 | # ---------------------------------------------------------------- typed-buffer formats (reference decode) | 76 | # ---------------------------------------------------------------- typed-buffer formats (reference decode) |
| @@ -126,7 +123,7 @@ def f32_round(q): | |||
| 126 | 123 | ||
| 127 | 124 | ||
| 128 | def decode_element(raw, widths, kind): | 125 | 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.""" | 126 | """Decode one element (an int of sum(widths) bits) -> 4 result words; missing channels repeat (*c* mod *n*).""" |
| 130 | from fractions import Fraction | 127 | from fractions import Fraction |
| 131 | out, bit = [], 0 | 128 | out, bit = [], 0 |
| 132 | for w in widths: | 129 | for w in widths: |
| @@ -147,9 +144,9 @@ def decode_element(raw, widths, kind): | |||
| 147 | out.append(v) | 144 | out.append(v) |
| 148 | else: # FLOAT 16 -> f32 bits (exact) | 145 | else: # FLOAT 16 -> f32 bits (exact) |
| 149 | out.append(struct.unpack("<I", struct.pack("<f", struct.unpack("<e", struct.pack("<H", v))[0]))[0]) | 146 | 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 | 147 | n = len(out) |
| 151 | while len(out) < 4: | 148 | while len(out) < 4: |
| 152 | out.append(0 if len(out) < 3 else one) | 149 | out.append(out[len(out) % n]) |
| 153 | return out | 150 | return out |
| 154 | 151 | ||
| 155 | 152 | ||
| @@ -551,7 +548,7 @@ def gen_buffer_format(rng, prog): | |||
| 551 | else: | 548 | else: |
| 552 | code = rng.choice(_typed_codes()) | 549 | code = rng.choice(_typed_codes()) |
| 553 | widths, kind = parse_format(FORMAT_NAMES[code]) | 550 | 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 | 551 | sels = [rng.choice((0, 1, 4, 5, 6, 7)) for _ in range(4)] |
| 555 | word3 = code << 12 | sels[0] | sels[1] << 3 | sels[2] << 6 | sels[3] << 9 | 552 | word3 = code << 12 | sels[0] | sels[1] << 3 | sels[2] << 6 | sels[3] << 9 |
| 556 | size = sum(widths) // 8 | 553 | size = sum(widths) // 8 |
| 557 | base = _buffer_base(rng) | 554 | base = _buffer_base(rng) |
| @@ -580,12 +577,6 @@ def gen_buffer_format(rng, prog): | |||
| 580 | else: | 577 | else: |
| 581 | prog.lines.append(f"buffer_load_format_{suffix} {vdata}, {operands} {addr_mods}") | 578 | prog.lines.append(f"buffer_load_format_{suffix} {vdata}, {operands} {addr_mods}") |
| 582 | prog.lines.append("s_waitcnt vmcnt(0)") | 579 | 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)] | 580 | return [("v", VDATA + c) for c in range(ncomp)] |
| 590 | 581 | ||
| 591 | 582 | ||
| @@ -608,13 +599,9 @@ def generate_program(rng, n_ops, families=None): | |||
| 608 | for _ in range(n_ops): | 599 | for _ in range(n_ops): |
| 609 | family = rng.choice(names) | 600 | family = rng.choice(names) |
| 610 | prog.lines.append(f"; {family}") | 601 | prog.lines.append(f"; {family}") |
| 611 | prog.norm_regs = set() # only the last operation's results are exported | ||
| 612 | prog.missing_w32_regs = set() | ||
| 613 | results = GENERATORS[family](rng, prog) or results | 602 | results = GENERATORS[family](rng, prog) or results |
| 614 | picks = [results[min(i, len(results) - 1)] for i in range(4)] if results else [] | 603 | picks = [results[min(i, len(results) - 1)] for i in range(4)] if results else [] |
| 615 | prog.export_sources = {} | 604 | for dst, (kind, reg) in zip(EXPORT, picks): |
| 616 | for k, (dst, (kind, reg)) in enumerate(zip(EXPORT, picks)): | ||
| 617 | prog.export_sources[k] = reg if kind == "v" else None | ||
| 618 | if not (kind == "v" and reg == dst): | 605 | if not (kind == "v" and reg == dst): |
| 619 | prog.lines.append(f"v_mov_b32 v{dst}, {kind}{reg}") | 606 | prog.lines.append(f"v_mov_b32 v{dst}, {kind}{reg}") |
| 620 | return prog | 607 | return prog |
| @@ -659,16 +646,6 @@ def run_one(name, prog): | |||
| 659 | i = bad[0] | 646 | i = bad[0] |
| 660 | g = f"{got[i]:#010x}" if i < len(got) else "missing" | 647 | g = f"{got[i]:#010x}" if i < len(got) else "missing" |
| 661 | detail = f"export word {i}: expected {want[i]:#010x}, translated {g}" | 648 | 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 | 649 | return "mismatch", detail |
| 673 | if memory != ref.mem: | 650 | if memory != ref.mem: |
| 674 | bad = sorted(a for a in set(memory) | set(ref.mem) if memory.get(a, 0) != ref.mem.get(a, 0)) | 651 | bad = sorted(a for a in set(memory) | set(ref.mem) if memory.get(a, 0) != ref.mem.get(a, 0)) |
| @@ -679,7 +656,7 @@ def run_one(name, prog): | |||
| 679 | 656 | ||
| 680 | 657 | ||
| 681 | def run_suite(programs, seed, max_ops, families=None): | 658 | def run_suite(programs, seed, max_ops, families=None): |
| 682 | counts = {"pass": 0, "mismatch": 0, "norm-ulp1": 0, "missing-w32": 0, "skip": {}, "harness-error": 0} | 659 | counts = {"pass": 0, "mismatch": 0, "skip": {}, "harness-error": 0} |
| 683 | first = None | 660 | first = None |
| 684 | rng = random.Random(seed) | 661 | rng = random.Random(seed) |
| 685 | for i in range(programs): | 662 | for i in range(programs): |
| @@ -693,8 +670,6 @@ def run_suite(programs, seed, max_ops, families=None): | |||
| 693 | continue | 670 | continue |
| 694 | if status == "match": | 671 | if status == "match": |
| 695 | counts["pass"] += 1 | 672 | counts["pass"] += 1 |
| 696 | elif status in ("norm-ulp1", "missing-w32"): | ||
| 697 | counts[status] += 1 | ||
| 698 | elif status == "mismatch": | 673 | elif status == "mismatch": |
| 699 | counts["mismatch"] += 1 | 674 | counts["mismatch"] += 1 |
| 700 | if first is None: | 675 | if first is None: |
| @@ -748,9 +723,7 @@ def main(argv=None): | |||
| 748 | counts, first = run_suite(a.programs, a.seed, a.max_ops, a.family) | 723 | counts, first = run_suite(a.programs, a.seed, a.max_ops, a.family) |
| 749 | skips = sum(counts["skip"].values()) | 724 | skips = sum(counts["skip"].values()) |
| 750 | print(f"{a.programs} programs (seed {a.seed}): {counts['pass']} pass, {counts['mismatch']} mismatch, " | 725 | print(f"{a.programs} programs (seed {a.seed}): {counts['pass']} pass, {counts['mismatch']} mismatch, " |
| 751 | f"{counts['norm-ulp1']} UNORM/SNORM 1-ULP rounding differences, " | 726 | f"{skips} skipped, {counts['harness-error']} harness errors") |
| 752 | f"{counts['missing-w32']} missing-W-of-a-32-bit-format differences, {skips} skipped, " | ||
| 753 | f"{counts['harness-error']} harness errors") | ||
| 754 | for reason, n in sorted(counts["skip"].items()): | 727 | for reason, n in sorted(counts["skip"].items()): |
| 755 | print(f" skip x{n}: {reason}") | 728 | print(f" skip x{n}: {reason}") |
| 756 | if first: | 729 | if first: |
diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index fcb4e0f9f..8dbbbbcfb 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py | |||
| @@ -842,16 +842,20 @@ class Gen: | |||
| 842 | return words | 842 | return words |
| 843 | 843 | ||
| 844 | def decode_format(self, words, code): | 844 | def decode_format(self, words, code): |
| 845 | """Decode one element of format `code` from raw dwords -> 4 uint ids holding f32 bits (or uint for *_UINT/uint).""" | 845 | """Decode one element of format `code` from raw dwords -> 4 uint ids holding f32 bits (or uint for *_UINT/uint). |
| 846 | |||
| 847 | Missing channels repeat the format's own (component c = c mod n): measured on RX 6700 for SHADER-86. | ||
| 848 | UNORM/SNORM use correctly rounded OpFDiv by (2^n-1), not a reciprocal multiply.""" | ||
| 846 | try: | 849 | try: |
| 847 | size, kind, widths = self.FORMATS[code] | 850 | size, kind, widths = self.FORMATS[code] |
| 848 | except KeyError: | 851 | except KeyError: |
| 849 | raise Unsupported(f"format {code} (not in FORMATS)") | 852 | raise Unsupported(f"format {code} (not in FORMATS)") |
| 850 | if kind in ("f32", "u32"): | 853 | if kind in ("f32", "u32"): |
| 851 | vals = list(words[:len(widths)]) | 854 | n = len(widths) |
| 852 | if kind == "u32": | 855 | vals = list(words[:n]) |
| 853 | vals = vals # integers are passed through as their bits | 856 | while len(vals) < 4: |
| 854 | return vals + [self.cu(0)] * (3 - len(vals) + 1 if len(vals) < 4 else 0) | 857 | vals.append(vals[len(vals) % n]) |
| 858 | return vals | ||
| 855 | out, bit = [], 0 | 859 | out, bit = [], 0 |
| 856 | for w in widths: | 860 | for w in widths: |
| 857 | src = words[bit // 32] | 861 | src = words[bit // 32] |
| @@ -865,21 +869,21 @@ class Gen: | |||
| 865 | elif kind == "unorm": | 869 | elif kind == "unorm": |
| 866 | c, x = self.tmp(), self.tmp() | 870 | c, x = self.tmp(), self.tmp() |
| 867 | self.emit(f"{c} = OpConvertUToF %float {f}") | 871 | self.emit(f"{c} = OpConvertUToF %float {f}") |
| 868 | self.emit(f"{x} = OpFMul %float {c} {self.fconst(1.0 / ((1 << w) - 1))}") | 872 | self.emit(f"{x} = OpFDiv %float {c} {self.fconst(float((1 << w) - 1))}") |
| 869 | out.append(self.tou(x)) | 873 | out.append(self.tou(x)) |
| 870 | elif kind == "snorm": | 874 | elif kind == "snorm": |
| 871 | si, c, x, cl = self.tmp(), self.tmp(), self.tmp(), self.tmp() | 875 | si, c, x, cl = self.tmp(), self.tmp(), self.tmp(), self.tmp() |
| 872 | self.emit(f"{si} = OpBitFieldSExtract %int {self.as_int(src)} {self.cu(bit % 32)} {self.cu(w)}") | 876 | self.emit(f"{si} = OpBitFieldSExtract %int {self.as_int(src)} {self.cu(bit % 32)} {self.cu(w)}") |
| 873 | self.emit(f"{c} = OpConvertSToF %float {si}") | 877 | self.emit(f"{c} = OpConvertSToF %float {si}") |
| 874 | self.emit(f"{x} = OpFMul %float {c} {self.fconst(1.0 / ((1 << (w - 1)) - 1))}") | 878 | self.emit(f"{x} = OpFDiv %float {c} {self.fconst(float((1 << (w - 1)) - 1))}") |
| 875 | self.emit(f"{cl} = OpExtInst %float %glsl FMax {x} {self.fconst(-1.0)}") | 879 | self.emit(f"{cl} = OpExtInst %float %glsl FMax {x} {self.fconst(-1.0)}") |
| 876 | out.append(self.tou(cl)) | 880 | out.append(self.tou(cl)) |
| 877 | else: # uint: raw integer | 881 | else: # uint: raw integer |
| 878 | out.append(f) | 882 | out.append(f) |
| 879 | bit += w | 883 | bit += w |
| 880 | one = self.cu(0x3F800000) if kind in ("f16", "unorm", "snorm") else self.cu(1) | 884 | n = len(out) |
| 881 | while len(out) < 4: | 885 | while len(out) < 4: |
| 882 | out.append(self.cu(0) if len(out) < 3 else one) | 886 | out.append(out[len(out) % n]) |
| 883 | return out | 887 | return out |
| 884 | 888 | ||
| 885 | BUF_ATOMICS = {"add": ("OpAtomicIAdd", 0), "sub": ("OpAtomicISub", 0), "umin": ("OpAtomicUMin", 0xFFFFFFFF), | 889 | BUF_ATOMICS = {"add": ("OpAtomicIAdd", 0), "sub": ("OpAtomicISub", 0), "umin": ("OpAtomicUMin", 0xFFFFFFFF), |
| @@ -1055,13 +1059,22 @@ class Gen: | |||
| 1055 | self.emit(f"{s} = OpSelect %uint {e} {dec[c]} {comps[c]}") | 1059 | self.emit(f"{s} = OpSelect %uint {e} {dec[c]} {comps[c]}") |
| 1056 | nxt.append(s) | 1060 | nxt.append(s) |
| 1057 | comps = nxt | 1061 | comps = nxt |
| 1058 | # DST_SEL swizzle: 0 -> 0, 1 -> 1.0, 4..7 -> X..W. | 1062 | # DST_SEL swizzle: 0 -> 0, 1 -> 1.0 (float/norm) or integer 1 (UINT/SINT; SHADER-86), 4..7 -> X..W. |
| 1063 | is_int_fmt = "%false" | ||
| 1064 | for code_v, (_, kind, _) in self.FORMATS.items(): | ||
| 1065 | if kind in ("u32", "uint"): | ||
| 1066 | e, o = self.tmp(), self.tmp() | ||
| 1067 | self.emit(f"{e} = OpIEqual %bool {fmt} {self.cu(code_v)}") | ||
| 1068 | self.emit(f"{o} = OpLogicalOr %bool {is_int_fmt} {e}") | ||
| 1069 | is_int_fmt = o | ||
| 1070 | dst1 = self.tmp() | ||
| 1071 | self.emit(f"{dst1} = OpSelect %uint {is_int_fmt} {self.cu(1)} {self.cu(0x3F800000)}") | ||
| 1059 | outs = [] | 1072 | outs = [] |
| 1060 | for c in range(ncomp): | 1073 | for c in range(ncomp): |
| 1061 | sel = self.tmp() | 1074 | sel = self.tmp() |
| 1062 | self.emit(f"{sel} = OpBitFieldUExtract %uint {d3} {self.cu(3 * c)} {self.cu(3)}") | 1075 | self.emit(f"{sel} = OpBitFieldUExtract %uint {d3} {self.cu(3 * c)} {self.cu(3)}") |
| 1063 | r = self.cu(0) | 1076 | r = self.cu(0) |
| 1064 | for code_v, src in ((1, self.cu(0x3F800000)), (4, comps[0]), (5, comps[1]), (6, comps[2]), (7, comps[3])): | 1077 | for code_v, src in ((1, dst1), (4, comps[0]), (5, comps[1]), (6, comps[2]), (7, comps[3])): |
| 1065 | e, s = self.tmp(), self.tmp() | 1078 | e, s = self.tmp(), self.tmp() |
| 1066 | self.emit(f"{e} = OpIEqual %bool {sel} {self.cu(code_v)}") | 1079 | self.emit(f"{e} = OpIEqual %bool {sel} {self.cu(code_v)}") |
| 1067 | self.emit(f"{s} = OpSelect %uint {e} {src} {r}") | 1080 | self.emit(f"{s} = OpSelect %uint {e} {src} {r}") |