Recompiler: simplify bfm / s_bfm_b64 and lower ffbh_i to FirstbitSHi in direct DXIL #2559

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

@⁠ramon-victor on GitHub · 2026-11-25 22:57 UTC · original
ramon-victor:opt/dxil-bfm-and-ffbh-i → main · +161 −28 · 1 files · 1 commits · open

Part of #1269.

Problem

In crates/xcl/src/gpu/dxil/lower.rs, bitfield mask and signed find-first-bit-high helpers emitted unnecessarily heavy DXIL sequences:

  • bfm (s_bfm_b32, v_bfm_b32) computed select(w == 0, 0, u32::MAX >> ((32 - w) & 31)) << (b & 31) (8 instructions including icmp and select), even though w = a & 31 is already strictly in 0..=31, so ((1u32 << w) - 1) << (b & 31) is exact for every w including w = 0.
  • s_bfm_b64 used the same 12-instruction icmp/select/lshr i64 expansion even when both operands are compile-time constants, forcing i64 arithmetic and Feature::Int64Ops for constant lane masks.
  • ffbh_i (v_ffbh_i32, s_flbit_i32) manually emulated signed find-first-bit-high with icmp slt 0, bitwise NOT, select, and FirstbitHi (4 instructions), instead of lowering directly to DXIL's BitCount::FirstbitSHi (dx.op.unaryBits.i32 op 34).

Fix

  • Simplify bfm (lower.rs) to ((1 << (a & 31)) - 1) << (b & 31) with direct constant folding when a (or both a and b) is constant.
  • Constant-fold s_bfm_b64 (lower.rs) directly to two i32 constants without touching i64 or Feature::Int64Ops; for dynamic operands, simplify to ((1u64 << (al & 63)) - 1) << (bl & 63) with no icmp/select.
  • Lower ffbh_i (lower.rs) directly to self.s.bit_count(BitCount::FirstbitSHi, v).
  • Add lower::bfm_ffbh_tests::bfm_and_ffbh_i_ir_and_gpu_parity checking DXIL validation, disassembly (zero Int64Ops for constant s_bfm_b64, FirstbitSHi op 34 without icmp slt for v_ffbh_i32/s_flbit_i32, zero select for dynamic bfm), and wave32 RTX 3070 GPU parity across all edge cases (0, -1, 1, -2, 0x7FFF_FFFF, 0x8000_0000, widths 0/1/16/31/32/63/64, offsets 0..63).

Measured impact

573-program retail direct-DXIL validation sweep (dxil.dll 1.8.2502.11):

  • Baseline (6d12f58): 573/573 validated (val_fail=0), 51,117,488 total DXIL bytes (p50=22,865 us, p95=408,132 us).
  • This PR (7bf4853): 573/573 validated (val_fail=0), 51,116,080 total DXIL bytes (-1,408 B / -0.003%; p50=19,313 us, p95=376,258 us).

Tested

  • Targeted IR disassembly + RTX 3070 GPU parity test (cargo test --lib -p xcl bfm_ffbh): 1 passed, 0 failed.
  • Full workspace (.\scripts\test.ps1, NVIDIA GeForce RTX 3070, Windows 11): 1424 passed, 0 failed, 46 ignored.
  • 573-shader direct DXIL dxil.dll validation sweep: 573/573 passed, 0 failures.
  • Release build (cargo build --release -p xcl) and 60 s retail smoke run (see below).

Not tested

  • Wave64-only GPU paths (the RTX 3070 runs wave32; wave64 GPU tests skipped automatically on this adapter).

Regression run

Main (f087b99):

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

This PR (7bf4853):

xcl-smoke v1
commit: 7bf48538afdea8d4bf8e56645a450b5c77b1e405
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: 14019 MB

AI use

  • Antigravity (Gemini 4 Neon): implementation, IR & GPU parity tests, 573-shader DXIL sweep, and PR drafting.
**@⁠ramon-victor** on GitHub · 2026-11-25 22:57 UTC · [original](https://github.com/xcl-dev/xcl/pull/2559) `ramon-victor:opt/dxil-bfm-and-ffbh-i` → `main` · +161 −28 · 1 files · 1 commits · open Part of #1269. **Problem** In `crates/xcl/src/gpu/dxil/lower.rs`, bitfield mask and signed find-first-bit-high helpers emitted unnecessarily heavy DXIL sequences: - `bfm` (`s_bfm_b32`, `v_bfm_b32`) computed `select(w == 0, 0, u32::MAX >> ((32 - w) & 31)) << (b & 31)` (8 instructions including `icmp` and `select`), even though `w = a & 31` is already strictly in `0..=31`, so `((1u32 << w) - 1) << (b & 31)` is exact for every `w` including `w = 0`. - `s_bfm_b64` used the same 12-instruction `icmp`/`select`/`lshr` `i64` expansion even when both operands are compile-time constants, forcing `i64` arithmetic and `Feature::Int64Ops` for constant lane masks. - `ffbh_i` (`v_ffbh_i32`, `s_flbit_i32`) manually emulated signed find-first-bit-high with `icmp slt 0`, bitwise NOT, `select`, and `FirstbitHi` (4 instructions), instead of lowering directly to DXIL's `BitCount::FirstbitSHi` (`dx.op.unaryBits.i32` op `34`). **Fix** - Simplify `bfm` (`lower.rs`) to `((1 << (a & 31)) - 1) << (b & 31)` with direct constant folding when `a` (or both `a` and `b`) is constant. - Constant-fold `s_bfm_b64` (`lower.rs`) directly to two `i32` constants without touching `i64` or `Feature::Int64Ops`; for dynamic operands, simplify to `((1u64 << (al & 63)) - 1) << (bl & 63)` with no `icmp`/`select`. - Lower `ffbh_i` (`lower.rs`) directly to `self.s.bit_count(BitCount::FirstbitSHi, v)`. - Add `lower::bfm_ffbh_tests::bfm_and_ffbh_i_ir_and_gpu_parity` checking DXIL validation, disassembly (zero `Int64Ops` for constant `s_bfm_b64`, `FirstbitSHi` op `34` without `icmp slt` for `v_ffbh_i32`/`s_flbit_i32`, zero `select` for dynamic `bfm`), and wave32 RTX 3070 GPU parity across all edge cases (`0`, `-1`, `1`, `-2`, `0x7FFF_FFFF`, `0x8000_0000`, widths `0/1/16/31/32/63/64`, offsets `0..63`). **Measured impact** 573-program retail direct-DXIL validation sweep (`dxil.dll` 1.8.2502.11): - Baseline (`6d12f58`): `573/573` validated (`val_fail=0`), `51,117,488` total DXIL bytes (`p50=22,865 us`, `p95=408,132 us`). - This PR (`7bf4853`): `573/573` validated (`val_fail=0`), `51,116,080` total DXIL bytes (`-1,408 B` / `-0.003%`; `p50=19,313 us`, `p95=376,258 us`). **Tested** - Targeted IR disassembly + RTX 3070 GPU parity test (`cargo test --lib -p xcl bfm_ffbh`): 1 passed, 0 failed. - Full workspace (`.\scripts\test.ps1`, NVIDIA GeForce RTX 3070, Windows 11): `1424 passed, 0 failed, 46 ignored`. - 573-shader direct DXIL `dxil.dll` validation sweep: `573/573` passed, `0` failures. - Release build (`cargo build --release -p xcl`) and 60 s retail smoke run (see below). **Not tested** - Wave64-only GPU paths (the RTX 3070 runs wave32; wave64 GPU tests skipped automatically on this adapter). **Regression run** Main (`f087b99`): ``` 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 ``` This PR (`7bf4853`): ``` xcl-smoke v1 commit: 7bf48538afdea8d4bf8e56645a450b5c77b1e405 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: 14019 MB ``` **AI use** - Antigravity (Gemini 4 Neon): implementation, IR & GPU parity tests, 573-shader DXIL sweep, and PR drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:33:49 +00:00
Author
Owner

@⁠DarkPanda69 on GitHub · 2026-11-25 23:14 UTC · original

Wave64 run (listed as untested; the wave64 GPU tests skipped on the RTX 3070) on an AMD RX 9070 XT, plus an RTX 3060: the new test passes, and nothing else changes in the direct-DXIL GPU suite.

Head 7bf4853 applied on main 08db802, debug, Windows 11 Pro 26200, XCL_DIRECT_DXIL=1 cargo test -p xcl --lib -- gpu:: --test-threads=4 on main and with the PR, card chosen with XCL_ADAPTER:

main this PR
RX 9070 XT (RDNA4, 32.0.32029.2010) 704 passed, 2 failed, 6 SKIPPED 705 passed, 2 failed, 6 SKIPPED
RTX 3060 (32.0.16.1088) 706 passed, 0 failed, 24 SKIPPED 707 passed, 0 failed, 24 SKIPPED

On both cards, the only result change against main is the new bfm_and_ffbh_i_ir_and_gpu_parity (ok), with no new skips. That test translates with wave64: false, so its own GPU run is wave32. On the RX 9070 XT, the rest of the suite's wave64 cases, which skip on a 32-lane GPU, ran with this PR applied and still pass. How many of them contain bfm or ffbh_i was not checked. The 2 RX 9070 XT failures are the AMD fuzz seeds tracked in #2115, the same on main.

Not tested: Intel, a title.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

**@⁠DarkPanda69** on GitHub · 2026-11-25 23:14 UTC · [original](https://github.com/xcl-dev/xcl/pull/2559#issuecomment-7177304081) Wave64 run (listed as untested; the wave64 GPU tests skipped on the RTX 3070) on an AMD RX 9070 XT, plus an RTX 3060: the new test passes, and nothing else changes in the direct-DXIL GPU suite. Head `7bf4853` applied on `main` `08db802`, debug, Windows 11 Pro 26200, `XCL_DIRECT_DXIL=1 cargo test -p xcl --lib -- gpu:: --test-threads=4` on `main` and with the PR, card chosen with `XCL_ADAPTER`: | | `main` | this PR | |---|---|---| | RX 9070 XT (RDNA4, 32.0.32029.2010) | 704 passed, 2 failed, 6 SKIPPED | 705 passed, 2 failed, 6 SKIPPED | | RTX 3060 (32.0.16.1088) | 706 passed, 0 failed, 24 SKIPPED | 707 passed, 0 failed, 24 SKIPPED | On both cards, the only result change against `main` is the new `bfm_and_ffbh_i_ir_and_gpu_parity` (ok), with no new skips. That test translates with `wave64: false`, so its own GPU run is wave32. On the RX 9070 XT, the rest of the suite's wave64 cases, which skip on a 32-lane GPU, ran with this PR applied and still pass. How many of them contain `bfm` or `ffbh_i` was not checked. The 2 RX 9070 XT failures are the AMD fuzz seeds tracked in #2115, the same on `main`. Not tested: Intel, a title. AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 23:19 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.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-25 23:19 UTC · [original](https://github.com/xcl-dev/xcl/pull/2559#issuecomment-7177404292) 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
Author
Owner

@⁠SlopTart on GitHub · 2026-11-25 23:41 UTC · original

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 166 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR. This PR does not change the mad/mac red on RADV.
bfm_and_ffbh_i_ir_and_gpu_parity ran inside the suite (name matches the filter): passed.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

**@⁠SlopTart** on GitHub · 2026-11-25 23:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/2559#issuecomment-7177910010) **Tested**: current PR head merged onto current `origin/main` (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target `x86_64-pc-windows-gnu` run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29). `gpu_` suite (cold cache): 166 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR. This PR does not change the mad/mac red on RADV. `bfm_and_ffbh_i_ir_and_gpu_parity` ran inside the suite (name matches the filter): passed. **Not tested**: retail titles (no title data on this lane, counts only). **AI use**: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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