Skip to content

Shaders: trace uniform v_mov_b32 and v_readfirstlane_b32 copies in resource tracking - #2103

Open
fuzeofficlal wants to merge 2 commits into
Marvisak:mainfrom
fuzeofficlal:fix/shader-resource-copysource-vmov-728
Open

fuzeofficlal wants to merge 2 commits into
Marvisak:mainfrom
fuzeofficlal:fix/shader-resource-copysource-vmov-728

Conversation

@fuzeofficlal

@fuzeofficlal fuzeofficlal commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Scope: preventive. It targets part 4 of #728 (uniform values moved through v_mov_b32 / v_readfirstlane_b32, and waterfalls where only some descriptor dwords vary); an offline census of the title's shaders shows no count change, so it can be held until a title needs it.

Refs #728.

Summary

  • Sound uniform VALU copy propagation (Tracer::copy_source / unguarded_copy): follow plain unguarded v_mov_b32 and v_readfirstlane_b32 copies only when the source holds a wave-uniform scalar or constant (!self.varies(t)), every lane reaching the block executes the instruction (lanes.guards[b][i].is_none()), and v_readfirstlane_b32 reads no lane outside the block's reach.
  • Mixed uniform/varying descriptor support (classify / first_lane_words / waterfall): when a subset of descriptor dwords folds to wave-uniform values while remaining dwords still vary across lanes, retain waterfall loop verification over the varying dwords instead of failing with "a descriptor partly read from the first lane".

Testing

  • Added positive unit and GPU/WARP dxil.dll-validated tests (uniform_values_moved_through_vgprs_and_readfirstlane_are_tracked, uniform_vgpr_readfirstlane_descriptor_runs_on_gpu_and_warp) across NVIDIA RTX 5090, AMD Radeon RDNA2, and D3D12 WARP.
  • Added 5 negative soundness tests in divergent_or_masked_vgprs_are_not_folded_through_readfirstlane verifying input VGPRs, source modifiers, EXEC-narrowed moves, non-loop divergent v_readfirstlane_b32, and mixed uniform/varying descriptors.
  • cargo test -p victory-shader, cargo clippy --workspace --all-targets --all-features -- -D warnings, and cargo xtask policy.

…source

Follow plain unguarded `v_mov_b32` and `v_readfirstlane_b32` copies in
`Tracer::copy_source` only when the source holds a wave-uniform scalar or
constant (`!self.varies(t)`), every lane reaching the block runs the move
(`lanes.guards[b][i].is_none()`), and `v_readfirstlane_b32` reads no lane
outside the block's reach.

Uniform table bases, offsets, and descriptors spilled or shuttled through VGPRs
and read back via `v_readfirstlane_b32` now trace directly to their scalar or
constant origin without demanding a waterfall loop, while truly per-lane or
`EXEC`-narrowed VGPR values remain divergent (`varies == true`) and preserve
all existing waterfall and partial-write checks. For descriptors where only a
subset of dwords still varies across lanes, `classify`, `first_lane_words`, and
`waterfall` keep proving the varying dwords alongside the uniform ones.

Refs Marvisak#728

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

146 passed / 0 failed / 2 ignored. Uniform v_mov_b32 and v_readfirstlane_b32 copy tracing (+2 tests) — uniform-move tracing for the readfirstlane path; relates to #1718's helper-lane readfirstlane fix.

@fiestorra

Copy link
Copy Markdown
Contributor

Ran the census: it changes nothing on this archive — which makes the evidence question the important one

A 585-line change to address tracking is a change whose value should be visible somewhere, so I measured the place this tree usually measures:

main 940d32f:  validated 23,918 of 26,084   · 871 Flags must match usage  · 195 SGPR addresses  · …
this branch:   validated 23,918 of 26,084   · 867 Flags must match usage  · 195 SGPR addresses  · …

(same local 1.0.2.1 archive, 26,503 containers, validator SDK 10.0.26100 dxil.dll 1.9; the 871 → 867 difference is the four programs that #1767's note: reporting rewrites as a different class in my run, not a change of outcome — the totals and the class membership are the same.)

So: no program that failed before passes now, on this corpus. That is not an argument against the change — it refs #728, which is a specific pattern — but it means the description has no measurable "before/after" available here, and a reviewer should ask for the evidence that does exist:

  • which program or pattern does this fix, and does that pattern occur in the title's shaders at all? If it does, one container plus a before/after line is worth more than the change itself; if it does not, then say "preventive", because that word decides whether it should be merged now or held until a title needs it;
  • and if it does occur, does the emitted code differ in a way that would show in translate --count (CLI: translate --count counts the instructions of the emitted shaders #2115) — extra or fewer instructions — even when both versions validate? That is the only census-visible trace a change like this can leave when the modules already validated.

What I can vouch for is the shape of the reasoning, which is the part I looked at hardest because it is where a wrong uniform copy turns into a wrong descriptor for every lane:

  • the copy is followed only when the source does not vary (!self.varies(t)) — the condition that makes the whole thing sound;
  • only when every lane reaching the block executes the instruction (lanes.guards[b][i].is_none()), which is what keeps a guarded copy from being treated as uniform;
  • and v_readfirstlane_b32 only when it reads no lane outside the block's reach, which is the subtle one: readfirstlane picks the lowest active lane, and if that lane is not one the block executes, the "uniform" value it would propagate is a value from a lane that is not there.

Those three are exactly the conditions I would have written, and they are stated in the description rather than left implicit — which is why I am reviewing this rather than guessing at it.

Two practical notes:

No game data is involved: counts and code.

Written with an AI assistant.

@fiestorra

Copy link
Copy Markdown
Contributor

Correction to the numbers in my comment above: both sides are 867, and the 871 I quoted was mine, not this comparison

I wrote that "the 871 → 867 difference is the four programs that #1767's note: reporting rewrites as a different class". That is wrong, and I should not leave it standing.

The figures for the two builds in this comment are:

main 940d32f        summary of that class: 867   (plus the summary line itself: 868 matches)
this branch         summary of that class: 867   (same)

So this pull request changes nothing in the census — not the class, not the total. The 871 was a number from an earlier census run of mine, taken on a different tree state, and quoting it here made it look as though something moved by four programs when nothing moved at all.

The substantive point of the review is unchanged and is now cleaner: 23,918 validated and the same class counts before and after, so the evidence for this change has to be the specific program or pattern it fixes, not a corpus number. My apology for putting a misleading pair in front of you.

No game data is involved: counts.

Written with an AI assistant.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
Dword 0 of a buffer descriptor is picked per lane by v_cndmask_b32 while
dwords 1-3 are constants reaching v_readfirstlane_b32 through v_mov_b32.
The loop compares dword 0 alone: the descriptor traces to a per-lane
choice of two built descriptors, and the waterfall proof records that one
compare and one first-lane read. Comparing a constant dword instead must
still be refused, since the varying dword is then left unproven.

Refs Marvisak#728
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Thanks for running the census and for the correction — 23,918 before and after with identical class counts matches what I'd expect.

On the evidence question: the pattern is a wave-uniform value (a scalar, a constant, a table base or offset, or individual descriptor dwords) moved into a VGPR with v_mov_b32 and read back with v_readfirstlane_b32 outside a waterfall loop, plus the waterfall case where only some of a descriptor's dwords still differ between lanes. It comes from part 4 of the branch in #728; the gains reported there were for the whole branch, and its other parts have since landed on main separately (0b35b7f, 75677cb). I have no container showing this pattern failing on its own, and your census agrees, so this is preventive — fine to hold until a title needs it. I've said so at the top of the description.

On the mixed-descriptor risk, I pushed a_mixed_descriptor_proves_only_its_varying_dwords (d0a07fce): dword 0 is chosen per lane by v_cndmask_b32, dwords 1–3 are constants that reach v_readfirstlane_b32 through v_mov_b32, and the loop compares dword 0 only. It pins the traced source (a per-lane choice between the two built descriptors), one compare and one first-lane read in the waterfall proof; a variant comparing a constant dword instead must still be refused ("compares do not select the access's lanes"). Without the uniform folding the positive case is refused, and with the !varies condition dropped the source no longer traces per lane, so it covers both under- and over-folding.

cargo test -p victory-shader --locked passes except the three hardware-dependent tests that also fail on main on this non-RDNA2 machine; both clippy modes and cargo xtask policy are clean.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

In resource.rs, Tracer::copy_source now follows v_mov_b32/v_readfirstlane_b32 copies, and first_lane_words/classify take mixed uniform/varying descriptors. The soundness conditions are there:

  • !varies on the source;
  • unguarded_copy checks for no guard and that EXEC stays within the block's reach;
  • tests pin the mixed-descriptor case and over-folding.

However, by the author's own account and a census, no title program changes (23,918 validated before and after). This is a 640-line preventive change to descriptor tracking, where a wrong fold gives every lane a wrong descriptor. It should be held until a title program needs it. A container or pattern from the title showing the failure would change the verdict. CI: passing.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants