shader: lower SDWA moves and half widening in pixel_aot #324

Merged
Its3rr0rsWRLD merged 5 commits from upstream-pr/329 into main 2026-11-22 09:24:16 +00:00
Its3rr0rsWRLD commented 2026-11-21 22:20:08 +00:00 (Migrated from github.com)

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


Change

pixel_aot refused every SDWA instruction. This lowers the SDWA forms of v_mov_b32 and v_cvt_f32_f16, which shaders use to unpack bytes and halves. It also adds sdwa_source and sdwa_dest helpers that later SDWA lowerings can reuse.

  • src0_sel takes BYTE_0–BYTE_3, WORD_0/WORD_1 or DWORD. The selection is zero-extended, or sign-extended under sext().
  • dst_sel writes the low bits of the result into that byte or word. UNUSED_PAD zeroes the other bits. UNUSED_PRESERVE keeps them from the destination's tracked value, or uses zero when the destination has none.
  • SDWA v_cvt_f32_f16 widens WORD_0 or WORD_1 through the existing widen16, and applies abs, neg and clamp after widening as the e64 form does. It accepts only dst_sel:DWORD.
  • Refused:
    • UNUSED_SEXT;
    • byte selections for half widening;
    • SDWA text that isn't LLVM 15's canonical form (all three fields printed).

Sources for the semantics. The selection and padding rules follow the instruction equivalences in LLVM 15's SIPeepholeSDWA.cpp:

  • v_lshlrev_b32 by 16 or 24 becomes dst_sel:WORD_1/BYTE_3 dst_unused:UNUSED_PAD;
  • v_lshrrev_b32/v_ashrrev_i32 by 16 become a zero- or sign-extended src0_sel:WORD_1;
  • a mask-and-OR pair becomes UNUSED_PRESERVE.

That pass never produces UNUSED_SEXT, and I couldn't confirm from a source I could quote how it treats the bits below a byte or word field, so it stays refused rather than guessed.

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 two programs × two constant sets. Every draw reports debug_errors=0 and max_absolute_error=0.
  • tests/pixel_aot_check.py binary passes with both programs in 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 pass.

Evidence and follow-up

Both programs are synthetic.

The UNUSED_PRESERVE program moves WORD_1 of one constant, and a sign-extended BYTE_2 of another, into WORD_0 of a register holding 1.0. The word is then 1 + f × 2^-23, and (v - 1) × 2^23 is split into the two bytes of f. A zeroed high half would make the word a subnormal, and the readback would fail.

The UNUSED_PAD program does three things:

  • moves BYTE_0 into BYTE_3, which gives an exact power of two;
  • moves WORD_0 into WORD_1;
  • widens both halves of one constant, the low one through -|x|.

The reference works on the integer words in Python and does not use the lowering. Thirteen wrong references each report pixel_mismatch: the neighbouring byte or word, a zero-extended negative byte, a sign-extended positive byte, and the other half for widening. The translator on 6ee2406 rejects both programs at their first SDWA instruction.

Next steps would be SDWA forms of the other supported VOP1/VOP2 operations, UNUSED_SEXT once its effect on the lower bits is pinned down, and the dst_sel:WORD_1 packing form of v_cvt_f16_f32 after #324.

This branch merges cleanly with #288, #293 and #324.

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:6535148152 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/329). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T22:20:08Z. --- ## Change `pixel_aot` refused every SDWA instruction. This lowers the SDWA forms of `v_mov_b32` and `v_cvt_f32_f16`, which shaders use to unpack bytes and halves. It also adds `sdwa_source` and `sdwa_dest` helpers that later SDWA lowerings can reuse. - **`src0_sel`** takes `BYTE_0`–`BYTE_3`, `WORD_0`/`WORD_1` or `DWORD`. The selection is zero-extended, or sign-extended under `sext()`. - **`dst_sel`** writes the low bits of the result into that byte or word. `UNUSED_PAD` zeroes the other bits. `UNUSED_PRESERVE` keeps them from the destination's tracked value, or uses zero when the destination has none. - **SDWA `v_cvt_f32_f16`** widens `WORD_0` or `WORD_1` through the existing `widen16`, and applies `abs`, `neg` and `clamp` after widening as the e64 form does. It accepts only `dst_sel:DWORD`. - **Refused:** - `UNUSED_SEXT`; - byte selections for half widening; - SDWA text that isn't LLVM 15's canonical form (all three fields printed). **Sources for the semantics.** The selection and padding rules follow the instruction equivalences in LLVM 15's `SIPeepholeSDWA.cpp`: - `v_lshlrev_b32` by 16 or 24 becomes `dst_sel:WORD_1`/`BYTE_3 dst_unused:UNUSED_PAD`; - `v_lshrrev_b32`/`v_ashrrev_i32` by 16 become a zero- or sign-extended `src0_sel:WORD_1`; - a mask-and-OR pair becomes `UNUSED_PRESERVE`. That pass never produces `UNUSED_SEXT`, and I couldn't confirm from a source I could quote how it treats the bits below a byte or word field, so it stays refused rather than guessed. ## 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 two programs × two constant sets. Every draw reports `debug_errors=0` and `max_absolute_error=0`. - `tests/pixel_aot_check.py binary` passes with both programs in 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` pass. ## Evidence and follow-up Both programs are synthetic. **The `UNUSED_PRESERVE` program** moves `WORD_1` of one constant, and a sign-extended `BYTE_2` of another, into `WORD_0` of a register holding 1.0. The word is then 1 + f × 2^-23, and `(v - 1) × 2^23` is split into the two bytes of f. A zeroed high half would make the word a subnormal, and the readback would fail. **The `UNUSED_PAD` program** does three things: - moves `BYTE_0` into `BYTE_3`, which gives an exact power of two; - moves `WORD_0` into `WORD_1`; - widens both halves of one constant, the low one through `-|x|`. The reference works on the integer words in Python and does not use the lowering. Thirteen wrong references each report `pixel_mismatch`: the neighbouring byte or word, a zero-extended negative byte, a sign-extended positive byte, and the other half for widening. The translator on 6ee2406 rejects both programs at their first SDWA instruction. Next steps would be SDWA forms of the other supported VOP1/VOP2 operations, `UNUSED_SEXT` once its effect on the lower bits is pinned down, and the `dst_sel:WORD_1` packing form of `v_cvt_f16_f32` after #324. This branch merges cleanly with #288, #293 and #324. ## 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:27 +00:00
alexander-src commented 2026-11-24 06:48:09 +00:00 (Migrated from github.com)

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


Checked a96a328 on Linux (python3 -B tests/pixel_aot_check.py binary llvm-mc llvm-objcopy libLLVM.so passed with LLVM 23.1).

A few edge cases caught in v_mov_b32_sdwa in tools/pixel_aot.py:

  1. old = vector.get(dst) only checks isinstance(old, str), so v_mov_b32_sdwa v0, v12 dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE src0_sel:WORD_0 reads "bary_i" and emits asuint(bary_i) & 0xffff0000u (undeclared identifier in HLSL; same for v1 and v8/v9). Checking old.startswith(("sample", "alu", "params[")) only when dst_sel != "DWORD" and unused == "UNUSED_PRESERVE" fixes that and avoids rejecting DWORD or UNUSED_PAD overwrites of a previously packed register.
  2. Under UNUSED_PRESERVE, if unused == "UNUSED_PAD" or old is None: return field silently substitutes zero for an untracked destination (such as v20), even though hardware preserves whatever was in v20. Raising ValueError when UNUSED_PRESERVE targets an untracked register (with dst_sel != "DWORD") would match pixel_aot's strict tracking contract elsewhere and avoid hiding an undefined-bit read.
  3. Minor: llvm-mc -mcpu=gfx1013 assembles and disassembles negative float inline constants like v_mov_b32_sdwa v12, -0.5 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD, while the regex ([vs]\d+|-?\d+|\d+\.\d+) rejects -0.5, -1.0, -2.0, and -4.0.

AI-assisted: yes.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070197293 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/329#issuecomment-7070197293). Originally posted by [alexander-src](https://github.com/alexander-src) on 2026-11-21T22:27:43Z. --- Checked `a96a328` on Linux (`python3 -B tests/pixel_aot_check.py binary llvm-mc llvm-objcopy libLLVM.so` passed with LLVM 23.1). A few edge cases caught in `v_mov_b32_sdwa` in `tools/pixel_aot.py`: 1. `old = vector.get(dst)` only checks `isinstance(old, str)`, so `v_mov_b32_sdwa v0, v12 dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE src0_sel:WORD_0` reads `"bary_i"` and emits `asuint(bary_i) & 0xffff0000u` (undeclared identifier in HLSL; same for `v1` and `v8`/`v9`). Checking `old.startswith(("sample", "alu", "params["))` only when `dst_sel != "DWORD"` and `unused == "UNUSED_PRESERVE"` fixes that and avoids rejecting `DWORD` or `UNUSED_PAD` overwrites of a previously packed register. 2. Under `UNUSED_PRESERVE`, `if unused == "UNUSED_PAD" or old is None: return field` silently substitutes zero for an untracked destination (such as `v20`), even though hardware preserves whatever was in `v20`. Raising `ValueError` when `UNUSED_PRESERVE` targets an untracked register (with `dst_sel != "DWORD"`) would match `pixel_aot`'s strict tracking contract elsewhere and avoid hiding an undefined-bit read. 3. Minor: `llvm-mc -mcpu=gfx1013` assembles and disassembles negative float inline constants like `v_mov_b32_sdwa v12, -0.5 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD`, while the regex `([vs]\d+|-?\d+|\d+\.\d+)` rejects `-0.5`, `-1.0`, `-2.0`, and `-4.0`. AI-assisted: yes.
Its3rr0rsWRLD commented 2026-11-24 06:48:11 +00:00 (Migrated from github.com)

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


Thanks, @alexander-src, all three confirmed and fixed in e99c9bc:

  1. and 2. sdwa_dest now checks provenance only when bits are kept (dst_sel not DWORD and UNUSED_PRESERVE). It refuses a destination that is untracked (v20) or non-numeric (v0, v1, v8, v9) instead of assuming zero. DWORD and UNUSED_PAD writes may still replace any register. The docs paragraph says so.
  2. Negative inline floats are accepted: v_mov_b32_sdwa v12, -0.5 ... lowers through asuint((-0.5f)).

The refusals, the -0.5 move and a DWORD/UNUSED_PRESERVE overwrite are new controls. Before the fix, the old code emitted asuint(bary_i) for v0 and zero-filled v20.

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:7070708450 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/329#issuecomment-7070708450). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T23:11:39Z. --- Thanks, @alexander-src, all three confirmed and fixed in e99c9bc: 1. and 2. `sdwa_dest` now checks provenance only when bits are kept (`dst_sel` not `DWORD` and `UNUSED_PRESERVE`). It refuses a destination that is untracked (`v20`) or non-numeric (`v0`, `v1`, `v8`, `v9`) instead of assuming zero. `DWORD` and `UNUSED_PAD` writes may still replace any register. The docs paragraph says so. 3. Negative inline floats are accepted: `v_mov_b32_sdwa v12, -0.5 ...` lowers through `asuint((-0.5f))`. The refusals, the -0.5 move and a `DWORD`/`UNUSED_PRESERVE` overwrite are new controls. Before the fix, the old code emitted `asuint(bary_i)` for `v0` and zero-filled `v20`. 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!324
No description provided.