diff --git a/docs/experiments/native-direct-cvt-pk-u8.md b/docs/experiments/native-direct-cvt-pk-u8.md new file mode 100644 index 0000000000..2174ac8acc --- /dev/null +++ b/docs/experiments/native-direct-cvt-pk-u8.md @@ -0,0 +1,51 @@ +# v_cvt_pk_u8_f32 in the direct C++ compute path (#2997) + +The direct compute frontend (`src/gfx1013_direct_compute.inl`) now lowers `v_cvt_pk_u8_f32`, which it refused with +`direct DXIL lowering for v_cvt_pk_u8_f32`. It computes +D = (S2 & ~(0xff << 8 * S1[1:0])) | (flt32_to_uint8(S0) << 8 * S1[1:0]). + +The conversion is the one `gfx2hlsl.py` (`op_v_cvt_pk_u8_f32`) uses, measured on RDNA 2 hardware +(`docs/experiments.md`, `tools/rdna_cvt_pk_u8.py`): round to nearest even, saturate to [0, 255]. NaN, both zeros and +every negative value give 0; +inf gives 255. + +The lowering follows that expression: +- The positive test is the integer comparison (bits - 1) < 0x7f800000, which fast-math cannot fold. +- The value then goes through DXIL `FMin` with 255.0 and `Round_ne`, and `fptoui`. +- The resulting byte is merged into S2. + +Source and output modifiers stay refused (`direct ALU operand modifiers`), as `gfx2hlsl.py` refuses them. + +## Fixture + +`tests/native_cvt_pk_u8_check.py controls|hardware` +(`ctest --preset dev --no-tests=error -R '^native_cvt_pk_u8_' -V`). + +Sixteen cases set S0 and S1 by `v_mov` literals over S2 = 0xa5a5a5a5, and every lane of two groups stores the +results. The cases cover: +- the ties 0.5, 1.5, 2.5 and 254.5; +- the bound 255.5, 1e30 and +inf; +- -0.0, -1.0 and -inf; +- a quiet NaN; +- the smallest subnormal; +- 127.4999, 0.0 and 255.0; +- all four byte selects, plus a select of 6 that must use only its low two bits. + +The VOP3 word (opcode 0x15e) was assembled with LLVM 15.0.7 (`clang -cc1as -triple amdgcn -target-cpu gfx1013 +-filetype asm -show-encoding`). + +The controls: +- compile the program and check its module with `dxc -dumpbin`: one `FMin` (DXIL opcode 36) and one `Round_ne` + (opcode 26) per case, and no `FMax`; +- refuse it with a negated S0; +- check that three references differ from the expected words: rounding half up, truncating, and letting negatives + through. + +The reference agrees with `model_rne_sat` of `tools/rdna_cvt_pk_u8.py` for all sixteen inputs (checked once while +writing the fixture). Fourteen of the sixteen inputs are also rows of the measured table +`tests/data/rdna_cvt_pk_u8_measured.tsv`; 1e30 and 127.4999 rely on the model only. + +Measured on 27.11.2026, `cpp-migration` `4ff5e36d` plus this change, Windows 10.0.26100.9711. The controls printed +`compiled with one FMin and one Round_ne per case; a negated source refused; the half-up, truncating and +negative-passing references differ`. The readback +reported a skip (77) on the NVIDIA Quadro M1000M (driver 32.0.15.7371, debug layer unavailable), because +`native_store_replay` needs shader model 6.6 with wave32. The readback is not measured. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index a03a41cdad..832556ff44 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -1948,6 +1948,19 @@ struct DirectCompute { auto pad = ir.binary("sub", "32", width); auto field = shift("ashr", a, b); z = select(cmp("eq", width, "0"), "0", shift("ashr", shift("shl", field, pad), pad)); + } else if (n == "v_cvt_pk_u8_f32") { + // (S2 & ~(0xff << 8 * S1[1:0])) | (flt32_to_uint8(S0) << 8 * S1[1:0]) (#2997), with the conversion + // gfx2hlsl.py measured on RDNA 2: round to nearest even, saturate to [0, 255], zero for NaN, both zeros + // and negatives. (bits - 1) < 0x7f800000 holds exactly from the smallest subnormal to +inf, as an integer + // test that fast-math cannot fold. + auto amount = ir.binary("shl", ir.binary("and", b, "3"), "3"); + auto positive = cmp("ult", ir.binary("sub", a, "1"), "2139095040"); + auto bounded = ir.invoke("float", "binary.f32", 36, {{"float", ir.value("bitcast i32 " + a + " to float")}, + {"float", "2.550000e+02"}}, "nounwind readnone"); + auto rounded = ir.invoke("float", "unary.f32", 26, {{"float", bounded}}, "nounwind readnone"); + auto byte = select(positive, ir.binary("and", ir.value("fptoui float " + rounded + " to i32"), "255"), "0"); + auto keep = ir.binary("xor", ir.binary("shl", "255", amount), "4294967295"); + z = ir.binary("or", ir.binary("and", val(2), keep), ir.binary("shl", byte, amount)); } else if (n == "s_orn2_b32") z = ir.binary("or", a, ir.binary("xor", b, "4294967295")); else if (n == "s_nand_b32" || n == "s_xnor_b32") diff --git a/tests/cmake/native_cvt_pk_u8.cmake b/tests/cmake/native_cvt_pk_u8.cmake new file mode 100644 index 0000000000..6b8b1fce2b --- /dev/null +++ b/tests/cmake/native_cvt_pk_u8.cmake @@ -0,0 +1,13 @@ +# Direct C++ compute: v_cvt_pk_u8_f32 (#2997). The controls compile and check the +# reference's mutants; the readback needs shader model 6.6 with wave32 and otherwise reports a skip (77). +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_pk_u8_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/native_cvt_pk_u8_check.py" controls + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_cvt_pk_u8_controls PROPERTIES TIMEOUT 60) + add_test(NAME native_cvt_pk_u8_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/native_cvt_pk_u8_check.py" hardware + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_cvt_pk_u8_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_cvt_pk_u8_check.py b/tests/native_cvt_pk_u8_check.py new file mode 100644 index 0000000000..987f072a6f --- /dev/null +++ b/tests/native_cvt_pk_u8_check.py @@ -0,0 +1,120 @@ +"""v_cvt_pk_u8_f32 in the direct compute path (#2997). + + native_cvt_pk_u8_check.py controls + native_cvt_pk_u8_check.py hardware + +D = (S2 & ~(0xff << 8 * S1[1:0])) | (flt32_to_uint8(S0) << 8 * S1[1:0]). The conversion is the one gfx2hlsl.py +measured on RDNA 2 hardware (see docs/experiments.md): round to nearest even, saturated to [0, 255], with NaN, both +zeros and every negative value giving 0 and +inf giving 255. The VOP3 word (opcode 0x15e) was assembled with LLVM +15.0.7 (clang -cc1as, gfx1013); the other words were checked with tools/disasm_rdna2.py --hex. + +Each case below sets S0 and S1 with v_mov literals over S2 = 0xa5a5a5a5 and stores the result; every lane of two +groups of 32 stores all of them. References that round half up, truncate, or let NaN or negative inputs through +must each differ from the expected words. +""" +from pathlib import Path +import math +import struct +import subprocess +import sys +import tempfile + +from native_dpp_quad_check import request + +REST = 0xA5A5A5A5 +F = lambda value: struct.unpack('