Skip to content

[upstream #1082] Shaders: console containers are walked without decoding instructions - #73

Closed
ufoptg wants to merge 1 commit into
mainfrom
upstream-pr/1082
Closed

ufoptg wants to merge 1 commit into
mainfrom
upstream-pr/1082

Conversation

@ufoptg

@ufoptg ufoptg commented Nov 24, 2026

Copy link
Copy Markdown
Owner

While a title loads, every console shader it submits goes through xbox::parse, and once through the instruction statistics of shader::capture. Both only need an instruction's length, whether it ends the program, its branch offset and its mnemonic, but they used rdna2::decode for that: three Vecs, SipHash lookups and bit-at-a-time field reads per instruction, with each program decoded in full three times.

I sampled a retail title's running threads for its first 40 s on main (6efdabc): xbox::parse was in 20.3 % of the samples and InstStats::add_shader in 10.8 %, on the threads that make the title's pipelines.

This adds rdna2_scan::step, which walks code from tables built from the decoder's own tables and does not allocate. decode() is unchanged and stays the reference that the tests compare against.

A whole retail pack: 26 503 shaders, 44.4 M instructions, one thread before after
xbox::parse (about 2 s of both is inflate) 38.1 s 4.7 s 8.1x
instruction statistics 23.3 s 1.8 s 13x

A digest over the Debug output of every parsed shader and of its statistics is the same before and after, for the whole pack.

Synthetic benchmark (bench_scan, release, three runs, other load on the machine). "Before" is the old walk, kept in the tests as the reference:

ns / instruction before after
split_segments 230 - 253 21 - 22 11 - 12x
branch_targets 346 - 370 20 - 22 17 - 18x
statistics 408 - 466 21 - 22 19 - 21x

The same sampling on this PR's head (one run each): 2150 samples of running threads instead of 3570; xbox::parse and InstStats::add_shader are below 12 % each over the run (no longer in the sampler's top 26; on one thread and 4 s window parse still had 13.5 %, inflate included). shader::capture itself is still visible (its lock and hashing, which xcl-dev#1026 is about). One run each, so take it as a direction, not a figure.

Related: xcl-dev#1026 moves the statistics of shader::capture to a worker thread (shader.rs only, no shared file). This PR makes that work cheaper, and also the parse that every pipeline translation does on its own thread. xcl-dev#479 speeds up decode() itself. Both change rdna2.rs; I have not tried merging them together.

What is in it (one commit)

  • gpu/rdna2_scan.rs (new): the tables, step, and its tests.
  • gpu/xbox.rs: split_segments, branch_targets, is_data_block and InstStats use it. A segment with s_getpc_b64 three instructions before an s_setpc_b64 (where resolve_far_jumps may find a far jump) still goes through decode. Mnemonics are counted by number and named once per shader.
  • gpu/rdna2.rs: three helpers are visible to the sibling module; mnemonic is split into mnemonic_of. No change in behaviour.
  • build.rs: the new file is part of the translator's source identity. As with any change to these sources, the translation cache is rebuilt once.

It is over 400 changed lines because more than half of them are the tests and the old walks they compare with.

Tested

  • AMD Radeon RX 6800 (driver 32.0.21045.29003), Ryzen 9 5900X, Windows 11 build 26300, Rust 1.100.0 stable-x86_64-pc-windows-msvc.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 (debug): 741 passed, 3 failed, 42 ignored. The three failures are title_textures::placed_title_textures_share_vram_and_preserve_pixels, title_textures::transitions_apply_when_their_list_is_submitted and vram::d32s8_targets_take_their_own_size_of_video_memory; the same three fail on main (384f7f7) on this machine.
  • SKIPPED: test_title_draws_with_a_root_signature_under_the_debug_layer and uploads_start_from_each_subresources_state (no D3D12 debug layer).
  • New tests: every_opcode_steps_as_it_decodes (every opcode of every primary encoding, assigned or not, with literals, SDWA / DPP and cut short), random_dwords_step_as_they_decode (100 000), programs_and_statistics_are_as_decoded (3 000 random programs with branches, far jumps, data blocks and padding: segments, branch targets, data blocks and statistics against the old walks).
  • cargo test --release -p xcl --lib scan_tests -- --ignored --nocapture for the benchmark.
  • A retail title reaches the same 60 presents per 2 s as main. The frame counts of these runs vary too much from run to run (earlier runs of 6efdabc gave 416 and 928) to show the gain.

Regression run

The title: line of both blocks is replaced by a neutral name; the rest is as scripts/smoke.ps1 printed it. The main block is from a run on the same machine by another session.

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 954 (PresentX 954)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15118 MB

PR:

xcl-smoke v1
commit: 3c325a9080790815bae0e79349ac24d93f2bb52b
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 1030 (PresentX 1030)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15102 MB

Not tested

AI use

  • Claude Code with Claude Opus 5.5 found the cost with a sampling profiler of its own, wrote the change, the tests and this description, and ran everything above.

Mirror of xcl-dev#1082 (auto-synced, do not edit).

xbox::parse decoded every program twice in full (segments, branch targets; a
third time for a segment that may be data) and the statistics of a captured
shader once more: three Vecs, hash lookups and bit-at-a-time fields per
instruction, for its length, terminator, branch offset and mnemonic. Sampled
in a retail title's first 40 s, the two were 31 % of the samples of its
running threads, on the threads that make its pipelines.

rdna2_scan::step gives those from tables made of the decoder's own, without
allocating; mnemonics are counted by number and named once. decode() is
unchanged and stays the reference: every opcode of every encoding, random
dwords and random programs are compared with it, and a segment with a far
jump still goes through it.

A whole retail pack (26 503 shaders, 44.4 M instructions), one thread:
parse 38.1 -> 4.7 s (2 s of both is inflate), statistics 23.3 -> 1.8 s, with
the same result for every shader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ufoptg

ufoptg commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Upstream PR xcl-dev#1082 was closed or merged.

@ufoptg ufoptg closed this Nov 25, 2026
@ufoptg
ufoptg deleted the upstream-pr/1082 branch November 25, 2026 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants