Shaders: fix duplicate latch folding, unreachable phi edges, and replace_const caching in DXIL bitcode #110

Open
hamdog0203 wants to merge 1 commit from upstream-pr/110 into main
hamdog0203 commented 2026-11-25 13:06:09 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by hamdog0203 on 2026-11-25T12:07:29Z.


Summary

Fixes three correctness bugs in the DXIL bitcode module/emitter (crates/victory-shader/src/emit/bitcode.rs), adapted from Marvisak/VIctory#1145 for ztrx:

  1. Duplicate empty-latch folding (Module::write): When two distinct empty latch blocks branch back to the same loop header from the same predecessor (e.g. a CondBr whose two arms carry different incoming phi values into the header), folding both latches mapped both back-edges onto the same predecessor block ID in the emitted PHI, causing dxil.dll validation to fail with PHI node has multiple entries for the same basic block with different incoming values!. Folding is now guarded so fold[*p].is_none(), fold[h].is_none(), and folded_edges.insert((*p, h)) prevent chained or duplicate (predecessor, header) folds.
  2. Unreachable blocks and unreachable PHI incoming edges (Module::reachable, Module::live, Module::write, census::census): live previously seeded instructions from unreachable blocks and followed all Op::Phi incoming values, while Module::write emitted PHI entries for unreachable predecessors (whose value IDs were unassigned u32::MAX, panicking with use of a void value). reachable() now computes reachability and reverse postorder once (reused by both Module::write and census::census), live(&seen) skips unreachable blocks and unreachable phi edges, and Module::write omits unreachable incoming edges when writing Op::Phi. (This fixes raw/unoptimized emission under VICTORY_SHADER_OPT=0 as well as any module reaching write() with dead predecessors.)
  3. Stale const_ids cache entry in Module::replace_const: Mutating self.consts[o] left the old constant value mapped to o in self.const_ids, so subsequent calls requesting the original constant could receive the mutated constant ID. replace_const now removes the previous cache entry when it still points to o.

Negative Control (unpatched main 90b529f)

Verified in a separate detached worktree on 90b529f with only the new tests applied:

  • replace_const_evicts_old_cache_key: failed (assertion left != right failed: left: Const(0), right: Const(0)).
  • two_empty_latches_to_same_header_validate: failed DXIL validation (PHI node has multiple entries for the same basic block with different incoming values!).
  • phi_with_unreachable_predecessor_validates: panicked in bitcode.rs:761 (use of a void value).

Verification (at fa28c62)

Environment: Windows 11 x64 (26200.9974), NVIDIA GeForce RTX 4060 (8 GB, Wave32, driver 621.35), Microsoft WARP, Windows SDK dxil.dll (10.0.26100.0), rustc 1.98.1.

  • cargo test --locked -p victory-shader --lib -- replace_const_evicts_old_cache_key two_empty_latches_to_same_header_validate phi_with_unreachable_predecessor_validates written_bytes_unchanged worklist_liveness_matches_passes unread_phis_are_dropped --nocapture: 6 passed, 0 failed (both finish_with(0) and default finish() validated via dxil.dll and created compute pipelines on both RTX 4060 and WARP with no skips; written_bytes_unchanged confirms byte-identical output on existing valid shaders).
  • cargo clippy --workspace --all-targets --locked -- -D warnings: passed (0 warnings).
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passed (0 warnings).
  • cargo test --workspace --locked: all crates passed with 0 failures (victory 1, victory_core 11, victory_formats 24 / 4 ignored, victory_game 2, victory_gpu 110 / 1 ignored, victory_hle 95 + no_dll_planting 1, victory_loader 24 + load_probe 2 + title_dlls 1, victory_pe 7, victory_pipeline 4, victory_shader 213 / 2 ignored, xtask 4 including repository_passes_policy).
  • .githooks/pre-commit: passed (asset policy: 3 files ok).
