Skip to content

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

Open
hamdog0203 wants to merge 1 commit into
Zettrox:mainfrom
hamdog0203:fix/bitcode-latch-phi-const
Open

hamdog0203 wants to merge 1 commit into
Zettrox:mainfrom
hamdog0203:fix/bitcode-latch-phi-const

Conversation

@hamdog0203

Copy link
Copy Markdown

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant