Shader: translate a whole-wave section that a jump splits with the join's code in each arm #36

Closed
64bitMango wants to merge 4 commits from upstream-pr/36 into main
64bitMango commented 2026-11-26 10:46:50 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T10:46:50Z.


Two reductions a branch chooses between that share their end (cond ? WaveActiveMax(x) : WaveActiveMin(x)) left no block with a whole section, so v_permlanex16_b32 was refused. Such a program is translated once more with each section in one block (ir::whole_sections, up to four rounds), and that translation is taken if it succeeds.

Bounded. Over all rounds the copies may total at most the program's own instruction count (256 for a smaller program), so a joined program is at most twice the original plus 256. A round past that is skipped and logged; earlier rounds are kept.

Logged. Replacing a translation that already succeeded logs at info (digest, stage, rounds, instructions copied); a rescued refusal and a failed retry log at debug.

Measured. On the Wine route, one compute shader that was refused for v_permlanex16_b32 now translates, and no other container of 19,106 changes. Through the native host's path, a 19,105-shader capture gives one translation replaced, no refusals rescued (the unjoined lowering already handles it on 32 lanes), the bound never reached, and the other 19,104 byte-identical to main. No switch: other programs translate as before.

Credit: after darkworldhm/victory-fork-darkworldhm.

Tests: the ported structural test; sections_are_not_joined_past_the_copy_bound; a_reduction_a_branch_picks_gives_each_lane_its_value_on_metal runs the reduction on Metal with 48 of 64 lanes and checks every lane (it fails with the arms swapped, but passes without the join on this 32-lane host; the 64-lane D3D12 route has no GPU test). victory-shader 221 passed, victory-gpu 145 passed; Windows clippy and xtask policy clean. Notes in docs/native-host.md.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6624953105 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T10:46:50Z. --- Two reductions a branch chooses between that share their end (`cond ? WaveActiveMax(x) : WaveActiveMin(x)`) left no block with a whole section, so `v_permlanex16_b32` was refused. Such a program is translated once more with each section in one block (`ir::whole_sections`, up to four rounds), and that translation is taken if it succeeds. **Bounded.** Over all rounds the copies may total at most the program's own instruction count (256 for a smaller program), so a joined program is at most twice the original plus 256. A round past that is skipped and logged; earlier rounds are kept. **Logged.** Replacing a translation that already succeeded logs at info (digest, stage, rounds, instructions copied); a rescued refusal and a failed retry log at debug. **Measured.** On the Wine route, one compute shader that was refused for `v_permlanex16_b32` now translates, and no other container of 19,106 changes. Through the native host's path, a 19,105-shader capture gives one translation replaced, no refusals rescued (the unjoined lowering already handles it on 32 lanes), the bound never reached, and the other 19,104 byte-identical to main. No switch: other programs translate as before. **Credit:** after darkworldhm/victory-fork-darkworldhm. **Tests:** the ported structural test; `sections_are_not_joined_past_the_copy_bound`; `a_reduction_a_branch_picks_gives_each_lane_its_value_on_metal` runs the reduction on Metal with 48 of 64 lanes and checks every lane (it fails with the arms swapped, but passes without the join on this 32-lane host; the 64-lane D3D12 route has no GPU test). victory-shader 221 passed, victory-gpu 145 passed; Windows clippy and `xtask policy` clean. Notes in docs/native-host.md.
LakshanSivakumar commented 2026-11-26 11:01:03 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T11:01:03Z.

Review: CHANGES_REQUESTED


The transformation looks sound. The join's tail runs once on each path, back-edge joins are skipped, and failure falls back to the original translation. Tests, Windows clippy and policy pass merged with current main. Requests:

  1. Cap the duplicated size. Four rounds of copying a large join into several arms can blow up; skip fusing past a limit.
  2. Log the retry. The second translation replaces one that already succeeded whenever a permlane sits outside a section. Please log when that happens, and say how it was checked beyond the one title's containers.
  3. Test the output, not just the structure. The test only checks section counts. Please add a lower/run test that cond ? WaveActiveMax : WaveActiveMin gives the right per-lane value.
  4. Docs. Please add a note to docs/native-host.md (AGENTS.md asks for one).
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6072961739 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#pullrequestreview-6072961739). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T11:01:03Z. **Review:** CHANGES_REQUESTED --- The transformation looks sound. The join's tail runs once on each path, back-edge joins are skipped, and failure falls back to the original translation. Tests, Windows clippy and policy pass merged with current main. Requests: 1. **Cap the duplicated size.** Four rounds of copying a large join into several arms can blow up; skip fusing past a limit. 2. **Log the retry.** The second translation replaces one that already succeeded whenever a permlane sits outside a section. Please log when that happens, and say how it was checked beyond the one title's containers. 3. **Test the output, not just the structure.** The test only checks section counts. Please add a lower/run test that `cond ? WaveActiveMax : WaveActiveMin` gives the right per-lane value. 4. **Docs.** Please add a note to `docs/native-host.md` (AGENTS.md asks for one).
LakshanSivakumar commented 2026-11-26 11:22:24 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T11:22:24Z.

Review: CHANGES_REQUESTED


Please fix the chained-join case first. If A → B → C has one section ending in B and another starting in B and ending in C, both fusions can be picked in one round. The rewrite then redirects A to C and clears C using its original predecessor list, which can erase a reachable restore or store (ir.rs:~394-404). Please add a regression that keeps a visible store in C.

Still open from before:

  • Cap the copied instruction count.
  • Add an opt-in retry log, and a note in docs/native-host.md.
  • Lower and run both min/max arms with per-lane assertions.

Also, please remove the title-derived shader ID from the source and test comments (emit/mod.rs:~482, ~790, ir.rs:~313). This repo keeps nothing taken from the game.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073282645 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#pullrequestreview-6073282645). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T11:22:24Z. **Review:** CHANGES_REQUESTED --- Please fix the chained-join case first. If `A → B → C` has one section ending in `B` and another starting in `B` and ending in `C`, both fusions can be picked in one round. The rewrite then redirects `A` to `C` and clears `C` using its original predecessor list, which can erase a reachable restore or store (`ir.rs:~394-404`). Please add a regression that keeps a visible store in `C`. Still open from before: - Cap the copied instruction count. - Add an opt-in retry log, and a note in `docs/native-host.md`. - Lower and run both min/max arms with per-lane assertions. Also, please remove the title-derived shader ID from the source and test comments (`emit/mod.rs:~482`, `~790`, `ir.rs:~313`). This repo keeps nothing taken from the game.
LakshanSivakumar commented 2026-11-26 12:01:54 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:01:54Z.

Review: CHANGES_REQUESTED


Thanks for bounding the copies and adding the min/max fixture. I reproduced the chained-join bug with a synthetic store in C (A → B → C): the retry succeeds and drops the store from DXIL (ir.rs ~401/411 clears C using its original predecessors). Please fix the predecessor handling and add that regression. Please also make the retry logs opt-in behind a documented VICTORY_* switch (they're info! now, which the app enables by default), and remove the title-derived shader ID from the source (emit/mod.rs ~491/860, ir.rs ~313), the test comment, the PR description, and the landed history.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073790412 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#pullrequestreview-6073790412). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:01:54Z. **Review:** CHANGES_REQUESTED --- Thanks for bounding the copies and adding the min/max fixture. I reproduced the chained-join bug with a synthetic store in C (A → B → C): the retry succeeds and drops the store from DXIL (`ir.rs` ~401/411 clears C using its original predecessors). Please fix the predecessor handling and add that regression. Please also make the retry logs opt-in behind a documented `VICTORY_*` switch (they're `info!` now, which the app enables by default), and remove the title-derived shader ID from the source (`emit/mod.rs` ~491/860, `ir.rs` ~313), the test comment, the PR description, and the landed history.
64bitMango commented 2026-11-26 12:37:01 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T12:37:01Z.


Thanks. All four points:

  1. Cap: over all rounds the copies may total at most the program's own instruction count (256 for smaller programs), so a joined program is at most twice the original plus 256. A round past that is skipped and logged, with earlier rounds kept. sections_are_not_joined_past_the_copy_bound tests it. Across a 19,105-shader capture the bound never triggered.
  2. Log: replacing a translation that already succeeded now logs at info (digest, stage, rounds, instructions copied); a rescued refusal and a failed retry log at debug. Translated through the native host's path, that capture gives one translation replaced, no refusals rescued, and the other 19,104 byte-identical to main. Beyond this title's shaders, the coverage is the synthetic tests.
  3. Output test: a_reduction_a_branch_picks_gives_each_lane_its_value_on_metal runs cond ? WaveActiveMax : WaveActiveMin on Metal with 48 of 64 lanes active and checks every lane; it fails with the arms swapped. Caveat: on this 32-lane host the unjoined lowering is already right, so it also passes without the join. The 64-lane D3D12 route, where the join matters, has no GPU test.
  4. Docs: added a section to docs/native-host.md.

