shader: lower s_mulk_i32 and s_addk_i32 in gfx2hlsl (Part of #1723) #2018

Closed
tedin7 wants to merge 1 commit from upstream-pr/2025 into main
tedin7 commented 2026-11-24 23:26:54 +00:00 (Migrated from github.com)

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


Part of #1723

What this unblocks: Run 20 census #5 (79/77 pairs).

Lower s_mulk_i32 and s_addk_i32 in tools/startup/gfx2hlsl.py. Both read and write SDST and take a 16-bit immediate. The decoder prints it as unsigned hex, so it is sign-extended first, then routed through the existing two-source scalar lowering (s_mul_i32 / s_add_i32). s_addk_i32 also sets SCC to the signed overflow of the add, computed from the old destination before the result is written. s_mulk_i32 leaves SCC alone. A destination that is not a plain SGPR (m0, exec, vcc) is refused, and so is s_addk_i32 on descriptor data (SCC would depend on it).

Verification:

  • python3 -B tests/startup_gfx2hlsl_s_mulk_addk_check.py on this branch: PASS. The test builds SOPK words from the decoder layout, executes the emitted statements on 32-bit values and compares them with the architectural result for immediates 0, 1, 0x7fff, 0x8000, 0xfffe, 0xffff.
  • The same test against tools/startup/gfx2hlsl.py from origin/main: fails with Unsupported: s_mulk_i32.
  • Every tests/startup_gfx2hlsl_*check.py run one by one on this branch: all pass.
  • Registered as startup_gfx2hlsl_s_mulk_addk in CMakeLists.txt. These were run on Linux, not on Win2025. No ctest run.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6593492135 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2025). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T23:26:54Z. --- Part of #1723 What this unblocks: Run 20 census #5 (79/77 pairs). Lower `s_mulk_i32` and `s_addk_i32` in `tools/startup/gfx2hlsl.py`. Both read and write SDST and take a 16-bit immediate. The decoder prints it as unsigned hex, so it is sign-extended first, then routed through the existing two-source scalar lowering (`s_mul_i32` / `s_add_i32`). `s_addk_i32` also sets SCC to the signed overflow of the add, computed from the old destination before the result is written. `s_mulk_i32` leaves SCC alone. A destination that is not a plain SGPR (m0, exec, vcc) is refused, and so is `s_addk_i32` on descriptor data (SCC would depend on it). Verification: - `python3 -B tests/startup_gfx2hlsl_s_mulk_addk_check.py` on this branch: `PASS`. The test builds SOPK words from the decoder layout, executes the emitted statements on 32-bit values and compares them with the architectural result for immediates 0, 1, 0x7fff, 0x8000, 0xfffe, 0xffff. - The same test against `tools/startup/gfx2hlsl.py` from `origin/main`: fails with `Unsupported: s_mulk_i32`. - Every `tests/startup_gfx2hlsl_*check.py` run one by one on this branch: all pass. - Registered as `startup_gfx2hlsl_s_mulk_addk` in CMakeLists.txt. These were run on Linux, not on Win2025. No ctest run. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-25 00:04:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T23:34:48Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 710b1cf9de

ℹ️ 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:6049128913 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2025#pullrequestreview-6049128913). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T23:34:48Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `710b1cf9de` <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>
upstream-sync closed this pull request 2026-11-25 01:01:08 +00:00
chatgpt-codex-connector[bot commented 2026-11-25 01:01:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T23:27:05Z.


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-24T23:34:51.473613Z 710b1cf 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:7147414266 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2025#issuecomment-7147414266). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T23:27:05Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"710b1cf9de268e4ba63109efa4a3f42ca7e964ac","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-24T23:34:51.473613Z">2026-11-24T23:34:51.473613Z</relative-time> | `710b1cf` | 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>
tedin7 commented 2026-11-25 01:02:06 +00:00 (Migrated from github.com)

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


Closing as superseded by #2102 which also guards mask/descriptor destinations and adds mutation controls.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148587511 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2025#issuecomment-7148587511). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:30:57Z. --- Closing as superseded by #2102 which also guards mask/descriptor destinations and adds mutation controls.
chatgpt-codex-connector[bot commented 2026-11-25 01:02:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T23:34:48Z.

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

@@ -1750,6 +1750,38 @@ def handler(self, ops, mods):
         setattr(Stage, 'op_s_cmpk_%s_%s' % (_name, _typ), _make())
 
 
+def _sopk_arith(self, base, scalar_op, ops, mods):
+    """s_mulk_i32 / s_addk_i32: SDST = SDST op sign_extend(SIMM16), through the two-source scalar lowering.
+
+    The decoder prints the 16-bit immediate as unsigned hex; it is sign-extended here. s_addk_i32 also sets SCC
+    to the signed overflow of the add, which the plain add lowering does not model."""
+    if mods or len(ops) != 2:
+        raise Unsupported('%s with %d operands' % (base, len(ops)))
+    reg = reg_range(ops[0])
+    if not reg or reg[0] != 's' or reg[1] != reg[2] or reg[1] > 105:
+        raise Unsupported('%s destination %s' % (base, ops[0]))

P1 Badge Reject SOPK arithmetic on projected lane-mask SGPRs

When SDST holds a comparison mask, this validation accepts it even though Stage.compare() stores only the current lane's 0/1 projection and records that fact in self.masks. For example, after v_cmp_lt_i32_e64 s4, v0, v1 produces a guest low mask of 0x7fffffff, s_addk_i32 s4, 1 must set SCC, but the emitted expression computes on 0/1 and sets SCC false in every invocation; a following scalar select or SCC branch therefore takes the wrong path. Reject mask-tagged SGPRs before either SOPK lowering, as the complement/pack handlers already do, or model the full mask instead of treating its projection as a numeric word.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688031003 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2025#discussion_r4688031003). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T23:34:48Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1762 ~~~~diff @@ -1750,6 +1750,38 @@ def handler(self, ops, mods): setattr(Stage, 'op_s_cmpk_%s_%s' % (_name, _typ), _make()) +def _sopk_arith(self, base, scalar_op, ops, mods): + """s_mulk_i32 / s_addk_i32: SDST = SDST op sign_extend(SIMM16), through the two-source scalar lowering. + + The decoder prints the 16-bit immediate as unsigned hex; it is sign-extended here. s_addk_i32 also sets SCC + to the signed overflow of the add, which the plain add lowering does not model.""" + if mods or len(ops) != 2: + raise Unsupported('%s with %d operands' % (base, len(ops))) + reg = reg_range(ops[0]) + if not reg or reg[0] != 's' or reg[1] != reg[2] or reg[1] > 105: + raise Unsupported('%s destination %s' % (base, ops[0])) ~~~~ --- **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Reject SOPK arithmetic on projected lane-mask SGPRs** When SDST holds a comparison mask, this validation accepts it even though `Stage.compare()` stores only the current lane's 0/1 projection and records that fact in `self.masks`. For example, after `v_cmp_lt_i32_e64 s4, v0, v1` produces a guest low mask of `0x7fffffff`, `s_addk_i32 s4, 1` must set SCC, but the emitted expression computes on 0/1 and sets SCC false in every invocation; a following scalar select or SCC branch therefore takes the wrong path. Reject mask-tagged SGPRs before either SOPK lowering, as the complement/pack handlers already do, or model the full mask instead of treating its projection as a numeric word. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-25 01:02:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T23:34:48Z.

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

@@ -1750,6 +1750,38 @@ def handler(self, ops, mods):
         setattr(Stage, 'op_s_cmpk_%s_%s' % (_name, _typ), _make())
 
 
+def _sopk_arith(self, base, scalar_op, ops, mods):
+    """s_mulk_i32 / s_addk_i32: SDST = SDST op sign_extend(SIMM16), through the two-source scalar lowering.
+
+    The decoder prints the 16-bit immediate as unsigned hex; it is sign-extended here. s_addk_i32 also sets SCC
+    to the signed overflow of the add, which the plain add lowering does not model."""
+    if mods or len(ops) != 2:
+        raise Unsupported('%s with %d operands' % (base, len(ops)))
+    reg = reg_range(ops[0])
+    if not reg or reg[0] != 's' or reg[1] != reg[2] or reg[1] > 105:
+        raise Unsupported('%s destination %s' % (base, ops[0]))
+    try:
+        imm = int(ops[1], 0)
+    except ValueError:
+        raise Unsupported('%s immediate %s' % (base, ops[1]))
+    if not -0x8000 <= imm <= 0xffff:
+        raise Unsupported('%s immediate %s outside 16 bits' % (base, ops[1]))
+    if imm >= 0x8000:
+        imm -= 0x10000
+    word = imm & 0xffffffff
+    if base == 's_addk_i32':
+        if self.sym.get(reg[1]):
+            raise Unsupported('scc from descriptor data')

P2 Badge Allow numeric symbolic SGPRs through the addk guard

When SDST is initialized by an immediate move or supplied as a root constant, self.sym contains a numeric const or ud entry, not descriptor data. Consequently, the ordinary sequence s_mov_b32 s4, 1; s_addk_i32 s4, 1 raises Unsupported('scc from descriptor data') through the production Stage constructor instead of lowering the addition. Numeric fixed_buffer_word entries are rejected too. Restrict this guard to genuinely non-numeric symbols so the new instruction works on initialized scalar values; the added test misses this because it starts with an empty symbol table.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688031011 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2025#discussion_r4688031011). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T23:34:48Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1774 ~~~~diff @@ -1750,6 +1750,38 @@ def handler(self, ops, mods): setattr(Stage, 'op_s_cmpk_%s_%s' % (_name, _typ), _make()) +def _sopk_arith(self, base, scalar_op, ops, mods): + """s_mulk_i32 / s_addk_i32: SDST = SDST op sign_extend(SIMM16), through the two-source scalar lowering. + + The decoder prints the 16-bit immediate as unsigned hex; it is sign-extended here. s_addk_i32 also sets SCC + to the signed overflow of the add, which the plain add lowering does not model.""" + if mods or len(ops) != 2: + raise Unsupported('%s with %d operands' % (base, len(ops))) + reg = reg_range(ops[0]) + if not reg or reg[0] != 's' or reg[1] != reg[2] or reg[1] > 105: + raise Unsupported('%s destination %s' % (base, ops[0])) + try: + imm = int(ops[1], 0) + except ValueError: + raise Unsupported('%s immediate %s' % (base, ops[1])) + if not -0x8000 <= imm <= 0xffff: + raise Unsupported('%s immediate %s outside 16 bits' % (base, ops[1])) + if imm >= 0x8000: + imm -= 0x10000 + word = imm & 0xffffffff + if base == 's_addk_i32': + if self.sym.get(reg[1]): + raise Unsupported('scc from descriptor data') ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Allow numeric symbolic SGPRs through the addk guard** When SDST is initialized by an immediate move or supplied as a root constant, `self.sym` contains a numeric `const` or `ud` entry, not descriptor data. Consequently, the ordinary sequence `s_mov_b32 s4, 1; s_addk_i32 s4, 1` raises `Unsupported('scc from descriptor data')` through the production `Stage` constructor instead of lowering the addition. Numeric `fixed_buffer_word` entries are rejected too. Restrict this guard to genuinely non-numeric symbols so the new instruction works on initialized scalar values; the added test misses this because it starts with an empty symbol table. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-25 01:02:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T23:34:48Z.

Code review location: CMakeLists.txt; line 2162

@@ -2157,6 +2157,10 @@ if(EXISTS "${INTAKE_PYTHON}")
     # The 64-bit vector shifts (#1815).
     add_test(NAME startup_gfx2hlsl_b64_shift COMMAND "${INTAKE_PYTHON}" -B
         "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_b64_shift_check.py")
+    # The SOPK arithmetic forms s_mulk_i32 and s_addk_i32 (#1723).
+    add_test(NAME startup_gfx2hlsl_s_mulk_addk COMMAND "${INTAKE_PYTHON}" -B
+        "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_s_mulk_addk_check.py")

P1 Badge Document the new startup SOPK capability and reproduction check

The patch introduces startup-translator support and a new CTest, but updates neither README.md nor docs/experiments.md. AGENTS.md requires documentation updates when public capabilities or reproduction commands change; the existing SOPK paragraph documents pixel_aot, not this startup lowering. Add the new startup check and its measured scope to the public experiment documentation, distinguishing the Linux emitted-statement evaluation from Windows/DXIL or GPU validation.

AGENTS.md reference: AGENTS.md:L209-L210

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688031018 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2025#discussion_r4688031018). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T23:34:48Z. **Code review location:** `CMakeLists.txt`; line 2162 ~~~~diff @@ -2157,6 +2157,10 @@ if(EXISTS "${INTAKE_PYTHON}") # The 64-bit vector shifts (#1815). add_test(NAME startup_gfx2hlsl_b64_shift COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_b64_shift_check.py") + # The SOPK arithmetic forms s_mulk_i32 and s_addk_i32 (#1723). + add_test(NAME startup_gfx2hlsl_s_mulk_addk COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_s_mulk_addk_check.py") ~~~~ --- **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Document the new startup SOPK capability and reproduction check** The patch introduces startup-translator support and a new CTest, but updates neither `README.md` nor `docs/experiments.md`. `AGENTS.md` requires documentation updates when public capabilities or reproduction commands change; the existing SOPK paragraph documents `pixel_aot`, not this startup lowering. Add the new startup check and its measured scope to the public experiment documentation, distinguishing the Linux emitted-statement evaluation from Windows/DXIL or GPU validation. AGENTS.md reference: [AGENTS.md:L209-L210](https://github.com/R0Y00/Xbox2Windows/blob/710b1cf9de268e4ba63109efa4a3f42ca7e964ac/AGENTS.md#L209-L210) Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->

Pull request closed

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!2018
No description provided.