Skip to content

Recompiler: direct DXIL folds EXEC selects no lane observes, and the bitcast pairs they leave - #2576

Draft
Kalmarv wants to merge 3 commits into
xcl-dev:mainfrom
Kalmarv:op-diet/on-1906
Draft

Kalmarv wants to merge 3 commits into
xcl-dev:mainfrom
Kalmarv:op-diet/on-1906

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #1906: review only the last two commits; this rebases onto main when #1906 lands.

The direct RDNA2 → DXIL translator writes a VGPR under EXEC as select(exec, new, old). Two passes in Module::optimize shrink the DXIL handed to the driver. Part of #1096.

  1. EXEC-select fold. A lane-local instruction under the same EXEC that reads such a register, and whose own result is selected by that EXEC again, never observes old. The pass tracks, per value, where it is observed (unused, only where one condition holds, all lanes) and reads through those selects; DCE drops the selects left without uses. Phis, memory, wave ops, derivatives and integer division observe all lanes and keep their selects.
  2. Bitcast-pair fold. Reading through select(exec, bitcast(f), old) turns a float operation's bitcast(select) operand into bitcast(bitcast(f)). The CSE peephole that folds such pairs runs before the select fold, so a linear pass after it maps bitcast(bitcast(x)) back to x when the types match (64-bit kept, as in the peephole).

#1972 (@raylitebull) had the select-guard idea first (propagate_select_guards). The fold here tracks observation per value and reaches more selects: on the bench below it saves 8.1 % of the bytes on main against 4.1 % for #1972, and #1972 adds 0.5 % on top of it. Our offer on #1972 (#1972 (comment)) to combine the two still stands. This PR came out of @Decodere's suggestion on #1906 to keep these passes separate from it.

What is in it

  • Recompiler: fold direct DXIL EXEC selects no lane observes: fold_guarded_selects and lane_local in crates/xcl/src/gpu/dxil/module/opt.rs, run after CSE (XCL_DXIL_NO_OPT=select leaves it out); NO_SELECT_FOLD (tests only), handed to the translator thread in kernel.rs; unit test guarded_selects_fold_where_no_lane_sees_the_other_arm; GPU test gpu_select_fold_keeps_inactive_lanes in crates/xcl/src/gpu/dxil/select_tests.rs.
  • Recompiler: fold the bitcast pairs the EXEC select fold leaves: fold_bitcast_pairs in module/opt.rs (XCL_DXIL_NO_OPT=pairs); NO_PAIR_FOLD (tests only); unit test bitcast_pairs_left_by_the_select_fold_fold (fails with the pass left out); GPU test gpu_bitcast_pair_fold_keeps_words.
  • Both GPU tests translate the same program with and without the pass and compare every VGPR of every lane and the SGPRs. The programs are VALU chains under a narrowed, re-narrowed and restored EXEC, with v_readlane and DPP reads while lanes are off, NaN (quiet and signalling payloads), signed-zero, infinity and denormal operands, and sentinels in lanes that never run. They run in wave32 and wave64, whole and split, and the folded form must be smaller.
  • 457 changed lines: 183 runtime, 274 tests.