The branch is squashed to one commit.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7192234060 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#issuecomment-7192234060). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T12:37:01Z. --- Thanks. All four points: 1. **Cap:** over all rounds the copies may total at most the program's own instruction count (256 for smaller programs), so a joined program is at most twice the original plus 256. A round past that is skipped and logged, with earlier rounds kept. `sections_are_not_joined_past_the_copy_bound` tests it. Across a 19,105-shader capture the bound never triggered. 2. **Log:** replacing a translation that already succeeded now logs at info (digest, stage, rounds, instructions copied); a rescued refusal and a failed retry log at debug. Translated through the native host's path, that capture gives one translation replaced, no refusals rescued, and the other 19,104 byte-identical to main. Beyond this title's shaders, the coverage is the synthetic tests. 3. **Output test:** `a_reduction_a_branch_picks_gives_each_lane_its_value_on_metal` runs `cond ? WaveActiveMax : WaveActiveMin` on Metal with 48 of 64 lanes active and checks every lane; it fails with the arms swapped. Caveat: on this 32-lane host the unjoined lowering is already right, so it also passes without the join. The 64-lane D3D12 route, where the join matters, has no GPU test. 4. **Docs:** added a section to docs/native-host.md. The branch is squashed to one commit.
64bitMango commented 2026-11-26 12:38:50 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T12:38:50Z.


@LakshanSivakumar ready for another look when you have time. The review points are addressed in the comment above, and the branch is squashed to one commit.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7192274526 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#issuecomment-7192274526). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T12:38:50Z. --- @LakshanSivakumar ready for another look when you have time. The review points are addressed in the comment above, and the branch is squashed to one commit.
LakshanSivakumar commented 2026-11-26 12:46:40 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:46:40Z.

Review: CHANGES_REQUESTED


Thanks for bounding the copies; the Metal test runs fine here. Three things are still open:

  1. The chained join is not fixed. A synthetic A→B→C program (A starts a section, B restores and starts another, C restores and does a buffer_store_dword) gets both (A,B) and (B,C) fused in one round, so A takes B's original tail and jumps to a C that was emptied. Reachable stores go from 1 to 0. Please fuse in dependency order or one fusion per round, and add a regression with a visible store in C. (The Metal test also passes without the join on a 32-lane host, so it doesn't guard this.)
  2. The retry logs are still info! with no switch. Per AGENTS.md, new diagnostics are off by default behind a documented VICTORY_* variable (e.g. VICTORY_SHADER_JOIN_LOG=1). Please also drop "No switch" from the PR body.
  3. The title shader ID is still in source comments (emit/mod.rs: the retry comment and the two_reductions doc; ir.rs). Please replace it with a plain description; squashing doesn't help while it's in the final tree.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6074362970 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#pullrequestreview-6074362970). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:46:40Z. **Review:** CHANGES_REQUESTED --- Thanks for bounding the copies; the Metal test runs fine here. Three things are still open: 1. **The chained join is not fixed.** A synthetic A→B→C program (A starts a section, B restores and starts another, C restores and does a `buffer_store_dword`) gets both (A,B) and (B,C) fused in one round, so A takes B's original tail and jumps to a C that was emptied. Reachable stores go from 1 to 0. Please fuse in dependency order or one fusion per round, and add a regression with a visible store in C. (The Metal test also passes without the join on a 32-lane host, so it doesn't guard this.) 2. **The retry logs are still `info!` with no switch.** Per AGENTS.md, new diagnostics are off by default behind a documented `VICTORY_*` variable (e.g. `VICTORY_SHADER_JOIN_LOG=1`). Please also drop "No switch" from the PR body. 3. **The title shader ID is still in source comments** (`emit/mod.rs`: the retry comment and the `two_reductions` doc; `ir.rs`). Please replace it with a plain description; squashing doesn't help while it's in the final tree.
64bitMango commented 2026-11-26 13:17:28 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T13:17:28Z.


Closing this: I'm continuing this work on my fork, 64bitMango/Native-Victory-macOS, where it's merged with the other open PRs (#33–#37). Thanks for the thorough reviews; the fixes from your latest feedback are included there.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7193137163 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#issuecomment-7193137163). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T13:17:28Z. --- Closing this: I'm continuing this work on my fork, [64bitMango/Native-Victory-macOS](https://github.com/64bitMango/Native-Victory-macOS), where it's merged with the other open PRs (#33–#37). Thanks for the thorough reviews; the fixes from your latest feedback are included there.

Pull request closed

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
LakshanSivakumar/Native-Victory-macOS!36
No description provided.