diff options
| author | EdenBendheim <edenbendheim@gmail.com> | 2026-11-23 13:18:00 -0500 |
|---|---|---|
| committer | EdenBendheim <edenbendheim@gmail.com> | 2026-11-24 00:38:49 -0500 |
| commit | d94a025f5e07234371fe13b3fca82f32cee75cbe (patch) | |
| tree | 0759d4d63e40f5777e51c360703fe4518b07089d /tools/shader_diff.py | |
| parent | 8f481cf16f261c7f37458aeb45e8f4e19994146d (diff) | |
shader_diff: exercise scalar addition and carry
Diffstat (limited to 'tools/shader_diff.py')
| -rw-r--r-- | tools/shader_diff.py | 74 |
1 files changed, 43 insertions, 31 deletions
diff --git a/tools/shader_diff.py b/tools/shader_diff.py index e1719a4cc..b48a85482 100644 --- a/tools/shader_diff.py +++ b/tools/shader_diff.py | |||
| @@ -10,8 +10,8 @@ it owns the program table, the seed and the comparison; cpu_ref stays a pure | |||
| 10 | interpreter and is never modified here. | 10 | interpreter and is never modified here. |
| 11 | 11 | ||
| 12 | Usage: | 12 | Usage: |
| 13 | python -I tools/shader_diff.py [--programs N] [--seed S] [--max-ops N] | 13 | python -I tools/shader_diff.py [--programs N] [--seed S] [--max-ops N] [--ops OP,...] |
| 14 | python -I tools/shader_diff.py --program-index I --seed S --max-ops N | 14 | python -I tools/shader_diff.py --program-index I --seed S --max-ops N [--ops OP,...] |
| 15 | python -I tools/shader_diff.py --self-test # harness canaries (must fail loudly if broken) | 15 | python -I tools/shader_diff.py --self-test # harness canaries (must fail loudly if broken) |
| 16 | python -I tools/shader_diff.py --list-ops | 16 | python -I tools/shader_diff.py --list-ops |
| 17 | 17 | ||
| @@ -51,6 +51,7 @@ EXPORT_REGS = (8, 9, 10, 11) | |||
| 51 | # real semantic divergence, not a rounding convention. | 51 | # real semantic divergence, not a rounding convention. |
| 52 | _SMALL_INTS = (0, 1, 2, 3, 5, 7, 15, 16, 31, 64, 127, 255) | 52 | _SMALL_INTS = (0, 1, 2, 3, 5, 7, 15, 16, 31, 64, 127, 255) |
| 53 | _EXACT_FLOATS = (0.5, 1.5, 2.5, -1.5, 4.0) | 53 | _EXACT_FLOATS = (0.5, 1.5, 2.5, -1.5, 4.0) |
| 54 | _SCALAR_SEEDS = _SMALL_INTS + (0x7FFFFFFF, 0x80000000, 0xFFFFFFFE, 0xFFFFFFFF) | ||
| 54 | 55 | ||
| 55 | 56 | ||
| 56 | def _u32_bits(value): | 57 | def _u32_bits(value): |
| @@ -113,21 +114,6 @@ def _op_templates(): | |||
| 113 | ] | 114 | ] |
| 114 | 115 | ||
| 115 | 116 | ||
| 116 | # NOTE: s_add_u32 is deliberately NOT generated: the translator lowers scalar | ||
| 117 | # adds to OpIAddCarry, which tests/semantic/spirv_mini_interp.py does not | ||
| 118 | # model yet, so every such program would skip with a reason instead of | ||
| 119 | # proving anything. Scalar reads are still covered: seeds are moved through | ||
| 120 | # s_regs and observed via the v_mov_b32/sgpr copy into an exported VGPR. Extending the | ||
| 121 | # mini-interp (not this runner) re-enables scalar ALU coverage. | ||
| 122 | _VEC_OPS = ("v_mov_b32", "v_add_f32", "v_sub_f32", "v_mul_f32", "v_fma_f32", | ||
| 123 | "v_and_b32", "v_or_b32", "v_xor_b32", "v_lshlrev_b32", | ||
| 124 | "v_lshrrev_b32", "v_ashrrev_i32", | ||
| 125 | "v_add_nc_u32", "v_sub_nc_u32", "v_subrev_nc_u32", "v_mul_u32_u24", | ||
| 126 | "v_mul_i32_i24", "v_mul_lo_u32", "v_mul_hi_u32", "v_mul_hi_i32", | ||
| 127 | "v_mad_u32_u24", "v_mad_i32_i24", "v_min_u32", "v_max_u32", | ||
| 128 | "v_min_i32", "v_max_i32", | ||
| 129 | "v_bfe_u32", "v_bfe_i32", "v_bfi_b32", "v_alignbit_b32", | ||
| 130 | "v_alignbyte_b32", "v_perm_b32", "v_med3_i32", "v_med3_u32") | ||
| 131 | _FLOAT_OPS = ("v_add_f32", "v_sub_f32", "v_mul_f32", "v_fma_f32") | 117 | _FLOAT_OPS = ("v_add_f32", "v_sub_f32", "v_mul_f32", "v_fma_f32") |
| 132 | 118 | ||
| 133 | 119 | ||
| @@ -137,7 +123,7 @@ def _seed_value(rng): | |||
| 137 | return _u32_bits(rng.choice(_EXACT_FLOATS)) | 123 | return _u32_bits(rng.choice(_EXACT_FLOATS)) |
| 138 | 124 | ||
| 139 | 125 | ||
| 140 | def generate_program(rng, n_ops): | 126 | def generate_program(rng, n_ops, *, ops=None): |
| 141 | """One straight-line program. Returns (body_lines, export_writers). | 127 | """One straight-line program. Returns (body_lines, export_writers). |
| 142 | 128 | ||
| 143 | body_lines are tab-less assembly lines (seeds, ops). export_writers maps | 129 | body_lines are tab-less assembly lines (seeds, ops). export_writers maps |
| @@ -151,8 +137,10 @@ def generate_program(rng, n_ops): | |||
| 151 | writers[v] = ("v_mov_b32/seed", len(lines)) | 137 | writers[v] = ("v_mov_b32/seed", len(lines)) |
| 152 | s_regs = (4, 5, 6, 7) | 138 | s_regs = (4, 5, 6, 7) |
| 153 | for s in s_regs: | 139 | for s in s_regs: |
| 154 | lines.append(f"s_mov_b32 s{s}, {_seed_value(rng) % 256}") | 140 | lines.append(f"s_mov_b32 s{s}, {rng.choice(_SCALAR_SEEDS):#010x}") |
| 155 | vec_ops = [t for t in _op_templates() if t[0] in _VEC_OPS and t[0] != "v_mov_b32"] | 141 | templates = [(name, emit) for name, emit in _op_templates() |
| 142 | if (name != "v_mov_b32" if ops is None else name in ops)] | ||
| 143 | scalar_result = None | ||
| 156 | # A float op may return any NaN payload (Vulkan does not promise one) and the | 144 | # A float op may return any NaN payload (Vulkan does not promise one) and the |
| 157 | # comparison accepts both-NaN. If that value then fed an integer op, the two | 145 | # comparison accepts both-NaN. If that value then fed an integer op, the two |
| 158 | # payloads would produce different integer results, so integer ops never read | 146 | # payloads would produce different integer results, so integer ops never read |
| @@ -160,7 +148,13 @@ def generate_program(rng, n_ops): | |||
| 160 | # both oracles; a later integer write clears the taint. | 148 | # both oracles; a later integer write clears the taint. |
| 161 | tainted = set() | 149 | tainted = set() |
| 162 | for _ in range(n_ops): | 150 | for _ in range(n_ops): |
| 163 | name, emit = rng.choice(vec_ops) | 151 | name, emit = rng.choice(templates) |
| 152 | if name == "s_add_u32": | ||
| 153 | # Chain scalar adds so earlier sums contribute to the exported result. | ||
| 154 | src = rng.choice(s_regs) if scalar_result is None else scalar_result | ||
| 155 | scalar_result = 8 | ||
| 156 | lines.append(emit(rng, scalar_result, (src, rng.choice(s_regs)))) | ||
| 157 | continue | ||
| 164 | dst = rng.choice(SCRATCH) | 158 | dst = rng.choice(SCRATCH) |
| 165 | clean = [v for v in SCRATCH if v not in tainted] | 159 | clean = [v for v in SCRATCH if v not in tainted] |
| 166 | if name not in _FLOAT_OPS and not clean: | 160 | if name not in _FLOAT_OPS and not clean: |
| @@ -178,10 +172,15 @@ def generate_program(rng, n_ops): | |||
| 178 | tainted.add(dst) | 172 | tainted.add(dst) |
| 179 | else: | 173 | else: |
| 180 | tainted.discard(dst) | 174 | tainted.discard(dst) |
| 181 | # Reuse the oldest export: an untouched seed if possible, else an ALU result. | 175 | if scalar_result is None: |
| 182 | dst = min(EXPORT_REGS, key=lambda reg: writers[reg][1]) | 176 | scalar_outputs = [(f"s{rng.choice(s_regs)}", "sgpr-copy")] |
| 183 | lines.append(f"v_mov_b32 v{dst}, s{rng.choice(s_regs)}") | 177 | else: |
| 184 | writers[dst] = ("v_mov_b32/sgpr-copy", len(lines)) | 178 | scalar_outputs = [(f"s{scalar_result}", "sgpr-copy"), ("scc", "scc-copy")] |
| 179 | # Reuse the oldest exports: untouched seeds if possible, then ALU results. | ||
| 180 | export_order = sorted(EXPORT_REGS, key=lambda reg: writers[reg][1]) | ||
| 181 | for dst, (src, kind) in zip(export_order, scalar_outputs): | ||
| 182 | lines.append(f"v_mov_b32 v{dst}, {src}") | ||
| 183 | writers[dst] = (f"v_mov_b32/{kind}", len(lines)) | ||
| 185 | return lines, writers | 184 | return lines, writers |
| 186 | 185 | ||
| 187 | 186 | ||
| @@ -263,14 +262,17 @@ def run_one(name, lines, writers): | |||
| 263 | return ("mismatch", mismatch) | 262 | return ("mismatch", mismatch) |
| 264 | 263 | ||
| 265 | 264 | ||
| 266 | def run_suite(programs, seed, max_ops, limit=None, *, start=0): | 265 | def run_suite(programs, seed, max_ops, limit=None, *, start=0, ops=None): |
| 267 | """Generate the seeded sequence, executing cases from index start. Return (counts, first_failure).""" | 266 | """Generate the seeded sequence, executing cases from index start. Return (counts, first_failure).""" |
| 268 | counts = {"pass": 0, "mismatch": 0, "skip": {}, "harness-error": 0} | 267 | counts = {"pass": 0, "mismatch": 0, "skip": {}, "harness-error": 0} |
| 269 | first_failure = None | 268 | first_failure = None |
| 270 | rng = random.Random(seed) | 269 | rng = random.Random(seed) |
| 271 | total = programs if limit is None else min(programs, limit) | 270 | total = programs if limit is None else min(programs, limit) |
| 271 | replay = f"--seed {seed} --max-ops {max_ops}" | ||
| 272 | if ops is not None: | ||
| 273 | replay += " --ops " + ",".join(ops) | ||
| 272 | for i in range(total): | 274 | for i in range(total): |
| 273 | lines, writers = generate_program(rng, rng.randrange(1, max_ops + 1)) | 275 | lines, writers = generate_program(rng, rng.randrange(1, max_ops + 1), ops=ops) |
| 274 | if i < start: # advance the original RNG stream, but skip both oracles | 276 | if i < start: # advance the original RNG stream, but skip both oracles |
| 275 | continue | 277 | continue |
| 276 | name = f"ps_diff_{i:04d}" | 278 | name = f"ps_diff_{i:04d}" |
| @@ -280,7 +282,7 @@ def run_suite(programs, seed, max_ops, limit=None, *, start=0): | |||
| 280 | counts["harness-error"] += 1 | 282 | counts["harness-error"] += 1 |
| 281 | if first_failure is None: | 283 | if first_failure is None: |
| 282 | first_failure = (f"{name}: harness error {type(e).__name__}: {e}\n" | 284 | first_failure = (f"{name}: harness error {type(e).__name__}: {e}\n" |
| 283 | f"Replay: --seed {seed} --max-ops {max_ops} --program-index {i}") | 285 | f"Replay: {replay} --program-index {i}") |
| 284 | continue | 286 | continue |
| 285 | if status == "match": | 287 | if status == "match": |
| 286 | counts["pass"] += 1 | 288 | counts["pass"] += 1 |
| @@ -288,7 +290,7 @@ def run_suite(programs, seed, max_ops, limit=None, *, start=0): | |||
| 288 | counts["mismatch"] += 1 | 290 | counts["mismatch"] += 1 |
| 289 | if first_failure is None: | 291 | if first_failure is None: |
| 290 | first_failure = (f"{name} (seed {seed} program {i}): {detail}\n" | 292 | first_failure = (f"{name} (seed {seed} program {i}): {detail}\n" |
| 291 | f"Replay: --seed {seed} --max-ops {max_ops} --program-index {i}\n" + | 293 | f"Replay: {replay} --program-index {i}\n" + |
| 292 | "\n".join("\t" + line for line in lines)) | 294 | "\n".join("\t" + line for line in lines)) |
| 293 | else: | 295 | else: |
| 294 | counts["skip"][detail] = counts["skip"].get(detail, 0) + 1 | 296 | counts["skip"][detail] = counts["skip"].get(detail, 0) + 1 |
| @@ -346,6 +348,7 @@ def main(argv=None): | |||
| 346 | help="maximum ops per generated program (default: 8)") | 348 | help="maximum ops per generated program (default: 8)") |
| 347 | ap.add_argument("--limit", type=_at_least_one, default=None, | 349 | ap.add_argument("--limit", type=_at_least_one, default=None, |
| 348 | help="run only the first N programs (N >= 1)") | 350 | help="run only the first N programs (N >= 1)") |
| 351 | ap.add_argument("--ops", help="comma-separated opcodes to generate (see --list-ops)") | ||
| 349 | ap.add_argument("--list-ops", action="store_true", help="print the op table and exit") | 352 | ap.add_argument("--list-ops", action="store_true", help="print the op table and exit") |
| 350 | ap.add_argument("--self-test", action="store_true", help="run harness canaries and exit") | 353 | ap.add_argument("--self-test", action="store_true", help="run harness canaries and exit") |
| 351 | a = ap.parse_args(argv) | 354 | a = ap.parse_args(argv) |
| @@ -355,15 +358,24 @@ def main(argv=None): | |||
| 355 | return 0 | 358 | return 0 |
| 356 | if a.self_test: | 359 | if a.self_test: |
| 357 | return 0 if self_test() else 1 | 360 | return 0 if self_test() else 1 |
| 361 | ops = None | ||
| 362 | if a.ops is not None: | ||
| 363 | ops = tuple(token.strip() for token in a.ops.split(",")) | ||
| 364 | available = {name for name, _ in _op_templates()} | ||
| 365 | if not all(ops): | ||
| 366 | ap.error("--ops must be a comma-separated list of opcodes") | ||
| 367 | unknown = sorted(set(ops) - available) | ||
| 368 | if unknown: | ||
| 369 | ap.error("unknown --ops opcode(s): " + ", ".join(unknown)) | ||
| 358 | if a.program_index is None: | 370 | if a.program_index is None: |
| 359 | counts, first_failure = run_suite(a.programs or 200, a.seed, a.max_ops, a.limit) | 371 | counts, first_failure = run_suite(a.programs or 200, a.seed, a.max_ops, a.limit, ops=ops) |
| 360 | else: | 372 | else: |
| 361 | if a.program_index < 0: | 373 | if a.program_index < 0: |
| 362 | ap.error("--program-index must be nonnegative") | 374 | ap.error("--program-index must be nonnegative") |
| 363 | if a.limit is not None: | 375 | if a.limit is not None: |
| 364 | ap.error("--program-index cannot be combined with --limit") | 376 | ap.error("--program-index cannot be combined with --limit") |
| 365 | print(f"replaying program {a.program_index} (seed {a.seed}, max ops {a.max_ops})") | 377 | print(f"replaying program {a.program_index} (seed {a.seed}, max ops {a.max_ops})") |
| 366 | counts, first_failure = run_suite(a.program_index + 1, a.seed, a.max_ops, start=a.program_index) | 378 | counts, first_failure = run_suite(a.program_index + 1, a.seed, a.max_ops, start=a.program_index, ops=ops) |
| 367 | total_skips = sum(counts["skip"].values()) | 379 | total_skips = sum(counts["skip"].values()) |
| 368 | total_run = counts["pass"] + counts["mismatch"] + total_skips + counts["harness-error"] | 380 | total_run = counts["pass"] + counts["mismatch"] + total_skips + counts["harness-error"] |
| 369 | print(f"{total_run} programs (seed {a.seed}): " | 381 | print(f"{total_run} programs (seed {a.seed}): " |