Skip to content

Recompiler: pick root-UAV memory windows with bit tests, not a switch - #2191

Draft
ezamelczyk wants to merge 1 commit into
xcl-dev:mainfrom
ezamelczyk:recomp-window-bit-tests
Draft

ezamelczyk wants to merge 1 commit into
xcl-dev:mainfrom
ezamelczyk:recomp-window-bit-tests

Conversation

@ezamelczyk

@ezamelczyk ezamelczyk commented Nov 25, 2026 •

Copy link
Copy Markdown

The memory-window accessors (xcl_mLoad, xcl_mStore, the atomics, xcl_loadx4) now choose a root UAV with one [branch] if per window, testing a bit of a one-hot mask, instead of [branch] switch (xcl_w). Part of #1269.

Why: Mesa's SPIR-V front end lowers an OpSwitch into a chain of ifs with a path on which no case runs. Inside a loop, every accessor result then becomes a loop-carried value that is undefined on entry, and RADV keeps all of them live. This is the cause of the CS_RaytracingResolve builds that @Fran-co-F and @lucsoft saw ACO's spiller run out of memory on in #1269. In those builds the windows are root UAVs, which is the default because heap windows are opt-in. With per-window ifs the values are defined before the first test, so nothing is carried round the loop. The bits are tested as (wm & (1u << k)) != 0u, with wm = select(xcl_w < windows, 1u << xcl_w, 0u), not xcl_w == k: with equality tests, DXC's jump threading took 31 of 47 s of the compile. An xcl_w outside the windows sets no bit and keeps the default, as the switch's default did. The heap form (XCL_HEAP_WINDOWS=1) is unchanged.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs (mem_accessors): the switch form is replaced by per-window bit tests.
  • crates/xcl/src/gpu/recomp/tests.rs: the two checks that looked for switch (xcl_w) look for the new form.

Tested

  • RX 9070 XT (RDNA4), Mesa 26.3.1 RADV (ACO), Ryzen 5 8400F, 30 GB RAM, CachyOS kernel 7.3.6. Wine 11.0 Staging (GE-Proton), vkd3d-proton 3.1 with Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257's patch and dxil-spirv 53aac418. DXC 1.9.2609. rustc 1.99.1, release, cross-built for x86_64-pc-windows-msvc. Not Windows.

  • One retail program, CS_RaytracingResolve (console 84eee477d68b6fdf, 1.28 MB of DXIL in the switch form), one cold pipeline build (VKD3D_SHADER_CACHE_PATH=0, empty caches):

    form DXC + pipeline peak RSS RADV registers loop-header phis (NIR)
    main (switch) 103–140 s 20.5 GB ~3 840 SGPRs needed 4 624
    ifs on xcl_w == k 61 s – 273 SGPRs, 192 VGPRs 644
    this PR (bit tests) 39 s (DXC ~20 s) 1.26 GB 301 SGPRs, 192 VGPRs 644

    (The register and phi counts come from a locally instrumented Mesa build. The times and memory are with the system Mesa.)

  • The retail pack's 2 689 compute programs, xcl shaders shaders.rpf --precompile --only cs --pipelines: 2 689 compiled, 0 errors, 2 689 pipelines made, 0 refused. This was on a local tree with this change on top of Recompiler: route compute programs through a compact backend (XCL_COMPACT_CS) #1316, Recompiler: compact cases share code whose branch targets differ #1448 and kernelx: Sleep does not count the time a bind waits for a pipeline build #1288, not on this branch alone. In the switch form, the same pass ran out of memory on RADV in the CS_RaytracingResolve stretch for @lucsoft ([OFFICIAL] Looking for PRs with shaders optimization #1269).

  • cargo test --workspace -- --test-threads=4 (release, Wine): 1 083 passed, 15 failed, 26 ignored. SKIPPED: f16_programs_on_the_gpu / f32_programs_on_the_gpu (per-lane form refuses V_MUL_F16, V_PK_MUL_F16, V_CVT_F16_F32) and pso_measurement_compiles_an_exact_synthetic_record (no Developer Mode under Wine).

    • Each of the 15 was run again alone on main 568e7db and on this branch, with identical results on both. 6 fail on both: fuzz_validate_and_gpu_2, f64_programs_on_the_gpu, f32_programs_on_the_gpu, gpu_nonfused_mad_and_mac_prevent_fma_contraction, gpu_depth_buffer_created_for_unordered_access_is_placed, xma2_source_voice_decodes_and_manages_lifetime. The other 9 pass alone on both; in the full run they failed because a device removal from another test took them out.
    • All memory-window GPU tests pass, among them gpu_memory_windows_reject_out_of_uma_aliasing, gpu_mesh_mode_reads_memory_windows, gpu_memory_windows_above_4_gib and gpu_bvh64_intersect_ray_across_memory_windows_and_grazing.

Regression run

scripts/linux/smoke.sh from #257 (it prints the block smoke.ps1 prints), retail title, 300 s, the same button script. Each run had an empty DXIL cache, Mesa's shader cache off and VKD3D_SHADER_CACHE_PATH=0.

In both runs presents had stopped before the end (presents per 2 s (last 3): 0 0 0). I have not checked where each run stopped.

main:

