Recompiler: specialize constant-field s_bfe_u32 and s_bfe_i32 in direct DXIL #2281

Open
github-bot wants to merge 1 commit from github-bot/gh-2281 into main AGit
Owner

@⁠ramon-victor on GitHub · 2026-11-25 12:16 UTC · original
ramon-victor:opt/dxil-sbfe-const-field → main · +127 −0 · 1 files · 1 commits · open

Specializes Lw::sbfe_u (s_bfe_u32) and Lw::sbfe_i (s_bfe_i32) in the direct DXIL translator when the packed offset/width operand c (offset = c & 31, width = (c >> 16) & 0x7F) is a compile-time constant.

While constant-folding in Lw::bin / Lw::sel already collapses the mask/width predicates for constant c, specializing directly:

  • Avoids emitting 10–11 intermediate IR ops per s_bfe_* before optimization.
  • Drops the trailing & ((1 << width) - 1) mask in s_bfe_u32 (and the shl + ashr sign-extension pair in s_bfe_i32) whenever width >= 32 - off, since the right shift already clears/sign-extends the top off bits.
  • Lowers interior signed extraction (1 <= width < 32 - off) in s_bfe_i32 with 2 shifts (shl(v, 32 - off - width) then ashr(..., 32 - width)) instead of 3 shifts (ashr + shl + ashr).

Scalar BFE uses a 7-bit width (0..=127 where width >= 32 extracts all remaining bits), so this remains exact without using DXIL Ubfe/Ibfe (which mask width to 5 bits).

Part of #1269.

What is in it

  • crates/xcl/src/gpu/dxil/lower.rs:
    • Add constant-field fast paths to Lw::sbfe_u and Lw::sbfe_i (dynamic c stays on the existing general lowering).
    • Add sbfe_const_tests::sbfe_const_algebra_matches_reference_exhaustively verifying every (off, width) pair in 0..32 x 0..128 against the reference ISA semantics, and sbfe_const_tests::gpu_direct_sbfe_const_field_validates_and_matches_isa checking DXIL validation, disassembly (0 selects), and GPU execution across 10 edge-case (off, width) pairs for both S_BFE_U32 and S_BFE_I32.

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), all 573/573 validate cleanly (0 validator failures):

  • Total emitted DXIL: 41,323,736 B -> 41,322,584 B (-1,152 B)

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 gpu::dxil::lower::sbfe_const_tests (debug): 2 passed; 0 failed (exhaustive 4,096 (off, width) algebraic proof + 20-op GPU execution check on RTX 3070).
  • .\scripts\test.ps1 (debug, full workspace): 1252 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: 837364677f87988b5081e7ad0e9b61fd4f28644f
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1614 (PresentX 1614)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14466 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 constant-field specialization in Lw::sbfe_u and Lw::sbfe_i, added exhaustive algebraic and GPU ISA tests in lower.rs, and ran the full workspace test suite, 573-shader validation benchmark, and 60 s smoke regression run.
**@⁠ramon-victor** on GitHub · 2026-11-25 12:16 UTC · [original](https://github.com/xcl-dev/xcl/pull/2281) `ramon-victor:opt/dxil-sbfe-const-field` → `main` · +127 −0 · 1 files · 1 commits · open Specializes `Lw::sbfe_u` (`s_bfe_u32`) and `Lw::sbfe_i` (`s_bfe_i32`) in the direct DXIL translator when the packed offset/width operand `c` (`offset = c & 31`, `width = (c >> 16) & 0x7F`) is a compile-time constant. While constant-folding in `Lw::bin` / `Lw::sel` already collapses the mask/width predicates for constant `c`, specializing directly: - Avoids emitting 10–11 intermediate IR ops per `s_bfe_*` before optimization. - Drops the trailing `& ((1 << width) - 1)` mask in `s_bfe_u32` (and the `shl` + `ashr` sign-extension pair in `s_bfe_i32`) whenever `width >= 32 - off`, since the right shift already clears/sign-extends the top `off` bits. - Lowers interior signed extraction (`1 <= width < 32 - off`) in `s_bfe_i32` with 2 shifts (`shl(v, 32 - off - width)` then `ashr(..., 32 - width)`) instead of 3 shifts (`ashr` + `shl` + `ashr`). Scalar BFE uses a 7-bit width (`0..=127` where `width >= 32` extracts all remaining bits), so this remains exact without using DXIL `Ubfe`/`Ibfe` (which mask width to 5 bits). Part of #1269. **What is in it** - `crates/xcl/src/gpu/dxil/lower.rs`: - Add constant-field fast paths to `Lw::sbfe_u` and `Lw::sbfe_i` (dynamic `c` stays on the existing general lowering). - Add `sbfe_const_tests::sbfe_const_algebra_matches_reference_exhaustively` verifying every `(off, width)` pair in `0..32 x 0..128` against the reference ISA semantics, and `sbfe_const_tests::gpu_direct_sbfe_const_field_validates_and_matches_isa` checking DXIL validation, disassembly (0 selects), and GPU execution across 10 edge-case `(off, width)` pairs for both `S_BFE_U32` and `S_BFE_I32`. 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), all 573/573 validate cleanly (`0` validator failures): - Total emitted DXIL: `41,323,736 B -> 41,322,584 B` (`-1,152 B`) **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 gpu::dxil::lower::sbfe_const_tests` (debug): `2 passed; 0 failed` (exhaustive 4,096 `(off, width)` algebraic proof + 20-op GPU execution check on RTX 3070). - `.\scripts\test.ps1` (debug, full workspace): `1252 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: 837364677f87988b5081e7ad0e9b61fd4f28644f gpu: NVIDIA GeForce RTX 3070 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 1614 (PresentX 1614) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14466 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 constant-field specialization in `Lw::sbfe_u` and `Lw::sbfe_i`, added exhaustive algebraic and GPU ISA tests in `lower.rs`, and ran the full workspace test suite, 573-shader validation benchmark, and 60 s smoke regression run.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:25:34 +00:00
Author
Owner

@⁠DarkPanda69 on GitHub · 2026-11-25 12:59 UTC · original

The AMD native wave64 run listed as untested, plus an RTX 3060: the new test passes on both.

Head 8373646 applied on main 1a0bbe3, debug, Windows 11 Pro 26200, cargo test -p xcl --lib -- gpu:: --test-threads=4:

  • RX 9070 XT (RDNA4, driver 32.0.32029.2010, waves 32..64): gpu_direct_sbfe_const_field_validates_and_matches_isa ok. 631 passed, 2 failed, 22 ignored, 6 SKIPPED, the same as main plus the new test; the 2 failures are the AMD fuzz seeds tracked in #2115.
  • RTX 3060 (driver 32.0.16.1088): the new test ok; 633 passed, 0 failed, 22 ignored.

Not tested: a title with XCL_DIRECT_DXIL=1.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

**@⁠DarkPanda69** on GitHub · 2026-11-25 12:59 UTC · [original](https://github.com/xcl-dev/xcl/pull/2281#issuecomment-7163143327) The AMD native wave64 run listed as untested, plus an RTX 3060: the new test passes on both. Head `8373646` applied on `main` `1a0bbe3`, debug, Windows 11 Pro 26200, `cargo test -p xcl --lib -- gpu:: --test-threads=4`: - **RX 9070 XT** (RDNA4, driver 32.0.32029.2010, waves 32..64): `gpu_direct_sbfe_const_field_validates_and_matches_isa` ok. 631 passed, 2 failed, 22 ignored, 6 SKIPPED, the same as `main` plus the new test; the 2 failures are the AMD fuzz seeds tracked in #2115. - **RTX 3060** (driver 32.0.16.1088): the new test ok; 633 passed, 0 failed, 22 ignored. Not tested: a title with `XCL_DIRECT_DXIL=1`. AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.
Author
Owner

@⁠nnonno91 on GitHub · 2026-11-25 15:10 UTC · original

Independent Windows test of 837364677f87988b5081e7ad0e9b61fd4f28644f. Signed and unsigned constant bit-field results matched the reference on Radeon integrated graphics and RTX 5090.

Tested

  • Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, Radeon integrated graphics (32.0.21042.62), RTX 5090 (32.0.16.1742), Rust 1.100.0 x64 MSVC, Windows SDK DXC/DXIL 1.8.2502.11. Unoptimized test profile.
  • Ran cargo test --locked -p xcl --lib sbfe_const_tests -- --nocapture --test-threads=1 once per explicitly selected adapter. Each run reported 2 passed, 0 failed, 0 ignored, and no logged skips.
  • Expanded the local GPU fixture from 10 to 16 offset/width pairs and from four to seven input words. The additions covered offsets plus widths just below, at, and above 32, width 127, and zero and all-one inputs. All 224 signed and unsigned GPU comparisons per adapter matched the ISA reference. DXIL validation and the no-select assertion also passed. The existing exhaustive CPU test passed.

Not tested

Dynamic fields on GPU, the parent revision, retail titles, or the full workspace suite. The fixture changes were temporary.

AI use

OpenAI Codex (GPT-6.2 Sol) prepared and ran the tests, verified the results, and wrote this report.

**@⁠nnonno91** on GitHub · 2026-11-25 15:10 UTC · [original](https://github.com/xcl-dev/xcl/pull/2281#issuecomment-7166422100) Independent Windows test of `837364677f87988b5081e7ad0e9b61fd4f28644f`. Signed and unsigned constant bit-field results matched the reference on Radeon integrated graphics and RTX 5090. **Tested** - Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, Radeon integrated graphics (32.0.21042.62), RTX 5090 (32.0.16.1742), Rust 1.100.0 x64 MSVC, Windows SDK DXC/DXIL 1.8.2502.11. Unoptimized test profile. - Ran `cargo test --locked -p xcl --lib sbfe_const_tests -- --nocapture --test-threads=1` once per explicitly selected adapter. Each run reported 2 passed, 0 failed, 0 ignored, and no logged skips. - Expanded the local GPU fixture from 10 to 16 offset/width pairs and from four to seven input words. The additions covered offsets plus widths just below, at, and above 32, width 127, and zero and all-one inputs. All 224 signed and unsigned GPU comparisons per adapter matched the ISA reference. DXIL validation and the no-select assertion also passed. The existing exhaustive CPU test passed. **Not tested** Dynamic fields on GPU, the parent revision, retail titles, or the full workspace suite. The fixture changes were temporary. **AI use** OpenAI Codex (GPT-6.2 Sol) prepared and ran the tests, verified the results, and wrote this report.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 17:17 UTC · original

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

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 17:17 UTC · [original](https://github.com/xcl-dev/xcl/pull/2281#issuecomment-7169286575) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
This pull request has changes conflicting with the target branch.
  • crates/xcl/src/gpu/dxil/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2281/head:github-bot/gh-2281'
git switch 'github-bot/gh-2281'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2281'
git switch 'github-bot/gh-2281'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2281'
git switch 'github-bot/gh-2281'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2281'
git switch 'main'
git merge --squash 'github-bot/gh-2281'
git switch 'main'
git merge --ff-only 'github-bot/gh-2281'
git switch 'main'
git merge 'github-bot/gh-2281'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.