diff --git a/docs/experiments.md b/docs/experiments.md index f3bb185135..39ead23e3a 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6532,9 +6532,9 @@ The source-built draw (`pixel_aot_dpp_cvt_{warp,hardware}`) compares all four ch Plain `v_mul_lo_u32` multiplies two full 32-bit operands and keeps the low 32 bits. The `pixel_aot_e64_{warp,hardware}` draw reads back the integer word for `0xffffffff * 0xffffffff` and `0x12345678 * 0x9abcdef0`, whose low words are `0x00000001` and `0x242d2080`. Replacing multiply with addition must fail the same readback. The LLVM 15 binary fixture verifies the opcode through the normal decoding path. -Quad-local `DPP16` (`quad_perm` with `row_mask:0xf bank_mask:0xf`) and `DPP8` (`dpp8` repeating the same 2×2 quad permutation on lanes 0–3 and 4–7) lower `v_cvt_f16_f32_dpp` via the shared quad routing helpers and existing integer-arithmetic `narrow16` nearest-even helper under the shared low-16-only destination contract (#732/#881: bits 15:0 are defined and tagged `("half16", alu)`, while bits 31:16 stay unspecified so no previous high half is merged). Lowering requires explicit `half_denorm="preserve"` at shader entry and respects `HW_REG_MODE` binary16 rounding (`HALF_RTZ` refuses `v_cvt_f16_f32_dpp`, while restoring `HALF_RNE` matches `s_nop 0`). `DPP16` supports bit-level `abs`/`neg` on the binary32 source; `DPP8` rejects source modifiers, and unproved `fi:`/`bound_ctrl:`, partial row/bank masks, cross-quad or non-repeated `DPP8`, uninitialized operands, and other binary16 DPP opcodes (`v_fmac_f16_dpp`, arithmetic, rounding, transcendentals) remain refused. +Quad-local `DPP16` (`quad_perm` with `row_mask:0xf bank_mask:0xf`) and `DPP8` (`dpp8` repeating the same 2×2 quad permutation on lanes 0–3 and 4–7) lower `v_cvt_f16_f32_dpp` via the shared quad routing helpers and existing integer-arithmetic `narrow16` nearest-even helper under the shared low-16-only destination contract (#732/#881: bits 15:0 are defined and tagged `("half16", alu)`, while bits 31:16 stay unspecified so no previous high half is merged). The same quad-local `DPP16`/`DPP8` slice also lowers the eight binary16 arithmetic and rounding opcodes (`v_add_f16_dpp`, `v_sub_f16_dpp`, `v_subrev_f16_dpp`, `v_mul_f16_dpp`, `v_floor_f16_dpp`, `v_ceil_f16_dpp`, `v_trunc_f16_dpp`, `v_rndne_f16_dpp`, 16 forms across `DPP16` and `DPP8`) under the static finite-proof contract (#839): VGPR sources must carry a statically proven finite low half (`known_ssa_bits` or `known_half`), `src0` is quad-swizzled via `emit_quad_swizzle_u32` while `src1` stays on the current lane, `DPP16` permits bit-15 `abs`/`neg` modifiers on either source (refused on `DPP8`), overflowing results are refused via `f16_vop2_result`, and destinations are tagged `("half16", alu)` with their proven low-16 bits for chaining. Lowering requires explicit `half_denorm="preserve"` at shader entry and respects `HW_REG_MODE` binary16 rounding (`HALF_RTZ` refuses every form, while restoring `HALF_RNE` matches `s_nop 0`). Unproved `fi:`/`bound_ctrl:`, partial row/bank masks, cross-quad or non-repeated `DPP8`, uninitialized or unproven runtime operands, non-finite inputs, and other binary16 DPP opcodes (`v_min_f16_dpp`, `v_max_f16_dpp`, `v_fract_f16_dpp`, `v_fmac_f16_dpp`, `v_ldexp_f16_dpp`, transcendentals) remain refused. -The dedicated `pixel_aot_dpp_f16_{warp,hardware}` draw runs two integer-export fixtures (both round-tripped through LLVM 15 `check_binary`): `DPP_F16_CVT_LANES` derives per-quad lane IDs `{0, 1, 2, 3}` from `uv.x`/`uv.y`, routes lane-varying inputs through `DPP16` `AcrossX` negation and `DPP8` cyclic rotation with preloaded high-half sentinels (`0x3f400000` / `0xdeadbeef`), gathers all four lanes under injective base-4 weights `(1, 4, 16, 64)` via `v_cvt_f32_f16_e32`, and exports raw `WORD_0` SDWA words; `DPP_F16_CVT_WORDS` checks raw low-16 readbacks across in-range RNE vs RTZ and ties-to-even discriminators (`0x3f801000 -> 0x3c00`, `0x3f803000 -> 0x3c02`, `0xbf801001 -> 0x3c01`), binary16 subnormals and underflow ties (`0x33000000 -> 0x0000`, `0x33000001 -> 0x0001`), max finite/overflow (`65504`/`65520`), signed zeros, infinities, and quiet NaN. Positive and negative controls verify that low-half widening (`v_cvt_f32_f16_{e32,e64,sdwa}` and `v_mov_b32_sdwa ... WORD_0`) succeeds while `WORD_1`/`BYTE_0` SDWA reads, whole-word reads (`v_mov_b32_e32`, `v_add_f32_e32`), direct whole-word exports, and unproved finite-arithmetic consumers reject even with a preloaded sentinel, and that mutations (swapping `AcrossX`/`AcrossY`, reversing `dpp8`, dropping `-` or `|.|`, expecting RTZ `0x3c01`, or altering `narrow16` to truncate or tie-away) each report `pixel_mismatch`. Run `ctest --preset dev --no-tests=error -R "^pixel_aot_dpp_f16_(warp|hardware)$" -V`. [AMD RDNA 2 ISA reference](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) +The dedicated `pixel_aot_dpp_f16_{warp,hardware}` draw runs `DPP_F16_CVT_LANES`, `DPP_F16_CVT_WORDS`, and four raw low-16 programs (`arith_dpp16`, `arith_dpp8`, `round_dpp16`, `round_dpp8`, all round-tripped through LLVM 15 `check_binary`) covering all 16 arithmetic/rounding DPP forms under noisy upper-16 halves, preloaded destination sentinels, subnormal preservation (tested under both default denorm mode and `-denorm ftz`), `-0.0` sign preservation, ties-to-even rounding, `half16` chaining, and `DPP16` `-`/`|.|`/`-|.|` modifiers. Positive and negative controls verify low-half widening, `QuadRead*` emission on `src0` only, and rejections for `WORD_1`/whole-word reads, unproven runtime sources, overflow, non-finite inputs, `HALF_RTZ`, and mutations (swapping `AcrossX`/`AcrossY`, reversing `dpp8`, dropping `-` or `|.|`, swapping `sub`/`subrev` or `floor`/`ceil`/`trunc`/`rndne`, or altering `narrow16` to truncate or tie-away). Run `ctest --preset dev --no-tests=error -R "^pixel_aot_dpp_f16_(warp|hardware)$" -V`. [AMD RDNA 2 ISA reference](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) Quad-local `DPP16` (`quad_perm` with `row_mask:0xf bank_mask:0xf`) and `DPP8` (`dpp8` repeating the same 2×2 quad permutation on lanes 0–3 and 4–7) lower across 24 32-bit integer, 24-bit multiply, bitwise, shift, bit-scan and saturating-pack `VOP1`/`VOP2` forms (`add_nc_u32`, `sub_nc_u32`, `subrev_nc_u32`, `min`/`max` `i32`/`u32`, `mul`/`mul_hi` `i24`/`u24`, `and`/`or`/`xor`/`xnor`/`not_b32`, `lshlrev`/`lshrrev_b32`, `ashrrev_i32`, `bfrev_b32`, `ffbl_b32`, `ffbh_u32`/`ffbh_i32` and `sat_pk_u8_i16`, which clamps each signed halfword to `[0, 255]` as its e32 form does) via SM 6.0 quad intrinsics, emitting 12-bit-split `dpp_mul_hi_{u,i}24` and MSB-indexed `dpp_ffbh_{u,i}32` helpers only when used. The `dpp_int32` fixture derives integer quad lane ID `{0, 1, 2, 3}` from `uv.x`/`uv.y`, tests all 24 forms across three constant sets (including negative/extreme `i24` products, zero/all-ones bit scans, shift counts `>= 32`, and halfwords that saturate at 0 and at 255 or stay in range), exports all four bytes of the 32-bit accumulator as exact `[0, 255]` integers, and checks six mutations (`AcrossX` vs `AcrossY`, cyclic `dpp8` rotation, `ashrrev` vs `lshrrev`, signed vs unsigned `mul_hi` and `ffbh`, and `sat_pk_u8_i16` of the wrong source) that must each report a mismatch. [AMD RDNA 2 ISA reference](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) diff --git a/tests/form_coverage_check.py b/tests/form_coverage_check.py index 3067c00bac..f494b5791d 100644 --- a/tests/form_coverage_check.py +++ b/tests/form_coverage_check.py @@ -1362,6 +1362,42 @@ def check_sdwa_cmp_coverage(): assert ("SDWA", "v_cmp_lt_f16_sdwa") not in fc.gfx_forms(stripped, reference), name +def check_dpp_f16_arith_round_forms(): + """The 16 quad-local DPP16/DPP8 binary16 arithmetic and rounding forms count as covered once + the translator accepts their fixtures; unlowered f16 DPP/SDWA neighbours stay uncovered.""" + import pixel_aot_check as pixel + saved = set(pixel.ACCEPTED) + pixel.ACCEPTED.clear() + try: + for body in pixel.DPP_F16_ARITH_ROUND_PROGRAMS.values(): + pixel.translate_f16(pixel.uncompressed_export(body)) + ops = ("add", "sub", "subrev", "mul", "floor", "ceil", "trunc", "rndne") + forms = {(group, f"v_{op}_f16_dpp") for group in ("DPP16", "DPP8") for op in ops} + assert len(forms) == 16 + neighbours = { + ("DPP16", "v_min_f16_dpp"), ("DPP8", "v_min_f16_dpp"), + ("DPP16", "v_max_f16_dpp"), ("DPP8", "v_max_f16_dpp"), + ("DPP16", "v_fract_f16_dpp"), ("DPP8", "v_fract_f16_dpp"), + ("DPP16", "v_fmac_f16_dpp"), ("DPP8", "v_fmac_f16_dpp"), + ("DPP16", "v_ldexp_f16_dpp"), ("DPP8", "v_ldexp_f16_dpp"), + ("SDWA", "v_add_f16_sdwa"), ("SDWA", "v_floor_f16_sdwa"), + } + for name in ("gfx1013.tsv", "rdna2.tsv"): + reference = fc.load_reference(name) + assert forms <= reference, (name, sorted(forms - reference)) + assert neighbours <= reference, (name, sorted(neighbours - reference)) + covered = fc.gfx_forms(pixel.ACCEPTED, reference) + assert forms <= covered, (name, sorted(forms - covered)) + assert not (neighbours & covered), (name, sorted(neighbours & covered)) + # Control: removing one opcode removes both its DPP16 and DPP8 forms from the count. + stripped = pixel.ACCEPTED - {"v_add_f16_dpp"} + assert not {("DPP16", "v_add_f16_dpp"), ("DPP8", "v_add_f16_dpp")} & fc.gfx_forms( + stripped, fc.load_reference("gfx1013.tsv")) + finally: + pixel.ACCEPTED.clear() + pixel.ACCEPTED.update(saved) + + if __name__ == "__main__": check_denominators() check_example() @@ -1382,6 +1418,7 @@ def check_sdwa_cmp_coverage(): check_f16_fmac_ldexp_forms() check_f16_cvt_int16_forms() check_f16_to_int16_forms() + check_dpp_f16_arith_round_forms() check_swap_coverage() check_pk_fmac_coverage() check_sdwa_cmp_coverage() diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index 11c677d06f..e726526dac 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -9502,6 +9502,110 @@ def bits(value): "v_mov_b32_sdwa v15, v27 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", ] +# Quad-local DPP16 and DPP8 binary16 arithmetic (add/sub/subrev/mul) and rounding (floor/ceil/trunc/rndne) +# fixtures under the low-16-only and static finite-proof contract (#839). All inputs carry noisy upper-16 +# halves, destinations are preloaded with non-zero sentinels, and results cover subnormals, signed zero, +# ties-to-even rounding, half16 chaining, and DPP16 bit-15 abs/neg modifiers. +DPP_F16_ARITH_ROUND_PROGRAMS = { + "arith_dpp16": [ + "v_mov_b32_e32 v20, 0xdead3e00", # +1.5 + "v_mov_b32_e32 v21, 0xbeefc200", # -3.0 + "v_mov_b32_e32 v22, 0x12343800", # +0.5 + "v_mov_b32_e32 v23, 0x56780003", # subnormal 3 * 2^-24 + "v_mov_b32_e32 v24, 0x3f400000", + "v_mov_b32_e32 v25, 0xdeadbeef", + "v_mov_b32_e32 v26, 0xc0a00000", + "v_mov_b32_e32 v27, 0x41200000", + "v_add_f16_dpp v24, -v20, |v21| quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf", + "v_sub_f16_dpp v25, |v21|, v22 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf", + "v_subrev_f16_dpp v26, v22, -v20 quad_perm:[3,2,1,0] row_mask:0xf bank_mask:0xf", + "v_mul_f16_dpp v27, v23, v24 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", + "v_mov_b32_sdwa v12, v24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v13, v25 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v14, v26 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v15, v27 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + ], + "arith_dpp8": [ + "v_mov_b32_e32 v20, 0xdead8000", # -0.0 + "v_mov_b32_e32 v21, 0xbeef8000", # -0.0 + "v_mov_b32_e32 v22, 0x12343c00", # +1.0 + "v_mov_b32_e32 v23, 0x56784100", # +2.5 + "v_mov_b32_e32 v28, 0x9abc0005", # subnormal 5 * 2^-24 + "v_mov_b32_e32 v24, 0x3f400000", + "v_add_f16_dpp v24, v20, v21 dpp8:[1,2,3,0,5,6,7,4]", + "v_sub_f16_dpp v25, v22, v23 dpp8:[2,3,0,1,6,7,4,5]", + "v_subrev_f16_dpp v26, v22, v23 dpp8:[3,2,1,0,7,6,5,4]", + "v_mul_f16_dpp v27, v28, v26 dpp8:[1,0,3,2,5,4,7,6]", + "v_mov_b32_sdwa v12, v24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v13, v25 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v14, v26 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v15, v27 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + ], + "round_dpp16": [ + "v_mov_b32_e32 v20, 0xdead3d00", # +1.25 + "v_mov_b32_e32 v21, 0xbeefb400", # -0.25 + "v_mov_b32_e32 v22, 0x12343e00", # +1.5 + "v_mov_b32_e32 v23, 0x5678c100", # -2.5 + "v_mov_b32_e32 v24, 0xdeadbeef", + "v_floor_f16_dpp v24, -v20 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf", + "v_ceil_f16_dpp v25, -|v21| quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf", + "v_trunc_f16_dpp v26, -v22 quad_perm:[3,2,1,0] row_mask:0xf bank_mask:0xf", + "v_rndne_f16_dpp v27, |v23| quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", + "v_mov_b32_sdwa v12, v24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v13, v25 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v14, v26 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v15, v27 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + ], + "round_dpp8": [ + "v_mov_b32_e32 v20, 0xdead0001", # smallest positive subnormal 2^-24 + "v_mov_b32_e32 v21, 0xbeefb800", # -0.5 + "v_mov_b32_e32 v22, 0x12343e00", # +1.5 + "v_mov_b32_e32 v24, 0x3f400000", + "v_floor_f16_dpp v24, v20 dpp8:[1,2,3,0,5,6,7,4]", + "v_ceil_f16_dpp v25, v20 dpp8:[2,3,0,1,6,7,4,5]", + "v_trunc_f16_dpp v26, v21 dpp8:[3,2,1,0,7,6,5,4]", + "v_rndne_f16_dpp v27, v22 dpp8:[1,0,3,2,5,4,7,6]", + "v_mov_b32_sdwa v12, v24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v13, v25 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v14, v26 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_sdwa v15, v27 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + ], +} +DPP_F16_ARITH_ROUND_EXPECTED = { + "arith_dpp16": (0x3e00, 0x4100, 0xc000, 0x0004), + "arith_dpp8": (0x8000, 0xbe00, 0x3e00, 0x0008), + "round_dpp16": (0xc000, 0x8000, 0xbc00, 0x4000), + "round_dpp8": (0x0000, 0x3c00, 0x8000, 0x4000), +} +# Op and modifier mutations for the four DPP16/DPP8 arithmetic and rounding draw programs; each +# mutation changes at least one exported channel by at least 1.0 in numerical value (never relying +# on a signed-zero-only delta across fsub %x, -0.0). +DPP_F16_ARITH_ROUND_MUTATIONS = { + "arith_dpp16": ( + ("v_add_f16_dpp v24, -v20, |v21|", "v_add_f16_dpp v24, v20, |v21|"), + ("v_add_f16_dpp v24, -v20, |v21|", "v_add_f16_dpp v24, -v20, v21"), + ("v_sub_f16_dpp v25", "v_subrev_f16_dpp v25"), + ("v_subrev_f16_dpp v26", "v_sub_f16_dpp v26"), + ("v_mul_f16_dpp v27", "v_add_f16_dpp v27"), + ), + "arith_dpp8": ( + ("v_add_f16_dpp v24, v20, v21", "v_add_f16_dpp v24, v20, v22"), + ("v_sub_f16_dpp v25", "v_subrev_f16_dpp v25"), + ("v_sub_f16_dpp v25", "v_add_f16_dpp v25"), + ("v_mul_f16_dpp v27", "v_add_f16_dpp v27"), + ), + "round_dpp16": ( + ("v_floor_f16_dpp v24, -v20", "v_floor_f16_dpp v24, v20"), + ("v_ceil_f16_dpp v25", "v_floor_f16_dpp v25"), + ("v_trunc_f16_dpp v26", "v_floor_f16_dpp v26"), + ("v_rndne_f16_dpp v27, |v23|", "v_rndne_f16_dpp v27, v23"), + ), + "round_dpp8": ( + ("v_ceil_f16_dpp v25", "v_floor_f16_dpp v25"), + ("v_rndne_f16_dpp v27", "v_trunc_f16_dpp v27"), + ), +} + def dpp_f16_narrow16_ref(bits): """Exact IEEE-754 binary32-to-binary16 RNE reference matching tools/pixel_aot.py narrow16().""" @@ -9578,12 +9682,58 @@ def check_dpp_f16_contract_controls(): (f"v_cvt_f16_f32_dpp v23, v70 {identity}", "DPP operand v70 is read before it is written"), (f"v_cvt_f16_f32_dpp v256, v20 {identity}", "Invalid f16 DPP operation"), (f"v_fmac_f16_dpp v20, v20, v20 {identity}", "Unsupported instruction"), - (f"v_add_f16_dpp v23, v20, v20 {identity}", "Unsupported instruction"), + (f"v_min_f16_dpp v23, v20, v20 {identity}", "Unsupported instruction"), + (f"v_max_f16_dpp v23, v20, v20 {identity}", "Unsupported instruction"), (f"v_sin_f16_dpp v23, v20 {identity}", "Unsupported instruction"), (f"v_cos_f16_dpp v23, v20 {identity}", "Unsupported instruction"), - (f"v_floor_f16_dpp v23, v20 {identity}", "Unsupported instruction"), + (f"v_fract_f16_dpp v23, v20 {identity}", "Unsupported instruction"), ): expect_value_error(translate_f16, uncompressed_export(["v_mov_b32_e32 v20, s44", bad]), message) + # Positive and negative controls for binary16 arithmetic & rounding DPP16/DPP8. + for name, body in DPP_F16_ARITH_ROUND_PROGRAMS.items(): + prog = uncompressed_export(body) + hlsl = translate_f16(prog) + if name == "arith_dpp16": + assert "QuadReadAcrossX(" in hlsl and "QuadReadAcrossY(" in hlsl and "QuadReadAcrossDiagonal(" in hlsl + src1_lines = [line for line in hlsl.splitlines() if "_1 = " in line and "uint hf" in line] + assert len(src1_lines) == 4 and all("QuadRead" not in line for line in src1_lines), src1_lines + elif name == "arith_dpp8": + assert "QuadReadLaneAt(" in hlsl + for old, new in DPP_F16_ARITH_ROUND_MUTATIONS[name]: + mutated = [line.replace(old, new) for line in prog] + assert mutated != prog, (name, old) + assert translate_f16(mutated) != hlsl, (name, old, new) + proven_pair = ["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 0x4000"] + arith_ok = uncompressed_export([*proven_pair, f"v_add_f16_dpp v24, v20, v21 {identity}", + f"v_mov_b32_sdwa v12, v24 {sdwa}WORD_0"]) + round_ok = uncompressed_export([*proven_pair, f"v_floor_f16_dpp v24, v20 {identity}", + f"v_mov_b32_sdwa v12, v24 {sdwa}WORD_0"]) + expect_value_error(translate_int, arith_ok, "binary16 denormal mode unknown at entry") + expect_value_error(translate_int, round_ok, "binary16 denormal mode unknown at entry") + expect_value_error(translate_f16, [HALF_RTZ, *arith_ok], "under round toward zero: v_add_f16_dpp") + expect_value_error(translate_f16, [HALF_RTZ, *round_ok], "under round toward zero: v_floor_f16_dpp") + expect_value_error(translate_f16, uncompressed_export([*proven_pair, f"v_add_f16_dpp v24, v20, v21 {identity}", + f"v_mov_b32_sdwa v12, v24 {sdwa}WORD_1"]), + "Reads the undefined high half of a 16-bit result") + expect_value_error(translate_f16, uncompressed_export([*proven_pair, f"v_floor_f16_dpp v24, v20 {identity}", + "v_mov_b32_e32 v12, v24"]), + "Expected a numeric register") + for prefix, bad, message in ( + (["v_mov_b32_e32 v20, s44"], f"v_add_f16_dpp v24, v20, v20 {identity}", "binary16 operand lacks a finite proof: v20"), + (["v_mov_b32_e32 v20, s44"], f"v_floor_f16_dpp v24, v20 {identity}", "binary16 operand lacks a finite proof: v20"), + (["v_mov_b32_e32 v20, 0x7c00"], f"v_floor_f16_dpp v24, v20 {identity}", "binary16 operand is not finite: v20"), + (["v_mov_b32_e32 v20, 0x7e00"], f"v_add_f16_dpp v24, v20, v20 {identity}", "binary16 operand is not finite: v20"), + (["v_mov_b32_e32 v20, 0x7bff"], f"v_add_f16_dpp v24, v20, v20 {identity}", "binary16 result overflows: v_add_f16_dpp"), + (["v_mov_b32_e32 v20, 0x7bff"], f"v_mul_f16_dpp v24, v20, v20 {identity}", "binary16 result overflows: v_mul_f16_dpp"), + (proven_pair, "v_add_f16_dpp v24, -v20, v21 dpp8:[1,2,3,0,5,6,7,4]", "Source modifiers are not allowed on -v20"), + (proven_pair, "v_add_f16_dpp v24, v20, |v21| dpp8:[1,2,3,0,5,6,7,4]", "Source modifiers are not allowed on |v21|"), + (proven_pair, "v_floor_f16_dpp v24, -v20 dpp8:[1,2,3,0,5,6,7,4]", "Source modifiers are not allowed on -v20"), + (proven_pair, f"v_add_f16_dpp v256, v20, v21 {identity}", "Invalid f16 DPP operation"), + (proven_pair, f"v_floor_f16_dpp v256, v20 {identity}", "Invalid f16 DPP operation"), + (proven_pair, f"v_add_f16_dpp v24, v20, v21 {identity} bound_ctrl:1", "Unsupported DPP16 modifier bound_ctrl:1"), + (proven_pair, f"v_floor_f16_dpp v24, v20 {identity} fi:1", "Unsupported DPP16 modifier fi:1"), + ): + expect_value_error(translate_f16, uncompressed_export([*prefix, bad]), message) # Scheduling: outstanding sample and non-entry EXEC mask must refuse. base = uncompressed_export([]) sample = next(i for i, line in enumerate(base) if line.startswith("image_sample")) @@ -9678,6 +9828,31 @@ def check_dpp_f16_draw(directory, compiler, renderer, backend): words_src.write_text(mutated_hlsl, encoding="ascii") compile_shader(compiler, words_src, words_shader) run_pixel_batch(renderer, backend, words_shader, [(cases[0][0], "mismatch")]) + + # 3. Binary16 arithmetic (add/sub/subrev/mul) and rounding (floor/ceil/trunc/rndne) DPP16 & DPP8 draws, + # verified in both default denorm mode and binary32 -denorm ftz, plus op/modifier mutations. + for name, body in DPP_F16_ARITH_ROUND_PROGRAMS.items(): + prog = uncompressed_export(body) + expected = DPP_F16_ARITH_ROUND_EXPECTED[name] + src_path = directory / f"dpp_f16_{name}.hlsl" + cso_path = directory / f"dpp_f16_{name}.cso" + pass_case = directory / f"dpp_f16_{name}_pass.case" + pass_case.write_bytes(bytes(256) + struct.pack("<4I", *expected) * 64 + b"\x01") + src_path.write_text(translate_f16(prog), encoding="ascii") + compile_shader(compiler, src_path, cso_path) + run_pixel_batch(renderer, backend, cso_path, [(pass_case, "pass")]) + # Binary32 -denorm ftz must preserve binary16 subnormals and -0.0. + compile_shader(compiler, src_path, cso_path, mode="ftz") + run_pixel_batch(renderer, backend, cso_path, [(pass_case, "pass")]) + for old, new in DPP_F16_ARITH_ROUND_MUTATIONS[name]: + mutated = [line.replace(old, new) for line in prog] + assert mutated != prog, (name, old) + src_path.write_text(translate_f16(mutated), encoding="ascii") + compile_shader(compiler, src_path, cso_path) + statuses, _ = run_pixel_batch_statuses(renderer, backend, cso_path, [pass_case]) + print(f"dpp_f16 {name} mutation {old!r} -> {new!r}: statuses {statuses}") + if "mismatch" not in statuses: + raise AssertionError(f"dpp_f16 {name} mutation {old!r} -> {new!r} escaped detection") return 0 @@ -13948,7 +14123,7 @@ def refuse(line, message, translator=translate_f16): refuse(line, "Binary16 e32 src1 must be a VGPR") # Forms the lowering does not accept fall through to the generic refusal, and so do the neighbouring binary16 ops. for line in ("v_add_f16_e64 v24, v18, v19 clamp", "v_mul_f16_e64 v24, v18, v19 mul:2", "v_mul_f16_e64 v24, v18, v19 div:2", - "v_add_f16_e64 v24, v18, v19 op_sel:[0,1]", "v_add_f16_dpp v24, v18, v19 quad_perm:[0,1,2,3]", + "v_add_f16_e64 v24, v18, v19 op_sel:[0,1]", "v_min_f16_dpp v24, v18, v19 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", "v_add_f16_sdwa v24, v18, v19 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:WORD_0", "v_add_f16 v24, v18, v19, v19", "v_add_f16 v24, v18", "v_min_f16_e64 v24, v18, v19", "v_max_f16_e32 v24, v18, v19"): @@ -14778,7 +14953,7 @@ def refuse(line, message, translator=translate_f16): for line in ("v_floor_f16_e64 v24, v18 clamp", "v_ceil_f16_e64 v24, v18 clamp", "v_trunc_f16_e64 v24, v18 mul:2", "v_rndne_f16_e64 v24, v18 div:2", "v_rndne_f16_e64 v24, v18 mul:4", "v_floor_f16_e64 v24, v18 op_sel:[0,1]", "v_floor_f16_e64 v24, -v18 clamp", "v_floor_f16_e32 v24, v18 clamp", - "v_floor_f16_dpp v24, v18 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", + "v_fract_f16_dpp v24, v18 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", "v_floor_f16_sdwa v24, v18 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", "v_floor_f16 v24, v18, v19", "v_floor_f16 v24", "v_fract_f16_e32 v24, v18", "v_rcp_f16_e32 v24, v18", "v_sqrt_f16_e64 v24, v18", "v_fract_f16_e64 v24, v18"): @@ -18306,6 +18481,7 @@ def check_binary(directory, assembler, objcopy, library): fixtures.append((with_constants(SMOVREL_BODY), translate)) fixtures.append((uncompressed_export(DPP_F16_CVT_LANES), translate_f16)) fixtures.append((uncompressed_export(DPP_F16_CVT_WORDS), translate_f16)) + fixtures.extend((uncompressed_export(body), translate_f16) for body in DPP_F16_ARITH_ROUND_PROGRAMS.values()) fixtures.extend((bit_program(op), translate) for op in BIT_OPERATIONS) fixtures.extend((program, function) for _, program, function, _ in e64_spelling_programs()) fixtures.append((vop1_nop_program(), translate)) diff --git a/tools/pixel_aot.py b/tools/pixel_aot.py index 828419dbdd..a0494b1f60 100644 --- a/tools/pixel_aot.py +++ b/tools/pixel_aot.py @@ -2298,6 +2298,48 @@ def dpp_vgpr(token, vector, sample_dest, *, allow_mods): return value, use_abs, neg +def half16_dpp_source(token, vector, sample_dest, known_ssa_bits, known_half, *, allow_mods): + """Parse one DPP VGPR source of a binary16 VOP1/VOP2 op under the static finite-proof contract. + + Accepts a VGPR holding either a statically known word (known_ssa_bits) or a proven binary16 + half16 result (known_half), with optional bit-15 abs/neg modifiers on DPP16 (refused on DPP8). + """ + neg = False + use_abs = False + raw = token + if raw.startswith("-"): + if not allow_mods: + raise ValueError(f"Source modifiers are not allowed on {token}") + neg = True + raw = raw[1:] + if m := re.fullmatch(r"abs\(([^()|]+)\)|\|([^()|]+)\|", raw): + if not allow_mods: + raise ValueError(f"Source modifiers are not allowed on {token}") + use_abs = True + raw = m.group(1) or m.group(2) + m = re.fullmatch(r"v(\d+)", raw) + if not m or not 0 <= int(m.group(1)) <= 255: + raise ValueError(f"DPP operand must be a VGPR, got {token}") + if raw not in vector: + raise ValueError(f"DPP operand {raw} is read before it is written") + if raw in sample_dest: + raise ValueError("Sample used before vmcnt wait") + value = vector[raw] + if isinstance(value, str) and value in known_ssa_bits: + ssa_var, bits = value, known_ssa_bits[value] & 0xffff + elif (name := half16_name(raw, vector)) is not None and name in known_half: + ssa_var, bits = name, known_half[name] & 0xffff + else: + raise ValueError(f"binary16 operand lacks a finite proof: {raw}") + if (bits >> 10) & 0x1f == 0x1f: + raise ValueError(f"binary16 operand is not finite: {raw}") + if use_abs: + bits &= 0x7fff + if neg: + bits ^= 0x8000 + return ssa_var, bits, use_abs, neg + + def parse_dpp(rest, operand_count): """Parse operands and quad-local DPP16 (quad_perm) or DPP8 (dpp8) modifiers.""" parts_ = rest.split(", ") @@ -5786,6 +5828,67 @@ def sample_coord(reg): bits = f"({bits} ^ 0x80000000u)" vector[dst] = ("half16", f"alu{i}") lines.append(f" precise float alu{i} = asfloat(narrow16(asfloat({bits})));") + elif m := match(rf"v_({'|'.join(VOP2_F16)})_f16_dpp (v\d+), (.+)"): + # Quad-local DPP16/DPP8 binary16 VOP2 arithmetic under the shared low-16-only and + # static finite-proof contract (#839): src0 is quad-swizzled via emit_quad_swizzle_u32, + # src1 stays on the current lane, DPP16 allows bit-15 abs/neg modifiers on either source + # (refused on DPP8), and the destination is tagged ("half16", alu) with its proven bits. + name, dst, rest = m.groups() + if mask != "entry" or not 0 <= int(dst[1:]) <= 255: + raise ValueError("Invalid f16 DPP operation") + if half_denorm != HALF_DENORM_PRESERVE: + raise ValueError("binary16 denormal mode unknown at entry") + if dst in sample_dest: + raise ValueError("Write overlaps an outstanding sample") + tokens, perm, is_dpp8 = parse_dpp(rest, 2) + s0_val, b0, abs0, neg0 = half16_dpp_source( + tokens[0], vector, sample_dest, known_ssa_bits, known_half, allow_mods=not is_dpp8) + s1_val, b1, abs1, neg1 = half16_dpp_source( + tokens[1], vector, sample_dest, known_ssa_bits, known_half, allow_mods=not is_dpp8) + result = f16_vop2_result(name, b0, b1) + if result is None: + raise ValueError(f"binary16 result overflows: v_{name}_f16_dpp") + s0_bits = f"(({emit_quad_swizzle_u32(lines, i, s0_val, perm)}) & 0xffffu)" + if abs0: + s0_bits = f"({s0_bits} & 0x7fffu)" + if neg0: + s0_bits = f"({s0_bits} ^ 0x8000u)" + s1_bits = f"(asuint({s1_val}) & 0xffffu)" + if abs1: + s1_bits = f"({s1_bits} & 0x7fffu)" + if neg1: + s1_bits = f"({s1_bits} ^ 0x8000u)" + lines.append(f" uint hf{i}_0 = {s0_bits};") + lines.append(f" uint hf{i}_1 = {s1_bits};") + expression = VOP2_F16[name].format(f"widen16(hf{i}_0)", f"widen16(hf{i}_1)") + lines.append(f" precise float hs{i} = {expression};") + lines.append(f" precise float alu{i} = asfloat(narrow16(hs{i}));") + vector[dst] = ("half16", f"alu{i}") + known_half[f"alu{i}"] = result + elif m := match(rf"v_({'|'.join(VOP1_F16_ROUND)})_f16_dpp (v\d+), (.+)"): + # Quad-local DPP16/DPP8 binary16 VOP1 rounding under the same low-16-only and + # static finite-proof contract (#839). + name, dst, rest = m.groups() + if mask != "entry" or not 0 <= int(dst[1:]) <= 255: + raise ValueError("Invalid f16 DPP operation") + if half_denorm != HALF_DENORM_PRESERVE: + raise ValueError("binary16 denormal mode unknown at entry") + if dst in sample_dest: + raise ValueError("Write overlaps an outstanding sample") + tokens, perm, is_dpp8 = parse_dpp(rest, 1) + s0_val, b0, abs0, neg0 = half16_dpp_source( + tokens[0], vector, sample_dest, known_ssa_bits, known_half, allow_mods=not is_dpp8) + result = f16_round_result(name, b0) + s0_bits = f"(({emit_quad_swizzle_u32(lines, i, s0_val, perm)}) & 0xffffu)" + if abs0: + s0_bits = f"({s0_bits} & 0x7fffu)" + if neg0: + s0_bits = f"({s0_bits} ^ 0x8000u)" + lines.append(f" uint hf{i}_0 = {s0_bits};") + lines.append(f" precise float hs{i} = {VOP1_F16_ROUND[name].format(f'widen16(hf{i}_0)')};") + lines.append(f" precise float alu{i} = asfloat(narrow16(hs{i}));") + vector[dst] = ("half16", f"alu{i}") + known_half[f"alu{i}"] = result elif m := match(rf"v_({'|'.join(DPP_INT32)})_dpp (v\d+), (.+)"): op, dst, rest = m.groups() arity, template, helper = DPP_INT32[op]