Skip to content

Shaders/IR: eliminate per-instruction/CFG Vec allocations and O(1) effects dedup - #2664

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/ir-decode-zero-alloc
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/ir-decode-zero-alloc

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 25, 2026

Copy link
Copy Markdown

Claimed in #1633: #1633 (comment)

Summary

Eliminates hot per-instruction and per-CFG-edge heap allocations and O(REGS) / O(n²) scans during GFX10 decoding and SSA IR construction (crates/victory-shader/src/gfx10/fields.rs, crates/victory-shader/src/ir.rs, +319/-53), producing identical SSA IR:

  1. gfx10/fields.rs (valu_dwords): parses suffix widths directly without split('_').collect::<Vec<_>>() and only checks VOP3B for Encoding::Vop3.
  2. Stack-allocated Exit::succs(): adds zero-alloc Succs { to: [usize; 2], len } used across reverse_postorder, link, irreducible_edge, split_irreducible, multi_entry_loop (also borrowing preds without cloning), and liveness, keeping pub Exit::successors() intact.
  3. Bit-scanning RegSet::members(): iterates only set bits via trailing_zeros() / w &= w - 1 instead of scanning all REGS slots.
  4. O(1) effects() dedup: replaces Vec::contains O(n²) dedup with a stack RegSet bitset and writes (Reg, NONE) directly without a second Vec allocation per instruction.

Measured (--release ir::tests::bench_ir_build)

Workload Before (main) After Speedup
valu_cfg (2,048 insts, 128 blocks) 2,310.3 µs/run 1,966.9 µs/run 1.17× (-14.9%)
movrel (64 insts, 128 VGPRs) 602.0 µs/run 139.3 µs/run 4.32× (-76.9%)

Not a duplicate of

Verification

  • Verified on windows-latest MSVC + WARP SM 6.6 CI (both clippy -D warnings profiles, xtask policy 6/6, victory-shader 281/281 including unit tests verifying valu_dwords, RegSet::members, and Exit::succs): https://github.com/RVZO6/VIctory/actions/runs/45666985023
  • Not run: the title; any hardware GPU.

AI-assisted: T3 Code (Antigravity, gemini-4-neon)

@kotekompotek

Copy link
Copy Markdown

Reviewed: mechanical perf cleanup — VOP3B.contains is read inline now (same lookup, no hoisted variable), and valu_dwords parses via split('_') iterator instead of a Vec. No semantic change visible in the hunks read. CI green.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent verification of #2664 (IR: inline Succs, no per-instruction Vecs, bit-scan RegSet)

PR head e6ab345e7b73be37a0741a54767e061aad35239c, merges cleanly onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca.

Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1, VICTORY_WARP=1 + VICTORY_D3D12_WARP=1 on every run (WARP adapter only, no physical GPU created), DXC/dxil 1.8.2502.11 (Windows SDK 10.0.26100.0, via VICTORY_DXIL), cargo ... -j 4 --locked.

Tests/clippy vs baseline. origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca: cargo test -p victory-shader --lib --locked -- --test-threads=1 = 267 passed, 3 failed, 3 ignored. The 3 failures are WARP limits and are the same 3 in every variant below: a_mask_joined_with_data_at_a_uniform_loop_is_its_number and permutes_read_the_lane_named_and_zero_from_lanes_left_out ("no pipeline for a wave of 32"), and gathers_at_a_level_match_the_hardware (WARP gather result differs). Clippy: cargo clippy -p victory-shader --all-targets --locked = 0 warnings.
With #2664: 271 passed, 3 failed (same 3), 4 ignored (+4 tests, +1 ignored benchmark). Clippy: 0 warnings.

Equivalence. Full diff read. Succs keeps the old order and duplicates (a branch with taken == not_taken still yields two entries, so the "single predecessor" patterns elsewhere behave the same); members() stops at REGS so padding bits are ignored; the bitset dedup in effects() keeps first-occurrence order (covered by effects_dedup_preserves_first_occurrence_order); valu_dwords is checked against the old split-based version for every VOP1/2/C/3/3P name plus edge strings. Output check with the throwaway hashing patch: 650 containers (476 distinct) byte-identical to origin/main.

Perf (release; bench_ir_build added to base for comparison, combined run). ir::build 2048 insts/128 blocks: 2235 / 2290 us (base, 2 rounds) -> 1839 / 1796 us; 64 movrel insts: 426 / 456 us -> 103 / 88 us.

