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
39 changes: 39 additions & 0 deletions docs/experiments/native-direct-cvt-f16-i16.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Binary16 <-> 16-bit integer conversions in the direct C++ compute path (#3148)

`src/gfx1013_direct_compute.inl` now lowers `v_cvt_f16_u16`, `v_cvt_f16_i16`, `v_cvt_u16_f16`, and `v_cvt_i16_f16`
(VOP1 and VOP3A) in the direct C++ compute path (`DirectCompute::float_alu()`):

- **High-half preservation:** Every operation reads `S0[15:0]`, writes the converted 16-bit result into `D[15:0]`, and
preserves `D[31:16]`, matching LLVM `GCNSubtarget::zeroesHigh16BitsOfDest` (`"On gfx10, all 16-bit instructions preserve
the high bits"`).
- **`v_cvt_f16_u16` / `v_cvt_f16_i16`:** Sign-extends (for `i16`) or zero-extends (for `u16`) `S0[15:0]`, converts to
binary32 (`sitofp`/`uitofp`), rounds to nearest-even binary16 with `packed_half_rne()` (e.g. `2049 -> 2048`, `2051 ->
2052`, `65519 -> 65504`, `65520 -> +inf`, `32767 -> 32768.0h`), and optionally applies VOP3 `clamp` (`[0.0, 1.0]`).
- **`v_cvt_u16_f16` / `v_cvt_i16_f16`:** Applies optional VOP3 `abs`/`neg` on `S0[15:0]`, widens with
`@dx.op.legacyF16ToF32`, truncates toward zero, saturates to `[0, 65535]` / `[-32768, 32767]`, and maps NaN to `0`.
- **Mode & modifier contract:** Included in `half_binary` in `DirectCompute::lower()` so the default FP16 rounding
(`round16 = 0` RNE) and denormal (`denorm16 = 3` keep, `FP16_OVFL = 0`) mode contract is enforced. Refuses DPP, SDWA,
output modifiers (`omod`), `clamp` on `*_f16` integer targets, `op_sel` high-half bits, `abs`/`neg` on integer sources,
unused VOP3 `src1`/`src2`, and non-default FP16 modes. Inline float constants use the same binary16 bit table as
`v_add_f16`.

## Fixture

[`tests/native_cvt_f16_i16_check.py`](../../tests/native_cvt_f16_i16_check.py) via
[`tests/cmake/native_half_int_cvt.cmake`](../../tests/cmake/native_half_int_cvt.cmake) (`native_cvt_f16_i16_ir`,
`native_cvt_f16_i16_controls`, `native_cvt_f16_i16_hardware`):

- **IR (`native_cvt_f16_i16_ir`):** Evaluates lane 0 across 82 conversion cases (VGPR, SGPR, literal, inline integer,
inline float, VOP3 `clamp`, `abs`, `neg`, `-|x|`, rounding ties, saturation, subnormals, infinities, NaNs, and
non-zero upper halves) plus 38 refusal cases (DPP, SDWA, `omod`, `op_sel`, illegal `abs`/`neg`/`clamp`, `src1`, and
MODE writes).
- **Controls (`native_cvt_f16_i16_controls`):** Compiles the full 82-case shader to DXIL via `compile_dxil` and checks
`dxc.exe -dumpbin` plus all 38 refusals.
- **Hardware (`native_cvt_f16_i16_hardware`):** Replays all 82 conversions on hardware via `native_store_replay --direct`.

Measured on 27.11.2026, `main` `429f11d5` plus this change, Windows 10.0.26100 (`dev` preset): `native_cvt_f16_i16_ir`
(`1.09s`: 82 conversions + 38 refusals), `native_cvt_f16_i16_controls` (`3.84s`), all 47 `native_*_(controls|ir)` tests
(`54.43s`), and `tests/docs_links_check.py` (`101` Markdown files, `589` links) passed; `native_cvt_f16_i16_hardware`
skipped (`77`, Quadro M1000M does not support shader model 6.6 with wave32, so GPU readback is not measured). Five
broken-lowering sensitivity checks (`zero_high_half`, `drop_ordered_nan`, `swap_signed`, `drop_clamp`,
`wrong_inline_float`) each failed `native_cvt_f16_i16_ir`.
70 changes: 69 additions & 1 deletion src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,73 @@ struct DirectCompute {
vector_write(i.dst, ir.binary("or", kept, ir.binary("and", result, "65535")));
return true;
}
if (n == "v_cvt_f16_u16" || n == "v_cvt_f16_i16" || n == "v_cvt_u16_f16" || n == "v_cvt_i16_f16") {
if (graphics_input)
return false;
const bool to_f16 = n == "v_cvt_f16_u16" || n == "v_cvt_f16_i16";
const bool is_signed = n == "v_cvt_f16_i16" || n == "v_cvt_i16_f16";
if (i.dpp_control || permuted)
refuse(i.pc, "unsupported " + string(n) + " DPP form");
if (i.subword)
refuse(i.pc, "unsupported " + string(n) + " SDWA form");
if (i.scale || (!to_f16 && i.clamp))
refuse(i.pc, "unsupported " + string(n) + " clamp or omod");
if (i.wide && (i.a & 0x7800))
refuse(i.pc, "unsupported " + string(n) + " high-half op_sel");
if ((to_f16 && (i.abs || i.neg)) || (i.wide && (((i.abs | i.neg) & 6) || i.src[1] || i.src[2])))
refuse(i.pc, "unsupported " + string(n) + " operand modifiers");
if (!half_mode_known || ((mode_bits ^ 0xf0u) & 0x8000ccu))
refuse(i.pc, "unsupported binary16 rounding or denormal mode");
const unsigned r = i.src[0];
string bits;
if (r >= 240 && r <= 248) {
constexpr unsigned halves[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000,
0xc000, 0x4400, 0xc400, 0x3118};
bits = to_string(halves[r - 240]);
} else if (r == 255) {
bits = to_string(i.literal & 65535u);
} else if (r >= 128 && r <= 192) {
bits = to_string((r - 128) & 65535u);
} else if (r >= 193 && r <= 208) {
bits = to_string((192u - r) & 65535u);
} else {
bits = ir.binary("and", operand32(i, r), "65535");
}
string result;
if (to_f16) {
string f;
if (is_signed) {
auto s16 = ir.binary("ashr", ir.binary("shl", bits, "16"), "16");
f = ir.value("sitofp i32 " + s16 + " to float");
} else {
f = ir.value("uitofp i32 " + bits + " to float");
}
auto fbits = ir.value("bitcast float " + f + " to i32");
result = packed_half_rne(fbits);
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));
}
} else {
if (i.abs & 1)
bits = ir.binary("and", bits, "32767");
if (i.neg & 1)
bits = ir.binary("xor", bits, "32768");
auto f = ir.invoke("float", "legacyF16ToF32", 131, {{"i32", bits}}, "nounwind readnone");
auto lower = ir.value("fcmp oge float " + f + ", " + (is_signed ? "-32768.0" : "0.0"));
auto upper = ir.value("fcmp olt float " + f + ", " + (is_signed ? "32768.0" : "65536.0"));
auto safe = select_float(ir.value("and i1 " + lower + ", " + upper), f, "0.0");
auto converted = ir.value(string(is_signed ? "fptosi" : "fptoui") + " float " + safe + " to i32");
auto bounded = select(upper, converted, is_signed ? "32767" : "65535");
bounded = select(lower, bounded, is_signed ? "32768" : "0");
auto ordered = ir.value("fcmp ord float " + f + ", " + f);
result = ir.binary("and", select(ordered, bounded, "0"), "65535");
}
auto kept = ir.binary("and", read(i.dst), "4294901760");
vector_write(i.dst, ir.binary("or", kept, ir.binary("and", result, "65535")));
return true;
}
if (n == "v_ldexp_f32") {
// pixel_aot.py ldexp32 (#3013): exact binary32 scaling of S0 by 2^S1. Zero, Inf and NaN pass unchanged,
// a denormal S0 is normalized rather than flushed, and a denormal result rounds to nearest even. S1 is
Expand Down Expand Up @@ -3061,7 +3128,8 @@ struct DirectCompute {
if (!p.reached[k])
continue;
const auto &op = p.code[k];
if (binary16_alu(op.name))
if (binary16_alu(op.name) || op.name == "v_cvt_f16_u16" || op.name == "v_cvt_f16_i16" ||
op.name == "v_cvt_u16_f16" || op.name == "v_cvt_i16_f16")
half_binary = true;
if (op.name == "s_round_mode" || op.name == "s_denorm_mode" || op.name == "s_setreg_b32" ||
op.name == "s_setreg_imm32_b32")
Expand Down
19 changes: 19 additions & 0 deletions tests/cmake/native_half_int_cvt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# v_cvt_f16_u16, v_cvt_f16_i16, v_cvt_u16_f16 and v_cvt_i16_f16 in the direct C++ compute path.
# Named to sort after native_f16_alu.cmake so native_compute_ir_dump is already defined.
if(TARGET native_compute_ir_dump AND EXISTS "${INTAKE_PYTHON}")
add_test(NAME native_cvt_f16_i16_ir COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_cvt_f16_i16_check.py" ir
"$<TARGET_FILE:native_compute_ir_dump>")
set_tests_properties(native_cvt_f16_i16_ir PROPERTIES TIMEOUT 60)
endif()
if(TARGET compile_dxil AND TARGET native_store_replay AND
EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
add_test(NAME native_cvt_f16_i16_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_cvt_f16_i16_check.py" controls
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_cvt_f16_i16_controls PROPERTIES TIMEOUT 60)
add_test(NAME native_cvt_f16_i16_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_cvt_f16_i16_check.py" hardware
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_cvt_f16_i16_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
Loading
Loading