shader: lower 64-bit scalar shifts and s_bfe in gfx2hlsl (Part of #1723) #2081

Merged
tedin7 merged 3 commits from upstream-pr/2088 into main 2026-11-25 12:54:07 +00:00
tedin7 commented 2026-11-25 00:06:49 +00:00 (Migrated from github.com)

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


Part of #1723

What this unblocks: scalar 64-bit shifts and bitfield extracts (s_lshl_b64, s_lshr_b64, s_ashr_i64, s_bfe_i32, s_bfe_u64, s_bfe_i64) no longer stop gfx2hlsl translation of titles that build 64-bit addresses and packed fields in SGPRs.

Verification:
Run on Linux (Python only, no GPU, no dxc/fxc available there). Not run on Win2025.

  • python3 -I -B tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py -> startup scalar 64-bit shift and bitfield extract controls passed (3498 helper cases)
  • Same test copied onto a clean origin/main checkout -> fails (AssertionError: bfe32 helper missing).
  • python3 -B tests/startup_gfx2hlsl_*_check.py, tests/startup_translator_check.py, tests/startup_flat_param_check.py (no arguments) -> all pass, including shift64, scalar_m0, bit_alu, cvt_alu, mad.

What changed:

  • tools/startup/gfx2hlsl.py: six handlers sharing one scalar_wide_op. Constant operands fold to exact words; otherwise one block reads all sources before writing either destination register (an overlapping destination is safe). SCC = (result != 0). m0, vcc, odd register pairs, lane masks and vector sources are refused. New HLSL helpers bfe32 and bfe64 reuse shl64, shr64u, shr64i.
  • tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py (new, in CMakeLists.txt and the Windows Python check list): hand-written constant cases, the emitted helper text run on a grid against an independent bit-string reference (no 32-bit shift ever gets a count of 32 or more), overlap, uniformity and refusal checks.

Notes:

  • The instruction name printed by the disassembler is s_ashr_i64, so that is the handler name.
  • Width of 64 or more in s_bfe_u64/s_bfe_i64 is treated as the whole field (hardware behaviour for that corner is not confirmed here).
  • No draw/GPU test is included; the helper text was not compiled with dxc/fxc.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6594029435 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:06:49Z. --- Part of #1723 What this unblocks: scalar 64-bit shifts and bitfield extracts (s_lshl_b64, s_lshr_b64, s_ashr_i64, s_bfe_i32, s_bfe_u64, s_bfe_i64) no longer stop gfx2hlsl translation of titles that build 64-bit addresses and packed fields in SGPRs. Verification: Run on Linux (Python only, no GPU, no dxc/fxc available there). Not run on Win2025. - `python3 -I -B tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py` -> `startup scalar 64-bit shift and bitfield extract controls passed (3498 helper cases)` - Same test copied onto a clean `origin/main` checkout -> fails (`AssertionError: bfe32 helper missing`). - `python3 -B tests/startup_gfx2hlsl_*_check.py`, `tests/startup_translator_check.py`, `tests/startup_flat_param_check.py` (no arguments) -> all pass, including shift64, scalar_m0, bit_alu, cvt_alu, mad. What changed: - `tools/startup/gfx2hlsl.py`: six handlers sharing one `scalar_wide_op`. Constant operands fold to exact words; otherwise one block reads all sources before writing either destination register (an overlapping destination is safe). SCC = (result != 0). m0, vcc, odd register pairs, lane masks and vector sources are refused. New HLSL helpers `bfe32` and `bfe64` reuse `shl64`, `shr64u`, `shr64i`. - `tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py` (new, in CMakeLists.txt and the Windows Python check list): hand-written constant cases, the emitted helper text run on a grid against an independent bit-string reference (no 32-bit shift ever gets a count of 32 or more), overlap, uniformity and refusal checks. Notes: - The instruction name printed by the disassembler is `s_ashr_i64`, so that is the handler name. - Width of 64 or more in `s_bfe_u64`/`s_bfe_i64` is treated as the whole field (hardware behaviour for that corner is not confirmed here). - No draw/GPU test is included; the helper text was not compiled with dxc/fxc. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-25 01:01:35 +00:00 (Migrated from github.com)

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

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4c7111a02f

ℹ️ 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:6049542469 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#pullrequestreview-6049542469). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:13:29Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `4c7111a02f` <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>
chatgpt-codex-connector[bot commented 2026-11-25 01:02:03 +00:00 (Migrated from github.com)

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


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-25T00:13:32.410921Z 4c7111a PR opened
ℹ️ 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:7148157062 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#issuecomment-7148157062). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:06:56Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"4c7111a02fb315bc89f8c86ffb4d904a470c61de","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-25T00:13:32.410921Z">2026-11-25T00:13:32.410921Z</relative-time> | `4c7111a` | PR opened | <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 01:02:16 +00:00 (Migrated from github.com)

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

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

@@ -1916,6 +1998,23 @@ def handler(self, ops, mods):
     setattr(Stage, 'op_s_bitcmp%d_b32' % _bit, _make_bitcmp())
 
 
+def scalar_wide_fold(helper, x, amount, signed):
+    """Translation-time value of one SOP2 shift or bitfield extract (the helper name picks the operation)."""
+    m64 = 0xffffffffffffffff
+    if helper == 'shl64':
+        return (x << (amount & 63)) & m64
+    if helper == 'shr64u':
+        return x >> (amount & 63)
+    if helper == 'shr64i':
+        return ((x - (1 << 64) if x >> 63 else x) >> (amount & 63)) & m64
+    bits = 32 if helper == 'bfe32' else 64
+    field = (x >> (amount & (bits - 1))) & ((1 << ((amount >> 16) & 127)) - 1) & ((1 << bits) - 1)
+    wd = (amount >> 16) & 127
+    if signed and 0 < wd < bits and field >> (wd - 1) & 1:
+        field |= ((1 << bits) - 1) ^ ((1 << wd) - 1)

P2 Badge Sign-extend signed extractions beyond the source boundary

When a signed field extends past bit 31/63 and the source's most-significant bit is set, the result must retain that sign. This logic instead zero-fills the missing bits before testing bit width - 1. For example, s_bfe_i32 of 0x80000000 with control 0x0002001f must produce 0xffffffff, but both scalar_wide_fold and the emitted bfe32 helper produce 1; the analogous 64-bit case also fails. Clamp the effective width to the remaining source bits or use an arithmetic shift for this case, and correct the test reference, which currently zero-pads the same way.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688401339 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688401339). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:13:29Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 2014 ~~~~diff @@ -1916,6 +1998,23 @@ def handler(self, ops, mods): setattr(Stage, 'op_s_bitcmp%d_b32' % _bit, _make_bitcmp()) +def scalar_wide_fold(helper, x, amount, signed): + """Translation-time value of one SOP2 shift or bitfield extract (the helper name picks the operation).""" + m64 = 0xffffffffffffffff + if helper == 'shl64': + return (x << (amount & 63)) & m64 + if helper == 'shr64u': + return x >> (amount & 63) + if helper == 'shr64i': + return ((x - (1 << 64) if x >> 63 else x) >> (amount & 63)) & m64 + bits = 32 if helper == 'bfe32' else 64 + field = (x >> (amount & (bits - 1))) & ((1 << ((amount >> 16) & 127)) - 1) & ((1 << bits) - 1) + wd = (amount >> 16) & 127 + if signed and 0 < wd < bits and field >> (wd - 1) & 1: + field |= ((1 << bits) - 1) ^ ((1 << wd) - 1) ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Sign-extend signed extractions beyond the source boundary** When a signed field extends past bit 31/63 and the source's most-significant bit is set, the result must retain that sign. This logic instead zero-fills the missing bits before testing bit `width - 1`. For example, `s_bfe_i32` of `0x80000000` with control `0x0002001f` must produce `0xffffffff`, but both `scalar_wide_fold` and the emitted `bfe32` helper produce `1`; the analogous 64-bit case also fails. Clamp the effective width to the remaining source bits or use an arithmetic shift for this case, and correct the test reference, which currently zero-pads the same way. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-25 01:02:16 +00:00 (Migrated from github.com)

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

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

@@ -1051,6 +1051,88 @@ def op_s_bfm_b32(self, ops, mods):
         self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2])))
         return None
 
+    def scalar_const(self, tok):
+        """Value of a 32-bit scalar source that is known at translation time, else None."""
+        r = reg_range(tok)
+        if r:
+            return self.is_const(r[1]) if r[0] == 's' and r[1] == r[2] else None
+        try:
+            return int(tok, 0) & 0xffffffff
+        except ValueError:
+            return None
+
+    def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=None):
+        """SOP2 shifts and bitfield extracts with a 64-bit source and/or result.
+
+        D = f(S0, S1) with S1 a 32-bit word; SCC = (D != 0). Every source is read before any destination
+        register is written, so an overlapping destination cannot feed a half-written value back."""
+        if mods or len(ops) != 3:
+            raise Unsupported('%s with %d operands and modifiers %s' % (name, len(ops), mods))
+        dst = reg_range(ops[0])
+        width = 1 + wide_dst
+        if (not dst or dst[0] != 's' or dst[2] != dst[1] + wide_dst or dst[2] > 105
+                or (wide_dst and dst[1] % 2)):
+            raise Unsupported('%s destination %s' % (name, ops[0]))
+        s1 = reg_range(ops[2])
+        if (any(self.is_lane_mask(t) for t in ops[1:]) or ops[2] in FLOATS or ops[1] in FLOATS
+                or (s1 and (s1[0] != 's' or s1[1] != s1[2] or s1[1] > 105))):
+            raise Unsupported('%s source %s' % (name, ', '.join(ops[1:])))
+        if wide_src:
+            r0 = reg_range(ops[1])
+            if r0 and r0[0] != 's':
+                raise Unsupported('%s source %s' % (name, ops[1]))
+            low, high = self.i64_words(ops[1])
+            known = [self.scalar_const('s%d' % r0[1]), self.scalar_const('s%d' % r0[2])] if r0 else [
+                int(low[:-1]), int(high[:-1])]
+        else:
+            low, high = self.u(ops[1]), None
+            known = [self.scalar_const(ops[1])]
+        amount = self.u(ops[2])
+        amount_known = self.scalar_const(ops[2])
+        d = dst[1]
+        if None not in known and amount_known is not None:
+            x = known[0] | (known[1] << 32 if wide_src else 0)
+            value = scalar_wide_fold(helper, x, amount_known, signed)
+            for i in range(width):
+                word = (value >> (32 * i)) & 0xffffffff
+                self.set_s(d + i, '0x%08xu' % word, ('const', word))
+            self.set_scc('true' if value else 'false')
+            return None
+        uniform = (self.tracks_uniformity()
+                   and self.scalar_expr_uniform('%s %s %s' % (low, high or '', amount)))
+        sg = '' if signed is None else ', %du' % (1 if signed else 0)
+        if wide_dst:
+            call = '%s(%s, %s, %s%s)' % (helper, low, high if wide_src else '0u', amount, sg)
+            self.emit('{ uint2 t = %s; s%d = t.x; s%d = t.y; scc = (t.x | t.y) != 0u; }' % (call, d, d + 1))
+        else:
+            self.emit('{ uint t = %s(%s, %s%s); s%d = t; scc = t != 0u; }' % (helper, low, amount, sg, d))
+        for r in range(d, d + width):
+            self.sym.pop(r, None)
+            self.used_s.add(r)

P2 Badge Clear mask metadata when overwriting numeric destinations

When these handlers reuse an SGPR that previously held a lane mask, neither the folded path nor this runtime path removes it from self.masks. The decoded sequence s_mov_b32 s20, vcc_lo; s_lshl_b64 s[20:21], 8, 1; s_mov_b32 s22, s20 therefore emits s22 = (0x00000010u) & 1, producing zero instead of 16 because Stage.scalar still treats the overwritten register as a mask. Clear the mask tag for every destination word in both write paths.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688401345 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688401345). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:13:29Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1111 ~~~~diff @@ -1051,6 +1051,88 @@ def op_s_bfm_b32(self, ops, mods): self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2]))) return None + def scalar_const(self, tok): + """Value of a 32-bit scalar source that is known at translation time, else None.""" + r = reg_range(tok) + if r: + return self.is_const(r[1]) if r[0] == 's' and r[1] == r[2] else None + try: + return int(tok, 0) & 0xffffffff + except ValueError: + return None + + def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=None): + """SOP2 shifts and bitfield extracts with a 64-bit source and/or result. + + D = f(S0, S1) with S1 a 32-bit word; SCC = (D != 0). Every source is read before any destination + register is written, so an overlapping destination cannot feed a half-written value back.""" + if mods or len(ops) != 3: + raise Unsupported('%s with %d operands and modifiers %s' % (name, len(ops), mods)) + dst = reg_range(ops[0]) + width = 1 + wide_dst + if (not dst or dst[0] != 's' or dst[2] != dst[1] + wide_dst or dst[2] > 105 + or (wide_dst and dst[1] % 2)): + raise Unsupported('%s destination %s' % (name, ops[0])) + s1 = reg_range(ops[2]) + if (any(self.is_lane_mask(t) for t in ops[1:]) or ops[2] in FLOATS or ops[1] in FLOATS + or (s1 and (s1[0] != 's' or s1[1] != s1[2] or s1[1] > 105))): + raise Unsupported('%s source %s' % (name, ', '.join(ops[1:]))) + if wide_src: + r0 = reg_range(ops[1]) + if r0 and r0[0] != 's': + raise Unsupported('%s source %s' % (name, ops[1])) + low, high = self.i64_words(ops[1]) + known = [self.scalar_const('s%d' % r0[1]), self.scalar_const('s%d' % r0[2])] if r0 else [ + int(low[:-1]), int(high[:-1])] + else: + low, high = self.u(ops[1]), None + known = [self.scalar_const(ops[1])] + amount = self.u(ops[2]) + amount_known = self.scalar_const(ops[2]) + d = dst[1] + if None not in known and amount_known is not None: + x = known[0] | (known[1] << 32 if wide_src else 0) + value = scalar_wide_fold(helper, x, amount_known, signed) + for i in range(width): + word = (value >> (32 * i)) & 0xffffffff + self.set_s(d + i, '0x%08xu' % word, ('const', word)) + self.set_scc('true' if value else 'false') + return None + uniform = (self.tracks_uniformity() + and self.scalar_expr_uniform('%s %s %s' % (low, high or '', amount))) + sg = '' if signed is None else ', %du' % (1 if signed else 0) + if wide_dst: + call = '%s(%s, %s, %s%s)' % (helper, low, high if wide_src else '0u', amount, sg) + self.emit('{ uint2 t = %s; s%d = t.x; s%d = t.y; scc = (t.x | t.y) != 0u; }' % (call, d, d + 1)) + else: + self.emit('{ uint t = %s(%s, %s%s); s%d = t; scc = t != 0u; }' % (helper, low, amount, sg, d)) + for r in range(d, d + width): + self.sym.pop(r, None) + self.used_s.add(r) ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Clear mask metadata when overwriting numeric destinations** When these handlers reuse an SGPR that previously held a lane mask, neither the folded path nor this runtime path removes it from `self.masks`. The decoded sequence `s_mov_b32 s20, vcc_lo; s_lshl_b64 s[20:21], 8, 1; s_mov_b32 s22, s20` therefore emits `s22 = (0x00000010u) & 1`, producing zero instead of 16 because `Stage.scalar` still treats the overwritten register as a mask. Clear the mask tag for every destination word in both write paths. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-25 01:02:16 +00:00 (Migrated from github.com)

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

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

@@ -1051,6 +1051,88 @@ def op_s_bfm_b32(self, ops, mods):
         self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2])))
         return None
 
+    def scalar_const(self, tok):
+        """Value of a 32-bit scalar source that is known at translation time, else None."""
+        r = reg_range(tok)
+        if r:
+            return self.is_const(r[1]) if r[0] == 's' and r[1] == r[2] else None
+        try:
+            return int(tok, 0) & 0xffffffff
+        except ValueError:
+            return None
+
+    def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=None):
+        """SOP2 shifts and bitfield extracts with a 64-bit source and/or result.
+
+        D = f(S0, S1) with S1 a 32-bit word; SCC = (D != 0). Every source is read before any destination
+        register is written, so an overlapping destination cannot feed a half-written value back."""
+        if mods or len(ops) != 3:
+            raise Unsupported('%s with %d operands and modifiers %s' % (name, len(ops), mods))
+        dst = reg_range(ops[0])
+        width = 1 + wide_dst
+        if (not dst or dst[0] != 's' or dst[2] != dst[1] + wide_dst or dst[2] > 105
+                or (wide_dst and dst[1] % 2)):
+            raise Unsupported('%s destination %s' % (name, ops[0]))
+        s1 = reg_range(ops[2])
+        if (any(self.is_lane_mask(t) for t in ops[1:]) or ops[2] in FLOATS or ops[1] in FLOATS

P2 Badge Check both source words for lane-mask values

When the high word of a source pair holds a lane mask, this guard misses it because is_lane_mask('s[6:7]') only inspects s6. For example, s_mov_b32 s7, vcc_lo followed by s_lshr_b64 s[20:21], s[6:7], 32 is accepted. For a whole-wave VCC mask of 2, the guest result is s20 = 2 in every lane, but the generated HLSL instead returns each lane's projected zero/one value and computes SCC from it. Reject masks in either member of a wide source pair before lowering the operation.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688401353 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688401353). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:13:29Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1077 ~~~~diff @@ -1051,6 +1051,88 @@ def op_s_bfm_b32(self, ops, mods): self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2]))) return None + def scalar_const(self, tok): + """Value of a 32-bit scalar source that is known at translation time, else None.""" + r = reg_range(tok) + if r: + return self.is_const(r[1]) if r[0] == 's' and r[1] == r[2] else None + try: + return int(tok, 0) & 0xffffffff + except ValueError: + return None + + def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=None): + """SOP2 shifts and bitfield extracts with a 64-bit source and/or result. + + D = f(S0, S1) with S1 a 32-bit word; SCC = (D != 0). Every source is read before any destination + register is written, so an overlapping destination cannot feed a half-written value back.""" + if mods or len(ops) != 3: + raise Unsupported('%s with %d operands and modifiers %s' % (name, len(ops), mods)) + dst = reg_range(ops[0]) + width = 1 + wide_dst + if (not dst or dst[0] != 's' or dst[2] != dst[1] + wide_dst or dst[2] > 105 + or (wide_dst and dst[1] % 2)): + raise Unsupported('%s destination %s' % (name, ops[0])) + s1 = reg_range(ops[2]) + if (any(self.is_lane_mask(t) for t in ops[1:]) or ops[2] in FLOATS or ops[1] in FLOATS ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Check both source words for lane-mask values** When the high word of a source pair holds a lane mask, this guard misses it because `is_lane_mask('s[6:7]')` only inspects `s6`. For example, `s_mov_b32 s7, vcc_lo` followed by `s_lshr_b64 s[20:21], s[6:7], 32` is accepted. For a whole-wave VCC mask of 2, the guest result is `s20 = 2` in every lane, but the generated HLSL instead returns each lane's projected zero/one value and computes SCC from it. Reject masks in either member of a wide source pair before lowering the operation. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
tedin7 commented 2026-11-25 02:01:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:04:54Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049972353 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#pullrequestreview-6049972353). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:04:54Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 02:01:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:04:56Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049972539 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#pullrequestreview-6049972539). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:04:56Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 02:01:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:04:57Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049972727 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#pullrequestreview-6049972727). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:04:57Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 02:02:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:04:54Z.

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

@@ -1916,6 +1998,23 @@ def handler(self, ops, mods):
     setattr(Stage, 'op_s_bitcmp%d_b32' % _bit, _make_bitcmp())
 
 
+def scalar_wide_fold(helper, x, amount, signed):
+    """Translation-time value of one SOP2 shift or bitfield extract (the helper name picks the operation)."""
+    m64 = 0xffffffffffffffff
+    if helper == 'shl64':
+        return (x << (amount & 63)) & m64
+    if helper == 'shr64u':
+        return x >> (amount & 63)
+    if helper == 'shr64i':
+        return ((x - (1 << 64) if x >> 63 else x) >> (amount & 63)) & m64
+    bits = 32 if helper == 'bfe32' else 64
+    field = (x >> (amount & (bits - 1))) & ((1 << ((amount >> 16) & 127)) - 1) & ((1 << bits) - 1)
+    wd = (amount >> 16) & 127
+    if signed and 0 < wd < bits and field >> (wd - 1) & 1:
+        field |= ((1 << bits) - 1) ^ ((1 << wd) - 1)

Fixed in bea78121: the bfe field width is clamped to the bits left above the offset, so the sign comes from the source MSB; the test reference no longer zero-pads. Verified: python3 tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py (pass). AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688781963 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688781963). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:04:54Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 2014 ~~~~diff @@ -1916,6 +1998,23 @@ def handler(self, ops, mods): setattr(Stage, 'op_s_bitcmp%d_b32' % _bit, _make_bitcmp()) +def scalar_wide_fold(helper, x, amount, signed): + """Translation-time value of one SOP2 shift or bitfield extract (the helper name picks the operation).""" + m64 = 0xffffffffffffffff + if helper == 'shl64': + return (x << (amount & 63)) & m64 + if helper == 'shr64u': + return x >> (amount & 63) + if helper == 'shr64i': + return ((x - (1 << 64) if x >> 63 else x) >> (amount & 63)) & m64 + bits = 32 if helper == 'bfe32' else 64 + field = (x >> (amount & (bits - 1))) & ((1 << ((amount >> 16) & 127)) - 1) & ((1 << bits) - 1) + wd = (amount >> 16) & 127 + if signed and 0 < wd < bits and field >> (wd - 1) & 1: + field |= ((1 << bits) - 1) ^ ((1 << wd) - 1) ~~~~ --- Fixed in bea78121: the bfe field width is clamped to the bits left above the offset, so the sign comes from the source MSB; the test reference no longer zero-pads. Verified: python3 tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py (pass). AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 02:02:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:04:56Z.

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

@@ -1051,6 +1051,88 @@ def op_s_bfm_b32(self, ops, mods):
         self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2])))
         return None
 
+    def scalar_const(self, tok):
+        """Value of a 32-bit scalar source that is known at translation time, else None."""
+        r = reg_range(tok)
+        if r:
+            return self.is_const(r[1]) if r[0] == 's' and r[1] == r[2] else None
+        try:
+            return int(tok, 0) & 0xffffffff
+        except ValueError:
+            return None
+
+    def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=None):
+        """SOP2 shifts and bitfield extracts with a 64-bit source and/or result.
+
+        D = f(S0, S1) with S1 a 32-bit word; SCC = (D != 0). Every source is read before any destination
+        register is written, so an overlapping destination cannot feed a half-written value back."""
+        if mods or len(ops) != 3:
+            raise Unsupported('%s with %d operands and modifiers %s' % (name, len(ops), mods))
+        dst = reg_range(ops[0])
+        width = 1 + wide_dst
+        if (not dst or dst[0] != 's' or dst[2] != dst[1] + wide_dst or dst[2] > 105
+                or (wide_dst and dst[1] % 2)):
+            raise Unsupported('%s destination %s' % (name, ops[0]))
+        s1 = reg_range(ops[2])
+        if (any(self.is_lane_mask(t) for t in ops[1:]) or ops[2] in FLOATS or ops[1] in FLOATS
+                or (s1 and (s1[0] != 's' or s1[1] != s1[2] or s1[1] > 105))):
+            raise Unsupported('%s source %s' % (name, ', '.join(ops[1:])))
+        if wide_src:
+            r0 = reg_range(ops[1])
+            if r0 and r0[0] != 's':
+                raise Unsupported('%s source %s' % (name, ops[1]))
+            low, high = self.i64_words(ops[1])
+            known = [self.scalar_const('s%d' % r0[1]), self.scalar_const('s%d' % r0[2])] if r0 else [
+                int(low[:-1]), int(high[:-1])]
+        else:
+            low, high = self.u(ops[1]), None
+            known = [self.scalar_const(ops[1])]
+        amount = self.u(ops[2])
+        amount_known = self.scalar_const(ops[2])
+        d = dst[1]
+        if None not in known and amount_known is not None:
+            x = known[0] | (known[1] << 32 if wide_src else 0)
+            value = scalar_wide_fold(helper, x, amount_known, signed)
+            for i in range(width):
+                word = (value >> (32 * i)) & 0xffffffff
+                self.set_s(d + i, '0x%08xu' % word, ('const', word))
+            self.set_scc('true' if value else 'false')
+            return None
+        uniform = (self.tracks_uniformity()
+                   and self.scalar_expr_uniform('%s %s %s' % (low, high or '', amount)))
+        sg = '' if signed is None else ', %du' % (1 if signed else 0)
+        if wide_dst:
+            call = '%s(%s, %s, %s%s)' % (helper, low, high if wide_src else '0u', amount, sg)
+            self.emit('{ uint2 t = %s; s%d = t.x; s%d = t.y; scc = (t.x | t.y) != 0u; }' % (call, d, d + 1))
+        else:
+            self.emit('{ uint t = %s(%s, %s%s); s%d = t; scc = t != 0u; }' % (helper, low, amount, sg, d))
+        for r in range(d, d + width):
+            self.sym.pop(r, None)
+            self.used_s.add(r)

Fixed in bea78121: both the folded and the runtime path now drop the mask tag of every destination word; new overwritten_mask test covers all six ops. Verified: python3 tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py (pass). AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688782099 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688782099). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:04:56Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1111 ~~~~diff @@ -1051,6 +1051,88 @@ def op_s_bfm_b32(self, ops, mods): self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2]))) return None + def scalar_const(self, tok): + """Value of a 32-bit scalar source that is known at translation time, else None.""" + r = reg_range(tok) + if r: + return self.is_const(r[1]) if r[0] == 's' and r[1] == r[2] else None + try: + return int(tok, 0) & 0xffffffff + except ValueError: + return None + + def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=None): + """SOP2 shifts and bitfield extracts with a 64-bit source and/or result. + + D = f(S0, S1) with S1 a 32-bit word; SCC = (D != 0). Every source is read before any destination + register is written, so an overlapping destination cannot feed a half-written value back.""" + if mods or len(ops) != 3: + raise Unsupported('%s with %d operands and modifiers %s' % (name, len(ops), mods)) + dst = reg_range(ops[0]) + width = 1 + wide_dst + if (not dst or dst[0] != 's' or dst[2] != dst[1] + wide_dst or dst[2] > 105 + or (wide_dst and dst[1] % 2)): + raise Unsupported('%s destination %s' % (name, ops[0])) + s1 = reg_range(ops[2]) + if (any(self.is_lane_mask(t) for t in ops[1:]) or ops[2] in FLOATS or ops[1] in FLOATS + or (s1 and (s1[0] != 's' or s1[1] != s1[2] or s1[1] > 105))): + raise Unsupported('%s source %s' % (name, ', '.join(ops[1:]))) + if wide_src: + r0 = reg_range(ops[1]) + if r0 and r0[0] != 's': + raise Unsupported('%s source %s' % (name, ops[1])) + low, high = self.i64_words(ops[1]) + known = [self.scalar_const('s%d' % r0[1]), self.scalar_const('s%d' % r0[2])] if r0 else [ + int(low[:-1]), int(high[:-1])] + else: + low, high = self.u(ops[1]), None + known = [self.scalar_const(ops[1])] + amount = self.u(ops[2]) + amount_known = self.scalar_const(ops[2]) + d = dst[1] + if None not in known and amount_known is not None: + x = known[0] | (known[1] << 32 if wide_src else 0) + value = scalar_wide_fold(helper, x, amount_known, signed) + for i in range(width): + word = (value >> (32 * i)) & 0xffffffff + self.set_s(d + i, '0x%08xu' % word, ('const', word)) + self.set_scc('true' if value else 'false') + return None + uniform = (self.tracks_uniformity() + and self.scalar_expr_uniform('%s %s %s' % (low, high or '', amount))) + sg = '' if signed is None else ', %du' % (1 if signed else 0) + if wide_dst: + call = '%s(%s, %s, %s%s)' % (helper, low, high if wide_src else '0u', amount, sg) + self.emit('{ uint2 t = %s; s%d = t.x; s%d = t.y; scc = (t.x | t.y) != 0u; }' % (call, d, d + 1)) + else: + self.emit('{ uint t = %s(%s, %s%s); s%d = t; scc = t != 0u; }' % (helper, low, amount, sg, d)) + for r in range(d, d + width): + self.sym.pop(r, None) + self.used_s.add(r) ~~~~ --- Fixed in bea78121: both the folded and the runtime path now drop the mask tag of every destination word; new overwritten_mask test covers all six ops. Verified: python3 tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py (pass). AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 02:02:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:04:57Z.

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

@@ -1051,6 +1051,88 @@ def op_s_bfm_b32(self, ops, mods):
         self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2])))
         return None
 
+    def scalar_const(self, tok):
+        """Value of a 32-bit scalar source that is known at translation time, else None."""
+        r = reg_range(tok)
+        if r:
+            return self.is_const(r[1]) if r[0] == 's' and r[1] == r[2] else None
+        try:
+            return int(tok, 0) & 0xffffffff
+        except ValueError:
+            return None
+
+    def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=None):
+        """SOP2 shifts and bitfield extracts with a 64-bit source and/or result.
+
+        D = f(S0, S1) with S1 a 32-bit word; SCC = (D != 0). Every source is read before any destination
+        register is written, so an overlapping destination cannot feed a half-written value back."""
+        if mods or len(ops) != 3:
+            raise Unsupported('%s with %d operands and modifiers %s' % (name, len(ops), mods))
+        dst = reg_range(ops[0])
+        width = 1 + wide_dst
+        if (not dst or dst[0] != 's' or dst[2] != dst[1] + wide_dst or dst[2] > 105
+                or (wide_dst and dst[1] % 2)):
+            raise Unsupported('%s destination %s' % (name, ops[0]))
+        s1 = reg_range(ops[2])
+        if (any(self.is_lane_mask(t) for t in ops[1:]) or ops[2] in FLOATS or ops[1] in FLOATS

Fixed in bea78121: every register of a wide source pair is checked against the mask set, so s_mov_b32 s7, vcc_lo then a pair read is refused; new refusal test added. Verified: python3 tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py (pass). AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688782283 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688782283). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:04:57Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1077 ~~~~diff @@ -1051,6 +1051,88 @@ def op_s_bfm_b32(self, ops, mods): self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2]))) return None + def scalar_const(self, tok): + """Value of a 32-bit scalar source that is known at translation time, else None.""" + r = reg_range(tok) + if r: + return self.is_const(r[1]) if r[0] == 's' and r[1] == r[2] else None + try: + return int(tok, 0) & 0xffffffff + except ValueError: + return None + + def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=None): + """SOP2 shifts and bitfield extracts with a 64-bit source and/or result. + + D = f(S0, S1) with S1 a 32-bit word; SCC = (D != 0). Every source is read before any destination + register is written, so an overlapping destination cannot feed a half-written value back.""" + if mods or len(ops) != 3: + raise Unsupported('%s with %d operands and modifiers %s' % (name, len(ops), mods)) + dst = reg_range(ops[0]) + width = 1 + wide_dst + if (not dst or dst[0] != 's' or dst[2] != dst[1] + wide_dst or dst[2] > 105 + or (wide_dst and dst[1] % 2)): + raise Unsupported('%s destination %s' % (name, ops[0])) + s1 = reg_range(ops[2]) + if (any(self.is_lane_mask(t) for t in ops[1:]) or ops[2] in FLOATS or ops[1] in FLOATS ~~~~ --- Fixed in bea78121: every register of a wide source pair is checked against the mask set, so s_mov_b32 s7, vcc_lo then a pair read is refused; new refusal test added. Verified: python3 tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py (pass). AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 13:00:35 +00:00
R0Y00 commented 2026-11-25 13:01:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T12:50:02Z.


The dynamic bitfield helpers at the previous head raised FXC X4000 under /WX. I fast-forwarded the branch to eca7576f with masked shift counts and one result expression. The same dynamic helper sample now compiles with both FXC ps_5_1 /WX and DXC ps_6_0 -WX. The 3,498 independent helper cases still pass. This update also records the emitted runtime SCC write and pins the documented refusal of m0 operands. Hosted checks are running on the new head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7162920203 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2088#issuecomment-7162920203). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T12:50:02Z. --- The dynamic bitfield helpers at the previous head raised FXC X4000 under `/WX`. I fast-forwarded the branch to `eca7576f` with masked shift counts and one result expression. The same dynamic helper sample now compiles with both FXC `ps_5_1 /WX` and DXC `ps_6_0 -WX`. The 3,498 independent helper cases still pass. This update also records the emitted runtime SCC write and pins the documented refusal of m0 operands. Hosted checks are running on the new head.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2081
No description provided.