Shader: backward dead-code elimination in DXIL bitcode emission #697

Open
wbkepler wants to merge 1 commit from upstream-pr/697 into main
wbkepler commented 2026-11-24 07:00:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T07:00:19Z.


Summary

Replaces fixed-point phi-only liveness in victory-shader Module::write with a single-pass backward worklist over reachable reverse-postorder blocks seeded from side-effecting roots (stores, atomics, barriers, void/non-pure calls, dx.op.atomic*) and branch conditions:

  • Omits dead non-phi instructions, cyclic dead phis, unreachable blocks, and unreferenced dx.op declarations from emitted DXIL bitcode.
  • Folds loop latches left empty by DCE and reuses a single record scratch Vec during FUNCTION_BLOCK serialization.
  • Prunes instruction-driven ShaderFlags (TYPED_UAV_LOADS, WAVE_OPS, INT64, RESOURCE_HEAP, SAMPLER_HEAP) via Module::active_flags in Shader::finish so declared flags and SFI0 match live post-DCE usage.

Verification

  • cargo clippy --workspace --all-targets — passed (zero new warnings)
  • cargo clippy -p victory --features diagnostics --all-targets — passed
  • cargo test --workspace — passed (victory-shader: 72/72 including 3 new bitcode/DCE/dxil.dll validation tests)
  • cargo xtask policy — passed (151 files ok)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6576460847 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/697). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T07:00:19Z. --- ## Summary Replaces fixed-point phi-only liveness in `victory-shader` `Module::write` with a single-pass backward worklist over reachable reverse-postorder blocks seeded from side-effecting roots (stores, atomics, barriers, void/non-pure calls, `dx.op.atomic*`) and branch conditions: - Omits dead non-phi instructions, cyclic dead phis, unreachable blocks, and unreferenced `dx.op` declarations from emitted DXIL bitcode. - Folds loop latches left empty by DCE and reuses a single record scratch `Vec` during `FUNCTION_BLOCK` serialization. - Prunes instruction-driven `ShaderFlags` (`TYPED_UAV_LOADS`, `WAVE_OPS`, `INT64`, `RESOURCE_HEAP`, `SAMPLER_HEAP`) via `Module::active_flags` in `Shader::finish` so declared flags and `SFI0` match live post-DCE usage. ## Verification - `cargo clippy --workspace --all-targets` — passed (zero new warnings) - `cargo clippy -p victory --features diagnostics --all-targets` — passed - `cargo test --workspace` — passed (`victory-shader`: 72/72 including 3 new bitcode/DCE/`dxil.dll` validation tests) - `cargo xtask policy` — passed (151 files ok)
LAKSHYA-cmd-tech commented 2026-11-24 08:02:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T07:09:01Z.

Review: APPROVED


Tested on Windows 11 MSVC (rustc 1.98.1):

  • cargo test -p victory-shader: 72 passed, 0 failed (all 3 DCE/bitcode/DXIL validation unit tests pass cleanly)
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean (0 warnings)
  • cargo xtask policy: 151 files ok

Backward worklist DCE correctly eliminates dead non-phi opcodes, cyclic dead phis, and prunes unused ShaderFlags before DXIL container bitcode emission.

<!-- greyfinch-sync:Marvisak/VIctory:review:6035063594 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/697#pullrequestreview-6035063594). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T07:09:01Z. **Review:** APPROVED --- Tested on Windows 11 MSVC (rustc 1.98.1): - cargo test -p victory-shader: 72 passed, 0 failed (all 3 DCE/bitcode/DXIL validation unit tests pass cleanly) - cargo clippy -p victory-shader --all-targets -- -D warnings: clean (0 warnings) - cargo xtask policy: 151 files ok Backward worklist DCE correctly eliminates dead non-phi opcodes, cyclic dead phis, and prunes unused ShaderFlags before DXIL container bitcode emission.
catminou4 commented 2026-11-24 09:05:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T08:48:16Z.

Review: COMMENTED


Heads-up: no longer merges cleanly with current main (2005e35a) — conflict in crates/victory-shader/src/emit/bitcode.rs. Needs a rebase.

For the record, the head alone passes on macOS 26 arm64 / rustc 1.98.1: cargo test -p victory-shader --locked → 58 passed / 0 failed (against its own base). The backward-DCE worklist replaces fixed-point phi-only liveness in Module::write — the DXIL bitcode emission path, so the Windows shader run is the real check for output equivalence.

<!-- greyfinch-sync:Marvisak/VIctory:review:6036155107 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/697#pullrequestreview-6036155107). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T08:48:16Z. **Review:** COMMENTED --- Heads-up: no longer merges cleanly with current main (`2005e35a`) — conflict in `crates/victory-shader/src/emit/bitcode.rs`. Needs a rebase. For the record, the head alone passes on macOS 26 arm64 / rustc 1.98.1: `cargo test -p victory-shader --locked` → **58 passed / 0 failed** (against its own base). The backward-DCE worklist replaces fixed-point phi-only liveness in `Module::write` — the DXIL bitcode emission path, so the Windows shader run is the real check for output equivalence.
iurysf commented 2026-11-24 09:06:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by iurysf on 2026-11-24T08:27:47Z.


Measured offline against main c2e7254 with victory translate --list --validate over the title's shaders.rpf + executable (26,096 programs), dxil.dll 1.9.2602.17, comparing outcomes program by program: no outcome changes. All 25,902 programs translate, fail, validate or get rejected exactly as on main: 21,983 validated, 185 rejected, 3,734 failed. So the dead-code elimination doesn't break validation anywhere in this corpus. (I didn't measure the size or compile-time effect.)

AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7126164211 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/697#issuecomment-7126164211). Originally posted by [iurysf](https://github.com/iurysf) on 2026-11-24T08:27:47Z. --- Measured offline against main `c2e7254` with `victory translate --list --validate` over the title's `shaders.rpf` + executable (26,096 programs), dxil.dll 1.9.2602.17, comparing outcomes program by program: **no outcome changes**. All 25,902 programs translate, fail, validate or get rejected exactly as on main: 21,983 validated, 185 rejected, 3,734 failed. So the dead-code elimination doesn't break validation anywhere in this corpus. (I didn't measure the size or compile-time effect.) AI-assisted.
mercuryVM commented 2026-11-24 11:03:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T10:27:31Z.

Review: APPROVED


Tested 2eea8e2 on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 72 passed, including the GPU VALU tests. cargo clippy -p victory-shader --all-targets is clean. I also ran victory translate --validate --pipelines on the local title executable at the merge base (d2a4bae) and at this head. Both give 12 of 12 vertex/pixel programs emitted, 12 validated and 12 pipelines, with identical I/O counts.

  • Roots in is_root: I checked every non-void Attr::NoUnwind op the lowerer emits (wavePrefixOp, waveActiveOp, waveActiveBit, waveActiveBallot, waveReadLaneFirst, waveReadLaneAt, quadReadLaneAt, atomicBinOp, atomicCompareExchange). Only the atomics have side effects, and the dx.op.atomic prefix keeps those live. Void calls (storeOutput, textureStore, emitIndices, discard, …) are always roots. So dropping an unused wave op is the only new removal among calls, and that is safe.
  • Future-proofing: any later non-void op with a side effect and NoUnwind (for example bufferUpdateCounter, if it is ever lowered) would be silently removed when its result is unused. A short comment on is_root, or an explicit allow-list next to the dx.op.atomic prefix, would make that rule visible to whoever adds the next op.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6037539510 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/697#pullrequestreview-6037539510). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T10:27:31Z. **Review:** APPROVED --- Tested 2eea8e2 on Windows 11, RTX 5070, MSVC: `cargo test -p victory-shader -- --test-threads=1` 72 passed, including the GPU VALU tests. `cargo clippy -p victory-shader --all-targets` is clean. I also ran `victory translate --validate --pipelines` on the local title executable at the merge base (d2a4bae) and at this head. Both give 12 of 12 vertex/pixel programs emitted, 12 validated and 12 pipelines, with identical I/O counts. - Roots in `is_root`: I checked every non-void `Attr::NoUnwind` op the lowerer emits (`wavePrefixOp`, `waveActiveOp`, `waveActiveBit`, `waveActiveBallot`, `waveReadLaneFirst`, `waveReadLaneAt`, `quadReadLaneAt`, `atomicBinOp`, `atomicCompareExchange`). Only the atomics have side effects, and the `dx.op.atomic` prefix keeps those live. Void calls (`storeOutput`, `textureStore`, `emitIndices`, `discard`, …) are always roots. So dropping an unused wave op is the only new removal among calls, and that is safe. - Future-proofing: any later non-void op with a side effect and `NoUnwind` (for example `bufferUpdateCounter`, if it is ever lowered) would be silently removed when its result is unused. A short comment on `is_root`, or an explicit allow-list next to the `dx.op.atomic` prefix, would make that rule visible to whoever adds the next op. AI-assisted (Claude Code).
molodayamama commented 2026-11-24 14:04:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by molodayamama on 2026-11-24T14:01:10Z.


Independent Windows check of 2eea8e2e817032c7574c64b2adb8fa870504d75a (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64).

  • cargo test -p victory-shader --locked --lib dead_instructions_and_unused_declarations_are_eliminated -- --test-threads=1 --nocapture: 1 passed.
  • cargo test -p victory-shader --locked --lib side_effecting_atomics_with_unused_results_stay_live -- --test-threads=1 --nocapture: 1 passed.
  • cargo test -p victory-shader --locked --lib post_dce_shader_flags_validate_with_dxil -- --test-threads=1 --nocapture: 1 passed.

The focused tests cover removal of dead instructions and unused declarations, keeping a side-effecting atomic with an unused result, and DXIL shader flags. This checks the emitted module and the local validator, without submitting a dispatch.

Only source-built synthetic fixtures were used. AI-assisted verification.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7133703093 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/697#issuecomment-7133703093). Originally posted by [molodayamama](https://github.com/molodayamama) on 2026-11-24T14:01:10Z. --- Independent Windows check of `2eea8e2e817032c7574c64b2adb8fa870504d75a` (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64). - `cargo test -p victory-shader --locked --lib dead_instructions_and_unused_declarations_are_eliminated -- --test-threads=1 --nocapture`: 1 passed. - `cargo test -p victory-shader --locked --lib side_effecting_atomics_with_unused_results_stay_live -- --test-threads=1 --nocapture`: 1 passed. - `cargo test -p victory-shader --locked --lib post_dce_shader_flags_validate_with_dxil -- --test-threads=1 --nocapture`: 1 passed. The focused tests cover removal of dead instructions and unused declarations, keeping a side-effecting atomic with an unused result, and DXIL shader flags. This checks the emitted module and the local validator, without submitting a dispatch. Only source-built synthetic fixtures were used. AI-assisted verification.
Marvisak commented 2026-11-25 02:05:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:16:56Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/bitcode.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149388153 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/697#issuecomment-7149388153). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:16:56Z. --- <!-- victory-triage v1 head=fa8d0845f7a4de09e913638acbad983591f50bdf verdict=needs-rebase at=2026-11-25T01:16:55Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/bitcode.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
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/697:upstream-pr/697'
git switch 'upstream-pr/697'

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