Skip to content

Recompiler: specialize constant-selector v_perm_b32 in direct DXIL - #2267

Closed
ramon-victor wants to merge 1 commit into
xcl-dev:mainfrom
ramon-victor:opt/dxil-perm-b32-const-sel
Closed

ramon-victor wants to merge 1 commit into
xcl-dev:mainfrom
ramon-victor:opt/dxil-perm-b32-const-sel

Conversation

@ramon-victor

Copy link
Copy Markdown
Contributor

Specializes Lw::perm_b32 (v_perm_b32) in the direct DXIL translator when the byte selector operand sel is a compile-time constant, avoiding the ~125-instruction dynamic per-byte select tree emitted for arbitrary runtime selectors.

For constant selectors (the standard RDNA2 idiom for packing, unpacking, byte-swapping, and sign-extending), perm_b32_const:

  • Groups output bytes sharing the same source (s0 or s1) and byte-shift delta into a single shift (and omits the byte mask whenever the shift already zeroes the excluded lanes, reducing identity selectors like 0x03020100/0x07060504 to 0 ops and half-packs like 0x05040100 to 3 ops).
  • Coalesces output bytes sharing the same sign-extension selector (8..=11) into a single ashr (or shl 16 + ashr 31 for bit-15 signs).
  • Folds constant zero (12) and 0xFF (>= 13) bytes directly into a single constant operand.

Part of #1269.

What is in it

  • crates/xcl/src/gpu/dxil/prelude.rs: add Lw::perm_b32_const, dispatched from Lw::perm_b32 when self.s.m.const_u64(sel) is Some(k) (dynamic selectors stay on the existing loop), plus perm_b32_const_tests::gpu_direct_perm_b32_const_selector_matches_isa checking DXIL validation, disassembly (select-tree elimination), and GPU execution across 10 selector patterns covering all selector classes (0..=3, 4..=7, 8..=11, 12, 13, 255).

On a seeded 600-program sample (xcl shaders <archive> --direct --sample 600 --seed 3 --max-bytes 40000 --validate --threads 8, 573 programs within the cap, measured before rebasing onto 1a0bbe3), all 573/573 validate cleanly (0 validator failures):

  • Total emitted DXIL: 42,232,344 B -> 42,221,860 B (-10,484 B)
  • Translation time p95: 302.63 ms -> 284.48 ms

