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

Open
github-bot wants to merge 1 commit from github-bot/gh-479 into main AGit
Owner

@⁠TG0000 on GitHub · 2026-11-24 00:53 UTC · original
TG0000:perf/cpu-hotpath → main · +215 −43 · 1 files · 1 commits · open

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

**@⁠TG0000** on GitHub · 2026-11-24 00:53 UTC · [original](https://github.com/xcl-dev/xcl/pull/479) `TG0000:perf/cpu-hotpath` → `main` · +215 −43 · 1 files · 1 commits · open 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](https://claude.com/claude-code)
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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:22:53 +00:00
Author
Owner

@⁠brothernog on GitHub · 2026-11-24 01:22 UTC · original

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.

**@⁠brothernog** on GitHub · 2026-11-24 01:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/479#issuecomment-7119042903) 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.
Author
Owner

@⁠eizispark on GitHub · 2026-11-24 02:42 UTC · original

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.
**@⁠eizispark** on GitHub · 2026-11-24 02:42 UTC · [original](https://github.com/xcl-dev/xcl/pull/479#issuecomment-7120401555) 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.
Author
Owner

@⁠raylitebull on GitHub · 2026-11-25 01:12 UTC · original

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.

**@⁠raylitebull** on GitHub · 2026-11-25 01:12 UTC · [original](https://github.com/xcl-dev/xcl/pull/479#issuecomment-7149307723) 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.
Author
Owner

@⁠TG0000 on GitHub · 2026-11-25 12:28 UTC · original

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

**@⁠TG0000** on GitHub · 2026-11-25 12:28 UTC · [original](https://github.com/xcl-dev/xcl/pull/479#issuecomment-7162426098) 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).
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 14:05 UTC · original

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

**@⁠DavidM1911** on GitHub · 2026-11-25 14:05 UTC · [original](https://github.com/xcl-dev/xcl/pull/479#issuecomment-7164737499) Verification on RTX 3060 Ti (Windows 11): **merge conflict with current `main`** — needs a rebase before it can be tested.
Author
Owner

@⁠TG0000 on GitHub · 2026-11-25 15:04 UTC · original

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

**@⁠TG0000** on GitHub · 2026-11-25 15:04 UTC · [original](https://github.com/xcl-dev/xcl/pull/479#issuecomment-7166262312) @⁠DavidM1911 Rebased onto latest `main` (head `0ee075f`) — thanks for testing!
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 02:45 UTC · original

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

**@⁠DavidM1911** on GitHub · 2026-11-26 02:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/479#issuecomment-7181099184) 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
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/479/head:github-bot/gh-479'
git switch 'github-bot/gh-479'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-479'
git switch 'github-bot/gh-479'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-479'
git switch 'github-bot/gh-479'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-479'
git switch 'main'
git merge --squash 'github-bot/gh-479'
git switch 'main'
git merge --ff-only 'github-bot/gh-479'
git switch 'main'
git merge 'github-bot/gh-479'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.