Shaders: find the written instructions with a worklist, not repeated passes #104

Manually merged
github merged 1 commit from gh-pr/104/head into gh-pr/104/base 2026-11-26 03:50:22 +00:00
Owner

@⁠TG0000 on GitHub · 2026-11-24 00:32 UTC · original
TG0000:perf/bitcode-worklist-liveness → main · +258 −50 · 1 files · 1 commits · merged 2026-11-24 07:49 UTC

Module::write (crates/victory-shader/src/emit/bitcode.rs) drops the phis nothing reads before numbering values. It did that by sweeping every instruction until a sweep changed nothing, and each sweep built an operand Vec for every live instruction. A use reached through a chain of merges is found one merge per sweep: the last merge's phis read the previous merge's phis, which have lower indices, so a forward sweep finds one more level each time. The cost grew as instructions × merges, and every DXIL shader the translator writes goes through this.

Change

  • Module::live: a worklist over instructions, seeded with every body instruction and branch condition. Each instruction is visited once, and its operands are read through for_each_operand, a callback with no allocation. The result is the same set: the smallest one closed under operands.
  • successors returns a two-slot inline slice instead of a Vec. It is called once per DFS step and in latch folding.
  • The call, phi and gep records reuse one operand buffer instead of allocating per instruction.

The output is byte-for-byte the same. written_bytes_unchanged pins the length and FNV-1a hash of two synthetic modules to what the writer produced before this change.

Tests (synthetic only, no game data)

  • worklist_liveness_matches_passes: the new liveness equals the old sweep algorithm, kept in the tests as the reference, on several module shapes (diamond chains with one phi per register, plus a loop whose empty latch is folded).
  • unread_phis_are_dropped: an unread phi and a cycle of phis that only read each other stay dead; a read phi stays live.
  • successors_of_each_terminator.
  • written_bytes_unchanged: see above.

Benchmark

cargo test --release -p victory-shader bitcode -- --ignored --nocapture (bench_write, average of 5 write() calls on a synthetic module):

module instructions before after speedup
64 regs × 64 merges 4,417 8.75 ms 0.35 ms ~25×
128 regs × 256 merges 33,569 110.7 ms 2.36 ms ~47×
256 regs × 512 merges 132,865 985.7 ms 13.4 ms ~74×

The written bytes were identical before and after in all three cases (same length and hash).