Tested

  • Hardware and toolchain: NVIDIA GeForce RTX 3070 (8 GiB, driver 621.24, wave32), AMD Ryzen 7 5700X, Windows 11 Pro 26300, rustc 1.99.0 (x86_64-pc-windows-msvc), DXC/DXIL 1.8.2502.11 (Windows SDK 10.0.26100.0).
  • cargo test -p xcl --lib perm_b32_const_tests (debug): 1 passed; 0 failed (validates DXIL, verifies select-tree elimination, and verifies 10 selector patterns × 8 inputs on the RTX 3070).
  • .\scripts\test.ps1 (debug, full workspace on head c665154): 1251 passed; 0 failed; 46 ignored (25 GPU sub-checks skipped on this wave32 adapter: 21 no wave64, 1 no heap windows, 3 per-lane #1426 refusals).
  • cargo build --release -p xcl + direct DXIL validation sweep on 573 sampled retail shader programs: 573/573 translated and dxil.dll-validated (0 failures).

Regression run

xcl-smoke v1
commit: f087b998d0d51a9f11bbfa0761967a63e778edd9
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14467 MB
xcl-smoke v1
commit: c6651548d9b30d1da3929e5cbbe7de1d9c6067ca
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1619 (PresentX 1619)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14475 MB

Not tested

  • Native wave64 GPU hardware (AMD/Intel): RTX 3070 only supports wave32, so native wave64 tests skip on this adapter.
  • Sample-gated tests (XCL_SAMPLE_* env vars unset).

AI use

  • Antigravity (Gemini 4 Neon): implemented perm_b32_const in prelude.rs, added DXIL disassembly and GPU ISA parity tests, and ran the full workspace test suite, 573-shader validation benchmark, and 60 s smoke regression run.

@itsSio

itsSio commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tested

Independent Windows verification at clean c6651548d9b30d1da3929e5cbbe7de1d9c6067ca. Both PCs used Windows 11 Pro build 26200, x64 MSVC, and unoptimized dev/test profiles with debug info off. cargo build --workspace --locked and cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --nocapture exited 0 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 1251 / 0 / 46 25
RTX 2080 SUPER / 32.0.16.1088 Ryzen 5 5600X, 16 GiB; Rust 1.99.1, DXC/DXIL 1.8.2502.11 1251 / 0 / 46 27

cargo test -p xcl --lib --locked perm_b32_const_tests -- --test-threads=1 --nocapture: 1 passed / 0 failed / 0 ignored, no runtime skips, on each RTX card and the Radeon RDNA2 iGPU (driver 32.0.21043.5001, same Ryzen 7 system and toolchain). The fixture validated DXIL, checked the generated select count, and compared 10 selectors ? 8 inputs against the CPU reference. The GPU fixture uses wave32 on all three adapters.

Not tested

Native or split wave64 for this fixture, full AMD workspace, Intel, title samples, and performance.

AI use

Ran via Samaritan:

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

@luckyteagirl

Copy link
Copy Markdown
Contributor

Independent RX 6800M check of PR head c6651548d9b30d1da3929e5cbbe7de1d9c6067ca, including a local test-only native-wave64 variant of its new GPU fixture.

Environment: Windows 11 build 26200; Ryzen 9 5980HX; 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; debug cargo test --locked -p xcl --lib -j 2 perm_b32_const_tests -- --test-threads=1 --show-output.

  • Clean exact head: gpu_direct_perm_b32_const_selector_matches_isa passed 1/0/0, no skip. Its KernelOptions is hard-coded to wave64: false, so this is a wave32 execution on the wave64-capable adapter.
  • Local test-only variant: I changed only that fixture's KernelOptions to wave64: true, split: false and its capability check from wave32_supported() to wave64_supported() (with matching skip text). The same selector/input fixture passed 1/0/0, no skip, on native wave64. This validates the PR's unchanged production lowering on the RX 6800M in native-wave64 mode. The two test-only edits were not committed; I restored the clean PR head afterward.

The fixture checks 10 selector patterns across 8 input pairs and validates/disassembles the generated DXIL. I did not run the full workspace suite, a retail-title smoke, split-wave64, or a performance benchmark on this head. The local wave64 result is not an exact-head test-binary result; it uses the exact-head production code with the fixture mode changed as described.

AI use: OpenAI Codex (GPT-6; exact variant not exposed in this session) ran the native Windows tests, made and restored the local fixture-only change, checked the logs, and drafted this report. No game data was used.

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

Copy link
Copy Markdown
Contributor

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

Thanks!

@raylitebull

Copy link
Copy Markdown
Member

Diff review, CI, and both AMD and NVIDIA checks passed on c665154, but recent origin/main merges turned this branch CONFLICTING/DIRTY. Please rebase onto origin/main so it becomes mergeable again.

Kalmarv added a commit to Kalmarv/xcl that referenced this pull request Nov 30, 2026
…ecialize const v_perm_b32

Rebases xcl-dev#2212 and xcl-dev#2267 onto main and adds constant reversebits folding.

Co-authored-by: tibzejoker <tibzejoker@users.noreply.github.com>
Co-authored-by: ramon-victor <ramon-victor@users.noreply.github.com>
Prime1997 added a commit that referenced this pull request Nov 30, 2026
Recompiler: drop dead raw loads/f16 convs (#2212), specialize const v_perm_b32 (#2267), fold const reversebits
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head c665154

#3503 (main commit 6ff86e3, "specialize const v_perm_b32 (#2267)") merged the same perm_b32_const and the same constant-selector branch in perm_b32, line for line. Your GPU test gpu_direct_perm_b32_const_selector_matches_isa is not on main. It could go in as a small test-only PR.
Recommendation: close (superseded by #3503 / main commit 6ff86e3).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded: this change is already on main (6ff86e3). 6ff86e3 (rebases #2212 and #2267) has perm_b32_const in dxil/prelude.rs line for line and the const_u64 call site; only the PR's GPU test perm_b32_const_tests is not on main (test-only remainder)

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

@throny throny added the duplicate This issue or pull request already exists label Dec 1, 2026
@throny throny closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists 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.

7 participants