Repository navigation
Water and Light Probe Shader Fix (Wave32 LDS Safety Proofs) - #1773
Conversation
…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>
|
Independent synthetic verification on Windows 11 x64, NVIDIA GeForce RTX 5080 (32-lane waves), Rust 1.98.1 MSVC, exact PR head
AI-assisted. Synthetic tests only. |
|
RTX 5070 Ti / Windows / Series X: cherry-picked #1773 alone onto |
|
Tested against current main, where things have moved since this PR was opened. RTX 3090 (32-lane), Windows 11, this PR merged onto main
Note: on current main the two headline shaders already translate. They're no longer refused, presumably through main's newer wave64 fallbacks ( In game (balcony save, looking at open water, 2 rounds each after a warm-up): both shaders are translated and bound in both builds, with 4 refused shaders in both, the same set. 25.0 fps against main's 25.6 (inside the 2-3% noise). The water and sky look the same by eye in the screenshots. No crash. 🤖 Generated with Claude Code |
Melow-code
left a comment
There was a problem hiding this comment.
Read the proofs and ran them on Linux, 82221d1 merged onto main 568c5e5. One finding, in analysis time. I found no accepted program that is unsafe.
Finding: counter_bits takes exponential time on a shared DAG (emit/lds.rs, the v_or_b32 and v_or3_b32 arms). It is not memoized, unlike eval_val, and each v_or_b32 evaluates both operands. An OR of all-zero bits succeeds, so v_mov_b32 v2, 0 then N times v_or_b32 v2, v2, v2 ahead of a ds_write_b32 makes about 2^N calls; depth > 32 does not bound that usefully. run_analyze on that body, debug build, best of three:
| levels | main 568c5e5 | this PR | this PR with a memo |
|---|---|---|---|
| 12 | 0.9 ms | 17 ms | 0.9 ms |
| 16 | 1.0 ms | 297 ms | 1.2 ms |
| 20 | 1.1 ms | 5.8 s | 2.2 ms |
A chain from a value that is not zero is refused at the first level, and stays fast (2.3 ms at 20).
shared_dag_eval_val_stays_linear_time guards the same thing for eval_val. I have no real program with such a chain; it is a synthetic case. The memo I tried is a RefCell<HashMap<(Value, Option<Bool>), Option<(Option<Value>, [u8; 32])>>> around counter_bits, keyed by the value and use_lanes. The 28 emit::lds tests pass with it, three of them mine, and the check below accepts the same number of programs. (It also remembers a None that came from the depth limit, which only refuses more.) A test, to sit beside the existing one:
let mut inner = vec![v_mov_k(2, 0), v_mov_k(3, 5)];
for _ in 0..20 {
inner.push(0x3800_0000 | 2 << 17 | 2 << 9 | 258); // v_or_b32 v2, v2, v2
}
inner.extend(ds_write_b32(2, 3, 0));
inner.extend(ds_read_b32(4, 2, 4));
inner.extend(store_uav(4));
let t = std::time::Instant::now();
let _ = run_analyze(&if_v0_lt_s0(&inner), [64, 1, 1], 1);
assert!(t.elapsed().as_secs() < 2, "{:?}", t.elapsed());
The strided proof held against brute force: 20,000 random loops (groups of 32, 64, 96 and 128, in one and two dimensions; one to three shifts, masks and ORs on the counter; random step, start and offsets; accesses of 1, 4, 8 and 16 bytes). 1,222 were accepted. Simulating 24 passes of every thread, none of them lets two threads touch one byte; 16,419 of the 18,778 refused ones do. A step or a start that only some lanes take is refused.
Also: cargo test -p victory-formats -p victory-shader --lib: 57 and 279 passed (three of them mine), 0 failed.
Not run: Windows, any card, the game's shaders. The same-constant and lane-mask proofs were read, not fuzzed.
AI-assisted review (Claude Code).
|
To help address @Melow-code's
What it changes (
|
Synthetic shared DAG (run_analyze) |
82221d1 (before) |
d6f46fd (with memo) |
|---|---|---|
20-level v_or_b32 v2, v2, v2 (2^20 calls) |
1.64 s | 1.50 ms |
40-level v_or_b32 v2, v2, v2 (crosses depth > 32) |
— | 0.75 ms |
13-level v_or3_b32 v2, v2, v2, v2 (3^13 calls) |
2.65 s | 0.35 ms |
20-level v_or3_b32 v2, v2, v2, v2 (3^20 calls) |
— | 0.88 ms |
Verification
cargo test --locked -p victory-shader --lib -- emit::lds::tests --test-threads=1: 25 passed, 0 failed (0.17 s).cargo clippy --locked -p victory-shader --lib --tests: zero warnings inlds.rs.
AI-assisted. Synthetic tests only.
d6f46fd to
82221d1
Compare
|
Checked d6f46fd: the finding from my review is fixed.
Read only: the cycle guard. A Not run: Windows, any card, the game's shaders. AI-assisted (Claude Code). |
|
@Melow-code @steven-seagull27 I've left the memo out for now, but I'm open to bringing it in and would like your view before this is merged. My concern is whether this case is realistic for the game. The chain in the test is the same register ORed with itself 20 times, which I wouldn't expect a compiler to emit, and the full retail set (2,675 programs) translates through this PR without stalling. Even if a shader did hit it, the cost would be a one-time translation delay, not wrong output. Have either of you seen address math in a real shader, from this title or another, that shares values deeply enough for this to matter? If so, I'm glad to bring it in here. If not, I'd lean towards landing it on its own later, next to the Apologies if I've misunderstood anything. |
|
@Remorc-e no, I have not seen it in a real shader, from this title or another. I do not have the title's programs, and my case was synthetic, made by reading the code. You read it right: it can only cost translation time, not a wrong result. With the 2,675 programs translating without a stall, leaving it out here and landing it later next to the AI-assisted (Claude Code). |
…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>
|
Hardware check of this PR's pattern on a gfx1036 oracle (native wave64), against the 32-lane result on an RTX 5080 and a Radeon 680M. Hand-written wave64 cases matched in every case. On main those cases only translated through the folded form; with this PR they go to the plain split. Rebased onto current main together with the other LDS proof PR, the #3441 is built on this PR and #2358/#1773 (the two conflict with each other; the resolution is in #3441). It adds two more proofs and takes the refused/folded count from 181 (main) via 162 (this pair) to 139. AI use: Claude Code (Claude Opus 5.5). |
…ix (Wave32 LDS Safety Proofs) trustno1-coder#1773 by Remorce, head 82221d1.
Follow-up to #43. On NVIDIA (32-lane waves), #43 requires proof that a wave64 program's LDS accesses are safe before splitting it across two 32-lane host waves. The water classification and light-probe variance filter shaders use safe patterns the analysis couldn't prove yet. On current main they translate only through a last-resort fallback: the wave-wide form, or failing that the write-only shortcut from before #43. This adds three proofs to #43's analysis that cover those patterns. With them, both shaders are lowered by exact proof in the normal split form and no longer go through the wave-wide fallback or the write-only shortcut. Every other shader's output is unchanged.
Summary
emit/lds.rs:VICTORY_LDS_WRITE_ONLY_OKis untouched.Details
Values made under the write's own lane mask
eval_valtreats any guarded definition as unknown. Sov_mov v11, 0; v_mov v14, 1; ds_write_b32 v11, v14 offset:4under onev_cmpxmask leaves the address and data unknown, and the region is "unproven".A guarded definition now counts as known when every lane of the LDS instruction holds its guard. This is checked with
Bools::meet, which answers "may meet" when it runs out of budget, so the check fails safe. The eval memo is keyed by the lanes looked at.One constant per dword
Take a write-only region where every write puts a known constant at a constant address, with the same constant wherever two writes meet. It ends with the same bytes in any order of the two host halves, however often a loop repeats it, and nothing in the region reads those bytes. Such a region is now accepted even when it contains a loop. This is the "any lane sets the flag" idiom in
CS_Classification_Water.Loop counters stepping by the group
CS_VarianceFilterAlphaloads a tile into LDS withi = tid; while (...) { lds[(i << 2) + 0] = ...; lds[(i << 2) + 1280] = ...; i += 64 }.The proof: for a counter that starts at the thread's linear index and steps by a multiple of the group size
n(a power of two), threadt's counter staystmodulon. With addresses of the form(counter << s) + off, takem = n << s. If eachoff % mand width fit in one slot, threadtonly touches bytes[t << s, (t + 1) << s)modulom, in every pass.mdivides 2^32.The shader rebuilds
ifrom three bit fields before shifting it. The counter's bits are followed through shifts, masks,v_bfe_u32and ORs of disjoint bits, so that reshuffle resolves toi << 2.What stays refused
In
InstanceCulling:CS_Tree_BatchCull*andvfx_proj_decal_vbuffer:CS_ProjDecalReduceClassification_Scalarized, one lane-divergent region covers most of the program. It has an LDS atomic early on and a read of the same address later, with nowhere to put a barrier. One half can read before the other half's atomics land. That looks like a real hazard rather than a gap in the proof, so they stay refused by default.Results
Retail shaders,
victory translate <game dir> --listwithVICTORY_HOST_WAVE=32, from nick0451's run with this PR merged onto maind148613.Now lowered by proof, no longer through the wave-wide or write-only fallback:
water_shading:CS_Classification_Water(0e366d15868e17bd)indirect_light_probes:CS_VarianceFilterAlpha(5964b734c70b91eb)Take an earlier, exact form:
sr_tracing:CS_Tiling_Generate_Waterand_Alpha: from local waves (APPROXIMATE) to local waves (idiom).water_shading:CS_VolumePortalTiling_Generate: from the "without skips" retry to "without early-outs".TAA2:CS_TAA2_PrepareFrameStats: accepted as lane-private, the same loop pattern as the light probes.Unchanged: the other 2,669 programs' emitted output, byte-for-byte.
Validation: every emitted compute program passes
dxil.dll's validator. In game (RTX 3090, balcony save over open water), both shaders are translated and bound, the same 4 shaders are refused with and without this PR, and frame rate and visuals match.Tests
Three new tests in
emit::lds::tests, each with cases that must be accepted and cases that must be refused. All three accept cases fail on current main.cargo test -p victory-shader --libpasses (351), and clippy is clean for the change.