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

Closed
github-bot wants to merge 4 commits from gh-pr/36/head into gh-pr/36/base
Owner

@⁠64bitMango on GitHub · 2026-11-26 10:46 UTC · original
64bitMango:whole-wave-sections-split → main · +550 −14 · 6 files · 4 commits · closed without merging

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.

**@⁠64bitMango** on GitHub · 2026-11-26 10:46 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36) `64bitMango:whole-wave-sections-split` → `main` · +550 −14 · 6 files · 4 commits · closed without merging 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.
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:43:27 +00:00
github-bot closed this pull request 2026-11-26 14:43:27 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 11:01 UTC · original · requested changes

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** on GitHub · 2026-11-26 11:01 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#pullrequestreview-6072961739) · **requested changes** 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).
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 11:22 UTC · original · requested changes

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** on GitHub · 2026-11-26 11:22 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#pullrequestreview-6073282645) · **requested changes** 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.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:01 UTC · original · requested changes

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.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:01 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#pullrequestreview-6073790412) · **requested changes** 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.
Author
Owner

@⁠64bitMango on GitHub · 2026-11-26 12:37 UTC · original

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** on GitHub · 2026-11-26 12:37 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#issuecomment-7192234060) 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.
Author
Owner

@⁠64bitMango on GitHub · 2026-11-26 12:38 UTC · original

@⁠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.

**@⁠64bitMango** on GitHub · 2026-11-26 12:38 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#issuecomment-7192274526) @⁠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.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:46 UTC · original · requested changes

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.
**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:46 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#pullrequestreview-6074362970) · **requested changes** 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.
Author
Owner

@⁠64bitMango on GitHub · 2026-11-26 13:17 UTC · original

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.

**@⁠64bitMango** on GitHub · 2026-11-26 13:17 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/36#issuecomment-7193137163) 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

This discussion has been locked. Commenting is limited to contributors.
No description provided.