Shaders/bitcode: prevent folding twin empty latches from the same predecessor #678

Open
github-bot wants to merge 1 commit from github-bot/gh-678 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-25 04:20 UTC · original
AscilCH:fix/shader-bitcode-duplicate-empty-latch-fold → main · +84 −22 · 1 files · 1 commits · open

Summary

Fixes an invalid LLVM/DXIL bitcode emission bug in crates/victory-shader/src/emit/bitcode.rs during empty-latch folding:

  • Module::write() folds empty Br(h) backedge blocks with a single predecessor p into p, previously checking only !succs(*p).contains(&h) against the original CFG.
  • When p ends in CondBr(c, b1, b2) where both b1 and b2 are empty Br(h) blocks supplying distinct values to a PHI node in h, both b1 and b2 folded into Some((p, h)). Both PHI incoming edges then mapped to from(b1) == pos[p] == from(b2), producing invalid LLVM 3.7 / DXIL IR (PHI node has multiple entries for the same basic block with different incoming values).
  • Extracted Module::empty_latch_folds() and guarded folding with && !fold.contains(&Some((*p, h))), ensuring at most one empty latch folds per (p, h) pair while leaving single-latch bitcode output byte-identical (verified by written_bytes_unchanged).

Verification

  • cargo test -p victory-shader --lib --locked emit::bitcode::tests — 6 passed, 0 failed, 1 ignored (including existing written_bytes_unchanged and new twin_empty_latches_from_one_predecessor_do_not_both_fold)
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings — clean (0 warnings)
  • cargo xtask policy — asset policy: 169 files ok

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 04:20 UTC · [original](https://github.com/fxceless/victory-fork/pull/678) `AscilCH:fix/shader-bitcode-duplicate-empty-latch-fold` → `main` · +84 −22 · 1 files · 1 commits · open ### Summary Fixes an invalid LLVM/DXIL bitcode emission bug in `crates/victory-shader/src/emit/bitcode.rs` during empty-latch folding: - `Module::write()` folds empty `Br(h)` backedge blocks with a single predecessor `p` into `p`, previously checking only `!succs(*p).contains(&h)` against the original CFG. - When `p` ends in `CondBr(c, b1, b2)` where *both* `b1` and `b2` are empty `Br(h)` blocks supplying distinct values to a PHI node in `h`, both `b1` and `b2` folded into `Some((p, h))`. Both PHI incoming edges then mapped to `from(b1) == pos[p] == from(b2)`, producing invalid LLVM 3.7 / DXIL IR (`PHI node has multiple entries for the same basic block with different incoming values`). - Extracted `Module::empty_latch_folds()` and guarded folding with `&& !fold.contains(&Some((*p, h)))`, ensuring at most one empty latch folds per `(p, h)` pair while leaving single-latch bitcode output byte-identical (verified by `written_bytes_unchanged`). ### Verification - `cargo test -p victory-shader --lib --locked emit::bitcode::tests` — **6 passed, 0 failed, 1 ignored** (including existing `written_bytes_unchanged` and new `twin_empty_latches_from_one_predecessor_do_not_both_fold`) - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings` — **clean (0 warnings)** - `cargo xtask policy` — **asset policy: 169 files ok** *(AI-assisted; synthetic unit tests only, no game data.)*
In `emit/bitcode.rs`, empty latch folding rewrote any empty `Br(h)` block
with single predecessor `p` to jump directly from `p` to `h`, checking only
`!succs(*p).contains(&h)` against the original CFG. When `p` ended in
`CondBr(c, b1, b2)` and *both* `b1` and `b2` were empty `Br(h)` blocks
feeding distinct incoming values into a PHI in `h`, both blocks folded into
`(p, h)` — emitting an invalid LLVM 3.7 / DXIL PHI with two incoming entries
naming the same predecessor `%p` with different values.

Extract `Module::empty_latch_folds` and require `!fold.contains(&Some((*p, h)))`
so at most one empty latch from a given `(p, h)` pair is folded, keeping
every PHI's incoming predecessor block IDs distinct while preserving
byte-identical output on all single-latch loops.

Add `emit::bitcode::tests::twin_empty_latches_from_one_predecessor_do_not_both_fold`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:48:49 +00:00
Author
Owner

@⁠JRamos1993 on GitHub · 2026-11-25 05:27 UTC · original

Offline check on the retail shader pack: no program of the title reaches this path. The change is neutral for it.

victory translate --validate --list over the title's full shader pack (26,084 programs) on an RTX 5090, main 310c6eb against this head merged onto it:

  • Validated: 25,382 in both, none lost.
  • DXIL: with a local hash on each validated line, 0 programs' DXIL differs (two runs of one build hash the same; the hash catches real changes).
  • Failures: none of the 507 failing programs on main fails on twin empty latches from one predecessor.

So this fix doesn't change anything for this title's shaders as the batch tool translates them. It may still matter for other programs or titles.

AI use: Claude Code with Claude Opus 5.5 ran the pack and wrote this. Nothing from the title is in it.

**@⁠JRamos1993** on GitHub · 2026-11-25 05:27 UTC · [original](https://github.com/fxceless/victory-fork/pull/678#issuecomment-7153794372) **Offline check on the retail shader pack: no program of the title reaches this path. The change is neutral for it.** `victory translate --validate --list` over the title's full shader pack (26,084 programs) on an RTX 5090, `main` `310c6eb` against this head merged onto it: - **Validated:** 25,382 in both, none lost. - **DXIL:** with a local hash on each validated line, **0 programs' DXIL differs** (two runs of one build hash the same; the hash catches real changes). - **Failures:** none of the 507 failing programs on `main` fails on twin empty latches from one predecessor. So this fix doesn't change anything for this title's shaders as the batch tool translates them. It may still matter for other programs or titles. AI use: Claude Code with Claude Opus 5.5 ran the pack and wrote this. Nothing from the title is in it.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/678/head:github-bot/gh-678'
git switch 'github-bot/gh-678'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-678'
git switch 'github-bot/gh-678'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-678'
git switch 'github-bot/gh-678'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-678'
git switch 'main'
git merge --squash 'github-bot/gh-678'
git switch 'main'
git merge --ff-only 'github-bot/gh-678'
git switch 'main'
git merge 'github-bot/gh-678'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.