Skip to content

RDNA2 decoder: opcodes from a table per encoding, fields a word at a time - #479

Closed
TG0000 wants to merge 1 commit into
xcl-dev:mainfrom
TG0000:perf/cpu-hotpath
Closed

TG0000 wants to merge 1 commit into
xcl-dev:mainfrom
TG0000:perf/cpu-hotpath

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

The RDNA2 decoder is the first stage of every shader translation. The recompiler reads every operand through the same field_value, so a faster decoder speeds up all of them. Per instruction, decode() used to:

  • look the opcode up in a SipHash HashMap<(encoding, opcode), _> once for every primary encoding whose fixed bits matched,
  • read each field one bit at a time (a field of n bits cost n bounds-checked word reads),
  • build three Vec<u32>: the head, the base words and the final words.

What is in it

  • crates/xcl/src/gpu/rdna2.rs only. Each primary encoding now keeps a dense 1 << width(OP) table of its instructions (at most 1024 entries; the EXP encoding, which has no OP field, uses slot 0). When two instructions claim an opcode, the first one keeps it (spec before console extensions), as the old or_insert did, and the tables are still tried in spec priority order.
  • bits() takes whole runs of a word at once. Bits past the last word still read as 0.
  • The head and base words live in a [u32; 5] on the stack (the widest encoding is 160 bits). Only Decoded::words is allocated: 1 allocation per decode instead of 3.
  • No change to the public API (decode, field_value, Decoded) or to its results.

