startup: read S0 before writing D in gfx2hlsl s_*_saveexec #2300

Merged
denn0x merged 2 commits from upstream-pr/2308 into main 2026-11-25 12:02:24 +00:00
denn0x commented 2026-11-25 11:37:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-25T11:37:37Z.


What

RDNA 2 ISA, SOP1 S_AND_SAVEEXEC_B64: D = EXEC; EXEC = S0 & EXEC; SCC = EXEC != 0, where S0 is the value read at issue. S_OR_SAVEEXEC and S_ANDN2_SAVEEXEC work the same way, with | and S0 & ~EXEC.

Compiled code uses the form where the destination is the source, for example s_and_saveexec_b64 s[6:7], s[6:7] after a compare into s[6:7]. tools/startup/gfx2hlsl.py emitted:

s6 = exec;
exec = (exec & s6) & 1; scc = exec != 0;

So the new exec was exec & exec, and every lane entered the branch whatever the compare gave.

With D == S0, S0 is now copied to the scratch word w before D is written. w is already declared in every stage. All other forms emit the same lines as before.

Test

tests/startup_gfx2hlsl_saveexec_same_reg_check.py (new). It is registered in CMake as startup_gfx2hlsl_saveexec_same_reg_controls, in the Python CI job and in AGENTS.md.

  • Cases: s_and/or/andn2_saveexec_b64 and s_and_saveexec_b32 with D == S0.
    • The emitted lines are run for a lane with exec = 1 and S0 = 0 or 1, using the restricted evaluator of startup_gfx2hlsl_bit_alu_check.
    • The new exec, SCC and D (the old exec) are checked against the ISA rule.
  • Controls: with S0 a distinct register (s[20:21] / s20), no scratch copy is made and the result is the same. The old line order is evaluated too and must admit the lane for S0 = 0.
python -B tests/startup_gfx2hlsl_saveexec_same_reg_check.py
saveexec same-register checks: 0 failed

The same check on main without the change fails 6 checks. Two are the exec result: s_and_saveexec_b64 and _b32 admit the lane for S0 = 0. The other four are the missing scratch copy in each D == S0 form. Or and andn2 give the same exec either way, because exec | exec and exec & ~exec do not depend on S0 with exec = 1.

  • Translator checks: every tests/startup_gfx2hlsl_*_check.py, tests/startup_image_*_check.py, startup_translator_check.py and startup_flat_param_check.py passes on this branch.
  • Build: all builds (Ninja Release, MSVC Build Tools 18, Windows 11). Device-free, so there were no GPU runs and no capability skips.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6603608354 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2308). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-25T11:37:37Z. --- ## What RDNA 2 ISA, SOP1 `S_AND_SAVEEXEC_B64`: `D = EXEC; EXEC = S0 & EXEC; SCC = EXEC != 0`, where S0 is the value read at issue. `S_OR_SAVEEXEC` and `S_ANDN2_SAVEEXEC` work the same way, with `|` and `S0 & ~EXEC`. Compiled code uses the form where the destination is the source, for example `s_and_saveexec_b64 s[6:7], s[6:7]` after a compare into `s[6:7]`. `tools/startup/gfx2hlsl.py` emitted: ``` s6 = exec; exec = (exec & s6) & 1; scc = exec != 0; ``` So the new exec was `exec & exec`, and every lane entered the branch whatever the compare gave. With D == S0, S0 is now copied to the scratch word `w` before D is written. `w` is already declared in every stage. All other forms emit the same lines as before. ## Test `tests/startup_gfx2hlsl_saveexec_same_reg_check.py` (new). It is registered in CMake as `startup_gfx2hlsl_saveexec_same_reg_controls`, in the Python CI job and in AGENTS.md. - **Cases:** `s_and/or/andn2_saveexec_b64` and `s_and_saveexec_b32` with D == S0. - The emitted lines are run for a lane with exec = 1 and S0 = 0 or 1, using the restricted evaluator of `startup_gfx2hlsl_bit_alu_check`. - The new exec, SCC and D (the old exec) are checked against the ISA rule. - **Controls:** with S0 a distinct register (`s[20:21]` / `s20`), no scratch copy is made and the result is the same. The old line order is evaluated too and must admit the lane for S0 = 0. ``` python -B tests/startup_gfx2hlsl_saveexec_same_reg_check.py saveexec same-register checks: 0 failed ``` The same check on `main` without the change fails 6 checks. Two are the exec result: `s_and_saveexec_b64` and `_b32` admit the lane for S0 = 0. The other four are the missing scratch copy in each D == S0 form. Or and andn2 give the same exec either way, because `exec | exec` and `exec & ~exec` do not depend on S0 with exec = 1. - **Translator checks:** every `tests/startup_gfx2hlsl_*_check.py`, `tests/startup_image_*_check.py`, `startup_translator_check.py` and `startup_flat_param_check.py` passes on this branch. - **Build:** `all` builds (Ninja Release, MSVC Build Tools 18, Windows 11). Device-free, so there were no GPU runs and no capability skips.
tedin7 commented 2026-11-25 12:01:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:41:02Z.

Review: COMMENTED


Review of head 60f8276a97 (Linux only, no Windows run needed: the change is a text-emission order fix covered by the restricted evaluator).

Verdict: logic is correct. One blocker: the PR is CONFLICTING and needs a rebase.

P1 (rebase)

  • tools/startup/gfx2hlsl.py, op_s_and_saveexec_b64 conflicts with main (#2305 and earlier refactors). Main now ends the op with self.set_exec_scc(text.format(self.u(src))) (main line ~1794) instead of the inline exec = (...) & 1; scc = exec != 0; plus scc_uniform reset that this PR edits. After the rebase, keep the new w = S0; copy and pass value to set_exec_scc. Main still has the original bug, so the fix is still needed.
  • After the rebase, re-run the new check: it evaluates emitted lines, so check it still matches the set_exec_scc output.

What I checked

  • Semantics: S0 is read at issue, before D is written (RDNA 2 ISA, SOP1 S_AND_SAVEEXEC_B64). Copying S0 to w when D == S0 is right. w is declared in every stage prologue (uint ... w = 0), so no new declaration is needed. Other forms emit unchanged lines.
  • Or/andn2 are unaffected in exec value, as the PR says; the copy is harmless there.
  • Title is neutral. No model names or probe mentions. Diff touches CI, AGENTS.md and CMake registration consistently.
  • CI: no checks reported on the branch (conflict blocks them).

Nits (P2)

  • None blocking. (P2 count 0.)

Commands run locally

  • git diff --stat origin/main...HEAD: 5 files, +116 -2
  • git merge-tree --write-tree origin/main HEAD: CONFLICT in tools/startup/gfx2hlsl.py
  • python3 -B tests/startup_gfx2hlsl_saveexec_same_reg_check.py: saveexec same-register checks: 0 failed
  • python3 -B tests/startup_gfx2hlsl_bit_alu_check.py: pass
  • python3 tests/docs_links_check.py: 18 files, 446 links ok
  • python3 tests/bidi_controls_check.py: no bidirectional controls
  • git diff --check origin/main...HEAD: clean
  • Mutation: changed the D == S0 condition (src_reg[1] == d to d + 99) so the copy is skipped; the new check reported 6 failed. Reverted with git checkout -- ..

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6056946148 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2308#pullrequestreview-6056946148). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T11:41:02Z. **Review:** COMMENTED --- Review of head 60f8276a97fb27d24859917f12f20a6c4b31e712 (Linux only, no Windows run needed: the change is a text-emission order fix covered by the restricted evaluator). **Verdict: logic is correct. One blocker: the PR is CONFLICTING and needs a rebase.** **P1 (rebase)** - `tools/startup/gfx2hlsl.py`, `op_s_and_saveexec_b64` conflicts with main (#2305 and earlier refactors). Main now ends the op with `self.set_exec_scc(text.format(self.u(src)))` (main line ~1794) instead of the inline `exec = (...) & 1; scc = exec != 0;` plus `scc_uniform` reset that this PR edits. After the rebase, keep the new `w = S0;` copy and pass `value` to `set_exec_scc`. Main still has the original bug, so the fix is still needed. - After the rebase, re-run the new check: it evaluates emitted lines, so check it still matches the `set_exec_scc` output. **What I checked** - Semantics: S0 is read at issue, before D is written (RDNA 2 ISA, SOP1 `S_AND_SAVEEXEC_B64`). Copying S0 to `w` when D == S0 is right. `w` is declared in every stage prologue (`uint ... w = 0`), so no new declaration is needed. Other forms emit unchanged lines. - Or/andn2 are unaffected in exec value, as the PR says; the copy is harmless there. - Title is neutral. No model names or probe mentions. Diff touches CI, AGENTS.md and CMake registration consistently. - CI: no checks reported on the branch (conflict blocks them). **Nits (P2)** - None blocking. (P2 count 0.) **Commands run locally** - `git diff --stat origin/main...HEAD`: 5 files, +116 -2 - `git merge-tree --write-tree origin/main HEAD`: CONFLICT in tools/startup/gfx2hlsl.py - `python3 -B tests/startup_gfx2hlsl_saveexec_same_reg_check.py`: `saveexec same-register checks: 0 failed` - `python3 -B tests/startup_gfx2hlsl_bit_alu_check.py`: pass - `python3 tests/docs_links_check.py`: 18 files, 446 links ok - `python3 tests/bidi_controls_check.py`: no bidirectional controls - `git diff --check origin/main...HEAD`: clean - Mutation: changed the D == S0 condition (`src_reg[1] == d` to `d + 99`) so the copy is skipped; the new check reported `6 failed`. Reverted with `git checkout -- .`. AI-assisted; commands listed were executed locally.
RavenCurseBurg commented 2026-11-25 12:01:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RavenCurseBurg on 2026-11-25T11:40:34Z.


I'll take an independent compute readback for the overlapping SAVEEXEC forms. With one active hardware lane per source-built workgroup, four groups can cover both input EXEC bits and both source bits. In particular, an incoming EXEC of zero and S0 of one can distinguish OR and ANDN2 from reading D after it was overwritten. I'll run the same cases and the previous instruction order on WARP and RTX 4060, and share the evidence here. The source fix remains in this PR.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7161353379 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2308#issuecomment-7161353379). Originally posted by [RavenCurseBurg](https://github.com/RavenCurseBurg) on 2026-11-25T11:40:34Z. --- I'll take an independent compute readback for the overlapping SAVEEXEC forms. With one active hardware lane per source-built workgroup, four groups can cover both input EXEC bits and both source bits. In particular, an incoming EXEC of zero and S0 of one can distinguish OR and ANDN2 from reading D after it was overwritten. I'll run the same cases and the previous instruction order on WARP and RTX 4060, and share the evidence here. The source fix remains in this PR.
RavenCurseBurg commented 2026-11-25 12:01:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RavenCurseBurg on 2026-11-25T11:46:31Z.


I ran the LLVM 15.0.7 source-built SAVEEXEC cases on WARP and a desktop RTX 4060 (32.0.15.9579, Windows 26100.9711; local debug layer unavailable). Each of four single-thread groups covers one (old EXEC, S0) pair: (0,0), (0,1), (1,0), (1,1). The stored word contains saved EXEC, SCC and new EXEC in bits 2, 1 and 0. This makes an inactive initial lane observable after re-enabling it for the output store.

Form Four expected words First mismatch with the old overlapping-operand lowering
AND, b32 and b64 0, 0, 4, 7 Group 2: expected 4, got 7
OR, b32 and b64 0, 3, 7, 7 Group 1: expected 3, got 0
ANDN2, b64 0, 3, 4, 4 Group 1: expected 3, got 0

At 60f8276a, all five forms matched on both adapters with both overlapping and distinct operands. I also combined them in a test-only commit for the existing compute CTest. With your translator, both devices matched all 320 words of this 648-byte fixture. On main 209938b0, each device returned exactly five mismatches, one per alias case. The distinct-register cases and all 280 guard words stayed correct.

The test-only branch is ready for a follow-up once the lowering lands. The reference for the flag and source order is AMD RDNA 2 ISA, section 12.3.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7161482758 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2308#issuecomment-7161482758). Originally posted by [RavenCurseBurg](https://github.com/RavenCurseBurg) on 2026-11-25T11:46:31Z. --- I ran the LLVM 15.0.7 source-built SAVEEXEC cases on WARP and a desktop RTX 4060 (32.0.15.9579, Windows 26100.9711; local debug layer unavailable). Each of four single-thread groups covers one (old EXEC, S0) pair: (0,0), (0,1), (1,0), (1,1). The stored word contains saved EXEC, SCC and new EXEC in bits 2, 1 and 0. This makes an inactive initial lane observable after re-enabling it for the output store. | Form | Four expected words | First mismatch with the old overlapping-operand lowering | | --- | --- | --- | | AND, b32 and b64 | 0, 0, 4, 7 | Group 2: expected 4, got 7 | | OR, b32 and b64 | 0, 3, 7, 7 | Group 1: expected 3, got 0 | | ANDN2, b64 | 0, 3, 4, 4 | Group 1: expected 3, got 0 | At `60f8276a`, all five forms matched on both adapters with both overlapping and distinct operands. I also combined them in a [test-only commit](https://github.com/RavenCurseBurg/Xbox2Windows/commit/ed11438341cbc892f27774da9910831f3ae974c0) for the existing compute CTest. With your translator, both devices matched all 320 words of this 648-byte fixture. On main `209938b0`, each device returned exactly five mismatches, one per alias case. The distinct-register cases and all 280 guard words stayed correct. The test-only branch is ready for a follow-up once the lowering lands. The reference for the flag and source order is [AMD RDNA 2 ISA, section 12.3](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf).
denn0x commented 2026-11-25 12:01:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-25T11:51:22Z.


Rebased onto 5074371f. op_s_and_saveexec_b64 now keeps the w = S0; copy and passes value to set_exec_scc. Every other line is unchanged.

After the rebase:

  • python -B tests/startup_gfx2hlsl_saveexec_same_reg_check.py gives saveexec same-register checks: 0 failed. It still evaluates the lines set_exec_scc emits.
  • Every tests/startup_gfx2hlsl_*_check.py, tests/startup_image_*_check.py, startup_translator_check.py and startup_flat_param_check.py passes.
  • all builds (Ninja Release, MSVC Build Tools 18).

Thanks for the WARP and hardware readback. It matches the cases the check covers, and the test-only compute fixture makes sense as a follow-up once this lands.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7161589018 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2308#issuecomment-7161589018). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-25T11:51:22Z. --- Rebased onto `5074371f`. `op_s_and_saveexec_b64` now keeps the `w = S0;` copy and passes `value` to `set_exec_scc`. Every other line is unchanged. After the rebase: - `python -B tests/startup_gfx2hlsl_saveexec_same_reg_check.py` gives `saveexec same-register checks: 0 failed`. It still evaluates the lines `set_exec_scc` emits. - Every `tests/startup_gfx2hlsl_*_check.py`, `tests/startup_image_*_check.py`, `startup_translator_check.py` and `startup_flat_param_check.py` passes. - `all` builds (Ninja Release, MSVC Build Tools 18). Thanks for the WARP and hardware readback. It matches the cases the check covers, and the test-only compute fixture makes sense as a follow-up once this lands.
denn0x commented 2026-11-25 12:01:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-25T11:58:41Z.


Rebased again onto current main after #2310 merged. The only conflict was the adjacent test registration lines in CMake, CI and AGENTS.md, and both entries are kept. The saveexec and compare-destination checks both report 0 failed, and all builds.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7161756459 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2308#issuecomment-7161756459). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-25T11:58:41Z. --- Rebased again onto current main after #2310 merged. The only conflict was the adjacent test registration lines in CMake, CI and AGENTS.md, and both entries are kept. The saveexec and compare-destination checks both report 0 failed, and `all` builds.
denn0x commented 2026-11-25 12:01:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-25T11:59:38Z.


Correction to my last comment: my push was refused because you had already updated the branch (36019ef9). I have reset to your head and not pushed anything over it. Your version is what I would have pushed, with the same conflict resolution, plus the cache revision bump to x2w-startup-shader-12, which I had missed. Thanks.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7161776818 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2308#issuecomment-7161776818). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-25T11:59:38Z. --- Correction to my last comment: my push was refused because you had already updated the branch (36019ef9). I have reset to your head and not pushed anything over it. Your version is what I would have pushed, with the same conflict resolution, plus the cache revision bump to x2w-startup-shader-12, which I had missed. Thanks.
upstream-sync closed this pull request 2026-11-25 13:00:40 +00:00
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!2300
No description provided.