Skip to content

LDS analysis: a region one thread runs, and per-thread footprints over the threads that run it (#1435; on #2358 and #1773) - #3441

Open
Lyrnc wants to merge 3 commits into
trustno1-coder:mainfrom
Lyrnc:fix/lds-proofs-1435
Open

Lyrnc wants to merge 3 commits into
trustno1-coder:mainfrom
Lyrnc:fix/lds-proofs-1435

Conversation

@Lyrnc

@Lyrnc Lyrnc commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1435, not all of it. Built on #2358 (nidea1) and #1773 (Remorce): their commits come first here, with their authorship kept. Once both are on main, this branch rebases to the third commit alone.

Rebased onto main 7995b81 (#2358 and #1773 are both still open, so all three commits stay): 80b451e, 8bac5b6 and a5ae292 below are now f0a4374, 934475b and 66da0fe, with the same patches (git range-diff shows no change). The archive and synthetic measurements below were made on the 829188e base; the cargo test results are on 66da0fe.

What it changes

What was wrong. On a 32-lane host, #43's lds::analyze lets a wave64 compute program run as two host waves only when it can prove that the program's lane-branch regions need no order between the halves. Without a proof the program falls back to another form. On 829188e the first retry is the folded form. On main since 5c262d7 and 05d1c56, the plain retries and the local-wave idiom run first, and folding comes before the wave-wide and approximate forms. 5c262d7 measured what folded programs cost in card time.

On the title's shader archive at 829188e (2,682 compute programs, VICTORY_HOST_WAVE=32), the analysis refuses 181 programs: 179 are folded and 2 take the wave-wide form. Two common shapes had no proof:

  • a region that only one thread runs (if (tid == 0) { ... }), whatever it reads or writes;
  • a region where each thread of a subset uses its own bytes. if (tid < 16) { lds[28 + 12 t] = ...; lds[220 + 12 t] = ...; } overlaps over the whole group, because thread 16's first slot is thread 0's second. It does not overlap over threads 0..15, which are the only threads that run it.

These were refused as "an LDS read and write in one lane-branch region" or "unproven LDS writes in one lane-branch region".

What this does. a5ae292 adds two proofs to crates/victory-shader/src/emit/lds.rs:

  • Refinement D (AddrCtx::one_thread): at most one thread of the group runs the region's LDS instructions, and it is the same thread at each of them. One thread sits in one host wave and makes its accesses in program order, so the halves cannot see each other's order.
  • Refinement E (AddrCtx::thread_footprints): over the threads that may run each access, no two threads touch the same byte where one of them writes. A dword access takes its whole dwords. The check refuses the region when it goes past 65,536 spans, 1,024 open spans or 1 MiB of offset.
  • Which threads may run an LDS instruction (site_threads, may_hold): the check runs once per thread and looks at the block's reach and the instruction's guard. A lane compare whose operands are known per thread (constants and thread ids) takes that thread's outcome, and a constant mask gives the thread's bit. Any other atom stays free. The decision diagram is walked with a memo and a node budget, and past the budget the answer is "may run". Free atoms can only add threads, so both proofs err towards refusing. Groups of more than 1,024 threads are not looked at.
  • Both proofs are tried last, after the existing ones, in the read-write check and in the write-only check. A region that an earlier proof accepted takes the same path as before. There is no new switch. VICTORY_LDS_WRITE_ONLY_OK and VICTORY_LDS_LAST_RESORT are unchanged: the write-only shortcut stays off by default, and the last resort keeps its default list of one program.
  • One shape is left refused on purpose: a lane-branch region where lanes of one host half read what other lanes of the same half wrote, with no barrier in between (a wave reduction under tid < 32). The console wave saw those writes in lockstep, but the host has no order between its lanes there. That case needs a lowering with a wave-level sync, not a proof, and a test checks that it stays refused.

Six tests are added to emit::lds::tests:

Credit:

How it was tested

  • GPU and profile: no game run. Two kinds of checks were done:

    • offline translation of the title's shader archive with VICTORY_HOST_WAVE=32;
    • 13 hand-written wave64 cases (our own instructions, nothing from the title), dispatched on the RTX 5080 (32-lane waves) and on the Ryzen 7 9800X3D's integrated Radeon (RDNA 2, gfx1036). Windows 11, 32 GB.
  • Archive, per compute program. The command was VICTORY_HOST_WAVE=32 VICTORY_WAVE_WHY=1 victory translate --list --jobs 1 <archive>, with a local print (not in this branch) of each program's hash ahead of its WAVE lines. A program counts as refused when a WAVE64-WHY ... lds=true line is printed. Its form is the last WAVE64-RETRY line, or the split form when there is none.

    829188e + shader: accept purely commutative non-returning 32-bit integer LDS atomics in Wave32 regions #2358 + Water and Light Probe Shader Fix (Wave32 LDS Safety Proofs) #1773 (tree of 8bac5b6) + a5ae292 (this branch)
    refused by the LDS analysis 181 162 139
    of those, folded 179 160 137
    of those, another fallback form 2 wave-wide 2 wave-wide the same 2, "without early-outs" retry
    folded compute programs in all 266 255 232 (computed, see below)
  • Before: the 829188e column above.

  • After, shader: accept purely commutative non-returning 32-bit integer LDS atomics in Wave32 regions #2358 + Water and Light Probe Shader Fix (Wave32 LDS Safety Proofs) #1773: 19 programs are no longer refused by the LDS analysis.

    • 11 of them now take the split form instead of the folded one.
    • The other 8 stay folded for reasons that are not about LDS: lane masks read as data, v_mbcnt, v_readlane.
    • Over the whole listing (26,084 programs, 2,681 of 2,682 compute emitted in both builds), exactly 11 emitted outputs change. Every other line is identical. The same 11 change in a --jobs 8 run on empty caches.
    • In those --jobs 8 runs, VICTORY_WAVE_WIDE_FALLBACK=0 VICTORY_LDS_LAST_RESORT=0 VICTORY_LDS_WRITE_ONLY_OK=0 gives the same listing as the defaults, and no run prints no wave32 lowering.
  • After, a5ae292: this run covered only the 162 programs that shader: accept purely commutative non-returning 32-bit integer LDS atomics in Wave32 regions #2358 + Water and Light Probe Shader Fix (Wave32 LDS Safety Proofs) #1773 still refuse (VICTORY_TRANSLATE_ONLY, 162 translated, 162 compute programs emitted).

    • 23 more programs leave the folded form and take the split form. On 829188e, 7 of them were refused as "an LDS read and write in one lane-branch region" and 16 as "unproven LDS writes in one lane-branch region".
    • The 2 wave-wide programs take the earlier "without early-outs" retry instead.
    • The other programs were not translated again on this branch. The new proofs only run where the earlier ones fail, so they should not change, but that comes from reading the code, not from a measurement. The 232 above is 255 less the 23.
  • All three commits together: 42 programs are no longer refused by the LDS analysis (181 to 139). Of those, 34 leave the folded form.

  • Synthetic cases, against wave64 hardware:

  • For shader changes, the wave sizes tested: wave64 console programs on 32-lane host waves (the case this analysis is for), on NVIDIA and AMD. The reference is native wave64 on the AMD iGPU. 64-lane hosts were not run.

  • cargo test passed for the crates it touches: yes, apart from 3 tests that fail on main in the same way. On 66da0fe (main 7995b81 + the three commits), RTX 5080:

    • cargo test -p victory-shader --lib lds: 51 passed, 0 failed, 1 ignored (430 filtered out). The 32 tests of emit::lds::tests are among them.
    • cargo test -p victory-shader --lib -- --test-threads=6: 470 passed, 3 failed, 9 ignored. The 3 failures are emit::barycentric_tests::first_half_values_render_on_hardware_and_warp, emit::rect::tests::rectangles_are_drawn_whole and emit::rect::tests::the_rectangle_stage_is_a_geometry_shader, all with Container part 'Pipeline State Validation' does not match expected for module from the local DXIL validator. The same 3 fail the same way on main 7995b81 without this branch.
    • cargo clippy -p victory-shader --all-targets -- -D warnings: fails on main's own code (unused imports in emit/lower/analysis.rs:6 and emit/lower.rs:594, while_immutable_condition in emit/lower/analysis.rs:467, too_many_arguments in native.rs:1655). None is in lds.rs.
    • For reference: the tip has 32 tests in emit::lds::tests and 482 #[test] functions in the crate's sources. shader: accept purely commutative non-returning 32-bit integer LDS atomics in Wave32 regions #2358's description reports 23 passed in emit::lds::tests at its head, and steven-seagull27 reported 24 passed at Water and Light Probe Shader Fix (Wave32 LDS Safety Proofs) #1773's head. Neither result covers this branch.
  • Not tested:

    • No in-game check: no frame rate and no picture comparison.
    • The archive counts and the synthetic cases were not run again on a5ae292 or 66da0fe (only cargo test was). They come from a build of the working tree before the commit, with local debug prints. The commit adds rustfmt formatting, doc comments and the 1,024-open-span bound in the footprint check, which can only refuse more.
    • Not measured on main (fbe3dc2 or 7995b81). There folding is off by default and comes after the plain retries and the local-wave idiom, so what the 34 programs take on main without this branch is not known from these runs.
    • The archive runs used --list without --validate (0 validated). The new DXIL of the 34 programs was emitted but not validated, and no pipeline was made from it. Only the 13 synthetic cases went through the validator and the cards.
    • Inherited from Water and Light Probe Shader Fix (Wave32 LDS Safety Proofs) #1773: Melow-code's review found that counter_bits takes exponential time on a deeply shared OR chain. steven-seagull27's d6f46fd memoises it, and Remorce left it out of Water and Light Probe Shader Fix (Wave32 LDS Safety Proofs) #1773 for now. This branch carries Water and Light Probe Shader Fix (Wave32 LDS Safety Proofs) #1773 as it is. The new walk in may_hold is memoised and has a node budget.
  • In game: pending. An earlier in-game A/B is void: its build reused a stale victory-shader from a shared target dir. It is being redone and the result will be posted here.

Checked for overlap

AI use

Claude Code (Claude Opus 5.5) wrote a5ae292 (Refinements D and E and their tests). It cherry-picked #2358's and #1773's commits with their authorship kept, and merged #1773's Access fields next to #2358's. It also wrote the 13 synthetic cases and the local harness, ran the archive counts and the comparisons on both cards, and wrote this description. The first two commits are nidea1's and Remorce's own work.

🤖 Generated with Claude Code

nidea1 and others added 3 commits November 30, 2026 00:42
…d, and loop counters stepping by the group

Three more cases trustno1-coder#43's analysis proves safe on 32-lane hosts:

- A value made under a lane guard is known to an LDS instruction whose lanes
  all hold that guard (checked on the guards with `meet`, which answers no
  when it gives up). Before, any guarded definition left the address or data
  unknown, so `v_mov v11, 0; v_mov v14, 1; ds_write_b32 v11, v14` under one
  mask was "unproven".
- A write-only region whose writes put a known constant at a constant
  address, the same constant wherever two writes meet, is accepted in a loop
  too: the bytes end the same in any order of the host halves.
- A loop counter that starts at the thread's linear index and steps by a
  multiple of the (power-of-two) group size keeps each thread in its own
  slots modulo `n << shift`, in every pass, when the addresses are
  `(counter << shift) + off` with each `off % m` and width inside one slot.
  The counter's bits are followed through shifts, masks, bit-field extracts
  and ORs of disjoint bits.

On the retail shaders with VICTORY_HOST_WAVE=32: water_shading
CS_Classification_Water (0e366d15868e17bd) and indirect_light_probes
CS_VarianceFilterAlpha (5964b734c70b91eb) translate again (37 -> 35 without
a wave32 lowering), and four that translated take an earlier form
(sr_tracing CS_Tiling_Generate_Water/Alpha leave the APPROXIMATE local
waves). Every other program's output is unchanged; all emitted compute
programs pass dxil.dll's validator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r the threads that run it

Two more proofs for trustno1-coder#1435, on top of trustno1-coder#2358 (commutative atomics) and trustno1-coder#1773 (own-mask
values, one constant per dword, strided loop counters), whose commits come first here.

- Refinement D: at most one thread of the group runs the region's LDS instructions
  (`if (tid == 0) { ... }`). One thread is in one host wave and makes its accesses in
  program order, so whatever it reads or writes, the halves cannot see each other's order.
- Refinement E: over the threads that may run each access, no two threads touch one byte
  where one writes. `if (tid < 16) { lds[28 + 12 t]; lds[220 + 12 t]; }` overlaps over the
  whole group (thread 16's first slot is thread 0's second) but not over threads 0..15.

Which threads may run an LDS instruction: for each thread, the lane compares in its
block's reach and its guard whose operands are known per thread (constants and thread
ids) take that thread's outcome, constant masks its bit, other atoms stay free (a walk of
the decision diagram, with a node budget that answers "may run"). Free atoms only add
threads, so both proofs err towards refusing. Groups of more than 1024 threads are not
looked at.

Not taken, on purpose: a lane-branch region where several lanes of one host half read
what other lanes of it wrote with no barrier (a wave reduction under `tid < 32`). The
console wave saw those writes in lockstep; the host has no order between its lanes there.
That needs a lowering with a wave-level sync, not a proof.

Tests: one thread at tid 0, 40 and 63, under a branch and under v_cmpx; two threads
refused; an 8 x 8 group column refused; footprints accepted for 16 threads and refused
for 17; a read of the next thread's slot refused (across the halves and within one); one
or two atomic families in a lane loop (trustno1-coder#2358's proof).

The trustno1-coder#1773 commit was cherry-picked onto main with its Access fields merged next to trustno1-coder#2358's
(`atomic_alg` beside `data_const`, `strided`, `ds_off`) and both proofs in the read-write
check.

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.

3 participants