diff options
| author | TreXito <81475944+TreXito@users.noreply.github.com> | 2026-11-23 12:25:55 +0000 |
|---|---|---|
| committer | TreXito <81475944+TreXito@users.noreply.github.com> | 2026-11-23 12:25:55 +0000 |
| commit | c542d871c196eaa5ad32bf05733464ecbe800c00 (patch) | |
| tree | 35dd0b8736559c64fab8cd5979d118d62bef8102 /tools/f16misc_diff.py | |
| parent | ee2d373b54aca98eb15b2b9cd8402b62f4977438 (diff) | |
tools/f16misc_diff.py: generate the f16 output modifiers and clamp on v_frexp_exp_i16_f16
Main 7de2d446 and 2c3cc2a8 measured them on gfx1036 and the translator now
accepts them: an output multiplier changes nothing on an f16 result while
clamp still acts, and on v_frexp_exp_i16_f16 clamp and the multiplier are
both no-ops. The self-test still pinned three of them as refused, so the PR
failed its own test once merged onto main.
The model applies the measured rules, min3/max3, frexp_mant, frexp_exp and
ldexp get a multiplier in the random stage, frexp_exp gets clamp, and the
self-test and tests pin the measured words. A translator mutant that applies
the multiplier on clamp_f16 results is caught by the sweep (80 programs) and
the random stage (21).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools/f16misc_diff.py')
| -rw-r--r-- | tools/f16misc_diff.py | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/tools/f16misc_diff.py b/tools/f16misc_diff.py index 9400b823a..a6d27f88a 100644 --- a/tools/f16misc_diff.py +++ b/tools/f16misc_diff.py | |||
| @@ -24,6 +24,9 @@ Seeded straight-line programs run through two independent oracles, and the four | |||
| 24 | Zero gives a signed zero (exponent 0); inf and NaN pass through unchanged (exponent 0). | 24 | Zero gives a signed zero (exponent 0); inf and NaN pass through unchanged (exponent 0). |
| 25 | - clamp: to [0, 1] after the operation, before the rounding. On frexp_mant / ldexp a negative result and a | 25 | - clamp: to [0, 1] after the operation, before the rounding. On frexp_mant / ldexp a negative result and a |
| 26 | NaN give +0 (measured on gfx1036). | 26 | NaN give +0 (measured on gfx1036). |
| 27 | - output multipliers (mul:2, mul:4, div:2) change nothing: the hardware ignores them on an f16 result with | ||
| 28 | f16 denormals kept, and clamp still acts; on frexp_exp's integer result clamp changes nothing either | ||
| 29 | (measured on gfx1036, docs/design.md's f16 output modifier and v_frexp/ldexp_f16 rows). | ||
| 27 | - destinations: an f16 result goes to the half op_sel's last bit names (the low half for VOP1/VOP2 and | 30 | - destinations: an f16 result goes to the half op_sel's last bit names (the low half for VOP1/VOP2 and |
| 28 | v_fma_mixlo_f16, the high half for v_fma_mixhi_f16) and the other half is kept; v_pk_fmac_f16 writes both | 31 | v_fma_mixlo_f16, the high half for v_fma_mixhi_f16) and the other half is kept; v_pk_fmac_f16 writes both |
| 29 | halves; v_fma_mix_f32 writes the word. | 32 | halves; v_fma_mix_f32 writes the word. |
| @@ -48,9 +51,9 @@ its f32 value. Every program is encodable on GFX10: at most one literal, at most | |||
| 48 | or VOP3P op, and a VOP2 literal or SGPR only in src0. | 51 | or VOP3P op, and a VOP2 literal or SGPR only in src0. |
| 49 | 52 | ||
| 50 | Modifiers: neg/abs on the register sources of the VOP3/VOP3P forms (not on ldexp's exponent); op_sel on register | 53 | Modifiers: neg/abs on the register sources of the VOP3/VOP3P forms (not on ldexp's exponent); op_sel on register |
| 51 | sources and the destination bit; clamp on min3/max3, frexp_mant, ldexp and the fma_mix ops. Not generated, on | 54 | sources and the destination bit; clamp on min3/max3, frexp_mant, frexp_exp, ldexp and the fma_mix ops; an output |
| 52 | purpose: output multipliers (whether they apply to f16 results is SHADER-297, open), clamp on v_frexp_exp_i16_f16 | 55 | multiplier on the VOP3 forms of min3/max3, frexp_mant, frexp_exp and ldexp. Not generated, on purpose: the |
| 53 | and the modifiers the translator refuses (--self-test pins those as expected rejections), SDWA and DPP forms | 56 | modifiers the translator refuses (--self-test pins those as expected rejections), SDWA and DPP forms |
| 54 | (tools/sdwa_diff.py's domain), and op_sel on a constant (only measured for some ops). | 57 | (tools/sdwa_diff.py's domain), and op_sel on a constant (only measured for some ops). |
| 55 | 58 | ||
| 56 | Stages: | 59 | Stages: |
| @@ -137,6 +140,8 @@ FLOAT_INLINE = { # token -> (f32 word, f16 half) | |||
| 137 | "4.0": (0x40800000, 0x4400), "-4.0": (0xC0800000, 0xC400), "0.15915494": (0x3E22F983, 0x3118), | 140 | "4.0": (0x40800000, 0x4400), "-4.0": (0xC0800000, 0xC400), "0.15915494": (0x3E22F983, 0x3118), |
| 138 | } | 141 | } |
| 139 | INT_INLINE = tuple(range(0, 65)) + tuple(range(-16, 0)) | 142 | INT_INLINE = tuple(range(0, 65)) + tuple(range(-16, 0)) |
| 143 | OMODS = ("mul:2", "mul:4", "div:2") | ||
| 144 | OMOD_KINDS = ("min3", "max3", "mant", "exp", "ldexp") # the VOP3 forms that have an omod field | ||
| 140 | 145 | ||
| 141 | # Half states in the reference's register file: None (known bits), "nan" (an f16 NaN whose payload the reference | 146 | # Half states in the reference's register file: None (known bits), "nan" (an f16 NaN whose payload the reference |
| 142 | # does not model) and "w32" (half of an f32 NaN whose payload it does not model). | 147 | # does not model) and "w32" (half of an f32 NaN whose payload it does not model). |
| @@ -300,7 +305,7 @@ def clamp01(v, nan_to_zero=False): | |||
| 300 | 305 | ||
| 301 | _REG = re.compile(r"([sv])(\d+)$") | 306 | _REG = re.compile(r"([sv])(\d+)$") |
| 302 | _SRC = re.compile(r"(-?)(\|?)([sv]\d+)\|?$") | 307 | _SRC = re.compile(r"(-?)(\|?)([sv]\d+)\|?$") |
| 303 | _MOD = re.compile(r"\s+(op_sel:\[[01](?:,[01])*\]|op_sel_hi:\[[01],[01],[01]\]|clamp)$") | 308 | _MOD = re.compile(r"\s+(op_sel:\[[01](?:,[01])*\]|op_sel_hi:\[[01],[01],[01]\]|clamp|mul:[24]|div:2)$") |
| 304 | 309 | ||
| 305 | 310 | ||
| 306 | def split_line(line): | 311 | def split_line(line): |
| @@ -467,6 +472,9 @@ def step(line, mc, events=None): | |||
| 467 | ev = None if events is None else events.setdefault(op, Counter()) | 472 | ev = None if events is None else events.setdefault(op, Counter()) |
| 468 | d = mc.dst(args[0]) | 473 | d = mc.dst(args[0]) |
| 469 | clamp = "clamp" in mods | 474 | clamp = "clamp" in mods |
| 475 | # An output multiplier is a no-op on these f16 (and i16) results in the game's MODE (gfx1036): nothing to apply. | ||
| 476 | if any(m in OMODS for m in mods) and (kind not in OMOD_KINDS or op_full.endswith("_e32")): | ||
| 477 | raise HarnessError(f"output modifier on a form without one: {line}") | ||
| 470 | if kind in ("min3", "max3"): | 478 | if kind in ("min3", "max3"): |
| 471 | sel = _bits(mods, "op_sel", 4) | 479 | sel = _bits(mods, "op_sel", 4) |
| 472 | vals = [mc.f16(t, sel[i])[0] for i, t in enumerate(args[1:4])] | 480 | vals = [mc.f16(t, sel[i])[0] for i, t in enumerate(args[1:4])] |
| @@ -484,9 +492,7 @@ def step(line, mc, events=None): | |||
| 484 | sel = _bits(mods, "op_sel", 2) | 492 | sel = _bits(mods, "op_sel", 2) |
| 485 | v, h = mc.f16(args[1], sel[0]) | 493 | v, h = mc.f16(args[1], sel[0]) |
| 486 | _count_in(events, op, v) | 494 | _count_in(events, op, v) |
| 487 | if kind == "exp": | 495 | if kind == "exp": # clamp (and an omod) leave the integer result as it is (gfx1036) |
| 488 | if clamp: | ||
| 489 | raise HarnessError("clamp on v_frexp_exp_i16_f16") | ||
| 490 | e = _ilog2(v[2]) + 1 if v[0] == "num" and v[2] != 0 else 0 | 496 | e = _ilog2(v[2]) + 1 if v[0] == "num" and v[2] != 0 else 0 |
| 491 | mc.write_half(d, sel[1], e & M16) | 497 | mc.write_half(d, sel[1], e & M16) |
| 492 | return | 498 | return |
| @@ -694,6 +700,8 @@ def op_line(rng, op, kinds=None, form=None, mods=True, values=None): | |||
| 694 | extra = f" op_sel:[{','.join(map(str, sel))}] op_sel_hi:[{','.join(map(str, shi))}]" | 700 | extra = f" op_sel:[{','.join(map(str, sel))}] op_sel_hi:[{','.join(map(str, shi))}]" |
| 695 | if mods and rng.random() < 0.25: | 701 | if mods and rng.random() < 0.25: |
| 696 | extra += " clamp" | 702 | extra += " clamp" |
| 703 | if kind in ("min3", "max3") and mods and rng.random() < 0.2: | ||
| 704 | extra += " " + rng.choice(OMODS) | ||
| 697 | return f"{op} {d}, {', '.join(srcs)}{extra}", ks | 705 | return f"{op} {d}, {', '.join(srcs)}{extra}", ks |
| 698 | if kind in ("mant", "exp"): | 706 | if kind in ("mant", "exp"): |
| 699 | form = form or rng.choice(("e32", "e64")) | 707 | form = form or rng.choice(("e32", "e64")) |
| @@ -704,8 +712,10 @@ def op_line(rng, op, kinds=None, form=None, mods=True, values=None): | |||
| 704 | src = _reg_mod(rng, tok, ks[0]) if mods else tok | 712 | src = _reg_mod(rng, tok, ks[0]) if mods else tok |
| 705 | sel = [rng.getrandbits(1) if ks[0] in ("vgpr", "sgpr") else 0, rng.getrandbits(1)] | 713 | sel = [rng.getrandbits(1) if ks[0] in ("vgpr", "sgpr") else 0, rng.getrandbits(1)] |
| 706 | extra = f" op_sel:[{sel[0]},{sel[1]}]" if any(sel) or rng.random() < 0.2 else "" | 714 | extra = f" op_sel:[{sel[0]},{sel[1]}]" if any(sel) or rng.random() < 0.2 else "" |
| 707 | if kind == "mant" and mods and rng.random() < 0.25: | 715 | if mods and rng.random() < 0.25: |
| 708 | extra += " clamp" | 716 | extra += " clamp" |
| 717 | if mods and rng.random() < 0.2: | ||
| 718 | extra += " " + rng.choice(OMODS) | ||
| 709 | return f"{op}_e64 {d}, {src}{extra}", ks | 719 | return f"{op}_e64 {d}, {src}{extra}", ks |
| 710 | if kind == "ldexp": | 720 | if kind == "ldexp": |
| 711 | form = form or rng.choice(("e32", "e64")) | 721 | form = form or rng.choice(("e32", "e64")) |
| @@ -728,6 +738,8 @@ def op_line(rng, op, kinds=None, form=None, mods=True, values=None): | |||
| 728 | extra = f" op_sel:[{','.join(map(str, sel))}]" if any(sel) or rng.random() < 0.2 else "" | 738 | extra = f" op_sel:[{','.join(map(str, sel))}]" if any(sel) or rng.random() < 0.2 else "" |
| 729 | if mods and rng.random() < 0.25: | 739 | if mods and rng.random() < 0.25: |
| 730 | extra += " clamp" | 740 | extra += " clamp" |
| 741 | if mods and rng.random() < 0.2: | ||
| 742 | extra += " " + rng.choice(OMODS) | ||
| 731 | return f"{op}_e64 {d}, {src0}, {toks[1]}{extra}", ks | 743 | return f"{op}_e64 {d}, {src0}, {toks[1]}{extra}", ks |
| 732 | if kind in ("fmamk", "fmaak"): | 744 | if kind in ("fmamk", "fmaak"): |
| 733 | k0 = kinds[0] if kinds else rng.choice(("vgpr", "sgpr", "int", "float")) | 745 | k0 = kinds[0] if kinds else rng.choice(("vgpr", "sgpr", "int", "float")) |
| @@ -1115,9 +1127,6 @@ def run_suite(programs, seed, max_ops=6, ops=None): | |||
| 1115 | 1127 | ||
| 1116 | 1128 | ||
| 1117 | REJECTED = ( | 1129 | REJECTED = ( |
| 1118 | ("clamp on v_frexp_exp_i16_f16", "v_frexp_exp_i16_f16_e64 v8, v9 clamp"), | ||
| 1119 | ("mul:2 on v_frexp_mant_f16", "v_frexp_mant_f16_e64 v8, v9 mul:2"), | ||
| 1120 | ("div:2 on v_ldexp_f16", "v_ldexp_f16_e64 v8, v9, v10 div:2"), | ||
| 1121 | ("neg on v_pk_fmac_f16", "v_pk_fmac_f16 v8, -v9, v10"), | 1130 | ("neg on v_pk_fmac_f16", "v_pk_fmac_f16 v8, -v9, v10"), |
| 1122 | ("clamp on v_pk_fmac_f16", "v_pk_fmac_f16 v8, v9, v10 clamp"), | 1131 | ("clamp on v_pk_fmac_f16", "v_pk_fmac_f16 v8, v9, v10 clamp"), |
| 1123 | ) | 1132 | ) |
| @@ -1153,6 +1162,16 @@ def self_test(): | |||
| 1153 | mc.write_word(8, 0xDEADBEEF) | 1162 | mc.write_word(8, 0xDEADBEEF) |
| 1154 | step("v_min3_f16 v8, v9, v10, v11", mc) | 1163 | step("v_min3_f16 v8, v9, v10, v11", mc) |
| 1155 | report(mc.v[8] == 0xDEAD0000 | want, f"min3{tuple(hex(w) for w in words)} = 0xdead{want:04x}") | 1164 | report(mc.v[8] == 0xDEAD0000 | want, f"min3{tuple(hex(w) for w in words)} = 0xdead{want:04x}") |
| 1165 | # output multipliers are no-ops on f16 results and clamp still acts; frexp_exp ignores both (gfx1036) | ||
| 1166 | mc.write_word(9, 0x12343A00) | ||
| 1167 | mc.write_word(11, 0x9ABC4100) | ||
| 1168 | mc.write_word(12, 3) | ||
| 1169 | for line, want in (("v_frexp_mant_f16_e64 v8, v11 mul:4", 0x3900), | ||
| 1170 | ("v_frexp_exp_i16_f16_e64 v8, v11 clamp mul:2", 2), | ||
| 1171 | ("v_ldexp_f16_e64 v8, v9, v12 div:2", 0x4600), | ||
| 1172 | ("v_ldexp_f16_e64 v8, v11, v12 clamp mul:2", 0x3C00)): | ||
| 1173 | step(line, mc) | ||
| 1174 | report(mc.v[8] & M16 == want, f"{line} = 0x{want:04x}") | ||
| 1156 | # the translator rejects the forms the generator leaves out | 1175 | # the translator rejects the forms the generator leaves out |
| 1157 | for what, line in REJECTED: | 1176 | for what, line in REJECTED: |
| 1158 | try: | 1177 | try: |