<!-- greyfinch-sync:Zettrox/ztrx:issue:6604112111 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/110). Originally posted by [hamdog0203](https://github.com/hamdog0203) on 2026-11-25T12:07:29Z. --- ## Summary Fixes three correctness bugs in the DXIL bitcode module/emitter (`crates/victory-shader/src/emit/bitcode.rs`), adapted from `Marvisak/VIctory#1145` for `ztrx`: 1. **Duplicate empty-latch folding (`Module::write`):** When two distinct empty latch blocks branch back to the same loop header from the same predecessor (e.g. a `CondBr` whose two arms carry different incoming phi values into the header), folding both latches mapped both back-edges onto the same predecessor block ID in the emitted `PHI`, causing `dxil.dll` validation to fail with `PHI node has multiple entries for the same basic block with different incoming values!`. Folding is now guarded so `fold[*p].is_none()`, `fold[h].is_none()`, and `folded_edges.insert((*p, h))` prevent chained or duplicate `(predecessor, header)` folds. 2. **Unreachable blocks and unreachable `PHI` incoming edges (`Module::reachable`, `Module::live`, `Module::write`, `census::census`):** `live` previously seeded instructions from unreachable blocks and followed all `Op::Phi` incoming values, while `Module::write` emitted `PHI` entries for unreachable predecessors (whose value IDs were unassigned `u32::MAX`, panicking with `use of a void value`). `reachable()` now computes reachability and reverse postorder once (reused by both `Module::write` and `census::census`), `live(&seen)` skips unreachable blocks and unreachable phi edges, and `Module::write` omits unreachable incoming edges when writing `Op::Phi`. (This fixes raw/unoptimized emission under `VICTORY_SHADER_OPT=0` as well as any module reaching `write()` with dead predecessors.) 3. **Stale `const_ids` cache entry in `Module::replace_const`:** Mutating `self.consts[o]` left the old constant value mapped to `o` in `self.const_ids`, so subsequent calls requesting the original constant could receive the mutated constant ID. `replace_const` now removes the previous cache entry when it still points to `o`. ## Negative Control (unpatched `main` `90b529f`) Verified in a separate detached worktree on `90b529f` with only the new tests applied: - `replace_const_evicts_old_cache_key`: failed (`assertion left != right failed: left: Const(0), right: Const(0)`). - `two_empty_latches_to_same_header_validate`: failed DXIL validation (`PHI node has multiple entries for the same basic block with different incoming values!`). - `phi_with_unreachable_predecessor_validates`: panicked in `bitcode.rs:761` (`use of a void value`). ## Verification (at `fa28c62`) Environment: Windows 11 x64 (`26200.9974`), NVIDIA GeForce RTX 4060 (8 GB, Wave32, driver `621.35`), Microsoft WARP, Windows SDK `dxil.dll` (`10.0.26100.0`), `rustc 1.98.1`. - `cargo test --locked -p victory-shader --lib -- replace_const_evicts_old_cache_key two_empty_latches_to_same_header_validate phi_with_unreachable_predecessor_validates written_bytes_unchanged worklist_liveness_matches_passes unread_phis_are_dropped --nocapture`: 6 passed, 0 failed (both `finish_with(0)` and default `finish()` validated via `dxil.dll` and created compute pipelines on both RTX 4060 and WARP with no skips; `written_bytes_unchanged` confirms byte-identical output on existing valid shaders). - `cargo clippy --workspace --all-targets --locked -- -D warnings`: passed (0 warnings). - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passed (0 warnings). - `cargo test --workspace --locked`: all crates passed with 0 failures (`victory` 1, `victory_core` 11, `victory_formats` 24 / 4 ignored, `victory_game` 2, `victory_gpu` 110 / 1 ignored, `victory_hle` 95 + `no_dll_planting` 1, `victory_loader` 24 + `load_probe` 2 + `title_dlls` 1, `victory_pe` 7, `victory_pipeline` 4, `victory_shader` 213 / 2 ignored, `xtask` 4 including `repository_passes_policy`). - `.githooks/pre-commit`: passed (`asset policy: 3 files ok`).
Aniseto commented 2026-11-25 15:08:47 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by Aniseto on 2026-11-25T15:07:16Z.


Ran the DXIL bitcode tests at fa28c62 (clean PR head on main 90b529f):

System

  • GPU: NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB), driver 32.0.16.2124
  • CPU: Intel Core i5-12400F, 64 GB RAM
  • OS: Windows 11 Pro build 26200 x64
  • Toolchain: Rust 1.98.1 (pinned by rust-toolchain.toml), MSVC 14.44.35207, DXIL validator 1.9.2609.5 (VICTORY_DXIL), debug build

