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

Merged
TG0000 merged 1 commit from upstream-pr/104 into main 2026-11-24 07:49:48 +00:00
TG0000 commented 2026-11-24 00:32:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T00:32:08Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6571827647 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/104). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T00:32:08Z. --- `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)
lordon1a commented 2026-11-24 06:54:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lordon1a on 2026-11-24T00:51:49Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118513758 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/104#issuecomment-7118513758). Originally posted by [lordon1a](https://github.com/lordon1a) on 2026-11-24T00:51:49Z. --- 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.
DaviTostes commented 2026-11-24 06:54:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:09:57Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7118822736 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/104#issuecomment-7118822736). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:09:57Z. --- 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.
upstream-sync closed this pull request 2026-11-24 08:01:47 +00:00
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!104
No description provided.