Shaders: track SGPR origins across cancelling s_xor_b32/b64 swaps (#815) #2385

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/2387 into main
fuzeofficlal commented 2026-11-25 12:29:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T12:29:34Z.


Summary

Resolves cancelling s_xor_b32 and s_xor_b64 chains in Tracer::propagate_copies (crates/victory-shader/src/resource.rs) so temporary-free three-XOR SGPR exchanges (a ^= b; b ^= a; a ^= b, including swaps through vcc_lo/vcc_hi and 64-bit SGPR pairs) preserve root-parameter and descriptor origins across loops and control-flow joins.

In the native compute corpus measurements reported in #815 (and #1481 Class C), register permutations emitted via three-XOR exchanges caused root pointers and descriptor words to lose their SSA origin and fail with "an address not from a root parameter" (418 -> 339 -> 64 when including vcc_lo/vcc_hi) or "an address from a phi" (193 -> 24).

Details

  • XOR over 32-bit words is exact GF(2) arithmetic (x ^ x == 0, x ^ 0 == x): Tracer::xor_cancelled collects XOR operands with bounded expansion (MAX_XOR_STEPS = 32, expanding the latest s_xor_b32/s_xor_b64 definition in (rpo[block], inst_index) order so local swaps cancel cleanly without oscillating across loop phis).
  • Cancels paired Values and folds constant terms (NULL, 32-bit inline/literal constants, and sign-extended 64-bit inline integers 128..=208):
    • 0 remaining Values -> Some(Moved::Const(const_acc))
    • 1 remaining Value and const_acc == 0 -> Some(Moved::Value(v))
    • Otherwise None (any non-cancelling XOR remains opaque).

Verification

  • resource::tests::three_xor_swap_tracks_32bit_64bit_and_vcc_exchanges & three_xor_swap_preserves_phi_origins_and_non_cancelling_xor_stays_opaque: cover 32-bit SGPR swaps, 64-bit SGPR pair swaps, vcc_lo (code = 106) swaps, join/loop phi convergence, and negative non-cancelling/mutated-descriptor checks.
  • emit::valu_tests::three_xor_swap_tracks_table_through_vcc_and_64bit_pair: validates DXIL with dxil.dll and executes on both hardware GPU and WARP.
  • cargo clippy -p victory-shader --lib --tests --all-features --locked -- -D warnings & cargo xtask policy pass.

Refs #815, #1481

<!-- greyfinch-sync:Marvisak/VIctory:issue:6604497816 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2387). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T12:29:34Z. --- ### Summary Resolves cancelling `s_xor_b32` and `s_xor_b64` chains in `Tracer::propagate_copies` (`crates/victory-shader/src/resource.rs`) so temporary-free three-XOR SGPR exchanges (`a ^= b; b ^= a; a ^= b`, including swaps through `vcc_lo`/`vcc_hi` and 64-bit SGPR pairs) preserve root-parameter and descriptor origins across loops and control-flow joins. In the native compute corpus measurements reported in #815 (and #1481 Class C), register permutations emitted via three-XOR exchanges caused root pointers and descriptor words to lose their SSA origin and fail with `"an address not from a root parameter"` (`418 -> 339 -> 64` when including `vcc_lo`/`vcc_hi`) or `"an address from a phi"` (`193 -> 24`). ### Details - XOR over 32-bit words is exact GF(2) arithmetic (`x ^ x == 0`, `x ^ 0 == x`): `Tracer::xor_cancelled` collects XOR operands with bounded expansion (`MAX_XOR_STEPS = 32`, expanding the latest `s_xor_b32`/`s_xor_b64` definition in `(rpo[block], inst_index)` order so local swaps cancel cleanly without oscillating across loop phis). - Cancels paired `Value`s and folds constant terms (`NULL`, 32-bit inline/literal constants, and sign-extended 64-bit inline integers `128..=208`): - 0 remaining `Value`s -> `Some(Moved::Const(const_acc))` - 1 remaining `Value` and `const_acc == 0` -> `Some(Moved::Value(v))` - Otherwise `None` (any non-cancelling XOR remains opaque). ### Verification - `resource::tests::three_xor_swap_tracks_32bit_64bit_and_vcc_exchanges` & `three_xor_swap_preserves_phi_origins_and_non_cancelling_xor_stays_opaque`: cover 32-bit SGPR swaps, 64-bit SGPR pair swaps, `vcc_lo` (`code = 106`) swaps, join/loop phi convergence, and negative non-cancelling/mutated-descriptor checks. - `emit::valu_tests::three_xor_swap_tracks_table_through_vcc_and_64bit_pair`: validates DXIL with `dxil.dll` and executes on both hardware GPU and `WARP`. - `cargo clippy -p victory-shader --lib --tests --all-features --locked -- -D warnings` & `cargo xtask policy` pass. Refs #815, #1481
catminou4 commented 2026-11-25 13:02:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T12:41:45Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 146 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — SGPR origins tracked across cancelling s_xor_b32/b64 swaps, +1 test.

