Skip to content

Shaders: decode finds instructions in a table by dword, not a tree - #2012

Open
0xkn0wh3r3 wants to merge 3 commits into
Marvisak:mainfrom
0xkn0wh3r3:shader/decode-without-a-tree
Open

0xkn0wh3r3 wants to merge 3 commits into
Marvisak:mainfrom
0xkn0wh3r3:shader/decode-without-a-tree

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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::decode follows the control flow from offset 0 and kept every instruction it found in a BTreeMap by offset. That costs, per instruction:

  • a lookup, to stop at an instruction already found;
  • an insertion;
  • a range lookup, for the far-jump check;

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 through range::build, resource::track, io::raise and lower. gfx10::decode was 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, decode now uses a table instead of the tree (decode_aligned):

  1. It records, for each dword, the index of the instruction starting there.
  2. It keeps the instructions in a vector and sorts them by offset once at the end.
  3. The far-jump check only needs the instruction that ends exactly where the s_setpc_b64 starts. 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):

program tree (main) table
32,001 instructions (compares, saveexec, branches, VOP3, buffer stores, scalar loads) 6.1 ms 2.4 ms

This is independent of #1623 (an instruction's words kept inline): that one changes Instruction and instruction(), this one only the container decode collects into.

Testing

  • New test 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.
  • The existing decode tests (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-fast on 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 on main (NVIDIA's 1,000,000-descriptor heap cap, NVIDIA D3D12 (70564ae): stride_tests & synthetic_tests fail due to HEAP_VIEWS (2M > 1M NVIDIA limit) and Stride::Structured with rawBufferLoad #1835).
  • cargo clippy -p victory-shader --all-targets, with and without --features diagnostics: clean.

@Yoshi-233

Copy link
Copy Markdown

I compared gfx10::decode at 940d32f and 0905347 on 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):

Fixture Reachable instructions Base PR
Short straight line 25 2.13 µs 1.54 µs
Long straight line 18,001 3.31 ms 1.13 ms
Short conditional skips with both paths reachable 24,001 4.92 ms 1.48 ms
Same short instruction pattern, followed by 32 KiB of unreachable padding 25 1.87 µs 2.06 µs

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_endpgm and, for the branching case, s_cbranch_scc0. These are decoder-only timings; I did not measure game loading or full translation.

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

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, 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.

zhaibingye added a commit to zhaibingye/VIctory that referenced this pull request Nov 25, 2026
Retain original decoder and tests, but use the existing tree for code
larger than 256 KiB to bound temporary memory per concurrent translation.

Verified portable shader tests and Windows MSVC workspace test compilation.
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

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_aligned makes vec![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

Copy link
Copy Markdown

@0xkn0wh3r3 Thanks for explaining the table allocation. I retested your update at 023252d. All 42,052 decoded instructions from the four original fixtures matched both 0905347 and the PR base 940d32f, 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.

Trailing padding Previous 0905347 Updated 023252d
0 KiB 1.55 µs 1.85 µs
4 KiB 1.63 µs 1.85 µs
32 KiB 2.01 µs 1.84 µs
256 KiB 4.70 µs 1.85 µs

That 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-shader library suite also passed on 023252d (145 passed, 0 failed, 2 ignored).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (unchanged). The push since the last triage (a759494 to 12876d1) is a rebase onto current main. The PR's diff to crates/victory-shader/src/gfx10/mod.rs is identical line for line, and it merges cleanly. The earlier reading still applies. decode_aligned keeps 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 the BTreeMap walk (decode_any) at the first unaligned offset, and the far-jump look-back is bounded by MAX_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

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 023252d onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 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

Copy link
Copy Markdown
Contributor Author

@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):

trailing padding 0905347 023252d a759494
0 KiB 2.16 µs 1.45 µs 1.32 µs
4 KiB 2.13 µs 1.45 µs 1.32 µs
32 KiB 1.77 µs 1.45 µs 1.31 µs

These 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.

0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 25, 2026
`gfx10::decode` follows the control flow from offset 0 and kept every
instruction it found in a `BTreeMap` by offset: a lookup per instruction
to stop at one already found, an insertion per instruction, a range
lookup per instruction for the far-jump check, and a walk of the tree to
put them in order. On synthetic programs that was a large part of
decoding, and decoding about 8% of a translation (sampled).

While every offset it reaches is a dword's, it now records the index of
the instruction starting at each dword in a table, keeps the
instructions in a vector, and sorts them by offset once at the end. The
far-jump check only needs the instruction that ends exactly where the
`s_setpc_b64` starts; 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, as before, so every result
and error is the same.

Measured on an RTX 5070 Ti host (release, best of 30): 32,001
instructions (compares, saveexec, branches, VOP3, buffer stores, scalar
loads) decode in 2.4 ms instead of 6.1 ms.

The new test decodes branches both ways, a loop, data after the end, far
jumps both ways and bad code with both, and checks that a far jump to an
unaligned offset falls back to the tree.
Bytes past the code (data, padding) no longer cost a table entry each:
a short program followed by 32 KiB of padding filled 8,192 entries
up front (about 0.2 us).
Grown entry by entry, a short program's table was reallocated several
times: up to 4,096 entries (16 KiB) are reserved, still none filled, so
padding past the code costs nothing. A 25-instruction program decodes
in 1.32 us here, from 1.45.
@0xkn0wh3r3
0xkn0wh3r3 force-pushed the shader/decode-without-a-tree branch from a759494 to 12876d1 Compare November 25, 2026 22:59
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
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.

5 participants