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
47 changes: 47 additions & 0 deletions docs/experiments/native-minmax-med3-f16.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Direct DXIL `v_min3_f16`, `v_max3_f16` and `v_med3_f16`

Lowers the VOP3 opcodes `v_min3_f16` (`0x351`), `v_max3_f16` (`0x354`) and
`v_med3_f16` (`0x357`) in the native direct-DXIL path
(`src/gfx1013_direct_compute.inl`) for Compute and Pixel shaders. The
operations use 32-bit integer IEEE-754 binary16 ordering, without float
conversion.

## Scope

- Two-operand min/max follows `minNum`/`maxNum`: a single NaN returns the
other operand, two NaNs return the canonical `0x7e00`, and `-0.0h` orders
below `+0.0h`. `v_min3_f16` and `v_max3_f16` apply it twice.
- `v_med3_f16` returns the median when no source is NaN. When any source is
NaN it returns `min(min(a, b), c)`, as in the Python reference model.
- Sources: VGPRs, SGPRs, the 32-bit literal, inline integers (`128..208`) and
the inline binary16 float constants (`240..248`). VOP3 `abs`/`neg` per source,
`clamp` (NaN and negative results to `+0.0h`, results above `1.0h` to `1.0h`),
`op_sel[2:0]` high-half reads of VGPR sources, and `op_sel[3]` writes to
`D[31:16]`. The other destination half is preserved.
- Refuses non-VOP3, SDWA, DPP and permuted forms, `omod`, high-half `op_sel`
on a non-VGPR source, other special scalar sources (such as `vcc_lo`), a
binary16 denormal mode other than "preserve" (for example `s_denorm_mode 3`,
which flushes binary16 denormals), and a
MODE write reached through a conditional branch
(`unsupported binary16 min3/max3/med3 denormal mode`).
- `native_minmax_med3_f16_ir` evaluates the emitted IR on CPU against the
Python reference model for 29 cases and checks 7 refusal diagnostics.
`_controls` compiles the same cases to DXIL and checks that the refusals
produce no output. `_compute_hardware` replays the cases on a hardware
adapter, including a partial EXEC mask. `_pixel_warp` and `_pixel_hardware`
draw the cases that use a full EXEC mask.
- WARP covers the Pixel path only. No WARP variant of the Compute test is
registered.
- Does not establish full startup of the application.

## Reproduction

```powershell
.\tools\dev.ps1
cmake --preset dev
cmake --build --preset dev --target native_compute_ir_dump compile_dxil native_store_replay offscreen_check
ctest --preset dev --no-tests=error -R '^native_minmax_med3_f16_' -V
```

The `_hardware` tests return 77 (capability skip) when no hardware adapter is
available.
74 changes: 73 additions & 1 deletion src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2648,6 +2648,75 @@ struct DirectCompute {
vector_write(i.dst, ir.binary("or", kept, placed));
return true;
}
if (n == "v_min3_f16" || n == "v_max3_f16" || n == "v_med3_f16") {
if (i.enc != Encoding::Vop3 || i.subword || i.dpp_control || permuted)
refuse(i.pc, "unsupported " + string(n) + " encoding");
if (i.scale)
refuse(i.pc, "unsupported " + string(n) + " omod");
const unsigned op_sel = (i.a >> 11) & 15u;
for (unsigned j = 0; j < 3; ++j) {
if ((op_sel & (1u << j)) && i.src[j] < 256)
refuse(i.pc, "unsupported " + string(n) + " high-half op_sel on non-VGPR source");
}
if (!half_mode_known || (mode_bits & 0xc0u) != 0xc0u)
refuse(i.pc, "unsupported binary16 min3/max3/med3 denormal mode");
auto half_bits = [&](unsigned j) {
const unsigned r = i.src[j];
string bits;
if (op_sel & (1u << j)) {
bits = ir.binary("lshr", operand32(i, r), "16");
} else if (r >= 240 && r <= 248) {
bits = to_string(inline_half_words[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 if (r <= 105 || (r >= 256 && r < 512)) {
bits = ir.binary("and", operand32(i, r), "65535");
} else {
refuse(i.pc, "unsupported " + string(n) + " source");
}
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 is_nan = [&](const string &x) {
return cmp("ugt", ir.binary("and", x, "32767"), "31744");
};
auto order_key = [&](const string &x) {
return ir.binary("xor", x, select(cmp("uge", x, "32768"), "65535", "32768"));
};
auto minmax2 = [&](const string &x, const string &y, bool is_max) {
auto ord = select(cmp(is_max ? "ugt" : "ult", order_key(x), order_key(y)), x, y);
return select(is_nan(x), select(is_nan(y), "32256", y), select(is_nan(y), x, ord));
};
auto a = half_bits(0), b = half_bits(1), c = half_bits(2);
string result;
if (n == "v_med3_f16") {
auto lo = minmax2(a, b, false), hi = minmax2(a, b, true);
auto median = minmax2(lo, minmax2(hi, c, false), true);
auto any_nan = ir.value("or i1 " + is_nan(a) + ", " + ir.value("or i1 " + is_nan(b) + ", " + is_nan(c)));
result = select(any_nan, minmax2(lo, c, false), median);
} else {
const bool is_max = n == "v_max3_f16";
result = minmax2(minmax2(a, b, is_max), c, is_max);
}
if (i.clamp) {
auto nan = is_nan(result);
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 dst_old = read(i.dst);
auto packed_dst = (op_sel & 8u)
? ir.binary("or", ir.binary("and", dst_old, "65535"), ir.binary("shl", result, "16"))
: ir.binary("or", ir.binary("and", dst_old, "4294901760"), result);
vector_write(i.dst, packed_dst);
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 @@ -5025,7 +5094,10 @@ struct DirectCompute {
op.name == "v_cvt_norm_i16_f16" ||
op.name == "v_cvt_norm_u16_f16" ||
op.name == "v_cvt_pknorm_i16_f16" ||
op.name == "v_cvt_pknorm_u16_f16")
op.name == "v_cvt_pknorm_u16_f16" ||
op.name == "v_min3_f16" ||
op.name == "v_max3_f16" ||
op.name == "v_med3_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
20 changes: 20 additions & 0 deletions tests/cmake/native_minmax_med3_f16.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Direct Compute and Pixel DXIL VOP3 v_min3_f16, v_max3_f16, and v_med3_f16 checks.
if(TARGET native_compute_ir_dump AND TARGET compile_dxil AND TARGET native_store_replay AND TARGET offscreen_check AND
EXISTS "${INTAKE_PYTHON}" AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
set(checker "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_minmax_med3_f16_check.py")
add_test(NAME native_minmax_med3_f16_ir COMMAND "${INTAKE_PYTHON}" -B
"${checker}" ir "$<TARGET_FILE:native_compute_ir_dump>")
set_tests_properties(native_minmax_med3_f16_ir PROPERTIES TIMEOUT 60)
add_test(NAME native_minmax_med3_f16_controls COMMAND "${INTAKE_PYTHON}" -B
"${checker}" controls "$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_minmax_med3_f16_controls PROPERTIES TIMEOUT 60)
add_test(NAME native_minmax_med3_f16_compute_hardware COMMAND "${INTAKE_PYTHON}" -B
"${checker}" compute "$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_minmax_med3_f16_compute_hardware PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77)
foreach(adapter IN ITEMS warp hardware)
add_test(NAME native_minmax_med3_f16_pixel_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${checker}" pixel "$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}" "$<TARGET_FILE:offscreen_check>" ${adapter})
set_tests_properties(native_minmax_med3_f16_pixel_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77)
endforeach()
unset(checker)
endif()
Loading
Loading