From a7b31d1f0a0ae82db0eedc5ac0824ded604c1a73 Mon Sep 17 00:00:00 2001 From: JohnConnorNPC <128232619+JohnConnorNPC@users.noreply.github.com> Date: Sat, 28 Nov 2026 04:06:33 +1000 Subject: [PATCH] shader: read inline float sources as binary16 in v_frexp_mant_f16 and v_frexp_exp_i16_f16 The lowering masked the f32 pattern of an inline float (240-248) to its low 16 bits, so v_frexp_mant_f16 v3, 1.0 gave 0x0000 instead of 0x3800. Read the binary16 table that v_add_f16 already uses, now shared as inline_half_words, and add native_frexp_f16_ir, which evaluates the emitted IR for all nine constants without a GPU or DXC. --- docs/experiments/native-frexp-f16.md | 8 +++++- src/gfx1013_direct_compute.inl | 12 +++++--- tests/cmake/native_frexp_f16.cmake | 8 ++++++ tests/native_frexp_f16_check.py | 43 ++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 5 deletions(-) diff --git a/docs/experiments/native-frexp-f16.md b/docs/experiments/native-frexp-f16.md index 7fe0d34373..ac3db3dc02 100644 --- a/docs/experiments/native-frexp-f16.md +++ b/docs/experiments/native-frexp-f16.md @@ -10,6 +10,12 @@ binary16 manipulation while preserving `D[31:16]`. - Covers ±0.0h, normals, smallest normal, largest and smallest subnormals, and +Inf across VOP1 and VOP3A (`neg`, `abs`, and `clamp` on `v_frexp_mant_f16`), verifying `D[31:16]` preservation on every output. +- An inline float source (`0.5` … `-4.0`, `1/(2pi)`; encodings 240–248) reads + the binary16 value of the constant, as `v_add_f16` does, not the low half of + its f32 pattern. `native_frexp_f16_ir` evaluates the emitted IR (no GPU, no + DXC) for all nine constants with both ops, three VOP3A forms with + `abs`/`neg`/`clamp` and one VGPR control: `v_frexp_mant_f16 v3, 1.0` must + give `0x3800` and `v_frexp_exp_i16_f16 v3, 1.0` must give `1`. - Refuses unsupported modifiers (`clamp` on `v_frexp_exp_i16_f16`, `omod` on `v_frexp_mant_f16`). - Negative controls: flushing denormals, omitting `clamp`, and zeroing @@ -21,6 +27,6 @@ binary16 manipulation 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 compile_dxil native_store_replay native_compute_ir_dump ctest --preset dev --no-tests=error -R '^native_frexp_f16_' -V ``` diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index fb6e462aea..d000e0c55d 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -3,6 +3,9 @@ struct DirectCompute { static constexpr Word inline_float_words[]{0x3f000000, 0xbf000000, 0x3f800000, 0xbf800000, 0x40000000, 0xc0000000, 0x40800000, 0xc0800000, 0x3e22f983}; + // The same nine constants as binary16, which an f16 operand reads instead of the f32 pattern. + static constexpr Word inline_half_words[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000, + 0xc000, 0x4400, 0xc400, 0x3118}; static std::optional constant32(const Inst &i, unsigned r) { if (r >= 128 && r <= 192) return r - 128; if (r >= 193 && r <= 208) return Word(192) - r; @@ -1116,7 +1119,10 @@ struct DirectCompute { if (i.subword || i.dpp_control || permuted || i.scale || (exp_op && i.clamp) || (i.wide && ((i.a & 0x7800) || ((i.abs | i.neg) & 6) || i.src[1] || i.src[2]))) refuse(i.pc, "unsupported " + string(n) + " operand modifiers"); - auto bits = ir.binary("and", operand32(i, i.src[0]), "65535"); + // An inline float (240-248) reads its binary16 value, as v_add_f16 does. + const unsigned r = i.src[0]; + auto bits = r >= 240 && r <= 248 ? to_string(inline_half_words[r - 240]) + : ir.binary("and", operand32(i, r), "65535"); if (i.abs & 1) bits = ir.binary("and", bits, "32767"); if (i.neg & 1) bits = ir.binary("xor", bits, "32768"); auto sign = ir.binary("and", bits, "32768"); @@ -1280,9 +1286,7 @@ struct DirectCompute { } else if (r >= 240 && r <= 248) { // Inline floats are f32 patterns. The f16 operation consumes the // binary16 rounding of that value. - constexpr unsigned halves[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000, - 0xc000, 0x4400, 0xc400, 0x3118}; - bits = to_string(halves[r - 240]); + bits = to_string(inline_half_words[r - 240]); } else if (r == 255) bits = to_string(i.literal & 65535u); else if (r >= 128 && r <= 192) diff --git a/tests/cmake/native_frexp_f16.cmake b/tests/cmake/native_frexp_f16.cmake index 480dc327dc..4ab9311759 100644 --- a/tests/cmake/native_frexp_f16.cmake +++ b/tests/cmake/native_frexp_f16.cmake @@ -10,3 +10,11 @@ if(X2W_HAVE_DXCAPI AND EXISTS "${INTAKE_PYTHON}") SKIP_RETURN_CODE 77 TIMEOUT 60) endif() +# No GPU and no DXC: evaluates the emitted IR (inline float sources). native_compute_ir_dump is +# defined in native_f16_alu.cmake, which is included before this file. +if(TARGET native_compute_ir_dump AND EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_frexp_f16_ir + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_frexp_f16_check.py" + ir "$") + set_tests_properties(native_frexp_f16_ir PROPERTIES TIMEOUT 60) +endif() diff --git a/tests/native_frexp_f16_check.py b/tests/native_frexp_f16_check.py index 14135acbb8..1fd2d45f1d 100644 --- a/tests/native_frexp_f16_check.py +++ b/tests/native_frexp_f16_check.py @@ -137,6 +137,47 @@ def hardware(replay, sdk, work, root): return 0 +def ir(dumper): + """No GPU and no DXC: evaluate lane 0 of the module the direct frontend builds. + + An inline float source (240-248) is a binary16 operand here, so it must read the binary16 value + of the constant (as v_add_f16 does), not the low half of its f32 pattern. + """ + from native_direct_container_check import words + from native_f16_alu_check import INLINE_F16, SHIFT, compute_container, interpret, store + from native_scalar_lds_check import scalar, sop1, vmove + + # Pinned rows, independent of the lowering: 1.0 = 0.5 * 2^1 and 1/(2pi) = 0x3118 = 0.6367 * 2^-2. + assert frexp_f16(0x3C00) == (1, 0x3800) and frexp_f16(0x3118) == (0xFFFE, 0x3918) + cases = [] + for src, half in INLINE_F16.items(): + cases.append(([vop1(0x59, 3, src)], frexp_f16(half)[1])) + cases.append(([vop1(0x5A, 3, src)], frexp_f16(half)[0])) + cases.append((vop3_unary(0x1D9, 3, 245, abs0=1, clamp=1), frexp_f16(0x4000, clamp=True)[1])) # |-2.0| clamp + cases.append((vop3_unary(0x1D9, 3, 242, neg0=1), frexp_f16(0xBC00)[1])) # -1.0 + cases.append((vop3_unary(0x1DA, 3, 248, neg0=1), frexp_f16(0xB118)[0])) # -1/(2pi) + cases.append((v_mov_lit(1, 0x12343C00) + [vop1(0x59, 3, 257)], 0x3800)) # VGPR source + code = [sop1(3, 20, 4), sop1(3, 21, 128), 0xF408080A, 0xFA000020, + scalar(0x1E, 7, 5, 128 + SHIFT), vmove(8, 7), sop1(3, 126, 129)] + want = [] + for k, (op, result) in enumerate(cases): + code += v_mov_lit(3, HIGH_SEED | (0x1000 + k)) + op + store(4 * k, 3) + want.append(HIGH_SEED | result) + code.append(0xBF810000) + with tempfile.TemporaryDirectory(prefix='x2w_native_frexp_f16_ir_') as temp: + path = Path(temp) / 'frexp.cdx' + path.write_bytes(compute_container(words(*code))) + run = subprocess.run([dumper, str(path)], capture_output=True, text=True) + assert run.returncode == 0, run.stderr + stores = sorted(interpret(run.stdout)) + assert [address for address, _ in stores] == [4 * k for k in range(len(want))], stores[:4] + bad = [(k, f'{got:08x}', f'{expected:08x}') for k, ((_, got), expected) in enumerate(zip(stores, want)) + if got != expected] + assert not bad, bad + print(f'v_frexp_mant_f16/exp_i16_f16 IR: {len(want)} results matched, inline floats read as binary16') + return 0 + + def main(mode, executable, sdk): with tempfile.TemporaryDirectory(prefix='x2w_native_frexp_f16_') as temp: work = Path(temp) @@ -148,4 +189,6 @@ def main(mode, executable, sdk): if __name__ == '__main__': + if len(sys.argv) == 3 and sys.argv[1] == 'ir': + raise SystemExit(ir(sys.argv[2])) raise SystemExit(main(sys.argv[1], sys.argv[2], sys.argv[3]))