xshader: read whole lane masks where a program counts one #197

Open
github-bot wants to merge 4 commits from github-bot/gh-197 into main AGit
Owner

@⁠Jerkin42 on GitHub · 2026-11-25 21:03 UTC · original
Jerkin42:fix/mask-ballots → main · +868 −21 · 17 files · 4 commits · open

Summary

A translated thread holds only its own bit of a lane mask, in the register's bit 0. Instructions that use a mask as a number see that single bit: s_bcnt1 and v_mbcnt gave 0 or 1 for a whole wave, and s_ff1 always gave lane 0.

Compute programs use these to hand out list slots, so every thread got one of the same few slots. In ProjectScarlett this hits the shadow mask tiles: lit surfaces and people showed 16-pixel blocks. With the first version of this change their shadows were smooth in the game. The shadow tile passes are among the programs the current version changes, but I have not rerun the game with it.

The masks such instructions read are found by their reaching definitions. The wave's ballot is taken right after each definition and the counting instruction reads that. It is only taken where every lane that can be in the mask runs together: not in code that a branch on another mask skips or repeats, and not after some lanes have left the program. Both words of a wave64 mask have to come from the same definitions. A register that holds anything else on some path keeps the old translation, and so does a group that is not made of whole waves.

The program then runs in waves of the guest's width: WaveSize(32) or WaveSize(64). A wave64 program therefore needs an adapter with wave64. The alternative, reading half a wave on a 32-lane host, gives different counts. #201's portable Wave64 lowering could lift this later. Ray tracing resolve programs keep whole masks already and are left alone. Shader Model 6.5 is used where a ballot is taken.

Over the 24,463 ProjectScarlett programs I have, 76 change and none is newly refused. 33 of them, among them the shadow tile passes, now require wave64.

Verified on Lanes

  • python tuxbox.py check passed on this branch
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below

GPU, driver and backend: RX 9060 XT, RADV (Mesa 26.1.8), Proton Experimental

passed   Build the shader compiler
passed   Python tests
passed   Shader compiler tests
passed   DXBC tests
passed   XG layout tests
passed   D3D12 Windows typecheck
passed   Build
FAILED   Lanes and fixtures on the GPU   guest-present (5 cases), invalid-route
FAILED   D3D12 regression controls       buffer-remap
passed   Shader translator on the GPU    with the new mask-ballots check

The same seven cases fail on plain main (7b22d71) on this machine, where I run the check in a private Xwayland inside a container. The branch only touches the translator. The full check above ran on the second commit.

Not a Lanes scenario because it is about single instructions. A new GPU check, mask-ballots, assembles 300 small programs, runs them and compares every word with a reference: 300 of 300 pass. On plain main 200 of the 300 give wrong words. The unit tests cover each review point, and each of the six new ones fails on the first commit.

Checks

  • shader compiler tests: 322 passed
  • tuxbox-dxbc: 61, tuxbox-xg-layout: 14, Python: 348
  • cargo check of tuxbox-d3d12 for x86_64-pc-windows-msvc
  • docs/SHADERS.md has a paragraph on it

🤖 Generated with Claude Code

