Skip to content

Performance: Optimize RDNA2 Shader Translation and Resource Tiling - #7944

Open
ManiPeak wants to merge 1 commit into
ItsNotRexed:mainfrom
ManiPeak:main
Open

ManiPeak wants to merge 1 commit into
ItsNotRexed:mainfrom
ManiPeak:main

Conversation

@ManiPeak

@ManiPeak ManiPeak commented Nov 23, 2026 •

Copy link
Copy Markdown

Performance: Optimize RDNA2 Shader Translation and Resource Tiling

Summary

This PR introduces targeted performance optimizations to two CPU-side hot paths in Leonida:

  1. RDNA2 Shader Translation
  2. 64 KiB SW_64KB_D Resource Tiling and Detiling

The changes focus on eliminating repeated computations, reducing unnecessary allocations and string processing, and improving memory access efficiency without changing the generated SPIR-V output or intended behavior.

Shader Translation Optimizations

  • Centralized and memoized opcode classification in "shader_ir.base_op".
  • Added memoization for operand parsing while preserving independent mutable results.
  • Reduced unnecessary string copying during shader parsing.
  • Optimized construct-stack tracking in "Gen.emit".
  • Avoided repeated operand splitting and mnemonic classification during instruction analysis.
  • Reduced redundant processing in comparison, lane-local analysis, and type deduplication.

Resource Tiling and Detiling Optimizations

  • Reduced repeated per-element address calculations.
  • Reused intra-tile offsets across tile columns.
  • Improved row processing through grouped element operations.
  • Specialized tiling and detiling direction at compile time.
  • Preserved support for different element sizes and texture layouts.

Benchmark Results

Shader Translation

Measured across 97 fixtures:

Metric| Before| After
Best time| 342.1 ms| 294.2 ms
Median time| 361.0 ms| 312.5 ms
Speedup| | 1.16x

The generated SPIR-V output was byte-identical to the baseline.

Resource Tiling and Detiling

Selected measured speedups:

  • 1080p RGBA8: up to 1.74x
  • 1024² R8: up to 2.46x
  • 1024² RG8: up to 2.36x
  • BC1 2048²: up to 1.75x

Larger memory-bound workloads showed smaller improvements, as expected.

Testing

  • Clean CMake configure and build.
  • CTest: 21 passed, 1 skipped out of 22.
  • Semantic tests: 5,821 tests executed.
  • Python test suite: 7,635 passed; 25 failures verified as pre-existing.
  • Viewer tests: 56 passed; 1 environment-related failure verified against the baseline.
  • "git diff --check": clean.
  • Resource detiling tests cover multiple element sizes, tile boundaries, block-compressed layouts, and 240 deterministic randomized shapes.

Limitations

Windows runtime execution and the D3D12-to-Vulkan integration were not tested in the available Linux environment.

The complete 26,503-shader corpus also requires game data and was not benchmarked.

"spirv-val" was unavailable, so SPIR-V equivalence was verified through byte-for-byte textual output comparison rather than external validation.

Additional Notes

This contribution does not include game data, encryption keys, or decryption functionality.

The changes are focused on measurable CPU-side performance improvements while preserving existing behavior.

@ManiPeak ManiPeak closed this Nov 23, 2026
@ManiPeak ManiPeak reopened this Nov 23, 2026
@ManiPeak ManiPeak changed the title Add files via upload Performance: Optimize RDNA2 Shader Translation and Resource Tiling Nov 23, 2026
@kamilolejnik

Copy link
Copy Markdown

This branch would revert about 37 upstream commits, so it can't be merged as it stands. It looks like the 15 files were copied from a tree based on 1cc6e3c12 (the base named in CHANGED_FILES.txt) onto the newer dde82f27e that the branch is based on. The 310 commits between those two include changes to these same files, and the copies drop them.

How I checked: for every commit in 1cc6e3c12..dde82f27e that touches one of the PR's files, I tried git apply --check of that commit's diff against the PR's version of the file. If it applies forward, the change is missing from the PR. Result: 37 such commits, plus 4 that were unclear. In tools/rdna2spirv.py alone 16 are missing, among them:

runtime/CMakeLists.txt is missing 11, docs/README.md 3, tools/shader_ir.py 2 and tools/wave64_halves.py 2.

The semantic suite agrees (python3 -I scripts/run_semantic_tests.py tests/semantic, CPython 3.13, Linux):

tree result
merge-base dde82f27e 447 files, 5956 tests, 2 files failed (test_image_atomics_guard, test_image_store_d16, both known)
this PR 18f8dce23 447 files, 5956 tests, 20 files failed: test_atomic_csub, test_buffer_store_sub_fixture, test_buffer_sub16_fixture, test_buffer_sub_dword, test_float_nan_fract, test_fma_khr_f64, test_fma_mix_literal, test_image_atomics_guard, test_image_coherent, test_image_cube_store_glc, test_image_gather4_variants, test_image_sample_bias_nonps, test_lds_volatile, test_pk_mul_lo_const, test_sample_c, test_scalar_noop_waitcnt, test_smem_clock, test_wave32_bfm64_data, test_wave32_pixel_subgroup64, test_wave64_emulate_opt

The "byte-identical SPIR-V" result in the description was most likely measured against the old 1cc6e3c12 base, which would explain why it didn't show up. The optimisations themselves may well be fine. Re-applying them as a diff on top of current main (for example git diff 1cc6e3c12 <your tree> and then git apply -3), instead of copying whole files, should keep both.

AI-assisted (Claude Code).

@moistman7

Copy link
Copy Markdown

This PR reverts recent main code. The SPIR-V does change, and 12 runtime tests are dropped.

The single commit 18f8dce23 ("Add files via upload") has merge base dde82f27e, current main. Against that base it removes code and tests that are on main today, which looks like whole files uploaded from an older checkout.

Tests unregistered: runtime/CMakeLists.txt loses 12 add_test entries. All of their sources are still in the tree:

  • crt_timed_wait, d3d12_mesh and d3d12_set_descriptor_heaps;
  • d3d12_dxr_validation, d3d12_dxr_validation_khr and d3d12_execute_indirect_validation;
  • int3_skip, run_argv_modes, spirv_capability and vk_shader_fma;
  • xs_proxy_mock_env_slots and xs_proxy_mock_env_slots_invalid.

That's why CTest reports 22 tests here, against about 262 on main.

Translator code removed: from tools/rdna2spirv.py, src_f_sign_mods (#6685), emit_fma64, emit_gather4_lod, gather_mip, gather_depth_compare[_prep], buffer_sub, wave32_first_running_lane and Gen.GATHER4_LOD_OPS. From tools/wave64_emulate_opt.py, _solve_scalar_liveness and the *_with_meta helpers.

Steps (Windows 11, Python 3.12.10, PR head checked out; LLVM_MC set):

python -I -m unittest discover -s tests/semantic -p <file> main dde82f27e this PR
test_float_nan_fract.py OK FAILED (failures=2)
test_image_gather4_variants.py OK FAILED (errors=95, AttributeError: type object 'Gen' has no attribute 'GATHER4_LOD_OPS')

On hardware: I measured v_min/max/min3/max3/med3_f32 with src0 abs/neg on NaN inputs on an AMD gfx1036 iGPU (tools/hw_oracle, details in #7054). main's translator matches 160/160 words; this PR's matches 130/160. For example, v_min_f32_e64 v2, |v1|, v4 with v1 = 0x7F800001, v4 = 0x7FC00000 gives 0x7F800001 on the hardware and on main, and 0x7FC00001 here.

The detiling and memoization changes might still be worth having. Rebuilding the branch from current main with only those edits would keep the tests and the newer translator code.

Tested by me with AI assistance (Claude Code). No game data.

@ebnimaa

ebnimaa commented Nov 23, 2026

Copy link
Copy Markdown

Independent Windows verification for Leonida PR #7944
#7944

Intel Core i5-12400, AMD Radeon RX 6700 XT (Adrenalin 26.10.1,
32.0.21045.16010), Windows 11 25H2 (26220.9836), CPython 3.12.14,
MSVC 19.44.35227 Release, Windows SDK 10.0.26100.0, glslang 16.6.0,
Vulkan device API 1.4.315, and Khronos validation layer 1.4.363.

Baseline dde82f2 (PR parent).
PR head 18f8dce.

Shader translation on the i5-12400

Median wall time per pass over identical, synthetic RDNA2 listings. Timings
exclude input preparation, assembly, and validation. Each case had 20 timed
passes per revision after three warm-up pairs, alternating which ran first.

  • 100 listings, with stub DXBC only: 463.63 ms to 423.67 ms. Both revisions
    translated the same 52 listings and emitted byte-identical SPIR-V.
  • 100 listings with 48 synthetic FSSM compute headers: 744.69 ms to
    678.91 ms. The baseline translated all 100 and the patch translated 98.
  • 91 listings with byte-identical output: 708.52 ms to 643.52 ms (9.2% less
    time), with both revisions completing the same work.

Of the 100 listings in the compute-header run, 91 matched byte-for-byte in both
SPIR-V assembly and binary form. Seven emitted different assembly and binary:
cs_pk_mul_lo_const, cs_w32_fcmpswap, cs_w32_lds_subdword, cs_w32_lds_u8_d16,
cs_w32_lds_wide, cs_w32_misc_ext, and cs_w32_new_ops. The remaining two compiled
on the baseline but were rejected on the PR:

  • cs_w32_buffer_store_sub: Unsupported: buffer_store_byte
  • cs_w32_buffer_sub16: Unsupported: buffer_load_ubyte_d16

The diff removes the translator's BUFFER_SUB16 dispatch entries for both
instructions. Every emitted module from the 100-listing comparison passed
SPIRV-Tools v2026.4 validation for Vulkan 1.2. The shader_ir unit tests passed
39/39 on the baseline and 44/44 on the PR.

64 KiB texture tiling on the i5-12400

CPU medians in milliseconds, 20 paired samples per shape after four warm-up
pairs, alternating revision order in one process. The same MSVC /O2 harness
called each revision's header. The BC1 fixture used 8 bytes per 4x4 block.

Surface | Tile baseline | Tile PR | Detile baseline | Detile PR
1080p RGBA8 | 1.393 | 1.171 | 1.582 | 1.012
4K RGBA8 | 9.409 | 9.091 | 12.685 | 10.227
1024x1024 R8 | 0.545 | 0.233 | 0.545 | 0.278
1024x1024 RG8 | 0.583 | 0.259 | 0.616 | 0.285
1024x1024 RGBA32F | 1.786 | 1.984 | 1.935 | 1.836
4096x4096 RGBA16F | 28.379 | 27.886 | 31.674 | 26.812
2048x2048 BC1 | 0.177 | 0.134 | 0.175 | 0.104

Both revisions produced identical tiled buffers, including padding, and
detiled the same bytes back to the original buffer. Parity held across all
seven benchmark shapes and 240 additional deterministic randomized shapes.
RGBA32F tiling took about 11% longer in this run.

Windows Vulkan on the RX 6700 XT

  • With validation enabled, d3d12_subresource_copy and d3d12_draw passed on both
    revisions. d3d12_texture_tiles failed at the same 256x240 row-major alias
    check on both (first pixel 0x020000CC instead of 0xCC020000). The 256x256
    case matched native D3D12 across all 262,144 checked pixels. No Vulkan
    validation errors were reported. The CPU resource_detile CTest also passed
    on both revisions.
  • Six translated compute fixtures produced the expected words and identical
    complete GPU readbacks, with zero Vulkan validation errors. Two of these
    had different SPIR-V (cs_w32_fcmpswap and cs_w32_lds_u8_d16). The stock
    vk_compute_test wrote correct readbacks and then timed out on this setup,
    so I reran with explicit Vulkan teardown applied to the test harness only,
    identically on both revisions. All six reruns exited 0 on each revision.
  • The PR's CMake changes also drop 12 CTest registrations in this Windows
    configuration, including d3d12_mesh, d3d12_execute_indirect_validation,
    d3d12_set_descriptor_heaps, and crt_timed_wait.

All inputs were synthetic. AI-assisted with Codex.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 18f8dce on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_shader_ir.py - all exit 0.

AI-assisted (Claude Code).

@Impre-visible

Impre-visible commented Nov 23, 2026 •

Copy link
Copy Markdown

Tested 18f8dce on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18): fail, this head removes translator rules that main has.

Native build of runtime/ from this head (Ninja, RelWithDebInfo, clang 23.1.5), llvm-mc 23.1.5, SPIRV-Tools v2026.4. Same machine, same day, main-based PRs give build.py 270 of 270 and run_gpu_tests.py 192/241 passed, 1 failed (ms_wave_info_s3).

  • runtime/test/shaders/build.py: 270 shaders, 267 translated. The three refusals are buffer_store_byte, buffer_load_ubyte_d16 and image_gather4_c_l; the fixtures are cs_w32_buffer_store_sub, cs_w32_buffer_sub16 and ps_gather4_c_l.
  • Cause, from the diff against the merge base dde82f2 (one commit): tools/rdna2spirv.py loses the table entries for buffer_load_ubyte_d16 / _d16_hi and buffer_store_byte / _d16_hi, and image_gather4_c_l / _c_l_o drop out of two lists. Those are rules main already has, so this reads like the file was rewritten from an older copy.
  • scripts/run_gpu_tests.py: 244 of 247 translated; 189/241 passed, 48 skipped, 4 failed: the three fixtures above (missing .spv), which are this PR's, and ms_wave_info_s3, which fails the same way on every main-based head I tested today on this card (it returns (-1, -1, -1, -1) where (0.0078, 0.0078, 0.5, 1.0) is expected), so that one is not caused by this PR.
  • check.py: 0 of 211 pass; it stops on a shader module it cannot read (vs_ids.spv).
  • LEONIDA_W64_EMULATE=1 run_gpu_tests.py: 220/241 passed, 17 skipped.
  • tests/semantic: 5,956 OK, so the semantic suite does not cover these three rules.

AI-assisted run (Claude). Synthetic programs only, no game files involved.

@rideinstead

Copy link
Copy Markdown

🚨 Automated security review: DO-NOT-MERGE (commit 18f8dce2)

  • single upload commit reverts about 37 main commits and drops 12 runtime tests (noted on the PR by kamilolejnik and moistman7); a stale copy, not hidden code

Scope: static analysis of added diff lines only; no code was executed. Please review findings manually.

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.

7 participants