Checked

  • cargo +stable-x86_64-pc-windows-msvc test --workspace: all pass.
  • cargo +stable-x86_64-pc-windows-msvc clippy --workspace --all-targets: no new warnings (the ones that remain are in files this PR does not touch).
  • cargo +stable-x86_64-pc-windows-msvc xtask policy: ok.
  • Only bitcode.rs changed, and rustfmt ran on that file alone. No open PR touches it.
  • Not run against the retail game. Windows 11, MSVC stable toolchain; this change runs on the CPU only.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠TG0000** on GitHub · 2026-11-24 00:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/104) `TG0000:perf/bitcode-worklist-liveness` → `main` · +258 −50 · 1 files · 1 commits · merged 2026-11-24 07:49 UTC `Module::write` (`crates/victory-shader/src/emit/bitcode.rs`) drops the phis nothing reads before numbering values. It did that by sweeping every instruction until a sweep changed nothing, and each sweep built an operand `Vec` for every live instruction. A use reached through a chain of merges is found one merge per sweep: the last merge's phis read the previous merge's phis, which have lower indices, so a forward sweep finds one more level each time. The cost grew as instructions × merges, and every DXIL shader the translator writes goes through this. ## Change - `Module::live`: a worklist over instructions, seeded with every body instruction and branch condition. Each instruction is visited once, and its operands are read through `for_each_operand`, a callback with no allocation. The result is the same set: the smallest one closed under operands. - `successors` returns a two-slot inline slice instead of a `Vec`. It is called once per DFS step and in latch folding. - The call, phi and gep records reuse one operand buffer instead of allocating per instruction. The output is byte-for-byte the same. `written_bytes_unchanged` pins the length and FNV-1a hash of two synthetic modules to what the writer produced before this change. ## Tests (synthetic only, no game data) - `worklist_liveness_matches_passes`: the new liveness equals the old sweep algorithm, kept in the tests as the reference, on several module shapes (diamond chains with one phi per register, plus a loop whose empty latch is folded). - `unread_phis_are_dropped`: an unread phi and a cycle of phis that only read each other stay dead; a read phi stays live. - `successors_of_each_terminator`. - `written_bytes_unchanged`: see above. ## Benchmark `cargo test --release -p victory-shader bitcode -- --ignored --nocapture` (`bench_write`, average of 5 `write()` calls on a synthetic module): | module | instructions | before | after | speedup | |---|---:|---:|---:|---:| | 64 regs × 64 merges | 4,417 | 8.75 ms | 0.35 ms | ~25× | | 128 regs × 256 merges | 33,569 | 110.7 ms | 2.36 ms | ~47× | | 256 regs × 512 merges | 132,865 | 985.7 ms | 13.4 ms | ~74× | The written bytes were identical before and after in all three cases (same length and hash). ## Checked - `cargo +stable-x86_64-pc-windows-msvc test --workspace`: all pass. - `cargo +stable-x86_64-pc-windows-msvc clippy --workspace --all-targets`: no new warnings (the ones that remain are in files this PR does not touch). - `cargo +stable-x86_64-pc-windows-msvc xtask policy`: ok. - Only `bitcode.rs` changed, and rustfmt ran on that file alone. No open PR touches it. - Not run against the retail game. Windows 11, MSVC stable toolchain; this change runs on the CPU only. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Module::write dropped unread phis by sweeping every instruction until a
sweep changed nothing, allocating an operand Vec per instruction per
sweep. A chain of merges needs one sweep per merge, so the cost grew with
instructions x merges. A worklist now visits each instruction once and
reads operands through a callback; the block DFS, latch folding and the
call/phi/gep records stop allocating too. The written bytes are the same
(pinned by a test against the previous writer's output).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:50:20 +00:00
github manually merged commit 24480a73f5 into gh-pr/104/base 2026-11-26 03:50:22 +00:00
Author
Owner

@⁠lordon1a on GitHub · 2026-11-24 00:51 UTC · original

Verified 24480a73f on Windows 11 (x86_64-pc-windows-gnullvm, rustc 1.100.0, applied cleanly to main@4c0bb89): pass.

  • cargo xtask policy: 151 files ok
  • cargo test -p victory-shader -- --skip v_med3_f32_matches_rdna2_with_nan: 72 passed, 0 failed, 1 ignored (+4 new worklist tests).

AI-assisted.

**@⁠lordon1a** on GitHub · 2026-11-24 00:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/104#issuecomment-7118513758) Verified `24480a73f` on Windows 11 (`x86_64-pc-windows-gnullvm`, `rustc 1.100.0`, applied cleanly to `main@4c0bb89`): **pass**. - `cargo xtask policy`: 151 files ok - `cargo test -p victory-shader -- --skip v_med3_f32_matches_rdna2_with_nan`: **72 passed, 0 failed, 1 ignored** (+4 new worklist tests). AI-assisted.
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:09 UTC · original

Tested 24480a7 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test -p victory-shader: 55 passed, 0 failed, 1 ignored (2 test binaries), including the 5 tests this PR adds.
  • The ignored test is emit::bitcode::tests::bench_write (benchmark).
  • The full workspace still needs #112 for the victory-gpu E0425.
**@⁠DaviTostes** on GitHub · 2026-11-24 01:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/104#issuecomment-7118822736) Tested `24480a7` on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `cargo test -p victory-shader`: **55 passed, 0 failed, 1 ignored** (2 test binaries), including the 5 tests this PR adds. - The ignored test is `emit::bitcode::tests::bench_write` (benchmark). - The full workspace still needs #112 for the `victory-gpu` E0425.
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!104
No description provided.