gfx10: keep an instruction's words inline instead of in a heap vector (decode -18..-45%, translation -1..-5%) #1623

Open
github-bot wants to merge 2 commits from github-bot/gh-1623 into main AGit
Owner

@⁠pes7 on GitHub · 2026-11-24 19:55 UTC · original
pes7:perf/instruction-words → main · +39 −17 · 5 files · 2 commits · open

Every GFX10 instruction owns a heap vector for its one or two dwords. They fit inline - the longest instruction here is five dwords - which takes 18-45% off decode and 1-5% off a whole translation, with byte-identical output.

Instruction::words: Vec<u32> is built with collect() in the decoder, so every decoded instruction makes one allocation for one to five words of data, and reads them back through a pointer. #1002's counter put it at 1.17 allocations and 149 bytes per instruction - the single largest allocation source in decode.

What changes

words: [u32; Instruction::MAX_WORDS] plus len: u8, and words() returns the slice. MAX_WORDS is 5 because that is the longest instruction the decoder produces: an MIMG with its three extra address dwords (2 + ((w >> 1) & 3)); VOP3/VOP3P with a literal is 3, everything else 2 or 1. A longer instruction is now a clean Isa error rather than a silent truncation.

The struct does not grow: Vec is 24 bytes, [u32; 5] + u8 + padding is 24.

Call sites move from the field to the accessor (8 in gfx10/mod.rs, 1 in sp3.rs, 2 in fields.rs).

Measured

Bench from #1002, standalone against main ffe310e (not stacked on the other PRs), twelve interleaved rounds per arm, best of each, nanoseconds per instruction:

instructions decode before after ratio whole translation ratio
256 48 31 0.65 977 -> 934 0.956
1 024 99 54 0.55 970 -> 921 0.949
4 096 105 82 0.78 1 001 -> 981 0.980
16 384 104 85 0.82 1 046 -> 1 033 0.988

Honest about the shape: decode itself is 18-45% faster, but the whole translation is 1.2-5.1%, and the gain shrinks with program size - at 16 k instructions it is inside the run-to-run spread of this machine. What it buys beyond the CPU time is one allocation and one indirection per instruction gone.

Applied on top of #1002 + #1033 + #1180 the same change measured 0.929-0.957 of the translation, because the per-instruction constant those three leave behind is smaller and the removed allocation is a larger share of it. Both numbers are real; this one is the one that stands alone.

Correctness

  • DXIL byte-identical at all four sizes (FNV over the emitted bytes, printed by the bench).
  • cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored.
  • Both CI clippy configurations with -D warnings; cargo xtask policy.

No game data used. Four files, +38/-16; independent of #1002, #1033, #1180 and #1556.

What it gives (to decide the merge): an instruction's words are kept inline ([u32; 5]) instead of in a heap vector: decode -18..-45 %, translation -1..-5 % (the numbers in the description).

State: MERGEABLE, CI check green, rebased onto today's main (f3578e3).

Risk: rebased for #2143 (words().len() through the accessor); 302 victory-shader tests pass.

**@⁠pes7** on GitHub · 2026-11-24 19:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/1623) `pes7:perf/instruction-words` → `main` · +39 −17 · 5 files · 2 commits · open **Every GFX10 instruction owns a heap vector for its one or two dwords.** They fit inline - the longest instruction here is five dwords - which takes 18-45% off `decode` and 1-5% off a whole translation, with byte-identical output. `Instruction::words: Vec<u32>` is built with `collect()` in the decoder, so every decoded instruction makes one allocation for one to five words of data, and reads them back through a pointer. #1002's counter put it at **1.17 allocations and 149 bytes per instruction** - the single largest allocation source in decode. ## What changes `words: [u32; Instruction::MAX_WORDS]` plus `len: u8`, and `words()` returns the slice. `MAX_WORDS` is 5 because that is the longest instruction the decoder produces: an MIMG with its three extra address dwords (`2 + ((w >> 1) & 3)`); VOP3/VOP3P with a literal is 3, everything else 2 or 1. A longer instruction is now a clean `Isa` error rather than a silent truncation. The struct does not grow: `Vec` is 24 bytes, `[u32; 5]` + `u8` + padding is 24. Call sites move from the field to the accessor (8 in `gfx10/mod.rs`, 1 in `sp3.rs`, 2 in `fields.rs`). ## Measured Bench from #1002, standalone against `main` `ffe310e` (not stacked on the other PRs), twelve interleaved rounds per arm, best of each, nanoseconds per instruction: | instructions | `decode` before | after | ratio | whole translation | ratio | |---:|---:|---:|---:|---:|---:| | 256 | 48 | 31 | **0.65** | 977 -> 934 | **0.956** | | 1 024 | 99 | 54 | **0.55** | 970 -> 921 | **0.949** | | 4 096 | 105 | 82 | **0.78** | 1 001 -> 981 | **0.980** | | 16 384 | 104 | 85 | **0.82** | 1 046 -> 1 033 | **0.988** | Honest about the shape: `decode` itself is 18-45% faster, but the whole translation is 1.2-5.1%, and the gain shrinks with program size - at 16 k instructions it is inside the run-to-run spread of this machine. What it buys beyond the CPU time is one allocation and one indirection per instruction gone. Applied **on top of** #1002 + #1033 + #1180 the same change measured 0.929-0.957 of the translation, because the per-instruction constant those three leave behind is smaller and the removed allocation is a larger share of it. Both numbers are real; this one is the one that stands alone. ## Correctness - **DXIL byte-identical** at all four sizes (FNV over the emitted bytes, printed by the bench). - `cargo test --workspace --locked`: 342 passed, 0 failed, 7 ignored. - Both CI clippy configurations with `-D warnings`; `cargo xtask policy`. No game data used. Four files, +38/-16; independent of #1002, #1033, #1180 and #1556. --- **What it gives (to decide the merge):** an instruction's words are kept inline (`[u32; 5]`) instead of in a heap vector: decode -18..-45 %, translation -1..-5 % (the numbers in the description). **State:** MERGEABLE, CI `check` green, rebased onto today's `main` (`f3578e3`). **Risk:** rebased for #2143 (`words().len()` through the accessor); 302 `victory-shader` tests pass.
One `Vec<u32>` per instruction was one allocation and one cache miss for
the dword or two most instructions have. The words live in a fixed
`[u32; MAX_WORDS]` with a length now: five dwords covers the longest
instruction here (an MIMG with its three extra address dwords), and the
decoder refuses anything longer rather than truncating it.

