Recompiler: avoid AMD Bfrev.i32 miscompile #3225

Open
JulienReppert wants to merge 1 commit from upstream-pr/3225 into main
JulienReppert commented 2026-11-28 22:51:44 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by JulienReppert on 2026-11-28T22:51:44Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:5545365183 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3225). Originally posted by [JulienReppert](https://github.com/JulienReppert) on 2026-11-28T22:51:44Z. --- 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.
Foniass commented 2026-12-01 09:23:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T09:23:40Z.


Review (head 29a42c58, 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.

<!-- greyfinch-sync:ivall/xcl:comment:7306636700 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3225#issuecomment-7306636700). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T09:23:40Z. --- Review (head 29a42c58, 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 commented 2026-12-01 09:45:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T09:45:25Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7307159371 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3225#issuecomment-7307159371). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T09:45:25Z. --- **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.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3225:upstream-pr/3225'
git switch 'upstream-pr/3225'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3225'
git switch 'upstream-pr/3225'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3225'
git switch 'upstream-pr/3225'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3225'
git switch 'main'
git merge --squash 'upstream-pr/3225'
git switch 'main'
git merge --ff-only 'upstream-pr/3225'
git switch 'main'
git merge 'upstream-pr/3225'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3225
No description provided.