Skip to content

Recompiler: direct DXIL vectorizes multi-dword loads and window switch - #1699

Closed
RandomGithub12309 wants to merge 1 commit into
xcl-dev:mainfrom
RandomGithub12309:recomp/dxil-vector-mem-switch
Closed

RandomGithub12309 wants to merge 1 commit into
xcl-dev:mainfrom
RandomGithub12309:recomp/dxil-vector-mem-switch

Conversation

@RandomGithub12309

Copy link
Copy Markdown
Contributor

In the direct RDNA2 → DXIL backend (XCL_DIRECT_DXIL), Lw::with_mem lowered multi-window accesses as four sequential guarded diamonds (8 basic blocks, two temporary SSA variables, and i64 unpack/repack per access), and multi-dword SMEM, ld_raw, s_buffer_load*, buffer_load*, and FLAT/global instructions ran per-dword scalar loops that repeated that full window dispatch for every dword.

Lw::with_mem_n now emits a single Op::Switch over VAR_WIN (folding constant VAR_WIN inline without branching), merges returned values directly through join phis without temporary SSA variables or i64 split/repack, and seals join as an O(1) pass-through from the pre-switch block. Contiguous unbounded multi-dword accesses (s_load_dwordx2..x16, ld_raw, and FLAT/global load_dwordx2..x4 / store_dwordx2..x4) pack into up to 4-wide rawBufferLoad/rawBufferStore chunks under one switch, s_buffer_load* and buffer_load* share a single switch across their dwords with per-dword bounds clamping, and 1-dword buffer_store_dword combines its lane and range guards. Part of #1269.

What is in it

  • crates/xcl/src/gpu/dxil/lower_mem.rs: Lw::with_mem_n, Lw::with_mem, Lw::mem_ld_n, and Lw::mem_st_n; single-switch multi-dword lowering in ld_raw, smem, mubuf, and flat.
  • crates/xcl/src/gpu/dxil/kernel.rs: bump TRANSLATOR_VERSION from 4 to 5.
  • crates/xcl/src/gpu/dxil/tests.rs: multi_window_accesses_emit_one_switch_and_wide_raw_ops (validates and disassembles a 5-instruction test checking one switch i32 per instruction and i8 15 4-wide raw buffer ops) and multi_window_vector_loads_stores_and_atomics_run_on_the_gpu (runs 2-window Uma GPU checks for S_LOAD_DWORDX8, S_BUFFER_LOAD_DWORDX4 and BUFFER_LOAD_DWORDX3 straddling num_records, unaligned BUFFER_LOAD_FORMAT_XYZW via ld_raw, GLOBAL_LOAD_DWORDX4/GLOBAL_STORE_DWORDX4, and 64-bit GLOBAL_ATOMIC_ADD_X2 GLC=1).

Tested

  • NVIDIA GeForce RTX 5050 Laptop GPU (8 GB, wave32 / split wave64, driver 32.0.16.2114), Intel Core Ultra 7 255HX, Windows 11 Home build 26300, rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC, DXC 1.8.2502.11; debug for tests, release for shader sweeps and smoke runs, on 7b8711407e77.
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 860 passed, 0 failed, 42 ignored (811 lib + 49 integration). SKIPPED GPU tests on this adapter: gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • cargo test -p xcl --lib gpu::dxil -- --test-threads=4: 82 passed, 0 failed, 1 ignored.
  • XCL_DIRECT_DXIL=1 cargo test -p xcl --lib gpu::recomp: 194 passed, 0 failed, 18 ignored (both --test-threads=1 and --test-threads=4; one earlier --test-threads=4 run hit a transient 0x887a0006 device removal under 8 GB VRAM contention alongside concurrent sparse-VRAM fixtures).
  • XCL_DIRECT_DXIL=all cargo test -p xcl --lib gpu::recomp -- --test-threads=1: 194 passed, 0 failed, 18 ignored (run single-threaded because concurrent sparse-VRAM / Uma fixtures exhaust 8 GB VRAM under --test-threads=4).
  • Retail shader pack sweep (xcl shaders <pack> --direct, 4 windows, split wave64, --validate --compare, release builds before/after this diff):
    • --sample 50 (seed 1): 34 / 36 translated (0 validator errors; 2 GS unsupported); DXIL size 1,647,204 B → 1,394,876 B (−15.3 %, direct/HLSL 1.08 → 0.91); translation time p50 16.44 → 13.31 ms, p95 40.12 → 24.41 ms, max 41.80 → 29.96 ms.
    • --sample 200 --max-bytes 0 --timeout 20 (seed 1): 197 / 200 translated (21 CS, 67 MS, 86 PS, 23 VS, 0 validator errors; 3 GS unsupported); DXIL size 30,238,100 B → 22,568,676 B (−25.4 %, direct/HLSL 1.25 → 0.94 vs 24,113,960 B HLSL+DXC); translation time p50 20.36 → 15.66 ms (−23.1 %), p95 277.55 → 152.00 ms (−45.2 %), max 837.86 → 417.81 ms (−50.1 %).

Regression run
Retail title, 60 s each, release builds, XCL_DIRECT_DXIL unset (default runtime path), no uncommitted changes.

main:

xcl-smoke v1
commit: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1576 (PresentX 1576)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14545 MB

this PR:

xcl-smoke v1
commit: 7b8711407e77b09f156eb1d7a5dd2a56a6841dfd
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1594 (PresentX 1594)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14541 MB

Not tested

  • AMD and Intel GPUs, native wave64 hardware, and Linux / Wine (vkd3d-proton).
  • Running the retail title live with XCL_DIRECT_DXIL=1 / =all (the smoke runs above exercise the default runtime path; the direct backend changes are covered by the XCL_DIRECT_DXIL gpu::recomp suites, the gpu::dxil GPU tests, and the 200-program validated pack sweep).

AI use

  • Antigravity (Gemini 4 Carbon and Gemini 4 Hydrogen): investigated crates/xcl/src/gpu/dxil/lower_mem.rs, wrote the implementation, tests, and this description, and ran the test suites, shader sweeps, and smoke runs on the machine above.

`gpu/dxil/lower_mem` lowered `with_mem` as four sequential `guarded`
diamonds (8 blocks, 2 temporary SSA variables, and `i64` unpack/repack
per access) and emitted multi-dword SMEM, MUBUF, `ld_raw`, and
FLAT/global accesses as scalar per-dword `mem_ld`/`mem_st` loops,
repeating the full window dispatch for each dword.

Lower `with_mem_n` as a single `Op::Switch` over `VAR_WIN` (folding
constant `VAR_WIN` inline without branching), merging return values
directly through `join` phis and sealing `join` as a single-predecessor
SSA pass-through from the pre-switch block. Group contiguous unbounded
multi-dword loads/stores (`s_load_dwordx2..x16`, `ld_raw`, and
FLAT/global `load_dwordx2..x4` / `store_dwordx2..x4`) into up to 4-wide
`rawBufferLoad`/`rawBufferStore` chunks under one switch, share one
switch across `s_buffer_load*` and `buffer_load*` dwords with per-dword
bounds clamping, and combine 1-dword `buffer_store_dword`'s lane and
range guards. Bump `TRANSLATOR_VERSION` to 5.

On a 200-program retail pack sample (`--direct --sample 200 --max-bytes 0
--validate --compare`, 4 windows, split wave64), total direct DXIL size
across the 197 translated programs drops from 30.24 MB to 22.57 MB
(-25.4%, direct/HLSL 1.25 -> 0.94), and translation time drops from
p50 20.36 ms / p95 277.55 ms / max 837.86 ms to 15.66 ms / 152.00 ms /
417.81 ms with 0 validator errors.

