Skip to content

xshader: accept uniform loops around compute swizzles for single-wave - #284

Merged
brcly merged 2 commits into
brcly:mainfrom
s3rius:xshader/compute-swizzle-loop
Nov 27, 2026
Merged

brcly merged 2 commits into
brcly:mainfrom
s3rius:xshader/compute-swizzle-loop

Conversation

@s3rius

@s3rius s3rius commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The shader translator refused any shared-memory swizzle inside a loop (a "compute swizzle inside a loop" refusal class hitting ~36 recorded ProjectScarlett requests). A single-wave group (32/64 lanes) with a uniform back-edge — SCC, EXEC, or unconditional — makes the exchange run the same number of times for every invocation, so it translates now; divergent VCC loops and multi-wave groups keep the conservative rejection. Added three tests plus a loop fixture. Verified on the 36 affected real requests (all now translate or refuse for other reasons) and the full check --no-gpu suite.

Verified on Lanes

A pull request is not merged until Lanes passes on a GPU. Run:

python tuxbox.py check
  • 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:

  • This changes code both vendors run, and the check passed on an AMD and an NVIDIA card (why). Otherwise, say which vendor still needs to run it:

Paste the summary printed at the end:

Summary
  passed   Build the shader compiler (0s)
  passed   Python tests (3s)
  passed   Shader compiler tests (0s)
  passed   DXBC tests (0s)
  passed   XG layout tests (1s)
  passed   D3D12 Windows typecheck (0s)
  passed   Build (23s)
  passed   Lanes and fixtures on the GPU (335s)
  passed   D3D12 regression controls (104s)
  passed   Shader translator on the GPU (361s)

Checks

CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, and a Windows typecheck of tuxbox-d3d12.

If this pull request changes evidence JSON, regenerate the results table and the progress map and include both files:

python tuxbox.py status > docs/RESULTS.md
python tuxbox.py status --svg docs/progress.svg

@s3rius
s3rius force-pushed the xshader/compute-swizzle-loop branch from 81d591c to 5dcf365 Compare November 26, 2026 23:35
@brcly

brcly commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks for taking on the swizzle-in-loop refusals. I don't think this can merge as it stands: the translated loops can leave threads waiting at the exchange's barriers while other threads have already left.

Why

The new rule assumes every invocation runs a loop the same number of times when the group has at most 64 threads and the back-edge isn't vccz/vccnz. In this emitter that doesn't follow:

  • exec_lo and vcc_lo hold only the thread's own lane bit, so an execz/execnz back-edge is per-thread.
  • An unconditional or SCC back-edge doesn't help if a forward execz/vccz branch inside the loop leaves it: that becomes a per-thread jN flag, and the while condition tests !jN.
  • A 64-thread group running 32-lane waves holds two waves, whose SCC can differ.

Small case (this PR merged onto main at 12e4b06, fixture::shader(0x041f, 0) with the ISA replaced)

bf880003            s_cbranch_execz  (leaves the loop)
d8d4041f 00000001   ds_swizzle_b32 v0, v1
bf82fffc            s_branch         (back to the start)
bf8cc07f bf810000

It translates to:

do {
    tb_c2 = false;
    j3 = j3 | ((exec_lo & 1u) == 0u);
    tb_swizzle[tb_lane] = ((exec_lo & 1u) != 0u && !j3) ? v1 : 0u;
    GroupMemoryBarrierWithGroupSync();
    ...
    tb_c2 = (true);
} while (tb_c2 && !(j3) && ++tb_guard < 65536u);

A thread with its EXEC bit clear leaves after this iteration; the others come back to the barrier without it. A lone s_cbranch_execnz back-edge after the swizzle translates to while (((exec_lo & 1u) != 0u) && ...) around the same barriers.

On game shaders

I took 28 recorded ProjectScarlett requests refused with compute swizzle inside a loop. With this PR 10 of them translate (the rest hit other refusals), and all 10 have a loop containing a group barrier whose while also stops on a per-thread exit flag.

I haven't run any of this on a GPU, so this comes from reading the emitted HLSL, not from an observed hang.

What would make it work

  • Vote every exit of such a loop across the threads that share its barriers (back-edge and forward leaves alike), so they all stay until the last one is done, with the finished lanes masked off. Check that this matches what the console does for each accepted branch kind.
  • Keep refusing anything the vote doesn't cover.
  • Add a GPU control where lanes would leave on different iterations, comparing the result with a reference. The current tests check which programs are accepted, but nothing runs the loop.

Happy to look again, or to help with the vote if you'd like.

@s3rius

s3rius commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

That's true. There's a subtle problem and need you confirmation which direction to go.

What changed in response to your comment

