Skip to content

Recompiler: eliminate local array and branches in pixel-shader PRELUDE::rla() - #2247

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-2088-ws5-rla-no-alloca
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-2088-ws5-rla-no-alloca

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

What is in it

Eliminates local arrays (alloca [4 x i32]) and scalar control-flow branches inside the pixel-shader lane-read helper PRELUDE::rla() (crates/xcl/src/gpu/recomp/emit.rs), continuing #2088 Workstream 5 (following commit 9f65f95, which removed runtime uint4 indexing from PRELUDE_MEM format helpers but left PRELUDE::rla()):

  • Replaced xcl_lv[min(h >> 5, 3u)] (WaveActiveBallot(true) indexed by a runtime dword index) with inline branchless select() over xcl_lv.{x,y,z,w} (uint hw = h >> 5; uint lv_w = select(hw < 2u, select(hw == 0u, xcl_lv.x, xcl_lv.y), select(hw == 2u, xcl_lv.z, xcl_lv.w));). Kept inline inside PRELUDE rather than calling PRELUDE_MEM::pick4(), because PRELUDE_MEM is omitted when !uses_mem.
  • Replaced the four scalar ?: and if chains in rla() (q, r, and the two w updates) with branchless select() and and(), matching PRELUDE::ballot().

Across a 12,056-shader retail pixel-shader corpus (windows=4, split_wave64=true), 7,634 pixel shaders (63.3%) call rla() (213,536 total call sites, up to 212 calls in a single pixel shader), and DXC lowered every call site into a [4 x i32] local array plus conditional branches. Removing these eliminates 100% of rla() local arrays (212 -> 0 on the largest outlier), cuts DXIL basic blocks by up to 24.4% and conditional branches by up to 27.4%, and reduces cold NVIDIA CreateGraphicsPipelineState compile time by 16.5%–27.9% on the largest rla()-heavy retail pixel shaders.

Adds three tests in gpu::recomp::emit::tests:

  1. rla_pixel_helper_avoids_runtime_vector_indexing_and_branches (pure CPU, asserts no xcl_lv[, ?:, if (, or pick4( dependency in PRELUDE::rla()).
  2. rla_pixel_shader_compiles_without_local_arrays (DXC ps_6_6 + -dumpbin disassembly on a self-contained #define XCL_PIXEL 1 + PRELUDE pixel shader without PRELUDE_MEM, asserting 0 allocas and 0 conditional branches).
  3. gpu_rla_pixel_shader_matches_reference_including_helper_lanes (WARP-guarded GPU draw across two diagonal-splitting triangles comparing PRELUDE::rla() against the original reference implementation on own-quad and cross-quad helper/live lanes in the same hardware wave, plus exact analytical checks for x^1 / y^1 quad neighbors).

Part of #2088
Part of #1269

Tested

Hardware / toolchain: Windows 11 Home (26200), Intel Core i5-11400F (6C/12T), 32 GB RAM, NVIDIA GeForce RTX 4060 8 GB VRAM (32-lane waves, driver 621.14), DXC 1.9.2609.5 (01b62ad4), Rust 1.100.0.
Base commit: 707b84c7df012bff2b6f30cd5d7149b5d407fce4 (git merge-tree --write-tree also verified clean against origin/main f087b998).
Head commit: 5081dc8b6e22fc74f6fe7d31de333182e8f7ec92.

Unit & GPU test results:

  • Pre-fix failing-first verified on gpu::recomp::emit::tests::: rla_pixel_helper_avoids_runtime_vector_indexing_and_branches failed (runtime vector index remains in rla()) and rla_pixel_shader_compiles_without_local_arrays failed (expected 0 local arrays in rla() DXIL, got 3).
  • Post-fix cargo test --release -p xcl --lib gpu::recomp::emit::tests:: -- --nocapture --test-threads=1 on RTX 4060: 4 passed, 0 failed, 0 ignored (0 skipped).
  • Post-fix cargo test --release -p xcl --lib gpu::recomp::tests::gpu_ps_masks_hold_helper_lanes_on_triangle_edges -- --nocapture --test-threads=1 on RTX 4060: 1 passed, 0 failed (wave64 true: 0 of 1024 pixels wrong, wave64 false: 0 of 1024 pixels wrong).
  • CI WARP checks (XCL_ADAPTER="Microsoft Basic Render Driver" and XCL_ADAPTER=warp) on gpu::recomp::emit::tests::: 4 passed, 0 failed, 0 ignored (3 CPU/DXC tests passed; gpu_rla_pixel_shader_matches_reference_including_helper_lanes cleanly printed SKIPPED).
  • cargo build --release --workspace: exit 0.
  • cargo test --release --workspace --no-fail-fast -- --test-threads=4: 1226 passed, 2 failed, 46 ignored across 22 test suites, with 26 pre-existing runtime SKIPPED lines (0 new skips). Both failures are pre-existing upstream merge regressions on origin/main 707b84c7 unrelated to emit.rs:
    • hle::d3d12x::device_x::tests::pc_resource_desc_strips_uav_on_unsupported_formats_and_msaa (device_x.rs:1376: format 85 kept, left: 28, right: 85 after #1991 promoted format 85 UAVs to 28)
    • gpu::recomp::pso::shader_cache_tests::rerouted_disk_entries_follow_the_direct_translator (pso.rs:1360: left: 68, right: 1 after #2120 met DXBC-header test fixtures).

Cold NVIDIA CreateGraphicsPipelineState benchmark (#2088 Required Evidence):

  • Environment: XCL_DXC_DIR=%LOCALAPPDATA%\XCL\dxc\bin\x64, XCL_DXIL_CACHE=<empty scratch dir> (XCL DXIL cache cold/empty; windows=4, split_wave64=true, ps_6_6 paired with a minimal signature-matched vs_6_6 and abi_root_signature_with). Windows Developer Mode is unavailable on this host (ID3D12Device9::ShaderCacheControl cannot disable implicit caches), so cold NVIDIA implicit DXCache misses were forced via fresh executable copies per mode and verified on every shader by an immediate 2nd CreateGraphicsPipelineState call hitting warm cache (0.03–0.40 ms warm vs 780–26,909 ms cold, >20,000x ratio).
  • Retail pixel shader corpus census: 12,056 unique PS -> 12,056 complete -> 7,634 calling rla() (213,536 total rla() call sites).
  • Quiet-box stratified measurement (568e7db7 baseline vs branchless PRELUDE::rla(), same RTX 4060, ps_rla_bench bench <corpus> <base|fix> <hashes>):
Record hash rla() calls alloca [4 x i32] Blocks Cond br DXIL bytes IR insts Cold CreateGraphicsPipelineState Delta Warm 2nd call
547a9a33080eaab6 212 212 -> 0 5121 -> 4272 (-16.6%) 1602 -> 1293 (-19.3%) 797,296 -> 760,372 B (-4.6%) 110,070 -> 105,925 18,790 -> 15,379 ms -18.2% 0.23 / 0.33 ms
059e34becdccf5cf 211 211 -> 0 5081 -> 4246 (-16.4%) 1586 -> 1282 (-19.2%) 785,492 -> 748,972 B (-4.6%) 108,443 -> 104,349 18,278 -> 15,261 ms -16.5% 0.16 / 0.23 ms
11441aa968b462c5 199 199 -> 0 4777 -> 4020 (-15.8%) 1494 -> 1219 (-18.4%) 722,880 -> 689,072 B (-4.7%) 99,338 -> 95,552 16,558 -> 13,011 ms -21.4% 0.24 / 0.12 ms
a9af041f10fd1d7e 129 129 -> 0 3494 -> 2642 (-24.4%) 1165 -> 846 (-27.4%) 466,644 -> 439,328 B (-5.9%) 66,371 -> 63,162 10,281 -> 7,411 ms -27.9% 0.18 / 0.08 ms
d01b5f713fdca60e 61 61 -> 0 2731 -> 2585 (-5.3%) 819 -> 769 (-6.1%) 368,556 -> 358,484 B (-2.7%) 53,713 -> 52,770 10,662 -> 10,631 ms -0.3% 0.08 / 0.18 ms
4b05f6d6c18e1037 21 21 -> 0 712 -> 700 (-1.7%) 215 -> 209 (-2.8%) 89,752 -> 86,808 B (-3.3%) 12,214 -> 11,971 810 -> 780 ms -3.8% 0.03 / 0.03 ms
  • Rebased verification on exact base 707b84c7 vs PR head 5081dc8b reproduced identical alloca (212/211/199/129/21 -> 0) and block/instruction reductions (547a9a33080eaab6: 798,012 -> 761,104 B, 5137 -> 4288 blocks; 059e34becdccf5cf: 786,228 -> 749,736 B, 5097 -> 4262 blocks; 11441aa968b462c5: 723,632 -> 689,800 B, 4793 -> 4036 blocks; a9af041f10fd1d7e: 466,644 -> 439,328 B, 3494 -> 2642 blocks; 4b05f6d6c18e1037: 89,752 -> 86,808 B, 712 -> 700 blocks), reduced benchmark process peak commit from 633 MB to 605 MB, and reproduced the PR cold CreateGraphicsPipelineState timings (15,299, 12,943, 7,289, 790 ms on 059e34be / 11441aa9 / a9af041f / 4b05f6d6; 22,853 ms on 547a9a33 during concurrent CPU load vs 24,586 ms baseline).

Regression run

Paired 60-second scripts/smoke.ps1 runs on the same retail title build (29640639) from clean origin/main (707b84c7) and this branch (5081dc8b). Both reached the title menu with steady 30 FPS presents continuing (60 60 60 presents per 2 s), 0 device removals, 0 crashes, and no runtime GPU-time regression (1457 frames / 14436 MB peak commit on PR vs 1381 frames / 14462 MB peak commit on base):

Base (origin/main):

xcl-smoke v1
commit: 707b84c7df012bff2b6f30cd5d7149b5d407fce4
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1381 (PresentX 1382)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14462 MB

PR (fix-2088-ws5-rla-no-alloca):

xcl-smoke v1
commit: 5081dc8b6e22fc74f6fe7d31de333182e8f7ec92
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1457 (PresentX 1457)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14436 MB

Not tested

  • ID3D12Device9::ShaderCacheControl cache-disabled API (requires Windows Developer Mode, unavailable on this Windows 11 Home host; cold driver compiles were instead forced via fresh executables and verified against 2nd-call warm hits < 0.5 ms).
  • Native 64-lane wave64 GPU hardware (AMD RDNA / Intel Arc; tested on 32-lane NVIDIA RTX 4060 and checked for WARP CI skips).
  • Full cold in-title time-to-first-playable-frame run beyond the 60-second paired smoke test.
  • Non-Windows host platforms.

AI use

Drafted with Google Antigravity (Gemini 4 Neon) and verified locally by reproducing pre-fix test failures, verifying emit::tests and gpu_ps_masks_hold_helper_lanes_on_triangle_edges on RTX 4060 plus WARP skip checks, running the workspace test suite, measuring cold NVIDIA CreateGraphicsPipelineState compile times across stratified retail pixel shaders, and completing the paired 60-second smoke run above.

…E::rla()

In HLSL 2021 pixel shaders (#define XCL_PIXEL 1), PRELUDE::rla() indexed
uint4 xcl_lv with a runtime index xcl_lv[min(h >> 5, 3u)] and used scalar
ternary/if chains. DXC lowered every rla() call site into a [4 x i32]
alloca local array plus conditional branches; across the retail pixel
shader corpus (12,056 unique PS), 7,634 shaders call rla() (up to 212
calls / 212 local arrays in a single pixel shader).

Replace xcl_lv[min(h >> 5, 3u)] with inline branchless select() over
xcl_lv.{x,y,z,w} (self-contained inside PRELUDE without depending on
PRELUDE_MEM::pick4) and replace the scalar ?: and if chains in rla() with
select() and and().

Part of xcl-dev#2088
Part of xcl-dev#1269

Assisted-by: Google Antigravity (Gemini 4 Neon)
@itsSio

itsSio commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tested

Independent Windows check on clean 5081dc8b6e22fc74f6fe7d31de333182e8f7ec92. Windows 11 Pro 26200 on both PCs, x64 MSVC, unoptimized dev/test with debug info off. cargo build --workspace --locked passed on both. cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --nocapture exited 101 on both:

GPU / driver CPU, RAM, toolchain Passed / failed / ignored Runtime SKIPPED
RTX 5080 / 32.0.16.1088 Ryzen 7 9800X3D, 32 GiB; Rust 1.97.0, DXC/DXIL 1.9.2609.5 1226 / 2 / 46 25
RTX 2080 SUPER / 32.0.16.1088 Ryzen 5 5600X, 16 GiB; Rust 1.99.1, DXC/DXIL 1.8.2502.11 1226 / 2 / 46 27

Both logged 20 native-wave64 skips, three per-lane f16/f32 refusals, and one skip each for unavailable driver-cache control and heap windows. The RTX 2080 SUPER also logged two missing-D3D12-debug-layer skips.

Focused cargo test -p xcl --lib --locked <filter> -- --test-threads=1 --nocapture: gpu::recomp::emit::tests:: 4 / 0 / 0 and gpu_ps_masks_hold_helper_lanes_on_triangle_edges 1 / 0 / 0 on each of the RTX 5080, RTX 2080 SUPER, and Radeon RDNA2 iGPU (driver 32.0.21043.5001, same Ryzen 7 system and toolchain). No runtime skips in these runs. The triangle check logged 0 / 1,024 pixel mismatches for each wave setting on every adapter, including native wave64 on Radeon.

The two workspace failures on both PCs matched the author's baseline report: rerouted_disk_entries_follow_the_direct_translator (68 vs 1) and pc_resource_desc_strips_uav_on_unsupported_formats_and_msaa (format 85, got 28). I independently confirmed these same assertions on earlier main commit 5478c4a7 in the #2229 verification, not on this PR's exact parent. The workspace results above remain red.

Not tested

Retail smoke, full AMD workspace, Intel, non-Windows hosts, driver compile performance or gameplay timing.

AI use

Ran via Samaritan:

  • OpenCode with OpenAI GPT-6.2 Sol (openai/gpt-6.2-sol): scoped and ran both Windows verifications, audited the raw logs, and wrote this report. No subagents contributed to this check.

@Decodere Decodere closed this Nov 25, 2026
@Decodere Decodere reopened this Nov 25, 2026
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