Direct DXIL: fold Countbits of constants (AMD folds Bfrev(Countbits(K)) wrong) #2221
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2231"
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?
Imported from ivall/xcl. Originally posted by Diskuss on 2026-11-25T10:49:05Z.
On the RX 9070 XT (RDNA4), the AMD driver (amdxc64) folds
Bfrev(Countbits(K))wrong whenKis a constant. It givesbfrev(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 offuzz_validate_and_gpu_3failing on this card.Shader::bit_countnow foldsCountbitsof a constant itself, so the driver never sees that pattern. The reduced HLSL repro and the analysis are in https://github.com/xcl-dev/xcl/issues/2115#issuecomment-7156460558.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_countfoldsCountbitsof a constant, masked to the operand's width.crates/xcl/src/gpu/dxil/ops_tests.rs: new testbfrev_of_countbits_of_a_constant_matches_cpu, which compares the GPU result with the CPU.Tested
cargo test -p xcl --lib -- --test-threads=4:64eef49, onmain2898511): 1284 passed, 1 failed, 26 ignored.main2898511on the same machine: 1282 passed, 2 failed, 26 ignored.fuzz_validate_and_gpu_3now passes, and the new test is counted.mainand 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_cpuandfuzz_validate_and_gpu_3alone (--test-threads=1): both pass.pso_measurement_compiles_an_exact_synthetic_record(Developer Mode is off).f16_programs_on_the_gpu/f32_programs_on_the_gpuper-lane cases (refused ops).Regression run
main:This PR:
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'sCreateCommandSignature(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
2898511(only-p xcl --libwas rerun; the change is in that crate).Countbits(zext((in & 0) | 2)). The translator does not see those as constants.AI use
🤖 Generated with Claude Code
upstream-sync referenced this pull request2026-11-25 14:08:43 +00:00
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T16:31:25Z.
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):Same failure as #2229 (likely the same feature branch area). Needs a fix before merge.
— Devin
Imported from ivall/xcl. Originally posted by CharNoir on 2026-11-25T22:32:59Z.
Test report for
64eef49("Direct DXIL: fold Countbits of constants").Tested
<rustc --version>; debug (testprofile); 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:_0_1_2_3e1d84b7res[193] = 0x1, CPU0x0)64eef49Bfrev(Countbits(2)), which should be0x80000000; the GPU result on main is consistent with the driver misfoldingBfrev(Countbits(K)). The remaining seed 722 failure (WaveReadLaneFirst before a divergent switch) is unrelated to this PR; #2192 targets it.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
fuzz_validate_and_gputests were run on it.scripts/smoke.ps1regression run on a real title.AI use
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T00:59:26Z.
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
Imported from ivall/xcl. Originally posted by upipibi on 2026-11-26T16:25:22Z.
@Diskuss a sibling of this fold, for NVIDIA: on an RTX 3070 (driver 616.56) a FirstbitLo / FirstbitHi of a constant ANDed with a product of odd values reads bit 0 as 0 on the direct path (found by fuzzing
main).Countbitsof the same constant comes out right there, so this PR's fold doesn't cover it, but the fix belongs in the sameShader::bit_count: foldFirstbitLo/FirstbitHiof a 32-bit constant withModule::const_u64, next to yourCountbitsarm.A change on top of
main8b957becwith a GPU test is on a branch you are welcome to take into this PR:github.com/upipibi/xcl@b6406763(ops.rs+13,core_tests.rstestgpu_direct_bit_scan_of_a_constant_and_an_odd_productovers_ff1_b32,s_ff1_b64,s_flbit,v_ffbl,v_ffbh).RTX 3070 8 GB, driver 616.56, Windows 11, debug: the test fails 3/3 on
main(every case 0 instead of 1) and passes 3/3 with the change;cargo test -p xcl --lib -- --skip hle::xaudio2 --test-threads=4with it: 1311 passed, 0 failed, 27 ignored. Not tested: AMD/Intel, a title run.AI-assisted (Claude Code, Claude Opus 5.5): found it by fuzzing, wrote the change and test, ran them, wrote this.
Imported from ivall/xcl. Originally posted by Granite900 on 2026-11-26T16:26:05Z.
Tested
64eef49e(this PR's head) againstmainon an RX 9070 XT.Tested
cargo test -p xcl --lib gpu::dxil::fuzz_testsmain:fuzz_validate_and_gpu_1(seed 722) andfuzz_validate_and_gpu_3(seed 1599) both fail, reproducibly. Seed 1599:res[193] = 0x1, CPU 0x064eef49e: 4 passed, 1 failed, 2 ignored. Seed 1599 is fixed (fuzz_validate_and_gpu_3passes).Still failing: seed 722, unchanged:
res[30] = 0x1, CPU 0x7fffffff. With this PR the Countbits in the227944867case is folded away (that block is empty in the disassembly), and it still fails. So seed 722 doesn't look like the Bfrev(Countbits(K)) pattern, and #2115 stays open. The remaining suspect is the switch/phi path that carries theWaveReadLaneFirstresult (%10) throughcase 1and the default.Not tested
main: 1315 passed, 3 failed: the two fuzz seeds, pluspso_measurement_compiles_an_exact_synthetic_record, which needs Developer Mode)AI use: report drafted with Claude (Claude Opus 5.5) from my test runs.
Imported from ivall/xcl. Originally posted by Luislariosss on 2026-11-26T21:57:22Z.
Tested
64eef49with #2192 and #1946 merged on top ofmain8b957be, on an RX 9060 XT 16 GB (RDNA4), driver32.0.32029.2010:cargo test -p xcl --lib: 1320 passed, 0 failed, 27 ignored (onmainalone here: 1316 passed, 2 failed).fuzz_validate_and_gpu_1(seed 722) and_3(seed 1599) pass with the stack.gpu::dxil::fuzz_tests::amd_reduced_seeds -- --ignored: 6 of 7 variants pass.The one that still fails, deterministically (two runs), is the dynamic form of the shape this PR folds:
PASS full seed 1599: 0 mismatching dwordswith the stack, so the seed itself is covered. The failing variant differs only in building the operand at runtime (in[lane] & 0 | 2, opaque to the driver) instead ofC(T::I64, 2). Posting it here in case it belongs in this PR or in a follow-up. Details and the full run in #2115.Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:35:54Z.
Nothing has happened on this PR for 3 days, so it is marked
staleand will be closed in 4 days. A push or a comment takes the mark off: rebase it ontomainif it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T07:38:25Z.
Triage (#1607) – head
64eef49#3225 targets the same AMD fault (fuzz seed 1599, #2115) another way. It replaces every
Bfrev.i32with the shift-and-mask sequence (bfrev32_shifts). This PR foldsCountbitsof constants inShader::bit_count, so it only covers a constant operand. The dynamic variant infuzz_tests.rs(1599 Countbits.i64(dynamic 2) -> shift / Bfrev) is not covered by this fold.The fold itself looks right:
int_constreturns the sign-extended value, which is masked to the operand width beforecount_ones. #2124 (same issue) is only test tooling.Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.