<!-- greyfinch-sync:Marvisak/VIctory:review:6057785584 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2387#pullrequestreview-6057785584). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T12:41:45Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 146 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — SGPR origins tracked across cancelling s_xor_b32/b64 swaps, +1 test.
welisonzika14-jpg commented 2026-11-25 14:07:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by welisonzika14-jpg on 2026-11-25T13:08:10Z.


First run on NVIDIA hardware here, as far as I can see. RTX 5070 (Blackwell, driver 621.24), Ryzen 7 5700X, Windows 11, Rust 1.98.1 MSVC, no game files. Head 87317be merged onto main 940d32f, cargo test -p victory-gpu -p victory-shader --lib --no-fail-fast.

The PR's new test(s) pass on this card: emit::valu_tests::three_xor_swap_tracks_table_through_vcc_and_64bit_pair, resource::tests::three_xor_swap_tracks_32bit_64bit_and_vcc_exchanges, resource::tests::three_xor_swap_preserves_phi_origins_and_non_cancelling_xor_stays_opaque. The rest is the same as main on this card: the 5 known NVIDIA failures from #1744 (2 view heap cap tests in victory-gpu, 3 in victory-shader), nothing new.

One run, unit tests only, title not run.

Written with AI help (Claude/Kiro); the numbers are from runs on my machine.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163341255 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2387#issuecomment-7163341255). Originally posted by [welisonzika14-jpg](https://github.com/welisonzika14-jpg) on 2026-11-25T13:08:10Z. --- First run on NVIDIA hardware here, as far as I can see. RTX 5070 (Blackwell, driver 621.24), Ryzen 7 5700X, Windows 11, Rust 1.98.1 MSVC, no game files. Head `87317be` merged onto `main` `940d32f`, `cargo test -p victory-gpu -p victory-shader --lib --no-fail-fast`. The PR's new test(s) pass on this card: `emit::valu_tests::three_xor_swap_tracks_table_through_vcc_and_64bit_pair`, `resource::tests::three_xor_swap_tracks_32bit_64bit_and_vcc_exchanges`, `resource::tests::three_xor_swap_preserves_phi_origins_and_non_cancelling_xor_stays_opaque`. The rest is the same as `main` on this card: the 5 known NVIDIA failures from #1744 (2 view heap cap tests in `victory-gpu`, 3 in `victory-shader`), nothing new. One run, unit tests only, title not run. Written with AI help (Claude/Kiro); the numbers are from runs on my machine.
Marvisak commented 2026-11-25 15:04:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T14:59:03Z.


Automated triage for the maintainers.

Merge candidate. Tracer::xor_cancelled (crates/victory-shader/src/resource.rs) lets copy propagation see through three-XOR register exchanges (a ^= b; b ^= a; a ^= b, also via vcc_lo and 64-bit pairs). It expands s_xor_b32/s_xor_b64 definitions, at most 32 steps, cancels equal SSA values pairwise and folds constants. It yields an origin only when exactly one value (with zero constant) or only a constant remains, else nothing. Cancelling identical SSA values is exact, so this cannot invent an origin. Phis are not expanded. The 64-bit inline integers are sign-extended per half, and wide literals and float inline constants are refused. Tests cover 32/64-bit and VCC swaps, phi joins and non-cancelling XORs staying opaque, plus a GPU run on hardware and WARP.

CI: check passing. A contributor reports the new tests passing on an RTX 5070.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166120084 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2387#issuecomment-7166120084). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T14:59:03Z. --- <!-- victory-triage v1 head=87317be84f75116629b015945a335335c7da3880 verdict=merge-candidate at=2026-11-25T14:58:48Z --> Automated triage for the maintainers. **Merge candidate.** `Tracer::xor_cancelled` (`crates/victory-shader/src/resource.rs`) lets copy propagation see through three-XOR register exchanges (`a ^= b; b ^= a; a ^= b`, also via `vcc_lo` and 64-bit pairs). It expands `s_xor_b32`/`s_xor_b64` definitions, at most 32 steps, cancels equal SSA values pairwise and folds constants. It yields an origin only when exactly one value (with zero constant) or only a constant remains, else nothing. Cancelling identical SSA values is exact, so this cannot invent an origin. Phis are not expanded. The 64-bit inline integers are sign-extended per half, and wide literals and float inline constants are refused. Tests cover 32/64-bit and VCC swaps, phi joins and non-cancelling XORs staying opaque, plus a GPU run on hardware and WARP. CI: `check` passing. A contributor reports the new tests passing on an RTX 5070. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2387:upstream-pr/2387'
git switch 'upstream-pr/2387'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2387'
git switch 'upstream-pr/2387'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2387'
git switch 'upstream-pr/2387'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2387'
git switch 'main'
git merge --squash 'upstream-pr/2387'
git switch 'main'
git merge --ff-only 'upstream-pr/2387'
git switch 'main'
git merge 'upstream-pr/2387'
git push origin 'main'
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
Marvisak/VIctory!2385
No description provided.