From f68cd29864ba486f9d3f78df255c0833689ac6fc Mon Sep 17 00:00:00 2001 From: KomaGuy <265821007+KomaGuy@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:11:03 +0300 Subject: [PATCH 1/2] shader: lower quad-local DPP16 and DPP8 binary16 fmac, ldexp and int16 conversions in pixel_aot Lower the quad-local DPP16 and DPP8 spellings of the six remaining contracted binary16 opcodes (12 forms total across gfx1013 and rdna2): - VOP2 fused/scale: v_fmac_f16_dpp, v_ldexp_f16_dpp - VOP1 int16 <-> f16: v_cvt_f16_i16_dpp, v_cvt_f16_u16_dpp, v_cvt_i16_f16_dpp, v_cvt_u16_f16_dpp Add int16_dpp_source() alongside half16_dpp_source() to enforce the existing #839 contracts: - src0 is quad-swizzled via emit_quad_swizzle_u32 while src1 and the v_fmac_f16_dpp accumulator stay on the current lane. - DPP16 allows bit-15 abs/neg modifiers on float sources only (src0/src1 on v_fmac_f16_dpp, src0 on v_ldexp_f16_dpp and v_cvt_{i16,u16}_f16_dpp); DPP8 and integer operands refuse modifiers. - v_fmac_f16_dpp uses fma16_rne for single-rounding fused multiply-add, v_ldexp_f16_dpp requires a proven signed 16-bit exponent in [-64, 64], v_cvt_f16_i16_dpp accepts runtime i16/low16 sources while v_cvt_f16_u16_dpp requires a static proof < 65520, and v_cvt_{i16,u16}_f16_dpp truncates proven in-range binary16 values into ("low16", alu). - Explicit half_denorm="preserve" and HW_REG_MODE RNE remain required. Extend pixel_aot_dpp_f16_{warp,hardware} with four 4-channel programs (fmac_ldexp_dpp16, fmac_ldexp_dpp8, cvt_int16_dpp16, cvt_int16_dpp8) in both default and -denorm ftz modes plus 13 numerically distinct mutations and a per-lane runtime quad-routing witness (DPP_F16_CVT_I16_LANES), add all five to check_binary, update check_dpp_f16_contract_controls and docs/experiments.md, and add check_dpp_f16_cvt_fmac_ldexp_forms in tests/form_coverage_check.py. --- docs/experiments.md | 4 +- tests/form_coverage_check.py | 42 ++++- tests/pixel_aot_check.py | 292 ++++++++++++++++++++++++++++++++++- tools/pixel_aot.py | 134 ++++++++++++++++ 4 files changed, 460 insertions(+), 12 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index bda0e0243b..2cc5dbd8a2 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -8019,9 +8019,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). 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. +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`) plus `v_fmac_f16_dpp`, `v_ldexp_f16_dpp`, `v_cvt_f16_i16_dpp`, `v_cvt_f16_u16_dpp`, `v_cvt_i16_f16_dpp`, and `v_cvt_u16_f16_dpp` (12 additional forms across `DPP16` and `DPP8`) under the static finite-proof contract (#839): `src0` is quad-swizzled via `emit_quad_swizzle_u32` while `src1` and the `v_fmac_f16_dpp` accumulator stay on the current lane, `DPP16` permits bit-15 `abs`/`neg` modifiers on float sources only (refused on `DPP8` and on integer operands of `v_ldexp_f16_dpp` and `v_cvt_f16_{i16,u16}_dpp`), `v_fmac_f16_dpp` uses `fma16_rne` for single-rounding fused multiply-add, `v_ldexp_f16_dpp` requires a statically proven signed 16-bit exponent within `[-64, 64]`, `v_cvt_f16_i16_dpp` accepts runtime `i16`/`low16` sources while `v_cvt_f16_u16_dpp` requires a static proof `< 65520`, and `v_cvt_{i16,u16}_f16_dpp` truncates proven in-range binary16 values into `("low16", alu)`. 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 (except `v_cvt_f16_i16_dpp`), non-finite inputs, and other binary16 DPP opcodes (`v_min_f16_dpp`, `v_max_f16_dpp`, `v_fract_f16_dpp`, `v_cvt_norm_{i16,u16}_f16_dpp`, `v_frexp_*_f16_dpp`, transcendentals) remain refused. -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) +The dedicated `pixel_aot_dpp_f16_{warp,hardware}` draw runs `DPP_F16_CVT_LANES`, `DPP_F16_CVT_WORDS`, `DPP_F16_CVT_I16_LANES` (per-lane quad routing of runtime signed 16-bit integers with noisy upper halves), four raw low-16 arithmetic/rounding programs (`arith_dpp16`, `arith_dpp8`, `round_dpp16`, `round_dpp8`), and four raw low-16 fmac/ldexp/conversion programs (`fmac_ldexp_dpp16`, `fmac_ldexp_dpp8`, `cvt_int16_dpp16`, `cvt_int16_dpp8`, all round-tripped through LLVM 15 `check_binary`) covering all 28 arithmetic/rounding/fmac/ldexp/conversion DPP forms under noisy upper-16 halves, preloaded destination sentinels, fused-vs-non-fused rounding (`0x1c01` vs `0x1c00`), subnormal preservation (tested under both default denorm mode and `-denorm ftz`), `-0.0` sign preservation, ties-to-even rounding, the `65519` max finite `u16` boundary, `half16`/`low16` 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, out-of-range conversions/exponents, non-finite inputs, `HALF_RTZ`, and mutations (swapping `AcrossX`/`AcrossY`, reversing `dpp8`, dropping `-` or `|.|`, swapping `sub`/`subrev`, `fmac`/`mul`, `u16`/`i16`, `ldexp` exponents, 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 0d99c87395..a902aa14af 100644 --- a/tests/form_coverage_check.py +++ b/tests/form_coverage_check.py @@ -1438,8 +1438,8 @@ def check_dpp_f16_arith_round_forms(): ("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"), + ("DPP16", "v_rcp_f16_dpp"), ("DPP8", "v_rcp_f16_dpp"), + ("DPP16", "v_cvt_norm_i16_f16_dpp"), ("DPP8", "v_cvt_norm_i16_f16_dpp"), ("SDWA", "v_add_f16_sdwa"), ("SDWA", "v_floor_f16_sdwa"), } for name in ("gfx1013.tsv", "rdna2.tsv"): @@ -1458,6 +1458,43 @@ def check_dpp_f16_arith_round_forms(): pixel.ACCEPTED.update(saved) +def check_dpp_f16_cvt_fmac_ldexp_forms(): + """The 12 quad-local DPP16/DPP8 binary16 fmac, ldexp and int16 conversion 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_CVT_FMAC_LDEXP_PROGRAMS.values(): + pixel.translate_f16(pixel.uncompressed_export(body)) + ops = ("v_fmac_f16_dpp", "v_ldexp_f16_dpp", "v_cvt_f16_i16_dpp", + "v_cvt_f16_u16_dpp", "v_cvt_i16_f16_dpp", "v_cvt_u16_f16_dpp") + forms = {(group, op) for group in ("DPP16", "DPP8") for op in ops} + assert len(forms) == 12 + neighbours = { + ("DPP16", "v_cvt_norm_i16_f16_dpp"), ("DPP8", "v_cvt_norm_i16_f16_dpp"), + ("DPP16", "v_cvt_norm_u16_f16_dpp"), ("DPP8", "v_cvt_norm_u16_f16_dpp"), + ("DPP16", "v_frexp_mant_f16_dpp"), ("DPP8", "v_frexp_mant_f16_dpp"), + ("DPP16", "v_frexp_exp_i16_f16_dpp"), ("DPP8", "v_frexp_exp_i16_f16_dpp"), + ("DPP16", "v_rcp_f16_dpp"), ("DPP8", "v_rcp_f16_dpp"), + ("SDWA", "v_ldexp_f16_sdwa"), ("SDWA", "v_cvt_f16_i16_sdwa"), ("SDWA", "v_cvt_i16_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_fmac_f16_dpp"} + assert not {("DPP16", "v_fmac_f16_dpp"), ("DPP8", "v_fmac_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() @@ -1479,6 +1516,7 @@ def check_dpp_f16_arith_round_forms(): check_f16_cvt_int16_forms() check_f16_to_int16_forms() check_dpp_f16_arith_round_forms() + check_dpp_f16_cvt_fmac_ldexp_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 545dae4862..6790231403 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -9942,7 +9942,7 @@ def check_dpp_cvt_refusals(): "DPP operand v255 is read before it is written"), ("v_cvt_pkrtz_f16_f32_dpp v16, v12, v13 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf bound_ctrl:1", "Unsupported DPP16 modifier"), - ("v_cvt_i16_f16_dpp v55, v12 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", + ("v_cvt_norm_i16_f16_dpp v55, v12 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", "Unsupported instruction"), ("v_cvt_f32_i32_dpp v12, v255 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", "DPP operand v255 is read before it is written"), @@ -10234,6 +10234,146 @@ def bits(value): ), } +# Quad-local DPP16 and DPP8 binary16 fused multiply-accumulate (v_fmac_f16_dpp), exponent scaling +# (v_ldexp_f16_dpp), and 16-bit integer conversions (v_cvt_f16_{i16,u16}_dpp, v_cvt_{i16,u16}_f16_dpp) +# fixtures under the low-16-only and static finite-proof contract (#839). All static inputs carry +# noisy upper-16 halves, destinations are preloaded with sentinels, and results cover fused vs +# non-fused rounding (0x1c01 vs 0x1c00), subnormals, ties-to-even, 65519 max finite u16 boundary, +# truncation toward zero, half16 chaining, and DPP16 bit-15 abs/neg modifiers. +DPP_F16_CVT_FMAC_LDEXP_PROGRAMS = { + "fmac_ldexp_dpp16": [ + "v_mov_b32_e32 v20, 0xdeadbc03", # -(1 + 3*2^-10) + "v_mov_b32_e32 v21, 0xbeefbc01", # -(1 + 2^-10) + "v_mov_b32_e32 v22, 0x56784000", # +2.0 + "v_mov_b32_e32 v23, 0x9abc3e00", # +1.5 + "v_mov_b32_e32 v24, 0x1234bc00", # -1.0 accumulator + "v_mov_b32_e32 v25, 0xdead3800", # +0.5 accumulator + "v_mov_b32_e32 v26, 0xc0a00000", + "v_mov_b32_e32 v27, 0x41200000", + "v_mov_b32_e32 v28, 0xbeeffffd", # i16 exponent -3 + "v_mov_b32_e32 v29, 0x1234fff0", # i16 exponent -16 + "v_fmac_f16_dpp v24, -v20, |v21| quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf", + "v_fmac_f16_dpp v25, v22, -v23 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf", + "v_ldexp_f16_dpp v26, -v22, v28 quad_perm:[3,2,1,0] row_mask:0xf bank_mask:0xf", + "v_ldexp_f16_dpp v27, |v23|, v29 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", + ], + "fmac_ldexp_dpp8": [ + "v_mov_b32_e32 v20, 0xdead3c03", # +(1 + 3*2^-10) + "v_mov_b32_e32 v21, 0xbeef3c01", # +(1 + 2^-10) + "v_mov_b32_e32 v22, 0x56784100", # +2.5 + "v_mov_b32_e32 v23, 0x9abcbe00", # -1.5 + "v_mov_b32_e32 v24, 0x1234bc00", # -1.0 accumulator + "v_mov_b32_e32 v25, 0x41204200", # +3.0 accumulator + "v_mov_b32_e32 v28, 0xabcd0003", # i16 exponent +3 + "v_mov_b32_e32 v29, 0x77770001", # subnormal 2^-24 + "v_mov_b32_e32 v30, 0x88880002", # i16 exponent +2 + "v_fmac_f16_dpp v24, v20, v21 dpp8:[1,2,3,0,5,6,7,4]", + "v_fmac_f16_dpp v25, v22, v23 dpp8:[2,3,0,1,6,7,4,5]", + "v_ldexp_f16_dpp v26, v25, v28 dpp8:[3,2,1,0,7,6,5,4]", + "v_ldexp_f16_dpp v27, v29, v30 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", + ], + "cvt_int16_dpp16": [ + "v_mov_b32_e32 v20, 0xdeadf7fd", # i16 -2051 -> ties-to-even -2052 (0xe802) + "v_mov_b32_e32 v21, 0xbeef8002", # u16 32770 -> ties-to-even 32768 (0x7800) + "v_mov_b32_e32 v22, 0x123442e0", # +3.4375 -> -v22 = -3.4375 -> i16 -3 (0xfffd) + "v_mov_b32_e32 v23, 0x5678c4c0", # -4.75 -> |v23| = +4.75 -> u16 4 (0x0004) + "v_mov_b32_e32 v24, 0x3f400000", + "v_mov_b32_e32 v25, 0xdeadbeef", + "v_cvt_f16_i16_dpp v24, v20 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf", + "v_cvt_f16_u16_dpp v25, v21 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf", + "v_cvt_i16_f16_dpp v26, -v22 quad_perm:[3,2,1,0] row_mask:0xf bank_mask:0xf", + "v_cvt_u16_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", + ], + "cvt_int16_dpp8": [ + "v_mov_b32_e32 v20, 0xdeadf7ff", # i16 -2049 -> ties-to-even -2048 (0xe800) + "v_mov_b32_e32 v21, 0xbeefffef", # u16 65519 -> 65504 (0x7bff) + "v_mov_b32_e32 v22, 0x1234c880", # -9.0 -> i16 -9 (0xfff7) + "v_mov_b32_e32 v23, 0x56787bff", # +65504.0 -> u16 65504 (0xffe0) + "v_mov_b32_e32 v28, 0x9abc4200", # +3.0 (used by mutation) + "v_mov_b32_e32 v24, 0x3f400000", + "v_cvt_f16_i16_dpp v24, v20 dpp8:[1,2,3,0,5,6,7,4]", + "v_cvt_f16_u16_dpp v25, v21 dpp8:[2,3,0,1,6,7,4,5]", + "v_cvt_i16_f16_dpp v26, v22 dpp8:[3,2,1,0,7,6,5,4]", + "v_cvt_u16_f16_dpp v27, v23 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_CVT_FMAC_LDEXP_EXPECTED = { + "fmac_ldexp_dpp16": (0x1c01, 0xc100, 0xb400, 0x0180), + "fmac_ldexp_dpp8": (0x1c01, 0xba00, 0xc600, 0x0004), + "cvt_int16_dpp16": (0xe802, 0x7800, 0xfffd, 0x0004), + "cvt_int16_dpp8": (0xe800, 0x7bff, 0xfff7, 0xffe0), +} +DPP_F16_CVT_FMAC_LDEXP_MUTATIONS = { + "fmac_ldexp_dpp16": ( + ("v_fmac_f16_dpp v24, -v20, |v21|", "v_fmac_f16_dpp v24, v20, |v21|"), + ("v_fmac_f16_dpp v24, -v20, |v21|", "v_fmac_f16_dpp v24, -v20, v21"), + ("v_fmac_f16_dpp v24", "v_mul_f16_dpp v24"), + ("v_ldexp_f16_dpp v26, -v22, v28", "v_ldexp_f16_dpp v26, v22, v28"), + ("v_ldexp_f16_dpp v26, -v22, v28", "v_ldexp_f16_dpp v26, -v22, v29"), + ), + "fmac_ldexp_dpp8": ( + ("v_fmac_f16_dpp v24", "v_mul_f16_dpp v24"), + ("v_ldexp_f16_dpp v26, v25, v28", "v_ldexp_f16_dpp v26, v25, v30"), + ), + "cvt_int16_dpp16": ( + ("v_cvt_f16_u16_dpp v25, v21", "v_cvt_f16_i16_dpp v25, v21"), + ("v_cvt_i16_f16_dpp v26, -v22", "v_cvt_i16_f16_dpp v26, v22"), + ("v_cvt_u16_f16_dpp v27, |v23|", "v_cvt_u16_f16_dpp v27, v22"), + ), + "cvt_int16_dpp8": ( + ("v_cvt_f16_u16_dpp v25, v21", "v_cvt_f16_i16_dpp v25, v21"), + ("v_cvt_i16_f16_dpp v26, v22", "v_cvt_i16_f16_dpp v26, v24"), + ("v_cvt_u16_f16_dpp v27, v23", "v_cvt_u16_f16_dpp v27, v28"), + ), +} +# Per-lane quad-routing witness for runtime v_cvt_f16_i16_dpp (DPP16 AcrossX + DPP8 cyclic rotation) +# with noisy upper-16 bits (0x1234) over lanes {0..3} holding signed i16 values {-4, -3, -2, -1}. +DPP_F16_CVT_I16_LANES = [ + *DPP_CVT_LANES, + "v_cvt_i32_f32_e32 v21, v20", + "v_subrev_nc_u32_e32 v21, 4, v21", + "v_and_b32_e32 v21, 0xffff, v21", + "v_or_b32_e32 v21, 0x12340000, v21", + "v_mov_b32_e32 v22, 0x3f400000", + "v_cvt_f16_i16_dpp v22, v21 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf", + "v_mov_b32_e32 v24, 0xdeadbeef", + "v_cvt_f16_i16_dpp v24, v21 dpp8:[1,2,3,0,5,6,7,4]", + "v_cvt_f32_f16_e32 v23, v22", + "v_cvt_f32_f16_e32 v25, v24", + *[f"v_mov_b32_dpp v{26 + lane}, v23 quad_perm:[{lane},{lane},{lane},{lane}] row_mask:0xf bank_mask:0xf" + for lane in range(4)], + *[f"v_mov_b32_dpp v{30 + lane}, v25 quad_perm:[{lane},{lane},{lane},{lane}] row_mask:0xf bank_mask:0xf" + for lane in range(4)], + "v_fma_f32 v34, 4.0, v27, v26", + "v_fma_f32 v34, 0x41800000, v28, v34", + "v_fma_f32 v34, 0x42800000, v29, v34", + "v_cvt_i32_f32_e32 v12, v34", + "v_fma_f32 v35, 4.0, v31, v30", + "v_fma_f32 v35, 0x41800000, v32, v35", + "v_fma_f32 v35, 0x42800000, v33, v35", + "v_cvt_i32_f32_e32 v13, v35", + "v_mov_b32_sdwa v36, v22 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_dpp v14, v36 quad_perm:[0,0,0,0] row_mask:0xf bank_mask:0xf", + "v_mov_b32_sdwa v37, v24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", + "v_mov_b32_dpp v15, v37 quad_perm:[3,3,3,3] row_mask:0xf bank_mask:0xf", +] + def dpp_f16_narrow16_ref(bits): """Exact IEEE-754 binary32-to-binary16 RNE reference matching tools/pixel_aot.py narrow16().""" @@ -10298,7 +10438,7 @@ def check_dpp_f16_contract_controls(): for write, message in (("s_denorm_mode 15", "Unsupported instruction"), ("s_round_mode 0x3", "Unsupported rounding mode"), ("s_setreg_imm32_b32 hwreg(HW_REG_MODE, 6, 2), 3", "Unsupported hardware register write")): expect_value_error(translate_f16, [write, *valid], message) - # DPP encoding refusals and out-of-slice binary16 DPP opcodes (including FMAC and transcendentals). + # DPP encoding refusals and out-of-slice binary16 DPP opcodes (transcendentals, min/max, norm/frexp). for bad, message in ( ("v_cvt_f16_f32_dpp v23, v20 row_shl:1 row_mask:0xf bank_mask:0xf", "Unsupported DPP control row_shl:1"), ("v_cvt_f16_f32_dpp v23, v20 quad_perm:[0,1,2,3] row_mask:0x7 bank_mask:0xf", "Unsupported DPP16 modifier row_mask:0x7"), @@ -10309,7 +10449,8 @@ def check_dpp_f16_contract_controls(): ("v_cvt_f16_f32_dpp v23, -v20 dpp8:[1,2,3,0,5,6,7,4]", "Source modifiers are not allowed on -v20"), (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_cvt_norm_i16_f16_dpp v23, v20 {identity}", "Unsupported instruction"), + (f"v_rcp_f16_dpp v23, 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"), @@ -10362,6 +10503,95 @@ def check_dpp_f16_contract_controls(): (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) + # Positive and negative controls for binary16 fmac, ldexp, and int16 conversion DPP16/DPP8. + for name, body in DPP_F16_CVT_FMAC_LDEXP_PROGRAMS.items(): + prog = uncompressed_export(body) + hlsl = translate_f16(prog) + if name == "fmac_ldexp_dpp16": + assert "fma16_rne(" in hlsl + 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) == 2 and all("QuadRead" not in line for line in src1_lines), src1_lines + dst_lines = [line for line in hlsl.splitlines() if "_2 = " in line and "uint hf" in line] + assert len(dst_lines) == 2 and all("QuadRead" not in line for line in dst_lines), dst_lines + elif name == "fmac_ldexp_dpp8": + assert "fma16_rne(" in hlsl and "QuadReadLaneAt(" in hlsl + elif name == "cvt_int16_dpp16": + assert "QuadReadAcrossX(" in hlsl and "QuadReadAcrossY(" in hlsl and "QuadReadAcrossDiagonal(" in hlsl + elif name == "cvt_int16_dpp8": + assert "QuadReadLaneAt(" in hlsl + for old, new in DPP_F16_CVT_FMAC_LDEXP_MUTATIONS[name]: + mutated = [line.replace(old, new) for line in prog] + assert mutated != prog, (name, old) + assert translate_f16(mutated) != hlsl, (name, old, new) + translate_f16(uncompressed_export(DPP_F16_CVT_I16_LANES)) + # low16 -> v_cvt_f16_i16_dpp chaining and half16 -> v_cvt_u16_f16_dpp chaining are accepted, + # while half16 -> v_cvt_f16_i16_dpp and low16 -> v_cvt_f32_f16 are refused. + translate_f16(uncompressed_export([ + *proven_pair, f"v_cvt_i16_f16_dpp v24, v20 {identity}", + f"v_cvt_f16_i16_dpp v25, v24 {identity}", f"v_mov_b32_sdwa v12, v25 {sdwa}WORD_0"])) + translate_f16(uncompressed_export([ + "v_mov_b32_e32 v20, 0x0005", f"v_cvt_f16_u16_dpp v24, v20 {identity}", + f"v_cvt_u16_f16_dpp v25, v24 {identity}", f"v_mov_b32_sdwa v12, v25 {sdwa}WORD_0"])) + fmac_ok = uncompressed_export([*proven_pair, "v_mov_b32_e32 v24, 0x3c00", + f"v_fmac_f16_dpp v24, v20, v21 {identity}", + f"v_mov_b32_sdwa v12, v24 {sdwa}WORD_0"]) + ldexp_ok = uncompressed_export(["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 0x0002", + f"v_ldexp_f16_dpp v24, v20, v21 {identity}", + f"v_mov_b32_sdwa v12, v24 {sdwa}WORD_0"]) + cvt_i16_ok = uncompressed_export(["v_mov_b32_e32 v20, s44", f"v_cvt_f16_i16_dpp v24, v20 {identity}", + f"v_mov_b32_sdwa v12, v24 {sdwa}WORD_0"]) + to_i16_ok = uncompressed_export([*proven_pair, f"v_cvt_i16_f16_dpp v24, v20 {identity}", + f"v_mov_b32_sdwa v12, v24 {sdwa}WORD_0"]) + for prog, op in ((fmac_ok, "v_fmac_f16_dpp"), (ldexp_ok, "v_ldexp_f16_dpp"), + (cvt_i16_ok, "v_cvt_f16_i16_dpp"), (to_i16_ok, "v_cvt_i16_f16_dpp")): + expect_value_error(translate_int, prog, "binary16 denormal mode unknown at entry") + expect_value_error(translate_f16, [HALF_RTZ, *prog], f"under round toward zero: {op}") + for prefix, bad, message in ( + # v_fmac_f16_dpp: unproven accumulator/src, overflow, DPP8 modifiers, non-finite. + ([*proven_pair, "v_mov_b32_e32 v24, s44"], f"v_fmac_f16_dpp v24, v20, v21 {identity}", + "binary16 operand lacks a finite proof: v24"), + (["v_mov_b32_e32 v20, 0x7bff", "v_mov_b32_e32 v24, 0x3c00"], f"v_fmac_f16_dpp v24, v20, v20 {identity}", + "binary16 result overflows: v_fmac_f16_dpp"), + ([*proven_pair, "v_mov_b32_e32 v24, 0x3c00"], "v_fmac_f16_dpp v24, -v20, v21 dpp8:[1,2,3,0,5,6,7,4]", + "Source modifiers are not allowed on -v20"), + ([*proven_pair, "v_mov_b32_e32 v24, 0x3c00"], "v_fmac_f16_dpp v24, v20, |v21| dpp8:[1,2,3,0,5,6,7,4]", + "Source modifiers are not allowed on |v21|"), + # v_ldexp_f16_dpp: unproven exponent, out-of-range exponent, overflow, src1 modifier, DPP8 src0 modifier. + (["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, s44"], f"v_ldexp_f16_dpp v24, v20, v21 {identity}", + "binary16 ldexp exponent lacks a proof: v21"), + (["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 65"], f"v_ldexp_f16_dpp v24, v20, v21 {identity}", + "binary16 ldexp exponent out of range: 65"), + (["v_mov_b32_e32 v20, 0x7bff", "v_mov_b32_e32 v21, 1"], f"v_ldexp_f16_dpp v24, v20, v21 {identity}", + "binary16 result overflows: v_ldexp_f16_dpp"), + (["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 2"], f"v_ldexp_f16_dpp v24, v20, -v21 {identity}", + "Source modifiers are not allowed on -v21"), + (["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 2"], "v_ldexp_f16_dpp v24, -v20, v21 dpp8:[1,2,3,0,5,6,7,4]", + "Source modifiers are not allowed on -v20"), + # v_cvt_f16_{i16,u16}_dpp: runtime u16 refused, 65520 overflow, float modifiers refused on DPP16, half16 refused. + (["v_mov_b32_e32 v20, s44"], f"v_cvt_f16_u16_dpp v24, v20 {identity}", + "binary16 conversion source lacks a range proof: v20"), + (["v_mov_b32_e32 v20, 0xfff0"], f"v_cvt_f16_u16_dpp v24, v20 {identity}", + "binary16 result overflows: v_cvt_f16_u16_dpp"), + (["v_mov_b32_e32 v20, 0x0001"], f"v_cvt_f16_i16_dpp v24, -v20 {identity}", + "Source modifiers are not allowed on -v20"), + (["v_mov_b32_e32 v20, 0x0001"], f"v_cvt_f16_u16_dpp v24, |v20| {identity}", + "Source modifiers are not allowed on |v20|"), + ([*proven_pair, f"v_add_f16_dpp v24, v20, v21 {identity}"], f"v_cvt_f16_i16_dpp v25, v24 {identity}", + "Expected a numeric register"), + # v_cvt_{i16,u16}_f16_dpp: unproven, out-of-range (-1 for u16, 32768 for i16), DPP8 modifier, low16 tag check. + (["v_mov_b32_e32 v20, s44"], f"v_cvt_i16_f16_dpp v24, v20 {identity}", + "binary16 operand lacks a finite proof: v20"), + (["v_mov_b32_e32 v20, 0xbc00"], f"v_cvt_u16_f16_dpp v24, v20 {identity}", + "binary16 value out of range: v_cvt_u16_f16_dpp"), + (["v_mov_b32_e32 v20, 0x7800"], f"v_cvt_i16_f16_dpp v24, v20 {identity}", + "binary16 value out of range: v_cvt_i16_f16_dpp"), + (proven_pair, "v_cvt_i16_f16_dpp v24, -v20 dpp8:[1,2,3,0,5,6,7,4]", + "Source modifiers are not allowed on -v20"), + ([*proven_pair, f"v_cvt_i16_f16_dpp v24, v20 {identity}"], "v_cvt_f32_f16_e32 v12, v24", + "Expected a numeric register"), + ): + 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")) @@ -10481,6 +10711,55 @@ def check_dpp_f16_draw(directory, compiler, renderer, backend): 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") + + # 4. Binary16 fmac, ldexp, and int16 conversion DPP16 & DPP8 draws, verified in both default + # denorm mode and binary32 -denorm ftz, plus op/modifier mutations and per-lane i16 routing. + for name, body in DPP_F16_CVT_FMAC_LDEXP_PROGRAMS.items(): + prog = uncompressed_export(body) + expected = DPP_F16_CVT_FMAC_LDEXP_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. + 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_CVT_FMAC_LDEXP_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") + i16_lanes_prog = uncompressed_export(DPP_F16_CVT_I16_LANES) + i16_lanes_src = directory / "dpp_f16_cvt_i16_lanes.hlsl" + i16_lanes_cso = directory / "dpp_f16_cvt_i16_lanes.cso" + i16_lanes_case = directory / "dpp_f16_cvt_i16_lanes.case" + i16_lanes_src.write_text(translate_f16(i16_lanes_prog), encoding="ascii") + compile_shader(compiler, i16_lanes_src, i16_lanes_cso) + # ch0: (-3)*1 + (-4)*4 + (-1)*16 + (-2)*64 = -163 (0xffffff5d) + # ch1: (-3)*1 + (-2)*4 + (-1)*16 + (-4)*64 = -283 (0xfffffee5) + # ch2: lane 0 of v22 = f16(-3.0) = 0xc200; ch3: lane 3 of v24 = f16(-4.0) = 0xc400 + i16_lanes_expected = (0xffffff5d, 0xfffffee5, 0xc200, 0xc400) + i16_lanes_case.write_bytes(bytes(256) + struct.pack("<4I", *i16_lanes_expected) * 64 + b"\x01") + run_pixel_batch(renderer, backend, i16_lanes_cso, [(i16_lanes_case, "pass")]) + for old, new in ( + ("quad_perm:[1,0,3,2]", "quad_perm:[2,3,0,1]"), + ("dpp8:[1,2,3,0,5,6,7,4]", "dpp8:[3,0,1,2,7,4,5,6]"), + ): + mutated = [line.replace(old, new) for line in i16_lanes_prog] + assert mutated != i16_lanes_prog, old + i16_lanes_src.write_text(translate_f16(mutated), encoding="ascii") + compile_shader(compiler, i16_lanes_src, i16_lanes_cso) + statuses, _ = run_pixel_batch_statuses(renderer, backend, i16_lanes_cso, [i16_lanes_case]) + print(f"dpp_f16 cvt_i16_lanes mutation {old!r} -> {new!r}: statuses {statuses}") + if "mismatch" not in statuses: + raise AssertionError(f"dpp_f16 cvt_i16_lanes mutation {old!r} -> {new!r} escaped detection") return 0 @@ -15405,7 +15684,6 @@ def refuse(line, message, *lines): translate_f16(program("v_mov_b32_e32 v24, 0x3c00", f"v_{op}_f16_e32 v24, {operands}")) for form in (f"v_{op}_f16_e64 v24, v18, v19", f"v_{op}_f16_e64 v24, v18, v19 clamp", f"v_{op}_f16_e64 v24, v18, v19 mul:2", f"v_{op}_f16_e64 v24, v18, v19 op_sel:[0,1]", f"v_{op}_f16_e32 v24, v18, v19 clamp", - f"v_{op}_f16_dpp v24, v18, v19 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", f"v_{op}_f16_sdwa v24, v18, v19 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:WORD_0", f"v_{op}_f16 v24, v18", f"v_{op}_f16 v24, v18, v19, v19"): refuse(form, "Unsupported instruction") @@ -16260,8 +16538,6 @@ def refuse(line, message, translator=translate_f16): for line in ("v_cvt_f16_i16_e64 v24, v18 clamp", "v_cvt_f16_u16_e64 v24, v18 clamp", "v_cvt_f16_i16_e64 v24, v18 mul:2", "v_cvt_f16_u16_e64 v24, v18 div:2", "v_cvt_f16_i16_e64 v24, v18 op_sel:[0,1]", "v_cvt_f16_i16_e64 v24, -v18 clamp", "v_cvt_f16_i16_e32 v24, v18 clamp", "v_cvt_f16_u16_e32 v24, 1 clamp", - "v_cvt_f16_i16_dpp v24, v18 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", - "v_cvt_f16_u16_dpp v24, v18 row_shl:1 row_mask:0xf bank_mask:0xf", "v_cvt_f16_i16_sdwa v24, v18 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", "v_cvt_f16_u16_sdwa v24, v18 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", "v_cvt_f16_i16 v24, v18, v19", "v_cvt_f16_i16 v24", @@ -16625,8 +16901,6 @@ def refuse(line, message, translator=translate_f16): for line in ("v_cvt_i16_f16_e64 v24, v18 clamp", "v_cvt_u16_f16_e64 v24, v18 clamp", "v_cvt_i16_f16_e64 v24, v18 mul:2", "v_cvt_u16_f16_e64 v24, v18 div:2", "v_cvt_i16_f16_e64 v24, v18 mul:4", "v_cvt_i16_f16_e64 v24, v18 op_sel:[0,1]", "v_cvt_i16_f16_e64 v24, -v18 clamp", "v_cvt_i16_f16_e32 v24, v18 clamp", "v_cvt_u16_f16_e32 v24, 1.0 clamp", - "v_cvt_i16_f16_dpp v24, v18 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", - "v_cvt_u16_f16_dpp v24, v18 row_shl:1 row_mask:0xf bank_mask:0xf", "v_cvt_i16_f16_sdwa v24, v18 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", "v_cvt_u16_f16_sdwa v24, v18 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0", "v_cvt_i16_f16 v24, v18, v19", "v_cvt_u16_f16 v24", @@ -19371,6 +19645,8 @@ def check_binary(directory, assembler, objcopy, library): 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((uncompressed_export(body), translate_f16) for body in DPP_F16_CVT_FMAC_LDEXP_PROGRAMS.values()) + fixtures.append((uncompressed_export(DPP_F16_CVT_I16_LANES), translate_f16)) 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 71bb63dd2e..d88a24a2f4 100644 --- a/tools/pixel_aot.py +++ b/tools/pixel_aot.py @@ -2378,6 +2378,27 @@ def half16_dpp_source(token, vector, sample_dest, known_ssa_bits, known_half, *, return ssa_var, bits, use_abs, neg +def int16_dpp_source(token, scalar, vector, pending, sample_dest, known_ssa_bits, *, signed): + """Parse one integer VGPR source of a binary16 DPP op (no float modifiers allowed). + + Validates through int16_operand so low16 and numeric VGPRs pass while half16 and non-numeric + tags are rejected. Returns (ssa_var, known_low16_or_None). + """ + if token.startswith(("-", "|", "abs(")): + raise ValueError(f"Source modifiers are not allowed on {token}") + m = re.fullmatch(r"v(\d+)", token) + if not m or not 0 <= int(m.group(1)) <= 255: + raise ValueError(f"DPP operand must be a VGPR, got {token}") + if token not in vector: + raise ValueError(f"DPP operand {token} is read before it is written") + if token in sample_dest: + raise ValueError("Sample used before vmcnt wait") + int16_operand(token, scalar, vector, pending, sample_dest, signed=signed) + ssa_var = low16_name(token, vector) or vector[token] + known = int16_known_value(token, scalar, vector, known_ssa_bits) + return ssa_var, known + + def parse_dpp(rest, operand_count): """Parse operands and quad-local DPP16 (quad_perm) or DPP8 (dpp8) modifiers.""" parts_ = rest.split(", ") @@ -6108,6 +6129,119 @@ def sample_coord(reg): 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(r"v_(fmac|ldexp)_f16_dpp (v\d+), (.+)"): + # Quad-local DPP16/DPP8 binary16 fused multiply-accumulate (D = S0_swizzled * S1 + D) + # and exponent scaling (D = S0_swizzled * 2^S1) under the static finite-proof contract + # (#839). src0 is quad-swizzled via emit_quad_swizzle_u32; src1 and dst stay on the + # current lane. DPP16 allows bit-15 abs/neg modifiers on float sources (src0 and src1 + # for fmac, src0 only for ldexp); DPP8 and integer src1 refuse modifiers. + 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) + 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)" + if name == "fmac": + s1_val, b1, abs1, neg1 = half16_dpp_source( + tokens[1], vector, sample_dest, known_ssa_bits, known_half, allow_mods=not is_dpp8) + d_val, b2, _, _ = half16_dpp_source( + dst, vector, sample_dest, known_ssa_bits, known_half, allow_mods=False) + result = f16_pk_fmac_lane_result(b0, b1, b2) + if result is None: + raise ValueError("binary16 result overflows: v_fmac_f16_dpp") + 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};") + lines.append(f" uint hf{i}_2 = (asuint({d_val}) & 0xffffu);") + lines.append(f" uint hm{i} = fma16_rne(hf{i}_0, hf{i}_1, hf{i}_2);") + helpers.add("fma16_rne") + lines.append(f" precise float alu{i} = asfloat(hm{i});") + else: + _, known = int16_dpp_source( + tokens[1], scalar, vector, pending, sample_dest, known_ssa_bits, signed=True) + if known is None: + raise ValueError(f"binary16 ldexp exponent lacks a proof: {tokens[1]}") + exponent = known - 0x10000 if known & 0x8000 else known + if abs(exponent) > LDEXP_F16_EXPONENT_LIMIT: + raise ValueError(f"binary16 ldexp exponent out of range: {exponent}") + result = f16_ldexp_result(b0, exponent) + if result is None: + raise ValueError("binary16 result overflows: v_ldexp_f16_dpp") + lines.append(f" uint hf{i}_0 = {s0_bits};") + lines.append(f" precise float hs{i} = widen16(hf{i}_0) * asfloat({(127 + exponent) << 23}u);") + 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(r"v_cvt_f16_(i16|u16)_dpp (v\d+), (.+)"): + # Quad-local DPP16/DPP8 16-bit integer to binary16 conversion (round to nearest even) + # under the same contract as plain v_cvt_f16_{i16,u16}: runtime i16 is always finite, + # while u16 requires a static proof < 65520. Integer src0 refuses float modifiers. + kind, 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") + signed = kind == "i16" + tokens, perm, _ = parse_dpp(rest, 1) + s0_val, known = int16_dpp_source( + tokens[0], scalar, vector, pending, sample_dest, known_ssa_bits, signed=signed) + result = None + if known is not None: + result = f16_cvt_int_result(signed, known) + if result is None: + raise ValueError(f"binary16 result overflows: v_cvt_f16_{kind}_dpp") + elif not signed: + raise ValueError(f"binary16 conversion source lacks a range proof: {tokens[0]}") + swizzled = emit_quad_swizzle_u32(lines, i, s0_val, perm) + word = f"(asint(({swizzled}) << 16) >> 16)" if signed else f"(({swizzled}) & 0xffffu)" + lines.append(f" {'int' if signed else 'uint'} cv{i}_0 = {word};") + lines.append(f" precise float hs{i} = float(cv{i}_0);") + lines.append(f" precise float alu{i} = asfloat(narrow16(hs{i}));") + vector[dst] = ("half16", f"alu{i}") + if result is not None: + known_half[f"alu{i}"] = result + elif m := match(r"v_cvt_(i16|u16)_f16_dpp (v\d+), (.+)"): + # Quad-local DPP16/DPP8 binary16 to 16-bit integer conversion (truncation toward zero) + # under the same contract as plain v_cvt_{i16,u16}_f16: statically proven finite src0 + # whose truncated integer lies in range, producing ("low16", alu). DPP16 permits + # bit-15 abs/neg modifiers; DPP8 refuses modifiers. + kind, 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) + if f16_to_int_result(kind == "i16", b0) is None: + raise ValueError(f"binary16 value out of range: v_cvt_{kind}_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)" + lines.append(f" uint hf{i}_0 = {s0_bits};") + lines.append(f" precise float hs{i} = widen16(hf{i}_0);") + lines.append(f" int cv{i}_0 = int(trunc(hs{i}));") + lines.append(f" precise float alu{i} = asfloat(asuint(cv{i}_0) & 0xffffu);") + vector[dst] = ("low16", f"alu{i}") elif m := match(rf"v_({'|'.join(DPP_INT32)})_dpp (v\d+), (.+)"): op, dst, rest = m.groups() arity, template, helper = DPP_INT32[op] From ef9c434021b0f3699590dcee8d3c2cecef011d91 Mon Sep 17 00:00:00 2001 From: KomaGuy <265821007+KomaGuy@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:56:09 +0300 Subject: [PATCH 2/2] tests: pin |v| and abs(v) refusal on v_ldexp_f16_dpp src1 --- tests/pixel_aot_check.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index 6790231403..6f562ad22f 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -10566,6 +10566,10 @@ def check_dpp_f16_contract_controls(): "binary16 result overflows: v_ldexp_f16_dpp"), (["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 2"], f"v_ldexp_f16_dpp v24, v20, -v21 {identity}", "Source modifiers are not allowed on -v21"), + (["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 2"], f"v_ldexp_f16_dpp v24, v20, |v21| {identity}", + "Source modifiers are not allowed on |v21|"), + (["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 2"], f"v_ldexp_f16_dpp v24, v20, abs(v21) {identity}", + "Source modifiers are not allowed on abs(v21)"), (["v_mov_b32_e32 v20, 0x3c00", "v_mov_b32_e32 v21, 2"], "v_ldexp_f16_dpp v24, -v20, v21 dpp8:[1,2,3,0,5,6,7,4]", "Source modifiers are not allowed on -v20"), # v_cvt_f16_{i16,u16}_dpp: runtime u16 refused, 65520 overflow, float modifiers refused on DPP16, half16 refused.