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

Open
AscilCH wants to merge 1 commit from upstream-pr/678 into main
AscilCH commented 2026-11-25 04:20:55 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T04:20:55Z.


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.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6597135099 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/678). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T04:20:55Z. --- ### 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.)*
JRamos1993 commented 2026-11-25 06:06:38 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by JRamos1993 on 2026-11-25T05:27:30Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7153794372 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/678#issuecomment-7153794372). Originally posted by [JRamos1993](https://github.com/JRamos1993) on 2026-11-25T05:27:30Z. --- **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 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/678:upstream-pr/678'
git switch 'upstream-pr/678'

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/678'
git switch 'upstream-pr/678'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/678'
git switch 'upstream-pr/678'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/678'
git switch 'main'
git merge --squash 'upstream-pr/678'
git switch 'main'
git merge --ff-only 'upstream-pr/678'
git switch 'main'
git merge 'upstream-pr/678'
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
fxceless/victory-fork!678
No description provided.