Skip to content

shader: lower plain binary16 add, sub, subrev and mul in pixel_aot - #854

Open
JB2WIN wants to merge 3 commits into
R0Y00:mainfrom
JB2WIN:shader/f16-vop2-arith
Open

JB2WIN wants to merge 3 commits into
R0Y00:mainfrom
JB2WIN:shader/f16-vop2-arith

Conversation

@JB2WIN

@JB2WIN JB2WIN commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Change

This adds the first plain binary16 VOP2 slice from #839. tools/pixel_aot.py now lowers v_add_f16, v_sub_f16, v_subrev_f16 and v_mul_f16 in their e32 and _e64 spellings. The scope follows the decisions in #839:

  • Finite operands only (variant b). Both operands must be statically proven finite: a binary16 inline constant, a 16-bit literal, a tracked scalar constant, a VGPR copy of a known constant, or the result of an earlier proven op. The proof is keyed on immutable SSA temporaries, so a later write to the register cannot make a value stale. Every other source is refused. A constant-buffer word, a sample or an interpolant gives binary16 operand lacks a finite proof: <tok>, and a NaN/inf constant gives binary16 operand is not finite: <tok>.
  • Finite overflow is refused. The result is known at translation time. If it would round to ±inf, the op is refused with binary16 result overflows: v_<op>_f16. Both boundaries are pinned with both signs: 65520 is refused and 65519.99 is accepted.
  • Explicit entry profile. lower(..., half_denorm="preserve"), also exposed as --half-denorm. Without it the four forms are refused with binary16 denormal mode unknown at entry. MODE writes that could change the denormal field stay refused, and the existing round-toward-zero gate covers all eight spellings.
  • Exact arithmetic. The lowering applies widen16 to both low halves, does one precise binary32 operation, then narrow16. Binary32 mul of two halves is exact. For add/sub, double rounding through binary32 is harmless (24 ≥ 2·11+2). An offline numpy run over all 4,030,726,144 finite operand pairs of add and mul found 0 differences; that run is not part of CI. CI runs an independent Python reference.
  • Low-half contract. The result is tracked as ("half16", …), separate from integer low16. Its only readers are v_cvt_f32_f16 (e32/e64, without modifiers), v_cvt_f32_f16_sdwa with WORD_0, and v_mov_b32_sdwa with WORD_0. Every other reader is refused, with a control per reader class.
  • Refused spellings. Source modifiers, clamp, omod, DPP/SDWA spellings and integer inline constants other than 0 are refused. min/max stay out of scope.
  • Changed expectation, not removed. The existing v_add_f16_e64 refusal neighbour in the controls and form_coverage_check.py is replaced by v_min_f16_e64, which is still refused.

Refs #839. AI-assisted (Claude Code).

Verification

Windows 11 10.0.26200.9974, NVIDIA GeForce RTX 3080 (driver 32.0.16.2124) and WARP 10.0.26100.9818, debug layer enabled, at 5d80e54.

  • Draw draw-f16-arith (pixel_aot_f16_arith_{hardware,warp}). It covers 40 literal operand pairs per op, including chains of proven results. Results are read back losslessly through the integer target and checked against an independent reference. The cases cover halfway rounding, positive and negative subnormals, and signed zero, including (-0)+(-0) and exact cancellation. Programs with subnormal operands also pass under -denorm ftz, because the readers use asuint only.

  • Mutations. Each of these must report a mismatch and does on both adapters, and a reference-side hook checks before the draw that each one changes an expected word:

    • truncation instead of rounding;
    • ties away from zero;
    • flushing subnormal outputs;
    • flushing subnormal inputs;
    • dropping the sign of negative subnormal inputs;
    • dropping the sign of a zero result;
    • reading the operand from bits 31:16;
    • subrev as sub, sub as add, and mul as add;
    • the sign of an exact-zero sum.

    An SDWA WORD_1 widen of a result is refused at lowering.

  • Dev-time check (not committed). With the new branch disabled, the new controls fail.

  • Commands:

    • ctest --preset dev --no-tests=error -R "^pixel_aot_f16_arith_": 2/2 passed, 33.3 s on hardware.
    • Full ctest --preset dev --no-tests=error -j4: 435/435 passed. Only capability skips remained (bvh_wave64_*, wave_shuffle_warp, spirv_minlod_*).
    • The LLVM 15 binary round trip passes, including a pin that LLVM prints the literal 0xffff as -1. It runs locally with llvm-mc through a clang -cc1as shim.
    • The controls, form_coverage_check.py, rdna2_tools_check.py, pso_readiness_check.py and docs_links_check.py pass.

Limits

  • Runtime operands, NaN/inf behaviour and finite overflow are left for a later extension, as pixel_aot: non-packed binary16 VOP1/VOP2 ALU under the low-16-only contract (scope question) #839 decided. The subrev operand order and the console's binary16 denormal handling are not measured natively. The destination's high half stays unknown and is guarded only by the refusals.
  • tools/scan_unsupported_shaders.py still reports these forms as unsupported. That is conservative, since the scanner cannot see the finite proof.

🤖 Generated with Claude Code

JB2WIN and others added 3 commits November 23, 2026 14:31
v_{add,sub,subrev,mul}_f16 (e32 and e64) under the maintainer's first
slice (R0Y00#839): both operands must be statically proven finite (binary16
inline constants, 16-bit literals, tracked constants and register copies,
or an earlier proven result), finite overflow is refused, and the forms
require an explicit binary16 denormal-preserve entry profile
(half_denorm="preserve"). widen16, one precise binary32 operation and
narrow16 give the exact nearest-even result. The result is tracked as a
half16 low half read only by v_cvt_f32_f16 and SDWA WORD_0; every other
reader is refused. Adds controls, LLVM 15 binary fixtures, form coverage
and an integer-readback draw with 11 mutations (pixel_aot_f16_arith_*).

Refs R0Y00#839

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@R0Y00

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

I refreshed the branch in c608fc1 after #704 and #853. The shared dispatcher retains the divide-FMA, DS NOP and binary16 checks. In an independent run, 80,144 finite-input proof checks matched Python binary16 conversion, and WARP and RTX 4060 Laptop caught all 11 mutations. On the final rebased candidate I reran all three WARP draw modes, the LLVM 15 binary suite, form coverage and docs links. They passed. GitHub CI is running on the updated head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants