shader: lower gfx1013 v_cvt_f16_f32 in pixel_aot #319

Merged
Its3rr0rsWRLD merged 4 commits from upstream-pr/324 into main 2026-11-22 09:40:35 +00:00
Its3rr0rsWRLD commented 2026-11-21 22:09:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Its3rr0rsWRLD on 2026-11-21T22:09:21Z.


Change

pixel_aot rejected v_cvt_f16_f32, the binary32-to-binary16 conversion that is the reverse of the v_cvt_f32_f16 lowering from #297. This adds both encodings, e32 and e64 with abs and neg.

A new narrow16 helper builds the binary16 encoding in integer arithmetic, rounding to nearest even. Desktop f32tof16 and denormal handling therefore cannot change the result:

  • overflow, including the 65520 tie, gives infinity;
  • a binary16 subnormal result is kept;
  • a binary32 subnormal input gives a signed zero;
  • a NaN keeps its sign and top payload bits with the quiet bit set.

On GFX10 the result replaces only the low 16 bits of the destination. The LLVM 15 list GCNSubtarget::zeroesHigh16BitsOfDest (AMDGPUSubtarget.cpp) says "On gfx10, all 16-bit instructions preserve the high bits" and includes V_CVT_F16_F32_e32/_e64. The lowering therefore merges into the destination's tracked value. A destination with no tracked value contributes zero. clamp and SDWA forms are refused.

Verification

Commit 6ee2406 plus this change, Windows 10 Enterprise 26H2 build 26300.9974, MSVC 14.51.36231, Windows SDK 10.0.26100.0 DXC.

ctest --preset dev --no-tests=error -R '^pixel_aot_' -V
  • pixel_aot_hardware: Passed on NVIDIA GeForce RTX 4070 Ti (10de:2782), driver 32.0.16.1088.
  • pixel_aot_warp: Passed on WARP 10.0.26100.9818.
  • Each test runs 58 draws with this change and 54 on 6ee2406; the four extra draws are the four constant sets. Every draw reports debug_errors=0 and max_absolute_error=0.
  • tests/pixel_aot_check.py binary passes with the program added to the binary fixtures: LLVM 15.0.6 llvm-mc and llvm-objcopy, disassembled through libLLVM-17.so and libLLVM-18.so on Linux. The controls, tests/docs_links_check.py and tests/bidi_controls_check.py also pass.
  • The rounding in narrow16, transcribed to Python, matches NumPy's float16 conversion on 412,799 binary32 inputs: 400,000 random bit patterns, the rounding points of every exponent, and both neighbours of every binary16 subnormal boundary. NaN inputs were excluded, because NumPy's payload handling differs.

Evidence and follow-up

The fixture is synthetic. One shader reads the whole destination word. It narrows into a register holding 1.0, so a kept high half makes the word 1 + h × 2^-23, and (v - 1) × 2^23 is split into the two bytes of the encoding with v_floor_f32 and v_fmac_f32. A zeroed high half would turn the word into a subnormal. The same shader also widens -|S0| and a subnormal result (scaled by 2^24) back to binary32. The four constant sets cover:

  • ties to even at 1 + 2^-11, 1 + 3 × 2^-11, 1.5 × 2^-24 and 2^-25;
  • 65520 − 2^-8 (rounds to 65504) and the overflow tie 65520 (rounds to infinity);
  • the carry from the largest subnormal into 2^-14;
  • negative subnormals, -0, the negation modifier, and a binary32 subnormal input.

Eleven wrong references each report pixel_mismatch: round-half-up, truncation, early and saturating overflow, a missing carry, and a lost sign or negation. The reference uses Python's own binary16 packing, not the lowering's algorithm. The translator on 6ee2406 rejects the program at its first v_cvt_f16_f32.

Not measured: NaN inputs, which are not drawn, and whether Scarlett titles run with binary16 denormals enabled, as the lowering assumes. That is LLVM's default for shaders.

This branch merges cleanly with #288 and #293.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: yes. Written with Claude Opus 5.5; verified on the hardware listed above.

🤖 Generated with Claude Code

https://claude.ai/code/session_01X1EBSe6YfE2ivQChRsx1hc

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535036758 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/324). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T22:09:21Z. --- ## Change `pixel_aot` rejected `v_cvt_f16_f32`, the binary32-to-binary16 conversion that is the reverse of the `v_cvt_f32_f16` lowering from #297. This adds both encodings, e32 and e64 with `abs` and `neg`. A new `narrow16` helper builds the binary16 encoding in integer arithmetic, rounding to nearest even. Desktop `f32tof16` and denormal handling therefore cannot change the result: - overflow, including the 65520 tie, gives infinity; - a binary16 subnormal result is kept; - a binary32 subnormal input gives a signed zero; - a NaN keeps its sign and top payload bits with the quiet bit set. On GFX10 the result replaces only the low 16 bits of the destination. The LLVM 15 list `GCNSubtarget::zeroesHigh16BitsOfDest` (`AMDGPUSubtarget.cpp`) says "On gfx10, all 16-bit instructions preserve the high bits" and includes `V_CVT_F16_F32_e32`/`_e64`. The lowering therefore merges into the destination's tracked value. A destination with no tracked value contributes zero. `clamp` and SDWA forms are refused. ## Verification Commit 6ee2406 plus this change, Windows 10 Enterprise 26H2 build 26300.9974, MSVC 14.51.36231, Windows SDK 10.0.26100.0 DXC. ```powershell ctest --preset dev --no-tests=error -R '^pixel_aot_' -V ``` - `pixel_aot_hardware`: Passed on NVIDIA GeForce RTX 4070 Ti (10de:2782), driver 32.0.16.1088. - `pixel_aot_warp`: Passed on WARP 10.0.26100.9818. - Each test runs 58 draws with this change and 54 on 6ee2406; the four extra draws are the four constant sets. Every draw reports `debug_errors=0` and `max_absolute_error=0`. - `tests/pixel_aot_check.py binary` passes with the program added to the binary fixtures: LLVM 15.0.6 `llvm-mc` and `llvm-objcopy`, disassembled through `libLLVM-17.so` and `libLLVM-18.so` on Linux. The controls, `tests/docs_links_check.py` and `tests/bidi_controls_check.py` also pass. - The rounding in `narrow16`, transcribed to Python, matches NumPy's float16 conversion on 412,799 binary32 inputs: 400,000 random bit patterns, the rounding points of every exponent, and both neighbours of every binary16 subnormal boundary. NaN inputs were excluded, because NumPy's payload handling differs. ## Evidence and follow-up The fixture is synthetic. One shader reads the whole destination word. It narrows into a register holding 1.0, so a kept high half makes the word 1 + h × 2^-23, and `(v - 1) × 2^23` is split into the two bytes of the encoding with `v_floor_f32` and `v_fmac_f32`. A zeroed high half would turn the word into a subnormal. The same shader also widens `-|S0|` and a subnormal result (scaled by 2^24) back to binary32. The four constant sets cover: - ties to even at 1 + 2^-11, 1 + 3 × 2^-11, 1.5 × 2^-24 and 2^-25; - 65520 − 2^-8 (rounds to 65504) and the overflow tie 65520 (rounds to infinity); - the carry from the largest subnormal into 2^-14; - negative subnormals, -0, the negation modifier, and a binary32 subnormal input. Eleven wrong references each report `pixel_mismatch`: round-half-up, truncation, early and saturating overflow, a missing carry, and a lost sign or negation. The reference uses Python's own binary16 packing, not the lowering's algorithm. The translator on 6ee2406 rejects the program at its first `v_cvt_f16_f32`. Not measured: NaN inputs, which are not drawn, and whether Scarlett titles run with binary16 denormals enabled, as the lowering assumes. That is LLVM's default for shaders. This branch merges cleanly with #288 and #293. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: yes. Written with Claude Opus 5.5; verified on the hardware listed above. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01X1EBSe6YfE2ivQChRsx1hc
upstream-sync closed this pull request 2026-11-24 06:42:25 +00:00
alexander-src commented 2026-11-24 06:48:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by alexander-src on 2026-11-21T22:27:18Z.


Checked 042b1f1 on Linux (python3 -B tests/pixel_aot_check.py binary llvm-mc llvm-objcopy libLLVM.so passed with LLVM 23.1, and narrow16 compiled in C++ matched Python's IEEE-754 half packing across all 25 boundary/rounding/overflow/subnormal cases).

Two bugs caught in the v_cvt_f16_f32 lowering in tools/pixel_aot.py:

  1. Pulling (-?) out before calling fp_operand() miscompiles negative inline integer constants. llvm-mc -mcpu=gfx1013 assembles and disassembles v_cvt_f16_f32_e32 v12, -1 verbatim, where -1 is the 32-bit pattern 0xffffffff (quiet NaN -> 0xfe00), but the current regex hands "1" to fp_operand() and emits narrow16((-asfloat(1u))) (-0.0 -> 0x8000). Passing the full operand directly to fp_operand() fixes -1 and also handles |0.5| / -|0.5|.
  2. old = vector.get(dst) only checks isinstance(old, str), so writing into v0 ("bary_i") or v1 ("bary_j") emits asuint(bary_i) & 0xffff0000u (undeclared identifier in HLSL), and v8/v9 ("uv.x"/"uv.y") also bypass the numeric-register check. Requiring old.startswith(("sample", "alu", "params[")) (matching fp_operand) rejects non-numeric provenance strings cleanly.

AI-assisted: yes.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070192274 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/324#issuecomment-7070192274). Originally posted by [alexander-src](https://github.com/alexander-src) on 2026-11-21T22:27:18Z. --- Checked `042b1f1` on Linux (`python3 -B tests/pixel_aot_check.py binary llvm-mc llvm-objcopy libLLVM.so` passed with LLVM 23.1, and `narrow16` compiled in C++ matched Python's IEEE-754 half packing across all 25 boundary/rounding/overflow/subnormal cases). Two bugs caught in the `v_cvt_f16_f32` lowering in `tools/pixel_aot.py`: 1. Pulling `(-?)` out before calling `fp_operand()` miscompiles negative inline integer constants. `llvm-mc -mcpu=gfx1013` assembles and disassembles `v_cvt_f16_f32_e32 v12, -1` verbatim, where `-1` is the 32-bit pattern `0xffffffff` (quiet NaN -> `0xfe00`), but the current regex hands `"1"` to `fp_operand()` and emits `narrow16((-asfloat(1u)))` (`-0.0` -> `0x8000`). Passing the full operand directly to `fp_operand()` fixes `-1` and also handles `|0.5|` / `-|0.5|`. 2. `old = vector.get(dst)` only checks `isinstance(old, str)`, so writing into `v0` (`"bary_i"`) or `v1` (`"bary_j"`) emits `asuint(bary_i) & 0xffff0000u` (undeclared identifier in HLSL), and `v8`/`v9` (`"uv.x"`/`"uv.y"`) also bypass the numeric-register check. Requiring `old.startswith(("sample", "alu", "params["))` (matching `fp_operand`) rejects non-numeric provenance strings cleanly. AI-assisted: yes.
Its3rr0rsWRLD commented 2026-11-24 06:48:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Its3rr0rsWRLD on 2026-11-21T23:11:37Z.


Thanks, @alexander-src, both confirmed and fixed in 3007879:

  1. The operand is now passed whole to fp_operand, so v_cvt_f16_f32_e32 v12, -1 lowers to narrow16(asfloat(4294967295u)). Before, the old regex emitted narrow16((-asfloat(1u))). -|0.5| lowers to narrow16((-abs(0.5f))).
  2. A destination must be absent or hold a sample/alu/params[ value, as in fp_operand, so writes into v0, v1, v8 and v9 are refused.

Both cases are new controls. Before the fix, the old code accepted v0 and produced the -asfloat(1u) form.

Rerun on Windows 10 Enterprise 26H2 build 26300.9974 with ctest --preset dev --no-tests=error -R '^pixel_aot_' -V, merged with current main (bc24dfd): pixel_aot_hardware (RTX 4070 Ti, driver 32.0.16.1088) and pixel_aot_warp (WARP 10.0.26100.9818) pass with 59 draws each against 55 on main. Every draw reports debug_errors=0 and max_absolute_error=0. tests/pixel_aot_check.py binary and the controls pass on Linux with LLVM 15.0.6 tools and libLLVM 18.

AI-assisted: yes. Claude Opus 5.5 replying from this account.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070708191 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/324#issuecomment-7070708191). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T23:11:37Z. --- Thanks, @alexander-src, both confirmed and fixed in 3007879: 1. The operand is now passed whole to `fp_operand`, so `v_cvt_f16_f32_e32 v12, -1` lowers to `narrow16(asfloat(4294967295u))`. Before, the old regex emitted `narrow16((-asfloat(1u)))`. `-|0.5|` lowers to `narrow16((-abs(0.5f)))`. 2. A destination must be absent or hold a `sample`/`alu`/`params[` value, as in `fp_operand`, so writes into `v0`, `v1`, `v8` and `v9` are refused. Both cases are new controls. Before the fix, the old code accepted `v0` and produced the `-asfloat(1u)` form. Rerun on Windows 10 Enterprise 26H2 build 26300.9974 with `ctest --preset dev --no-tests=error -R '^pixel_aot_' -V`, merged with current main (bc24dfd): `pixel_aot_hardware` (RTX 4070 Ti, driver 32.0.16.1088) and `pixel_aot_warp` (WARP 10.0.26100.9818) pass with 59 draws each against 55 on main. Every draw reports `debug_errors=0` and `max_absolute_error=0`. `tests/pixel_aot_check.py binary` and the controls pass on Linux with LLVM 15.0.6 tools and libLLVM 18. AI-assisted: yes. Claude Opus 5.5 replying from this account.
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!319
No description provided.