Shaders: one arena per program for instruction operands and effects #1756

Open
Luislariosss wants to merge 1 commit from upstream-pr/1757 into main
Luislariosss commented 2026-11-24 22:00:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Luislariosss on 2026-11-24T22:00:39Z.


FIND-1 (claimed here): translating one instruction still cost about five heap allocations (~180 ns of ~880 ns per instruction) — the decoder's Vec of source operands and Inst's reads/writes Vecs, each built from a temporary Vec, plus effects()' scratch and dedup buffers. This replaces those Vecs with one arena per program.

What is in it

  • Fields' variable-length lists (Sop.src, Ds.data, Image.addr, Valu.src) are now gfx10::Span { start, len } into Program::operands: Vec<Operand>; Inst.reads/Inst.writes are spans into Program::effects: Vec<(Reg, Value)>.
  • Instruction::fields takes the arena to append to; ir::build reuses one scratch pair for the per-instruction register lists, so the decoder and effects stop allocating per instruction.
  • Readers go through Program::operands(span) and Inst::reads/writes/read/write(&Program, ..). Exp.src stays a fixed array; Instruction.words is unchanged (per instruction, out of scope here).
  • Dropped instructions' operands are truncated; node splitting copies a block's effect spans, so each copy renames its own values (without the copy the clones would share and overwrite each other).
  • No behavior change: Inst, Fields and Program stay Clone/PartialEq/Eq/Debug; the offline victory translate path is adapted as well.
  • New test ir::tests::operand_and_effect_spans_slice_back_each_instructions_lists: 0, 2 and 3 sources, an implicit VCC operand, reads/writes order, and no gaps in either arena. A second test ir::tests::split_block_copies_get_their_own_effect_spans pins the block-split path: the split copy's spans do not overlap the original's, its write values differ, and renaming through one copy leaves the other unchanged.

Tested

  • Windows 11 26200, AMD RX 9060 XT 16 GB (RDNA4, driver 32.0.32015.7029), Ryzen 5 9600X, 16 GB RAM, Rust 1.98.1 (from rust-toolchain.toml).
  • cargo test --workspace --no-fail-fast: 478 passed, 0 failed, 7 ignored at this head (29d5f6e, rebased onto upstream 70564ae). Upstream main alone on the same machine: 476 passed, 0 failed, 7 ignored — the difference of two is the two new tests, so no upstream test regressed. The GPU-executed valu/waterfall tests ran on the RX 9060 XT (no GPU skips on this adapter).
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean. cargo check --workspace --all-targets: passed.
  • This machine needs CARGO_PROFILE_DEV_DEBUG=0 CARGO_PROFILE_TEST_DEBUG=0 — rustc 1.98.1 here dies with 0xc0000005 while compiling with debuginfo; the test behavior is unchanged by that.

Not tested / not measured

  • The allocation count and the ns per instruction were not measured: the tree has no counting allocator and one was not added. The five allocations above are the finding's own count.
  • Instruction.words and the other per-instruction Vecs the finding names remain, out of scope here.

AI use

  • opencode: HY4 Flash Free gathered the call-site map; implementation and commit by Sonnet (Claude, medium); an independent Sonnet review gate covered span aliasing, block splitting and the truncation path before this was pushed. The test counts are cargo's own output.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6592265118 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1757). Originally posted by [Luislariosss](https://github.com/Luislariosss) on 2026-11-24T22:00:39Z. --- FIND-1 (claimed [here](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7144193104)): translating one instruction still cost about five heap allocations (~180 ns of ~880 ns per instruction) — the decoder's Vec of source operands and `Inst`'s `reads`/`writes` Vecs, each built from a temporary Vec, plus `effects()`' scratch and dedup buffers. This replaces those Vecs with one arena per program. **What is in it** - `Fields`' variable-length lists (`Sop.src`, `Ds.data`, `Image.addr`, `Valu.src`) are now `gfx10::Span { start, len }` into `Program::operands: Vec<Operand>`; `Inst.reads`/`Inst.writes` are spans into `Program::effects: Vec<(Reg, Value)>`. - `Instruction::fields` takes the arena to append to; `ir::build` reuses one scratch pair for the per-instruction register lists, so the decoder and `effects` stop allocating per instruction. - Readers go through `Program::operands(span)` and `Inst::reads/writes/read/write(&Program, ..)`. `Exp.src` stays a fixed array; `Instruction.words` is unchanged (per instruction, out of scope here). - Dropped instructions' operands are truncated; node splitting copies a block's effect spans, so each copy renames its own values (without the copy the clones would share and overwrite each other). - No behavior change: `Inst`, `Fields` and `Program` stay `Clone`/`PartialEq`/`Eq`/`Debug`; the offline `victory translate` path is adapted as well. - New test `ir::tests::operand_and_effect_spans_slice_back_each_instructions_lists`: 0, 2 and 3 sources, an implicit VCC operand, reads/writes order, and no gaps in either arena. A second test `ir::tests::split_block_copies_get_their_own_effect_spans` pins the block-split path: the split copy's spans do not overlap the original's, its write values differ, and renaming through one copy leaves the other unchanged. **Tested** - Windows 11 26200, AMD RX 9060 XT 16 GB (RDNA4, driver 32.0.32015.7029), Ryzen 5 9600X, 16 GB RAM, Rust 1.98.1 (from `rust-toolchain.toml`). - `cargo test --workspace --no-fail-fast`: **478 passed, 0 failed, 7 ignored** at this head (`29d5f6e`, rebased onto upstream `70564ae`). Upstream `main` alone on the same machine: 476 passed, 0 failed, 7 ignored — the difference of two is the two new tests, so no upstream test regressed. The GPU-executed `valu`/`waterfall` tests ran on the RX 9060 XT (no GPU skips on this adapter). - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean. `cargo check --workspace --all-targets`: passed. - This machine needs `CARGO_PROFILE_DEV_DEBUG=0 CARGO_PROFILE_TEST_DEBUG=0` — rustc 1.98.1 here dies with 0xc0000005 while compiling with debuginfo; the test behavior is unchanged by that. **Not tested / not measured** - The allocation count and the ns per instruction were **not measured**: the tree has no counting allocator and one was not added. The five allocations above are the finding's own count. - `Instruction.words` and the other per-instruction Vecs the finding names remain, out of scope here. **AI use** - opencode: HY4 Flash Free gathered the call-site map; implementation and commit by Sonnet (Claude, medium); an independent Sonnet review gate covered span aliasing, block splitting and the truncation path before this was pushed. The test counts are cargo's own output.
catminou4 commented 2026-11-24 23:05:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T22:18:49Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (70564ae), cargo test -p victory-shader --locked → 120 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows.

One arena per program for instruction operands and effects: Span/push/since/of replace per-instruction Vec allocations across ir/structure/gfx10/emit/wave/resource/io/range (12 files, +3281 diff lines). round16_after/reads_mask_as_data/simulate/constant helpers support the new allocation scheme; 2 tests added. This is a pervasive representation change — macOS unit tests are green, but the real proof is the Windows hardware suite (valu/waterfall/diff/fuzz) which exercises every lowering path; please make sure CI Windows ran green before merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6048300069 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1757#pullrequestreview-6048300069). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T22:18:49Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (70564ae), cargo test -p victory-shader --locked → 120 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows. One arena per program for instruction operands and effects: Span/push/since/of replace per-instruction Vec allocations across ir/structure/gfx10/emit/wave/resource/io/range (12 files, +3281 diff lines). round16_after/reads_mask_as_data/simulate/constant helpers support the new allocation scheme; 2 tests added. This is a pervasive representation change — macOS unit tests are green, but the real proof is the Windows hardware suite (valu/waterfall/diff/fuzz) which exercises every lowering path; please make sure CI Windows ran green before merge.
TinkerBell-90 commented 2026-11-24 23:05:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TinkerBell-90 on 2026-11-24T22:21:59Z.

Review: COMMENTED


Tested the "no behavior change" claim on CPU, since this touches every pass from decode to emit.

Setup: Linux x86-64, Rust 1.98.1, main 70564ae against this head 29d5f6e, both with the random program generator from #1335 applied (identical file in both trees). For each program I ran the same stages as emit::translate (ir::build, structure, resource::track, io::raise, emit) and hashed the outcome: the whole HostShader (its Debug output, DXIL bytes included) or the refusal text.

  • 500,000 random programs: all 500,000 outcomes identical (39,357 translate to DXIL, 460,643 are refused with the same reason, no panics).
  • 500,000 more with 1 to 3 random bit flips each: all identical (22,575 translate).
  • The block split path runs: 6,596 programs of the first set go through the copy loop in split_irreducible (118 of them reach DXIL), 4,707 of the mutated set (68).
  • Negative control: with the effect span copy in split_irreducible removed, so the copies share their spans, 79 of the first 500,000 outcomes change (among them 26 translate, 52 are refused and 1 panics in emit/bitcode.rs). So the comparison does catch the aliasing that copy prevents.

Allocations, which the description lists as not measured: with a counting global allocator around translation only, the same 500,000 programs make 197,059,864 allocations on main and 156,581,445 here (-20.5%), or 18.3 and 14.5 per decoded instruction across the whole pipeline. On the mutated set, 14.8 and 11.4. Time, release build, 300,000 programs, three alternating runs each: 14.94 s and 14.26 s on average (about -4.5%), on a machine with other load, so only a rough figure.

Also: cargo test -p victory-shader --lib passes on both (118 on main, 120 here; the GPU tests are Windows only and did not run). cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings is clean (check only, no linker). I could not build apps/victory here (victory-gpu is Windows only), so the one line change in translate.rs is checked by reading only.

One thing to keep in mind, not a bug today: PartialEq on Fields and Inst now compares span positions rather than operands. I found no comparison of either outside tests, but a future a.fields == b.fields across two instructions would compare offsets and never match.

AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:review:6048335024 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1757#pullrequestreview-6048335024). Originally posted by [TinkerBell-90](https://github.com/TinkerBell-90) on 2026-11-24T22:21:59Z. **Review:** COMMENTED --- Tested the "no behavior change" claim on CPU, since this touches every pass from decode to emit. Setup: Linux x86-64, Rust 1.98.1, main `70564ae` against this head `29d5f6e`, both with the random program generator from #1335 applied (identical file in both trees). For each program I ran the same stages as `emit::translate` (`ir::build`, `structure`, `resource::track`, `io::raise`, `emit`) and hashed the outcome: the whole `HostShader` (its Debug output, DXIL bytes included) or the refusal text. - 500,000 random programs: all 500,000 outcomes identical (39,357 translate to DXIL, 460,643 are refused with the same reason, no panics). - 500,000 more with 1 to 3 random bit flips each: all identical (22,575 translate). - The block split path runs: 6,596 programs of the first set go through the copy loop in `split_irreducible` (118 of them reach DXIL), 4,707 of the mutated set (68). - Negative control: with the effect span copy in `split_irreducible` removed, so the copies share their spans, 79 of the first 500,000 outcomes change (among them 26 translate, 52 are refused and 1 panics in `emit/bitcode.rs`). So the comparison does catch the aliasing that copy prevents. Allocations, which the description lists as not measured: with a counting global allocator around translation only, the same 500,000 programs make 197,059,864 allocations on main and 156,581,445 here (-20.5%), or 18.3 and 14.5 per decoded instruction across the whole pipeline. On the mutated set, 14.8 and 11.4. Time, release build, 300,000 programs, three alternating runs each: 14.94 s and 14.26 s on average (about -4.5%), on a machine with other load, so only a rough figure. Also: `cargo test -p victory-shader --lib` passes on both (118 on main, 120 here; the GPU tests are Windows only and did not run). `cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings` is clean (check only, no linker). I could not build `apps/victory` here (`victory-gpu` is Windows only), so the one line change in `translate.rs` is checked by reading only. One thing to keep in mind, not a bug today: `PartialEq` on `Fields` and `Inst` now compares span positions rather than operands. I found no comparison of either outside tests, but a future `a.fields == b.fields` across two instructions would compare offsets and never match. AI-assisted.
DesertGamer commented 2026-11-24 23:05:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DesertGamer on 2026-11-24T22:14:19Z.


VIctory V2: community PRs, merged and tested

Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch.

Where it stands today: launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1).

⚠️ Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk.

Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help:
https://DesertGamer/VIctory-V2

Unofficial community fork of Marvisak/VIctory, GPL-3.0.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7145979350 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1757#issuecomment-7145979350). Originally posted by [DesertGamer](https://github.com/DesertGamer) on 2026-11-24T22:14:19Z. --- ### VIctory V2: community PRs, merged and tested Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch. **Where it stands today:** launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1). :warning: Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk. Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help: https://DesertGamer/VIctory-V2 *Unofficial community fork of Marvisak/VIctory, GPL-3.0.*
upipibi commented 2026-11-24 23:06:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T23:05:04Z.


NVIDIA counterpart to the RX 9060 XT runs: RTX 3070 8 GB, driver 616.56, Windows 11, debug, 29d5f6e (on main 70564ae). cargo test -p victory-shader --locked: 208 passed, 3 failed, 2 ignored. The 3 are #1744's NVIDIA heap-cap failures on main, so there are no new failures from the arena change.

AI-assisted (Claude Code). Synthetic tests only.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7146993959 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1757#issuecomment-7146993959). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-24T23:05:04Z. --- NVIDIA counterpart to the RX 9060 XT runs: RTX 3070 8 GB, driver 616.56, Windows 11, debug, `29d5f6e` (on `main` `70564ae`). `cargo test -p victory-shader --locked`: 208 passed, 3 failed, 2 ignored. The 3 are #1744's NVIDIA heap-cap failures on `main`, so there are no new failures from the arena change. AI-assisted (Claude Code). Synthetic tests only.
Marvisak commented 2026-11-25 04:07:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T03:40:45Z.


Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main (conflicts in crates/victory-shader/src/emit/lower.rs, ir.rs and structure.rs). It will be reviewed again once it is rebased.

From the earlier needs-work note, still open: a green CI check at the head, and a before/after timing (e.g. translate over a synthetic corpus) for this wide refactor.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152141767 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1757#issuecomment-7152141767). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T03:40:45Z. --- <!-- victory-triage v1 head=e18d930287b2865d39405997ac5be77bdd9ca032 verdict=needs-rebase at=2026-11-25T22:21:30Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main` (conflicts in `crates/victory-shader/src/emit/lower.rs`, `ir.rs` and `structure.rs`). It will be reviewed again once it is rebased. From the earlier needs-work note, still open: a green CI `check` at the head, and a before/after timing (e.g. `translate` over a synthetic corpus) for this wide refactor. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1757:upstream-pr/1757'
git switch 'upstream-pr/1757'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1757'
git switch 'upstream-pr/1757'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1757'
git switch 'upstream-pr/1757'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1757'
git switch 'main'
git merge --squash 'upstream-pr/1757'
git switch 'main'
git merge --ff-only 'upstream-pr/1757'
git switch 'main'
git merge 'upstream-pr/1757'
git push origin 'main'
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!1756
No description provided.