Skip to content

Recompiler: avoid AMD Bfrev.i32 miscompile - #3225

Draft
JulienReppert wants to merge 1 commit into
xcl-dev:mainfrom
JulienReppert:fix/amd-bfrev-i32
Draft

JulienReppert wants to merge 1 commit into
xcl-dev:mainfrom
JulienReppert:fix/amd-bfrev-i32

Conversation

@JulienReppert

@JulienReppert JulienReppert commented Nov 28, 2026 •

Copy link
Copy Markdown

AMD's RDNA4 driver can compute Bfrev.i32(x) as x | bfrev(x) for some values (direct-DXIL fuzz seed 1599). Route i32 bit reversal through the shift-and-mask sequence already used for the halves of Bfrev.i64. The emitted DXIL still validates and matches DXC's intrinsic attributes.

What is in it

  • crates/xcl/src/gpu/dxil/ops.rs: use bfrev32_shifts for i32 values instead of the Bfrev DXIL op.

Tested

  • AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010; Intel Core i5-14600KF; Windows 11 build 26100; Rust 1.98.1 (x86_64-pc-windows-msvc).
  • cargo build --workspace: passed.
  • cargo test --workspace -- --test-threads=4: 2453 passed, 2 failed, 29 ignored, 0 measured. SKIPPED GPU tests: 8.
  • Clean upstream 7db2f391 on the same host: 2452 passed, 3 failed, 29 ignored, 0 measured. This change fixes fuzz_validate_and_gpu_3; fuzz_validate_and_gpu_1 and gpu_ps_readfirstlane_loop_on_a_primitive_edge_ends still fail on clean upstream.
  • cargo test -p xcl --lib fuzz_validate_and_gpu -- --nocapture --test-threads=1: 3 passed, 1 failed; the remaining failure is seed 722 (WaveReadLaneFirst after a divergent switch), not this code path.
  • The ignored AMD reduction now passes both countbits/Bfrev variants and full seed 1599.

Regression run

xcl-smoke v1
commit: 7db2f391cefd748d47c7de63d50dd43ebb852185
gpu: AMD Radeon RX 9070 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1633 (PresentX 1633)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 31266 MB
xcl-smoke v1
commit: 29a42c58a082c7112f5a523e56eb0b6a9eb09b25
gpu: AMD Radeon RX 9070 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1661 (PresentX 1661)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 26872 MB

Not tested

  • NVIDIA and RDNA2 adapters.
  • A title shader that actually executes this exact Bfrev.i32 lowering; the evidence here is the direct-DXIL GPU fuzz test.

AI use

  • Pi coding agent with tencent/hy4-flash wrote the fix and this description.

AMD's RDNA3/4 driver can compute Bfrev.i32 as x | bfrev(x) for some computed values (fuzz seed 1599). Use the group-swap sequence already used for the i64 halves. This makes both reduced countbits/Bfrev variants and the full seed 1599 pass on RX 9070 XT. Seed 722 remains a separate divergent-switch/WaveReadLaneFirst issue. Commit identity is local to this worktree because no identity was configured.
@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Nov 28, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 29a42c5, diff read, not a merge-check; draft):

  • Every adapter pays for an AMD workaround. bfrev (gpu/dxil/ops.rs ~333) now lowers every i32 bit reversal to the shift-and-mask sequence: about 17 ALU ops instead of one Bfrev, on NVIDIA and Intel too, where Bfrev.i32 is correct. It's probably cheap where it's used (lower.rs ~748), but nothing measures it. If the direct translator ever gets a vendor in its cache identity, gating this on AMD would keep the single op elsewhere; otherwise say in the comment that it's deliberate.
  • No test that would catch a return of the bug. The diff has no test; the evidence is the existing fuzz_validate_and_gpu_3 passing on an RDNA4 card, which needs AMD hardware. A DXIL-level test that no i32 Bfrev (op 30) is emitted, beside the existing fuzz reductions in fuzz_tests.rs (~1316-1353), would run on every machine.
  • i16 still takes Bfrev directly. Whether AMD's driver has the same issue there is untested (unverified).

Fine: the i64 path already used bfrev32_shifts for its halves for the same driver bug (fuzz seed 817), so this reuses a known-exact sequence. ops.rs is under src/gpu/dxil, which the direct-DXIL cache identity hashes, so no stale DXIL is served. The PR's smoke pair on the 9070 XT is flat.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 29a42c5

#2231 targets the same AMD miscompile (fuzz seed 1599) by constant-folding Countbits of constants in bit_count. This PR instead routes every Bfrev.i32 through the existing bfrev32_shifts sequence in ops::bfrev, which also covers non-constant inputs. These are different approaches, not duplicates.
The change is exact on every adapter (the same sequence already reverses the halves of Bfrev.i64). No new test; it relies on the existing fuzz_validate_and_gpu seed 1599 and the AMD reduction in fuzz_tests.rs.
Recommendation: keep open.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants