shader: track kept lanes and partial bits across guarded writes #293
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/293"
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-27T09:06:42Z.
What this changes
Refs #256
Implements lane-aware tracking for guarded and partial VGPR writes (
write_placed,place_bits,write_half,v_fma_mix*, and guardedds_*) so inactive/unwritten bits of a missing value or phi are not silently turned into0when a reader can still observe them:kept_old_of/get_val(..., genuine)separate kept old-value preservation from genuine source readsresolve_kept_and_live_edgesreplaceslive_edges, propagating(reach, !guard_write)constraints backward from genuinekept_reads(widened for quad/wave cross-lane and implicit-LOD texture reads) intophi_ator directVal::Missingrefusalslow16_srcreads working while refusing reads of the full dword or kept bitssafe_wqm_helper_read) are kept accepted only when forward SSA uses cannot reach quad/wave/implicit-LOD ops, discards/branches, or unmasked exports/storesWin-back rules analysis (
Help wantedin #256)win_back_counterexamples_issue_256::rule1). A per-lane guarded write (c2) followed bys_wqmgives a quad-uniform reader guardQuad(c2); ac2lane reads0instead ofoldfrom its!c2neighbor in the same quad.rule2) when helper values flow intoquad_perm/ds_swizzle/ implicit-LODimage_sample; sound only under the restricted forward-sink proof insafe_wqm_helper_read(rule2_safe).How it was tested
cargo clippy --no-deps -j 2 -p victory-shader --all-targets --locked -- -D warnings(passed)cargo test -j 2 -p victory-shader --locked -- --test-threads=2(313 passed; 0 failed; 3 ignoredbenchmarks)cargo xtask policy(asset policy: 369 files ok)cargo test -j 2 -p victory-gpu --lib split_wave_tests -- --test-threads=2(34 passed; 0 failed)cargo test -j 2 -p victory-gpu --lib shader_tests -- --test-threads=2(6 passed; 0 failed)Note: hardware Metal execution tests skip locally because Metal Shader Converter is not installed; corpus fingerprint comparison has not been run locally yet.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:32:32Z.
Thank you. Showing with counter-examples why two of the "win-back" rules are unsound is what #256 needed.
Before it can merge:
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T10:53:27Z.
Added
guarded_write_tests.rsin victory-gpu (runs on Metal; Metal Shader Converter 4.0 beta 3 is installed here now) with the #256 shape as a synthetic compute program:if (v0 < 40) v2 = s_memtime; else v2 = 0;, then every lane stores v2. On main (2a3b03d) it translates and all 64 lanes read 0, so lanes 0..39 lose the timestamp they wrote; with this branch it is refused ("a scalar memory read as data"), and that test fails on main. When lanes 0..39 overwrite v2 under the same vcc before the store, it still translates and runs with 5 in lanes 0..39 and 0 in 40..63; an unguarded overwrite (a7fc47af's case) gives 7 in every lane. Full victory-gpu tests with the converter: 318 passed.One cost I found: if the overwrite recomputes the compare (a second
v_cmp_gt_u32 vcc, 40, v0) instead of reusing vcc, this branch refuses it, because each compare is its own atom in the lane booleans and the analysis cannot see that the two guards are equal. On main that program gives the right values. Your corpus table will show how often this happens; if it matters, folding equal compares into one atom would be a separate change.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T11:58:00Z.
CI fails on
emit::tests::interpolated_f16_writes_the_op_sel_half_with_modifiers(Windows-only, so I missed it here). Its program writes half of v2 withv_interp_p2_f16over v2 =v_interp_p1_f32, then exports all of v2, so the export reads the other half, which is the p1 intermediate. This branch refuses that ("the first half of an interpolation"), as it refuses any read of a kept value without a DXIL form; main makes that half 0. #256 left the 16-bit keep undecided (794 shaders reach it, reads not measured), so I see two ways: (a) keep the refusal and change the test to exportv_cvt_f32_f16of v2, which still checks op_sel, omod and clamp; (b) take the 16-bit keeps (write_half,place_bits,v_fma_mix*) out of this pull request, so it only does the guarded writes of #256, and leave the partial bits to a separate change with its own corpus count. I would do (b) unless you prefer (a).Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T12:00:35Z.
Thank you for the Metal test and for telling me what the change costs (a recomputed identical compare is refused).
Two things: the Windows
checkjob fails one80bced1; please look at it and fix it before anything else. And I need the corpus table on your head (newly refused and bound by the game), because the cost you describe is exactly what the corpus will count. I have not run mine on this head; I will whencheckis green. Folding equal compares into one atom should be a separate pull request.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T12:02:49Z.
I did run the corpus on
e80bced1after all (33,625 containers, against mainc1b95875):506 shaders that translate on main are refused with this change and 463 give different output; nothing is gained. Of the shaders the game binds in the world, 15 are lost. The new refusals I looked at carry many different reasons ("a descriptor built around a table in the code", "a descriptor read only as data", "a carry-in from an untracked mask", "a constant lane mask", "the first half of an interpolation", "barycentrics read directly", and two lane-mask ones), so the tracking reaches far beyond the guarded-write shape of #256. I cannot merge it like this. Please narrow it to the shape the test covers and show that the 462 changed pixel shaders mean the same as before, or say why they should change.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T12:07:11Z.
Pushed
0ab00ca, option (b): the 16-bit and SDWA keeps (write_half,place_bits,v_fma_mix*) are back to main's behaviour, which makesinterpolated_f16_writes_the_op_sel_half_with_modifierstranslate again (I ran its program on macOS without dxc: refused before, translated now; the DXIL checks need the Windows job). That alone does not answer your table: I am now narrowing the rest to the guarded-write shape the test covers, and will come back with why the 462 pixel outputs change and what is left of them.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T12:15:09Z.
Pushed
8c70e7f: the phi liveness walk had become a rewrite oflive_edgesthat remembered six branch conditions instead of main's four. That changes which join edges of every branchy pixel shader count as dead, and so their output, whether or not they have a guarded write; it is back to four, so a program without a kept value should lower as on main. To check that without your dump I traced every program the victory-shader and victory-gpu tests lower: of the 714 that main lowers to one output each, all give main's DXIL byte for byte on0ab00caplus8c70e7f. They are small programs, though, ande80bceddoes not change them either, so this is a likely cause of the 462, not a proven one. The 284 vertex refusals I cannot explain from here: could you rerun on8c70e7fand, for the vertex ones that stay, give the counts by reason and describe one shape in words (which write is guarded, by what, and what reads the kept value)?Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:23:51Z.
Leaving this open. I swept my full corpus (21,600 shaders) with this head (
8c70e7f5) on top of tonight's shader PRs: 604 shaders that translate without it are refused with it (19 compute, 393 pixel, 192 vertex), mostly as "a descriptor built around a table in the code" (219), "the first half of an interpolation" (194) and "barycentrics read directly" (93). It needs those losses gone before it can go in.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T18:02:01Z.
Rebased
#293ontomain(be7dc1b5, head44111d61) and eliminated all locally reproducible regressions:Changes
kept_readscheck fromlive_edges(Lower::resolve_kept_and_live_edges): keepsmain'slive_edgeswalk overread_phis(phi_atwithMAX_KNOWN = 4branch conditions) untouched while checkingKeptWriteorigins over thereaches_keptclosure, propagatingst.reach[q]on non-branch forward phi edges and deduplicating facts beforeMAX_KNOWNtruncation.phireads (Lower::get_val): records genuineis_phireads inkept_readswith the reader's(reach, guard)facts alongside directkept_writesreads (ReadKind::Laneonly receives the lane's own path through the phi; cross-laneQuad/Wavereads still widen/clear facts).v_readfirstlane_b32(Lower::read_kind,exec_nonzero_at):waterfall_readslower literally to the lane's own value (ReadKind::Lane); outside pixel shaders,v_readfirstlane_b32with a dominatingexecz/execnzcheck matching bothloandhi(across scalar copies and loop-invariantEXECphis) is also treated asReadKind::Lane(marked as aGuess:inlower.rsand documented indocs/native-host.md). Without a matching nonzero check (even whenst.guards[b][i]isNone), it staysReadKind::Wave.Local sweep (
25,985containers vsbe7dc1b5,sweep -j 4,wave32_host = true,zero_copy_memory = true)tools/corpus/compare.sh:25669 ok,316 refused—0regressions,0output changes (newly translated: 0,output changed: 0,other reason now: 0).20local regressions seen before these fixes (19pixel"the first half of an interpolation"and1compute): recording the reader's(reach, guard)facts onphireads inget_valrecovers all20; propagating forward-edgest.reach[q]recovers19of the20; the refusals only return when both are disabled.Checks on
44111d61cargo clippy -p victory-shader --all-targets -- -D warnings(host and--target x86_64-pc-windows-msvc): clean.cargo test -p victory-shader --locked:386unit tests passed (3ignored) +5sweep tests passed, including regression tests (verified to fail when their rule is reverted) forphireader facts, forwardreach[q],exec_nonzero_at(wave64exec_hiandguards == None), andwaterfall_reads.cargo test -p victory-gpu --lib guarded_write_tests:2Metal tests passed.cargo run -p xtask -- policy:391 files ok.Request for hashes
Neither
"a descriptor built around a table in the code"nor"barycentrics read directly"regresses in our local 25,985-container corpus. Could you share sample hashes from your sweep for those two shapes (and any remaining"the first half of an interpolation", if any remain after this update) so we can reproduce and fix them?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.