xcl-smoke v1
commit: 568e7db7729c8fa6c8f0f4b32d798faaf11462bb
gpu: AMD Radeon RX 9070 XT (RADV GFX1201)
title: game_dump
seconds: 300
exit: 0x0
frames shown: 2135 (PresentX 2135)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

this PR:

xcl-smoke v1
commit: fb84fde754d03463fcc56e424b2e6e79866e6b20
gpu: AMD Radeon RX 9070 XT (RADV GFX1201)
title: game_dump
seconds: 300
exit: 0x0
frames shown: 1885 (PresentX 1885)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

Not tested

  • Windows, and the AMD and NVIDIA Windows drivers. On NVIDIA the switch costs a lot of driver time ([OFFICIAL] Looking for PRs with shaders optimization #1269), and I have not measured whether bit tests do better or worse there than the switch or @pr1sm-dev's read-select form.
  • Other AMD generations on RADV, and the native-Windows test suite.
  • The direct DXIL path (XCL_DIRECT_DXIL), which picks its windows separately.

AI use

  • Claude Code with Claude Opus 5.5: found the cause (with a locally instrumented Mesa), wrote the change and the test updates, ran the builds, the tests and the smoke runs on the machine above, and wrote this description.

🤖 Generated with Claude Code

The accessors that pick a memory window (`xcl_mLoad`, `xcl_mStore`, the
atomics, `xcl_loadx4`) branched with `[branch] switch (xcl_w)` over the
root UAVs. Mesa's SPIR-V front end (vtn) lowers OpSwitch into a chain of
ifs with a path on which no case runs. In a loop, every accessor result
then becomes a loop-carried value that is undefined on entry, and RADV
has to keep all of them live around the loop. On a 1.28 MB-DXIL
CS_RaytracingResolve program (console 84eee477d68b6fdf), NIR had 4 624
loop-header phis where the SPIR-V had 204. ACO needed about 3 800 SGPRs,
and its spiller grew past 20 GB in 100-140 s (the out-of-memory builds
reported in xcl-dev#1269).

Each window is now one `[branch] if` on a value that is set before the
first one, so nothing is carried round the loop. The ifs test bits of a
one-hot mask, `wm = select(xcl_w < windows, 1u << xcl_w, 0u)`, not
`xcl_w == k`: with equality tests DXC's jump threading took 31 of 47 s
of the same program's compile. An `xcl_w` outside the windows sets no
bit and keeps the default, as the switch's `default` did. The heap form
(`XCL_HEAP_WINDOWS=1`) is unchanged.

That program on an RX 9070 XT, Mesa 26.3.1 RADV, vkd3d-proton
(VKD3D_SHADER_CACHE_PATH=0), one cold build:
- switch: 103-140 s, 20.5 GB peak, 3 840 SGPRs
- bit tests: 39 s (DXC about 20 s of it), 1.26 GB peak, 301 SGPRs,
  192 VGPRs; 644 loop-header phis in NIR

Part of xcl-dev#1269.

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

Copy link
Copy Markdown

Independent native Windows compiler measurement on exact PR head fb84fde754d03463fcc56e424b2e6e79866e6b20 against the runtime-identical pre-PR source 6020a33b16ba2de25b3aef5947552f2dc1831f09. The diff from that source to PR base 568e7db7729c8fa6c8f0f4b32d798faaf11462bb changes only README/SVG, not compiler code. The PR release workspace build passed and its checkout is clean.

Environment: Windows 11 build 26200, Ryzen 9 5980HX, RX 6800M driver 30.0.13002.13003, Rust 1.99.0 MSVC, DXC/DXIL 1.9.2609.5. From a locally owned title pack, each XCL release executable exported the same compute record hash 84eee477d68b6fdf (CS_RaytracingResolve), with four root-UAV windows and --split 0. Both exports reported one program and zero errors. No title or shader bytes are attached here.

I compiled each exported HLSL file using dxc.exe -E main -T cs_6_6 -HV 2021 -O1 -Fo ...; these are the flags dxc::compile selects for a source of this size. I alternated three sequential runs per revision, with a memory guard that did not fire:

Source HLSL bytes DXIL bytes DXC wall seconds (three runs) Median Sampled peak DXC RSS
Pre-PR 4,278,297 1,288,592 32.438, 34.156, 35.969 34.156 s 643–693 MiB
PR head 4,281,101 1,509,652 40.265, 42.891, 37.781 40.265 s 779–787 MiB

All six DXC processes exited 0. On this Windows DXC path, the PR's median was 6.109 s / 17.9% longer and its DXIL was 221,060 bytes / 17.2% larger for this record. The sampled RSS is an approximate process measure, not total system commit. This differs from the Linux/RADV end-to-end improvement in the PR description; it does not measure or contradict the RADV driver result.

Scope: Exact-record HLSL generation and standalone native DXC compilation only. I did not run CreateComputePipelineState, measure a Windows driver pipeline, execute the shader, run the full test suite, or perform a retail-title smoke on this PR. The synthetic/root-UAV semantics and gameplay performance remain unverified here.

AI use: OpenAI Codex identified the record in the locally owned pack, built the PR, exported both HLSL variants, wrote and ran the bounded DXC measurement script, checked the exact revisions and numbers, and prepared this report.

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