Tested

  • Size, offline (no GPU): a release xcl.exe translates and binds the 802 compute programs that a retail title (title: ea3b5e4d) has in use at its device-memory crash.

    tree bound DXIL MiB instructions
    main 6d12f58a 239.44 31,926,579
    main + select fold 219.95 28,068,096
    main + both passes 212.88 27,188,468
    Recompiler: fold 64-bit ints, bitcast selects and commutative CSE in direct DXIL #1906 894e29d6 235.61 30,634,434
    Recompiler: fold 64-bit ints, bitcast selects and commutative CSE in direct DXIL #1906 + both passes (this head) 210.55 26,710,803

    That is −10.6 % bytes and −12.8 % instructions on top of Recompiler: fold 64-bit ints, bitcast selects and commutative CSE in direct DXIL #1906.

  • Native code on the NVIDIA driver, RTX 5070 Ti, measured on main with and without the select fold: 4 of the largest of these programs go from 3.60 to 3.40 MB of driver blob (−5.4 %). The bitcast-pair fold changes only the DXIL: on a synthetic chain of 512 v_mul_legacy_f32 the driver's code is the same with and without it.

  • Graphics, measured on main by our graphics size bench (runtime path, XCL_DIRECT_DXIL=all): VS DXIL 30.41 → 26.73 MiB (−12.1 %), PS of the cold pipelines 104.41 → 94.73 MiB (−9.3 %), 0 failed translations.

  • GPU tests at 71b76742, on an RTX 2060 (NVIDIA 595.91) under Wine 11.21 + vkd3d-proton master 30b0eb9, DXC 1.9.2609, release test executables:

    • select_tests module::opt: 11 passed, 0 failed, 0 ignored, no SKIPPED (main e1d84b7d: 7 / 0 / 0). Both new GPU tests ran on the GPU and passed.
    • gpu::dxil --test-threads=4: 186 passed, 2 failed, 3 ignored (main e1d84b7d: 181 / 2 / 3). The same two tests fail on main on that stack: raw_loads_in_root_windows_respect_exec_and_bounds (memory-window gap of that Wine build) and directed_reference_basics (fma precision under vkd3d). SKIPPED: dispatch_chain_native_wave64_builds_whole (no wave64).
  • Whole suite at 71b76742 on an RTX 5070 Ti (NVIDIA 610.74), Windows 11 Pro 26200, rustc 1.99.0, DXC 1.9.2609, debug: cargo test --workspace --no-fail-fast -- --test-threads=4: 1,428 passed, 0 failed, 46 ignored. 25 SKIPPED notices, none from the new tests: 21 no wave64, 3 forms refused in f16_programs_on_the_gpu / f32_programs_on_the_gpu (gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics), 1 no heap windows. Then select_tests --nocapture: both new GPU tests ran and passed.

  • Output of a retail compute program (title: ea3b5e4d, program 6f3a2502a8d6b129) on the GPU, RTX 5070 Ti, native Windows D3D12, one seeded 8×16 group, guest wave64 split on wave32, full buffer and texture readback: main 6d12f58a, main + the select fold, and main + both passes each change 2,048 words, 0 different from main, and match the expected 256/256 texels; two repeated main controls also match. DXIL 477,884 → 454,064 → 437,536 bytes. The pass code is this head's, except that simplify_op takes Recompiler: fold 64-bit ints, bitcast selects and commutative CSE in direct DXIL #1906's extra argument.

Tested by others

Regression run

  • Title smoke pair, 60 s each, RTX 5070 Ti, NVIDIA 610.74, release builds. The base side is Recompiler: fold 64-bit ints, bitcast selects and commutative CSE in direct DXIL #1906's head 894e29d6, the base of this stacked PR, not main. It ran after one warm-up run of the base and back to back with it, as one job beside a CPU-bound shader precompile on the same machine that was running at both ends of both sides. Frames 1,291 against 1,318 (98 %), peak commit 26,833 against 26,885 MB, no crash or device removal.

    Base, Recompiler: fold 64-bit ints, bitcast selects and commutative CSE in direct DXIL #1906's head 894e29d6:

    xcl-smoke v1
    commit: 894e29d63b3a668f5d04bb74a6cc88f06798805f
    gpu: NVIDIA GeForce RTX 5070 Ti
    title: ea3b5e4d
    seconds: 60
    exit: 0x0
    frames shown: 1318 (PresentX 1318)
    presents per 2 s (last 3): 37 43 33
    device removals: 0
    crashes: 0
    peak commit: 26885 MB
    

    This head, 71b76742:

    xcl-smoke v1
    commit: 71b76742803a7055d1feb9083cdd290c56a96abc
    gpu: NVIDIA GeForce RTX 5070 Ti
    title: ea3b5e4d
    seconds: 60
    exit: 0x0
    frames shown: 1291 (PresentX 1291)
    presents per 2 s (last 3): 38 52 5
    device removals: 0
    crashes: 0
    peak commit: 26833 MB
    

Not tested

AI use

  • Claude Code, Claude Opus 5.5: wrote the code, the tests, the measurements and this description.

🤖 Generated with Claude Code

Decodere and others added 3 commits November 25, 2026 23:09
…direct DXIL

Extend `gpu::dxil::module::opt` to fold remaining post-phi redundancies in
direct-DXIL shaders without colliding with CFG-level folding or violating DXC
validator shader-flag invariants:

- Enable 64-bit integer constant/identity folding and DCE (`i64` arithmetic,
  shifts, `LShr(ZExt)` / `LShr(Or(ZExt, Shl(ZExt)))` window-high patterns,
  `Trunc(ZExt|SExt)`, and `i1` / `ZExt(i1)` compares) and clear
  `Feature::Int64Ops` in `Shader::infer_type_features` when no surviving
  instruction uses `i64`.
- Fold finite constant `BitCast` (`i32 <-> f32`, `i16 <-> f16`, excluding NaN
  and Inf in both directions), reuse dominating `BitCast`s from CSE, collapse
  nested same-condition `Select` chains, and push `BitCast` through `Select`
  only when the `Select` has no surviving non-bitcast uses (preventing select
  duplication).
- Canonicalize commutative integer binops (`Add`/`Mul`/`And`/`Or`/`Xor`) and
  integer `Cmp` predicates before dominator CSE, separate `XCL_DXIL_NO_OPT=fold`
  from `cse`, and expand flag-neutral `ReadNone` intrinsic DCE.
- Add validator + RTX 4060 GPU execution parity tests (direct-DXIL cache
  invalidation is automatic via `XCL_DIRECT_DXIL_SOURCE_ID`).

On a 252-program validated direct-DXIL corpus sample (100% across CS/GS/MS/PS/VS):
-3.25% DXIL bytes (-397 KB, 252/252 shrank), -6.33% instructions, -28.74%
bitcasts, -2.30% selects, -38.32% i64 ops, 0 validator errors (and 0 errors
across 977 programs in a 1,000-record sample).

Assisted-by: Google Antigravity (Gemini 4 Neon)
The direct translator writes a VGPR under EXEC as select(exec, new, old).
A lane-local instruction under the same EXEC that reads the register, and
whose own result is selected by that EXEC again, never observes the old
arm. optimize() now tracks where each value is observed (unused, only
where one condition holds, all lanes) and reads through such selects;
DCE then drops the selects left without uses. Phis, memory, wave ops,
derivatives and integer division observe all lanes.

XCL_DXIL_NO_OPT=select leaves the pass out. Tests turn it off per thread;
the translator thread takes the caller's setting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The select fold reads through select(exec, bitcast(f), old) to the
bitcast, so a float operation that read the register through
bitcast(select) now reads bitcast(bitcast(f)). The CSE peephole that
folds such pairs ran before the fold, so they stayed (6,305 in one
program of 110 k instructions). A linear pass after the fold maps
bitcast(bitcast(x)) back to x when the types match (64-bit kept, as in
the peephole); DCE drops the casts left without uses.

Measured on the 802 programs in use at the crash (bound DXIL bytes):
239.44 MiB on main, 219.95 with the select fold, 212.88 with this.

XCL_DXIL_NO_OPT=pairs leaves the pass out.

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

Copy link
Copy Markdown

Independent NVIDIA GPU check of 71b7674 on Windows 11 build 26300, RTX 3080 Ti / 621.35, rustc 1.99.1, DXC 1.8.2502.11 (debug build).

Tested

  • With XCL_DXIL_NO_OPT=pairs, cargo test -p xcl --lib gpu_select_fold_keeps_inactive_lanes -- --nocapture --test-threads=1: 1 passed, 0 failed, 0 ignored. Disabling the pair pass makes this comparison isolate the EXEC-select fold.
  • With that variable cleared, cargo test -p xcl --lib gpu_bitcast_pair_fold_keeps_words -- --nocapture --test-threads=1: 1 passed, 0 failed, 0 ignored.
  • cargo build --workspace: passed. With the variable cleared, cargo test --workspace --no-fail-fast -- --test-threads=4: 1,428 passed, 0 failed, 46 ignored; 25 explicit SKIPPED GPU notices. The two new GPU tests passed without skips.

Not tested: AMD or native hardware wave64, production title smoke, native driver blob size, memory use, or world-load time.

AI use: Codex (GPT-6.2 Sol) reviewed the test setup, ran these commands, and drafted this report.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 26 ignored.

Thanks!

— Devin

@luckyteagirl

Copy link
Copy Markdown
Contributor

RX 6800M focused GPU report for #2576: both new select/bitcast fold parity fixtures passed on the exact PR head.

Setup: clean detached head 71b76742803a7055d1feb9083cdd290c56a96abc; Windows 11 Home build 26200; Ryzen 9 5980HX; AMD Radeon RX 6800M, driver 30.0.13002.13003; Rust 1.99.0 MSVC; DXC/dxil 1.9.2609.5. XCL_ADAPTER=6800M XCL_REQUIRE_GPU=wave64 XCL_DIRECT_DXIL=1, pinned XCL_DXC_DIR, debug lib-test build in an isolated Cargo target.

