Skip to content

SHADER-579: barrier-round count borrows an LDS word so a full 32 KiB LDS stays at 32,768 B - #3245

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
tomgamer22:shader-579-barrier-counters
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
tomgamer22:shader-579-barrier-counters

Conversation

@tomgamer22

Copy link
Copy Markdown
Contributor

SHADER-579 (claimed in #3047).

What and why

Native wave32 compute shaders whose s_barrier is not in uniform control flow run in barrier rounds (SHADER-91, bar_scheduler). The round loop counts the invocations not done in %x_buf, three Workgroup dwords (#930). On top of a full 32 KiB LDS that came to 32,780 B, 12 bytes over the 32,768 B maxComputeSharedMemorySize of the AMD Windows (RX 9070 XT) and Intel (UHD 770) drivers. The runtime check from SHADER-309 then refused those pipelines: 2 native wave32 cs in towade's corpus run.

Design

New rule Gen.bar_counts_in_lds(). When the shader uses its LDS and the LDS array plus 12 bytes is over 16,384 B (the Vulkan minimum limit; in practice an array of 16, 32 or 64 KiB), there is no %x_buf. The count borrows LDS word 0 at the end of each round (bar_scheduler_tail):

  1. A: the round's barrier (s_barrier's own). After it every invocation is done or waits, so no shader code runs until the next round.
  2. Invocation 0 saves LDS word 0 in a Function variable and clears it. Then barrier B.
  3. Every invocation not done adds 1 atomically. Then barrier C.
  4. All invocations read the count. Then barrier D.
  5. Invocation 0 stores the saved word back. Then barrier E, before the next round's code.

The shader sees its LDS bit for bit as before. The extra barriers are in uniform control flow, at a point where the whole workgroup already waits, so the barrier semantics don't change. The cost is 4 more OpControlBarriers per round, in these large-LDS round shaders only. The initial clear and its barrier before round 0 go away.

Below the threshold, %x_buf stays: 12 bytes fit every device, and there's still 1 barrier per round. A shader that never touches its LDS also keeps %x_buf, because there's no array to borrow from (12 B in all). The wave64 emulation's %x_buf (SHADER-562's area) is untouched.

SHADER-309 accounting is kept:

  • x_buf_bytes() returns 0 in this mode, so lds_bytes is still the module's whole Workgroup total.
  • The assemble bounds check is unchanged.
  • fit_shared_memory no longer reserves the 12 bytes when they'll go into the LDS, so a full LDS keeps its power-of-two array.

Result: a 32 KiB LDS wave32 round shader is now exactly 32,768 B (was 32,780 B). A 64 KiB one is 65,536 B; before, it was shrunk with OpUMod, or refused when the need filled the array.

Rejected alternatives:

  • Emit the counters only when needed: that's already the case. needs_barrier_rounds adds them only when a barrier is not at the top level.
  • Reuse LDS the shader never touches: this can't be proven with dynamic ds_* addresses. The save/restore above needs no such proof.
  • Decide per device, or leave the counters out of the budget: the translator runs offline, and the counters are real Workgroup memory, so the runtime couldn't drop them.
  • Fewer slots: a single counter dword would still total 32,772 B.

No Depends on. This builds on main's SHADER-309 code (fit_shared_memory, lds_bytes), not on ce7a348's bounds checks. It touches fit_shared_memory, so a small merge conflict with #1882 is possible.

docs/design.md: new contract row "Barrier-round count in LDS (SHADER-579)". The SHADER-309 and "Workgroup barriers" rows point to it.

Testing

  • python -I -m unittest discover -s tests/semantic -p test_cs_barrier_counts_lds.py: 8 tests, OK. This is a new file. It covers:
    • the Workgroup bytes counted from the emitted SPIR-V, plus lds_bytes: 32 KiB → {%lds: 32768}, 32 KiB under --max-shared-memory 32768 (not shrunk), 64 KiB → 65,536 B;
    • in the workgroup interpreter of test_loop_ballot_barrier.py, whose LDS starts as 0xDEADBEEF, in both wave orders: a ring shift across 10 barriers that reads LDS word 0, and an early-ending wave;
    • small LDS keeps %x_buf with 2 barriers in the module;
    • no LDS access keeps %x_buf;
    • a top-level barrier still has no rounds.
  • -p test_cs_shared_memory_limit.py: 8 tests, OK.
  • -p test_loop_ballot_barrier.py: 15 tests, OK.
  • -p test_barrier_control.py: 17 tests, OK.
  • The whole semantic suite, -p "test*.py": 4150 tests, OK (4 skipped).
  • git diff --check: clean.

No GPU run was done by the author, and spirv-val wasn't available locally. @towade offered to run this on the RX 9070 XT and the UHD 770 (32 KiB limit), the two devices that refused the 32,780 B modules.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…arge

Native wave32 barrier rounds counted the invocations not done in %x_buf,
three Workgroup dwords (ItsNotRexed#930). With a full 32 KiB LDS that made 32,780 B,
12 bytes over the 32,768 B maxComputeSharedMemorySize of the AMD Windows
and Intel drivers, so the SHADER-309 runtime check refused the pipeline.

When the shader uses its LDS and the array plus 12 bytes is over 16,384 B
(the Vulkan minimum), the count now borrows LDS word 0 between rounds:
invocation 0 saves and clears it, the invocations not done add 1, all read
it, invocation 0 restores it, each step behind a workgroup barrier while
no shader code runs. Smaller shaders keep %x_buf and one barrier per round.
A 32 KiB LDS round shader is now exactly 32,768 B.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KiryaAfimin

Copy link
Copy Markdown

Tested 81ee035 on Windows 11 with an RTX 4070 (driver 621.01, native wave32). I translated the PR’s ring-shift and early-exit shaders with a 32 KiB shared-memory cap. All four modules passed spirv-val for Vulkan 1.2.

The ring shift returned all 64 expected words with 8, 16 and 32 KiB of LDS, including the value read back from LDS word 0. In the 32 KiB early-exit case, lanes 0–31 returned 2 and lanes 32–63 returned 4. Each case matched on five GPU runs. Both 32 KiB modules used exactly 32,768 bytes of Workgroup memory.

AI-assisted

@towade

towade commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

As offered in #3047: tested 81ee035 against its base 1628210 with my own 1.0.2.1 corpus, on the two 32 KiB devices. AMD RX 9070 XT (32.0.32029.2010) and Intel UHD 770 (101.7092), both maxComputeSharedMemorySize = 32768. Windows 11 Pro 10.0.26300. Counts only.

Translation, all 77 compute shaders with 16 KiB of LDS or more (native):

  • base and this PR: 77 translated, 77 pass spirv-val.
  • Exactly 2 modules change: the two wave32 shaders from SHADER-579, lds_bytes 32,780 → 32,768. The other 75 are byte-identical to the base.

GPU, vk_pipeline_check from main 1628210 (with BUILD-546's workgroup-memory check), on those 2 modules:

RX 9070 XT UHD 770
base 1628210 2 refused: workgroup memory 32780 bytes exceeds maxComputeSharedMemorySize 32768 same 2 refused
this PR 2 modules ok, 2 pipelines ok 2 modules ok; both pipelines fail with -3

The UHD 770's -3 (VK_ERROR_INITIALIZATION_FAILED) is the Intel driver on these large game compute shaders. In my #1882 run the same happened to every such pipeline, base and PR alike. On the base these two never reached compilation, so this PR takes them past the limit on both devices. On AMD they now compile.

tests/semantic/test_cs_barrier_counts_lds.py: OK. Unrelated to this PR: the 3 shaders with a 64 KiB LDS are still 65,536 B on main; #1882's exact need brings them to 32,768 B.

AI-assisted (Claude Code); no game data in this comment.

@YaBulochka YaBulochka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran the new test on a clean worktree of 81ee0357: python3 -m pytest -q tests/semantic/test_cs_barrier_counts_lds.py -> 8 passed, 5 subtests passed. It collects cleanly beside the existing semantic tests and only touches rdna2spirv.py plus the docs row.

Read the rule as well: the 16,384 B threshold (the Vulkan minimum) is the right side to be conservative on, since a device that reports exactly the minimum would otherwise get a module it cannot run, and borrowing one LDS word only when the array is large keeps the common case untouched. No blocker from this side; I have not run a full corpus translation of this branch.

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 81ee0357 on NVIDIA GeForce RTX 3060 Ti (Ampere, subgroup 32..32, maxComputeSharedMemorySize 49152), driver 580.173.02 (Vulkan 1.4.312), Linux (Ubuntu 26.04 container on a TrueNAS 26.0 host): pass.

The PR's own programs on real hardware. test_cs_barrier_counts_lds.py runs ring_shift and early_end in the CPU workgroup interpreter only, so I ran the same programs on the GPU. Each was translated through the test's own translate() with tools/rdna2spirv.py at the base 16282101 and at this head, then put through spirv-as and spirv-val --target-env vulkan1.2. Each ran as one 64-invocation workgroup (2 waves) with vk_compute_test <spv> 1 dump built from the base plus VKC_DUMP, 20 runs each. The output words were compared with the test's RING and [2]*32 + [4]*32:

program, LDS, cap base 16282101 this head
ring_shift 8 KiB 8,204 B (%x_buf): exact 20/20 8,204 B (%x_buf): exact 20/20
ring_shift 16 KiB 16,396 B: exact 20/20 16,384 B, borrowed word: exact 20/20
ring_shift 32 KiB (the finding) 32,780 B: exact 20/20 32,768 B, borrowed word: exact 20/20
ring_shift 40 KiB, --max-shared-memory 49152 49,152 B (OpUMod): exact 20/20 49,152 B (OpUMod), borrowed word: exact 20/20
ring_shift 48 KiB, --max-shared-memory 49152 Unsupported (65,548 B) 49,152 B (OpUMod): exact 20/20
early_end 32 KiB 32,780 B: exact 20/20 32,768 B: exact 20/20
early_end 48 KiB, --max-shared-memory 49152 Unsupported (65,548 B) 49,152 B (OpUMod): exact 20/20
  • Every module that translated passed spirv-val and returned the expected 64 words in all 20 runs. That includes the word-0 cases: thread 63 of ring_shift reads LDS word 0, and thread 0 of early_end keeps its count there.
  • With this device's own limit as the cap, a 48 KiB barrier-round shader now fits exactly and runs correctly, where the base refuses it.
  • Not counted above: the 64 KiB ring_shift (65,536 B at this head; the base refuses it at 65,548 B) and the 40 KiB case at the default 64 KiB cap (65,536 B on both) are over this device's 49,152 B limit. vk_compute_test doesn't check the limit, and the NVIDIA driver built and ran them anyway (exact), so those runs say nothing about the refusal path.

Whole suite. Linux runtime built from the branch (CMake + Ninja, GCC, RelWithDebInfo), python3 -I scripts/run_gpu_tests.py, fresh --work: 142/189 passed, 46 skipped, identical per test to the base 16282101 on the same GPU. The one failure on both is ps_sdwa_f16_const, the NVIDIA f16 denormal flush that #3220 / #3040 fix. cs_barrier_divergent, cs_barrier_early_end and cs_barrier_loop pass (their LDS is small, so they keep %x_buf). build.py: 194 translated, 194 pass spirv-val. ctest: 37/37.

Code read: the save, clear, count, read and restore sequence between barriers A and E matches bar_scheduler_tail's docstring. fit_shared_memory assuming the LDS is used is safe, because an unused LDS array isn't emitted and %x_buf comes back (test_no_lds_access_keeps_x_buf).

Tools: llvm-mc 21.1.8, SPIRV-Tools 2026.1, CMake 4.2.3, Python 3.14.4. AI-assisted (Claude Code). The probe script isn't part of the PR. It runs only the PR's own synthetic programs, no game files.

@kacper-serewis

Copy link
Copy Markdown
Contributor

Coordination note from SHADER-638: we reproduced the separate Wave64 full-LDS refusal (65,536 B guest LDS plus 8,604 B exchange for a synthetic 1,024-invocation group). We are prototyping exchange staging/capture at a uniform scheduler boundary with guest LDS save/restore, preserving all guest bytes and SHADER-309 accounting. Your native barrier-round path remains yours and unchanged; we will check integration with #3245 and declare a dependency if shared helpers are reused. Unlike a single counter, Wave64 values currently live across rounds, so proving every consumer's capture timing is the first gate. CPU-only investigation so far; no claim of GPU or retail support yet. AI-assisted.

Conflicts: fit_shared_memory (main now shrinks to the RSRC2 allocation
with bounds-checked LDS; kept that, still without the 12 bytes when the
barrier-round count goes into the LDS) and docs/design.md (main's rows,
with the SHADER-579 references and row re-applied).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

7 participants