shader: lower constant-lane SGPR spills in gfx2hlsl pixel stages (Part of #2125) #2163

Merged
tedin7 merged 3 commits from upstream-pr/2171 into main 2026-11-25 07:17:44 +00:00
tedin7 commented 2026-11-25 02:39:49 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:39:49Z.


Part of #2125

What this unblocks: pixel programs that park SGPRs in VGPR lanes (v_writelane_b32 vN, sM, k ... v_readlane_b32 sD, vN, k, constant k) now translate (item E).

What changed

  • tools/startup/gfx2hlsl.py: one scalar slot per (VGPR, lane). Both instructions ignore EXEC and touch only lane k, so with a constant k and a VGPR no other instruction names, no cross-lane read needs modelling. A constant value reloads as a constant.
  • Refused with a reason: lane from an SGPR, lane > 31, VGPR also used as a vector or in a register range, read of an unwritten slot, branches, lane mask / m0 / modifier on the value, non-SGPR destination, vertex stage.
  • tests/startup_gfx2hlsl_sgpr_spill_check.py: Python controls, binary assembler check, and a 16 x 16 D3D12 draw compared with an integer reference; four wrong lowerings must read a different image.
  • CMake tests startup_gfx2hlsl_sgpr_spill_{controls,warp,hardware}, CI list, AGENTS.md, docs/experiments.md.

Semantics: AMD RDNA 2 ISA reference (VOP3 lane operations); LLVM 15 AMDGPU.

Verification:

  • Linux: python3 -I -B tests/startup_gfx2hlsl_sgpr_spill_check.py -> PASS; ... binary llvm-mc -> PASS (LLVM 19 locally)
  • Linux: docs_links_check, startup_translator_check, readfirstlane_uniform, dpp_quad_swap, swap, refusals, handler_arity -> pass
  • Win2025 (Build 26100, MSVC 14.44, D3D12 WARP) at 2a111e5e: ctest --preset dev and ctest --preset release with -R 'startup_gfx2hlsl_sgpr_spill_|startup_gfx2hlsl_dpp_quad_swap_|startup_gfx2hlsl_swap' --parallel 3 --no-tests=error -> 7/7 passed on each, zero skips

Note: #2032 also adds an op_v_readlane_b32 (uniform-source form); whichever lands second needs a small merge.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6595935575 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2171). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T02:39:49Z. --- Part of #2125 What this unblocks: pixel programs that park SGPRs in VGPR lanes (`v_writelane_b32 vN, sM, k` ... `v_readlane_b32 sD, vN, k`, constant k) now translate (item E). ## What changed - `tools/startup/gfx2hlsl.py`: one scalar slot per (VGPR, lane). Both instructions ignore EXEC and touch only lane k, so with a constant k and a VGPR no other instruction names, no cross-lane read needs modelling. A constant value reloads as a constant. - Refused with a reason: lane from an SGPR, lane > 31, VGPR also used as a vector or in a register range, read of an unwritten slot, branches, lane mask / m0 / modifier on the value, non-SGPR destination, vertex stage. - `tests/startup_gfx2hlsl_sgpr_spill_check.py`: Python controls, `binary` assembler check, and a 16 x 16 D3D12 draw compared with an integer reference; four wrong lowerings must read a different image. - CMake tests `startup_gfx2hlsl_sgpr_spill_{controls,warp,hardware}`, CI list, AGENTS.md, docs/experiments.md. Semantics: AMD RDNA 2 ISA reference (VOP3 lane operations); LLVM 15 AMDGPU. Verification: - Linux: `python3 -I -B tests/startup_gfx2hlsl_sgpr_spill_check.py` -> PASS; `... binary llvm-mc` -> PASS (LLVM 19 locally) - Linux: docs_links_check, startup_translator_check, readfirstlane_uniform, dpp_quad_swap, swap, refusals, handler_arity -> pass - Win2025 (Build 26100, MSVC 14.44, D3D12 WARP) at 2a111e5e: `ctest --preset dev` and `ctest --preset release` with `-R 'startup_gfx2hlsl_sgpr_spill_|startup_gfx2hlsl_dpp_quad_swap_|startup_gfx2hlsl_swap' --parallel 3 --no-tests=error` -> 7/7 passed on each, zero skips Note: #2032 also adds an `op_v_readlane_b32` (uniform-source form); whichever lands second needs a small merge. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 03:01:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:59:46Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 2a111e5e6d518302dc6ace1ed55b2b4beee8dfb8:

  • Build: cmake --build --preset dev --target offscreen_check -- -j 4
  • Test: ctest --preset dev -R '^startup_gfx2hlsl_sgpr_spill_(controls|warp)$' --parallel 3 --no-tests=error --output-on-failure -> 2/2 passed (DXC and FXC ps_5_1 draws, 256 pixels each, all four wrong-lowering controls fail as required)
  • tests/startup_gfx2hlsl_sgpr_spill_check.py -> ok

The first Win2025 run (31df9f30) caught one control that did not bite: a constant slot folds into the export, so the lane mutation had no effect. Fixed in 2a111e5e by mutating the folded word.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7151479442 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2171#issuecomment-7151479442). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T02:59:46Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `2a111e5e6d518302dc6ace1ed55b2b4beee8dfb8`: - Build: `cmake --build --preset dev --target offscreen_check -- -j 4` - Test: `ctest --preset dev -R '^startup_gfx2hlsl_sgpr_spill_(controls|warp)$' --parallel 3 --no-tests=error --output-on-failure` -> 2/2 passed (DXC and FXC ps_5_1 draws, 256 pixels each, all four wrong-lowering controls fail as required) - `tests/startup_gfx2hlsl_sgpr_spill_check.py` -> ok The first Win2025 run (31df9f30) caught one control that did not bite: a constant slot folds into the export, so the lane mutation had no effect. Fixed in 2a111e5e by mutating the folded word. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-25 04:04:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T03:17:10Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a111e5e6d

ℹ️ 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:6051341919 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2171#pullrequestreview-6051341919). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T03:17:10Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `2a111e5e6d` <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-25 04:04:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T03:32:07Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6051453987 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2171#pullrequestreview-6051453987). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T03:32:07Z. **Review:** COMMENTED --- _No description provided._
chatgpt-codex-connector[bot commented 2026-11-25 04:04:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T03:12:08Z.


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-25T03:17:13.631750Z 2a111e5 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:7151688864 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2171#issuecomment-7151688864). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T03:12:08Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"2a111e5e6d518302dc6ace1ed55b2b4beee8dfb8","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-25T03:17:13.631750Z">2026-11-25T03:17:13.631750Z</relative-time> | `2a111e5` | 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-25 04:04:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T03:17:11Z.

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

@@ -1736,6 +1736,75 @@ def op_v_readfirstlane_b32(self, ops, mods):
             raise Unsupported('v_readfirstlane_b32 requires a proven uniform source')
         self.set_s(reg_range(ops[0])[1], self.u(ops[1]))
 
+    # ---- SGPR spill to VGPR lanes (#2125, item E) --------------------------------------------
+    # The compiler parks a scalar in one lane of a VGPR: `v_writelane_b32 vN, sM, k` writes sM into lane k
+    # and `v_readlane_b32 sD, vN, k` reads it back (AMD RDNA 2 ISA reference, VOP3 lane ops; LLVM 15
+    # AMDGPU spill lowering). Both ignore EXEC. With a constant k and a VGPR that no other instruction
+    # touches, vN is only a table of scalar slots, so one scalar variable per (vN, k) is an exact model
+    # and no other lane is ever read. Anything else keeps the one-lane model out of reach and is refused.
+    SPILL_LANES = 32
+
+    def spill_operands(self, name, ops, mods):
+        if mods or len(ops) != 3:
+            raise Unsupported('%s with modifiers or %d operands' % (name, len(ops)))
+        vreg, sreg = ((reg_range(ops[0]), reg_range(ops[1])) if name == 'v_writelane_b32'
+                      else (reg_range(ops[1]), reg_range(ops[0])))
+        if (not vreg or vreg[0] != 'v' or vreg[1] != vreg[2] or vreg[1] > 255 or
+                (sreg is not None and (sreg[0] != 's' or sreg[1] != sreg[2] or sreg[1] > 103))):
+            raise Unsupported('%s requires one VGPR and one plain SGPR' % name)
+        try:
+            lane = int(ops[2], 0)
+        except ValueError:
+            raise Unsupported('%s requires a constant lane, got %s' % (name, ops[2])) from None
+        if not 0 <= lane < self.SPILL_LANES:
+            raise Unsupported('%s lane %d outside 0..%d' % (name, lane, self.SPILL_LANES - 1))
+        if getattr(self, 'kind', None) != 'ps':
+            raise Unsupported('%s is lowered in pixel stages only' % name)
+        if getattr(self, 'has_branches', True) or not self.tracks_uniformity():
+            raise Unsupported('%s needs straight-line code to prove write-before-read' % name)
+        self.require_spill_only(vreg[1], name)
+        return vreg[1], sreg, lane
+
+    def require_spill_only(self, n, name):
+        """Refuse unless every instruction naming v<n> is a lane write or read of it."""
+        for _, _, op, asm in getattr(self, 'ins', ()):
+            if re.sub(r'_e(32|64)$', '', op) in ('v_writelane_b32', 'v_readlane_b32'):
+                continue
+            for lo, hi, single in re.findall(r'\bv\[(\d+):(\d+)\]|\bv(\d+)\b', asm):
+                a, b = (int(lo), int(hi)) if lo else (int(single), int(single))
+                if a <= n <= b:
+                    raise Unsupported('%s: v%d is also used as a vector (%s)' % (name, n, op))
+
+    def op_v_writelane_b32(self, ops, mods):
+        n, src, lane = self.spill_operands('v_writelane_b32', ops, mods)
+        if src is None:
+            if ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'):
+                raise Unsupported('v_writelane_b32 source %s is a lane mask or m0' % ops[1])
+            value, sym, uniform = self.u(ops[1]), None, True
+            if re.fullmatch(r'0x[0-9a-f]{8}u', value):
+                sym = ('const', int(value[2:-1], 16))
+        else:
+            if self.is_lane_mask(ops[1]) or (self.sym.get(src[1]) or ('',))[0] not in ('', 'const', 'ud'):
+                raise Unsupported('v_writelane_b32 source s%d holds a lane mask or descriptor' % src[1])

P2 Badge Accept numeric fixed-buffer words as spill sources

When an SGPR comes directly from s_buffer_load_dword* with an immediate offset, the existing load handler tags it as ('fixed_buffer_word',). This whitelist rejects that ordinary numeric value as a lane mask or descriptor, even though S() and uniform_source() already support it. Consequently, a load/spill/reload sequence satisfying the straight-line pixel-stage restrictions still fails translation. I reproduced this with assembled gfx1013 code; replacing the immediate zero offset with an SGPR containing zero, or inserting s_or_b32 s0, s0, 0 after the load, makes it translate. Include fixed_buffer_word among the numeric spill-source tags and add a memory-loaded spill control.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4689945211 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2171#discussion_r4689945211). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T03:17:11Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1788 ~~~~diff @@ -1736,6 +1736,75 @@ def op_v_readfirstlane_b32(self, ops, mods): raise Unsupported('v_readfirstlane_b32 requires a proven uniform source') self.set_s(reg_range(ops[0])[1], self.u(ops[1])) + # ---- SGPR spill to VGPR lanes (#2125, item E) -------------------------------------------- + # The compiler parks a scalar in one lane of a VGPR: `v_writelane_b32 vN, sM, k` writes sM into lane k + # and `v_readlane_b32 sD, vN, k` reads it back (AMD RDNA 2 ISA reference, VOP3 lane ops; LLVM 15 + # AMDGPU spill lowering). Both ignore EXEC. With a constant k and a VGPR that no other instruction + # touches, vN is only a table of scalar slots, so one scalar variable per (vN, k) is an exact model + # and no other lane is ever read. Anything else keeps the one-lane model out of reach and is refused. + SPILL_LANES = 32 + + def spill_operands(self, name, ops, mods): + if mods or len(ops) != 3: + raise Unsupported('%s with modifiers or %d operands' % (name, len(ops))) + vreg, sreg = ((reg_range(ops[0]), reg_range(ops[1])) if name == 'v_writelane_b32' + else (reg_range(ops[1]), reg_range(ops[0]))) + if (not vreg or vreg[0] != 'v' or vreg[1] != vreg[2] or vreg[1] > 255 or + (sreg is not None and (sreg[0] != 's' or sreg[1] != sreg[2] or sreg[1] > 103))): + raise Unsupported('%s requires one VGPR and one plain SGPR' % name) + try: + lane = int(ops[2], 0) + except ValueError: + raise Unsupported('%s requires a constant lane, got %s' % (name, ops[2])) from None + if not 0 <= lane < self.SPILL_LANES: + raise Unsupported('%s lane %d outside 0..%d' % (name, lane, self.SPILL_LANES - 1)) + if getattr(self, 'kind', None) != 'ps': + raise Unsupported('%s is lowered in pixel stages only' % name) + if getattr(self, 'has_branches', True) or not self.tracks_uniformity(): + raise Unsupported('%s needs straight-line code to prove write-before-read' % name) + self.require_spill_only(vreg[1], name) + return vreg[1], sreg, lane + + def require_spill_only(self, n, name): + """Refuse unless every instruction naming v<n> is a lane write or read of it.""" + for _, _, op, asm in getattr(self, 'ins', ()): + if re.sub(r'_e(32|64)$', '', op) in ('v_writelane_b32', 'v_readlane_b32'): + continue + for lo, hi, single in re.findall(r'\bv\[(\d+):(\d+)\]|\bv(\d+)\b', asm): + a, b = (int(lo), int(hi)) if lo else (int(single), int(single)) + if a <= n <= b: + raise Unsupported('%s: v%d is also used as a vector (%s)' % (name, n, op)) + + def op_v_writelane_b32(self, ops, mods): + n, src, lane = self.spill_operands('v_writelane_b32', ops, mods) + if src is None: + if ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'): + raise Unsupported('v_writelane_b32 source %s is a lane mask or m0' % ops[1]) + value, sym, uniform = self.u(ops[1]), None, True + if re.fullmatch(r'0x[0-9a-f]{8}u', value): + sym = ('const', int(value[2:-1], 16)) + else: + if self.is_lane_mask(ops[1]) or (self.sym.get(src[1]) or ('',))[0] not in ('', 'const', 'ud'): + raise Unsupported('v_writelane_b32 source s%d holds a lane mask or descriptor' % src[1]) ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Accept numeric fixed-buffer words as spill sources** When an SGPR comes directly from `s_buffer_load_dword*` with an immediate offset, the existing load handler tags it as `('fixed_buffer_word',)`. This whitelist rejects that ordinary numeric value as a lane mask or descriptor, even though `S()` and `uniform_source()` already support it. Consequently, a load/spill/reload sequence satisfying the straight-line pixel-stage restrictions still fails translation. I reproduced this with assembled gfx1013 code; replacing the immediate zero offset with an SGPR containing zero, or inserting `s_or_b32 s0, s0, 0` after the load, makes it translate. Include `fixed_buffer_word` among the numeric spill-source tags and add a memory-loaded spill control. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
tedin7 commented 2026-11-25 04:04:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T03:32:07Z.

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

@@ -1736,6 +1736,75 @@ def op_v_readfirstlane_b32(self, ops, mods):
             raise Unsupported('v_readfirstlane_b32 requires a proven uniform source')
         self.set_s(reg_range(ops[0])[1], self.u(ops[1]))
 
+    # ---- SGPR spill to VGPR lanes (#2125, item E) --------------------------------------------
+    # The compiler parks a scalar in one lane of a VGPR: `v_writelane_b32 vN, sM, k` writes sM into lane k
+    # and `v_readlane_b32 sD, vN, k` reads it back (AMD RDNA 2 ISA reference, VOP3 lane ops; LLVM 15
+    # AMDGPU spill lowering). Both ignore EXEC. With a constant k and a VGPR that no other instruction
+    # touches, vN is only a table of scalar slots, so one scalar variable per (vN, k) is an exact model
+    # and no other lane is ever read. Anything else keeps the one-lane model out of reach and is refused.
+    SPILL_LANES = 32
+
+    def spill_operands(self, name, ops, mods):
+        if mods or len(ops) != 3:
+            raise Unsupported('%s with modifiers or %d operands' % (name, len(ops)))
+        vreg, sreg = ((reg_range(ops[0]), reg_range(ops[1])) if name == 'v_writelane_b32'
+                      else (reg_range(ops[1]), reg_range(ops[0])))
+        if (not vreg or vreg[0] != 'v' or vreg[1] != vreg[2] or vreg[1] > 255 or
+                (sreg is not None and (sreg[0] != 's' or sreg[1] != sreg[2] or sreg[1] > 103))):
+            raise Unsupported('%s requires one VGPR and one plain SGPR' % name)
+        try:
+            lane = int(ops[2], 0)
+        except ValueError:
+            raise Unsupported('%s requires a constant lane, got %s' % (name, ops[2])) from None
+        if not 0 <= lane < self.SPILL_LANES:
+            raise Unsupported('%s lane %d outside 0..%d' % (name, lane, self.SPILL_LANES - 1))
+        if getattr(self, 'kind', None) != 'ps':
+            raise Unsupported('%s is lowered in pixel stages only' % name)
+        if getattr(self, 'has_branches', True) or not self.tracks_uniformity():
+            raise Unsupported('%s needs straight-line code to prove write-before-read' % name)
+        self.require_spill_only(vreg[1], name)
+        return vreg[1], sreg, lane
+
+    def require_spill_only(self, n, name):
+        """Refuse unless every instruction naming v<n> is a lane write or read of it."""
+        for _, _, op, asm in getattr(self, 'ins', ()):
+            if re.sub(r'_e(32|64)$', '', op) in ('v_writelane_b32', 'v_readlane_b32'):
+                continue
+            for lo, hi, single in re.findall(r'\bv\[(\d+):(\d+)\]|\bv(\d+)\b', asm):
+                a, b = (int(lo), int(hi)) if lo else (int(single), int(single))
+                if a <= n <= b:
+                    raise Unsupported('%s: v%d is also used as a vector (%s)' % (name, n, op))
+
+    def op_v_writelane_b32(self, ops, mods):
+        n, src, lane = self.spill_operands('v_writelane_b32', ops, mods)
+        if src is None:
+            if ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'):
+                raise Unsupported('v_writelane_b32 source %s is a lane mask or m0' % ops[1])
+            value, sym, uniform = self.u(ops[1]), None, True
+            if re.fullmatch(r'0x[0-9a-f]{8}u', value):
+                sym = ('const', int(value[2:-1], 16))
+        else:
+            if self.is_lane_mask(ops[1]) or (self.sym.get(src[1]) or ('',))[0] not in ('', 'const', 'ud'):
+                raise Unsupported('v_writelane_b32 source s%d holds a lane mask or descriptor' % src[1])

Fixed in 59f23fb5: fixed_buffer_word is now accepted as a numeric spill source in op_v_writelane_b32, and startup_gfx2hlsl_sgpr_spill_check.py has a control for a buffer word loaded at an immediate offset (fails before, passes after).

Verification:

  • Linux: python3 -B tests/startup_gfx2hlsl_sgpr_spill_check.py PASS (control FAILs with the old whitelist); docs_links_check, startup_gfx2hlsl_refusals_check, startup_gfx2hlsl_handler_arity_check, startup_gfx2hlsl_smem_offset_check, startup_gfx2hlsl_scalar_m0_check, mesh_lane_spill_check, startup_gfx2hlsl_readfirstlane_uniform_check all PASS.
  • Win2025 WARP, dev and release: startup_gfx2hlsl_sgpr_spill_(controls|warp) 2/2 passed each.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4690042948 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2171#discussion_r4690042948). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T03:32:07Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1788 ~~~~diff @@ -1736,6 +1736,75 @@ def op_v_readfirstlane_b32(self, ops, mods): raise Unsupported('v_readfirstlane_b32 requires a proven uniform source') self.set_s(reg_range(ops[0])[1], self.u(ops[1])) + # ---- SGPR spill to VGPR lanes (#2125, item E) -------------------------------------------- + # The compiler parks a scalar in one lane of a VGPR: `v_writelane_b32 vN, sM, k` writes sM into lane k + # and `v_readlane_b32 sD, vN, k` reads it back (AMD RDNA 2 ISA reference, VOP3 lane ops; LLVM 15 + # AMDGPU spill lowering). Both ignore EXEC. With a constant k and a VGPR that no other instruction + # touches, vN is only a table of scalar slots, so one scalar variable per (vN, k) is an exact model + # and no other lane is ever read. Anything else keeps the one-lane model out of reach and is refused. + SPILL_LANES = 32 + + def spill_operands(self, name, ops, mods): + if mods or len(ops) != 3: + raise Unsupported('%s with modifiers or %d operands' % (name, len(ops))) + vreg, sreg = ((reg_range(ops[0]), reg_range(ops[1])) if name == 'v_writelane_b32' + else (reg_range(ops[1]), reg_range(ops[0]))) + if (not vreg or vreg[0] != 'v' or vreg[1] != vreg[2] or vreg[1] > 255 or + (sreg is not None and (sreg[0] != 's' or sreg[1] != sreg[2] or sreg[1] > 103))): + raise Unsupported('%s requires one VGPR and one plain SGPR' % name) + try: + lane = int(ops[2], 0) + except ValueError: + raise Unsupported('%s requires a constant lane, got %s' % (name, ops[2])) from None + if not 0 <= lane < self.SPILL_LANES: + raise Unsupported('%s lane %d outside 0..%d' % (name, lane, self.SPILL_LANES - 1)) + if getattr(self, 'kind', None) != 'ps': + raise Unsupported('%s is lowered in pixel stages only' % name) + if getattr(self, 'has_branches', True) or not self.tracks_uniformity(): + raise Unsupported('%s needs straight-line code to prove write-before-read' % name) + self.require_spill_only(vreg[1], name) + return vreg[1], sreg, lane + + def require_spill_only(self, n, name): + """Refuse unless every instruction naming v<n> is a lane write or read of it.""" + for _, _, op, asm in getattr(self, 'ins', ()): + if re.sub(r'_e(32|64)$', '', op) in ('v_writelane_b32', 'v_readlane_b32'): + continue + for lo, hi, single in re.findall(r'\bv\[(\d+):(\d+)\]|\bv(\d+)\b', asm): + a, b = (int(lo), int(hi)) if lo else (int(single), int(single)) + if a <= n <= b: + raise Unsupported('%s: v%d is also used as a vector (%s)' % (name, n, op)) + + def op_v_writelane_b32(self, ops, mods): + n, src, lane = self.spill_operands('v_writelane_b32', ops, mods) + if src is None: + if ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'): + raise Unsupported('v_writelane_b32 source %s is a lane mask or m0' % ops[1]) + value, sym, uniform = self.u(ops[1]), None, True + if re.fullmatch(r'0x[0-9a-f]{8}u', value): + sym = ('const', int(value[2:-1], 16)) + else: + if self.is_lane_mask(ops[1]) or (self.sym.get(src[1]) or ('',))[0] not in ('', 'const', 'ud'): + raise Unsupported('v_writelane_b32 source s%d holds a lane mask or descriptor' % src[1]) ~~~~ --- Fixed in 59f23fb5: `fixed_buffer_word` is now accepted as a numeric spill source in `op_v_writelane_b32`, and `startup_gfx2hlsl_sgpr_spill_check.py` has a control for a buffer word loaded at an immediate offset (fails before, passes after). Verification: - Linux: `python3 -B tests/startup_gfx2hlsl_sgpr_spill_check.py` PASS (control FAILs with the old whitelist); `docs_links_check`, `startup_gfx2hlsl_refusals_check`, `startup_gfx2hlsl_handler_arity_check`, `startup_gfx2hlsl_smem_offset_check`, `startup_gfx2hlsl_scalar_m0_check`, `mesh_lane_spill_check`, `startup_gfx2hlsl_readfirstlane_uniform_check` all PASS. - Win2025 WARP, dev and release: `startup_gfx2hlsl_sgpr_spill_(controls|warp)` 2/2 passed each. AI-assisted; commands listed were executed locally.
legacy-code-archaeologist commented 2026-11-25 06:01:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-25T05:43:14Z.


Ran head 59f23fb5 on three adapters. All pass. Windows 11 build 26100, debug layer unavailable, DXC and FXC from SDK 10.0.26100.0.

Test Adapter Driver Result
startup_gfx2hlsl_sgpr_spill_controls n/a n/a pass
startup_gfx2hlsl_sgpr_spill_warp Microsoft Basic Render Driver 10.0.26100.8972 pass
startup_gfx2hlsl_sgpr_spill_hardware (XBOX2WINDOWS_ADAPTER=Quadro) NVIDIA Quadro M1000M (Maxwell) 32.0.15.7371 pass
startup_gfx2hlsl_sgpr_spill_hardware (XBOX2WINDOWS_ADAPTER=Intel) Intel HD Graphics 530 (Gen9) 31.0.101.2111 pass

On each adapter all 33 checks printed ok, including hardware dxc: all 256 pixels equal the slot reference and the same line for fxc. The hardware draws do not print the adapter line; I selected the adapter with XBOX2WINDOWS_ADAPTER, the same way the other draws report it.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7154073915 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2171#issuecomment-7154073915). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-25T05:43:14Z. --- Ran head `59f23fb5` on three adapters. All pass. Windows 11 build 26100, debug layer unavailable, DXC and FXC from SDK 10.0.26100.0. | Test | Adapter | Driver | Result | |---|---|---|---| | `startup_gfx2hlsl_sgpr_spill_controls` | n/a | n/a | pass | | `startup_gfx2hlsl_sgpr_spill_warp` | Microsoft Basic Render Driver | 10.0.26100.8972 | pass | | `startup_gfx2hlsl_sgpr_spill_hardware` (`XBOX2WINDOWS_ADAPTER=Quadro`) | NVIDIA Quadro M1000M (Maxwell) | 32.0.15.7371 | pass | | `startup_gfx2hlsl_sgpr_spill_hardware` (`XBOX2WINDOWS_ADAPTER=Intel`) | Intel HD Graphics 530 (Gen9) | 31.0.101.2111 | pass | On each adapter all 33 checks printed ok, including `hardware dxc: all 256 pixels equal the slot reference` and the same line for fxc. The hardware draws do not print the adapter line; I selected the adapter with `XBOX2WINDOWS_ADAPTER`, the same way the other draws report it. _AI-assisted._
upstream-sync closed this pull request 2026-11-25 08:04:49 +00:00
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!2163
No description provided.