shader: lower SDWA binary16 add, sub, subrev and mul in gfx2hlsl #2731

Merged
tedin7 merged 6 commits from upstream-pr/2739 into main 2026-11-26 05:57:38 +00:00
tedin7 commented 2026-11-26 04:43:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T04:43:29Z.


Part of #1723

What this unblocks: v_add_f16_sdwa, v_sub_f16_sdwa, v_subrev_f16_sdwa and v_mul_f16_sdwa stop being refused in gfx2hlsl.py, so shaders that add or multiply a selected half of a register and write it into one half of another (the packed-half pattern) translate instead of stopping at the first SDWA binary16 instruction.

What changed

  • Stage.sdwa_half (called from sdwa()): each source is the half chosen by src0_sel / src1_sel (WORD_0 or WORD_1; DWORD reads the low half, as the existing SDWA v_cmp_class_f16 does), widened exactly with f16tof32, combined with the same expression the plain VEC_H entry uses, rounded once with f16rn, and written into dst_sel WORD_0 or WORD_1 through the existing sdwa_dest (UNUSED_PAD zeroes the other half, UNUSED_PRESERVE keeps the old one).
  • abs / neg source modifiers (-v, |v|, -|v|) flip or clear the half's sign bit, the same as on the plain form.
  • A WORD_0 write with UNUSED_PRESERVE into a register whose upper half is still undefined (after a d16 load) keeps it tagged as low-16-only, and a WORD_1 source read of such a register is refused (u() already refuses it).

