xshader: run loops that meet the group at barriers as a group #372

Merged
NickStallman merged 2 commits from upstream-pr/372 into main 2026-11-28 09:22:22 +00:00
NickStallman commented 2026-11-28 05:07:36 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-28T05:07:36Z.


Refs #232.

Problem

A 128-thread translated mesh group holds two 64-lane guest waves. On a 32-lane host, a vote over one guest wave reaches the other half of that wave through group barriers: tb_page_ballot uses three, tb_page_read two.

When such a vote sat inside a guest loop, each host thread left the loop by its own copy of its wave's condition. The two guest waves could then run the loop a different number of times, as a loop over distinct values does, and one wave met group barriers the other never reached. That is undefined behaviour.

wave_cull also took the exit vote of an s_andn2_b64 / s_cbranch_scc0/1 loop inside the guest branch guard, which differs between guest waves. Only threads 0–7, all in the first guest wave, clear the vote words. So once the first wave is done, the second can keep reading stale votes and never leave its loop, which had no iteration guard.

Measured on main, over the 7,990 recorded mesh requests of ProjectScarlett:

  • 6,648 translate, and every one of them has group barriers.
  • In 244 of them, 371 group-barrier calls sit inside a loop under a guest branch guard. All of them are this exit vote.
  • Only 8 of 5,112 mesh loops have the iteration guard.

The link to #232 is still unproven. Breadcrumbs from a ProjectScarlett GPU hang on an RTX 5090 (Xid 109) put the unfinished work in a run of 39 ExecuteIndirect mesh draws. Four of the mesh programs in those draws hold such a loop. Game runs on the same machine, all on a warm driver cache, all reaching the story load:

Build Result
main (four runs: 10b48fd4 three times, 70b825a7 once) Xid 109, CTX SWITCH TIMEOUT, at 2.5–14 min
this change on df4f27c3 no Xid 109. About 14 min in, Xid 31 instead: MMU Fault: ENGINE GRAPHICS HUBCLIENT_FE … FAULT_PDE ACCESS_TYPE_VIRT_WRITE

That is one run of this change, so the timeout's disappearance is suggestive, not proven. The new fault is a front-end write to an unmapped address. It followed vkd3d-proton dropping raytracing builds and postbuild-info writes whose title addresses map to no resource. That is the area #320 covers, and we are testing the two together next.

Change

A loop now iterates as a group when its instructions hold a group rendezvous, or when it encloses such a loop. This applies to the mesh programs that vote across guest waves, the hoisted wave-cull compute programs and the page-wave programs.

  • Group vote. The loop repeats while tb_group_any(tb_live) is true. That is a groupshared OR, the same in every thread.

  • Done threads change nothing. A thread whose own condition says stop puts its skip target aside and sets tb_skip past every instruction. Every guard in the body tests that, so the thread changes no state but still meets every barrier. After the loop it takes its skip target back.

  • Votes move before the guard. The votes wave_cull took inside the guard are now taken before it:

    • the SCC branch after s_andn2_b64;
    • s_cmp_lg_u32 exec_hi, 0;
    • s_cselect_b32 exec_hi, exec_lo.

    A thread the guard skips votes false. For every thread that runs the instruction, the result is the same as before.

  • Iteration guards.

    • A group loop counts its own rounds, the same in every thread, up to 65,536.
    • Mesh loops without a rendezvous get the per-thread tb_guard other stages have. Leaving one early skips no barrier.
  • Unchanged. Compute loops without a rendezvous, and every program outside those three kinds.

After the change, no group barrier in any recorded mesh program sits under a guard. There are 351 group loops in 244 programs, and every one of the 5,112 mesh loops is bounded.

Tests

  • Rust:
    • src/tests/hlsl/group_loops.rs covers loop selection, the guarded vote, and the emitted shape of a full mesh translation. In that translation, no barrier is inside an if, and every loop around one closes on tb_group_any with the round count.
    • A scalar-exit variant keeps the per-thread exit and gets tb_guard.
  • GPU: the translated mesh check gains loop-3, loop-67 and loop-128.
    • The two guest waves of the group run a wave-voted loop once and nine times.
    • A wrong trip count moves the triangle off the expected pixels. The 67 and 128 cases draw from the second wave's lanes.
    • Without the translator change, on RADV (1002:13c0): loop-3 passed. loop-67 hung the GPU (the kernel logged ring gfx_0.0.0 timeout and a successful ring reset) and then read back 0 of 64 words. The guest stops at the first mismatch, so loop-67 clipped and loop-128 did not run.

Checks

Run at 0c08bc31, this branch before the rebase onto 7bafe6b6:

  • Translator GPU checks (tools/check_translator_gpu.py, all 59): pass on AMD (RADV, 1002:13c0) and on an RTX 5090 (10de:2b85).
  • Lanes suites lanes-shader-cache, lanes-raster-tables, lanes-compute-root, lanes-compute-root-recovery and lanes-bindless-compute (28 checks): pass on both cards.

After the rebase:

  • cargo test for the shader compiler: pass.
  • python tuxbox.py check --no-gpu: pass (all seven steps).
  • Shader harness --no-gpu against 7bafe6b6: PASS.
    • wave64: 27,976 programs translated before and after, 0 regressed, 1,100 outputs changed.
    • wave32: 4,880 translated, 0 changed.
    • DXC/DXV: 200 of 200 sampled changed programs compiled.

The 1,100 changed outputs are all mesh programs (ms_6_6): 1,004 programs and 96 .link requests. I split them by kind in the earlier run against df4f27c3, which changed the same number: 244 gained group loops and the guard, and 760 only gained tb_guard on loops without a rendezvous.

🤖 Generated with Claude Code

<!-- greyfinch-sync:brcly/TuxBox:issue:5536904737 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/372). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-28T05:07:36Z. --- Refs #232. ## Problem A 128-thread translated mesh group holds two 64-lane guest waves. On a 32-lane host, a vote over one guest wave reaches the other half of that wave through group barriers: `tb_page_ballot` uses three, `tb_page_read` two. When such a vote sat inside a guest loop, each host thread left the loop by its own copy of its wave's condition. The two guest waves could then run the loop a different number of times, as a loop over distinct values does, and one wave met group barriers the other never reached. That is undefined behaviour. `wave_cull` also took the exit vote of an `s_andn2_b64` / `s_cbranch_scc0/1` loop inside the guest branch guard, which differs between guest waves. Only threads 0–7, all in the first guest wave, clear the vote words. So once the first wave is done, the second can keep reading stale votes and never leave its loop, which had no iteration guard. Measured on main, over the 7,990 recorded mesh requests of ProjectScarlett: - 6,648 translate, and every one of them has group barriers. - In 244 of them, 371 group-barrier calls sit inside a loop under a guest branch guard. All of them are this exit vote. - Only 8 of 5,112 mesh loops have the iteration guard. The link to #232 is still unproven. Breadcrumbs from a ProjectScarlett GPU hang on an RTX 5090 (Xid 109) put the unfinished work in a run of 39 `ExecuteIndirect` mesh draws. Four of the mesh programs in those draws hold such a loop. Game runs on the same machine, all on a warm driver cache, all reaching the story load: | Build | Result | |---|---| | `main` (four runs: 10b48fd4 three times, 70b825a7 once) | Xid 109, CTX SWITCH TIMEOUT, at 2.5–14 min | | this change on `df4f27c3` | no Xid 109. About 14 min in, Xid 31 instead: `MMU Fault: ENGINE GRAPHICS HUBCLIENT_FE … FAULT_PDE ACCESS_TYPE_VIRT_WRITE` | That is one run of this change, so the timeout's disappearance is suggestive, not proven. The new fault is a front-end write to an unmapped address. It followed vkd3d-proton dropping raytracing builds and postbuild-info writes whose title addresses map to no resource. That is the area #320 covers, and we are testing the two together next. ## Change A loop now iterates as a group when its instructions hold a group rendezvous, or when it encloses such a loop. This applies to the mesh programs that vote across guest waves, the hoisted wave-cull compute programs and the page-wave programs. - **Group vote.** The loop repeats while `tb_group_any(tb_live)` is true. That is a groupshared OR, the same in every thread. - **Done threads change nothing.** A thread whose own condition says stop puts its skip target aside and sets `tb_skip` past every instruction. Every guard in the body tests that, so the thread changes no state but still meets every barrier. After the loop it takes its skip target back. - **Votes move before the guard.** The votes `wave_cull` took inside the guard are now taken before it: - the SCC branch after `s_andn2_b64`; - `s_cmp_lg_u32 exec_hi, 0`; - `s_cselect_b32 exec_hi, exec_lo`. A thread the guard skips votes false. For every thread that runs the instruction, the result is the same as before. - **Iteration guards.** - A group loop counts its own rounds, the same in every thread, up to 65,536. - Mesh loops without a rendezvous get the per-thread `tb_guard` other stages have. Leaving one early skips no barrier. - **Unchanged.** Compute loops without a rendezvous, and every program outside those three kinds. After the change, no group barrier in any recorded mesh program sits under a guard. There are 351 group loops in 244 programs, and every one of the 5,112 mesh loops is bounded. ## Tests - Rust: - `src/tests/hlsl/group_loops.rs` covers loop selection, the guarded vote, and the emitted shape of a full mesh translation. In that translation, no barrier is inside an `if`, and every loop around one closes on `tb_group_any` with the round count. - A scalar-exit variant keeps the per-thread exit and gets `tb_guard`. - GPU: the translated mesh check gains `loop-3`, `loop-67` and `loop-128`. - The two guest waves of the group run a wave-voted loop once and nine times. - A wrong trip count moves the triangle off the expected pixels. The 67 and 128 cases draw from the second wave's lanes. - Without the translator change, on RADV (1002:13c0): `loop-3` passed. `loop-67` hung the GPU (the kernel logged `ring gfx_0.0.0 timeout` and a successful ring reset) and then read back 0 of 64 words. The guest stops at the first mismatch, so `loop-67` clipped and `loop-128` did not run. ## Checks Run at 0c08bc31, this branch before the rebase onto 7bafe6b6: - Translator GPU checks (`tools/check_translator_gpu.py`, all 59): pass on AMD (RADV, 1002:13c0) and on an RTX 5090 (10de:2b85). - Lanes suites `lanes-shader-cache`, `lanes-raster-tables`, `lanes-compute-root`, `lanes-compute-root-recovery` and `lanes-bindless-compute` (28 checks): pass on both cards. After the rebase: - `cargo test` for the shader compiler: pass. - `python tuxbox.py check --no-gpu`: pass (all seven steps). - Shader harness `--no-gpu` against 7bafe6b6: PASS. - wave64: 27,976 programs translated before and after, 0 regressed, 1,100 outputs changed. - wave32: 4,880 translated, 0 changed. - DXC/DXV: 200 of 200 sampled changed programs compiled. The 1,100 changed outputs are all mesh programs (`ms_6_6`): 1,004 programs and 96 `.link` requests. I split them by kind in the earlier run against df4f27c3, which changed the same number: 244 gained group loops and the guard, and 760 only gained `tb_guard` on loops without a rendezvous. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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
brcly/TuxBox!372
No description provided.