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

Manually merged
github merged 3 commits from gh-pr/2088/head into gh-pr/2088/base 2026-11-26 23:55:32 +00:00
Owner

@⁠tedin7 on GitHub · 2026-11-25 00:06 UTC · original
tedin7:x2w-b3-s-shift64-bfe → main · +439 −0 · 4 files · 3 commits · merged 2026-11-25 12:54 UTC

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.

**@⁠tedin7** on GitHub · 2026-11-25 00:06 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088) `tedin7:x2w-b3-s-shift64-bfe` → `main` · +439 −0 · 4 files · 3 commits · merged 2026-11-25 12:54 UTC 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.
Scalar 64-bit shifts and bitfield extracts fold constant operands and otherwise emit one block that reads every source before writing a destination; SCC is set to (result != 0). Adds bfe32/bfe64 helpers and a pure-Python check that runs the emitted helper text against a bit-string reference.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:55:31 +00:00
github manually merged commit eca7576faa into gh-pr/2088/base 2026-11-26 23:55:32 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:06 UTC · original · edited 2026-11-25 00:13 UTC

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.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:06 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#issuecomment-7148157062) · edited 2026-11-25 00:13 UTC <!-- 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>
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:13 UTC · original

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 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688401339) **<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 -->
@ -2529,0 +2586,4 @@
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)
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:13 UTC · original

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 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688401345) **<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 -->
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:13 UTC · original

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 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688401353) **<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 -->
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 01:04 UTC · original

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** on GitHub · 2026-11-25 01:04 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688781963) 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.
@ -2529,0 +2586,4 @@
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)
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 01:04 UTC · original

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** on GitHub · 2026-11-25 01:04 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688782099) 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.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 01:04 UTC · original

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.

**@⁠tedin7** on GitHub · 2026-11-25 01:04 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#discussion_r4688782283) 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.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-25 12:50 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-25 12:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#issuecomment-7162920203) 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.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:13 UTC · original · reviewed

💡 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".

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2088#pullrequestreview-6049542469) · **reviewed** <!-- 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>
This discussion has been locked. Commenting is limited to contributors.
No description provided.