Shaders/opt: keep resource handles block-local, clear stale DOUBLES flag, and guard double-latch folding #576

Closed
Lewandox wants to merge 1 commit from upstream-pr/576 into main
Lewandox commented 2026-11-25 02:26:01 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Lewandox on 2026-11-25T02:26:01Z.


Summary

Fixes three soundness / DXIL validation bugs in the shader clean-up passes (VICTORY_SHADER_OPT, related to #310):

  1. Keep resource handle intrinsics block-local (opt.rs): createHandleFromHeap, createHandleFromBinding, and nnotateHandle were classified as Effect::Pure (Attr::ReadNone), allowing REUSE to share handles across basic blocks (including NonUniformResourceIndex heap handles). Reusing handles across blocks breaks DXIL handle legalisation on GPU drivers (NVIDIA refusals / visual corruption, AMD compiler crashes). Classifying them as Effect::Lanes (and checking wave*/quad* before Attr::ReadNone) prevents cross-block handle reuse while preserving same-block LOADS deduplication and DEAD removal.
  2. Clear stale lags::DOUBLES after DEAD/FOLD (opt.rs, shader.rs): Usage did not track TypeDef::Double, so eliminating the last f64 instruction left SFI0 bit 0 (DOUBLES) set and caused dxil.dll to reject the shader (Flags must match usage).
  3. Guard empty-latch folding against duplicate edges (itcode.rs): When opt empties both arms (latch_a, latch_b) of a CondBr jumping back to loop header head, both latches satisfied !succs(*p).contains(&h) and folded into the same predecessor p, emitting two PHI entries from p with distinct incoming values (invalid LLVM/DXIL bitcode). Also guards against folding into/from an already-folded block.

Verification

  • Added 3 unit/validation tests in emit::bitcode::opt::tests:

esource_handles_are_block_local_and_not_reused_across_blocks

  • dead_double_clears_doubles_flag_and_validates (verified against dxil.dll)
  • wo_empty_latches_from_one_branch_validate_with_dxil (verified against dxil.dll)
    
  • Ran cargo test -p victory-shader --lib -- opt::tests bitcode::tests with VICTORY_DXIL set: 19 passed, 0 failed (1 ignored benchmark).
  • Not run on a retail game build; all tests use synthetic shaders.

AI-assisted.

<!-- greyfinch-sync:fxceless/victory-fork:issue:5483957782 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/576). Originally posted by [Lewandox](https://github.com/Lewandox) on 2026-11-25T02:26:01Z. --- ## Summary Fixes three soundness / DXIL validation bugs in the shader clean-up passes (VICTORY_SHADER_OPT, related to #310): 1. **Keep resource handle intrinsics block-local (opt.rs)**: createHandleFromHeap, createHandleFromBinding, and nnotateHandle were classified as Effect::Pure (Attr::ReadNone), allowing REUSE to share handles across basic blocks (including NonUniformResourceIndex heap handles). Reusing handles across blocks breaks DXIL handle legalisation on GPU drivers (NVIDIA refusals / visual corruption, AMD compiler crashes). Classifying them as Effect::Lanes (and checking wave*/quad* before Attr::ReadNone) prevents cross-block handle reuse while preserving same-block LOADS deduplication and DEAD removal. 2. **Clear stale lags::DOUBLES after DEAD/FOLD (opt.rs, shader.rs)**: Usage did not track TypeDef::Double, so eliminating the last f64 instruction left SFI0 bit 0 (DOUBLES) set and caused dxil.dll to reject the shader (Flags must match usage). 3. **Guard empty-latch folding against duplicate edges (itcode.rs)**: When opt empties both arms (latch_a, latch_b) of a CondBr jumping back to loop header head, both latches satisfied !succs(*p).contains(&h) and folded into the same predecessor p, emitting two PHI entries from p with distinct incoming values (invalid LLVM/DXIL bitcode). Also guards against folding into/from an already-folded block. ## Verification - Added 3 unit/validation tests in emit::bitcode::opt::tests: - esource_handles_are_block_local_and_not_reused_across_blocks - dead_double_clears_doubles_flag_and_validates (verified against dxil.dll) - wo_empty_latches_from_one_branch_validate_with_dxil (verified against dxil.dll) - Ran cargo test -p victory-shader --lib -- opt::tests bitcode::tests with VICTORY_DXIL set: **19 passed, 0 failed** (1 ignored benchmark). - Not run on a retail game build; all tests use synthetic shaders. AI-assisted.
Lewandox commented 2026-11-30 00:59:16 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Lewandox on 2026-11-30T00:59:16Z.


Closing for now.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7265745473 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/576#issuecomment-7265745473). Originally posted by [Lewandox](https://github.com/Lewandox) on 2026-11-30T00:59:16Z. --- Closing for now.

Pull request closed

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!576
No description provided.