r"""Measure the VOP3 output modifiers of the binary64 conversion, frexp and div_fixup VALU ops across MODE settings. Standard library only. docs/design.md records for the f32 VALU ops that `mul:2`/`mul:4`/`div:2` are applied only when MODE.IEEE = 0 and the output denormals of the destination precision are flushed, and `clamp` in every mode. Running this probe natively on RDNA2 (gfx1036) through tools/hw_oracle extends that gate to the remaining binary64 and 16-bit results: `v_cvt_f64_i32`, `v_cvt_f64_u32`, `v_frexp_mant_f64`, `v_div_fixup_f64` and `v_div_fixup_f16` ignore the scaling while `denorm_mode_16_64 = 3` — which is the mode the translator assumes — and apply it only with IEEE = 0 and `denorm_mode_16_64 = 0`, while `clamp` still applies in every mode. `v_cvt_f32_f64` writes an f32 result and follows the f32 gate: it scales whenever `denorm32 = 0`, including under `denorm_mode_16_64 = 3`, and stops scaling when `denorm32 = 3`. The gate is therefore per result precision, not per instruction family. wave32 and wave64 agree on every word written; for the 16-bit form only the low half of the destination is written, as the ISA defines. Every expectation is derived from the plain (no-modifier) result measured in the same mode on the same device, so the probe compares the effect of the modifier and not the semantics of the operation. Usage: python -I tools/f64_omod_probe.py [--waves 32,64] [--modes ...] [--json out.json] python -I tools/f64_omod_probe.py --self-test Exit 0 when every measured result matches the model (with wave32 and wave64 agreeing bit-for-bit), 1 on a model mismatch or a wave32/wave64 difference, 2 on a harness or CLI error, and 77 when the hardware oracle is unavailable or the GPU is not GFX10. Nothing here reads game files. """ import argparse import json import math import os import stat import struct import sys import tempfile HERE = os.path.dirname(os.path.abspath(__file__)) sys.dont_write_bytecode = True if HERE not in sys.path: sys.path.insert(0, HERE) from file_mode import published_mode # noqa: E402 from replace_retry import replace as replace_retry # noqa: E402 MODES = { # The mode the translator emits and the game runs with: scaling ignored, clamp applied. "game_i0_d3": {"round16_64": 0, "denorm16_64": 3, "denorm32": 0, "dx10_clamp": 1, "ieee": 0}, # 16/64-bit denormals flushed, IEEE off: the scaling applies to the f16 and binary64 results. "i0_d0": {"round16_64": 0, "denorm16_64": 0, "denorm32": 0, "dx10_clamp": 1, "ieee": 0}, # IEEE on: the scaling is ignored in both denormal settings. "i1_d3": {"round16_64": 0, "denorm16_64": 3, "denorm32": 0, "dx10_clamp": 1, "ieee": 1}, "i1_d0": {"round16_64": 0, "denorm16_64": 0, "denorm32": 0, "dx10_clamp": 1, "ieee": 1}, # f32 denormals kept while 16/64-bit ones are flushed: the discriminator for the f32 result of v_cvt_f32_f64. "i0_d3_f32keep": {"round16_64": 0, "denorm16_64": 3, "denorm32": 3, "dx10_clamp": 1, "ieee": 0}, } MODS = ("", "mul:2", "mul:4", "div:2", "clamp") SCALE = {"mul:2": 2.0, "mul:4": 4.0, "div:2": 0.5} INT_SOURCES = [("0", 0x00000000), ("1", 0x00000001), ("-1", 0xFFFFFFFF), ("2^24+1", 0x01000001), ("2^31-1", 0x7FFFFFFF), ("-2^31", 0x80000000)] F64_TO_F32 = [("1.0", 1.0), ("2.0", 2.0), ("1.5", 1.5), ("3.25", 3.25), ("-1.0", -1.0), ("2^-1074 min sub", 2.0 ** -1074), ("2^-1050 sub", 2.0 ** -1050), ("2^-130", 2.0 ** -130)] FREXP_INPUTS = [("3.0", 3.0), ("1.0", 1.0), ("0.5", 0.5), ("-1.75", -1.75), ("8.0", 8.0), ("2^-1074 min sub", 2.0 ** -1074), ("1.5*2^-1050 sub", 1.5 * 2.0 ** -1050)] def _f64_bits(x: float) -> int: return struct.unpack(" int: try: return struct.unpack("> 32:#010x}") return lines # (op, e64 form, result kind, [(case name, setup lines)]) OPS = [ ("v_cvt_f64_i32", "v_cvt_f64_i32_e64 v[8:9], v1", "f64", [(name, [f"v_mov_b32_e32 v1, {bits:#010x}"]) for name, bits in INT_SOURCES]), ("v_cvt_f64_u32", "v_cvt_f64_u32_e64 v[8:9], v1", "f64", [(name, [f"v_mov_b32_e32 v1, {bits:#010x}"]) for name, bits in INT_SOURCES]), ("v_cvt_f32_f64", "v_cvt_f32_f64_e64 v8, v[1:2]", "f32", [(name, load([value])) for name, value in F64_TO_F32]), ("v_frexp_mant_f64", "v_frexp_mant_f64_e64 v[8:9], v[1:2]", "f64", [(name, load([value])) for name, value in FREXP_INPUTS]), ("v_div_fixup_f64", "v_div_fixup_f64_e64 v[8:9], v[1:2], v[3:4], v[5:6]", "f64", [(name, load(values)) for name, values in [("q=2 d=1.5 n=3", (2.0, 1.5, 3.0)), ("q=-2 d=1.5 n=3", (-2.0, 1.5, 3.0)), ("q=2 d=-1.5 n=3", (2.0, -1.5, 3.0)), ("q=0.75 d=2 n=1.5", (0.75, 2.0, 1.5))]]), ("v_div_fixup_f16", "v_div_fixup_f16_e64 v8, v1, v2, v3", "f16", [(name, [f"v_mov_b32_e32 v1, {h:#010x}", f"v_mov_b32_e32 v2, {d:#010x}", f"v_mov_b32_e32 v3, {n:#010x}"]) for name, h, d, n in [("q=2h d=1.5h n=3h", _f16_bits(2.0), _f16_bits(1.5), _f16_bits(3.0)), ("q=-2h d=1.5h n=3h", _f16_bits(-2.0), _f16_bits(1.5), _f16_bits(3.0)), ("q=0.75h d=2h n=1.5h", _f16_bits(0.75), _f16_bits(2.0), _f16_bits(1.5)), ("q=1h d=1h n=1h", _f16_bits(1.0), _f16_bits(1.0), _f16_bits(1.0))]]), ] def word_count(kind: str) -> int: return 2 if kind == "f64" else 1 def build_kernels(asm, modes, waves): kernels, meta = [], [] for wave in waves: for mode_name in modes: for op, form, kind, cases in OPS: lines, words = [], 0 for _, setup in cases: for mod in MODS: lines.extend(setup) lines.append(form + (f" {mod}" if mod else "")) lines.append(f"v_mov_b32_e32 v7, {words * 4}") lines.append("global_store_dword v7, v8, s[2:3]") words += 1 if kind == "f64": lines.append("v_lshrrev_b64 v[10:11], 32, v[8:9]") lines.append(f"v_mov_b32_e32 v7, {words * 4}") lines.append("global_store_dword v7, v10, s[2:3]") words += 1 lines.append("s_waitcnt vmcnt(0)") lines.append("s_endpgm") kernels.append(asm.Kernel(lines, wave=wave, threads=1, words=words, raw=True, mode=dict(MODES[mode_name]))) meta.append((wave, mode_name, op, kind, [name for name, _ in cases])) return kernels, meta def scalar_value(lo, hi, kind): if kind == "f64": return struct.unpack("> 32) & 0xFFFFFFFF) if kind == "f32": try: return (struct.unpack(" int: ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) ap.add_argument("--modes", default=",".join(MODES)) ap.add_argument("--waves", default="32,64") ap.add_argument("--json", help="write the measurement report here") ap.add_argument("--hw-arch") ap.add_argument("--self-test", action="store_true") args = ap.parse_args(argv) if args.self_test: return self_test() if args.hw_arch is not None: args.hw_arch = args.hw_arch.strip() if not args.hw_arch: ap.error("--hw-arch must not be empty") mode_tokens = [m.strip() for m in args.modes.split(",")] wave_tokens = [w.strip() for w in args.waves.split(",")] if not any(mode_tokens) or any(not m for m in mode_tokens): ap.error("--modes must be a comma-separated list of mode names") if not any(wave_tokens) or any(not w for w in wave_tokens): ap.error("--waves must be a comma-separated list of 32 and/or 64") modes = list(dict.fromkeys(mode_tokens)) bad = [m for m in modes if m not in MODES] + [w for w in wave_tokens if w not in ("32", "64")] if bad: ap.error("unknown or empty --modes/--waves entry: " + ", ".join(bad)) waves = list(dict.fromkeys(int(w) for w in wave_tokens)) if args.json is not None: if not args.json.strip(): ap.error("--json must not be empty") if os.path.isdir(args.json): ap.error(f"--json {args.json} is a folder, not a file") if not validate_report_path(args.json): return 2 from hw_oracle import asm, runner try: oracle = runner.open_oracle(arch=args.hw_arch) except runner.Unavailable as exc: print(f"hardware oracle unavailable: {exc}") return 77 try: try: fam = asm.family(oracle.arch) except ValueError as exc: print(f"hardware oracle unavailable: {exc}") return 77 if fam != "gfx10": print(f"hardware oracle unavailable: {oracle.arch} is not GFX10") return 77 print(f"hardware oracle: {oracle.device} ({oracle.arch})") kernels, meta = build_kernels(asm, modes, waves) summary = evaluate(modes, waves, meta, oracle.run(kernels)) except runner.Unavailable as exc: print(f"hardware oracle unavailable: {exc}") return 77 except (RuntimeError, KeyError, TypeError) as exc: print(f"harness error: {exc}") return 2 finally: oracle.close() if args.json and not write_report(args.json, { "device": str(oracle.device), "arch": oracle.arch, "modes": modes, "waves": waves, "mods": list(MODS), "ops": [op for op, *_ in OPS], **summary}): return 2 print(f"{summary['total_cases']} cases across {len(modes)} mode(s) x {len(MODS)} modifier forms " f"x {len(waves)} wave(s): scaling_gated_on={summary['scaling_gated_on']}, " f"scaling_gated_off={summary['scaling_gated_off']}, " f"scaling_changed_value={summary['scaling_changed_value']}, " f"clamp_changed_value={summary['clamp_changed_value']}, " f"mismatches={summary['mismatches']}, wave_diffs={summary['wave_diffs']}") for m in summary["first_mismatches"]: print(f" MISMATCH {m['mode']} {m['op']} {m['case']} {m['mod']}: plain {m['plain']} " f"got {m['got']} want {m['want']}") for d in summary["first_wave_diffs"]: print(f" WAVE DIFF {d['mode']} {d['op']} {d['mod']!r} wave{d['wave']}") return 1 if (summary["mismatches"] or summary["wave_diffs"]) else 0 if __name__ == "__main__": try: sys.stdout.reconfigure(errors="replace") except (AttributeError, ValueError, OSError): pass sys.exit(main())