I tightened the acceptance rule in tools/shader-compiler/src/hlsl/driver/pipeline.rs. A loop around a shared exchange (ds_swizzle_b32, tb_group_readlane, tb_group_gather) is now accepted only when all of the following hold:

  • the workgroup is a single 32-lane host wave (count <= 32 — multi-wave groups are refused, since SCC is per-wave and a 64-thread group here is two waves),
  • the back-edge is unconditional or SCC (never per-lane VCC, never per-thread EXEC — in this emitter both exec_lo and vcc_lo hold only the thread's own lane bit),
  • no branch at all appears inside the loop body — no forward exits, continues, or nested loops.

Everything else keeps the conservative refusal. That's the "keep refusing" half of your suggestion, implemented without touching the loop emission itself.

Shortcomings — what's still wrong

The tightened rule removes the obvious hazards but does not close the loop-exit hole, which I confirmed by tracing the emitted HLSL of an accepted game shader. What still happens:

  1. Outer exit flags can still leave the loop per-thread. Flags like j252/j285 are set by forward branches before the loop (one of them tests (exec_lo & 1u) == 0u, a per-thread EXEC comparison). These are emitted in the while exit condition (tb_c{i} && !(j252 | j255 | j285)), so a thread carrying one of those flags leaves the loop at the bottom of the first iteration — while the remaining threads keep executing the barriers in later iterations. That's the barrier-mismatch/deadlock shape you flagged; I haven't run this on a GPU, so this is from reading the emitted code.
  2. The exchange write is not masked on those flags. tb_swizzle[tb_lane] = v19; is emitted unguarded (no !j{label} predicate wrapped around it), and neither the write nor the value-computation guard is extended to the exit-flag set — so a thread that skipped its value computations writes a stale/garbage v19 into its lane, and other lanes read it.
  3. No GPU control. The current tests check which programs are accepted, not that the emitted loops execute correctly with lanes finishing on different iterations.

So the accepted set is smaller than it was, but it is not yet sound.

What I need from you

  • Confirm you want the vote implemented as the mechanism for loop exits, keeping all threads in the loop until the last one is done, with finished lanes masked off — with per-wave votes being out of scope (multi-wave groups keep refusing).
  • Any guidance on how we should handle the outer-exit-flag cases (is the fix a new uniform exit term, masking on the exchange write, or both?) — I'd like to make sure I'm not reinventing something you've already thought through.
  • Confirm the GPU control is the right bar for this: a synthetic loop where lanes leave on different iterations, compared against a reference, run through tools/check_compute_swizzle.py-style harness.

So, it's not really clear which option is the best for now.

@s3rius

s3rius commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Okay, nvm, seems like solution was found. Will get back soon.

@s3rius
s3rius force-pushed the xshader/compute-swizzle-loop branch from 668c90d to cce9ada Compare November 27, 2026 01:12
@s3rius

s3rius commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@brcly, now all comments are addressed. Can you re-verify?

@s3rius
s3rius force-pushed the xshader/compute-swizzle-loop branch from cce9ada to 0877691 Compare November 27, 2026 01:18
@brcly

brcly commented Nov 27, 2026

Copy link
Copy Markdown
Owner

I rechecked the latest push (merged with main at d8c4327, 41,599 recorded ProjectScarlett requests). The barrier hang in my earlier example is addressed, but compared with main: 2 more translate, 6 no longer do ("unverified EXEC election"), and 362 that already translated produce different output.

  • The WaveActiveAnyTrue condition applies to every loop with a barrier or other group operation, not just the swizzle loops this PR newly accepts.
  • 28 of the changed shaders have more than 32 threads per group. A wave vote can't keep a whole group in step if it spans several waves.
  • 31 repeat on (exec_lo & 1u) == 0u. The console takes that branch only when the whole wave's EXEC is zero, but voting "any" repeats while any single lane's bit is clear. Conditions on a per-thread SCC being zero may have the same problem.

Suggestion: apply the vote only to the newly accepted single-wave swizzle loops, leave other loops' output unchanged, refuse the inverted conditions (or require all lanes to agree), and check that no shader is lost against main.

This comes from translator output; I haven't run it on a GPU.

@s3rius
s3rius force-pushed the xshader/compute-swizzle-loop branch from 0877691 to 4833731 Compare November 27, 2026 01:51
@s3rius

s3rius commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Okay, this new version resolves few swizzle_loop shaders, 43 -> 40. Other shaders seem to be the same. Ran GPU tests, they passed.

… groups

Signed-off-by: Pavel Kirilin <s3riussan@gmail.com>
Signed-off-by: Pavel Kirilin <s3riussan@gmail.com>
@s3rius
s3rius force-pushed the xshader/compute-swizzle-loop branch from 4833731 to a27ca0a Compare November 27, 2026 02:13
@brcly

brcly commented Nov 27, 2026

Copy link
Copy Markdown
Owner

good work, my tests all pass!

@brcly
brcly merged commit 5444f38 into brcly:main Nov 27, 2026
2 checks passed
@s3rius
s3rius deleted the xshader/compute-swizzle-loop branch November 27, 2026 02:15
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