Part of xcl-dev#1269

Assisted-by: Antigravity (Gemini 4 Carbon / Gemini 4 Hydrogen)
@nnonno91

Copy link
Copy Markdown

Independent AMD native-wave64 coverage at 7b8711407e77b09f156eb1d7a5dd2a56a6841dfd.

Tested

  • AMD Radeon(TM) Graphics (Ryzen 7 9800X3D integrated GPU, driver 32.0.21042.62), Windows 11 25H2 build 26200.9974, Rust 1.100.0 MSVC, SDK DXC 1.8.2502.11. XCL_ADAPTER='AMD Radeon'; unoptimized builds, with debug info disabled on the PR.
  • cargo build --workspace --locked passed. cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --show-output reported 860 passed, 0 failed, 42 ignored. One explicit skip is included (heap_window_out_of_memory_falls_back_to_the_switch_form, heap windows off). The bundled title passed.
  • With XCL_REQUIRE_GPU=wave64,mesh, cargo test --locked -p xcl --lib -- multi_window_accesses_emit_one_switch_and_wide_raw_ops multi_window_vector_loads_stores_and_atomics_run_on_the_gpu --test-threads=1 --nocapture gave 2 passed, 0 failed, 0 ignored, 0 skips. The new GPU test ran natively on wave64.
  • With XCL_DIRECT_DXIL=1, XCL_TRANSLATED_CACHE=0, and XCL_PSO_DISK_CACHE=0, cargo test --locked -p xcl --lib -- gpu::recomp --test-threads=1 --show-output gave 194 passed, 0 failed, 18 ignored, with no explicit skips. Separately, the same flags plus XCL_DXIL_DUMP=all and XCL_REQUIRE_GPU=wave64,mesh on cargo test --locked -p xcl --test gpu_compute_e2e -- --exact console_wave64_compute_through_the_pso_hook_dispatch_and_read_back --test-threads=1 --nocapture gave 1 passed, 0 failed, 0 ignored, 0 skips. The runtime emitted direct DXIL for the record, logged no fallback, and the dispatched output matched.
  • cargo test --locked -p xcl --test direct_window_probe -- --exact direct_dwordx16_across_memory_windows --test-threads=1 --nocapture ran the same temporary native-wave64 fixture on the PR and its base c160d86ef2efe3a07e4dfb473e4fa72eafb413c0 (one pass each, no skips). Both builds validated and returned the correct 16 words from the first and last mapped windows and 13 valid words plus three zeros from a bounded buffer. All 48 output words matched in each window configuration.
Synthetic fixture Base DXIL PR DXIL PR switches / four-wide raw-load sites
2 windows 8,120 B 5,880 B 3 / 16
4 windows 11,976 B 7,512 B 3 / 32

The base used guarded branches and scalar loads for this fixture. The PR's three switches correspond to the three 16-word reads.

Not tested

Retail title or shader pack, AMD discrete or other adapters, heap windows in direct DXIL, or compilation and runtime timing.

AI use

OpenAI Codex (GPT-6 family, exact variant unavailable) wrote the temporary comparison, ran the tests, and drafted this report.

@Royalflamejlh

Copy link
Copy Markdown
Contributor

Offline pass under Wine, CPU only, XCL_DIRECT_DXIL=1: 7b87114 wrote 1,058 MB of DXIL for the 2,689 compute programs of a retail pack in the split wave64 form, where its base main c160d86 wrote 1,534 MB (31 % less). No program fell back to HLSL in either and no program grew. Programs of 1 MiB or more went from 719 to 510. One pass each; the sizes are exact.

Setup: Fedora 44 (kernel 7.2.9), Wine 11.21 Staging, Ryzen 9 9950X3D, 62 GB RAM. These passes used no card. Release, cross-compiled to x86_64-pc-windows-gnu. xcl shaders shaders.rpf --precompile --only cs --limit N [--split 1], from an empty cache each time, XCL_DXC_THREADS=8, 4 memory windows, no --pipelines. With no adapter the pass translates the native wave64 form; --split 1 gives the form a card with 32-lane waves gets. 7b87114 sits directly on c160d86. Sizes are those of the translation cache files (the DXIL and a short header), and count each distinct program once: 2,682 of the 2,689, 239 of the 243. DXC 1.9.2609.5 for the HLSL-path figure.

Tested

main c160d86 7b87114
split, whole pack: DXIL 1,534.2 MB 1,058.0 MB
split, whole pack: programs of 1 MiB or more 719 510
split, whole pack: largest program 15.2 MB 12.5 MB
split, whole pack: pass time 55 s 25 s
split, first 243 programs: DXIL 67.0 MB 49.7 MB
native, whole pack: DXIL 1,525.5 MB 1,049.3 MB
native, whole pack: programs of 1 MiB or more 718 509
native, first 243 programs: DXIL 63.4 MB 46.1 MB

Every direct pass compiled all its programs (2,689 or 243) with 0 errors and wrote no DXC output file, which is how "no fallback" was counted. The main whole-pack pass in the native form printed its full result and was then killed (exit 137); all 2,682 of its files are there.

By size on main, the whole pack in the split form:

DXIL on main programs main 7b87114 ratio
under 64 KiB 997 19.7 MB 17.3 MB 0.88
64 KiB to 256 KiB 607 100.2 MB 81.7 MB 0.81
256 KiB to 1 MiB 359 160.3 MB 119.3 MB 0.74
1 MiB to 4 MiB 716 1,212.9 MB 807.3 MB 0.67
over 4 MiB 3 41.1 MB 32.4 MB 0.79

On the first 243 programs in the split form the HLSL path writes 49.1 MB on c160d86. With this change the direct path is 1 % above it there, from 37 % above.

Read

  • lower_mem.rs:705 to :713 at 7b87114: s_buffer_load_dwordx2..x16 shares one switch but still loads one dword per rawBufferLoad, each at an address selected to 0 when out of range. Would a 4-wide load at the real address, with the same per-dword select of the result afterwards, be acceptable there (and in buffer_load_dwordx2..x4)? A local prototype on c160d86 that does this, with the chain of four tests left as it is there, wrote 1,031.5 MB for the whole pack in the split form and left 413 programs of 1 MiB or more. That is 97 fewer than 7b87114 leaves, from a tree without its switch and grouped stores; the two together were not measured. On c160d86 each of these dwords is already loaded at its real address and only the result is selected (lower_mem.rs:670–:676, :807–:811); the select of the address is new here. The prototype reads the same bytes main reads, in range or not; it has not run on a GPU.

Not tested: main since #1693 (merged after these passes; it changed the same window dispatch, and GitHub reports this pull request as conflicting with main); the prototype together with this pull request; whether a driver accepts the DXIL, driver compile time, a title run, the test suite and the two new tests (these passes used no card), XCL_DIRECT_DXIL=all, Windows, more than one pass per row. A title run of ours went on beside the passes.

AI use: Claude Code (Claude Fable 5.2) ran the passes, read the diff and wrote this report.

Posted without review by a person: the account owner was away and reads replies later.

@MatyEjsr

Copy link
Copy Markdown

Independent NVIDIA wave32 / split-wave64 verification and rebase review of 7b8711407e77b09f156eb1d7a5dd2a56a6841dfd.

Environment

  • OS: Windows 11 Pro for Workstations (build 26200)
  • CPU: AMD Ryzen 7 7800X3D (8 cores / 16 threads)
  • RAM: 31.4 GB (32 GB installed)
  • GPU: NVIDIA GeForce RTX 3090 (24 GB VRAM, Ampere, wave32 / split wave64, driver 617.42 / 32.0.16.1742)
  • Toolchain: rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC, debug profile, DXC 1.9.2602.17 (21d28f72)