What stays refused

  • dst_sel:DWORD: the upper half of a 16-bit result is not claimed here (same reason the low-16-only contract of #732 exists; no new lowering depends on it).
  • Byte selections, UNUSED_SEXT, sext(), clamp, output modifiers, inline-constant or scalar sources, v_min_f16 / v_max_f16 / v_fmac_f16 in SDWA form.
  • Binary16 compares and packed (v_pk_*) operations are separate slices and are not touched.

Why it is correct

The arithmetic is the one already merged for the plain forms (binary32 operation on exactly widened halves, one rounding to binary16; the 11-bit significand argument of #2073 applies unchanged). SDWA only changes which 16 bits are read and where the result lands; both are plain bit placement.

Tests

  • startup_gfx2hlsl_f16_alu_check.py: the emitted statement is evaluated for every lane of the draw fixtures (zeros of both signs, smallest and largest denormal, smallest normal, 65504, infinities, quiet/signalling NaN, NaN payload, tie-to-even cases, inf - inf, 0 * inf) against the exact-rational reference; the refusals above; the low-16-only tag cases.
  • startup_gfx2hlsl_f16_alu_draw_check.py (existing draw suite, extended): 22 configurations (3 x 3 source selections, the four destination placements with PAD and PRESERVE, all four operations, the abs/neg spellings, edge values) drawn on D3D12 and compared with the CPU reference word by word. Every source word carries a different non-NaN value in its unselected half, and the old destination word is a known value so PRESERVE is observable. NaN halves are canonicalised to 0x7e00 on both sides (no portable sign or payload). A wrong-value control (one bit changed in the expected word) must be reported as a mismatch, and the Python controls require that a swapped source half, the other destination half and PAD/PRESERVE exchanged each change at least one expected lane of every configuration. The startup_gfx2hlsl_f16_alu_draw_* CTest timeout goes from 300 s to 900 s for the larger set.

Verification:

  • python3 -I tests/startup_gfx2hlsl_f16_alu_check.py -> f16 alu ok
  • python3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py -> controls all ok
  • python3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py binary llvm-mc-19 -> fixture words match llvm-mc (gfx1013 encodings of all 22 bodies and vertex fixtures)
  • Mutants that fail the Python check: source half swapped, abs ignored, operand order reversed, PRESERVE tag dropped.
  • Linux tests/startup_*check.py sweep compared with main: 0 scripts differ.
  • Win2025 WARP dev+release: pending Stage-2

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6619675046 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T04:43:29Z. --- Part of #1723 What this unblocks: `v_add_f16_sdwa`, `v_sub_f16_sdwa`, `v_subrev_f16_sdwa` and `v_mul_f16_sdwa` stop being refused in `gfx2hlsl.py`, so shaders that add or multiply a selected half of a register and write it into one half of another (the packed-half pattern) translate instead of stopping at the first SDWA binary16 instruction. ## What changed - `Stage.sdwa_half` (called from `sdwa()`): each source is the half chosen by `src0_sel` / `src1_sel` (`WORD_0` or `WORD_1`; `DWORD` reads the low half, as the existing SDWA `v_cmp_class_f16` does), widened exactly with `f16tof32`, combined with the same expression the plain `VEC_H` entry uses, rounded once with `f16rn`, and written into `dst_sel` `WORD_0` or `WORD_1` through the existing `sdwa_dest` (`UNUSED_PAD` zeroes the other half, `UNUSED_PRESERVE` keeps the old one). - `abs` / `neg` source modifiers (`-v`, `|v|`, `-|v|`) flip or clear the half's sign bit, the same as on the plain form. - A `WORD_0` write with `UNUSED_PRESERVE` into a register whose upper half is still undefined (after a d16 load) keeps it tagged as low-16-only, and a `WORD_1` source read of such a register is refused (`u()` already refuses it). ## What stays refused - `dst_sel:DWORD`: the upper half of a 16-bit result is not claimed here (same reason the low-16-only contract of #732 exists; no new lowering depends on it). - Byte selections, `UNUSED_SEXT`, `sext()`, `clamp`, output modifiers, inline-constant or scalar sources, `v_min_f16` / `v_max_f16` / `v_fmac_f16` in SDWA form. - Binary16 compares and packed (`v_pk_*`) operations are separate slices and are not touched. ## Why it is correct The arithmetic is the one already merged for the plain forms (binary32 operation on exactly widened halves, one rounding to binary16; the 11-bit significand argument of #2073 applies unchanged). SDWA only changes which 16 bits are read and where the result lands; both are plain bit placement. ## Tests - `startup_gfx2hlsl_f16_alu_check.py`: the emitted statement is evaluated for every lane of the draw fixtures (zeros of both signs, smallest and largest denormal, smallest normal, 65504, infinities, quiet/signalling NaN, NaN payload, tie-to-even cases, `inf - inf`, `0 * inf`) against the exact-rational reference; the refusals above; the low-16-only tag cases. - `startup_gfx2hlsl_f16_alu_draw_check.py` (existing draw suite, extended): 22 configurations (3 x 3 source selections, the four destination placements with PAD and PRESERVE, all four operations, the abs/neg spellings, edge values) drawn on D3D12 and compared with the CPU reference word by word. Every source word carries a different non-NaN value in its unselected half, and the old destination word is a known value so PRESERVE is observable. NaN halves are canonicalised to `0x7e00` on both sides (no portable sign or payload). A wrong-value control (one bit changed in the expected word) must be reported as a mismatch, and the Python controls require that a swapped source half, the other destination half and PAD/PRESERVE exchanged each change at least one expected lane of every configuration. The `startup_gfx2hlsl_f16_alu_draw_*` CTest timeout goes from 300 s to 900 s for the larger set. Verification: - `python3 -I tests/startup_gfx2hlsl_f16_alu_check.py` -> `f16 alu ok` - `python3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py` -> controls all ok - `python3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py binary llvm-mc-19` -> `fixture words match llvm-mc` (gfx1013 encodings of all 22 bodies and vertex fixtures) - Mutants that fail the Python check: source half swapped, abs ignored, operand order reversed, PRESERVE tag dropped. - Linux `tests/startup_*check.py` sweep compared with main: 0 scripts differ. - Win2025 WARP dev+release: pending Stage-2 AI-assisted; commands listed were executed locally.
RonLogic commented 2026-11-26 05:02:28 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RonLogic on 2026-11-26T04:47:10Z.


Independent Windows GPU blocker on 4145d0803ecf77de448543ab1bf0be82b935cc91. The pure-Python check passes. The complete D3D12 flow check reported identical failures on in-box WARP 10.0.26100.10068 and RTX 4070 32.0.16.2124: ten FXC ps_5_1 WORD_0/PAD cases read back all four zero words. The first is v_add_f16_dword_dword_word_0_pad_plain_plain: expected 00004200,00004000,00003666,0000644c, actual 00000000,00000000,00000000,00000000. Both backends pass the DXC ps_6_0 cases. D3D12 debug layer enabled, Windows 26300.10075, SDK 10.0.26100.0. I am isolating whether the zeroing comes from the draw's NaN canonicalization or the translated arithmetic.

Isolated to the FLOAT test output. Each expected WORD_0/PAD result fits in 16 bits, so the existing asfloat export represents it as binary32 zero or a subnormal. In a local probe I changed only those fixture exports to F(nanc(vN) ^ 0x3f800000u) and XORed the expected words with the same constant. This keeps the expected output normal and does not hide a destination bit as an OR tag would. All ten original FXC failures then matched on each adapter, and a one-bit reference mutation was rejected for every case. The translated arithmetic and placement stayed unchanged. Please use this export encoding for the WORD_0/PAD draw cases or use an integer render target.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7182900656 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#issuecomment-7182900656). Originally posted by [RonLogic](https://github.com/RonLogic) on 2026-11-26T04:47:10Z. --- Independent Windows GPU blocker on `4145d0803ecf77de448543ab1bf0be82b935cc91`. The pure-Python check passes. The complete D3D12 flow check reported identical failures on in-box WARP `10.0.26100.10068` and RTX 4070 `32.0.16.2124`: ten FXC `ps_5_1` WORD_0/PAD cases read back all four zero words. The first is `v_add_f16_dword_dword_word_0_pad_plain_plain`: expected `00004200,00004000,00003666,0000644c`, actual `00000000,00000000,00000000,00000000`. Both backends pass the DXC `ps_6_0` cases. D3D12 debug layer enabled, Windows `26300.10075`, SDK `10.0.26100.0`. I am isolating whether the zeroing comes from the draw's NaN canonicalization or the translated arithmetic. Isolated to the FLOAT test output. Each expected WORD_0/PAD result fits in 16 bits, so the existing `asfloat` export represents it as binary32 zero or a subnormal. In a local probe I changed only those fixture exports to `F(nanc(vN) ^ 0x3f800000u)` and XORed the expected words with the same constant. This keeps the expected output normal and does not hide a destination bit as an OR tag would. All ten original FXC failures then matched on each adapter, and a one-bit reference mutation was rejected for every case. The translated arithmetic and placement stayed unchanged. Please use this export encoding for the WORD_0/PAD draw cases or use an integer render target.
upstream-sync closed this pull request 2026-11-26 06:01:59 +00:00
chatgpt-codex-connector[bot commented 2026-11-26 06:01:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T05:22:54Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ef44ab4b01

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6068948081 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#pullrequestreview-6068948081). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-26T05:22:54Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `ef44ab4b01` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". </details>
tedin7 commented 2026-11-26 06:01:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:31:24Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6069006244 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#pullrequestreview-6069006244). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T05:31:24Z. **Review:** COMMENTED --- _No description provided._
R0Y00 commented 2026-11-26 06:02:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-26T05:53:37Z.

Review: APPROVED


Rechecked the combined suite after the #2738 merge. Both the existing packed and new SDWA assertions passed, then FXC and DXC readback passed on Windows 11 build 26200 on WARP 10.0.26100.9818 and RTX 4060 Laptop 32.0.16.1656. The output tag carries the raw SDWA word through the float target, and the wrong-value control still fails on both compilers and adapters.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6069185761 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#pullrequestreview-6069185761). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-26T05:53:37Z. **Review:** APPROVED --- Rechecked the combined suite after the #2738 merge. Both the existing packed and new SDWA assertions passed, then FXC and DXC readback passed on Windows 11 build 26200 on WARP 10.0.26100.9818 and RTX 4060 Laptop 32.0.16.1656. The output tag carries the raw SDWA word through the float target, and the wrong-value control still fails on both compilers and adapters.
tedin7 commented 2026-11-26 06:02:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:14:21Z.


Thank you, RonLogic: your isolation was exactly right. A result that fits in 16 bits reads back from the float target as binary32 zero or a subnormal, and FXC drops it. The translated arithmetic was fine; the fixture export was not.

Fix pushed (startup_gfx2hlsl_f16_alu_draw_check.py): the drawn export is now F(nanc(vN) ^ TAG) and every expected word is XORed with the same tag.

One change from the suggestion: with 0x3f800000 some lanes (upper halves of the 0x4000 class, from the WORD_1 cases) turn into infinity or NaN as binary32. I use 0x60000000 instead, and a new control requires that every expected word of every configuration stays a finite normal float after the XOR.

Checks run locally (Linux only):

  • Tag removed from the HLSL export (the old behaviour): a new control fails for all 22 configurations.
  • The wrong-value draw control now XORs the tag too, so a one-bit error still shows as a mismatch.
  • python3 -I tests/startup_gfx2hlsl_f16_alu_check.py -> f16 alu ok; draw check controls all ok; binary llvm-mc-19 words still match; main merged in (no conflicts), Linux sweep compared with main: 0 differ.

The D3D12 draw itself is still for the Stage-2 run; if you can re-run your WARP / RTX flow on the new head, that would confirm the ten FXC cases.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7183305622 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#issuecomment-7183305622). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T05:14:21Z. --- Thank you, RonLogic: your isolation was exactly right. A result that fits in 16 bits reads back from the float target as binary32 zero or a subnormal, and FXC drops it. The translated arithmetic was fine; the fixture export was not. Fix pushed (`startup_gfx2hlsl_f16_alu_draw_check.py`): the drawn export is now `F(nanc(vN) ^ TAG)` and every expected word is XORed with the same tag. One change from the suggestion: with `0x3f800000` some lanes (upper halves of the `0x4000` class, from the WORD_1 cases) turn into infinity or NaN as binary32. I use `0x60000000` instead, and a new control requires that every expected word of every configuration stays a finite normal float after the XOR. Checks run locally (Linux only): - Tag removed from the HLSL export (the old behaviour): a new control fails for all 22 configurations. - The wrong-value draw control now XORs the tag too, so a one-bit error still shows as a mismatch. - `python3 -I tests/startup_gfx2hlsl_f16_alu_check.py` -> `f16 alu ok`; draw check controls all ok; `binary llvm-mc-19` words still match; main merged in (no conflicts), Linux sweep compared with main: 0 differ. The D3D12 draw itself is still for the Stage-2 run; if you can re-run your WARP / RTX flow on the new head, that would confirm the ten FXC cases. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-26 06:02:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:16:34Z.


Independent gate on head ef44ab4b0 passed, promoting to Ready.

Verification:

  • cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim startup_buffer_format_word3_check then ctest --preset dev -R '^startup_gfx2hlsl_f16_alu' --no-tests=error --output-on-failure on Windows Server 2025 (D3D12 WARP): 4/4 passed.
  • Same with --preset release: 4/4 passed. The WARP draw test compares GPU readback with the CPU reference word by word, including zeros of both signs, denormals, infinities, NaN and a wrong-value control.
  • python3 -I tests/startup_gfx2hlsl_f16_alu_check.py and python3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py: ok on Linux and Windows.

@MasterDomino @R0Y00 this is ready for your review.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7183340216 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#issuecomment-7183340216). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T05:16:34Z. --- Independent gate on head ef44ab4b0 passed, promoting to Ready. Verification: - `cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim startup_buffer_format_word3_check` then `ctest --preset dev -R '^startup_gfx2hlsl_f16_alu' --no-tests=error --output-on-failure` on Windows Server 2025 (D3D12 WARP): 4/4 passed. - Same with `--preset release`: 4/4 passed. The WARP draw test compares GPU readback with the CPU reference word by word, including zeros of both signs, denormals, infinities, NaN and a wrong-value control. - `python3 -I tests/startup_gfx2hlsl_f16_alu_check.py` and `python3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py`: ok on Linux and Windows. @MasterDomino @R0Y00 this is ready for your review. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-26 06:02:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T05:16:41Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T05:22:57.303362Z ef44ab4 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7183342159 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#issuecomment-7183342159). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-26T05:16:41Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"ef44ab4b01e49e9358632f8587531adfe53320f2","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-26T05:22:57.303362Z">2026-11-26T05:22:57.303362Z</relative-time> | `ef44ab4` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
chatgpt-codex-connector[bot commented 2026-11-26 06:02:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T05:22:55Z.

Code review location: tools/startup/gfx2hlsl.py; line 4141

@@ -4081,7 +4081,64 @@ def half_alu(self, base, ops, mods):
     # v_mov_b32 is the same path with the selected source as its result, v_cndmask_b32 selects with VCC,
     # VEC_U_TO_F conversions apply output modifiers before dst_sel, VEC_F forms stay refused, and
     # dst_unused UNUSED_SEXT has no lowering.
+    def sdwa_half(self, base, ops, mods):
+        """SDWA binary16 add, sub, subrev and mul: a selected half of each source, one rounding, one half written back.
+
+        Sources: src0_sel/src1_sel WORD_0 or WORD_1 pick the half; DWORD reads the low half, as the 16-bit
+        operation does. Destination: dst_sel WORD_0 or WORD_1 with dst_unused UNUSED_PAD (other half zero) or
+        UNUSED_PRESERVE (other half of the old register kept). dst_sel DWORD stays refused: the upper half of a
+        16-bit result is not claimed (an open question, see the low-16-only contract), and a byte selection, sext(),
+        clamp and the output modifiers have no lowering here. Sources are VGPRs with the abs/neg modifiers.
+        """
+        _arity, _kind, text, _result = VEC_H[base]
+        fields = {}
+        for m in mods:
+            key, _, value = m.partition(':')
+            if key in ('src0_sel', 'src1_sel', 'dst_sel', 'dst_unused') and value and key not in fields:
+                fields[key] = value
+            else:
+                raise Unsupported('%s SDWA with modifier %s' % (base, m))
+        if len(ops) != 3:
+            raise Unsupported('%s SDWA with %d operands, expected 3' % (base, len(ops)))
+        args = []
+        for index in range(2):
+            selection = fields.get('src%d_sel' % index)
+            if selection not in ('DWORD', 'WORD_0', 'WORD_1'):
+                raise Unsupported('%s SDWA src%d_sel %s' % (base, index, selection))
+            neg, token = self.split_neg(ops[1 + index])
+            absolute = False
+            while True:
+                if token.startswith('|') and token.endswith('|') and len(token) > 2:
+                    token = token[1:-1]
+                elif token.startswith('abs(') and token.endswith(')'):
+                    token = token[len('abs('):-1]
+                else:
+                    break
+                absolute = True
+            reg = reg_range(token)
+            if not reg or reg[0] != 'v' or reg[1] != reg[2]:
+                raise Unsupported('%s SDWA source %s: expected a single VGPR' % (base, ops[1 + index]))
+            word = self.u(token, low16_only=selection != 'WORD_1')
+            half = '(%s >> 16u)' % word if selection == 'WORD_1' else '(%s & 0xffffu)' % word
+            value = 'f16tof32(%s)' % half
+            if absolute:
+                value = 'abs(%s)' % value
+            args.append('(-%s)' % value if neg else value)
+        select = fields.get('dst_sel')
+        unused = fields.get('dst_unused', 'UNUSED_PAD')
+        if select not in ('WORD_0', 'WORD_1'):
+            raise Unsupported('%s SDWA dst_sel %s' % (base, select))
+        if unused not in ('UNUSED_PAD', 'UNUSED_PRESERVE'):
+            raise Unsupported('%s SDWA dst_unused %s' % (base, unused))
+        destination = reg_range(ops[0])[1]
+        result = '(f16rn(%s))' % text.format(*args)
+        # Keeping the old high half of a register whose high half is undefined leaves it undefined.
+        keep_low16 = select == 'WORD_0' and unused == 'UNUSED_PRESERVE' and destination in self.low16_only_v
+        self.set_v(destination, sdwa_dest(result, select, unused, 'v%d' % destination), low16_only=keep_low16)
+
     def sdwa(self, base, ops, mods):
+        if base in ('v_add_f16', 'v_sub_f16', 'v_subrev_f16', 'v_mul_f16'):
+            return self.sdwa_half(base, ops, mods)

P1 Badge Document the newly supported binary16 SDWA capability

AGENTS.md requires a README or docs/experiments.md update whenever a public capability changes, but this exposes four previously refused instructions without changing either document. For these f16 arithmetic forms, the retained statement in docs/startup-runtime.md:2065-2067 that floating-point SDWA stops translation is now incorrect. Document the accepted selectors, PAD/PRESERVE behavior, remaining refusals, and reproduction commands, and correct the blanket refusal statement.

AGENTS.md reference: AGENTS.md:L317-L318

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4704213480 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#discussion_r4704213480). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-26T05:22:55Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 4141 ~~~~diff @@ -4081,7 +4081,64 @@ def half_alu(self, base, ops, mods): # v_mov_b32 is the same path with the selected source as its result, v_cndmask_b32 selects with VCC, # VEC_U_TO_F conversions apply output modifiers before dst_sel, VEC_F forms stay refused, and # dst_unused UNUSED_SEXT has no lowering. + def sdwa_half(self, base, ops, mods): + """SDWA binary16 add, sub, subrev and mul: a selected half of each source, one rounding, one half written back. + + Sources: src0_sel/src1_sel WORD_0 or WORD_1 pick the half; DWORD reads the low half, as the 16-bit + operation does. Destination: dst_sel WORD_0 or WORD_1 with dst_unused UNUSED_PAD (other half zero) or + UNUSED_PRESERVE (other half of the old register kept). dst_sel DWORD stays refused: the upper half of a + 16-bit result is not claimed (an open question, see the low-16-only contract), and a byte selection, sext(), + clamp and the output modifiers have no lowering here. Sources are VGPRs with the abs/neg modifiers. + """ + _arity, _kind, text, _result = VEC_H[base] + fields = {} + for m in mods: + key, _, value = m.partition(':') + if key in ('src0_sel', 'src1_sel', 'dst_sel', 'dst_unused') and value and key not in fields: + fields[key] = value + else: + raise Unsupported('%s SDWA with modifier %s' % (base, m)) + if len(ops) != 3: + raise Unsupported('%s SDWA with %d operands, expected 3' % (base, len(ops))) + args = [] + for index in range(2): + selection = fields.get('src%d_sel' % index) + if selection not in ('DWORD', 'WORD_0', 'WORD_1'): + raise Unsupported('%s SDWA src%d_sel %s' % (base, index, selection)) + neg, token = self.split_neg(ops[1 + index]) + absolute = False + while True: + if token.startswith('|') and token.endswith('|') and len(token) > 2: + token = token[1:-1] + elif token.startswith('abs(') and token.endswith(')'): + token = token[len('abs('):-1] + else: + break + absolute = True + reg = reg_range(token) + if not reg or reg[0] != 'v' or reg[1] != reg[2]: + raise Unsupported('%s SDWA source %s: expected a single VGPR' % (base, ops[1 + index])) + word = self.u(token, low16_only=selection != 'WORD_1') + half = '(%s >> 16u)' % word if selection == 'WORD_1' else '(%s & 0xffffu)' % word + value = 'f16tof32(%s)' % half + if absolute: + value = 'abs(%s)' % value + args.append('(-%s)' % value if neg else value) + select = fields.get('dst_sel') + unused = fields.get('dst_unused', 'UNUSED_PAD') + if select not in ('WORD_0', 'WORD_1'): + raise Unsupported('%s SDWA dst_sel %s' % (base, select)) + if unused not in ('UNUSED_PAD', 'UNUSED_PRESERVE'): + raise Unsupported('%s SDWA dst_unused %s' % (base, unused)) + destination = reg_range(ops[0])[1] + result = '(f16rn(%s))' % text.format(*args) + # Keeping the old high half of a register whose high half is undefined leaves it undefined. + keep_low16 = select == 'WORD_0' and unused == 'UNUSED_PRESERVE' and destination in self.low16_only_v + self.set_v(destination, sdwa_dest(result, select, unused, 'v%d' % destination), low16_only=keep_low16) + def sdwa(self, base, ops, mods): + if base in ('v_add_f16', 'v_sub_f16', 'v_subrev_f16', 'v_mul_f16'): + return self.sdwa_half(base, ops, mods) ~~~~ --- **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Document the newly supported binary16 SDWA capability** `AGENTS.md` requires a README or `docs/experiments.md` update whenever a public capability changes, but this exposes four previously refused instructions without changing either document. For these f16 arithmetic forms, the retained statement in `docs/startup-runtime.md:2065-2067` that floating-point SDWA stops translation is now incorrect. Document the accepted selectors, PAD/PRESERVE behavior, remaining refusals, and reproduction commands, and correct the blanket refusal statement. AGENTS.md reference: [AGENTS.md:L317-L318](https://github.com/R0Y00/Xbox2Windows/blob/ef44ab4b01e49e9358632f8587531adfe53320f2/AGENTS.md#L317-L318) Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
tedin7 commented 2026-11-26 06:02:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:31:24Z.

Code review location: tools/startup/gfx2hlsl.py; line 4141

@@ -4081,7 +4081,64 @@ def half_alu(self, base, ops, mods):
     # v_mov_b32 is the same path with the selected source as its result, v_cndmask_b32 selects with VCC,
     # VEC_U_TO_F conversions apply output modifiers before dst_sel, VEC_F forms stay refused, and
     # dst_unused UNUSED_SEXT has no lowering.
+    def sdwa_half(self, base, ops, mods):
+        """SDWA binary16 add, sub, subrev and mul: a selected half of each source, one rounding, one half written back.
+
+        Sources: src0_sel/src1_sel WORD_0 or WORD_1 pick the half; DWORD reads the low half, as the 16-bit
+        operation does. Destination: dst_sel WORD_0 or WORD_1 with dst_unused UNUSED_PAD (other half zero) or
+        UNUSED_PRESERVE (other half of the old register kept). dst_sel DWORD stays refused: the upper half of a
+        16-bit result is not claimed (an open question, see the low-16-only contract), and a byte selection, sext(),
+        clamp and the output modifiers have no lowering here. Sources are VGPRs with the abs/neg modifiers.
+        """
+        _arity, _kind, text, _result = VEC_H[base]
+        fields = {}
+        for m in mods:
+            key, _, value = m.partition(':')
+            if key in ('src0_sel', 'src1_sel', 'dst_sel', 'dst_unused') and value and key not in fields:
+                fields[key] = value
+            else:
+                raise Unsupported('%s SDWA with modifier %s' % (base, m))
+        if len(ops) != 3:
+            raise Unsupported('%s SDWA with %d operands, expected 3' % (base, len(ops)))
+        args = []
+        for index in range(2):
+            selection = fields.get('src%d_sel' % index)
+            if selection not in ('DWORD', 'WORD_0', 'WORD_1'):
+                raise Unsupported('%s SDWA src%d_sel %s' % (base, index, selection))
+            neg, token = self.split_neg(ops[1 + index])
+            absolute = False
+            while True:
+                if token.startswith('|') and token.endswith('|') and len(token) > 2:
+                    token = token[1:-1]
+                elif token.startswith('abs(') and token.endswith(')'):
+                    token = token[len('abs('):-1]
+                else:
+                    break
+                absolute = True
+            reg = reg_range(token)
+            if not reg or reg[0] != 'v' or reg[1] != reg[2]:
+                raise Unsupported('%s SDWA source %s: expected a single VGPR' % (base, ops[1 + index]))
+            word = self.u(token, low16_only=selection != 'WORD_1')
+            half = '(%s >> 16u)' % word if selection == 'WORD_1' else '(%s & 0xffffu)' % word
+            value = 'f16tof32(%s)' % half
+            if absolute:
+                value = 'abs(%s)' % value
+            args.append('(-%s)' % value if neg else value)
+        select = fields.get('dst_sel')
+        unused = fields.get('dst_unused', 'UNUSED_PAD')
+        if select not in ('WORD_0', 'WORD_1'):
+            raise Unsupported('%s SDWA dst_sel %s' % (base, select))
+        if unused not in ('UNUSED_PAD', 'UNUSED_PRESERVE'):
+            raise Unsupported('%s SDWA dst_unused %s' % (base, unused))
+        destination = reg_range(ops[0])[1]
+        result = '(f16rn(%s))' % text.format(*args)
+        # Keeping the old high half of a register whose high half is undefined leaves it undefined.
+        keep_low16 = select == 'WORD_0' and unused == 'UNUSED_PRESERVE' and destination in self.low16_only_v
+        self.set_v(destination, sdwa_dest(result, select, unused, 'v%d' % destination), low16_only=keep_low16)
+
     def sdwa(self, base, ops, mods):
+        if base in ('v_add_f16', 'v_sub_f16', 'v_subrev_f16', 'v_mul_f16'):
+            return self.sdwa_half(base, ops, mods)

Fixed in f3764680b (docs/startup-runtime.md): the SDWA section no longer says all float SDWA stops translation. It now documents the binary16 add, sub, subrev and mul forms: accepted src0_sel/src1_sel (WORD_0, WORD_1, DWORD), dst_sel WORD_0/WORD_1 with UNUSED_PAD (default) or UNUSED_PRESERVE, the remaining refusals (dst_sel DWORD, byte selects, sext, clamp, output modifiers, UNUSED_SEXT, non-VGPR sources), and the ctest reproduction command.

Verification:

  • python3 -B tests/docs_links_check.py: passed (Linux).
  • python3 -B tests/startup_gfx2hlsl_f16_alu_check.py: f16 alu ok (Linux).
  • Win2025 WARP at f3764680b (x2w_pipe.py win2025 2739 --regex startup_gfx2hlsl_f16_alu): dev 4/4 passed, release 4/4 passed (includes the WARP/hardware readback draws).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4704263290 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#discussion_r4704263290). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T05:31:24Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 4141 ~~~~diff @@ -4081,7 +4081,64 @@ def half_alu(self, base, ops, mods): # v_mov_b32 is the same path with the selected source as its result, v_cndmask_b32 selects with VCC, # VEC_U_TO_F conversions apply output modifiers before dst_sel, VEC_F forms stay refused, and # dst_unused UNUSED_SEXT has no lowering. + def sdwa_half(self, base, ops, mods): + """SDWA binary16 add, sub, subrev and mul: a selected half of each source, one rounding, one half written back. + + Sources: src0_sel/src1_sel WORD_0 or WORD_1 pick the half; DWORD reads the low half, as the 16-bit + operation does. Destination: dst_sel WORD_0 or WORD_1 with dst_unused UNUSED_PAD (other half zero) or + UNUSED_PRESERVE (other half of the old register kept). dst_sel DWORD stays refused: the upper half of a + 16-bit result is not claimed (an open question, see the low-16-only contract), and a byte selection, sext(), + clamp and the output modifiers have no lowering here. Sources are VGPRs with the abs/neg modifiers. + """ + _arity, _kind, text, _result = VEC_H[base] + fields = {} + for m in mods: + key, _, value = m.partition(':') + if key in ('src0_sel', 'src1_sel', 'dst_sel', 'dst_unused') and value and key not in fields: + fields[key] = value + else: + raise Unsupported('%s SDWA with modifier %s' % (base, m)) + if len(ops) != 3: + raise Unsupported('%s SDWA with %d operands, expected 3' % (base, len(ops))) + args = [] + for index in range(2): + selection = fields.get('src%d_sel' % index) + if selection not in ('DWORD', 'WORD_0', 'WORD_1'): + raise Unsupported('%s SDWA src%d_sel %s' % (base, index, selection)) + neg, token = self.split_neg(ops[1 + index]) + absolute = False + while True: + if token.startswith('|') and token.endswith('|') and len(token) > 2: + token = token[1:-1] + elif token.startswith('abs(') and token.endswith(')'): + token = token[len('abs('):-1] + else: + break + absolute = True + reg = reg_range(token) + if not reg or reg[0] != 'v' or reg[1] != reg[2]: + raise Unsupported('%s SDWA source %s: expected a single VGPR' % (base, ops[1 + index])) + word = self.u(token, low16_only=selection != 'WORD_1') + half = '(%s >> 16u)' % word if selection == 'WORD_1' else '(%s & 0xffffu)' % word + value = 'f16tof32(%s)' % half + if absolute: + value = 'abs(%s)' % value + args.append('(-%s)' % value if neg else value) + select = fields.get('dst_sel') + unused = fields.get('dst_unused', 'UNUSED_PAD') + if select not in ('WORD_0', 'WORD_1'): + raise Unsupported('%s SDWA dst_sel %s' % (base, select)) + if unused not in ('UNUSED_PAD', 'UNUSED_PRESERVE'): + raise Unsupported('%s SDWA dst_unused %s' % (base, unused)) + destination = reg_range(ops[0])[1] + result = '(f16rn(%s))' % text.format(*args) + # Keeping the old high half of a register whose high half is undefined leaves it undefined. + keep_low16 = select == 'WORD_0' and unused == 'UNUSED_PRESERVE' and destination in self.low16_only_v + self.set_v(destination, sdwa_dest(result, select, unused, 'v%d' % destination), low16_only=keep_low16) + def sdwa(self, base, ops, mods): + if base in ('v_add_f16', 'v_sub_f16', 'v_subrev_f16', 'v_mul_f16'): + return self.sdwa_half(base, ops, mods) ~~~~ --- Fixed in f3764680b (docs/startup-runtime.md): the SDWA section no longer says all float SDWA stops translation. It now documents the binary16 add, sub, subrev and mul forms: accepted src0_sel/src1_sel (WORD_0, WORD_1, DWORD), dst_sel WORD_0/WORD_1 with UNUSED_PAD (default) or UNUSED_PRESERVE, the remaining refusals (dst_sel DWORD, byte selects, sext, clamp, output modifiers, UNUSED_SEXT, non-VGPR sources), and the ctest reproduction command. Verification: - `python3 -B tests/docs_links_check.py`: passed (Linux). - `python3 -B tests/startup_gfx2hlsl_f16_alu_check.py`: `f16 alu ok` (Linux). - Win2025 WARP at f3764680b (`x2w_pipe.py win2025 2739 --regex startup_gfx2hlsl_f16_alu`): dev 4/4 passed, release 4/4 passed (includes the WARP/hardware readback draws). AI-assisted; commands listed were executed locally.
MasterDomino commented 2026-11-26 07:00:28 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-26T06:50:15Z.


Post-merge check on an AMD Radeon RX 9070 XT (driver 32.0.32029.2010) and WARP 10.0.26100.9818, Windows 11 build 26300.

What passes: startup_gfx2hlsl_f16_alu, _draw_controls and _draw_warp.

What fails: startup_gfx2hlsl_f16_alu_draw_hardware fails on one SDWA case, with both FXC and DXC.

  • The case: v_mul_f16_word_1_word_0_word_1_preserve_plain_plain_edge, set 0, lane 2, which multiplies -0 by +0.
  • The result: expected e000abcd, read back 6000abcd, so the product loses its sign of zero.
  • Context: it's the same signed-zero class as #2746, and the same run shows it in seven v_pk_mul_f16 cases from #2738.

Everything else checks out: source selection, placement, operand order (including v_subrev) and refusals. Six one-line mutations each fail startup_gfx2hlsl_f16_alu_check.py: source half swapped, PRESERVE treated as PAD, abs dropped, operands swapped, dst_sel:DWORD accepted, and the low-16 tag dropped. No cache bump was needed, since these forms were refused before.

Could the -0 product case be folded into the #2746 fix (for example a precise multiply), so that the AMD hardware variant goes green?

Commands: ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_f16_alu' --output-on-failure; python -B tests/startup_gfx2hlsl_f16_alu_check.py.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7184831640 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2739#issuecomment-7184831640). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-26T06:50:15Z. --- Post-merge check on an AMD Radeon RX 9070 XT (driver 32.0.32029.2010) and WARP 10.0.26100.9818, Windows 11 build 26300. **What passes:** `startup_gfx2hlsl_f16_alu`, `_draw_controls` and `_draw_warp`. **What fails:** `startup_gfx2hlsl_f16_alu_draw_hardware` fails on one SDWA case, with both FXC and DXC. - **The case:** `v_mul_f16_word_1_word_0_word_1_preserve_plain_plain_edge`, set 0, lane 2, which multiplies -0 by +0. - **The result:** expected `e000abcd`, read back `6000abcd`, so the product loses its sign of zero. - **Context:** it's the same signed-zero class as #2746, and the same run shows it in seven `v_pk_mul_f16` cases from #2738. **Everything else checks out:** source selection, placement, operand order (including `v_subrev`) and refusals. Six one-line mutations each fail `startup_gfx2hlsl_f16_alu_check.py`: source half swapped, PRESERVE treated as PAD, abs dropped, operands swapped, `dst_sel:DWORD` accepted, and the low-16 tag dropped. No cache bump was needed, since these forms were refused before. Could the -0 product case be folded into the #2746 fix (for example a `precise` multiply), so that the AMD hardware variant goes green? Commands: `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_f16_alu' --output-on-failure`; `python -B tests/startup_gfx2hlsl_f16_alu_check.py`. AI-assisted; commands listed were executed locally.
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!2731
No description provided.