Decode -18..45% depending on program size; whole translation -1..5%,
since decode is a small part of it.

Now that #1180 is in `main`, this applies on its own: an `Instruction`
carries both the inline `words`/`len` and the cached `name`, and neither
costs the other its win.

cargo test --workspace --locked; both clippy configurations with
-D warnings; cargo xtask policy.
#2143 (merged today) reads `i.machine.words.len()` in `code_table_start`,
and this branch made `words` an inline array behind an accessor, so the two
textually disjoint hunks do not build together. The accessor returns the
slice, so `.len()` is the same number; no behaviour change.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:38:41 +00:00
Author
Owner

@⁠pes7 on GitHub · 2026-11-24 22:56 UTC · original

Heads up before merging: this PR and #1180 conflict with each other in gfx10/mod.rs. Both change the Instruction struct and both change what decode puts in it, so whichever is merged second does not apply - I checked by merging my own branches together, not just each against main.

This branch is stacked on perf/instruction-name (#1180) to fix that, so:

  1. merge #1180 first;
  2. then this one - its diff shrinks to the words change alone.

The resolution keeps both wins, since they are independent: words is the inline [u32; MAX_WORDS] with len (#1623) and name is the opcode name resolved once at decode (#1180), so an instruction carries both.

    words: [u32; Instruction::MAX_WORDS],
    len: u8,
    /// [`name`] of `encoding` and `opcode`, resolved once at decode time.
    name: Option<&'static str>,

Verified on the stack: cargo test --workspace --locked 476 passed, 0 failed; both clippy configurations with -D warnings; cargo xtask policy. The old head was 72eff75, this is 13eece9 (three commits: #1180's, mine, and the merge that resolves the conflict - they collapse once #1180 is in main).

**@⁠pes7** on GitHub · 2026-11-24 22:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/1623#issuecomment-7146816839) Heads up before merging: this PR and **#1180** conflict with each other in `gfx10/mod.rs`. Both change the `Instruction` struct and both change what `decode` puts in it, so whichever is merged second does not apply - I checked by merging my own branches together, not just each against `main`. This branch is stacked on `perf/instruction-name` (#1180) to fix that, so: 1. merge **#1180** first; 2. then this one - its diff shrinks to the `words` change alone. The resolution keeps both wins, since they are independent: `words` is the inline `[u32; MAX_WORDS]` with `len` (#1623) and `name` is the opcode name resolved once at decode (#1180), so an instruction carries both. ```rust words: [u32; Instruction::MAX_WORDS], len: u8, /// [`name`] of `encoding` and `opcode`, resolved once at decode time. name: Option<&'static str>, ``` Verified on the stack: `cargo test --workspace --locked` 476 passed, 0 failed; both clippy configurations with `-D warnings`; `cargo xtask policy`. The old head was `72eff75`, this is `13eece9` (three commits: #1180's, mine, and the merge that resolves the conflict - they collapse once #1180 is in `main`).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:31 UTC · original · edited 2026-11-25 22:23 UTC

Automated triage for the maintainers.

Merge candidate. Re-reviewed at cc2a2bd, rebased onto current main. Instruction::words becomes an inline [u32; 5] plus len, read through words() (crates/victory-shader/src/gfx10/mod.rs); five is the decoder's longest instruction (MIMG, 2 + ((w >> 1) & 3)), and a longer one is now an Isa error instead of a truncation. The only new hunk is the one the second look asked for: code_table_start in crates/victory-shader/src/emit/lower.rs now reads i.machine.words().len(). I checked the merged tree: no other read of the old field and no other construction of Instruction remains outside the files this PR changes.

On your reply: confirmed, that was the one change needed. CI: check passing on this head.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1623#issuecomment-7152000267) · edited 2026-11-25 22:23 UTC <!-- victory-triage v1 head=cc2a2bd5cb16c9a046aaa47431cc5a28b626ee8a verdict=merge-candidate at=2026-11-25T22:26:30Z --> Automated triage for the maintainers. **Merge candidate.** Re-reviewed at `cc2a2bd`, rebased onto current `main`. `Instruction::words` becomes an inline `[u32; 5]` plus `len`, read through `words()` (`crates/victory-shader/src/gfx10/mod.rs`); five is the decoder's longest instruction (MIMG, `2 + ((w >> 1) & 3)`), and a longer one is now an `Isa` error instead of a truncation. The only new hunk is the one the second look asked for: `code_table_start` in `crates/victory-shader/src/emit/lower.rs` now reads `i.machine.words().len()`. I checked the merged tree: no other read of the old field and no other construction of `Instruction` remains outside the files this PR changes. On your reply: confirmed, that was the one change needed. CI: `check` passing on this head. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠pes7 on GitHub · 2026-11-25 16:26 UTC · original

Real corpus, same method as the note on #1002: the Xbox build's shaders.rpf - 26 084 programs, 40 601 701 IR instructions - victory translate <shaders.rpf> --list, three binaries running at the same time on one thread each (--jobs 1) on a 32-core machine, so they share identical conditions:

binary wall against main
main 940d32f 600 s -
this PR 246c928 593 s -1.2%
#1033 d809fb7 581 s -3.2%

-1.2% is at the bottom of the -1..-5% the PR claimed on synthetic programs, which is what one would expect: the allocation it removes is one per instruction, and decode is a small part of a translation.

The output is identical to main on all 26 084 programs (every summary count and every emit:/resources: line; only the (e.g. ...) examples differ, supplied by whichever thread finishes first).

That the two PRs are -3.2% and -1.2% rather than overlapping noise is also a check on the method: they were measured in the same window as the control.

**@⁠pes7** on GitHub · 2026-11-25 16:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1623#issuecomment-7167998309) Real corpus, same method as the note on #1002: the Xbox build's `shaders.rpf` - **26 084 programs, 40 601 701 IR instructions** - `victory translate <shaders.rpf> --list`, three binaries running **at the same time on one thread each** (`--jobs 1`) on a 32-core machine, so they share identical conditions: | binary | wall | against `main` | |---|---:|---:| | `main` `940d32f` | 600 s | - | | **this PR** `246c928` | **593 s** | **-1.2%** | | #1033 `d809fb7` | 581 s | -3.2% | -1.2% is at the bottom of the -1..-5% the PR claimed on synthetic programs, which is what one would expect: the allocation it removes is one per instruction, and decode is a small part of a translation. The output is identical to `main` on all 26 084 programs (every summary count and every `emit:`/`resources:` line; only the `(e.g. ...)` examples differ, supplied by whichever thread finishes first). That the two PRs are -3.2% and -1.2% rather than overlapping noise is also a check on the method: they were measured in the same window as the control.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:03 UTC · original

Automated triage for the maintainers, second look (a review against main, with the PR built and tested).

This is right as reviewed (an instruction's words fit inline; the tests pass). It no longer builds on main, though: #2143, merged today, reads i.machine.words.len() in code_table_start (crates/victory-shader/src/emit/lower.rs), and words becomes an accessor here. GitHub shows no conflict because the two touch different lines. Please rebase and change that call to i.machine.words().len(); with that one change the two were built and tested together.

Push the change and it will be reviewed again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 22:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/1623#issuecomment-7175873223) Automated triage for the maintainers, second look (a review against `main`, with the PR built and tested). This is right as reviewed (an instruction's words fit inline; the tests pass). It no longer builds on `main`, though: #2143, merged today, reads `i.machine.words.len()` in `code_table_start` (`crates/victory-shader/src/emit/lower.rs`), and `words` becomes an accessor here. GitHub shows no conflict because the two touch different lines. Please rebase and change that call to `i.machine.words().len()`; with that one change the two were built and tested together. Push the change and it will be reviewed again. Thank you for the contribution.
Author
Owner

@⁠pes7 on GitHub · 2026-11-25 22:16 UTC · original

Rebased onto today's main and fixed the one thing the triage found in #2143: code_table_start reads i.machine.words.len(), which the inline array makes a private field. It now goes through the accessor (words().len(), the same slice length), the branch is rebased (4ca9bc1 -> cc2a2bd), and cargo test --release -p victory-shader passes (302 tests, 3 ignored).

No other file needed a change: the rest of the branch applies as it was.

**@⁠pes7** on GitHub · 2026-11-25 22:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1623#issuecomment-7176153023) Rebased onto today's `main` and fixed the one thing the triage found in #2143: `code_table_start` reads `i.machine.words.len()`, which the inline array makes a private field. It now goes through the accessor (`words().len()`, the same slice length), the branch is rebased (`4ca9bc1` -> `cc2a2bd`), and `cargo test --release -p victory-shader` passes (302 tests, 3 ignored). No other file needed a change: the rest of the branch applies as it was.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 20:05 UTC · original · approved these changes

gfx10::Instruction.words moves from a heap Vec<u32> to an inline [u32; MAX_WORDS=5] + len, with a decode-time guard rejecting instructions longer than the budget. All call sites (fields.rs, sp3.rs, apps/victory disasm) converted to words(); the public field removal is workspace-internal. Existing gfx10 decode tests stay green on macOS (96 passed). Nice allocation win for shader-heavy paths — consider a quick cargo bench-style before/after if you have one.

**@⁠catminou4** on GitHub · 2026-11-24 20:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/1623#pullrequestreview-6046496184) · **approved these changes** `gfx10::Instruction.words` moves from a heap `Vec<u32>` to an inline `[u32; MAX_WORDS=5]` + `len`, with a decode-time guard rejecting instructions longer than the budget. All call sites (`fields.rs`, `sp3.rs`, `apps/victory` disasm) converted to `words()`; the public field removal is workspace-internal. Existing gfx10 decode tests stay green on macOS (96 passed). Nice allocation win for shader-heavy paths — consider a quick `cargo bench`-style before/after if you have one.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 19:06 UTC · original · approved these changes

Verified locally merged onto origin/main (63bcca43, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test --locked -p victory-shader --lib gfx10:: — 16 passed, 0 failed
  • cargo test --locked -p victory-shader --lib emit::lower::tests::the_code_table_starts_after_the_instructions — 1 passed, 0 failed
  • cargo test --locked -p victory — 18 passed, 0 failed (14 unit + 4 integration)
  • cargo clippy --locked -p victory-shader -p victory --all-targets -- -D warnings (default and --all-features) — 0 warnings
  • cargo xtask policy — ok (191 files ok)

Review notes

  • Verified all decoder branches in instruction(): every supported encoding produces 1-5 words (MIMG maxes out at 5), and the len > Instruction::MAX_WORDS guard prevents silent truncation or lossy len as u8 conversion.
  • Heads-up for combining with #2012: if #2012 merges before this PR (or vice versa), decode_aligned in gfx10/mod.rs uses .words.len(), which would still compile against the private [u32; 5] array inside the same module and silently evaluate to 5 instead of words().len().
  • Minor non-blocking formatting nit: crates/victory-shader/src/gfx10/mod.rs:137 (control) exceeds the rustfmt line width after switching to words().len().
**@⁠fuzeofficlal** on GitHub · 2026-11-26 19:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/1623#pullrequestreview-6079319366) · **approved these changes** Verified locally merged onto `origin/main` (`63bcca43`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test --locked -p victory-shader --lib gfx10::` — **16 passed, 0 failed** - `cargo test --locked -p victory-shader --lib emit::lower::tests::the_code_table_starts_after_the_instructions` — **1 passed, 0 failed** - `cargo test --locked -p victory` — **18 passed, 0 failed** (14 unit + 4 integration) - `cargo clippy --locked -p victory-shader -p victory --all-targets -- -D warnings` (default and `--all-features`) — 0 warnings - `cargo xtask policy` — ok (`191 files ok`) ### Review notes - Verified all decoder branches in `instruction()`: every supported encoding produces 1-5 words (MIMG maxes out at 5), and the `len > Instruction::MAX_WORDS` guard prevents silent truncation or lossy `len as u8` conversion. - **Heads-up for combining with #2012**: if #2012 merges before this PR (or vice versa), `decode_aligned` in `gfx10/mod.rs` uses `.words.len()`, which would still compile against the private `[u32; 5]` array inside the same module and silently evaluate to `5` instead of `words().len()`. - Minor non-blocking formatting nit: `crates/victory-shader/src/gfx10/mod.rs:137` (`control`) exceeds the `rustfmt` line width after switching to `words().len()`.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/1623/head:github-bot/gh-1623'
git switch 'github-bot/gh-1623'

Merge

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