shader: accept more loop masks a divergent loop changes in kept bits (#257, part two) #271
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/271"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T07:30:59Z.
What this changes
Refs #257 (part two of the split agreed below; part one is #329, and until it lands this diff includes it)
On top of #329's refusal and its one accept rule, more loop-mask updates are accepted when they keep the bits of lanes that have left, each traced back to the loop header's phi:
s_cselectands_cmovof two values that both keep the header phi's bits;~vccof a compare made in the pass), an ANDN2 or XOR with one that is 0 for them;How it was tested
emit::lower::tests::loop_masks_whose_updates_keep_the_bits_of_lanes_that_left_translate: one accepted program per rule above (except xor copies and restored EXEC, not yet covered) and a near form of each that is refused. Fails on #329 alone (first atcselect_same_phi). Programs assembled withllvm-mc -mcpu=gfx1030.split_wave_testswith Metal Shader Converter 4.0 beta 3 (GPU execution): 35 passed.cargo test -p victory-shader(333 passed),cargo clippy --no-deps -p victory-shader --all-targets -- -D warnings,cargo xtask policy.Checklist
VICTORY_*variable and documented (none added)Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:08:12Z.
Thank you for taking #257 and for saying plainly that the lockstep execution test was skipped on your machine. Making the shape a refusal is the right direction.
Before it can merge:
split_wave_testswith the converter installed, or ask someone who has it to, and put the result in the text. The shape in #257 reads 0 in lane 0 where the console gives 1; the test should show that value before and after.zero_when_exited,trace_preserved_phiandexec_unwideneddecide what stays accepted, so they are where a wrong accept can remain. Please add negatives: an OR with an operand that is not zero for exited lanes; acselectwhose arms come from different phis; an EXEC write that widens (restored from a value saved before the loop); a loop with a second exit. And say why a loop thatis_divergent_loopdoes not find divergent cannot let lanes leave on different passes.This touches the mask code in #261 and the area of the #254 branch, so expect conflicts.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:53:09Z.
My own corpus run (33,625 containers, compared by output fingerprint, this branch merged onto main
2a3b03d1):502 shaders that translate on main are refused with this branch (373 pixel, 129 compute), and 12 compute outputs change. 493 of the 502 carry your new refusal; 4 and 2 are refused for two lane-mask reasons that are not this one; 3 compute shaders that finished on main no longer finish in 300 s, and 6 time out in all. 62 of the 502 are shaders the game binds in a run. Nothing is gained. As it stands this costs too much: please find why shaders hang, and narrow the refusal, before the other points.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T10:11:57Z.
Thanks for running the corpus and for the detailed feedback. I tracked down both the 300 s hangs and the bulk of the false refusals (and addressed points 3 and 4 as well) in
6f56b37:1. Why shaders hung (>300 s)
loop_invariant_mask,trace_preserved_phi,zero_when_exited, andexec_unwidened::saved_in_looppopped visited nodes on return without memoizing results (O(2^N)backtracking across diamond DAGs inside loops), andcheck_one_loop_maskre-traversed the same(loop_idx, want_exec, value)pairs repeatedly. All five now memoize visited outcomes (usingloop_mask_okplus local memo maps), keeping each check linear in the visited IR/CFG slice.2. Why 493+ shaders were falsely refused (narrowing)
Several overly broad checks fired on valid shaders even when
structure.rsalready resolved guards/exits or whenEXECwas used normally:EXECchecks: dropped theEXECchecks fromguard(),Fields::Valu(_)(checkingEXECon every post-loopv_cmp),exit()(s_cbranch_execz/execnz), and theEXECsource operand of loop-restoresop()writes (s_or exec, exec, saved), plus skippedexec == 0edges (is_exec_zero_edge).Atom::Opaque(v)still checks resolved opaque masks.is_divergent_loop: ignoredst.discardsexits, blocked traversal through back-edges to loop headerh(s != h) inreach_b, and cached loop exit reachability (reachable_from) per loop.zero_when_exited/exec_unwidenedfalse positives: removed the single-block restriction (db != inst_b) on intra-loopv_cmp, handleds_and/s_cselect/s_cmov/ intra-loop phis monotonically (includingis_one_when_exited_op), accepted intra-loop phis +xor_copies/st.restored+ pre-discards_wqminexec_unwidened, and restrictedst.othertraversal viais_mask_sopso integer scalar arithmetic is not treated as a lane mask.3. Accept-side negative tests &
is_divergent_loopcompleteness (point 3)neg_or_nonzero,neg_cselect_diff_phis,neg_exec_widens,neg_second_exit) pluspos_or_restore_execina_loop_carried_mask_changing_exited_lanes_is_refused_after_divergent_loop.is_divergent_loop's doc comment: in a structured CFG loop, lanes can only exit on different passes if either an exit condition is non-uniform (sostructure.rsleaves the loop with a divergent exit/latch outsideuniform) or the loop has multiple distinct non-discard exit targets / a side exit before the latch; any loop with a single non-discard exit whose exit condition is uniform keeps all active-at-entry lanes exiting on the same pass.4. Status of points 1, 2, 4
ORlanes in and out / single value with two instances), so I updated the PR description fromCloses #257toRefs #257.run_lockstep's Metal hardware execution path remains skipped locally whilecargo test -j 2 -p victory-shader --locked -- --test-threads=2(314 passed; 0 failed; 3 ignored) andcargo test -j 2 -p victory-gpu --lib split_wave_tests -- --test-threads=2(35 passed; 0 failed) pass.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T10:30:40Z.
Checked both branches merged onto current main: clippy, victory-shader tests and split_wave_tests pass; the GPU part of split_wave_tests is still skipped here. On point 5: once your corpus numbers for
6f56b37are in, I can split this into the refusal with the lockstep retry first and the accept analysis second, if you still want that. #293 touches the same spot in lower.rs, so whichever lands second I will update.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:32:21Z.
Thank you for the quick work, the four negative tests, and for changing "Closes" to "Refs #257".
My corpus run was on the first head (502 newly refused, 62 of them bound by the game, 3 hangs), so it says nothing about
6f56b37f. I will run it again and post the table; I have not done it yet. The explanation of the hangs (nothing memoized, a walk of 2^N paths) sounds right, and the table will show it.Still open: the execution test. The converter is a download from Apple's developer site; if you cannot install it, please ask someone who has it to run
split_wave_testsand post the result. Also open: splitting the refusal from the accept analysis. Your workflow has not run; it needs my approval.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T10:43:56Z.
Ran split_wave_tests with Metal Shader Converter 4.0 beta 3 installed, so with GPU execution and no skips: 35 passed on
6f56b37. The #257 shape on 64 lanes, per-lane value ofv_cndmask_b32 v2, 0, 1, s[8:9]: before (2a3b03d) it is not refused and all 64 lanes read 0; after (6f56b37) the fast path refuses it ("a loop-carried lane mask read after a divergent loop is not supported"), it runs in lockstep, lanes 0..47 read 1 and lanes 48..63 read 0, so lane 0 reads 1 as on the console. The after values are whata_loop_carried_mask_changing_exited_lanes_runs_in_lockstepasserts; the before values come from the same program run against the base lower.rs.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T11:11:23Z.
On splitting: I tried it. The refusal alone (every mask a pass of a divergent loop changes, with the lockstep retry) breaks two of main's tests,
a_mask_of_the_lanes_leaving_a_loop_by_a_uniform_exit_holds_them(the shadow pass) anda_mask_a_32_lane_wave_starts_as_a_pairs_constant_holds_the_lanes_ored_in(the G-buffer's 32-lane programs): both OR EXEC into the mask on each pass, which main translates and the refusal alone would refuse. So I would split it as: first the refusal plus the one accept rule those two need (ans_orof the mask with EXEC, or with a compare made in the pass, keeps the bits of lanes that left), then the rest of the accept analysis (s_cselect/s_cmov, phis inside the loop,s_wqm, restored EXEC). Does that work for you, or would you rather wait for the corpus table on6f56b37first?Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T12:00:33Z.
Thank you for running the converter tests, and for finding that the refusal alone breaks two of main's tests. That is exactly the information I needed.
I ran every shader by output on
6f56b37f(33,625 containers, against mainc1b95875): 503 shaders that translate on main are refused (373 pixel, 130 compute), 13 compute shaders change, nothing is gained, and there are no hangs now (the first head had some). Of the shaders the game binds in the world, 62 are lost. So as one piece this would cost a lot of the world's draws, and I cannot merge it like this.Yes, please split it as you describe: first the refusal plus the one accept rule those two tests need (an
s_orof the mask with EXEC, or with a compare made in the pass), then the rest. For each part, the number I will look at is how many of those 503 stay refused. I have not run this in the game.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T12:06:59Z.
Split as you asked: #329 is part one (the refusal plus the rule for an
s_orof the mask with EXEC or with a compare made in the pass), and this pull request now holds part two on top of it, with a test of each accept rule that fails without part two; until #329 lands this diff includes it. Both keep main's two loop-mask tests and pass split_wave_tests on Metal (35 passed). Not yet covered by a test in part two: xor copies and restored EXEC.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:01:32Z.
Leaving this open: it contains #329's commit, so it carries the same two problems I noted there (the in-loop v_cmp accept, and main's
a_loop_register_that_is_exec_on_every_edge_is_exec_at_the_headtest failing once merged). It can follow #329.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T16:18:00Z.
Rebased onto the updated PR #329 head (
056ac63a, onorigin/mainbe7dc1b5), updated to69434f75:docs/native-host.mdupdate (in-loopv_cmpremoved,exec_unwidened::saved_in_loopacceptingst.activeloop joins ands_and/s_andn2narrowings while limiting single-operand copies tos_mov_b32/s_mov_b64, plusneg_in_loop_v_cmpandneg_exec_widens_s_not).loop_invariant_mask(accepting pre-loop dominating inputs and chains of invariant scalar SOPs inside the loop whose operands remain loop-invariant; nov_cmp), and fixes 64-bit SOP operand lookup to readReg::S(code + half)instead ofReg::S(code)(with ans_and_b64regression test ina_wave64_loop_carried_mask_changing_exited_lanes_falls_back_to_lockstepwheres7mutates whiles6stays invariant, verified to translate erroneously underReg::S(code)and refuse underReg::S(code + half), plus its invariant twin translating).Verification on
69434f75:cargo clippy --no-deps -p victory-shader --all-targets --locked -- -D warnings(passed on macOS host and--target x86_64-pc-windows-msvc)cargo test -p victory-shader --locked(384 passed; 0 failed; 3 ignoredunit tests +5 passedintegration tests)cargo test -p victory-gpu --lib split_wave_tests(47 passed; 0 failedon Metal; run on0761b014)cargo xtask policy(asset policy: 390 files ok)25,985unique containers,wave32_host: true, zero_copy_memory: true, run on0761b014):25,599 ok / 386 refusedvsbe7dc1b5(70 Csnewly refused,0 Ps/Vs/Ms,1 Cschanged output via lockstep; accepts3 Csshaders refused in #329).View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.