Results

  • cargo test --locked -p victory-shader --lib -- replace_const_evicts_old_cache_key two_empty_latches_to_same_header_validate phi_with_unreachable_predecessor_validates written_bytes_unchanged worklist_liveness_matches_passes unread_phis_are_dropped --nocapture: 6 passed, 0 failed, 0 ignored, 209 filtered out (exit 0)
  • cargo test --locked -p victory-shader --lib -- --test-threads=1: 213 passed, 0 failed, 2 ignored (exit 0)

What this adds
The thread already has the author's RTX 4060 (Ampere) run with the Windows SDK dxil.dll 10.0.26100.0. This adds a Blackwell wave32 part with DXIL validator 1.9.2609.5: the two shader tests that previously failed DXIL validation and the cache test pass here, and written_bytes_unchanged still passes.

Not tested
The title's shader pack, live pipeline creation, retail runs, VICTORY_SHADER_OPT=0 raw emission, a WARP control lane, the console compiler's DXC, release profile, AMD and Intel GPUs.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.

<!-- greyfinch-sync:Zettrox/ztrx:comment:7166334585 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/110#issuecomment-7166334585). Originally posted by [Aniseto](https://github.com/Aniseto) on 2026-11-25T15:07:16Z. --- Ran the DXIL bitcode tests at `fa28c62` (clean PR head on `main` `90b529f`): **System** - GPU: NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB), driver 32.0.16.2124 - CPU: Intel Core i5-12400F, 64 GB RAM - OS: Windows 11 Pro build 26200 x64 - Toolchain: Rust 1.98.1 (pinned by `rust-toolchain.toml`), MSVC 14.44.35207, DXIL validator 1.9.2609.5 (`VICTORY_DXIL`), debug build **Results** - `cargo test --locked -p victory-shader --lib -- replace_const_evicts_old_cache_key two_empty_latches_to_same_header_validate phi_with_unreachable_predecessor_validates written_bytes_unchanged worklist_liveness_matches_passes unread_phis_are_dropped --nocapture`: 6 passed, 0 failed, 0 ignored, 209 filtered out (exit 0) - `cargo test --locked -p victory-shader --lib -- --test-threads=1`: 213 passed, 0 failed, 2 ignored (exit 0) **What this adds** The thread already has the author's RTX 4060 (Ampere) run with the Windows SDK `dxil.dll` 10.0.26100.0. This adds a Blackwell wave32 part with DXIL validator 1.9.2609.5: the two shader tests that previously failed DXIL validation and the cache test pass here, and `written_bytes_unchanged` still passes. **Not tested** The title's shader pack, live pipeline creation, retail runs, `VICTORY_SHADER_OPT=0` raw emission, a WARP control lane, the console compiler's DXC, release profile, AMD and Intel GPUs. AI-assisted (OpenCode), model: **DeepSeek V4.1 Flash Free** - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.
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/110:upstream-pr/110'
git switch 'upstream-pr/110'

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/110'
git switch 'upstream-pr/110'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/110'
git switch 'upstream-pr/110'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/110'
git switch 'main'
git merge --squash 'upstream-pr/110'
git switch 'main'
git merge --ff-only 'upstream-pr/110'
git switch 'main'
git merge 'upstream-pr/110'
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
Zettrox/ztrx!110
No description provided.