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

Open
github-bot wants to merge 3 commits from github-bot/gh-2012 into main AGit
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 03:03 UTC · original
0xkn0wh3r3:shader/decode-without-a-tree → main · +138 −0 · 1 files · 3 commits · open

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, #1835).
  • cargo clippy -p victory-shader --all-targets, with and without --features diagnostics: clean.
**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 03:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/2012) `0xkn0wh3r3:shader/decode-without-a-tree` → `main` · +138 −0 · 1 files · 3 commits · open ## 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, #1835). - `cargo clippy -p victory-shader --all-targets`, with and without `--features diagnostics`: clean.
`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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:51:37 +00:00
Author
Owner

@⁠Yoshi-233 on GitHub · 2026-11-25 03:17 UTC · original

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.

**@⁠Yoshi-233** on GitHub · 2026-11-25 03:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2012#issuecomment-7151779865) 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.
Author
Owner

@⁠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_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).

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 07:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2012#issuecomment-7155766868) 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).
Author
Owner

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

**@⁠Yoshi-233** on GitHub · 2026-11-25 07:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2012#issuecomment-7155949523) @⁠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).
Author
Owner

@⁠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 (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

**@⁠Marvisak** on GitHub · 2026-11-25 09:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2012#issuecomment-7158138030) · edited 2026-11-26 01:20 UTC <!-- victory-triage v1 head=12876d139a0becb7a0c0b41db14ce0bd2a3239a5 verdict=merge-candidate at=2026-11-26T01:20:27Z --> 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](https://claude.ai/code)_
Author
Owner

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

**@⁠sinan-cmyk** on GitHub · 2026-11-25 09:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/2012#issuecomment-7158981812) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 023252d5 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).
Author
Owner

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

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** on GitHub · 2026-11-25 18:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/2012#issuecomment-7171158830) @⁠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.
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/2012/head:github-bot/gh-2012'
git switch 'github-bot/gh-2012'

Merge

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