Tested: cargo test --locked -p xcl --lib -j 1 gpu_select_fold_keeps_inactive_lanes -- --test-threads=1 --show-output under a free-memory guard: 1 passed, 0 failed, 0 ignored, 0 measured, 1328 filtered out, 1.27 s, no SKIPPED line, guard did not stop the run. Then the exact gpu::dxil::select_tests::gpu_bitcast_pair_fold_keeps_words fixture was run directly from the just-built test binary with --exact --test-threads=1 --nocapture and the same environment: 1 passed, 0 failed, 0 ignored, 0 measured, 1328 filtered out, 0.59 s, no SKIPPED line. This second run did not rebuild.

Both fixtures compare folded and unfolded GPU results across all tested lanes/registers. The source always runs wave32 whole and split-wave64 forms, and conditionally runs native wave64 when runner::wave64_supported() reports true. The test output does not record that capability or print which forms ran, so I cannot confirm the native-wave64 branch from this log alone.

Not tested: full workspace suite, title smoke pair, other GPU vendors, or a measured native-wave64-only run. No game files were used.

AI use: Codex (GPT-6) and a Codex Luna subagent prepared the clean worktree, ran the focused tests, checked the counts and source-level form coverage, and wrote this report.

@bielfreedom

Copy link
Copy Markdown

AMD Windows driver timing for #2576 (the last two commits, on top of #1906): on the RX 9070 XT the EXEC-select fold cuts direct DXIL by 7.6 % over all 2,689 retail compute programs, and AMD's CreateComputePipelineState is about 6 % faster over 26 large ones. No pipeline failed.

Setup

  • GPU: RX 9070 XT 16 GB (RDNA4, native wave64), AMD driver 32.0.32029.2010.
  • CPU and memory: Ryzen 7 7700 (16 threads), 32 GB RAM, Windows 11, release.
  • Builds, both merged onto main 08db802:
  • Both builds carry a local timing patch in xcl shaders that writes each program's DXIL size and each CreateComputePipelineState time to a file.
  • Offline: xcl shaders <retail shaders.rpf> --precompile --only cs --pipelines with XCL_DIRECT_DXIL=1, switch windows, XCL_PSO_DISK_CACHE=0, one driver thread, a fresh translation cache and a fresh executable name for every run (the AMD driver keys its own cache on the exe name, so every build is cold).

DXIL size, all 2,689 compute programs (translation only)

build total direct DXIL programs changed
main 885.9 MB
#1906 875.1 MB (−1.2 %) 2,659 vs main
this PR 808.7 MB (−7.6 % vs #1906) 2,528 vs #1906

Driver time, 26 large programs, 3 interleaved rounds

build pipelines (incl. split pieces) DXIL to driver driver time r1 / r2 / r3 peak memory
#1906 75 9.45 MB 76.1 / 81.6 / 83.9 s 1.47 GB
this PR 67 8.47 MB 75.2 / 75.4 / 77.3 s 1.39 GB

Mean 80.5 → 76.0 s (−5.7 %). The PR was faster in every round, but by 1 % in the first, so read it as a modest gain. Per program, the larger ones gain the most; small ones move in both directions, within noise:

program DXIL #1906 → PR driver ms #1906 driver ms PR
620491641aa9bad6 370 → 326 KB 10,701 / 11,328 / 11,655 10,172 / 10,280 / 10,507
6d1e4583f8129771 629 → 576 KB 6,856 / 6,997 / 7,226 6,397 / 6,300 / 6,493
6f3a2502a8d6b129 360 → 336 KB 3,440 / 3,899 / 3,851 2,944 / 2,995 / 3,021
6fd9a5ebc95aee46 317 → 288 KB 2,706 / 3,007 / 3,107 2,614 / 2,613 / 2,713

With the fold, the six split programs produce 47 pieces instead of 55.

Not tested: heap windows, GPU output, and a title run.

AI use: Claude Code (Claude Opus 5.5) built the binaries, ran the measurements and wrote this report; I posted it.

@pvfreis

pvfreis commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Test report for 71b76742 (on #1906 894e29d6), stacked on flat heap + no-carry and #2567: NVIDIA driver code size (GetCachedBlob) on Ampere.

Tested

Not tested: the suite, this PR's GPU tests or any correctness arm with these passes on; timing (build or dispatch); a title run; graphics; other GPUs.

AI use: Claude Code. Claude Opus 5.5 wrote the size test, ran it and wrote this; Claude Fable 5.2 rechecked it.

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