Skip to content

xshader: don't branch on the lane around a ballot's operand - #670

Open
3kynox wants to merge 1 commit into
brcly:mainfrom
3kynox:pr-ballot-no-branch
Open

3kynox wants to merge 1 commit into
brcly:mainfrom
3kynox:pr-ballot-no-branch

Conversation

@3kynox

@3kynox 3kynox commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

What you see. With the programs that shade ray hits running, the console ray-tracing structures light the room in ProjectScarlett's first playable scene. The right side of the view (the drawings on the wall, the bunk) is lit by indirect light while the structures are on, and goes black when they are off. I alternated them every 45 s from a still camera, all nine walks served and nothing skipped. In one run the corner switched 5 times out of 5 each way (mean grey about 40 on, 3 off). A second, shorter run did the same (37 and 21 on, 3 off). On this card those programs used to stop the GPU 11 to 20 s after the world loaded, so this could not be seen.

What was wrong. Programs translated to one thread per console lane ballot their compares as WaveActiveBallot((exec_lo & tb_lane_bit) != 0u && cond). HLSL's && evaluates its right side only where the left one holds, so DXC branches on the lane around the operand. When another branch on the same condition follows, and then another ballot, the program NVIDIA's driver compiles never ends on some inputs: Xid 109, one dispatch in flight, no bad address. The smallest program I reduced it to has no loop. I take it for a convergence deadlock in the driver, which I can't confirm from outside.

The change. Both sides are evaluated, and(...) instead of &&, at the five places that write a ballot. The operands only read registers and constants.

It changes many programs. 3,601 of the 38,785 ProjectScarlett requests in my cache that translate on their own (1,649 more don't), all cs_6_6, so their cached shaders recompile. CONTRIBUTING asks to agree on that first: if you'd rather land it together with another translator change, that's fine with me. None of them uses the packed form ((exec_lo & 1u), one of the five places). I changed it for the same reason and have measured nothing on it.

Tested

  • The checks relevant to this change passed (two fail, and fail the same way on 106120ca without the change, see below)
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below
  • Where the table calls for AMD and NVIDIA, they passed on both. Otherwise, say which vendor still needs to run them: AMD passed (run by @NickStallman, below); NVIDIA has the two failures above, which 106120ca has too

GPU, driver and backend: RTX 4070 Ti SUPER 16 GB, NVIDIA 620.73.04, vkd3d-proton (Proton Experimental), Intel CPU.

Evidence: TuxBox check of 705be7c49b96, no uncommitted changes, GPU default adapter: 15 passed, 2 failed, 0 not run

AMD, run by @NickStallman on a Radeon iGPU (1002:13c0), GE-Proton11-15, Windows DXC 1.8.2505, tuxbox.py check --changed 106120ca (his comment):

Evidence: TuxBox check of 705be7c49b96, no uncommitted changes, GPU 1002:13c0: 16 passed, 0 failed, 0 not run
python tuxbox.py check --proton "Proton - Experimental" --prefix PREFIX --windows-dxc DXC/bin/x64
  FAILED   Python tests: test_a_guest_that_died_stops_the_sandbox_instead_of_waiting_out_the_timeout
  FAILED   Lanes and fixtures on the GPU: lanes-selftest-queue-page-mappings
  passed   the other 15, with the D3D12 regression controls and the shader translator on the GPU

No Lanes scenario: the hang needs two ballots and a branch arranged the way a title program has them, and a scenario that fails without the change would hang the card of whoever runs it. The unit test a_resolve_ballots_without_branching_on_its_lane checks the emitted form and fails without the change.

Outside the game. I reproduced the hang with generated inputs, the same DXC and vkd3d-proton, and reduced one program to 57 lines of HLSL that still hang.

program original hangs at input same HLSL with and(...)
1 2 none in 1,500
2 2 none in 1,500
3 61 none in 1,500
4 (cut down) 293 none in 20,000

One of them wrote the same bytes before and after on the 60 inputs before its first hang. This branch's own translator output for the four programs also ran there: 29,000 inputs, no hang.

In the game. These runs did not use this branch's translator. They used 6d4f1cd7 with #647 and local diagnostics, and the cached HLSL of the same programs rewritten the same way. 20 runs reached the world with the hit-shading programs running and nothing skipped. None ended with one of those programs stuck without a bad address.

What I can't claim:

  • The two runs with the visible effect had TUXBOX_READ_RESIDENCY=0, TUXBOX_CONSOLE_STRUCTURES=1 and no TUXBOX_CONSOLE_WALKS.
  • I haven't compared the light with a console, so I don't know that its colour is right.
  • Runs still end in device losses. Most look like d3d12: GPU reads a placed texture's image after it was destroyed (GPUVM ReadInvalid on ProjectScarlett, RX 9070 XT) #603. Some look like a walk reading outside the structures buffer (the fault addresses point there), which is mine to find, and one left a walk in flight with no address (its translation has no ballot of this form). This change fixes none of these.
  • docs/ENVIRONMENT.md says only four walks can be served in ProjectScarlett. I left that line until that walk is fixed.

Checks

No evidence JSON changes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The shared shader changes still need human hardware validation because required AMD checks are unrun and the packed-ballot path has no reported runtime measurement.

No open issues 🎉

What changed in this PR

Updates TuxBox’s shader translator to avoid lane-dependent short-circuit evaluation around ballot operands, addressing reported NVIDIA GPU hangs.

Changes:

  • Replaces && with eager and(...) at five ballot emission sites.
  • Adds a regression test checking the emitted ballot form.
File Description
tools/​shader-compiler/​src/​tests/​hlsl/​exec_group_wave32.rs Tests that resolve ballots avoid short-circuit operands.
tools/​shader-compiler/​src/​hlsl/​float64_div.rs Uses eager evaluation for the division carry ballot.
tools/​shader-compiler/​src/​hlsl/​emitter/​vector.rs Uses eager evaluation for the vector carry ballot.
tools/​shader-compiler/​src/​hlsl/​emitter/​vector_wide.rs Updates resolve comparisons, packed comparisons, and carry ballots.

Review effort: Balanced

@NickStallman

Copy link
Copy Markdown
Contributor

AMD run, and a second NVIDIA hang that this fixes.

AMD. PR head 705be7c on a Radeon iGPU (1002:13c0), GE-Proton11-15, Windows DXC 1.8.2505, tuxbox.py check --changed 106120ca:

Evidence: TuxBox check of 705be7c49b96, no uncommitted changes, GPU 1002:13c0: 16 passed, 0 failed, 0 not run

That includes the wave32 guard checks (wave-guard-32-*, 33 cases each) and mask-ballots (665 cases). The shader-compiler tests pass natively too, with the new test among them.

A second hang, on an RTX 5090 (NVIDIA 620). I had been chasing a different ProjectScarlett program that ends in Xid 109: a ray-traced shadow pass that dispatches without rays and still hangs. In its translation a per-lane if (exec_lo & tb_lane_bit) { … 1.0 / … } sits between two ballots, so I took the division branch for the trigger. Turning that division into a select did stop it. But your change alone stops it too, with that branch still in place.

Method: same request, same DXC, run standalone with a fixed pseudo-random input, 1,700 groups, outside the game:

translation result
106120c hung 2 of 2 (Xid 109)
106120c, the runtime's DXC flags hung 1 of 1
this PR 130 of 130 rounds ran, 8 invocations
this PR, the runtime's DXC flags 60 of 60
this PR + my division change 130 of 130

The HLSL with this PR differs from 106120c's only in the 120 ballot lines (&& to and(...)); the per-lane division branch is unchanged. So in this program too, the hang follows the && in the ballot operand. That supports your reading. With this PR applied, my division change makes no measured difference, so I'll drop it.

Limits: one program, one input fill (a zero fill never hung, with or without either change), small counts. Not yet run in the game with this PR.

🤖 Generated with Claude Code

@3kynox

3kynox commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @NickStallman. Another card and another program, with the two changes tested apart: that's a stronger confirmation than mine. I've added your AMD run to the description.

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.

3 participants