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
Contributor

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.

@bielfreedom

Copy link
Copy Markdown

AMD Windows driver data for #2191: on the RX 9070 XT, the bit-test form makes the DXIL 15 % larger and AMD's CreateComputePipelineState 1.56× slower on 24 large retail programs. CS_RaytracingResolve still doesn't finish on the HLSL path. This adds the driver side to @luckyteagirl's DXC-only numbers above. The PR's gain is on RADV/ACO; on the native AMD Windows compiler it goes the other way.

Setup

  • GPU: RX 9070 XT 16 GB (RDNA4), AMD driver 32.0.32029.2010.
  • CPU and memory: Ryzen 7 7700 (16 threads), 32 GB RAM, Windows 11, release, DXC 1.9.2609.
  • Builds:
    • base 568e7db;
    • this PR at fb84fde.
  • Both builds carry a local patch that logs per-program DXIL size, record hash (fnv64), DXC time and per-CreateComputePipelineState time.

Method

  • HLSL path (direct DXIL off), root-UAV windows (XCL_HEAP_WINDOWS=0), XCL_PIPELINE_THREADS=1, XCL_DXC_THREADS=1.
  • XCL_PSO_DISK_CACHE=0 and a fresh exe name per run, because the AMD driver cache is keyed on the exe file name.
  • 24 retail compute programs from 8 KB to 1.0 MB of DXIL, the same set I used for earlier PRs. 2 rounds, interleaved; the rounds agree within 1 %.
  • A watchdog killed a run after 25 min or at 24 GB.

24 programs (means of 2 rounds; all 96 builds succeeded, 0 refused)

base #2191 change
DXIL, total 7,930,908 B 9,120,608 B +15.0 %
DXC, sum 192.9 s 224.2 s +16 %
driver, sum 128.5 s 201.0 s ×1.56
driver run, process peak private 4.48 GB 7.32 GB +63 %
  • 23 of the 24 programs grow by 4–19 % of DXIL. Driver time is within ±5 % on 9 of them and 8–26 % slower on most of the others.
  • Two programs dominate the change:
program DXIL base → PR driver base → PR
ed2f8fa2e996c6a6 1,024,664 → 1,206,152 B 20.9 → 53.8 s (×2.58)
c6f8ee0f53ed5709 917,236 → 1,077,452 B 18.7 → 46.1 s (×2.46)

84eee477d68b6fdf (CS_RaytracingResolve), 1 run each:

build DXIL DXC driver peak private
base 1,288,596 B 21.6 s not finished after 25 min (killed) 19.6 GB
#2191 1,509,660 B 25.6 s not finished after 25 min (killed) 22.9 GB

The DXIL sizes match luckyteagirl's (+17 %). Both builds were still working, at 9–11 GB private, when they were killed. So on AMD Windows the bit tests don't remove whatever stalls this program in the driver. The direct route (53 s whole, 7.8 s with #2107's split; data) remains the way through here.

Read / inference

  • The AMD Windows compiler takes the switch form better than the bit-test chain. It slows down more than the DXIL grows, so this is not size alone.
  • If this goes in for RADV, it might be worth keeping the switch on the native AMD (and maybe NVIDIA) Windows drivers. For example, the form could be chosen by a vendor or Vulkan-layer check, or by an env switch, so both paths can still be measured.

Not tested

  • The PR's unit tests on this card.
  • XCL_HEAP_WINDOWS=1 (unchanged by the PR).
  • The direct DXIL route (the PR changes HLSL emission only).
  • A title run.
  • RDNA2 and RDNA3.

AI use: Claude Code (Claude Opus 5.5) ran the measurements and drafted this. The account owner is away; counts and timings only, no game data.

@zohaiblazuli

Copy link
Copy Markdown
Contributor

Data for the cost this PR targets, measured on main 1a0bbe3 (not on this PR's head): #2088 comment. RTX 3060 (NVIDIA 617.42), 8 compute programs, cold driver builds. Replacing switch (xcl_w) with switch (0u) (wrong results; an upper bound) makes the build 1.6–6.2× shorter.

The largest gap left after the heap form is on the 5 MB outlier 219287eb2f6021fe: 819 s with the switch, 434 s heap, 133 s for the bound. A timing of this PR on that program would show how much of it this form recovers.

AI use: Claude Code (Claude Opus 5.5) ran the measurements and wrote this.

@siam4201

Copy link
Copy Markdown
Contributor

Tested on NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.2101, Ryzen 9 7900X, Rust 1.97.1), including a cold PSO benchmark on a large compute outlier from a retail pack (83a0f0eec0ddb740):

Cold PSO benchmark (--measure-pso 83a0f0eec0ddb740 --repeat 2)

  • main (568e7db): 263716 bytes DXIL, 3053 ms DXC, median 10.442 s (min 9.418 s, max 11.465 s)
  • pr-2191 (fb84fde): 312640 bytes DXIL, 2428 ms DXC, median 6.383 s (min 5.855 s, max 6.911 s) — ~38.9% faster cold driver compile (~20.5% faster DXC)

Tested (.\scripts\test.ps1)

Regression run (.\scripts\smoke.ps1, 30s)

main:

xcl-smoke v1
commit: 568e7db7729c8fa6c8f0f4b32d798faaf11462bb
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 30
exit: 0x0
frames shown: 740 (PresentX 740)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15434 MB

pr-2191:

xcl-smoke v1
commit: fb84fde754d03463fcc56e424b2e6e79866e6b20
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 30
exit: 0x0
frames shown: 660 (PresentX 660)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15887 MB

Not tested

  • Not tested on AMD RDNA / wave64 GPUs

AI use

  • Antigravity (Gemini 4 Neon)

coolpersonnn added a commit to coolpersonnn/xcl that referenced this pull request Nov 25, 2026
In direct DXIL root-UAV window mode (`XCL_DIRECT_DXIL=1`, `XCL_HEAP_WINDOWS=0`),
every scalar/buffer/flat read previously went through `with_mem_many_tys`,
emitting a per-load `switch i32` + guard CFG and `phi` nodes over all
4 root-UAV windows (`u2..u5`). Compute shaders with hundreds of loads grew
thousands of basic blocks, triggering multi-second to multi-minute cold
NVIDIA driver compiles and high compiler commit memory.

Replace multi-window direct DXIL read-only raw loads on non-graphics stages
(`self.gfx.is_none()`, `self.heap_mem.is_none()`) with branchless straight-line
loads via `with_mem_raw_loads`:
- Resolve the effective window `w_eff = sel(guard, w, n)` once and compare
  `w_eff == k` across windows `0..n`, reading offset `0` on unselected
  windows/lanes and merging with `select`.
- Preserve `xcl-dev#1693` semantics: out-of-range window indices and inactive lanes
  select `0` while emitting only valid offset-0 reads on bound windows.
- Ensure `bind.rs` (`u2..u5`, filtering `v != 0`) and `runner::dispatch_with`
  always bind valid non-null buffers for all 4 root-UAV window slots.
- Exclude graphics stages (`VS`/`PS`, `self.gfx.is_some()`), where flattening
  root-UAV loads regressed straight-line PS cold NVIDIA compile time by 5.64x
  (1,632 -> 9,203 ms on `0829441`).
- Keep stores/atomics (`xcl-dev#2288`), `bvh_intersect`, `XCL_HEAP_WINDOWS`
  (`xcl-dev#1695`), and HLSL emission (`xcl-dev#1380`/`xcl-dev#2191`) unchanged.

Measured on RTX 5090 (driver 621.01, Windows 11, release) against `3f4495a`
(including PR xcl-dev#2346 non-coherent compute windows, 3 paired interleaved
trials per spec with cold root-signature keys and fresh exe copies):
- `cs-500-raw-w32` (non-coherent / coherent): 5,133 -> 1,748 ms (2.94x) /
  5,129 -> 1,751 ms (2.93x); peak commit 309 -> 191 MiB
- `cs-1000-raw-w32` (non-coherent / coherent): 19,742 -> 5,156 ms (3.83x) /
  19,767 -> 5,132 ms (3.85x); peak commit 993 -> 259 MiB (-73.9%)
- `cs-1000-raw-w64` (split wave64): 19,709 -> 5,171 ms (3.81x); peak 994 -> 259 MiB
- `cs-1000-w32` (mixed w/ 33% FormatLoad, non-coherent / coherent):
  49,431 -> 17,977 ms (2.75x) / 49,944 -> 18,058 ms (2.77x); peak 1.34 GiB -> 464 MiB
- Synthetic GPU dispatch (128 loads/lane, uniform & mixed, w32 & split w64):
  1.84x to 4.68x faster (3.296-6.688 us vs 9.696-22.608 us), 100% readback match.

Assisted-by: T3 Code (gemini-pro-orchestrator-neon)
Assisted-by: Codex in T3 Code (gpt-6.2-sol)
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.

5 participants