Wave64 own bits: refuse a scalar-joined bit loop whose set is read after the loop (host waves read their half's last bit) #286

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

@⁠sharan104 on GitHub · 2026-11-26 14:27 UTC · original
sharan104:fix/opt-audit → main · +36 −1 · 1 files · 1 commits · open

own_bits lets a wave64 program running as two host waves of 32 use each host wave's own half of a wave-wide OR in a "visit each set bit" loop. That's only safe when the set of bits still to visit isn't read outside the loop. Ctx::site checked this, but only for the form where the set is kept in a VGPR and read back with a first-lane read. In the form where the set is joined in an SGPR itself (cur == r), the check if cur != r && !self.joins(cur).is_empty() skipped the joins that read it.

That form leaves the loop when one bit is left, so the SGPR holds the OR's highest bit after the loop. Carried out through the exit's join and read there, it was taken as the lane's own. So each host wave of 32 read the highest bit of its own half instead of the wave64's: a miscompile.

Fix: the joins of the set are checked in both forms: if !self.joins(cur).is_empty().

Test: the_last_bits_left_read_after_a_scalar_loop_are_not_the_lanes_own builds the scalar form of the light loop, with s_mov_b32 s8, s7 … again: s_mov_b32 s8, s9. It asserts the loop is still found as before, and that it's refused once s8 is read after the loop (v_mov_b32 v6, s8). The test fails before the fix and passed 10 runs out of 10 after it. The existing own_bits tests, VGPR form included, are unchanged and pass.

Results:

  • cargo test -p victory-shader --locked: 303 passed, 0 failed, 4 ignored.
  • Clippy -D warnings is clean, with and without --all-features.

This doesn't overlap #269, which also touches the optimizer (opt.rs, FxHash tables).

🤖 Generated with Claude Code

**@⁠sharan104** on GitHub · 2026-11-26 14:27 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/286) `sharan104:fix/opt-audit` → `main` · +36 −1 · 1 files · 1 commits · open `own_bits` lets a wave64 program running as two host waves of 32 use each host wave's own half of a wave-wide OR in a "visit each set bit" loop. That's only safe when the set of bits still to visit isn't read outside the loop. `Ctx::site` checked this, but only for the form where the set is kept in a VGPR and read back with a first-lane read. In the form where the set is joined in an SGPR itself (`cur == r`), the check `if cur != r && !self.joins(cur).is_empty()` skipped the joins that read it. That form leaves the loop when one bit is left, so the SGPR holds the OR's **highest bit** after the loop. Carried out through the exit's join and read there, it was taken as the lane's own. So each host wave of 32 read the highest bit of its own half instead of the wave64's: a miscompile. **Fix:** the joins of the set are checked in both forms: `if !self.joins(cur).is_empty()`. **Test:** `the_last_bits_left_read_after_a_scalar_loop_are_not_the_lanes_own` builds the scalar form of the light loop, with `s_mov_b32 s8, s7` … `again: s_mov_b32 s8, s9`. It asserts the loop is still found as before, and that it's refused once `s8` is read after the loop (`v_mov_b32 v6, s8`). The test fails before the fix and passed 10 runs out of 10 after it. The existing `own_bits` tests, VGPR form included, are unchanged and pass. **Results:** - `cargo test -p victory-shader --locked`: 303 passed, 0 failed, 4 ignored. - Clippy `-D warnings` is clean, with and without `--all-features`. This doesn't overlap #269, which also touches the optimizer (`opt.rs`, FxHash tables). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
own_bits::site checked that the joined set of bits still to visit is
read by nothing outside the loop's own steps, but for a set joined in
an SGPR (no VGPR copy and first-lane read) it skipped the joins that
read it: `cur != r` held only for the VGPR form. Such a loop leaves
when one bit is left, so the register holds the wave OR's highest bit
after the loop; carried out through the exit's join and read there, it
was taken as the lane's own and a host wave of 32 read its own half's
highest bit instead of the wave64's.

The joins of the set are now checked in both forms. Test: the scalar
form of the light loop is still found, and refused once s8 is read
after the loop (fails before the fix, passes after).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:28:30 +00:00
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/286/head:github-bot/gh-286'
git switch 'github-bot/gh-286'

Merge

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