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

Open
github-bot wants to merge 1 commit from github-bot/gh-2664 into main AGit
Owner

@⁠RVZO6 on GitHub · 2026-11-25 17:44 UTC · original
RVZO6:rvzo/ir-decode-zero-alloc → main · +230 −26 · 2 files · 1 commits · open

Claimed in #1633: #1633

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

  • Checked open PRs and #1633; no open PR or claim overlaps gfx10/fields.rs valu_dwords or ir.rs Exit::succs / RegSet / effects.

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)

**@⁠RVZO6** on GitHub · 2026-11-25 17:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/2664) `RVZO6:rvzo/ir-decode-zero-alloc` → `main` · +230 −26 · 2 files · 1 commits · open Claimed in #1633: https://git.gtahub.st/github/Marvisak--VIctory/issues/1633 ### 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 - Checked open PRs and #1633; no open PR or claim overlaps `gfx10/fields.rs` `valu_dwords` or `ir.rs` `Exit::succs` / `RegSet` / `effects`. ### 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)
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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:11:31 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 17:57 UTC · original

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.

**@⁠kotekompotek** on GitHub · 2026-11-25 17:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/2664#issuecomment-7170230144) 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.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 18:21 UTC · original

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** on GitHub · 2026-11-25 18:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2664#issuecomment-7170805033) **Independent verification of #2664** (IR: inline `Succs`, no per-instruction `Vec`s, 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.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 18:23 UTC · original

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

**@⁠oni-swr** on GitHub · 2026-11-25 18:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2664#issuecomment-7170834074) **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 #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.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 19:27 UTC · original

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)

  • cargo xtask policy: 184 files ok.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings and --features host: 0 warnings.
  • victory-shader (gfx10, ir, structure, emit::tests, emit::lower, emit::waterfall_tests on RTX 5090 + WARP + dxil.dll, emit::bitcode with unchanged FNV-1a): 101 passed, 0 failed, 3 ignored.
  • --release ir::tests::bench_ir_build: valu_cfg(2048 insts, 128 blks) = 1511.6 µs/run, movrel(64 insts, 128 vgprs) = 67.5 µs/run.
  • Merge compatibility with #2662 & #2665: clean zero-conflict 3-way git merge-tree across all three PRs.

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-25 19:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/2664#issuecomment-7172325408) 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`) - `cargo xtask policy`: **184 files ok**. - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` and `--features host`: **0 warnings**. - `victory-shader` (`gfx10`, `ir`, `structure`, `emit::tests`, `emit::lower`, `emit::waterfall_tests` on RTX 5090 + WARP + `dxil.dll`, `emit::bitcode` with unchanged FNV-1a): **101 passed, 0 failed, 3 ignored**. - `--release` `ir::tests::bench_ir_build`: `valu_cfg(2048 insts, 128 blks) = 1511.6 µs/run`, `movrel(64 insts, 128 vgprs) = 67.5 µs/run`. - **Merge compatibility with #2662 & #2665:** clean zero-conflict 3-way `git merge-tree` across all three PRs. ### 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:16 UTC · original · edited 2026-11-26 03:21 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 22:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/2664#issuecomment-7176146072) · edited 2026-11-26 03:21 UTC <!-- victory-triage v1 head=02fb20dff69a7ed6f16c5cefa91f65c89e885608 verdict=merge-candidate at=2026-11-26T03:21:59Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-26 00:21 UTC · original

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** on GitHub · 2026-11-26 00:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2664#issuecomment-7178662609) 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.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2664/head:github-bot/gh-2664'
git switch 'github-bot/gh-2664'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2664'
git switch 'github-bot/gh-2664'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2664'
git switch 'github-bot/gh-2664'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2664'
git switch 'main'
git merge --squash 'github-bot/gh-2664'
git switch 'main'
git merge --ff-only 'github-bot/gh-2664'
git switch 'main'
git merge 'github-bot/gh-2664'
git push origin 'main'
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!2664
No description provided.