Shaders: compute Module::plan once in Shader::finish and reuse for uses and bitcode write #3152

Open
github-bot wants to merge 1 commit from github-bot/gh-3152 into main AGit
Owner

@⁠reeledMarek on GitHub · 2026-11-26 10:27 UTC · original
reeledMarek:fix/shader-single-plan-in-finish → main · +43 −8 · 2 files · 1 commits · open

Summary

  • On every shader compilation, Shader::finish() in crates/victory-shader/src/emit/shader.rs ran self.m.uses() (which executes full Module::plan(): worklist liveness, RPO DFS, predecessor lists, empty-block folding, and block layout) and then ran self.m.write() (which computed the exact same Module::plan() a second time from scratch).
  • Between uses() and write(), Shader::finish() only appends metadata and constants and never adds or modifies blocks, instructions, or terminators, so Plan is invariant.
  • Expose pub(crate) fn plan, uses_of(&Plan), and write_with(&Plan) in crates/victory-shader/src/emit/bitcode.rs, compute let plan = self.m.plan(); once in Shader::finish(), and add reused_plan_matches_fresh_uses_and_write verifying byte-identical bitcode and identical Uses.
**@⁠reeledMarek** on GitHub · 2026-11-26 10:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/3152) `reeledMarek:fix/shader-single-plan-in-finish` → `main` · +43 −8 · 2 files · 1 commits · open ## Summary - On every shader compilation, `Shader::finish()` in `crates/victory-shader/src/emit/shader.rs` ran `self.m.uses()` (which executes full `Module::plan()`: worklist liveness, RPO DFS, predecessor lists, empty-block folding, and block layout) and then ran `self.m.write()` (which computed the exact same `Module::plan()` a second time from scratch). - Between `uses()` and `write()`, `Shader::finish()` only appends metadata and constants and never adds or modifies blocks, instructions, or terminators, so `Plan` is invariant. - Expose `pub(crate) fn plan`, `uses_of(&Plan)`, and `write_with(&Plan)` in `crates/victory-shader/src/emit/bitcode.rs`, compute `let plan = self.m.plan();` once in `Shader::finish()`, and add `reused_plan_matches_fresh_uses_and_write` verifying byte-identical bitcode and identical `Uses`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:08:50 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 11:25 UTC · original

Independent local verification of head 86eb516 on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-shader --lib reused_plan_matches_fresh_uses_and_write — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean.

CPU-only lowering test; no D3D12 device or game assets used. Worktree-local CARGO_TARGET_DIR. AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only). No game data.

**@⁠cloph-dsp** on GitHub · 2026-11-26 11:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/3152#issuecomment-7190704204) Independent local verification of head `86eb516` on Windows 11, Rust 1.100.0/MSVC: - `cargo test --locked -p victory-shader --lib reused_plan_matches_fresh_uses_and_write` — 1 passed, 0 failed. - `git diff --check origin/main HEAD` — clean. CPU-only lowering test; no D3D12 device or game assets used. Worktree-local `CARGO_TARGET_DIR`. AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only). No game data.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 11:31 UTC · original · approved these changes

Verified against origin/main (63bcca4):

  • cargo test -p victory-shader --lib emit::bitcode:: -- --test-threads=1: 13/13 passed (1 ignored bench; including reused_plan_matches_fresh_uses_and_write).
  • cargo test -p victory-shader --lib emit::tests:: -- --test-threads=1: 17/17 passed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly computes Module::plan once in Shader::finish and reuses it for both uses and bitcode emission.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 11:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/3152#pullrequestreview-6073402042) · **approved these changes** Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-shader --lib emit::bitcode:: -- --test-threads=1`: 13/13 passed (1 ignored bench; including `reused_plan_matches_fresh_uses_and_write`). - `cargo test -p victory-shader --lib emit::tests:: -- --test-threads=1`: 17/17 passed. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly computes `Module::plan` once in `Shader::finish` and reuses it for both `uses` and bitcode emission. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠rezioliver-collab on GitHub · 2026-11-26 18:01 UTC · original

Timing the full Shader::finish() call on Apple M1 showed where reusing the plan pays off. On a compute graph with 256 branch-and-join stages, finalization went from 888 to 776 µs. Building and finalizing that graph together went from 2.98 to 2.85 ms.

Branch-and-join stages Base finish() PR finish() Build + finish speedup
4 33.0 µs 30.0 µs 1.04×
32 121.7 µs 108.1 µs 1.04×
256 888.2 µs 776.4 µs 1.04×

I compared 63bcca43 and 86eb5162 in release mode on macOS 14.7 (Rust 1.98.1). Each arm had eight dependent integer operations, followed by a phi at the join. I timed graph construction and finalization separately over 12 alternating pairs. The complete DXBC containers matched byte for byte at all three sizes (2,416, 9,784 and 78,692 bytes).

**@⁠rezioliver-collab** on GitHub · 2026-11-26 18:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/3152#issuecomment-7199961849) Timing the full Shader::finish() call on Apple M1 showed where reusing the plan pays off. On a compute graph with 256 branch-and-join stages, finalization went from **888 to 776 µs**. Building and finalizing that graph together went from **2.98 to 2.85 ms**. | Branch-and-join stages | Base `finish()` | PR `finish()` | Build + finish speedup | |---:|---:|---:|---:| | 4 | 33.0 µs | 30.0 µs | 1.04× | | 32 | 121.7 µs | 108.1 µs | 1.04× | | 256 | 888.2 µs | 776.4 µs | 1.04× | I compared `63bcca43` and `86eb5162` in release mode on macOS 14.7 (Rust 1.98.1). Each arm had eight dependent integer operations, followed by a phi at the join. I timed graph construction and finalization separately over 12 alternating pairs. The complete DXBC containers matched byte for byte at all three sizes (2,416, 9,784 and 78,692 bytes).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3152/head:github-bot/gh-3152'
git switch 'github-bot/gh-3152'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3152'
git switch 'github-bot/gh-3152'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3152'
git switch 'github-bot/gh-3152'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3152'
git switch 'main'
git merge --squash 'github-bot/gh-3152'
git switch 'main'
git merge --ff-only 'github-bot/gh-3152'
git switch 'main'
git merge 'github-bot/gh-3152'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3152
No description provided.