Shaders: decode finds instructions in a table by dword, not a tree #2010
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2012"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T03:03:46Z.
The problem
Decoding a console program into instructions was slower than it needs to be. On synthetic programs it was a large part of decoding, and decoding is about 8% of a translation (sampled).
gfx10::decodefollows the control flow from offset 0 and kept every instruction it found in aBTreeMapby offset. That costs, per instruction:plus a walk of the tree at the end to put them in order.
How it was found
A sampling profile (a helper thread suspending the translating thread every 0.5 ms and unwinding it) over a mix of synthetic compute programs (straight-line VALU, if/endif with
s_and_saveexec, nested ifs, buffer stores, scalar ALU), all translated throughrange::build,resource::track,io::raiseandlower.gfx10::decodewas 8.2% of the samples inclusive, with the tree's inserts and lookups a large part of it. No title shaders were available, so the share in a real corpus may differ.The fix
While every offset it reaches is a dword's,
decodenow uses a table instead of the tree (decode_aligned):s_setpc_b64starts. The nearest one found below it, within the longest instruction's reach (5 dwords), is the tree's answer wherever that can end there.An offset that is no dword's (only a far jump's literal can give one) leaves the whole decode to the tree (
decode_any), as before. Every result and error is the same.Measured on an RTX 5070 Ti host (release, best of 30):
This is independent of #1623 (an instruction's words kept inline): that one changes
Instructionandinstruction(), this one only the containerdecodecollects into.Testing
gfx10::tests::the_table_decodes_as_the_tree_does: decodes branches both ways, a loop, data after the end, far jumps both ways and bad code with both, and checks the table gives the tree's result. It also checks that a far jump to an unaligned offset falls back to the tree.follows_branches_and_skips_data,follows_far_jumps,follows_far_jumps_backward,rejects_bad_code) now run through the table.cargo test --release -p victory-shader --no-fail-faston an RTX 5070 Ti (driver 621.24): 245 passed. The 3 failures (emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride,emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,emit::synthetic_tests::gathers_at_a_level_match_the_hardware) fail the same way onmain(NVIDIA's 1,000,000-descriptor heap cap, #1835).cargo clippy -p victory-shader --all-targets, with and without--features diagnostics: clean.Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T03:34:29Z.
Review: APPROVED
Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main
940d32f69b,cargo test -p victory-shader --locked.145 passed / 0 failed. Decode finds instructions in a table by dword, not a tree (+1 test) — decode lookup perf/simplicity; suite green.
Imported from Marvisak/VIctory. Originally posted by Yoshi-233 on 2026-11-25T03:17:45Z.
I compared
gfx10::decodeat940d32fand0905347on x86-64 Linux with synthetic, valid programs. I compared the offset, encoding, opcode, name and instruction words for every output instruction. All 42,052 entries across the four fixtures matched in order.Median time per decode, including dropping the result (release, Rust 1.98.1, nine alternating pairs of batches pinned to the same logical CPU):
The long inputs were faster in all nine pairs; the padded short input was slightly slower in all nine. The last case isolates sensitivity to the supplied byte slice length while keeping the reachable instruction count fixed. I used
s_mov_b32,v_mov_b32,s_nop,s_endpgmand, for the branching case,s_cbranch_scc0. These are decoder-only timings; I did not measure game loading or full translation.Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T07:21:17Z.
Thanks for the comparison. 42,052 instructions matching in order is a better check than the unit test.
The padded case is the per-dword table:
decode_alignedmakesvec![u32::MAX; code.len() / 4]up front. 32 KiB of padding is 8,192 entries to fill, about 0.2 µs, which matches 1.87 → 2.06 µs. It's linear in the slice, not in the reachable code.023252dmakes the table grow to the furthest dword reached instead, so bytes past the last instruction cost nothing (decode tests and clippy pass).Imported from Marvisak/VIctory. Originally posted by Yoshi-233 on 2026-11-25T07:31:11Z.
@0xkn0wh3r3 Thanks for explaining the table allocation. I retested your update at
023252d. All 42,052 decoded instructions from the four original fixtures matched both0905347and the PR base940d32f, in order and field for field.To isolate the padding cost, I used the same 25 instructions followed by zeros in a fixed-size buffer and varied only the slice length passed to
gfx10::decode. These are median times per decode on x86-64 Linux, using Rust 1.98.1 in release mode, pinned to one logical CPU, with 12 rounds per revision.0905347023252dThat confirms the fix on this machine. The updated decoder stayed within 1.84–1.85 µs across all four lengths and was faster in all 12 rounds at both 32 and 256 KiB. The trade-off I measured was about 0.29 µs (+19%) for the unpadded 25-instruction case. On the original long straight-line and branching fixtures, the updated head was within 1% of the previous head. These timings cover CPU decoding only.
The release-mode
victory-shaderlibrary suite also passed on023252d(145 passed, 0 failed, 2 ignored).Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:17:44Z.
Automated triage for the maintainers.
Merge candidate (unchanged). The push since the last triage (
a759494to12876d1) is a rebase onto currentmain. The PR's diff tocrates/victory-shader/src/gfx10/mod.rsis identical line for line, and it merges cleanly. The earlier reading still applies.decode_alignedkeeps a per-dword index table, reserved up to 4,096 entries and grown only to the furthest dword reached, and sorts once at the end. It falls back to theBTreeMapwalk (decode_any) at the first unaligned offset, and the far-jump look-back is bounded byMAX_WORDS. The independent field-by-field comparison (42,052 instructions) and the unchanged retail translate count also still apply.CI: passing (
check).Recommending a look, not approving.
Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T09:55:22Z.
Offline measurement on a retail dump
Result: no count change against
main.I merged head
023252d5ontomain940d32f(clean merge) and ranvictory translate --validate --listonshaders.rpf(1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result withmainprogram by program (25,889 programs). Validated: 23,918 onmain, 23,918 with this PR.No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.
No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T18:37:10Z.
@Yoshi-233 thanks for the sweep. The +0.29 µs on the unpadded case is the table growing entry by entry: a short program reallocated it several times.
a759494reserves room for up to 4,096 entries (16 KiB) up front, without filling any, so padding past the code still costs nothing.Median per decode for a 25-instruction program on this machine (Windows, release, 21 rounds of 20,000):
0905347023252da759494These timings differ from your Linux numbers for
0905347(an allocator difference, I'd guess). Here the reserved version is the fastest at every length.@sinan-cmyk thanks for the retail count. It's a decoder speed change, so no change in outcome is what it should show.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:06:48Z.
Review: APPROVED
Verified locally merged onto
origin/main(63bcca43, Windows 11 x64, Rust 1.98.1 MSVC):cargo test --locked -p victory-shader --lib gfx10::— 17 passed, 0 failed (includingthe_table_decodes_as_the_tree_does)cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default and--all-features) — 0 warningsrustfmt --edition 2024 --check crates/victory-shader/src/gfx10/mod.rs— cleancargo xtask policy— ok (191 files ok)Review notes
instruction():MAX_WORDS = 5holds across all encodings (MIMG up to 5 words, VOP3/VOP3P up to 3, all others 1-2), so the1..=MAX_WORDSlookback inbefore(end)matchesBTreeMap::range(..at).next_back()forfar_jumpwhile falling back cleanly todecode_anyon unaligned far-jump targets.decode_alignedreadsinst.words.len()andw[0].words.len()insidegfx10/mod.rs. Once #1623 turnsInstruction::wordsinto a private[u32; 5]array, those two calls will still compile inside the module and silently return5instead ofwords().len(), so whichever PR merges second will need both updated to.words().len()(and can shareInstruction::MAX_WORDS).Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T22:51:29Z.
Review: APPROVED
LGTM — verified on Windows against
origin/main(63bcca4).cargo test -p victory-shader --lib gfx10:: -- --test-threads=1: all passed.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).
Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-27T22:55:39Z.
A silent conflict between this PR and #1623, found while merging all
triage: merge candidatePRs together on one branch (details on #3347).words: [u32; Instruction::MAX_WORDS]plus a length, read throughInstruction::words().inst.words.len()(crates/victory-shader/src/gfx10/mod.rs, thenextoffset and the overlap check).Each PR is correct on its own and both merge cleanly into
main. Merged together,words.len()is always 5, so every instruction is walked as five dwords and the decoder refuses most programs. In the combined tree that took victory-shader's lib tests from 214 to 146 passed (335 failed). Usingwords()in those two places (inst.words().len(),w[0].words().len()) brings it to 344 passed / 137 failed; the remaining failures are unrelated (47 are "no SM 6.6 device" under Wine).Whichever of the two lands second needs that two-line change. Reference commit on the integration branch:
github.com/neonbyte1/VIctory@17c7845dd(merge resolution only).Measured with CPU tests under Wine on Linux; no game data involved.
AI-assisted (Claude); measured on my machine, no game data shared.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.