From eeaa3d78051f0a63e88fbcc7517ecdbec06a8711 Mon Sep 17 00:00:00 2001 From: R0D4X Date: Fri, 27 Nov 2026 17:14:52 +0100 Subject: [PATCH 1/2] shader: lower v_min_f16 and v_max_f16 in the direct compute path --- docs/experiments/native-minmax-f16.md | 25 ++++ src/gfx1013_direct_compute.inl | 29 +++++ tests/cmake/native_minmax_f16.cmake | 12 ++ tests/native_minmax_f16_check.py | 158 ++++++++++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 docs/experiments/native-minmax-f16.md create mode 100644 tests/cmake/native_minmax_f16.cmake create mode 100644 tests/native_minmax_f16_check.py diff --git a/docs/experiments/native-minmax-f16.md b/docs/experiments/native-minmax-f16.md new file mode 100644 index 0000000000..56c5516303 --- /dev/null +++ b/docs/experiments/native-minmax-f16.md @@ -0,0 +1,25 @@ +# Direct DXIL `v_min_f16` and `v_max_f16` + +Lowers `v_min_f16` (VOP2 `0x3a` / VOP3A `0x13a`) and `v_max_f16` (VOP2 `0x39` / +VOP3A `0x139`) in the native direct-DXIL compute path +(`src/gfx1013_direct_compute.inl`) using exact 32-bit integer IEEE-754 binary16 +ordering while preserving `D[31:16]`. + +## Scope + +- Covers `-0.0h` vs `+0.0h` in both operand orders, normals, subnormals, + `±Inf`, single-NaN `minNum`/`maxNum`, dual-NaN canonical `0x7e00`, VOP3A + `abs`/`neg`/`clamp`, and `D[31:16]` preservation. +- Refuses unsupported modifiers (`omod`, `op_sel`, unused `src2`). +- Negative controls: ignoring signed zero, propagating single NaN, omitting + `clamp`, and zeroing `D[31:16]` each differ from the reference. +- Does not establish full-game execution. + +## Reproduction + +```powershell +.\tools\dev.ps1 +cmake --preset dev +cmake --build --preset dev --target compile_dxil native_store_replay +ctest --preset dev --no-tests=error -R '^native_minmax_f16_' -V +``` diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index a30ed5defb..4cc63fdf1f 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -1245,6 +1245,35 @@ struct DirectCompute { ir.binary("and", rounded, "65535"))); return true; } + if (n == "v_min_f16" || n == "v_max_f16") { + if (i.subword || i.dpp_control || permuted || i.scale || + (i.wide && ((i.a & 0x7800) || ((i.abs | i.neg) & 4) || i.src[2]))) + refuse(i.pc, "unsupported " + string(n) + " operand modifiers"); + auto half_src = [&](unsigned j) { + auto bits = ir.binary("and", operand32(i, i.src[j]), "65535"); + if (i.abs & (1u << j)) bits = ir.binary("and", bits, "32767"); + if (i.neg & (1u << j)) bits = ir.binary("xor", bits, "32768"); + return bits; + }; + auto key = [&](const string &bits) { + auto neg_key = ir.binary("sub", "-1", ir.binary("and", bits, "32767")); + return select(cmp("ne", ir.binary("and", bits, "32768"), "0"), neg_key, bits); + }; + auto a = half_src(0), b = half_src(1); + auto a_nan = cmp("ugt", ir.binary("and", a, "32767"), "31744"); + auto b_nan = cmp("ugt", ir.binary("and", b, "32767"), "31744"); + auto both_nan = ir.value("and i1 " + a_nan + ", " + b_nan); + auto ordered = select(cmp(n == "v_min_f16" ? "slt" : "sgt", key(a), key(b)), a, b); + auto result = select(both_nan, "32256", select(a_nan, b, select(b_nan, a, ordered))); + if (i.clamp) { + auto nan = cmp("ugt", ir.binary("and", result, "32767"), "31744"); + auto low = ir.value("or i1 " + nan + ", " + cmp("ne", ir.binary("and", result, "32768"), "0")); + result = select(low, "0", select(cmp("ugt", result, "15359"), "15360", result)); + } + auto kept = ir.binary("and", read(i.dst), "4294901760"); + vector_write(i.dst, ir.binary("or", kept, ir.binary("and", result, "65535"))); + return true; + } const auto rule = alu_rules.find(n); const bool byte_to_float = n.size() == 16 && n.substr(0, 15) == "v_cvt_f32_ubyte" && n.back() >= '0' && n.back() <= '3'; diff --git a/tests/cmake/native_minmax_f16.cmake b/tests/cmake/native_minmax_f16.cmake new file mode 100644 index 0000000000..b640e5b81c --- /dev/null +++ b/tests/cmake/native_minmax_f16.cmake @@ -0,0 +1,12 @@ +if(X2W_HAVE_DXCAPI AND EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_minmax_f16_controls + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_minmax_f16_check.py" + controls "$" "${DXC_BIN_DIR}") + set_tests_properties(native_minmax_f16_controls PROPERTIES TIMEOUT 60) + add_test(NAME native_minmax_f16_hardware + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_minmax_f16_check.py" + hardware "$" "${DXC_BIN_DIR}") + set_tests_properties(native_minmax_f16_hardware PROPERTIES + SKIP_RETURN_CODE 77 + TIMEOUT 60) +endif() diff --git a/tests/native_minmax_f16_check.py b/tests/native_minmax_f16_check.py new file mode 100644 index 0000000000..fcbff26d7f --- /dev/null +++ b/tests/native_minmax_f16_check.py @@ -0,0 +1,158 @@ +"""Direct compute v_min_f16 and v_max_f16 (VOP2 and VOP3A, exact integer ordering, high-half preserving).""" +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from native_dpp_quad_check import request + +MASK = 0xFFFFFFFF +SENTINEL = 0x69696969 +HIGH_SEED = 0xBEEF0000 + +CASES = ( + (0xCAFE8000, 0x12340000), # -0.0h vs +0.0h (with high junk) + (0x00000000, 0x00008000), # +0.0h vs -0.0h (reversed order) + (0x00003C00, 0x0000BC00), # +1.0h vs -1.0h + (0x00000001, 0x00008001), # +min_sub vs -min_sub + (0x00007C00, 0x0000FC00), # +Inf vs -Inf + (0x00007E00, 0x0000C000), # qNaN vs -2.0h -> -2.0h + (0x00004000, 0x00007E01), # +2.0h vs qNaN -> +2.0h + (0x00007E01, 0x00007E02), # qNaN vs qNaN -> 0x7E00 (clamped to 0) +) + + +def v_mov_lit(vdst, lit): + return [0x7E0002FF | ((vdst & 0xFF) << 17), lit & MASK] + + +def vop2(op, vdst, src0, vsrc1): + return ((op & 0x3F) << 25) | ((vdst & 0xFF) << 17) | ((vsrc1 & 0xFF) << 9) | (src0 & 0x1FF) + + +def vop3_binary(op, vdst, src0, src1, *, abs_m=0, neg_m=0, clamp=0, omod=0, op_sel=0): + w0 = (0xD4000000 | ((op & 0x3FF) << 16) | ((clamp & 1) << 15) | + ((op_sel & 0xF) << 11) | ((abs_m & 7) << 8) | (vdst & 0xFF)) + w1 = ((neg_m & 7) << 29) | ((omod & 3) << 27) | ((src1 & 0x1FF) << 9) | (src0 & 0x1FF) + return [w0, w1] + + +def program(): + code = [0xBE940304, 0xBE950380, 0xF408080A, 0xFA000020, + 0x8F078C05, 0x34100087, 0x4A101007, 0xBF8CC07F] + for ci, (a, b) in enumerate(CASES): + code += v_mov_lit(2, a) + v_mov_lit(3, b) + # 0: VOP2 v_min_f16 v4, v2, v3 + code += v_mov_lit(4, HIGH_SEED | 0x1111) + [vop2(0x3A, 4, 258, 3), 0xE0701000 | (4 * (ci * 4 + 0)), 0x80080408] + # 1: VOP2 v_max_f16 v4, v2, v3 + code += v_mov_lit(4, HIGH_SEED | 0x2222) + [vop2(0x39, 4, 258, 3), 0xE0701000 | (4 * (ci * 4 + 1)), 0x80080408] + # 2: VOP3A v_min_f16 v4, -v2, |v3| + code += v_mov_lit(4, HIGH_SEED | 0x3333) + vop3_binary(0x13A, 4, 258, 259, neg_m=1, abs_m=2) + [ + 0xE0701000 | (4 * (ci * 4 + 2)), 0x80080408] + # 3: VOP3A v_max_f16 v4, v2, v3 clamp + code += v_mov_lit(4, HIGH_SEED | 0x4444) + vop3_binary(0x139, 4, 258, 259, clamp=1) + [ + 0xE0701000 | (4 * (ci * 4 + 3)), 0x80080408] + return code + [0xBF810000] + + +def f16_key(u, ignore_signed_zero=False): + u &= 0xFFFF + if ignore_signed_zero and (u & 0x7FFF) == 0: + return 0 + return (-1 - (u & 0x7FFF)) if (u & 0x8000) else u + + +def minmax_f16(a, b, is_min, *, clamp=False, ignore_signed_zero=False, propagate_nan=False): + a &= 0xFFFF + b &= 0xFFFF + a_nan = (a & 0x7FFF) > 0x7C00 + b_nan = (b & 0x7FFF) > 0x7C00 + if a_nan and b_nan: + res = 0x7E00 + elif a_nan: + res = 0x7E00 if propagate_nan else b + elif b_nan: + res = 0x7E00 if propagate_nan else a + else: + ka = f16_key(a, ignore_signed_zero=ignore_signed_zero) + kb = f16_key(b, ignore_signed_zero=ignore_signed_zero) + res = a if ((ka < kb) if is_min else (ka > kb)) else b + if clamp: + if (res & 0x7FFF) > 0x7C00 or (res & 0x8000): + res = 0 + elif res > 0x3BFF: + res = 0x3C00 + return res & 0xFFFF + + +def reference(*, ignore_signed_zero=False, propagate_nan=False, no_clamp=False, zero_high=False): + hi = 0 if zero_high else HIGH_SEED + words = [SENTINEL] * 8 + for _lane in range(64): + for a, b in CASES: + r_min = minmax_f16(a, b, True, ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) + r_max = minmax_f16(a, b, False, ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) + r_mod = minmax_f16((a & 0xFFFF) ^ 0x8000, b & 0x7FFF, True, + ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) + r_clamp = minmax_f16(a, b, False, clamp=not no_clamp, + ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) + words.extend((hi | r_min, hi | r_max, hi | r_mod, hi | r_clamp)) + return words + [SENTINEL] * 8 + + +def controls(cli, sdk, work, root): + marker = request(work, root, program()) + output = marker.with_suffix('.cso') + result = subprocess.run([cli, sdk, work / 'cache', 'gfx1013-direct-cs@0', marker, output], + capture_output=True, text=True) + assert result.returncode == 0 and output.is_file(), (result.stdout, result.stderr) + dump = subprocess.run([Path(sdk) / 'dxc.exe', '-dumpbin', output], capture_output=True, + text=True, check=True).stdout + assert 'icmp slt i32 ' in dump and 'icmp sgt i32 ' in dump, dump + for tag, bad, msg in ( + ('omod', vop3_binary(0x13A, 4, 258, 259, omod=1) + [0xBF810000], 'unsupported v_min_f16 operand modifiers'), + ('op_sel', vop3_binary(0x139, 4, 258, 259, op_sel=1) + [0xBF810000], 'unsupported v_max_f16 operand modifiers'), + ): + sub = work / tag + sub.mkdir(exist_ok=True) + bad_marker = request(sub, root, bad) + bad_res = subprocess.run([cli, sdk, work / 'cache', 'gfx1013-direct-cs@0', bad_marker, + bad_marker.with_suffix('.cso')], capture_output=True, text=True) + assert bad_res.returncode != 0 and msg in (bad_res.stdout + bad_res.stderr), (tag, bad_res.stdout, bad_res.stderr) + assert reference() != reference(ignore_signed_zero=True) + assert reference() != reference(propagate_nan=True) + assert reference() != reference(no_clamp=True) + assert reference() != reference(zero_high=True) + print('v_min_f16/v_max_f16 DXIL verified; invalid modifiers refused; signed-zero, NaN, clamp and high-half controls differ') + return 0 + + +def hardware(replay, sdk, work, root): + marker = request(work, root, program()) + output = work / 'out.bin' + result = subprocess.run([replay, sdk, marker, work / 'cache', '0', output, '--direct']) + if result.returncode == 77: + return 77 + result.check_returncode() + expected = reference() + actual = struct.unpack(f'<{len(expected)}I', output.read_bytes()) + mismatch = next((i for i, (a, e) in enumerate(zip(actual, expected)) if a != e), None) + if mismatch is not None: + raise AssertionError(f'word {mismatch}: got {actual[mismatch]:08x}, expected {expected[mismatch]:08x}') + print('v_min_f16/v_max_f16: 2048 result words (high halves kept) and 16 guards matched') + return 0 + + +def main(mode, executable, sdk): + with tempfile.TemporaryDirectory(prefix='x2w_native_minmax_f16_') as temp: + work = Path(temp) + shader = work / 'root.hlsl' + shader.write_text('#define SIG "DescriptorTable(UAV(u0,numDescriptors=8))"') + root = work / 'root.bin' + subprocess.run([Path(sdk) / 'dxc.exe', '-T', 'rootsig_1_1', '-E', 'SIG', '-Fo', root, shader], check=True) + return (controls if mode == 'controls' else hardware)(executable, sdk, work, root.read_bytes()) + + +if __name__ == '__main__': + raise SystemExit(main(sys.argv[1], sys.argv[2], sys.argv[3])) From 58468b353ed61c325719c602fe7d925d6220d346 Mon Sep 17 00:00:00 2001 From: R0D4X Date: Sat, 28 Nov 2026 10:55:15 +0100 Subject: [PATCH 2/2] tests: add CPU IR value verification and inline 248 coverage for v_min_f16/v_max_f16 --- docs/experiments/native-minmax-f16.md | 17 ++-- tests/cmake/native_minmax_f16.cmake | 6 ++ tests/native_minmax_f16_check.py | 120 +++++++++++++++++--------- 3 files changed, 97 insertions(+), 46 deletions(-) diff --git a/docs/experiments/native-minmax-f16.md b/docs/experiments/native-minmax-f16.md index 56c5516303..2704b41824 100644 --- a/docs/experiments/native-minmax-f16.md +++ b/docs/experiments/native-minmax-f16.md @@ -8,11 +8,18 @@ ordering while preserving `D[31:16]`. ## Scope - Covers `-0.0h` vs `+0.0h` in both operand orders, normals, subnormals, - `±Inf`, single-NaN `minNum`/`maxNum`, dual-NaN canonical `0x7e00`, VOP3A - `abs`/`neg`/`clamp`, and `D[31:16]` preservation. -- Refuses unsupported modifiers (`omod`, `op_sel`, unused `src2`). + `±Inf`, single-NaN `minNum`/`maxNum`, dual-NaN canonical `0x7e00`, all nine + inline float constants (`240..248`: `±0.5`, `±1.0`, `±2.0`, `±4.0`, and + `1/(2*pi)` -> `0x3118`), VOP3A `abs`/`neg`/`clamp`, and `D[31:16]` + preservation. +- Refuses unsupported modifiers (`omod`, `op_sel`, unused `src2`) and + graphics-input mode. +- `native_minmax_f16_ir` evaluates the emitted IR on CPU against the Python + reference model for both the main batch (32 results) and all 9 inline float + constants (27 results), plus modifier refusals. - Negative controls: ignoring signed zero, propagating single NaN, omitting - `clamp`, and zeroing `D[31:16]` each differ from the reference. + `clamp`, treating inline constants as zero, and zeroing `D[31:16]` each + differ from the reference. - Does not establish full-game execution. ## Reproduction @@ -20,6 +27,6 @@ ordering while preserving `D[31:16]`. ```powershell .\tools\dev.ps1 cmake --preset dev -cmake --build --preset dev --target compile_dxil native_store_replay +cmake --build --preset dev --target native_compute_ir_dump compile_dxil native_store_replay ctest --preset dev --no-tests=error -R '^native_minmax_f16_' -V ``` diff --git a/tests/cmake/native_minmax_f16.cmake b/tests/cmake/native_minmax_f16.cmake index 8e31048022..a0578ded50 100644 --- a/tests/cmake/native_minmax_f16.cmake +++ b/tests/cmake/native_minmax_f16.cmake @@ -1,3 +1,9 @@ +if(TARGET native_compute_ir_dump AND EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_minmax_f16_ir + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_minmax_f16_check.py" + ir "$") + set_tests_properties(native_minmax_f16_ir PROPERTIES TIMEOUT 60) +endif() if(TARGET compile_dxil AND TARGET native_store_replay AND EXISTS "${INTAKE_PYTHON}") add_test(NAME native_minmax_f16_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_minmax_f16_check.py" diff --git a/tests/native_minmax_f16_check.py b/tests/native_minmax_f16_check.py index c2a295b446..e9be347825 100644 --- a/tests/native_minmax_f16_check.py +++ b/tests/native_minmax_f16_check.py @@ -6,6 +6,7 @@ import tempfile from native_dpp_quad_check import request +from native_f16_alu_check import dump as dump_ir, interpret as interpret_ir MASK = 0xFFFFFFFF SENTINEL = 0x69696969 @@ -22,7 +23,7 @@ (0x00007E01, 0x00007E02), # qNaN vs qNaN -> 0x7E00 (clamped to 0) ) -# Inline float constants 240..247 (0.5, -0.5, 1.0, -1.0, 2.0, -2.0, 4.0, -4.0) paired with VGPR b. +# All nine inline float constants 240..248 (0.5, -0.5, 1.0, -1.0, 2.0, -2.0, 4.0, -4.0, 1/(2*pi)) paired with VGPR b. INLINE_CASES = ( (240, 0x3800, 0x00000000), # 0.5h vs +0.0h (241, 0xB800, 0x00000000), # -0.5h vs +0.0h @@ -32,6 +33,7 @@ (245, 0xC000, 0x0000BC00), # -2.0h vs -1.0h (246, 0x4400, 0x00004000), # 4.0h vs 2.0h (247, 0xC400, 0x0000C000), # -4.0h vs -2.0h + (248, 0x3118, 0x00000000), # 1/(2*pi)h (0x3118) vs +0.0h (f32 low half would be 0xf983, negative!) ) @@ -69,12 +71,10 @@ def program_inline(): 0x8F078C05, 0x34100087, 0x4A101007, 0xBF8CC07F] for ci, (src0, _f16, b) in enumerate(INLINE_CASES): code += v_mov_lit(3, b) - code += v_mov_lit(4, HIGH_SEED | 0x1111) + [vop2(0x3A, 4, src0, 3), 0xE0701000 | (4 * (ci * 4 + 0)), 0x80080408] - code += v_mov_lit(4, HIGH_SEED | 0x2222) + [vop2(0x39, 4, src0, 3), 0xE0701000 | (4 * (ci * 4 + 1)), 0x80080408] - code += v_mov_lit(4, HIGH_SEED | 0x3333) + vop3_binary(0x13A, 4, src0, 259, abs_m=1) + [ - 0xE0701000 | (4 * (ci * 4 + 2)), 0x80080408] - code += v_mov_lit(4, HIGH_SEED | 0x4444) + vop3_binary(0x139, 4, src0, 259, clamp=1) + [ - 0xE0701000 | (4 * (ci * 4 + 3)), 0x80080408] + code += v_mov_lit(4, HIGH_SEED | 0x1111) + [vop2(0x3A, 4, src0, 3), 0xE0701000 | (4 * (ci * 3 + 0)), 0x80080408] + code += v_mov_lit(4, HIGH_SEED | 0x2222) + [vop2(0x39, 4, src0, 3), 0xE0701000 | (4 * (ci * 3 + 1)), 0x80080408] + code += v_mov_lit(4, HIGH_SEED | 0x3333) + vop3_binary(0x13A, 4, src0, 259, abs_m=1, clamp=1) + [ + 0xE0701000 | (4 * (ci * 3 + 2)), 0x80080408] return code + [0xBF810000] @@ -108,34 +108,69 @@ def minmax_f16(a, b, is_min, *, clamp=False, ignore_signed_zero=False, propagate return res & 0xFFFF -def reference_main(*, ignore_signed_zero=False, propagate_nan=False, no_clamp=False, zero_high=False): +def lane_results_main(*, ignore_signed_zero=False, propagate_nan=False, no_clamp=False, zero_high=False): hi = 0 if zero_high else HIGH_SEED - words = [SENTINEL] * 8 - for _lane in range(64): - for a, b in CASES: - r_min = minmax_f16(a, b, True, ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) - r_max = minmax_f16(a, b, False, ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) - r_mod = minmax_f16((a & 0xFFFF) ^ 0x8000, b & 0x7FFF, True, - ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) - r_clamp = minmax_f16(a, b, False, clamp=not no_clamp, - ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) - words.extend((hi | r_min, hi | r_max, hi | r_mod, hi | r_clamp)) - return words + [SENTINEL] * 8 - - -def reference_inline(*, inline_as_zero=False, zero_high=False): + words = [] + for a, b in CASES: + r_min = minmax_f16(a, b, True, ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) + r_max = minmax_f16(a, b, False, ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) + r_mod = minmax_f16((a & 0xFFFF) ^ 0x8000, b & 0x7FFF, True, + ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) + r_clamp = minmax_f16(a, b, False, clamp=not no_clamp, + ignore_signed_zero=ignore_signed_zero, propagate_nan=propagate_nan) + words.extend((hi | r_min, hi | r_max, hi | r_mod, hi | r_clamp)) + return words + + +def lane_results_inline(*, inline_as_zero=False, zero_high=False): hi = 0 if zero_high else HIGH_SEED - words = [SENTINEL] * 8 - for _lane in range(64): - for _src0, f16, b in INLINE_CASES: - a = 0x0000 if inline_as_zero else f16 - words.extend(( - hi | minmax_f16(a, b, True), - hi | minmax_f16(a, b, False), - hi | minmax_f16(a & 0x7FFF, b, True), - hi | minmax_f16(a, b, False, clamp=True), - )) - return words + [SENTINEL] * 8 + words = [] + for _src0, f16, b in INLINE_CASES: + a = 0x0000 if inline_as_zero else f16 + words.extend(( + hi | minmax_f16(a, b, True), + hi | minmax_f16(a, b, False), + hi | minmax_f16(a & 0x7FFF, b, True, clamp=True), + )) + return words + + +def reference_main(**kwargs): + lane = lane_results_main(**kwargs) + return [SENTINEL] * 8 + lane * 64 + [SENTINEL] * 8 + + +def reference_inline(**kwargs): + lane = lane_results_inline(**kwargs) + [SENTINEL] * (32 - 3 * len(INLINE_CASES)) + return [SENTINEL] * 8 + lane * 64 + [SENTINEL] * 8 + + +REFUSALS = ( + ('omod', vop3_binary(0x13A, 4, 258, 259, omod=1) + [0xBF810000], 'unsupported v_min_f16 operand modifiers'), + ('op_sel', vop3_binary(0x139, 4, 258, 259, op_sel=1) + [0xBF810000], 'unsupported v_max_f16 operand modifiers'), +) + + +def run_ir(dumper): + with tempfile.TemporaryDirectory(prefix='x2w_native_minmax_f16_ir_') as temp: + work = Path(temp) + for tag, prog, expected in (('main', program_main(), lane_results_main()), + ('inline', program_inline(), lane_results_inline())): + run = dump_ir(dumper, work, prog, tag) + assert run.returncode == 0, (tag, run.stderr) + stores = sorted(interpret_ir(run.stdout)) + assert [addr for addr, _ in stores] == [4 * i for i in range(len(expected))], (tag, stores[:4]) + mismatch = next(((i, got, want) for i, ((_, got), want) in enumerate(zip(stores, expected)) if got != want), + None) + if mismatch is not None: + i, got, want = mismatch + raise AssertionError(f'{tag} slot {i}: got {got:08x}, expected {want:08x}') + for tag, bad, msg in REFUSALS: + run = dump_ir(dumper, work, bad, f'refused_{tag}') + assert run.returncode != 0 and msg in (run.stdout + run.stderr), (tag, run.stdout, run.stderr) + print(f'native_minmax_f16 IR: {len(lane_results_main()) + len(lane_results_inline())} CPU-evaluated results ' + f'(including all 9 inline float constants 240..248) match the model; {len(REFUSALS)} refusals verified') + return 0 def controls(cli, sdk, work, root): @@ -151,11 +186,10 @@ def controls(cli, sdk, work, root): text=True, check=True).stdout assert 'icmp slt i32 ' in dump and 'icmp sgt i32 ' in dump, dump if tag == 'inline': - assert '15360' in dump and '16384' in dump, 'inline f16 constants 1.0/2.0 must lower to 0x3c00/0x4000' - for tag, bad, msg in ( - ('omod', vop3_binary(0x13A, 4, 258, 259, omod=1) + [0xBF810000], 'unsupported v_min_f16 operand modifiers'), - ('op_sel', vop3_binary(0x139, 4, 258, 259, op_sel=1) + [0xBF810000], 'unsupported v_max_f16 operand modifiers'), - ): + # 0.5h (14336), 4.0h (17408), and 1/(2*pi)h (12568) appear only via inline-constant mapping. + assert '14336' in dump and '17408' in dump and '12568' in dump, ( + 'inline f16 constants 0.5/4.0/inv2pi must lower to 0x3800/0x4400/0x3118') + for tag, bad, msg in REFUSALS: sub = work / tag sub.mkdir(exist_ok=True) bad_marker = request(sub, root, bad) @@ -186,11 +220,15 @@ def hardware(replay, sdk, work, root): mismatch = next((i for i, (a, e) in enumerate(zip(actual, expected)) if a != e), None) if mismatch is not None: raise AssertionError(f'{tag} word {mismatch}: got {actual[mismatch]:08x}, expected {expected[mismatch]:08x}') - print(f'{tag}: 2048 result words (high halves kept) and 16 guards matched') + print(f'{tag}: all {len(expected)} words (high halves and guards kept) matched') return 0 -def main(mode, executable, sdk): +def main(args): + mode = args[0] + if mode == 'ir': + return run_ir(args[1]) + executable, sdk = args[1], args[2] with tempfile.TemporaryDirectory(prefix='x2w_native_minmax_f16_') as temp: work = Path(temp) shader = work / 'root.hlsl' @@ -201,4 +239,4 @@ def main(mode, executable, sdk): if __name__ == '__main__': - raise SystemExit(main(sys.argv[1], sys.argv[2], sys.argv[3])) + raise SystemExit(main(sys.argv[1:]))