Findings: no bugs. Nit only: Succs stores len as usize (a u8 would shrink it); irrelevant for correctness.

Limits: no physical GPU (WARP only), no game files; the 3 WARP-limited tests above are therefore not verified on hardware. Benchmarks are synthetic, release mode, one machine, two back-to-back rounds each for base and combined; they say nothing about real-title shaders.

AI-assisted: independent verification by Claude Code / Claude Sonnet 5.5, run on behalf of oni-swr. Claims for #1633.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Composition check: #2662 + #2664 + #2665 together

Heads: #2662 95f56a53f8ff90f700d74d102ad79538bad68771, #2664 e6ab345e7b73be37a0741a54767e061aad35239c, #2665 8105fdcfb68ac99d79ea728847a0f555b7a41521, merged in that order onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca as a local merge commit (31ed3543e4c18b978f7b0a699a71f8d18e52a81b, not pushed). No conflicts: the three touch disjoint files (structure.rs; gfx10/fields.rs + ir.rs; emit/bitcode.rs + emit/shader.rs).

Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1, VICTORY_WARP=1 + VICTORY_D3D12_WARP=1 (WARP only), dxil 1.8.2502.11 (SDK 10.0.26100.0), -j 4 --locked.

  • cargo test -p victory-shader --lib --locked -- --test-threads=1: 276 passed, 3 failed, 5 ignored. That is exactly baseline 267 + 2 + 4 + 3 new tests; the 3 failures are the same WARP-limited ones as on origin/main (wave-of-32 x2, WARP gather).
  • cargo clippy -p victory-shader --all-targets --locked: 0 warnings.
  • Emitted containers over the lib test run: 650 byte-identical to origin/main, plus one extra from Shaders/Bitcode: cache Shader::op declarations, primitive types, scalar constants, and zero-copy bitcode Writer #2665's new test (throwaway hashing patch, not committed).
  • Perf (release, combined vs base, 2 rounds each): shader::op 4096 calls 2.84-2.94 ms -> 0.53-0.56 ms; ir::build 2048 insts 2.24-2.29 ms -> 1.80-1.84 ms; 64 movrel insts 426-456 us -> 88-103 us; bitcode writer, 256x512 synthetic module, 8.9-9.8 ms -> 7.0-7.1 ms; BDD given n=13 ~465 us -> ~0.45 us (synthetic).
  • No interaction bugs found. The merge order does not matter (disjoint files).

Limits: no physical GPU, no game files; synthetic benchmarks only.

AI-assisted: independent verification by Claude Code / Claude Sonnet 5.5, run on behalf of oni-swr. Claims for #1633.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP + dxil.dll verification and code review of e6ab345e (on 13f91ca):

Verification (rustc 1.98.1 MSVC, Windows SDK 10.0.26100.0 dxil.dll)

Code Review Notes

  • Moving VOP3B.contains inside the non-Vop3p Vop3 arm, allocation-free valu_dwords (valu_dwords_matches_split_reference_for_all_valu_opcodes), stack-inline Succs, members bit-scan clearing padding bits past REGS = 385, and RegSet-backed effects() dedup preserving first-occurrence order are all strictly equivalent with zero unsafe. Clean and ready to merge.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Verdict: merge candidate. I re-reviewed the rebased head 02fb20d. It is a behaviour-preserving performance change:

  • valu_dwords (gfx10/fields.rs) parses without a Vec, and a test checks it against the old split-based version for every VALU opcode.
  • VOP3B.contains is only evaluated in the VOP3 arm.
  • Exit::succs() returns an inline Succs (same order and duplicates as successors(), which is kept).
  • effects() dedups with a RegSet bitset in place of Vec::contains.

I checked that every entry in r is (reg, NONE) at that point, so deduplicating by register alone matches the old tuple compare, and first-occurrence order is kept. The RegSet::members part that already landed on main has been dropped. One small point: the bench_ir_build test is #[ignore]d, so it adds code to maintain but nothing runs it.

CI: check passing.


Generated by Claude Code

Airamek added a commit to Airamek/VIctory that referenced this pull request Nov 25, 2026
…isak#2664)

Port of upstream Marvisak#2664: Exit::succs() returns an inline Succs
([usize; 2] + len, Deref/IntoIterator, no alloc) with successors()
kept as a compat wrapper for the structure/resource/lower users;
all six hot ir.rs call sites use it (reverse_postorder, link,
irreducible_edge, split_irreducible, multi_entry_loop, liveness;
the loop walk also stops cloning the preds vec). effects() builds
(Vec<(Reg, Value)>) directly with NONE placeholders instead of
Vec<Reg> + a renaming map pass, and read-set dedup uses a RegSet
bitset instead of a Vec::contains scan. The per-decode
VOP3B.contains hoist in fields.rs moves to the one arm that needs
it; valu_dwords' split().collect() was already gone here, and the
PR's differential test for it is added.

Correctness: exit_succs_matches_successors,
members_bit_scan_matches_linear_scan (this tree's members lacked
the PR's padding-bits guard for the all-set word; adopted the
PR's version) and effects_dedup_preserves_first_occurrence_order
prove the rewrites; full shader suite green.

Microbench (release, this machine, committed ignored bench
bench_ir_build, base measured with the same bench on the parent):
ir::build valu_cfg (2048 insts, 128 blocks) 2264.8 -> 1738.8
us/run (-23%); movrel (64 insts, 128 VGPRs) 470.5 -> 121.3
us/run (-74%). The relative-VGPR path gains most (range
expansion + dedup no longer allocate per dword).
…fects dedup

Eliminate hot per-instruction and CFG Vec heap allocations in gfx10::fields and ir::build:
- gfx10/fields.rs: zero-alloc valu_dwords + VOP3B scoping
- ir.rs: stack Exit::succs() small array instead of Vec<usize> per CFG visit
- ir.rs: O(1) RegSet deduplication in effects() preserving first-occurrence order

(Note: RegSet::members() bit-scan already landed on main in 63bcca4 and was dropped from this PR during rebase.)

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
@RVZO6

RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown
Author

Rebased onto current main (63bcca4, head 02fb20d). Dropped the RegSet::members() bit-scan and Effects changes that already landed on main (63bcca4), keeping zero-alloc valu_dwords + VOP3B scoping in gfx10/fields.rs, stack Exit::succs(), and O(1) RegSet dedup in effects(). Verified green on windows-latest MSVC + WARP SM 6.6 CI (clippy, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45727774760.

@RVZO6
RVZO6 force-pushed the rvzo/ir-decode-zero-alloc branch from e6ab345 to 02fb20d Compare November 26, 2026 00:21
@RVZO6 RVZO6 changed the title Shaders/IR: eliminate per-instruction/CFG Vec allocations and bit-scan RegSet Shaders/IR: eliminate per-instruction/CFG Vec allocations and O(1) effects dedup Nov 26, 2026

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-shader --lib ir::tests: 11 passed, 0 failed, 1 ignored (including new exit_succs_matches_successors and effects_dedup_preserves_first_occurrence_order).
  • cargo test -p victory-shader --lib gfx10::fields::tests: 1 passed, 0 failed (valu_dwords_matches_split_reference_for_all_valu_opcodes).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…n/CFG Vec allocations and O(1) effects dedup

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 02fb20dff69a7ed6f16c5cefa91f65c89e885608 — APPROVE.

Verification

  • cargo xtask policy (191 files ok) and cargo clippy -p victory-shader --all-targets --all-features -- -D warnings passed cleanly (with no new rustfmt issues introduced).
  • Verified Succs ([usize; 2]) matches Exit's exact 0/1/2-successor invariant and preserves [taken, not_taken] order across all CFG callers, while preds, phis, reads, and writes remain growable Vecs with no fixed-capacity truncation risk for multi-dword memory ops or wide relative VGPR ranges.
  • Verified RegSet ([u64; 7], 448 bits) covers all 385 register indices (128 SGPR + SCC + 256 VGPR), and because all reads carry placeholder NONE during effects(), Reg-keyed deduplication preserves the exact first-occurrence order of the prior (Reg, Value) Vec::contains check.
  • Passed gfx10:: (17/17), ir:: (11/11, 1 benchmark ignored), structure:: (13/13, 1 benchmark ignored), and emit::lower::tests (33/33).

Non-blocking test notes

  • exit_succs_matches_successors (crates/victory-shader/src/ir.rs:1528) compares succs() against successors() (which delegates to succs()); asserting literal expected slices (including a branch with identical targets) would make it self-contained.
  • effects_dedup_preserves_first_occurrence_order (crates/victory-shader/src/ir.rs:1550) projects to Reg; adding a wave64 relative move reaching v255 would also cover the upper end of the RegSet bitset and (Reg, Value) pairs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants