gfx2hlsl: prove a thread-id shift uniform when it clears every thread-id bit #2447

Merged
NiiRoZz merged 4 commits from upstream-pr/2455 into main 2026-11-26 02:40:28 +00:00
NiiRoZz commented 2026-11-25 17:14:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T17:14:44Z.


Depends on #2446: this branch is stacked on it, so the diff shows that commit too until it merges; this pull request then holds one commit. Refs #2274.

What / Why

Compute programs compute the group index as v_lshl_add_u32 v0, sG, 6, v0 (global id = group * 64 + local id) followed by v_lshrrev_b32 v1, 6, v0 and v_readfirstlane_b32 s, v1. The shifted value is the group index, the same in every lane, but the proof had no rule for a thread-id source.

Change (stacked on the uniform-load branch; independent of it)

Stage.tid_shift proves v_lshrrev_b32 vD, n, vS uniform in a compute entry block when:

  • the group's X size (pipe.group[0]) is at most 1 << n, and
  • vS is the untouched X thread id v0 (value 0), or vS is defined by the nearest earlier v_lshl_add_u32 vS, sK, K, v0 with n <= K, size <= 1 << K, v0 unwritten before that definition, sK uniform and not written between definition and read.
    The result is (sK << K) >> n (a recompute expression, so a read past the entry block is accepted until sK changes). The low K bits of sK << K are clear and the thread id is below 1 << K, so the sum does not carry. Every other form (larger group, shift larger than the scale, varying or rewritten scalar, vector base, rewritten or swapped v0, modifiers, other stages) stays refused.

Tests

  • python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: 0 failed (positives: group sizes 1, 33, 64, global id shifted back, recompute past the entry block; 20 negative controls incl. group 65/128, no recorded size, shift larger than scale, varying/rewritten scalar, vector base, rewritten v0, swap, load into v0).
  • All tests/startup_gfx2hlsl_*.py and tests/startup_translator_check.py: exit 0. No LLVM-dependent check run.

Measured (256 compute requests)

First refusal v_readfirstlane_b32 requires a proven uniform source: 241 baseline, 115 with the uniform-load rule, 88 with this commit on top of it (27 move on; next first refusals: s_load register offset through the resource heap 15, src_scc in s_mov_b32 13, buffer store without offen form 12, src_scc in s_add_u32 10).

Limits

The group size comes from compute state block A, which compute_entry documents as a hypothesis from a local observation, not a decoded field. The proof is as good as that field (the same value sizes numthreads). Wave size is not needed: the value does not depend on how lanes are grouped into waves. A group whose X size is a multiple of 64 (a wave index) is not proven.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6609769614 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2455). Originally posted by [NiiRoZz](https://github.com/NiiRoZz) on 2026-11-25T17:14:44Z. --- Depends on #2446: this branch is stacked on it, so the diff shows that commit too until it merges; this pull request then holds one commit. Refs #2274. ## What / Why Compute programs compute the group index as `v_lshl_add_u32 v0, sG, 6, v0` (global id = group * 64 + local id) followed by `v_lshrrev_b32 v1, 6, v0` and `v_readfirstlane_b32 s, v1`. The shifted value is the group index, the same in every lane, but the proof had no rule for a thread-id source. ## Change (stacked on the uniform-load branch; independent of it) `Stage.tid_shift` proves `v_lshrrev_b32 vD, n, vS` uniform in a compute entry block when: - the group's X size (`pipe.group[0]`) is at most `1 << n`, and - vS is the untouched X thread id v0 (value 0), or vS is defined by the nearest earlier `v_lshl_add_u32 vS, sK, K, v0` with n <= K, size <= `1 << K`, v0 unwritten before that definition, sK uniform and not written between definition and read. The result is `(sK << K) >> n` (a recompute expression, so a read past the entry block is accepted until sK changes). The low K bits of `sK << K` are clear and the thread id is below `1 << K`, so the sum does not carry. Every other form (larger group, shift larger than the scale, varying or rewritten scalar, vector base, rewritten or swapped v0, modifiers, other stages) stays refused. ## Tests - `python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py`: 0 failed (positives: group sizes 1, 33, 64, global id shifted back, recompute past the entry block; 20 negative controls incl. group 65/128, no recorded size, shift larger than scale, varying/rewritten scalar, vector base, rewritten v0, swap, load into v0). - All `tests/startup_gfx2hlsl_*.py` and `tests/startup_translator_check.py`: exit 0. No LLVM-dependent check run. ## Measured (256 compute requests) First refusal `v_readfirstlane_b32 requires a proven uniform source`: 241 baseline, 115 with the uniform-load rule, 88 with this commit on top of it (27 move on; next first refusals: s_load register offset through the resource heap 15, src_scc in s_mov_b32 13, buffer store without offen form 12, src_scc in s_add_u32 10). ## Limits The group size comes from compute state block A, which `compute_entry` documents as a hypothesis from a local observation, not a decoded field. The proof is as good as that field (the same value sizes `numthreads`). Wave size is not needed: the value does not depend on how lanes are grouped into waves. A group whose X size is a multiple of 64 (a wave index) is not proven. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
tedin7 commented 2026-11-25 19:03:51 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:26:31Z.

Review: COMMENTED


Review of head c30127d (comment only, no Windows run on my side; the PR body lists no Windows results, so nothing here covers any SHA on Windows).

Checked against origin/main:

  • Stage.tid_shift / vgpr_writer in tools/startup/gfx2hlsl.py: the bound logic is sound. The X thread id is below the group X size (v0 holds SV_GroupThreadID per the compute prologue note near line 1153), so >> n with size <= 1 << n clears it. For (sK << K) + v0 the low K bits of the base are clear, so there is no carry. The nearest-writer search, the v0-unwritten check at the definition, and the scalar-unwritten check between definition and read all look right. Gating (compute, entry block, exec known, no modifiers) matches the sibling rules. I found no correctness problem.
  • Scope is small and additive: 2 files, no launcher/CI/hook changes. No other open or merged PR covers the same rule.
  • Dependency: stacked on #2446, which is still OPEN. It should not merge before #2446; the diff against main includes that commit.

Commands run locally:

  • python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py -> 0 failed
  • git diff --check origin/main...HEAD -> clean
  • python3 tests/docs_links_check.py and python3 tests/bidi_controls_check.py -> OK
  • Mutation 1: allow size <= 1 << (n+1) -> test fails (3 failed).
  • Mutation 2: drop the n <= K check -> test fails (1 failed).
  • Mutation 3: drop the "v0 unwritten before the definition" check -> test fails (1 failed).

All three mutations are caught, so the tests guard the new rule. No blocking findings.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6062861624 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2455#pullrequestreview-6062861624). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T18:26:31Z. **Review:** COMMENTED --- Review of head c30127d (comment only, no Windows run on my side; the PR body lists no Windows results, so nothing here covers any SHA on Windows). Checked against origin/main: - `Stage.tid_shift` / `vgpr_writer` in `tools/startup/gfx2hlsl.py`: the bound logic is sound. The X thread id is below the group X size (v0 holds SV_GroupThreadID per the compute prologue note near line 1153), so `>> n` with size <= 1 << n clears it. For `(sK << K) + v0` the low K bits of the base are clear, so there is no carry. The nearest-writer search, the v0-unwritten check at the definition, and the scalar-unwritten check between definition and read all look right. Gating (compute, entry block, exec known, no modifiers) matches the sibling rules. I found no correctness problem. - Scope is small and additive: 2 files, no launcher/CI/hook changes. No other open or merged PR covers the same rule. - Dependency: stacked on #2446, which is still OPEN. It should not merge before #2446; the diff against main includes that commit. Commands run locally: - `python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py` -> 0 failed - `git diff --check origin/main...HEAD` -> clean - `python3 tests/docs_links_check.py` and `python3 tests/bidi_controls_check.py` -> OK - Mutation 1: allow size <= 1 << (n+1) -> test fails (3 failed). - Mutation 2: drop the `n <= K` check -> test fails (1 failed). - Mutation 3: drop the "v0 unwritten before the definition" check -> test fails (1 failed). All three mutations are caught, so the tests guard the new rule. No blocking findings. AI-assisted; commands listed were executed locally.
ChazY11 commented 2026-11-25 19:04:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T18:24:03Z.


Ten mutations: six caught, four survive — two of them provably redundant, two genuinely unexercised

Baseline: Readfirstlane uniformity checks: 0 failed, whole startup_gfx2hlsl_* family green.

mutation of tid_shift / its callers result
the v0 single-writer requirement dropped (self.vgpr_writer(0, defined) is not None or …) caught
the uniform-source requirement dropped caught
the n > K bound dropped caught
the earlier size > 1 << n bound dropped caught
both the K bound and the size > 1 << K bound dropped together caught
the v0 operand requirement dropped (d[3] != 'v0') caught
size > 1 << int(d[2], 0) alone (the second group-size check) survived
scalar[1] > 105 → > 106 survived
base != 'v_lshl_add_u32' dropped survived

Two of the survivors are redundant, and I proved that rather than assuming it

The size > 1 << int(d[2], 0) conjunct looks like a second copy of the size > 1 << n check at line 1922. It is not merely redundant by inspection: dropping both bounds is caught, dropping either alone is not — so the pair is a genuine belt-and-braces where n <= K already implies 1 << n <= 1 << K. Two options, both fine: leave it with a comment saying "implied by the line above; kept so a future relaxation of n <= K cannot silently drop the group-size proof", or delete it. What it should not be is an unexplained duplicate that a reader has to re-derive.

The two that are genuinely unexercised

  • scalar[1] > 105 → > 106: no fixture register at the boundary, same as the one I reported on #2459. That constant now appears at gfx2hlsl.py:1934, :2428 and in the shift-add path — three sites whose consistency is only in the reader's head.
  • base != 'v_lshl_add_u32' dropped: this one I expected to be caught, because the docstring's whole argument is that the shift's source must be (uniform << K) + v0. Dropping the form check means any single-writer vD with a matching shift count qualifies. No fixture exercises a program where v0 is written by something else and the shift still looks right, so the conjunct is unasserted. The cheapest case: entry block with v_mov_b32 v0, s5 (a plain copy, not v_lshl_add_u32) followed by v_lshrrev_b32 v1, 5, v0 — the proof must refuse, and today a mutation that accepts it is invisible.

What the caught rows say about the proof

The interesting one is that dropping n > K and size > 1 << K together is caught but neither alone is: that is exactly the situation where two guards overlap, and it means the fixture is testing the property (the group's X size fits below the shift) rather than one particular spelling of it. The v0-writer and uniform-source rows are the two the proof cannot work without, and both are pinned.

AI-assisted; the ten mutations were run locally on Windows 11 build 26300.9974.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7170855392 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2455#issuecomment-7170855392). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T18:24:03Z. --- ## Ten mutations: six caught, four survive — two of them provably redundant, two genuinely unexercised Baseline: `Readfirstlane uniformity checks: 0 failed`, whole `startup_gfx2hlsl_*` family green. | mutation of `tid_shift` / its callers | result | |---|---| | the v0 single-writer requirement dropped (`self.vgpr_writer(0, defined) is not None or …`) | **caught** | | the uniform-source requirement dropped | **caught** | | the `n > K` bound dropped | **caught** | | the earlier `size > 1 << n` bound dropped | **caught** | | **both** the `K` bound and the `size > 1 << K` bound dropped together | **caught** | | the `v0` operand requirement dropped (`d[3] != 'v0'`) | **caught** | | `size > 1 << int(d[2], 0)` alone (the second group-size check) | survived | | `scalar[1] > 105` → `> 106` | survived | | `base != 'v_lshl_add_u32'` dropped | survived | ### Two of the survivors are redundant, and I proved that rather than assuming it The `size > 1 << int(d[2], 0)` conjunct looks like a second copy of the `size > 1 << n` check at line 1922. It is not merely redundant by inspection: dropping **both** bounds is caught, dropping either alone is not — so the pair is a genuine belt-and-braces where `n <= K` already implies `1 << n <= 1 << K`. Two options, both fine: leave it with a comment saying "implied by the line above; kept so a future relaxation of `n <= K` cannot silently drop the group-size proof", or delete it. What it should not be is an unexplained duplicate that a reader has to re-derive. ### The two that are genuinely unexercised - **`scalar[1] > 105` → `> 106`**: no fixture register at the boundary, same as the one I reported on #2459. That constant now appears at `gfx2hlsl.py:1934`, `:2428` and in the shift-add path — three sites whose consistency is only in the reader's head. - **`base != 'v_lshl_add_u32'` dropped**: this one I expected to be caught, because the docstring's whole argument is that the shift's source must be `(uniform << K) + v0`. Dropping the form check means any single-writer `vD` with a matching shift count qualifies. No fixture exercises a program where `v0` is written by something else and the shift still looks right, so the conjunct is unasserted. The cheapest case: entry block with `v_mov_b32 v0, s5` (a plain copy, not `v_lshl_add_u32`) followed by `v_lshrrev_b32 v1, 5, v0` — the proof must refuse, and today a mutation that accepts it is invisible. ### What the caught rows say about the proof The interesting one is that dropping `n > K` **and** `size > 1 << K` together is caught but neither alone is: that is exactly the situation where two guards overlap, and it means the fixture is testing the *property* (the group's X size fits below the shift) rather than one particular spelling of it. The v0-writer and uniform-source rows are the two the proof cannot work without, and both are pinned. AI-assisted; the ten mutations were run locally on Windows 11 build 26300.9974.
tedin7 commented 2026-11-25 21:01:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T20:19:27Z.

Review: COMMENTED


Review of head c30127d3 (comment only). Not mergeable yet: the body says this branch is stacked on #2446, which is still open.

Code (tools/startup/gfx2hlsl.py, Stage.tid_shift and its call site):

  • The rule is sound as written. A right shift by n of the X thread id is 0 when the group X size is at most 1 << n. For (sK << K) + v0 with n <= K and size <= 1 << K, the low K bits of the base are clear, so the sum does not carry and the shift gives sK << (K - n) in every lane.
  • The guards cover what I could think of: v0 must be unwritten before the definition, the scalar must not be written between definition and read (including the 'any' destination case), no modifiers, compute entry block only, known exec.

Tests (tests/startup_gfx2hlsl_readfirstlane_uniform_check.py), run on the exact head:

  • python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: 0 failed.
  • Mutation 1, dropping the size > 1 << n guard: 4 failed.
  • Mutation 2, dropping the n > K guard: 1 failed.
  • git diff --check HEAD~1 HEAD and tests/bidi_controls_check.py: clean.

Notes:

  • This change only widens a uniformity proof. It emits no new lowering, so I did not ask for a D3D12 readback test.
  • The proof depends on the group X size from compute state block A, which compute_entry documents as an unconfirmed hypothesis. If that field is wrong, the proof is wrong with it, and the "88 first refusals" figure is a translator statistic, not an executable result. No game result is claimed here, and I cannot check one.
  • The branch is far behind main, so it needs an update, and the diff should shrink to one commit once #2446 lands. I will not merge until then.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6064372354 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2455#pullrequestreview-6064372354). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T20:19:27Z. **Review:** COMMENTED --- Review of head c30127d3 (comment only). Not mergeable yet: the body says this branch is stacked on #2446, which is still open. Code (`tools/startup/gfx2hlsl.py`, `Stage.tid_shift` and its call site): - The rule is sound as written. A right shift by n of the X thread id is 0 when the group X size is at most `1 << n`. For `(sK << K) + v0` with n <= K and size <= `1 << K`, the low K bits of the base are clear, so the sum does not carry and the shift gives `sK << (K - n)` in every lane. - The guards cover what I could think of: v0 must be unwritten before the definition, the scalar must not be written between definition and read (including the 'any' destination case), no modifiers, compute entry block only, known exec. Tests (`tests/startup_gfx2hlsl_readfirstlane_uniform_check.py`), run on the exact head: - `python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py`: 0 failed. - Mutation 1, dropping the `size > 1 << n` guard: 4 failed. - Mutation 2, dropping the `n > K` guard: 1 failed. - `git diff --check HEAD~1 HEAD` and `tests/bidi_controls_check.py`: clean. Notes: - This change only widens a uniformity proof. It emits no new lowering, so I did not ask for a D3D12 readback test. - The proof depends on the group X size from compute state block A, which `compute_entry` documents as an unconfirmed hypothesis. If that field is wrong, the proof is wrong with it, and the "88 first refusals" figure is a translator statistic, not an executable result. No game result is claimed here, and I cannot check one. - The branch is far behind main, so it needs an update, and the diff should shrink to one commit once #2446 lands. I will not merge until then. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-26 03:02:56 +00:00
tedin7 commented 2026-11-26 03:03:22 +00:00 (Migrated from github.com)

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


Merged current main into this branch (the stacked uniform-load commit landed as a squash, so the old commit conflicted in tools/startup/gfx2hlsl.py and tests/startup_gfx2hlsl_readfirstlane_uniform_check.py). Both conflicts resolved keeping this PR's tid_shift code and tests; the diff against main is now only this PR's commit.

Commands executed:

  • python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py -> 0 failed
  • python3 -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py, ..._compute_check.py, ..._loop_constants_check.py, ..._join_symbols_check.py -> pass
  • Win2025 (x2w_pipe.py win2025 2455): the only failure is tests/startup_gfx2hlsl_scalar_table_arity_check.py (1 failed, "without the guard the short form raises IndexError: MISMATCH 37 of 40"). The same check fails identically on a clean origin/main, so it is not caused by this PR and is the reason the Linux CI job is red.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7180783511 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2455#issuecomment-7180783511). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T02:24:57Z. --- Merged current main into this branch (the stacked uniform-load commit landed as a squash, so the old commit conflicted in `tools/startup/gfx2hlsl.py` and `tests/startup_gfx2hlsl_readfirstlane_uniform_check.py`). Both conflicts resolved keeping this PR's `tid_shift` code and tests; the diff against main is now only this PR's commit. Commands executed: - `python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py` -> 0 failed - `python3 -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py`, `..._compute_check.py`, `..._loop_constants_check.py`, `..._join_symbols_check.py` -> pass - Win2025 (`x2w_pipe.py win2025 2455`): the only failure is `tests/startup_gfx2hlsl_scalar_table_arity_check.py` (1 failed, "without the guard the short form raises IndexError: MISMATCH 37 of 40"). The same check fails identically on a clean `origin/main`, so it is not caused by this PR and is the reason the Linux CI job is red. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-26 03:03:24 +00:00 (Migrated from github.com)

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


Merged current main into this branch (no conflicts, foreach/endforeach balanced at 370/370) so the PR is no longer blocked by the old main failure fixed in #2659. No change to the PR's own code.

Commands executed:

  • git merge origin/main
  • python3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py (0 failed)
  • the four tests/startup_gfx2hlsl_*arity_check.py checks (pass)
  • python3 tests/docs_links_check.py, python3 tests/bidi_controls_check.py (pass)
  • x2w_pipe.py win2025 2455 --patch <merge diff>: Win2025 dev + release build PASS, Windows Python checks ok

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7180972209 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2455#issuecomment-7180972209). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T02:37:24Z. --- Merged current main into this branch (no conflicts, `foreach`/`endforeach` balanced at 370/370) so the PR is no longer blocked by the old main failure fixed in #2659. No change to the PR's own code. Commands executed: - `git merge origin/main` - `python3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py` (0 failed) - the four `tests/startup_gfx2hlsl_*arity_check.py` checks (pass) - `python3 tests/docs_links_check.py`, `python3 tests/bidi_controls_check.py` (pass) - `x2w_pipe.py win2025 2455 --patch <merge diff>`: Win2025 dev + release build PASS, Windows Python checks ok AI-assisted; commands listed were executed locally.
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!2447
No description provided.