**@⁠Jerkin42** on GitHub · 2026-11-25 21:03 UTC · [original](https://github.com/brcly/TuxBox/pull/197) `Jerkin42:fix/mask-ballots` → `main` · +868 −21 · 17 files · 4 commits · open ## Summary A translated thread holds only its own bit of a lane mask, in the register's bit 0. Instructions that use a mask as a number see that single bit: `s_bcnt1` and `v_mbcnt` gave 0 or 1 for a whole wave, and `s_ff1` always gave lane 0. Compute programs use these to hand out list slots, so every thread got one of the same few slots. In ProjectScarlett this hits the shadow mask tiles: lit surfaces and people showed 16-pixel blocks. With the first version of this change their shadows were smooth in the game. The shadow tile passes are among the programs the current version changes, but I have not rerun the game with it. The masks such instructions read are found by their reaching definitions. The wave's ballot is taken right after each definition and the counting instruction reads that. It is only taken where every lane that can be in the mask runs together: not in code that a branch on another mask skips or repeats, and not after some lanes have left the program. Both words of a wave64 mask have to come from the same definitions. A register that holds anything else on some path keeps the old translation, and so does a group that is not made of whole waves. The program then runs in waves of the guest's width: `WaveSize(32)` or `WaveSize(64)`. A wave64 program therefore needs an adapter with wave64. The alternative, reading half a wave on a 32-lane host, gives different counts. #201's portable Wave64 lowering could lift this later. Ray tracing resolve programs keep whole masks already and are left alone. Shader Model 6.5 is used where a ballot is taken. Over the 24,463 ProjectScarlett programs I have, 76 change and none is newly refused. 33 of them, among them the shadow tile passes, now require wave64. ## Verified on Lanes - [ ] `python tuxbox.py check` passed on this branch - [x] Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below GPU, driver and backend: RX 9060 XT, RADV (Mesa 26.1.8), Proton Experimental ```text passed Build the shader compiler passed Python tests passed Shader compiler tests passed DXBC tests passed XG layout tests passed D3D12 Windows typecheck passed Build FAILED Lanes and fixtures on the GPU guest-present (5 cases), invalid-route FAILED D3D12 regression controls buffer-remap passed Shader translator on the GPU with the new mask-ballots check ``` The same seven cases fail on plain main (7b22d71) on this machine, where I run the check in a private Xwayland inside a container. The branch only touches the translator. The full check above ran on the second commit. Not a Lanes scenario because it is about single instructions. A new GPU check, `mask-ballots`, assembles 300 small programs, runs them and compares every word with a reference: 300 of 300 pass. On plain main 200 of the 300 give wrong words. The unit tests cover each review point, and each of the six new ones fails on the first commit. ## Checks - shader compiler tests: 322 passed - tuxbox-dxbc: 61, tuxbox-xg-layout: 14, Python: 348 - `cargo check` of tuxbox-d3d12 for x86_64-pc-windows-msvc - `docs/SHADERS.md` has a paragraph on it 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A translated thread holds only its own bit of a lane mask, so counting a
mask's lanes (s_bcnt1), finding its first one (s_ff1) or counting those
before a lane (v_mbcnt) gave 0 or 1 for a whole wave. Compute programs that
hand out list slots this way gave the same few slots to every thread: in
ProjectScarlett the shadow mask tiles of lit surfaces, which showed as
16-pixel blocks.

Find the masks such instructions read by their reaching definitions, take
the wave's ballot right after each one, while every lane that can be in it
is there, and read that. A register that holds anything else on some path
keeps the old translation. One-word masks need 32-lane waves. Two words hold
a wave of any size, so wave64 programs gain no requirement and still run on
adapters that only have wave32.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review fixes for reading lane masks as numbers:

- Ray tracing resolve programs keep whole masks already. A ballot of one
  lane's bit broke their counts in about 1,000 programs.
- Both words of a wave64 mask must come from the same definitions, and a
  64-bit count must start at the first word.
- v_readfirstlane, v_readlane and the scalar carry of VOP3B instructions
  count as writes, and a VOP2 carry writes VCC.
- Masks are moved and combined only at the wave's width, and a program
  that uses both widths of EXEC, or the second word in a 32-lane wave,
  gets no ballots. S_WQM is not followed.
- A ballot is taken only where the lanes that can be in the mask run
  together: not where a branch on another mask skips or repeats, and not
  after an early exit. A branch on EXEC keeps the running lanes together.
- The width is applied before the complete-wave check. A group that is not
  made of whole waves keeps the old translation, and a wave64 program needs
  wave64 instead of reading half a wave on a 32-lane host.

Over the 24,463 ProjectScarlett programs I have, 76 change against main
and none is newly refused.

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

@⁠tsunderejay on GitHub · 2026-11-25 21:15 UTC · original

Static review against current main found multiple high-risk correctness issues before merge: (1) 64-bit counts can combine a captured low mask with a stale high word; (2) implicit scalar writes such as v_readfirstlane and VOP3B carry outputs are missing from reaching definitions; (3) WaveActiveBallot cannot reconstruct a 64-lane mask on a wave32 host, so the advertised wave64 fallback changes results; (4) partial waves can produce wrong counts because the width requirement is applied after complete-wave validation; (5) operand width is used as wave-width evidence, causing false SGPR aliases; (6) captures under divergent control flow do not see the whole wave; (7) high-word ballot names/declarations are incomplete. Please address these with focused regressions before merging.

**@⁠tsunderejay** on GitHub · 2026-11-25 21:15 UTC · [original](https://github.com/brcly/TuxBox/pull/197#issuecomment-7174830287) Static review against current main found multiple high-risk correctness issues before merge: (1) 64-bit counts can combine a captured low mask with a stale high word; (2) implicit scalar writes such as v_readfirstlane and VOP3B carry outputs are missing from reaching definitions; (3) WaveActiveBallot cannot reconstruct a 64-lane mask on a wave32 host, so the advertised wave64 fallback changes results; (4) partial waves can produce wrong counts because the width requirement is applied after complete-wave validation; (5) operand width is used as wave-width evidence, causing false SGPR aliases; (6) captures under divergent control flow do not see the whole wave; (7) high-word ballot names/declarations are incomplete. Please address these with focused regressions before merging.
Author
Owner

@⁠Jerkin42 on GitHub · 2026-11-26 01:41 UTC · original

Ai

Thanks, these were right, and fixing them turned up a worse problem. All in 1270654:

  1. Both words of a wave64 mask must have the same reaching definitions, and a 64-bit count must start at the mask's first word (both_words_of_a_mask_come_from_the_same_place).
  2. v_readfirstlane (VOP1 and VOP3), v_readlane and VOP3B carries (with v_mad_*64 and v_div_scale) now count as writes, and a VOP2 carry writes VCC (every_scalar_write_is_seen).
  3. A wave64 program now declares WaveSize(64), so a host without wave64 refuses it as it does other wave64 requirements. 33 programs need it, the shadow tile passes among them. #201's portable lowering could lift that later.
  4. The width is applied before the complete-wave check. A group that isn't made of whole waves keeps the old translation (partial_waves_keep_the_old_translation).
  5. Masks are only followed through moves and combinations at the program's width. Contradicting evidence (both widths of EXEC, or EXEC_HI / v_mbcnt_hi in a 32-lane program) means no ballots (a_pair_is_not_a_mask_of_32_lanes). I found no wave-size field in the extract. If there is one, I'll use it.
  6. A ballot is only taken where the lanes that can be in the mask run together. That excludes code a branch on a mask skips or repeats (VCC, or SCC from a mask operation or a compare of a mask) and anything after an early exit. A branch on EXEC keeps the running lanes together (lanes_that_parted_are_not_counted).
  7. Both words of a wave64 mask are declared, and the test checks it.

The worse problem: ray tracing resolve programs (resolve_wave32) already keep whole masks, so the first version's ballot of one lane's bit broke about 1,000 of them. They're excluded now (resolve_counts_its_whole_masks). The branch now changes 76 programs instead of 1,179. Each of the six new mask tests fails on the first commit, and mask-ballots (300 cases) and lockstep (245) pass on the GPU.

**@⁠Jerkin42** on GitHub · 2026-11-26 01:41 UTC · [original](https://github.com/brcly/TuxBox/pull/197#issuecomment-7180029453) Ai Thanks, these were right, and fixing them turned up a worse problem. All in 1270654: 1. Both words of a wave64 mask must have the same reaching definitions, and a 64-bit count must start at the mask's first word (`both_words_of_a_mask_come_from_the_same_place`). 2. `v_readfirstlane` (VOP1 and VOP3), `v_readlane` and VOP3B carries (with `v_mad_*64` and `v_div_scale`) now count as writes, and a VOP2 carry writes VCC (`every_scalar_write_is_seen`). 3. A wave64 program now declares `WaveSize(64)`, so a host without wave64 refuses it as it does other wave64 requirements. 33 programs need it, the shadow tile passes among them. #201's portable lowering could lift that later. 4. The width is applied before the complete-wave check. A group that isn't made of whole waves keeps the old translation (`partial_waves_keep_the_old_translation`). 5. Masks are only followed through moves and combinations at the program's width. Contradicting evidence (both widths of EXEC, or EXEC_HI / `v_mbcnt_hi` in a 32-lane program) means no ballots (`a_pair_is_not_a_mask_of_32_lanes`). I found no wave-size field in the extract. If there is one, I'll use it. 6. A ballot is only taken where the lanes that can be in the mask run together. That excludes code a branch on a mask skips or repeats (VCC, or SCC from a mask operation or a compare of a mask) and anything after an early exit. A branch on EXEC keeps the running lanes together (`lanes_that_parted_are_not_counted`). 7. Both words of a wave64 mask are declared, and the test checks it. The worse problem: ray tracing resolve programs (`resolve_wave32`) already keep whole masks, so the first version's ballot of one lane's bit broke about 1,000 of them. They're excluded now (`resolve_counts_its_whole_masks`). The branch now changes 76 programs instead of 1,179. Each of the six new mask tests fails on the first commit, and `mask-ballots` (300 cases) and `lockstep` (245) pass on the GPU.
Author
Owner

@⁠ReverendOzmund on GitHub · 2026-11-26 11:54 UTC · original

NVIDIA question: 33 of the changed programs now require wave64. On a wave32 host those get refused before the driver compile (#171), unless the paired wave64 translation that landed on main since picks them up. When you rebase, do those 33 go to the paired path, or does fixing the shadow blocks on AMD turn them into missing shadows on NVIDIA? Happy to check the counts on a 3080 Ti.

**@⁠ReverendOzmund** on GitHub · 2026-11-26 11:54 UTC · [original](https://github.com/brcly/TuxBox/pull/197#issuecomment-7191314320) NVIDIA question: 33 of the changed programs now require wave64. On a wave32 host those get refused before the driver compile (#171), unless the paired wave64 translation that landed on main since picks them up. When you rebase, do those 33 go to the paired path, or does fixing the shadow blocks on AMD turn them into missing shadows on NVIDIA? Happy to check the counts on a 3080 Ti.
github-bot force-pushed github-bot/gh-197 from 12706549bc to 575a878fd6 2026-11-26 13:14:54 +00:00 Compare
Author
Owner

@⁠Jerkin42 on GitHub · 2026-11-26 13:14 UTC · original

Rebased onto c6ce497 and checked the 76 programs this changes, with and without --wave32-host. Of the 33 that now need wave64, 27 take the paired wave64 translation on a wave32 host. The other 6 can't use the paired form (4 use implicit derivatives, 2 would exceed the 8 MiB compiler budget), so a wave32 host now keeps main's translation for those (new commit 575a878). Net result on wave32 hosts: no program that main translates is refused. The 4 that were already refused on wave32 stay refused. I also committed the regenerated host_profiles.txt. Counts from your 3080 Ti would still be welcome.

**@⁠Jerkin42** on GitHub · 2026-11-26 13:14 UTC · [original](https://github.com/brcly/TuxBox/pull/197#issuecomment-7193060171) Rebased onto c6ce497 and checked the 76 programs this changes, with and without --wave32-host. Of the 33 that now need wave64, 27 take the paired wave64 translation on a wave32 host. The other 6 can't use the paired form (4 use implicit derivatives, 2 would exceed the 8 MiB compiler budget), so a wave32 host now keeps main's translation for those (new commit 575a878). Net result on wave32 hosts: no program that main translates is refused. The 4 that were already refused on wave32 stay refused. I also committed the regenerated host_profiles.txt. Counts from your 3080 Ti would still be welcome.
This pull request can be merged automatically.
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/197/head:github-bot/gh-197'
git switch 'github-bot/gh-197'

Merge

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