Direct DXIL: fold Countbits of constants (AMD folds Bfrev(Countbits(K)) wrong) #2231

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

@⁠Diskuss on GitHub · 2026-11-25 10:49 UTC · original
Diskuss:fuzz-1599-rdna4 → main · +45 −3 · 3 files · 1 commits · draft

On the RX 9070 XT (RDNA4), the AMD driver (amdxc64) folds Bfrev(Countbits(K)) wrong when K is a constant. It gives bfrev(n) | n (n = countbits(K)): for K = 2 that is 0x80000001 instead of 0x80000000. This is the cause of fuzz seed 1599 (62 mismatches) and of fuzz_validate_and_gpu_3 failing on this card. Shader::bit_count now folds Countbits of a constant itself, so the driver never sees that pattern. The reduced HLSL repro and the analysis are in #2115.

Part of #2115

What is in it

  • crates/xcl/src/gpu/dxil/module.rs: Module::int_const, which reads the value of an integer constant.
  • crates/xcl/src/gpu/dxil/ops.rs: Shader::bit_count folds Countbits of a constant, masked to the operand's width.
  • crates/xcl/src/gpu/dxil/ops_tests.rs: new test bfrev_of_countbits_of_a_constant_matches_cpu, which compares the GPU result with the CPU.

Tested

  • AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32015.7029; Ryzen 7 5700X3D; Windows 11 Pro 26300.9974; Rust 1.100.0 msvc; DXC 1.9.2602.17; debug build for the tests, release build for the smoke runs.
  • cargo test -p xcl --lib -- --test-threads=4:
    • this PR (64eef49, on main 2898511): 1284 passed, 1 failed, 26 ignored.
    • main 2898511 on the same machine: 1282 passed, 2 failed, 26 ignored.
    • The difference: fuzz_validate_and_gpu_3 now passes, and the new test is counted.
    • The 1 failure also fails on main and is not touched here: fuzz_validate_and_gpu_1, seed 722, res[30] = 0x1, CPU 0x7fffffff. #2192 covers it.
  • bfrev_of_countbits_of_a_constant_matches_cpu and fuzz_validate_and_gpu_3 alone (--test-threads=1): both pass.
  • Fuzz seed 1599: 0 mismatches (was 62).
  • SKIPPED tests:
    • pso_measurement_compiles_an_exact_synthetic_record (Developer Mode is off).
    • 6 f16_programs_on_the_gpu/f32_programs_on_the_gpu per-lane cases (refused ops).
  • No device removal.

Regression run

main:

xcl-smoke v1
commit: 4d441866a42a0bca052044299f94ff1471a6523b
gpu: AMD Radeon RX 9070 XT
title: d5605821
seconds: 60
exit: 0xc0000005
frames shown: none logged (the run did not reach its end)
presents per 2 s (last 3): 0 0
device removals: 0
crashes: 2
peak commit: 866 MB

This PR:

xcl-smoke v1
commit: 64eef49e57698ef428941c75ab3b80ac817c8837
gpu: AMD Radeon RX 9070 XT
title: d5605821
seconds: 60
exit: 0xc0000005
frames shown: none logged (the run did not reach its end)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 2
peak commit: 866 MB

The title is The Matrix Awakens. On main (4d44186) and on this PR it dies the same way, about 1.5 to 3.5 s in: the title's CreateCommandSignature (DispatchIndirectMesh) fails, then it reads a null pointer (0xc0000005). That is fixed only in #1887. So this run shows no new crash and no device removal, but it never reaches a shader that this change affects.

Not tested

  • The other workspace crates after the rebase onto 2898511 (only -p xcl --lib was rerun; the change is in that crate).
  • A retail title that reaches the changed code. The smoke title crashes before that (see above).
  • Constants that the driver derives itself are not folded here, e.g. Countbits(zext((in & 0) | 2)). The translator does not see those as constants.
  • Other AMD generations, newer drivers, and NVIDIA/Intel. The fold gives the same value on any GPU.

AI use

  • Claude Code (Claude Opus 5.5): code, tests and description; reviewed by Diskuss.

🤖 Generated with Claude Code

**@⁠Diskuss** on GitHub · 2026-11-25 10:49 UTC · [original](https://github.com/xcl-dev/xcl/pull/2231) `Diskuss:fuzz-1599-rdna4` → `main` · +45 −3 · 3 files · 1 commits · draft On the RX 9070 XT (RDNA4), the AMD driver (amdxc64) folds `Bfrev(Countbits(K))` wrong when `K` is a constant. It gives `bfrev(n) | n` (n = countbits(K)): for K = 2 that is 0x80000001 instead of 0x80000000. This is the cause of fuzz seed 1599 (62 mismatches) and of `fuzz_validate_and_gpu_3` failing on this card. `Shader::bit_count` now folds `Countbits` of a constant itself, so the driver never sees that pattern. The reduced HLSL repro and the analysis are in https://git.gtahub.st/github/xcl-dev--xcl/issues/2115. Part of #2115 **What is in it** - `crates/xcl/src/gpu/dxil/module.rs`: `Module::int_const`, which reads the value of an integer constant. - `crates/xcl/src/gpu/dxil/ops.rs`: `Shader::bit_count` folds `Countbits` of a constant, masked to the operand's width. - `crates/xcl/src/gpu/dxil/ops_tests.rs`: new test `bfrev_of_countbits_of_a_constant_matches_cpu`, which compares the GPU result with the CPU. **Tested** - AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32015.7029; Ryzen 7 5700X3D; Windows 11 Pro 26300.9974; Rust 1.100.0 msvc; DXC 1.9.2602.17; debug build for the tests, release build for the smoke runs. - `cargo test -p xcl --lib -- --test-threads=4`: - this PR (`64eef49`, on `main` `2898511`): **1284 passed, 1 failed, 26 ignored**. - `main` `2898511` on the same machine: 1282 passed, 2 failed, 26 ignored. - The difference: `fuzz_validate_and_gpu_3` now passes, and the new test is counted. - The 1 failure also fails on `main` and is not touched here: `fuzz_validate_and_gpu_1`, seed 722, `res[30] = 0x1, CPU 0x7fffffff`. #2192 covers it. - `bfrev_of_countbits_of_a_constant_matches_cpu` and `fuzz_validate_and_gpu_3` alone (`--test-threads=1`): both pass. - Fuzz seed 1599: 0 mismatches (was 62). - SKIPPED tests: - `pso_measurement_compiles_an_exact_synthetic_record` (Developer Mode is off). - 6 `f16_programs_on_the_gpu`/`f32_programs_on_the_gpu` per-lane cases (refused ops). - No device removal. **Regression run** `main`: ``` xcl-smoke v1 commit: 4d441866a42a0bca052044299f94ff1471a6523b gpu: AMD Radeon RX 9070 XT title: d5605821 seconds: 60 exit: 0xc0000005 frames shown: none logged (the run did not reach its end) presents per 2 s (last 3): 0 0 device removals: 0 crashes: 2 peak commit: 866 MB ``` This PR: ``` xcl-smoke v1 commit: 64eef49e57698ef428941c75ab3b80ac817c8837 gpu: AMD Radeon RX 9070 XT title: d5605821 seconds: 60 exit: 0xc0000005 frames shown: none logged (the run did not reach its end) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 2 peak commit: 866 MB ``` The title is The Matrix Awakens. On `main` (`4d44186`) and on this PR it dies the same way, about 1.5 to 3.5 s in: the title's `CreateCommandSignature` (DispatchIndirectMesh) fails, then it reads a null pointer (0xc0000005). That is fixed only in #1887. So this run shows no new crash and no device removal, but it never reaches a shader that this change affects. **Not tested** - The other workspace crates after the rebase onto `2898511` (only `-p xcl --lib` was rerun; the change is in that crate). - A retail title that reaches the changed code. The smoke title crashes before that (see above). - Constants that the driver derives itself are not folded here, e.g. `Countbits(zext((in & 0) | 2))`. The translator does not see those as constants. - Other AMD generations, newer drivers, and NVIDIA/Intel. The fold gives the same value on any GPU. **AI use** - Claude Code (Claude Opus 5.5): code, tests and description; reviewed by Diskuss. ≡ƒñû Generated with [Claude Code](https://claude.com/claude-code)
amdxc64 on RDNA4 folds Bfrev(Countbits(K)) for a constant K to
Bfrev(n) | n (0x80000001 for K = 2). Fold Countbits of constants in
bit_count so the driver never sees that pattern. Fixes fuzz seed 1599
on the RX 9070 XT (#2115).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:23:58 +00:00
Author
Owner

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

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

  • cargo test -p xcl --lib: 1 real failure, deterministic (also fails --test-threads=1):
hle::d3d12x::device_x::tests::pc_resource_desc_strips_uav_on_unsupported_formats_and_msaa
  assertion failed: format 85 kept — left: 28, right: 85 (device_x.rs:1376)

Same failure as #2229 (likely the same feature branch area). Needs a fix before merge.

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-25 16:31 UTC · [original](https://github.com/xcl-dev/xcl/pull/2231#issuecomment-7168115941) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: **1 real failure**, deterministic (also fails `--test-threads=1`): ``` hle::d3d12x::device_x::tests::pc_resource_desc_strips_uav_on_unsupported_formats_and_msaa assertion failed: format 85 kept — left: 28, right: 85 (device_x.rs:1376) ``` Same failure as #2229 (likely the same feature branch area). Needs a fix before merge. — Devin
Author
Owner

@⁠CharNoir on GitHub · 2026-11-25 22:32 UTC · original

Test report for 64eef49 ("Direct DXIL: fold Countbits of constants").

Tested

  • Hardware: AMD Radeon RX 9070 XT (RDNA4, 16 GB), Adrenalin 26.11.2; AMD Ryzen 5 7600; Windows 11 Pro 25H2 (build 26200.9974); Rust <rustc --version>; debug (test profile); DXC 1.8.2502.11 (Windows SDK 10.0.26100.0)
  • cargo test -p xcl --lib fuzz_validate_and_gpu, on main and on this PR:
Commit _0 _1 _2 _3
main e1d84b7 ok FAIL (seed 722) ok FAIL (seed 1599: res[193] = 0x1, CPU 0x0)
PR 64eef49 ok FAIL (seed 722, unchanged) ok ok
  • Seed 1599 is fixed on RDNA4. The failing program computes Bfrev(Countbits(2)), which should be 0x80000000; the GPU result on main is consistent with the driver misfolding Bfrev(Countbits(K)). The remaining seed 722 failure (WaveReadLaneFirst before a divergent switch) is unrelated to this PR; #2192 targets it.
  • For reference, the full suite on main e1d84b7: cargo test --workspace -- --test-threads=4: 1421 passed, 2 failed (the two fuzz tests above), 46 ignored. SKIPPED GPU tests: 6 f16/f32 per-lane refusals (#1426) and the heap_window test ("no heap windows").

Not tested

  • The full suite on this PR's commit: only the fuzz_validate_and_gpu tests were run on it.
  • scripts/smoke.ps1 regression run on a real title.
  • Non-AMD GPUs.

AI use

  • Claude (Claude Sonnet 5.5) assembled this report from the logs of the runs above. I ran the commands on the hardware named.
**@⁠CharNoir** on GitHub · 2026-11-25 22:32 UTC · [original](https://github.com/xcl-dev/xcl/pull/2231#issuecomment-7176500939) Test report for 64eef49 ("Direct DXIL: fold Countbits of constants"). **Tested** - Hardware: AMD Radeon RX 9070 XT (RDNA4, 16 GB), Adrenalin 26.11.2; AMD Ryzen 5 7600; Windows 11 Pro 25H2 (build 26200.9974); Rust `<rustc --version>`; debug (`test` profile); DXC 1.8.2502.11 (Windows SDK 10.0.26100.0) - `cargo test -p xcl --lib fuzz_validate_and_gpu`, on main and on this PR: | Commit | `_0` | `_1` | `_2` | `_3` | |---|---|---|---|---| | main e1d84b7 | ok | FAIL (seed 722) | ok | **FAIL** (seed 1599: `res[193] = 0x1`, CPU `0x0`) | | PR 64eef49 | ok | FAIL (seed 722, unchanged) | ok | **ok** | - Seed 1599 is fixed on RDNA4. The failing program computes `Bfrev(Countbits(2))`, which should be `0x80000000`; the GPU result on main is consistent with the driver misfolding `Bfrev(Countbits(K))`. The remaining seed 722 failure (WaveReadLaneFirst before a divergent switch) is unrelated to this PR; #2192 targets it. - For reference, the full suite on main e1d84b7: `cargo test --workspace -- --test-threads=4`: 1421 passed, 2 failed (the two fuzz tests above), 46 ignored. SKIPPED GPU tests: 6 f16/f32 per-lane refusals (#1426) and the heap_window test ("no heap windows"). **Not tested** - The full suite on this PR's commit: only the `fuzz_validate_and_gpu` tests were run on it. - `scripts/smoke.ps1` regression run on a real title. - Non-AMD GPUs. **AI use** - Claude (Claude Sonnet 5.5) assembled this report from the logs of the runs above. I ran the commands on the hardware named.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 00:59 UTC · original

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-26 00:59 UTC · [original](https://github.com/xcl-dev/xcl/pull/2231#issuecomment-7179314687) Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
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/2231/head:github-bot/gh-2231'
git switch 'github-bot/gh-2231'

Merge

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