Release microbenchmark (#[ignore]d test gpu::rdna2::tests::bench_decode): it decodes the 23 812 instructions of the LLVM gfx10 disassembler corpus already in tests/data/llvm-amdgpu and reads every operand field, as the recompiler does. Each figure is the best of 15 runs of 8 rounds. Before and after alternated, with a rebuild each time:

ns / instruction
origin/main 271.1, 267.5, 308.9
this PR 148.0, 149.5, 152.5

That is ≈1.8× faster. The checksum over decoded lengths and field values is identical (sink 1484757240) in every run.

cargo test --release -p xcl --lib rdna2::tests::bench_decode -- --ignored --nocapture

Tested

  • AMD Radeon RX 6800, driver 32.0.21045.29003, Ryzen 9 5900X, Windows 11; rustc 1.100.0 stable-x86_64-pc-windows-msvc; debug and release.
  • New unit tests:
    • bits_matches_one_at_a_time: every offset (also past the end) and every count 0..=64, on 0–6 random words, against the old bit-at-a-time loop.
    • field_value_matches_one_at_a_time_for_every_field: every field of every encoding.
    • opcode_tables_match_first_claim_lookup: every slot of every table against a first-claim HashMap built like the old one.
    • decoded_words_are_the_instruction_dwords: across the whole LLVM corpus, plus a trailing partial dword.
  • The existing llvm_gfx10_corpus test (name and length of 23 812 instructions) and the other decoder tests pass: cargo test --release -p xcl --lib rdna2: 13 passed, 2 ignored (one is the benchmark).
  • cargo test --workspace --no-fail-fast -- --test-threads=4: lib 637 passed, 3 failed, 22 ignored; all integration tests pass (e2e 3/3). The three failures are GPU tests in hle::d3d12x (title_textures::placed_title_textures_share_vram_and_preserve_pixels, title_textures::transitions_apply_when_their_list_is_submitted, vram::d32s8_targets_take_their_own_size_of_video_memory). They fail the same way on origin/main on this machine, in two runs with main's rdna2.rs.
  • cargo clippy -p xcl --all-targets: same warning counts as origin/main (lib 422, lib test 632), so no new warnings.

Regression run

main:

xcl-smoke v1
commit: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 692 (PresentX 692)
presents per 2 s (last 3): 55 55 56
device removals: 0
crashes: 0
peak commit: 15153 MB

PR:

xcl-smoke v1
commit: cbb03e1b250e32801f1a5d6be6c5077e1961fc20
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 802 (PresentX 804)
presents per 2 s (last 3): 55 58 58
device removals: 0
crashes: 0
peak commit: 15157 MB

Not tested

  • Translation time over a full retail shader pack (needs game files in a PR; the microbenchmark uses the LLVM corpus only).
  • Other GPUs and Linux/Wine.

AI use

  • Claude Code (Claude Opus 5.5) found the hot spot, wrote the change, the tests and the benchmark, and ran the tests and smoke runs above.

🤖 Generated with Claude Code

@brothernog

Copy link
Copy Markdown

Independent check of cbb03e1 on NVIDIA. I read the decoder change against the old lookup, and I didn't find a behaviour difference:

  • First claim wins: building the tables with if let Some(s @ None) keeps the old or_insert order. Instructions of non-primary encodings are skipped, as before, since the old by_op entries for them were never looked up. EXP (no OP field) uses slot 0, which is the old no_op map.
  • A table has 1 << field_width("OP") slots, so a decoded OP always indexes inside it. field_width sums the field's ranges.
  • HEAD_WORDS = 5 covers the widest encoding in rdna2_spec.rs: 160 bits, one encoding. base_words is sliced from it, and bytes past the input still read as 0, as read_words did.

Tested

  • NVIDIA GeForce RTX 3050 (driver 610.88), Windows 11 Home 26300, Rust 1.100.0 (MSVC x64), DXC 1.9.2602.17, debug.
  • cargo test -p xcl --lib rdna2: 13 passed, 0 failed, 2 ignored.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 680 passed, 0 failed, 42 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware. Unlike your RX 6800 run, the three hle::d3d12x GPU tests pass here.

Not tested

  • The release benchmark, and a retail title.

This review was AI-assisted.

@eizispark

Copy link
Copy Markdown
Contributor

Hardware check of cbb03e1b250e32801f1a5d6be6c5077e1961fc20 (verified clean merge with main 3db536e): pass.

Tested

  • Hardware: AMD Radeon RX 9070 XT (RDNA4, driver 32.0.32015.16001), AMD Ryzen 7 9800X3D, 32 GB RAM, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profile
  • cargo test --workspace -- --test-threads=4: 680 passed, 0 failed, 42 ignored (GPU tests skipped on this adapter: none; the three hle::d3d12x GPU tests that failed on RX 6800 also pass here)
  • cargo test --release -p xcl --lib rdna2::tests::bench_decode -- --ignored --nocapture: 23812 instructions x 8, best of 15: 12.94 ms, 68.0 ns/instruction (sink 1484757240)

Regression run
main (9ce2070):

xcl-smoke v1
commit: 9ce2070eccf436d7bd38601c90407da447df34f3
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1213 (PresentX 1213)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15615 MB

PR (cbb03e1):

xcl-smoke v1
commit: cbb03e1b250e32801f1a5d6be6c5077e1961fc20
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1569 (PresentX 1569)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14778 MB

Not tested

  • wave32-only GPUs (NVIDIA / Intel); gameplay past the menus

AI use

  • Antigravity (Gemini 4 Carbon) ran the build, test suite, and scripts/smoke.ps1 harness on this machine and formatted this report.

@raylitebull

Copy link
Copy Markdown
Member

Both AMD (RX 9070 XT) and NVIDIA verification look great on cbb03e1. Please rebase onto main to resolve the current rdna2.rs merge conflict so Build and test (Windows, no GPU) can run cleanly.

@TG0000

TG0000 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 1a0bbe3: head 0f112d4. The conflict was in the tests only (main's SDWA text test and this PR's tests now stand side by side); the decoder change is as reviewed.

Run on this head (Ryzen 9 5900X, Windows 11, MSVC): cargo test -p xcl --lib rdna2: 24 passed, 0 failed, 2 ignored. bench_decode, release, one run: 111.4 ns per instruction, the checksum (1484757240) as before; main was not measured again beside it. Clippy stops on three TAU constants in files this PR does not touch, with this toolchain.

Not run on this head: the whole suite, and the xcl-smoke pair. The blocks in the description are of cbb03e1, so the description check is expected to ask for a fresh one until it is run.

AI-assisted (Claude Code).

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

…time

decode() looked an instruction up in a SipHash HashMap keyed by (encoding,
opcode) for every primary encoding whose fixed bits matched, read each field
one bit at a time, and built three Vecs per instruction (head, base words,
final words). The recompiler reads its operands through the same
field_value.

- each primary encoding keeps a dense 1 << width(OP) table of its
  instructions (at most 1024 entries); the first instruction to claim an
  opcode keeps it, as the HashMap's or_insert did
- bits() takes whole runs of a word at once; past the last word still reads 0
- the head and base words are a [u32; 5] on the stack; only Decoded::words
  is allocated

Decoding the LLVM gfx10 corpus (23 812 instructions) and reading every
operand field: 268 -> 149 ns per instruction in release, best of 15.
Tests check bits() and every field of every encoding against the
bit-at-a-time reference, the tables against a first-claim lookup, and
Decoded::words against the instruction bytes.

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

TG0000 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@DavidM1911 Rebased onto latest main (head 0ee075f) — thanks for testing!

@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

@github-actions

Copy link
Copy Markdown

Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Triage (#1607) – head 0ee075f

main already has this change. Commit 2159c3b (#1387, merged) changes gpu/rdna2.rs tables() from the (encoding, opcode) HashMap to indexed per-encoding Vec tables, reads the head without scratch Vecs, and extracts fields of up to 32 bits with a single shift instead of the bit-at-a-time loop in bits. This PR does the same with a slightly different layout (Primary { enc, op, insts }, a word-run loop in bits). It now conflicts with that code.
The parts main lacks (the bits/field equivalence tests, decoded_words_are_the_instruction_dwords) could be a small test-only PR on top of main.
Recommendation: close (superseded by main commit 2159c3b, #1387).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded: this change is already on main (2159c3b). main rdna2.rs has per-encoding opcode tables (no HashMap) and the word-run bits() read; same change as the PR

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

@throny throny added the duplicate This issue or pull request already exists label Dec 1, 2026
@throny throny closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants