upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tools/salu_diff.py
diff options
context:
space:
mode:
authorTreXito <81475944+TreXito@users.noreply.github.com>2026-11-23 13:02:35 +0000
committerTreXito <81475944+TreXito@users.noreply.github.com>2026-11-23 13:02:35 +0000
commit615f30d7d4f06e33fe11d36769735f9016f2ad3d (patch)
tree0bc4baf4441a319596a511af9c0ccf32ccdb3e99 /tools/salu_diff.py
parent561b65a832218fbb9e6d2effd392a8eef093a3f8 (diff)
tools/salu_diff.py: generate s_cmov_b32/b64, s_cmovk_i32 and s_movrels_b32/b64
No fuzzer generated these five scalar ops. The SCC selects keep D when SCC is 0, read SCC without writing it, and join the --branches consumers. For the relative reads a program sets M0 to 0..7 after the seeds, and each s_movrels reads SGPR[S0 + M0] inside the seeded SGPRs; an odd M0 gives no b64 form (an odd-aligned pair is unmeasured). Shrinking keeps the M0 line. The rows rest on the gfx1035 cases of tools/int_hw_probe.py. Five translator mutations (s_cmov_b32 ignores SCC, s_cmovk_i32 zero-extends, s_cmov_b64 keeps the high word, s_movrels ignores M0, the b64 high word read without M0) each fail a unit test and a 2,000-program run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools/salu_diff.py')
-rw-r--r--tools/salu_diff.py64
1 files changed, 52 insertions, 12 deletions
diff --git a/tools/salu_diff.py b/tools/salu_diff.py
index fd3df87da..634d5f7b4 100644
--- a/tools/salu_diff.py
+++ b/tools/salu_diff.py
@@ -42,6 +42,13 @@ names them explicitly). s_bfe_* control words stay inside offset + width <= 32 u
42that the field stops at bit 31 (width = min(width, 32 - offset), so s_bfe_i32 sign-extends from the top bit 42that the field stops at bit 31 (width = min(width, 32 - offset), so s_bfe_i32 sign-extends from the top bit
43that exists). gfx1150 hardware and the translator both do that; the ISA pseudocode does not say so, and RDNA2 43that exists). gfx1150 hardware and the translator both do that; the ISA pseudocode does not say so, and RDNA2
44itself is unmeasured (#156). s_ff0/ff1_i32_b32 with a constant source diverged until SHADER-46 (#349). 44itself is unmeasured (#156). s_ff0/ff1_i32_b32 with a constant source diverged until SHADER-46 (#349).
45
46SCC selects: s_cmov_b32, s_cmovk_i32 (SIMM16 sign-extended) and s_cmov_b64 keep D when SCC is 0; they read SCC
47and do not write it, so --branches uses them as consumers too. Relative reads: when the opcodes include
48s_movrels_b32/b64, the program sets M0 to 0..7 once, after the seeds, and every s_movrels reads
49SGPR[S0 + M0] (the b64 form the pair there) inside the seeded SGPRs. With an odd M0 the b64 form is not
50generated (the b32 one takes its place): an odd-aligned pair is unmeasured. These rows rest on the gfx1035
51cases of tools/int_hw_probe.py (docs/shader_validation.md).
45""" 52"""
46import argparse 53import argparse
47import gc 54import gc
@@ -266,6 +273,12 @@ def _sop1():
266 "s_bitset1_b32": ("sop1_rmw", lambda a, b, c, d: (d | (1 << (a & 31)), None)), 273 "s_bitset1_b32": ("sop1_rmw", lambda a, b, c, d: (d | (1 << (a & 31)), None)),
267 "s_bitset0_b64": ("sop1_64_rmw", lambda a, b, c, d: (d & ~(1 << (a & 63)) & M64, None)), 274 "s_bitset0_b64": ("sop1_64_rmw", lambda a, b, c, d: (d & ~(1 << (a & 63)) & M64, None)),
268 "s_bitset1_b64": ("sop1_64_rmw", lambda a, b, c, d: (d | (1 << (a & 63)), None)), 275 "s_bitset1_b64": ("sop1_64_rmw", lambda a, b, c, d: (d | (1 << (a & 63)), None)),
276 # SCC ? S0 : D. SCC is read, not written.
277 "s_cmov_b32": ("sop1_rmw", lambda a, b, c, d: (a if c else d, None)),
278 "s_cmov_b64": ("sop1_64_rmw64", lambda a, b, c, d: (a if c else d, None)),
279 # SGPR[S0 + M0]: the reference reads the register (or pair) and passes its value as a.
280 "s_movrels_b32": ("movrels", lambda a, b, c: (a, None)),
281 "s_movrels_b64": ("movrels_64", lambda a, b, c: (a, None)),
269 "s_mov_b64": ("sop1_64", lambda a, b, c: (a, None)), 282 "s_mov_b64": ("sop1_64", lambda a, b, c: (a, None)),
270 "s_not_b64": ("sop1_64", lambda a, b, c: (~a & M64, int(~a & M64 != 0))), 283 "s_not_b64": ("sop1_64", lambda a, b, c: (~a & M64, int(~a & M64 != 0))),
271 "s_brev_b64": ("sop1_64", lambda a, b, c: (int(f"{a:064b}"[::-1], 2), None)), 284 "s_brev_b64": ("sop1_64", lambda a, b, c: (int(f"{a:064b}"[::-1], 2), None)),
@@ -299,19 +312,22 @@ def _sopc():
299 ops["s_addk_i32"] = ("sopk", lambda d, k, c: ((d + sext16(k)) & M32, 312 ops["s_addk_i32"] = ("sopk", lambda d, k, c: ((d + sext16(k)) & M32,
300 int(not -2**31 <= s32(d) + sext16(k) < 2**31))) 313 int(not -2**31 <= s32(d) + sext16(k) < 2**31)))
301 ops["s_mulk_i32"] = ("sopk", lambda d, k, c: ((s32(d) * sext16(k)) & M32, None)) 314 ops["s_mulk_i32"] = ("sopk", lambda d, k, c: ((s32(d) * sext16(k)) & M32, None))
315 ops["s_cmovk_i32"] = ("sopk", lambda d, k, c: (sext16(k) & M32 if c else d, None))
302 return ops 316 return ops
303 317
304 318
305OPS = {**_sop2(), **_sop2_64(), **_sop1(), **_sopc()} 319OPS = {**_sop2(), **_sop2_64(), **_sop1(), **_sopc()}
306 320
307SCC_CONSUMERS = ("s_cselect_b32", "s_addc_u32", "s_subb_u32") 321SCC_CONSUMERS = ("s_cselect_b32", "s_addc_u32", "s_subb_u32", "s_cmov_b32", "s_cmovk_i32", "s_cmov_b64")
322RMW_FORMS = ("sop1_rmw", "sop1_64_rmw", "sop1_64_rmw64") # fn(a, b, scc, d): D is read too
323MOVREL_FORMS = ("movrels", "movrels_64")
308SCC_BRANCHES = ("s_cbranch_scc0", "s_cbranch_scc1") 324SCC_BRANCHES = ("s_cbranch_scc0", "s_cbranch_scc1")
309 325
310 326
311def writes_scc(name): 327def writes_scc(name):
312 """Whether the opcode writes SCC (each one always does or never does).""" 328 """Whether the opcode writes SCC (each one always does or never does)."""
313 form, fn = OPS[name] 329 form, fn = OPS[name]
314 return (fn(1, 2, 0, 3) if form in ("sop1_rmw", "sop1_64_rmw") else fn(1, 2, 0))[1] is not None 330 return (fn(1, 2, 0, 3) if form in RMW_FORMS else fn(1, 2, 0))[1] is not None
315 331
316 332
317def branch_skip(line): 333def branch_skip(line):
@@ -363,9 +379,16 @@ def _shared_literal_sources(src0: str, src1: str, register: int) -> str:
363 return f"{src0}, {src1}" 379 return f"{src0}, {src1}"
364 380
365 381
366def emit(rng, name): 382def emit(rng, name, m0=0):
383 """One instruction. m0: the program's M0, which keeps an s_movrels source inside the seeded SGPRs."""
367 form = OPS[name][0] 384 form = OPS[name][0]
368 d, p = rng.choice(REGS), rng.choice(PAIRS) 385 d, p = rng.choice(REGS), rng.choice(PAIRS)
386 if form == "movrels_64" and m0 % 2:
387 name, form = "s_movrels_b32", "movrels" # an odd-aligned pair is not generated
388 if form == "movrels":
389 return f"{name} s{d}, s{rng.choice([r for r in REGS if r + m0 <= REGS[-1]])}"
390 if form == "movrels_64":
391 return f"{name} {_pair(p)}, {_pair(rng.choice([r for r in PAIRS if r + m0 + 1 <= REGS[-1]]))}"
369 if form == "sop2": 392 if form == "sop2":
370 b = _src32(rng) if name.startswith("s_bfe") and BFE_ANY and rng.random() < 0.3 else _bfe_ctl(rng) if name.startswith("s_bfe") else _src32(rng) 393 b = _src32(rng) if name.startswith("s_bfe") and BFE_ANY and rng.random() < 0.3 else _bfe_ctl(rng) if name.startswith("s_bfe") else _src32(rng)
371 return f"{name} s{d}, {_shared_literal_sources(_src32(rng), b, d)}" 394 return f"{name} s{d}, {_shared_literal_sources(_src32(rng), b, d)}"
@@ -377,7 +400,7 @@ def emit(rng, name):
377 return f"{name} {_pair(p)}, {_shared_literal_sources(_src32(rng), _src32(rng), d)}" 400 return f"{name} {_pair(p)}, {_shared_literal_sources(_src32(rng), _src32(rng), d)}"
378 if form in ("sop1", "sop1_rmw"): 401 if form in ("sop1", "sop1_rmw"):
379 return f"{name} s{d}, {_src32(rng)}" 402 return f"{name} s{d}, {_src32(rng)}"
380 if form == "sop1_64": 403 if form in ("sop1_64", "sop1_64_rmw64"):
381 return f"{name} {_pair(p)}, {_src64(rng)}" 404 return f"{name} {_pair(p)}, {_src64(rng)}"
382 if form in ("sop1_32to64", "sop1_64_rmw"): 405 if form in ("sop1_32to64", "sop1_64_rmw"):
383 return f"{name} {_pair(p)}, {_src32(rng)}" 406 return f"{name} {_pair(p)}, {_src32(rng)}"
@@ -395,33 +418,37 @@ def emit(rng, name):
395 418
396def generate(rng, n_ops, ops, branches=False): 419def generate(rng, n_ops, ops, branches=False):
397 lines = [f"s_mov_b32 s{r}, 0x{_value(rng):x}" for r in REGS] 420 lines = [f"s_mov_b32 s{r}, 0x{_value(rng):x}" for r in REGS]
421 m0 = 0
422 if any(OPS[o][0] in MOVREL_FORMS for o in ops):
423 m0 = rng.randrange(8)
424 lines.append(f"s_mov_b32 m0, {m0}") # part of the seeds: shrinking keeps it
398 lines.append(f"s_cmp_eq_u32 s{rng.choice(REGS)}, s{rng.choice(REGS)}") # a known SCC to start with 425 lines.append(f"s_cmp_eq_u32 s{rng.choice(REGS)}, s{rng.choice(REGS)}") # a known SCC to start with
399 if branches: 426 if branches:
400 lines += _cf_body(rng, n_ops, ops) 427 lines += _cf_body(rng, n_ops, ops, m0)
401 else: 428 else:
402 for _ in range(n_ops): 429 for _ in range(n_ops):
403 lines.append(emit(rng, rng.choice(ops))) 430 lines.append(emit(rng, rng.choice(ops), m0))
404 lines += observe(rng.sample(REGS, 3)) 431 lines += observe(rng.sample(REGS, 3))
405 return lines 432 return lines
406 433
407 434
408def _cf_body(rng, n_ops, ops): 435def _cf_body(rng, n_ops, ops, m0=0):
409 """n_ops instructions, half of them SCC producers. After a producer: sometimes a chain of SCC consumers, 436 """n_ops instructions, half of them SCC producers. After a producer: sometimes a chain of SCC consumers,
410 sometimes s_cbranch_scc0/1 forward over the next 1..3 instructions (which never branch themselves).""" 437 sometimes s_cbranch_scc0/1 forward over the next 1..3 instructions (which never branch themselves)."""
411 producers = [o for o in ops if writes_scc(o)] or ops 438 producers = [o for o in ops if writes_scc(o)] or ops
412 body = [] 439 body = []
413 for _ in range(n_ops): 440 for _ in range(n_ops):
414 op = rng.choice(producers if rng.random() < 0.5 else ops) 441 op = rng.choice(producers if rng.random() < 0.5 else ops)
415 body.append(emit(rng, op)) 442 body.append(emit(rng, op, m0))
416 if not writes_scc(op): 443 if not writes_scc(op):
417 continue 444 continue
418 r = rng.random() 445 r = rng.random()
419 if r < 0.35: 446 if r < 0.35:
420 body += [emit(rng, rng.choice(SCC_CONSUMERS)) for _ in range(rng.randint(1, 3))] 447 body += [emit(rng, rng.choice(SCC_CONSUMERS), m0) for _ in range(rng.randint(1, 3))]
421 elif r < 0.8: 448 elif r < 0.8:
422 k = rng.randint(1, 3) 449 k = rng.randint(1, 3)
423 body.append(f"{rng.choice(SCC_BRANCHES)} +{k}") 450 body.append(f"{rng.choice(SCC_BRANCHES)} +{k}")
424 body += [emit(rng, rng.choice(ops + list(SCC_CONSUMERS))) for _ in range(k)] 451 body += [emit(rng, rng.choice(ops + list(SCC_CONSUMERS)), m0) for _ in range(k)]
425 return body 452 return body
426 453
427 454
@@ -466,7 +493,7 @@ def _write(sgpr, tok, value, width):
466 493
467def reference(lines): 494def reference(lines):
468 """Run the scalar program; returns the four exported words.""" 495 """Run the scalar program; returns the four exported words."""
469 sgpr, scc, vgpr = {}, 0, {} 496 sgpr, scc, vgpr, m0 = {}, 0, {}, 0
470 skip = 0 497 skip = 0
471 for line in lines: 498 for line in lines:
472 if skip: 499 if skip:
@@ -483,6 +510,9 @@ def reference(lines):
483 if op == "v_mov_b32": 510 if op == "v_mov_b32":
484 vgpr[int(args[0][1:])] = _parse_src(args[1], sgpr, 32) 511 vgpr[int(args[0][1:])] = _parse_src(args[1], sgpr, 32)
485 continue 512 continue
513 if op == "s_mov_b32" and args[0] == "m0":
514 m0 = _parse_src(args[1], sgpr, 32)
515 continue
486 if op not in OPS: 516 if op not in OPS:
487 raise RefError(f"{op} is not modelled") 517 raise RefError(f"{op} is not modelled")
488 form, fn = OPS[op] 518 form, fn = OPS[op]
@@ -510,6 +540,16 @@ def reference(lines):
510 elif form == "sop1_64_rmw": 540 elif form == "sop1_64_rmw":
511 r, c = fn(_parse_src(args[1], sgpr, 32), None, scc, _parse_src(args[0], sgpr, 64)) 541 r, c = fn(_parse_src(args[1], sgpr, 32), None, scc, _parse_src(args[0], sgpr, 64))
512 _write(sgpr, args[0], r, 64) 542 _write(sgpr, args[0], r, 64)
543 elif form == "sop1_64_rmw64":
544 r, c = fn(_parse_src(args[1], sgpr, 64), None, scc, _parse_src(args[0], sgpr, 64))
545 _write(sgpr, args[0], r, 64)
546 elif form in MOVREL_FORMS:
547 n = 2 if form == "movrels_64" else 1
548 base = int(args[1][2:args[1].index(":")] if n == 2 else args[1][1:]) + m0
549 if base < REGS[0] or base + n - 1 > REGS[-1]:
550 raise RefError(f"{line!r}: SGPR {base} (S0 + M0) is outside the seeded s{REGS[0]}..s{REGS[-1]}")
551 r, c = fn(sum(sgpr.get(base + w, 0) << (32 * w) for w in range(n)), None, scc)
552 _write(sgpr, args[0], r, 32 * n)
513 elif form == "sop1_32to64": 553 elif form == "sop1_32to64":
514 r, c = fn(_parse_src(args[1], sgpr, 32), None, scc) 554 r, c = fn(_parse_src(args[1], sgpr, 32), None, scc)
515 _write(sgpr, args[0], r, 64) 555 _write(sgpr, args[0], r, 64)
@@ -660,7 +700,7 @@ def drop(body, i):
660 700
661def shrink(lines, check_fn=check): 701def shrink(lines, check_fn=check):
662 """Drop instructions (not seeds or the observation) while the program still mismatches.""" 702 """Drop instructions (not seeds or the observation) while the program still mismatches."""
663 n_seed = len(REGS) 703 n_seed = len(REGS) + lines[len(REGS)].startswith("s_mov_b32 m0,") # the seeds, and M0 when set
664 body = lines[n_seed:-5] 704 body = lines[n_seed:-5]
665 changed = True 705 changed = True
666 while changed: 706 while changed: