startup: lower the 64-bit vector shifts #1811

Merged
triggered02 merged 1 commit from upstream-pr/1818 into main 2026-11-24 19:29:19 +00:00
triggered02 commented 2026-11-24 19:10:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by triggered02 on 2026-11-24T19:10:07Z.


Fixes #1817. The next census group (#870), and the first one that needed a new translator capability rather than a port.

Change

v_lshlrev_b64, v_lshrrev_b64 and v_ashrrev_i64 had no lowering, so the census form was refused:

v_lshrrev_b64 v[10:11], 0, 0          before -> REFUSED: v_lshrrev_b64
                                      after  -> { uint2 t = shr64u(0u, 0u, 0x00000000u); v10 = t.x; v11 = t.y; }
v_lshlrev_b64 v[10:11], v8, v[8:9]            -> { uint2 t = shl64(v8, v9, v8); v10 = t.x; v11 = t.y; }
v_ashrrev_i64 v[10:11], v8, v[8:9]            -> { uint2 t = shr64i(v8, v9, v8); v10 = t.x; v11 = t.y; }

Two things make this more than a single shift:

  • A 32-bit HLSL shift by 32 or more is undefined, so sh < 32 and sh >= 32 are separate cases (the >= 32 case takes the words from the other half).
  • An in-place destination must read both original words before writing either. The destination pair is very often also the value source, so the helpers take the low and high words by value and return the two result words packed as a uint2; the handler then writes both in one statement. Passing the words in also means no shared temporaries or name collisions between instructions.

The three preamble helpers mask the count to six bits:

uint2 shr64u(uint lo, uint hi, uint sh) { sh &= 63u; uint sb = sh & 31u, sc = (32u - sb) & 31u;
    uint cy = sb == 0u ? 0u : (hi << sc); uint h = hi >> sb;
    return uint2(sh < 32u ? ((lo >> sb) | cy) : h, sh < 32u ? h : 0u); }

Sources may be a VGPR pair, an SGPR pair or an integer inline constant. The low word always shifts unsigned; only the arithmetic form signs the high word.

Test

startup_gfx2hlsl_b64_shift builds VOP3A encodings from the checked-in decoder's layout (0b110101 prefix, opcodes 0x2ff/0x300/0x301, low destination in w0[7:0], count in w1[8:0], value in w1[17:9]), decodes them, then translates. Nine checks: all three shifts reach their own helper, both destination words are written, the census form lowers to a zero pair, an inline constant count stays a constant, and the helpers are in the preamble with the count masked.

release: startup_gfx2hlsl_b64_shift Passed 0.14 s
dev:     the JSON/Python checks pass; 2 failures in this tree are pre-existing (below)

Fail-first, all caught:

point the right shift at shl64          -> "the census form lowers to a zero pair" FAIL
drop the high destination write         -> "the right shift uses the helper and writes both words" FAIL
remove the count mask                   -> "the helpers mask the count" FAIL

Pre-existing failures, not from this PR

startup_gfx2hlsl_mad_preserve_{warp,hardware} fail in this dev tree with Unknown GPU operation or invalid arguments: --raw-uav-dump-sm62 — the built offscreen_check is stale. Confirmed identical on origin/main (ecc6e6ce) with the same binary. A rebuild fixes it; this branch does not touch that tool.