Tested

  • cargo build --workspace -j 8: succeeded in 5.88 s (2 pre-existing Atomic::fetch_update deprecation warnings).
  • cargo test --workspace -j 8 -- --test-threads=8: 860 passed, 0 failed, 42 ignored (811 lib + 49 integration; 19.70 s).
    • SKIPPED (3, counted in libtest's passed total): gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware (no wave64), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • cargo test -p xcl --lib gpu::dxil -j 8 -- --test-threads=8: 82 passed, 0 failed, 1 ignored, 0 skipped (0.47 s).
  • cargo test -p xcl --lib -- multi_window_accesses_emit_one_switch_and_wide_raw_ops multi_window_vector_loads_stores_and_atomics_run_on_the_gpu --test-threads=1 --nocapture: 2 passed, 0 failed, 0 ignored, 0 skipped (0.21 s). Both new PR tests validated with DXC 1.9.2602.17 and passed on the split-wave64 GPU path.
  • XCL_DIRECT_DXIL=1 cargo test -p xcl --lib gpu::recomp -j 8 -- --test-threads=8: 194 passed, 0 failed, 18 ignored (3.98 s; same 2 no wave64 skips). No device removals under 8-thread concurrency.
  • XCL_DIRECT_DXIL=all cargo test -p xcl --lib gpu::recomp -j 8 -- --test-threads=8: 194 passed, 0 failed, 18 ignored (3.93 s; same 2 no wave64 skips). No device removals under 8-thread concurrency.
  • XCL_DIRECT_DXIL=1 XCL_TRANSLATED_CACHE=0 XCL_PSO_DISK_CACHE=0 cargo test -p xcl --test gpu_compute_e2e -- --exact console_wave64_compute_through_the_pso_hook_dispatch_and_read_back --test-threads=1 --nocapture: 1 passed, 0 failed, 0 ignored, 0 skipped (0.36 s; emitted 2868 B DXIL in 0 ms with no HLSL fallback).

Review / rebase notes against current main
All lowering changes in 7b87114 look clean and pass on 24 GB NVIDIA hardware under DXC 1.9.2602.17 (rawBufferLoad/rawBufferStore alignment 4 matches (a & !3), s_buffer_load*/buffer_load* OOB offsets safely clamp to 0 before sel, and seal_region_join pass-through is guarded by written.is_empty()).

However, b5d6322 (Direct DXIL: memory windows through the descriptor heap (XCL_HEAP_WINDOWS)) has since landed on main, leaving this branch conflicting. Three things to watch on rebase:

  1. TRANSLATOR_VERSION in crates/xcl/src/gpu/dxil/kernel.rs is already 5 on main, so this PR will need 6.
  2. b5d6322 changed out-of-range root-window behavior so a VAR_WIN outside 0..mems.len() reads 0 and drops stores/atomics rather than falling back to window 0. In with_mem_n, cases should match 0..arms.len(), the switch default should supply typed zeros (i32c(0) / i64c(0)) to the join phis without calling f, and constant folding should only fold c < mems.len() rather than routing out-of-range constants to window 0.
  3. with_mem_n will also need to handle self.heap_mem (e.g. generalizing with_heap_mem to with_heap_mem_n with direct join phis and seal_region_join), which keeps XCL_HEAP_WINDOWS=1 working and extends the 4-wide mem_ld_n/mem_st_n vectorization to heap-window mode as well.

Not tested

  • AMD / Intel / native wave64 GPUs, XCL_HEAP_WINDOWS=1, release builds, shader pack sweep, and retail title smoke run.

AI use

  • agy with gemini-4-neon-high ran the build/test suite on the hardware above, reviewed the diff against main, and drafted this report.

@MarcosMDev

Copy link
Copy Markdown

Test report: 7b87114 against its base c160d86, retail compute programs, RTX 4060 (Windows). Counts and timings only.

Retail 1.0.2.1 shaders.rpf, translate_compute, 4 windows, split wave64, 7 programs per size range at even spacing (local harness, not a PR). One CreateComputePipelineState per program and build, one program at a time; the head's whole pass ran before the base's. The driver's cache was not cleared or disabled.

code programs DXIL driver, base → head (sum) faster
4–16 KB 7 −21.8 % 12.4 → 9.2 s (−25 %) 7 of 7
16–48 KB 7 −25.2 % 49.2 → 36.8 s (−25 %) 6 of 7
48–96 KB 7 −34.4 % 190.5 → 173.8 s (−9 %) 6 of 7

In the 16–48 KB range one program is 7.4 of the 12.4 s saved (23.5 → 16.1 s). The two slower ones: 2.70 → 3.33 s and 18.4 → 19.2 s.

For comparison, a local prototype that only groups loads (scalar loads, the same compare ladder, no switch, no vector loads) gave −6.5 / −8.9 / −11.0 % DXIL on similar samples (different programs), so I am not opening a PR for it.

Tested: XCL_DIRECT_DXIL=all cargo test -p xcl --lib gpu:: -- --test-threads=4 on 7b87114: 371 passed, 0 failed, 19 ignored. RTX 4060 (32.0.16.2101), Ryzen 7 5700X, Windows 11 26200, rustc 1.100.0, DXC 1.9.2609. Timings from a release build, tests from debug.

One question: a vector load or store that straddles the end of a 4 GiB window no longer wraps its later dwords to the window's start as the per-dword form did. I have not tested it. Intended?

Not tested: the branch merged with #1693 (it conflicts with main now), any title run, AMD / Intel, code over 96 KB, repeat runs.

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

@thiagocalile

Copy link
Copy Markdown

Tested commit 7b8711407e77b09f156eb1d7a5dd2a56a6841dfd on an AMD Radeon RX 6600 (RDNA2, native 64-lane waves): all targeted tests passed.

Tested

  • Hardware: AMD Radeon RX 6600 (8146 MiB VRAM, RDNA2, native wave64 + mesh shaders, driver 32.0.21045.29003; XCL_ADAPTER=radeon), AMD Ryzen 7 5700X3D, 47.9 GB RAM, Windows 11 (10.0.26200.0), Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.8.2502.11, debug build.
  • XCL_ADAPTER=radeon cargo test -p xcl --lib gpu::dxil -- --test-threads=4: 82 passed, 0 failed, 1 ignored (43.36s).
  • XCL_ADAPTER=radeon cargo test --workspace --no-fail-fast -- --test-threads=4: 857 passed, 3 failed, 42 ignored (58.31s).
    • The 3 workspace failures are pre-existing on main on this RX 6600 (placed_title_textures_share_vram_and_preserve_pixels: 3D mip subresource mismatch, plus 2 mutex-poison cascades transitions_apply_when_their_list_is_submitted and d32s8_targets_take_their_own_size_of_video_memory; tracked by D3D12.X: map and unmap 3D texture mips separately #1028).
    • SKIPPED (not counted as coverage): heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows (plus D3D12 debug-layer tests skip because d3d12SDKLayers.dll is not installed).

Not tested

  • Retail title run (scripts/smoke.ps1 — no game executable here), D3D12 debug layer, NVIDIA / Intel GPUs, Linux / Wine.

AI use

  • Antigravity (Gemini 4 Neon) ran the test suites on my RX 6600 and drafted this report; I checked the results before posting.

@ivall ivall added the goal:world-load Bears on the retail title getting past world loading label Nov 24, 2026
@DavidM1911

Copy link
Copy Markdown
Contributor

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

@RandomGithub12309

RandomGithub12309 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing as superseded by 0b64aa0 on main, which already implements single-switch multi-window dispatch (including heap windows) and vectorized 1–4 dword loads/stores.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants