Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/experiments/native-frexp-f16.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
12 changes: 8 additions & 4 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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<Word> constant32(const Inst &i, unsigned r) {
if (r >= 128 && r <= 192) return r - 128;
if (r >= 193 && r <= 208) return Word(192) - r;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/cmake/native_frexp_f16.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<TARGET_FILE:native_compute_ir_dump>")
set_tests_properties(native_frexp_f16_ir PROPERTIES TIMEOUT 60)
endif()
43 changes: 43 additions & 0 deletions tests/native_frexp_f16_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]))
Loading