What this does not establish

  • That the emitted HLSL compiles or computes the right values — the test compares text; DXC/WARP were not run here.
  • The 24-bit and 32-bit shift forms, and any use of the result in a draw, are out of scope.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6589386314 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1818). Originally posted by [triggered02](https://github.com/triggered02) on 2026-11-24T19:10:07Z. --- Fixes #1817. The next census group (#870), and the first one that needed a new translator capability rather than a port. ## Change `v_lshlrev_b64`, `v_lshrrev_b64` and `v_ashrrev_i64` had no lowering, so the census form was refused: ``` v_lshrrev_b64 v[10:11], 0, 0 before -> REFUSED: v_lshrrev_b64 after -> { uint2 t = shr64u(0u, 0u, 0x00000000u); v10 = t.x; v11 = t.y; } v_lshlrev_b64 v[10:11], v8, v[8:9] -> { uint2 t = shl64(v8, v9, v8); v10 = t.x; v11 = t.y; } v_ashrrev_i64 v[10:11], v8, v[8:9] -> { uint2 t = shr64i(v8, v9, v8); v10 = t.x; v11 = t.y; } ``` Two things make this more than a single shift: - **A 32-bit HLSL shift by 32 or more is undefined**, so `sh < 32` and `sh >= 32` are separate cases (the `>= 32` case takes the words from the other half). - **An in-place destination must read both original words before writing either.** The destination pair is very often also the value source, so the helpers take the low and high words *by value* and return the two result words packed as a `uint2`; the handler then writes both in one statement. Passing the words in also means no shared temporaries or name collisions between instructions. The three preamble helpers mask the count to six bits: ```hlsl uint2 shr64u(uint lo, uint hi, uint sh) { sh &= 63u; uint sb = sh & 31u, sc = (32u - sb) & 31u; uint cy = sb == 0u ? 0u : (hi << sc); uint h = hi >> sb; return uint2(sh < 32u ? ((lo >> sb) | cy) : h, sh < 32u ? h : 0u); } ``` Sources may be a VGPR pair, an SGPR pair or an integer inline constant. The low word always shifts unsigned; only the arithmetic form signs the high word. ## Test `startup_gfx2hlsl_b64_shift` builds VOP3A encodings from the checked-in decoder's layout (`0b110101` prefix, opcodes `0x2ff`/`0x300`/`0x301`, low destination in `w0[7:0]`, count in `w1[8:0]`, value in `w1[17:9]`), decodes them, then translates. Nine checks: all three shifts reach their own helper, both destination words are written, the census form lowers to a zero pair, an inline constant count stays a constant, and the helpers are in the preamble with the count masked. ``` release: startup_gfx2hlsl_b64_shift Passed 0.14 s dev: the JSON/Python checks pass; 2 failures in this tree are pre-existing (below) ``` Fail-first, all caught: ``` point the right shift at shl64 -> "the census form lowers to a zero pair" FAIL drop the high destination write -> "the right shift uses the helper and writes both words" FAIL remove the count mask -> "the helpers mask the count" FAIL ``` ## Pre-existing failures, not from this PR `startup_gfx2hlsl_mad_preserve_{warp,hardware}` fail in this dev tree with `Unknown GPU operation or invalid arguments: --raw-uav-dump-sm62` — the built `offscreen_check` is stale. **Confirmed identical on `origin/main` (`ecc6e6ce`)** with the same binary. A rebuild fixes it; this branch does not touch that tool. ## What this does not establish - That the emitted HLSL compiles or computes the right values — the test compares text; DXC/WARP were not run here. - The 24-bit and 32-bit shift forms, and any use of the result in a draw, are out of scope. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 20:00:46 +00:00
tedin7 commented 2026-11-24 20:00:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:18:50Z.

Review: COMMENTED


Review of d819085a (startup: lower the 64-bit vector shifts). Verdict: 0 P1, 1 P2, 1 P2 on the test, plus P3 notes.

What I checked

  • The three HLSL helpers (shl64, shr64u, shr64i). I transliterated them one to one, with 32-bit shifts masked to 5 bits as HLSL/DXIL do, and compared them to a 64-bit reference (Python integers) for 410 values (edge values, sign bits, 300 random) times counts 0..129, 255, 256 and 2^32-1, for the three shifts: 122,892 cases, 0 mismatches. The count is masked to six bits like the hardware's 64-bit shift; counts 32 and 63 are right for the logical and the arithmetic form.
  • The handler reads both source words by value and writes both result words from one uint2, so an in-place destination is safe. It refuses modifiers, a non-pair destination and sources that are not a VGPR/SGPR pair or an integer inline constant in -16..64 (zero- or sign-extended to 64 bits as the hardware does for an integer operand). Float inline constants and literals are refused, which is the safe choice.
  • Clean-room and title-neutral: only public encodings are used; the PR names no title.

P2 (code): shift64 does not clear the uniform-register record

set_v() does self.uniform_v.discard(n) before every VGPR write. shift64() (tools/startup/gfx2hlsl.py, the new handler) writes v%d/v%d itself and skips that, so a register proven uniform earlier stays "uniform" after a shift that overwrites it with a lane-varying value. op_v_readfirstlane_b32 trusts uniform_v, so it then accepts the register. Reproduction against this SHA (a Stage with v10 marked uniform, then v_lshrrev_b64 v[10:11], v8, v[8:9], then v_readfirstlane_b32 s5, v10):

v_lshrrev_b64 v[10:11], v8, v[8:9]
v10 still treated as uniform after the shift: True
readfirstlane s5, v10 ACCEPTED: s5 = v10;

Fix: self.uniform_v.discard(dst[1]); self.uniform_v.discard(dst[2]) before the emit (or route the write through a shared helper), and add a check for it.

P2 (test): the numeric behaviour is not tested

tests/startup_gfx2hlsl_b64_shift_check.py checks that certain substrings appear ('shr64u(v8, v9, v8)', 'sh &= 63u;' three times, ...). It never evaluates a helper. I mutated the PR code and ran the PR test:

mutation PR test
shl64: drop the carry word ` cy` from the high result
shr64i: high result for counts >= 32 changed from the sign fill to 0u PASSES
shr64u: drop the carry (cy = 0) PASSES
sh &= 63u to sh &= 31u (all three) FAILS (substring count)
v_ashrrev_i64 routed to shr64u FAILS

So three real arithmetic bugs survive the test. The helpers are correct today (see above), but nothing keeps them so. Suggested: evaluate the helper text in the check (a small Python transliteration, or compile the helper HLSL and read back) against a 64-bit reference at counts 0, 1, 31, 32, 33, 63 and 64+, for the three shifts.

P3

  • A missing blank line before def op_v_cvt_pkrtz_f16_f32 after the new handlers.
  • The helper count mask makes counts above 63 wrap, as intended; a one-line comment saying so next to the preamble strings would help the next reader.

Commands run (Linux, on exactly d819085a220f8a12cb7919484fef0c7f55c6f761)

python3 -B tests/startup_gfx2hlsl_b64_shift_check.py         -> PASS
for t in tests/startup_gfx2hlsl*_check.py; do python3 -B $t; done   -> 12 scripts, all rc=0
git diff --check origin/main...HEAD                          -> clean
python3 -B tests/docs_links_check.py                         -> Checked 17 Markdown files and 368 links
python3 -B tests/bidi_controls_check.py                      -> no bidirectional controls
python3 -B emulate_helpers.py                                -> cases 122892 mismatches 0
python3 -B uniform_repro.py                                  -> output above

Mutations were applied to the working tree and restored.

Limits

The generated HLSL was not compiled (no shader compiler on this host). The PR changes no C++, so I did not run the Windows pipeline.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6045776823 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1818#pullrequestreview-6045776823). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T19:18:50Z. **Review:** COMMENTED --- Review of `d819085a` (`startup: lower the 64-bit vector shifts`). Verdict: 0 P1, 1 P2, 1 P2 on the test, plus P3 notes. ## What I checked - The three HLSL helpers (`shl64`, `shr64u`, `shr64i`). I transliterated them one to one, with 32-bit shifts masked to 5 bits as HLSL/DXIL do, and compared them to a 64-bit reference (Python integers) for 410 values (edge values, sign bits, 300 random) times counts 0..129, 255, 256 and 2^32-1, for the three shifts: **122,892 cases, 0 mismatches**. The count is masked to six bits like the hardware's 64-bit shift; counts 32 and 63 are right for the logical and the arithmetic form. - The handler reads both source words by value and writes both result words from one `uint2`, so an in-place destination is safe. It refuses modifiers, a non-pair destination and sources that are not a VGPR/SGPR pair or an integer inline constant in -16..64 (zero- or sign-extended to 64 bits as the hardware does for an integer operand). Float inline constants and literals are refused, which is the safe choice. - Clean-room and title-neutral: only public encodings are used; the PR names no title. ## P2 (code): `shift64` does not clear the uniform-register record `set_v()` does `self.uniform_v.discard(n)` before every VGPR write. `shift64()` (`tools/startup/gfx2hlsl.py`, the new handler) writes `v%d`/`v%d` itself and skips that, so a register proven uniform earlier stays "uniform" after a shift that overwrites it with a lane-varying value. `op_v_readfirstlane_b32` trusts `uniform_v`, so it then accepts the register. Reproduction against this SHA (a Stage with `v10` marked uniform, then `v_lshrrev_b64 v[10:11], v8, v[8:9]`, then `v_readfirstlane_b32 s5, v10`): ``` v_lshrrev_b64 v[10:11], v8, v[8:9] v10 still treated as uniform after the shift: True readfirstlane s5, v10 ACCEPTED: s5 = v10; ``` Fix: `self.uniform_v.discard(dst[1]); self.uniform_v.discard(dst[2])` before the emit (or route the write through a shared helper), and add a check for it. ## P2 (test): the numeric behaviour is not tested `tests/startup_gfx2hlsl_b64_shift_check.py` checks that certain substrings appear (`'shr64u(v8, v9, v8)'`, `'sh &= 63u;'` three times, ...). It never evaluates a helper. I mutated the PR code and ran the PR test: | mutation | PR test | |---|---| | `shl64`: drop the carry word `| cy` from the high result | **PASSES** | | `shr64i`: high result for counts >= 32 changed from the sign fill to `0u` | **PASSES** | | `shr64u`: drop the carry (`cy = 0`) | **PASSES** | | `sh &= 63u` to `sh &= 31u` (all three) | FAILS (substring count) | | `v_ashrrev_i64` routed to `shr64u` | FAILS | So three real arithmetic bugs survive the test. The helpers are correct today (see above), but nothing keeps them so. Suggested: evaluate the helper text in the check (a small Python transliteration, or compile the helper HLSL and read back) against a 64-bit reference at counts 0, 1, 31, 32, 33, 63 and 64+, for the three shifts. ## P3 - A missing blank line before `def op_v_cvt_pkrtz_f16_f32` after the new handlers. - The helper count mask makes counts above 63 wrap, as intended; a one-line comment saying so next to the preamble strings would help the next reader. ## Commands run (Linux, on exactly `d819085a220f8a12cb7919484fef0c7f55c6f761`) ``` python3 -B tests/startup_gfx2hlsl_b64_shift_check.py -> PASS for t in tests/startup_gfx2hlsl*_check.py; do python3 -B $t; done -> 12 scripts, all rc=0 git diff --check origin/main...HEAD -> clean python3 -B tests/docs_links_check.py -> Checked 17 Markdown files and 368 links python3 -B tests/bidi_controls_check.py -> no bidirectional controls python3 -B emulate_helpers.py -> cases 122892 mismatches 0 python3 -B uniform_repro.py -> output above ``` Mutations were applied to the working tree and restored. ## Limits The generated HLSL was not compiled (no shader compiler on this host). The PR changes no C++, so I did not run the Windows pipeline. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-24 20:01:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T19:10:20Z.


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-24T19:12:24.163893Z d819085 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:7141703930 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1818#issuecomment-7141703930). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T19:10:20Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"d819085a220f8a12cb7919484fef0c7f55c6f761","has_any_findings":false,"has_unresolved_findings":false,"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-24T19:12:24.163893Z">2026-11-24T19:12:24.163893Z</relative-time> | `d819085` | 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>
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!1811
No description provided.