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

Open
reeledMarek wants to merge 1 commit from upstream-pr/3152 into main
reeledMarek commented 2026-11-26 10:27:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-26T10:27:23Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5508285478 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3152). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-26T10:27:23Z. --- ## 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`.
Dukestar69 commented 2026-11-26 11:31:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T11:31:04Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6073402042 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3152#pullrequestreview-6073402042). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T11:31:04Z. **Review:** APPROVED --- 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).
cloph-dsp commented 2026-11-26 12:05:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T11:25:19Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7190704204 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3152#issuecomment-7190704204). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T11:25:19Z. --- 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.
rezioliver-collab commented 2026-11-26 18:01:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by rezioliver-collab on 2026-11-26T18:01:17Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7199961849 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3152#issuecomment-7199961849). Originally posted by [rezioliver-collab](https://github.com/rezioliver-collab) on 2026-11-26T18:01:17Z. --- 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).
futurepro62 commented 2026-11-26 21:24:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-26T21:24:25Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head 86eb516 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: reused_plan_matches_fresh_uses_and_write.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7203695467 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3152#issuecomment-7203695467). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-26T21:24:25Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head `86eb516` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `reused_plan_matches_fresh_uses_and_write`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
fuzeofficlal commented 2026-11-26 21:52:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:52:37Z.

Review: APPROVED


Verified on 86eb5162:

  • cargo xtask policy: 191 files ok.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features): clean.
  • rustfmt --edition 2024 --check: clean on all modified hunks in crates/victory-shader/src/emit/bitcode.rs and shader.rs.
  • cargo test --locked -p victory-shader --lib -- --test-threads=1 emit::tests:: emit::lower::: 50 passed, 0 failed under both VICTORY_WARP=0 and VICTORY_WARP=1.

Between self.m.plan() (shader.rs:579) and self.m.write_with(&plan) (shader.rs:922), Shader::finish only interns types/constants and appends metadata while leaving blocks, instructions, terminators, globals, functions, attributes, and existing branch-condition constants untouched, so the reused Plan produces identical bitcode. Two minor non-blocking notes: the doc comments (shader.rs:576, bitcode.rs:730) mention metadata/constant additions without noting type additions or the replace_const branch-condition caveat, and reused_plan_matches_fresh_uses_and_write (bitcode.rs:1660) calls m.i32(42) on synthetic(64, 64, 64) which already pre-interns 0..63 (so it exercises metadata growth without actually appending a new constant and shifting value IDs). LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6080886038 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3152#pullrequestreview-6080886038). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T21:52:37Z. **Review:** APPROVED --- Verified on `86eb5162`: - `cargo xtask policy`: `191 files ok`. - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`): clean. - `rustfmt --edition 2024 --check`: clean on all modified hunks in `crates/victory-shader/src/emit/bitcode.rs` and `shader.rs`. - `cargo test --locked -p victory-shader --lib -- --test-threads=1 emit::tests:: emit::lower::`: **50 passed, 0 failed** under both `VICTORY_WARP=0` and `VICTORY_WARP=1`. Between `self.m.plan()` (`shader.rs:579`) and `self.m.write_with(&plan)` (`shader.rs:922`), `Shader::finish` only interns types/constants and appends metadata while leaving blocks, instructions, terminators, globals, functions, attributes, and existing branch-condition constants untouched, so the reused `Plan` produces identical bitcode. Two minor non-blocking notes: the doc comments (`shader.rs:576`, `bitcode.rs:730`) mention metadata/constant additions without noting type additions or the `replace_const` branch-condition caveat, and `reused_plan_matches_fresh_uses_and_write` (`bitcode.rs:1660`) calls `m.i32(42)` on `synthetic(64, 64, 64)` which already pre-interns `0..63` (so it exercises metadata growth without actually appending a new constant and shifting value IDs). LGTM.
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/3152:upstream-pr/3152'
git switch 'upstream-pr/3152'

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