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
sharan104 wants to merge 1 commit from upstream-pr/286 into main
sharan104 commented 2026-11-26 14:27:06 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by sharan104 on 2026-11-26T14:27:06Z.


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

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:issue:6628762793 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/286). Originally posted by [sharan104](https://github.com/sharan104) on 2026-11-26T14:27:06Z. --- `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)
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/286:upstream-pr/286'
git switch 'upstream-pr/286'

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/286'
git switch 'upstream-pr/286'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/286'
git switch 'upstream-pr/286'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/286'
git switch 'main'
git merge --squash 'upstream-pr/286'
git switch 'main'
git merge --ff-only 'upstream-pr/286'
git switch 'main'
git merge 'upstream-pr/286'
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
avatarmanz/VIctory-7900XTX!286
No description provided.