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-fma-f16.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Scalar binary16 FMA in the direct C++ compute path (#3186)

`src/gfx1013_direct_compute.inl` now lowers `v_fma_f16` (VOP3), `v_fmac_f16` (VOP2/VOP3A), `v_fmamk_f16` (VOP2), and
`v_fmaak_f16` (VOP2) in the direct C++ compute path (`DirectCompute::float_alu()`):

- **Exact single-rounded FMA (`fused_half_rne()`):** Reuses the shared 32-bit integer `fused_half_rne(a, b, c)` helper
(introduced in #3151 for `v_pk_fma_f16`) so `a * b + c` computes an exact product, aligns the addend with a sticky
bit, and rounds once to nearest-even binary16 rather than double-rounding through binary32.
- **High-half preservation:** Every operation reads `16`-bit sources (preserving `S0..S2[15:0]` / `K[15:0]` / `D[15:0]`),
writes the result into `D[15:0]`, and keeps `D[31:16]` unchanged.
- **Modifiers & MODE contract:** Supports VOP3 `abs`, `neg`, and `clamp` (`[0.0, 1.0]` saturation); refuses DPP, SDWA,
`omod`, high-half `op_sel`, third-source modifiers/`src2` on `v_fmac_f16_e64`, and non-default FP16 MODE writes
(`half_binary` in `DirectCompute::lower()`).
- **Test adjustment:** Updated the unlowered-instruction refusal placeholder in
[`tests/native_f16_alu_check.py`](../../tests/native_f16_alu_check.py) from `v_fmac_f16` to `v_pk_fmac_f16`, and added
`@dx.op.unaryBits.i32(i32 33, ...)` (`FirstbitHi`) evaluation to its IR interpreter so `native_fma_f16_ir` evaluates
`fused_half_rne()` directly.

## Fixture

[`tests/native_fma_f16_check.py`](../../tests/native_fma_f16_check.py) via
[`tests/cmake/native_fma_f16.cmake`](../../tests/cmake/native_fma_f16.cmake) (`native_fma_f16_ir`,
`native_fma_f16_controls`, `native_fma_f16_hardware`):

- **IR (`native_fma_f16_ir`):** Evaluates lane 0 across 41 cases (all 4 `#2073` single-rounding vs double-rounding tie
witnesses across all 4 instructions, SGPR/literal/inline-float/inline-int sources, `dst = src0` overlap, VOP3
`abs`/`neg`/`clamp`, signed zeros, exact cancellation, subnormals, `diff_e >= 30` sticky-bit midpoint, underflow
ties, overflow, `Inf`/`NaN`) against an exact `fractions.Fraction` reference, plus 12 refusals.
- **Controls (`native_fma_f16_controls`):** Compiles the full 41-case shader to DXIL via `compile_dxil` and verifies all
12 refusals.
- **Hardware (`native_fma_f16_hardware`):** Replays all 41 cases via `native_store_replay --direct`.

Measured on 27.11.2026, `main` `c45bf9ab` plus this change, Windows 10.0.26100 (`dev` preset): `native_fma_f16_ir`
(41 cases + 12 refusals), `native_fma_f16_controls`, `native_f16_alu_ir`, `native_packed_f16_controls`,
`native_packed_f16_pixel_warp`, `native_packed_f16_pixel_hardware`, all 57 `^native_.*_(controls|ir)$` tests, and
`tests/docs_links_check.py` passed; `native_fma_f16_hardware` skipped (`77`,
`Intel(R) HD Graphics 530` `31.0.101.2111`: requires shader model 6.6 with wave32, so GPU compute readback is
not measured). Five broken-lowering sensitivity checks (`zero_high_half`, `drop_sticky`, `swap_fmamk_fmaak`,
`drop_clamp`, `drop_inf_cancel_nan`) each failed `native_fma_f16_ir` (exit `1`).
58 changes: 57 additions & 1 deletion src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,58 @@ struct DirectCompute {
ir.binary("and", rounded, "65535")));
return true;
}
if (n == "v_fma_f16" || n == "v_fmac_f16" || n == "v_fmamk_f16" || n == "v_fmaak_f16") {
if (graphics_input)
return false;
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)
refuse(i.pc, "unsupported " + string(n) + " omod");
if (i.wide && (i.a & 0x7800))
refuse(i.pc, "unsupported " + string(n) + " high-half op_sel");
if (n == "v_fmac_f16" && i.wide && (((i.abs | i.neg) & 4) || i.src[2]))
refuse(i.pc, "unsupported v_fmac_f16 third-source modifier");
if (!half_mode_known || ((mode_bits ^ 0xf0u) & 0x8000ccu))
refuse(i.pc, "unsupported binary16 rounding or denormal mode");
auto half_bits = [&](unsigned j) {
const unsigned r = i.src[j];
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");
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 a = half_bits(0);
auto b = n == "v_fmamk_f16" ? to_string(i.literal & 65535u) : half_bits(1);
auto c = n == "v_fma_f16" ? half_bits(2)
: n == "v_fmac_f16" ? ir.binary("and", read(i.dst), "65535")
: n == "v_fmamk_f16" ? half_bits(1)
: to_string(i.literal & 65535u);
auto result = fused_half_rne(a, b, c);
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';
Expand Down Expand Up @@ -3362,7 +3414,11 @@ struct DirectCompute {
op.name == "v_cvt_f16_u16" ||
op.name == "v_cvt_f16_i16" ||
op.name == "v_cvt_u16_f16" ||
op.name == "v_cvt_i16_f16")
op.name == "v_cvt_i16_f16" ||
op.name == "v_fma_f16" ||
op.name == "v_fmac_f16" ||
op.name == "v_fmamk_f16" ||
op.name == "v_fmaak_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_fma_f16.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# v_fma_f16, v_fmac_f16, v_fmamk_f16 and v_fmaak_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_fma_f16_ir COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_fma_f16_check.py" ir
"$<TARGET_FILE:native_compute_ir_dump>")
set_tests_properties(native_fma_f16_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_fma_f16_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_fma_f16_check.py" controls
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_fma_f16_controls PROPERTIES TIMEOUT 60)
add_test(NAME native_fma_f16_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_fma_f16_check.py" hardware
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_fma_f16_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
6 changes: 5 additions & 1 deletion tests/native_f16_alu_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def refusal_programs():
programs.append(([0xb98005c1] + add + [0xbf810000], 'unsupported binary16 rounding or denormal mode'))
programs.append(([0xbfa40001] + add + [0xbf810000], 'unsupported compute control instruction s_round_mode'))
programs.append(([0xbfa5000c] + add + [0xbf810000], 'unsupported compute control instruction s_denorm_mode'))
programs.append((vop2(0x36, 3, 257, 2) + [0xbf810000], 'direct DXIL lowering for v_fmac_f16'))
programs.append((vop2(0x3c, 3, 257, 2) + [0xbf810000], 'direct DXIL lowering for v_pk_fmac_f16'))
programs.append((vop2(0x3a, 3, 257, 2) + [0xbf810000], 'direct DXIL lowering for v_min_f16'))
return programs

Expand Down Expand Up @@ -458,6 +458,10 @@ def bits_of(token):
elif 'legacyF16ToF32' in rhs:
argument = re.search(r'i32 (\S+)\)$', rhs)
values[name] = ('float', _widen(_i32(argument.group(1), values)))
elif 'unaryBits.i32(i32 33,' in rhs:
argument = re.search(r'i32 33,\s*i32 ([^,\s\)]+)\)', rhs)
x = _i32(argument.group(1), values) & 0xffffffff
values[name] = ('i32', (32 - x.bit_length()) if x else 0xffffffff)
elif 'createHandleFromBinding' in rhs or 'annotateHandle' in rhs:
values[name] = ('handle', 0)
else:
Expand Down
Loading
Loading