Skip to content

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

Open
pes7 wants to merge 2 commits into
Marvisak:mainfrom
pes7:perf/instruction-words
Open

pes7 wants to merge 2 commits into
Marvisak:mainfrom
pes7:perf/instruction-words

Conversation

@pes7

@pes7 pes7 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

@catminou4 catminou4 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.

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.

Korsuma added a commit to RicerBoy/DropItLikeItsHot that referenced this pull request Nov 24, 2026
…, #188)

Integrates selected runtime, GPU, and shader fixes from FulGerNet/macca-cl
(cd81e89, GPL-3.0-or-later) plus remaining clean upstream PRs on top of
origin/main (da435f1):

From FulGerNet/macca-cl:
- console_insn.rs: mwaitx yields (std::thread::yield_now) instead of spinning
  with pause so waiting threads do not starve video/audio/pipeline workers.
- texture.rs: mark view-aliased textures (Bink intro video frames) so they hash
  on every refresh rather than every 8th frame.
- pipeline/make.rs: VICTORY_PIPELINE_THREADS override, 3/4 core default (2-8),
  early prewarm at THREAD_PRIORITY_LOWEST and draw-requested at NORMAL.
- pipeline/library.rs: per-writer atomic cache staging files (+2 tests).
- shader_cache.rs & emit/{mod,shader}.rs: key translated shader cache by
  translation_limits() (host_max_wave + view_heap_limit) and wave/strict env
  options (+test), plus DXIL validator warning pass (VICTORY_DXIL_STRICT=1).
- emit/{mod,lower}.rs: last-resort wave-wide compute fallback on 32-lane hosts
  before WaveSize(64) (VICTORY_WAVE_WIDE=0 disables), plus mixed flat and
  interpolated input row lowering (+test).
- draw.rs, list.rs, queue.rs: host heap COM-pointer tracking (ABA guard),
  root-signature invalidation, recycled transient table submission guard, and
  initial pipeline bind on list creation (+3 tests).
- kernel.rs & xgame.rs: CP_ACP ANSI path encoding for GetModuleFileNameA and
  XLaunchNewGame (+test).

From upstream PRs:
- fxceless/victory-fork#25: uniform control-flow immediate samplers (+test).
- fxceless/victory-fork#24: cap constant buffer views at 64 KiB (+test).
- fxceless/victory-fork#23: gameinput reading history 32 -> 256.
- Marvisak/VIctory#1623: inline GFX10 instruction words (+from_word helper).
- Marvisak/VIctory#188: SSE2 16-byte TLS scan at startup.

Verified on Windows 11, RTX 4080 16 GB (Wave32), Ryzen 7 7800X3D, 32 GB RAM:
- cargo xtask policy --staged: 22 files ok
- cargo clippy --workspace --all-features --lib --tests -- -D warnings: clean
- cargo test --workspace: 0 failures (215/215 victory-shader passed)
- Live 2560x1440 diag run (VICTORY_DISPLAY_MODE=2560x1440): smooth Bink intro
  video at 30.0 fps (33.3 ms/frame, 12.7 MB/frame texture streaming, 0.4 ms GPU,
  0 dropped draws) -> UI Text Size setup screen (25.4-30.0 fps, 7.1 GB private,
  552 MB VRAM), 0 crashes (crashes.log empty).

Fixes #6
Fixes #8
@pes7
pes7 force-pushed the perf/instruction-words branch from e9254f2 to 72eff75 Compare November 24, 2026 22:32
pes7 added a commit to pes7/VIctory that referenced this pull request Nov 24, 2026
Marvisak#1180 and Marvisak#1623 both change `Instruction`, so they conflict with each
other in `gfx10/mod.rs`: this branch is stacked on `perf/instruction-name`
(Marvisak#1180) so that whichever lands second still applies.

The two are independent in what they save and compose without a trade:
`words` stays the inline `[u32; MAX_WORDS]` with `len` (Marvisak#1623), and
`name` stays resolved once at decode (Marvisak#1180), so an instruction carries
both and neither costs the other its win.

Merge Marvisak#1180 first; this diff then shrinks to the `words` change alone.
@pes7

pes7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

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 gfx10: resolve the opcode name once, at decode time (translation -4..-8%) #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).

Airamek added a commit to Airamek/VIctory that referenced this pull request Nov 24, 2026
Translation speed (compile-storm CPU): Marvisak#1623 inline instruction words
(decode -18..-45%), Marvisak#1834 VALU names without heap vector, Marvisak#1833 live-set
bit walk, Marvisak#1802 relative shift bounds, Marvisak#1824 dx.op declarations once.
Per-draw/per-list CPU: Marvisak#1825 view-stride table once per list, Marvisak#1826
object names only where logged, Marvisak#1800 capped-heap log spam once per 64Ki.
Tooling: Marvisak#1809 list pipeline keys without cache init.
Locking: Marvisak#1680 pipeline library loads without the global mutex.

Held: Marvisak#1819 (2^20->2^14 loop bound) breaks transcendental convergence
tests (sin/cos/exp/sqrt) — accuracy-lowering, rejected per policy.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

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.

@Marvisak

Copy link
Copy Markdown
Owner

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.

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 Marvisak#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.
@pes7
pes7 force-pushed the perf/instruction-words branch from 246c928 to cc2a2bd Compare November 25, 2026 22:16
…visak#2143 added

Marvisak#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.
@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

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.

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.

3 participants