Shaders: decode finds instructions in a table by dword, not a tree #2012
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2012
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@0xkn0wh3r3 on GitHub · 2026-11-25 03:03 UTC · original
0xkn0wh3r3:shader/decode-without-a-tree→main· +138 −0 · 1 files · 3 commits · openThe 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.@Yoshi-233 on GitHub · 2026-11-25 03:17 UTC · original
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.@0xkn0wh3r3 on GitHub · 2026-11-25 07:21 UTC · original
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. 023252d makes the table grow to the furthest dword reached instead, so bytes past the last instruction cost nothing (decode tests and clippy pass).@Yoshi-233 on GitHub · 2026-11-25 07:31 UTC · original
@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).@Marvisak on GitHub · 2026-11-25 09:17 UTC · original · edited 2026-11-26 01:20 UTC
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
@sinan-cmyk on GitHub · 2026-11-25 09:55 UTC · original
Offline measurement on a retail dump
Result: no count change against
main.I merged head 023252d5 onto
main940d32f(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).
@0xkn0wh3r3 on GitHub · 2026-11-25 18:37 UTC · original
@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. a759494 reserves 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.
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.