Skip to content

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

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
TG0000:perf/bitcode-worklist-liveness
Nov 24, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
TG0000:perf/bitcode-worklist-liveness

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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

…passes

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 <noreply@anthropic.com>
@lordon1a

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

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 Build: Linux workspace, contributor setup and CI #112 for the victory-gpu E0425.

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…, not repeated passes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ghost ghost mentioned this pull request Nov 24, 2026
@Marvisak
Marvisak merged commit 0e990e4 into Marvisak:main Nov 24, 2026
mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
Sync requested by the user. 17 files conflicted; resolutions:
- Same-spot additions on both sides kept both (13 hunks).
- bitcode.rs, bcpack.rs: kept the fork's rewrites (Marvisak#697 DCE writer, Marvisak#703
  streaming decoder) over upstream's Marvisak#104/Marvisak#93; removed upstream's now
  duplicate or unused helpers (second `live`, `for_each_operand`,
  Succs/successors, bcpack bucket table) and adapted upstream's tests.
- xasync.rs: fork's per-queue terminate plus upstream's wake of waiters.
- emit/mod.rs: fork's spill-table build plus upstream's structure-first
  resource tracking; watch.rs: fork's absent-block clear plus saturating_add.
- net.rs, draw.rs: upstream's side.
- Test helpers adjusted to the merged signatures (track(.., st),
  heap_handle(.., nonuniform, ..), lowers_with, Config::resolve(paths(..))).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
worklist_liveness_matches_passes, unread_phis_are_dropped and
written_bytes_unchanged pin upstream's writer (Marvisak#104), which the sync kept
the fork's Marvisak#697 dead-code-eliminating writer over; that writer's own
liveness tests cover the property.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
Shaders: find the written instructions with a worklist, not repeated passes
itsonlysimbo pushed a commit to itsonlysimbo/VIctory that referenced this pull request Nov 24, 2026
…eness

Shaders: find the written instructions with a worklist, not repeated passes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants