Skip to content

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

Open
Jerkin42 wants to merge 2 commits into
brcly:mainfrom
Jerkin42:fix/mask-ballots
Open

Jerkin42 wants to merge 2 commits into
brcly:mainfrom
Jerkin42:fix/mask-ballots

Conversation

@Jerkin42

@Jerkin42 Jerkin42 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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

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 <noreply@anthropic.com>
@tsunderejay

Copy link
Copy Markdown

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.

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 <noreply@anthropic.com>
@Jerkin42

Copy link
Copy Markdown
Contributor Author

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. shader-compiler: portable Wave64 ballot, vote & exec lowering for non-64-wide GPUs (#151) #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants