fix(shader): trace heap and table address carries formed by s_add_u32 with src_scc #250
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/250"
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 Sheffy007 on 2026-11-27T04:57:24Z.
Refs #258
Summary
Replaced the earlier speculative 32-to-64 index-extension rules (which matched 0 shaders in the title's corpus) with the single
Tracer::scc_carryrule (~35 lines incrates/victory-shader/src/resource.rs) covering all 798"an address into a descriptor heap with an untracked high half"shaders identified by @Sandile-0511's corpus sweep:hiwritten bys_add_u32of constant0andsrc_scc(Operand::Const(253)), in either operand order;hi'sSCCread to be the exact SSAValuewritten by thes_add_u32producinglo;s_add_u32(afterorigincopies) to bebaseitself;basetoTable,ResourceHeap, orSamplerHeap(high.is_none()).s_add_i32, non-zero constants, chained low adds where an earlier carry could be lost, overwritten/joinedSCC, and 64-bitRootType::Descriptorbases refused, and leavesTracerotherwise identical tomain(no slowdown on5e68767498c57eb6).Verification
cargo test -p victory-shader(resource::tests::a_heap_or_table_address_carry_added_as_scc_source_tracks_the_offset: both operand orders, copied base SGPR, waterfallv_readfirstlane_b32offset, and five negatives)cargo test -p victory-gpu a_compute_shader_loads_a_texture_with_scc_carry(Metal GPU execution test)cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warningscargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warningscargo xtask policyImported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T05:41:10Z.
Thanks for this; the untracked-high-half class is the largest compute refusal left in a world frame, so it matters. Before it can merge, the high-half acceptance needs tightening. From reading
offset_high_half:s_lshr_b32,v_lshrrev_b32) and the bit-field extracts are accepted for any amount, as long as the value shares a source with the low terms. Nothing ties the amount to the low half's left shift. Example that is accepted but wrong:lo = base + (idx << 5),hi = addc(0, idx >> 3). The real high dword isidx >> 27. The amount should have to equal 32 minus the matched low term's left shift, and the bit-field likewise.v_mad_u64_u32case does not check the multiplier against the low half's.shares_sourceis reachability over the SSA graph, so a shared user-data SGPR is enough (hi = s9 >> 1againstlo = base + s9*4). A def-use match of the same term would be sound.infer_rootsuses the same test, so a wrong accept there can also promote an SGPR to a heap root.A compiler will probably never emit the mismatched forms, but a wrong accept is worse than a refusal here, and the check is cheap. Please also add a full-corpus comparison by output fingerprint (gained, regressed, changed per stage), not only outcome counts. Two things to know: a local branch of ours overlaps part of this (the plain high-half case), and four compute shaders that translate through that path turned out to depend on the wrong accept in #256, so the comparison is worth doing with that in mind. Note #182 is closed; #258 continues it.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T06:47:42Z.
Rebased onto
main(2a3b03d) and replacedshares_sourcegraph reachability with exact SSA def-use and shift/multiplier matching (Tracer::extract_low_offset/Tracer::offset_high_half):s_lshr_b32/v_lshrrev_b32byr(1..32) now requires the low half's single dynamic addend to be a left shift of the same canonicalized SSA sourcexby32 - r(s_lshl_b32,v_lshlrev_b32,s_lshl1..4_add_u32,v_lshl_add_u32,s_lshl_b64/v_lshlrev_b64, or power-of-two multiply1 << (32 - r)).s_bfe_u32/v_bfe_u32(and signeds_bfe_i32/v_bfe_i32) likewise requireoff + width == 32and a low left shift ofxbywidth(plusoff == 31, width == 1orashr 31for unshifted sign-extension).v_mad_*64_*32andmul_hichecks: Zero-addendv_mad_u64_u32/v_mad_i64_i32ands_mul_hi_*/v_mul_hi_*inoffset_high_halfnow require either the same 64-bit instruction's low output (pair_lo) or a 32-bit low multiply (s_mul_i32,s_mulk_i32,v_mul_lo_*, or<< kfor1 << k) with the exact same canonicalized operand pair[a, b]. Directv_mad_*64_*32high outputs inhigh_half_inner(without anaddccarry) now also requirelow_termsto come from that instruction'spair_lo.histeps through a phi or select (picked/lane_picked), any matching phi or select inlow_termssteps along the same edge/branch so one arm cannot borrow another arm's shift or multiplier.trackandinfer_roots) for a mismatched shift (s9 >> 3vss9 << 5), mismatched bit-fields (width = 3, off = 27andwidth = 12, off = 20vss9 << 5), shared-parameter-only cases (s9 >> 1vss9 * 4and vss9), andv_mad_u64_u32with a mismatched multiplier (s9 * 20vss9 * 12), unrelated source, or missing carry.(base, offset_lo, offset_hi)ensures only mathematically equal 64-bit heap offsets are accepted.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sandile-0511 on 2026-11-27T07:20:20Z.
I ran this branch over a corpus of the title's shaders, since #258 asks for comparisons by output. Two results you will want before more work goes in.
Method. 21,608 containers (every shader the title registered in a run that reached the prologue world), translated with
emit::translate_withand the native host's options, compared by outcome and by a fingerprint of the output. Three comparisons: the first revision3cee03bagainst its own based77e766d; its diff applied to main2a3b03d1, against2a3b03d1; and the current head4b87810(after the rebase of 06:47 UTC) against2a3b03d1. All three give the same result.1. No shader changes outcome or output. The class this PR is for, "an address into a descriptor heap with an untracked high half", has 798 shaders in this corpus (776 compute, 22 vertex; 205 bound from the first world frame on). With the PR all 798 are still refused, with the same message. The branch's own tests pass here (victory-shader: 314 passed, none failed), so the rules work on the shapes they were written for; the title's shaders seem to have other shapes. I am grouping the 798 by what the high half is made of at the refusal site and will post the groups here, in words.
2. One compute shader no longer finishes translating.
5e68767498c57eb6(bound in the world frame) takes 43 ms on main. With this PR it had not finished after 3 minutes in one sweep of the first revision, and it is past a 60 s limit in every sweep since, the current head included. A sample of the process, the same on both revisions, shows the one busy thread inresource::track_graphics→Tracer::flat_address→Tracer::address→Tracer::sum_seen→Tracer::joined_on_taken_edges_seen(recursing at least 12 deep) →Tracer::edge_taken→structure::Bools::meetso the walk over joins seems to visit the same joins again along every path. In the game that would hold a prewarm thread, or the frame thread at the shader's first bind. Every other shader finishes, the slowest in under a second, as on main.
AI-assisted: sweeps and summary done with Claude Code on my own machine; I reviewed the text.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:07:54Z.
Thank you. The four points I raised are answered: the shift and bit-field now have to match the low half, the multiplier is checked, the matching is by exact def-use, and the negatives are in. That is what I asked for.
Two things remain, both from Sandile-0511's run over a corpus of the title's shaders (comment above).
Tracer::sum_seenandjoined_on_taken_edges_seenrecursing at least 12 deep, so the walk seems to visit the same joins again along every path. A translation that does not finish holds a prewarm thread or the frame thread at the first bind. Please fix that and add a test with a ladder of joins that finishes in a few milliseconds.I will not merge it until the second is fixed. Whether the first matters depends on what the groups show.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T08:12:11Z.
Thanks @Sandile-0511 for running the 21,608-shader sweep and catching
5e68767498c57eb6!5e68767498c57eb6):joined_on_taken_edges_seenandheap_phi_basepreviously poppedseenon return without memoizing DAG joins oredge_taken(Bools::meet), turning a chain ofNdiamond phis/adds inflat_address -> address -> sumintoO(4^N)work. Replaced both recursive walks withO(V + E)iterative Tarjan SCC passes precomputed once inTracer::new(Tracer::compute_joinedandTracer::compute_sums, makingjoined_on_taken_edgesandsumO(1)array lookups), memoizedTracer::edge_takenper(pred, block)CFG edge, memoized/boundedhigh_half_inner, and addedresource::tests::deep_diamond_phi_and_add_chains_track_in_linear_time(two 24-diamond2^24-path chains inside and outside a loop).untracked high halfshaders: Very helpful to know the corpus shapes differ from standard 32-to-64 index extension — happy to wait for your breakdown of whathiholds at those refusal sites before extending the high-half rules further.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sandile-0511 on 2026-11-27T08:29:36Z.
The new head, and the 798.
New head
a314e80. Same sweep as before (21,608 containers, by output fingerprint, against2a3b03d1): every shader finishes.5e68767498c57eb6takes 54 ms (43 ms on main), and the slowest shader of the sweep 0.8 s (0.9 s on main). No shader changes outcome or output. So the second point is fixed on this corpus. The 798 are still refused, with the same message.What the high half holds in the 798. One shape, in all of them:
s_add_u32 lo, <base>, <offset>, the base being a descriptor heap's or a table's base (the root parameter's SGPR or a copy of it);s_add_u32 hi, 0, scc: the constant 0 plus the SCC source operand (253), which reads the carry that low add has just left.The pair is then the address of the descriptor load. There is no
s_addc_u32, and no shift, multiplier or phi on the high side:hi:lois the base, whose high word is 0, plus the offset in 64 bits. On main the high-half rules knows_addc_u32, a mask and a bit-field cut; none of them looks at ans_add_u32whose operand is SCC. In the three I read (two compute bound in the world, one vertex) the offset isv_readfirstlane_b32of a per-lane index shifted left by 5: a waterfall loop over a heap index.That it is all 798 is measured, not sampled: a rule that accepts exactly this and nothing else leaves none of them with this refusal. Its conditions:
s_add_u32of the constant 0 and SCC, in either order;s_add_u32writing the low word leaves (that instruction's own write: not a join, not a later instruction's);Kept refused:
s_add_i32as the low add (its SCC is the signed overflow), a constant other than 0, a low add that does not take the base itself (an earlier add's carry would be lost), and a root descriptor's base.What it gains. With that rule on
2a3b03d1, by fingerprint over the whole corpus:The later refusals of the 730: "a descriptor read only as data" 585 (158 in the world), "a lane mask read as data" 86 (19), "a lane mask holding lanes elsewhere read as data: a program input" 20 (2), "a descriptor neither loaded nor built" 10 (2), a buffer's base address read as data 9 (7), and 20 more across nine reasons. So this refusal was mostly standing in front of the next one, and the 585 are what to look at next for the world frame.
The rule itself. I have it with a test (both operand orders accepted, four negatives) and the paragraph for
docs/native-host.md: about 40 lines inresource.rs. I can open it as a small pull request of its own, or you can take the conditions above into this one. I will do whichever the maintainer prefers, so it is not done twice.AI-assisted: sweeps and summary done with Claude Code on my own machine; I reviewed the text.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:53:11Z.
My own corpus run (33,625 containers, compared by output fingerprint, this branch merged onto main
2a3b03d1):No shader gained and no output changed. One regression: compute 5e68767498c57eb6 translates on main and does not finish with this branch (first revision; same shader as in Sandile-0511's run).
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T10:05:28Z.
Thanks @Sandile-0511 for isolating the exact
s_add_u32 lo, <base>, <offset>+s_add_u32 hi, 0, src_sccshape across all 798 shaders!Per @LakshanSivakumar's point ("A rule for a shape the title does not use costs review and risk and gains nothing"), I rebased onto
main(c1b95875), dropped all of the earlier speculative high-half rules soTracerstays identical tomainoutside this check (keeping5e68767498c57eb6atmain's 43 ms), and implementedTracer::scc_carry(d4b2d92a, 35 lines incrates/victory-shader/src/resource.rs) with the exact conditions above:baseis aTable,ResourceHeap, orSamplerHeap(high.is_none());lois written bys_add_u32with one operand equal tobase(afterorigincopies);hiis written bys_add_u32of constant0andOperand::Const(253)(src_scc), in either order, reading the exactSCCvalue written by that lows_add_u32;v_readfirstlane_b32waterfall offset, and five negatives (s_add_i32, non-zero constant, chained low add, overwrittenSCC, and 64-bitRootType::Descriptor) inresource::tests::a_heap_or_table_address_carry_added_as_scc_source_tracks_the_offset, plus a Metal GPU execution test inheap_tests::a_compute_shader_loads_a_texture_with_scc_carry.(Side note for #258: since SM 6.6
ResourceHeapSGPRs are inferred byresource::infer_rootsrather than declared in FSPX metadata, if any of the"a descriptor outside the heaps"shaders only touch their undeclared heap SGPR via this sames_add_u32 + src_sccform without anotherhi == 0load in the same shader,infer_rootsatresource.rs:2533/2573would be the matching place to check.)Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:32:09Z.
Thank you. This is what I asked for: one rule, for the shape that Sandile-0511 found in all 798 shaders, and the earlier rules gone. The five negatives and the GPU test are the right ones.
What is missing is a comparison on this head. My run and Sandile-0511's were on earlier revisions (mine hung on
5e68767498c57eb6). I will rund4b2d92aover my corpus and post the table; I have not done it yet. It can merge if it shows no regression, no hang, and no output change among the shaders that translate today. Sandile-0511, if you have already run it, please post your table here.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T11:54:36Z.
I ran every shader by output on
d4b2d92a(33,625 containers, against mainc1b95875):87 gained, none lost, none changed, and the hang I saw on the first revision (
compute_5e68767498c57eb6) is gone. 7 of the gained ones are in my list of shaders the game binds in the world. That is a clean result. What is left is a game run on my side, which I have not done yet.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T12:35:38Z.
Rebased onto
main(86786a72->15a37b7a) solely to resolve the adjacent test conflict incrates/victory-gpu/src/native/heap_tests.rsafter #264 and #278 landed;crates/victory-shader/src/resource.rsis unchanged fromd4b2d92a.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T13:36:24Z.
This is on main as of
b639c175, merged from the head before your latest push, so GitHub still shows the PR open. Your commits are in the history under your name. Checked as part of a batch of nine PRs on main60baf130: tests, Windows clippy, policy, a full-corpus comparison and one world run (240 s, no crash, no GPU errors, scene colour lit).If your new head has changes beyond a rebase, please rebase it onto main so only those remain; otherwise please close this. Thank you.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T13:47:14Z.
Closing as landed in
78d0ee45(b639c175);15a37b7ahad no changes beyond rebasingd4b2d92a.Pull request closed