Repository navigation
Shaders: decode finds instructions in a table by dword, not a tree - #2012
0xkn0wh3r3 wants to merge 3 commits into
Conversation
|
I compared 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 |
catminou4
left a comment
There was a problem hiding this comment.
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.
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.
|
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: |
|
@0xkn0wh3r3 Thanks for explaining the table allocation. I retested your update at 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
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 |
|
Automated triage for the maintainers. Merge candidate (unchanged). The push since the last triage ( CI: passing ( Recommending a look, not approving. Generated by Claude Code |
Offline measurement on a retail dumpResult: no count change against I merged head 023252d onto 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). |
|
@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):
These timings differ from your Linux numbers for @sinan-cmyk thanks for the retail count. It's a decoder speed change, so no change in outcome is what it should show. |
…ot a tree) into demo
`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.
a759494 to
12876d1
Compare
… dword, not a tree
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, 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.