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

Merged
JB2WIN merged 3 commits from upstream-pr/854 into main 2026-11-23 14:08:09 +00:00
JB2WIN commented 2026-11-23 13:41:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JB2WIN on 2026-11-23T13:41:29Z.


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 #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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6560628962 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/854). Originally posted by [JB2WIN](https://github.com/JB2WIN) on 2026-11-23T13:41:29Z. --- ## 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 #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](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:45:06 +00:00
R0Y00 commented 2026-11-24 06:49:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T13:49:26Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7103081255 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/854#issuecomment-7103081255). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T13:49:26Z. --- 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 in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!848
No description provided.