Recompiler: simplify bfm / s_bfm_b64 and lower ffbh_i to FirstbitSHi in direct DXIL #2559
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!2559
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2559"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 · openPart 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) computedselect(w == 0, 0, u32::MAX >> ((32 - w) & 31)) << (b & 31)(8 instructions includingicmpandselect), even thoughw = a & 31is already strictly in0..=31, so((1u32 << w) - 1) << (b & 31)is exact for everywincludingw = 0.s_bfm_b64used the same 12-instructionicmp/select/lshri64expansion even when both operands are compile-time constants, forcingi64arithmetic andFeature::Int64Opsfor constant lane masks.ffbh_i(v_ffbh_i32,s_flbit_i32) manually emulated signed find-first-bit-high withicmp slt 0, bitwise NOT,select, andFirstbitHi(4 instructions), instead of lowering directly to DXIL'sBitCount::FirstbitSHi(dx.op.unaryBits.i32op34).Fix
bfm(lower.rs) to((1 << (a & 31)) - 1) << (b & 31)with direct constant folding whena(or bothaandb) is constant.s_bfm_b64(lower.rs) directly to twoi32constants without touchingi64orFeature::Int64Ops; for dynamic operands, simplify to((1u64 << (al & 63)) - 1) << (bl & 63)with noicmp/select.ffbh_i(lower.rs) directly toself.s.bit_count(BitCount::FirstbitSHi, v).lower::bfm_ffbh_tests::bfm_and_ffbh_i_ir_and_gpu_paritychecking DXIL validation, disassembly (zeroInt64Opsfor constants_bfm_b64,FirstbitSHiop34withouticmp sltforv_ffbh_i32/s_flbit_i32, zeroselectfor dynamicbfm), and wave32 RTX 3070 GPU parity across all edge cases (0,-1,1,-2,0x7FFF_FFFF,0x8000_0000, widths0/1/16/31/32/63/64, offsets0..63).Measured impact
573-program retail direct-DXIL validation sweep (
dxil.dll1.8.2502.11):6d12f58):573/573validated (val_fail=0),51,117,488total DXIL bytes (p50=22,865 us,p95=408,132 us).7bf4853):573/573validated (val_fail=0),51,116,080total DXIL bytes (-1,408 B/-0.003%;p50=19,313 us,p95=376,258 us).Tested
cargo test --lib -p xcl bfm_ffbh): 1 passed, 0 failed..\scripts\test.ps1, NVIDIA GeForce RTX 3070, Windows 11):1424 passed, 0 failed, 46 ignored.dxil.dllvalidation sweep:573/573passed,0failures.cargo build --release -p xcl) and 60 s retail smoke run (see below).Not tested
Regression run
Main (
f087b99):This PR (
7bf4853):AI use
@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
7bf4853applied onmain08db802, debug, Windows 11 Pro 26200,XCL_DIRECT_DXIL=1 cargo test -p xcl --lib -- gpu:: --test-threads=4onmainand with the PR, card chosen withXCL_ADAPTER:mainOn both cards, the only result change against
mainis the newbfm_and_ffbh_i_ir_and_gpu_parity(ok), with no new skips. That test translates withwave64: 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 containbfmorffbh_iwas not checked. The 2 RX 9070 XT failures are the AMD fuzz seeds tracked in #2115, the same onmain.Not tested: Intel, a title.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.
@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
@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 targetx86_64-pc-windows-gnurun 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_parityran 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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.