Skip to content

shader: merge mesh writes under one CMPX inside a lane skip and restore the guard (Part of #3703) - #3765

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
mumba17:shader/mesh-skip-narrow-restore
Dec 2, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
mumba17:shader/mesh-skip-narrow-restore

Conversation

@mumba17

@mumba17 mumba17 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Part of #3703. Fixes the wall mesh EXEC write inside a lane-skip block (209 programs in my census on the current stack).

Shape

All 209 programs share one shape inside the vertex skip:

  1. save EXEC (the vertex mask) to an SGPR pair;
  2. one v_cmpx_ne_u32 that narrows EXEC, with no s_cbranch_execz after it;
  3. straight-line VALU work under the narrowed mask (it ORs a per-lane result into a register);
  4. s_mov_b64 exec, s[N:N+1] back to the saved vertex mask.

The restore was refused because an open lane skip may not write EXEC.

Change

When that CMPX runs, every register is exact for the skip's guard lanes. So:

  • Merge. A VGPR write under the narrowed mask whose old value is still exact for the guard (unchanged since the CMPX, or itself merged) becomes select(narrowed, new, old). The result is exact for the guard, and stays partial against full EXEC.
  • Restore. A restore of the guard's saved mask keeps every register that is unchanged since the CMPX or merged, and clears the rest. For example, a register first written under the narrowed mask has no value for the other lanes.
  • Region end. An EXECZ skip under the narrowed mask ends the region, so a nested skip's close is never taken for a merge.

These stay refused:

  • a second narrowing before the restore;
  • a restore of a mask saved under the narrowed EXEC.

Tests

Source-built in tests/native_direct_mesh_check.py:

  • skip-narrow-merge: v10 += 1.0 under v0 != 0, restore, then v10 is read and written under the vertex mask. Exactly one more select than the same program with an empty region (skip-narrow-empty).
  • skip-narrow-new-register: v11 first written under the narrowed mask and read after the restore. Refuses graphics register undefined on an incoming path: 267.
  • skip-narrow-twice and skip-narrow-inner-save: the two refusals above.

I broke each part in turn, and a test failed each time:

  • refusing the restore again fails skip-narrow-merge;
  • dropping the per-lane merge fails skip-narrow-merge (undefined ... 266);
  • keeping every register at the restore fails skip-narrow-new-register.

Validation (Windows 11, MSVC, Windows SDK 10.0.26100)

This touches the same EXEC restore as #3760, so whichever lands second has a small conflict there. Because a nested EXECZ skip ends this region, the two never apply to the same restore. I checked the combination, and both test sets pass together.

AI-assisted.

🤖 Generated with Claude Code

…re the guard

Inside the vertex skip, skinned mesh programs save EXEC, narrow it with
one v_cmpx_ne_u32 and no EXECZ, run straight-line VALU work and restore
the saved vertex mask. The restore was refused as "mesh EXEC write inside
a lane-skip block".

When the CMPX runs, every register is exact for the skip's guard lanes.
A write under the narrowed mask whose old value is still that exact value
(unchanged since the CMPX, or itself merged) is now merged per lane with
it, so it stays exact for the guard. A restore of the guard's saved mask
keeps every register that is unchanged since the CMPX or merged, and
clears the rest, such as a register first written under the narrowed
mask. A second narrowing, or a restore of a mask saved under the narrowed
EXEC, stays refused.

Part of R0Y00#3703.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@lukaslw-lab lukaslw-lab 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.

Reviewed 5625b205 by reading the diff; not built or run.

Verdict: changes requested. merged is a set of values, not of registers, so a copy of a merged value into another register passes the restore as exact.

  • MUST The restore keeps a register when its current value is in skip_narrowing->merged (gfx1013_direct_mesh.inl, narrowed_restore loop). The merge branch accepts an old value on the same test (merged.count(before_vec[j])). v_mov_b32 lowers to z = a (gfx1013_direct_compute.inl:5502), so v_mov_b32 v11, v10 under the narrowed mask, after v10 was merged to M, gives v11 = M by name. v11 had no value before, so the merge branch never ran for it. Yet at the restore merged.count(M) keeps it, and later reads under the vertex mask take M for every guard lane. On the GPU, lanes outside the narrowed mask never wrote v11. A later write to v11 would also merge against M as if it were exact. Recording merges per register (std::map<unsigned,string>, kept only while graphics_registers[r] still equals that entry) closes both. A fixture would pin it: narrowed([0x061414f2, 0x7e16030a], after=(0x0614150b,)) (v_mov_b32 v11, v10 after the merge, then read v11) should refuse with undefined on an incoming path: 267, the same as skip-narrow-new-register.
  • Checked, no finding:
    • VCC under the narrowed mask. A v_cmp there ANDs its predicate with lane_active (:1579), so VCC stays exact for every lane after the restore. VGPR writes that were not merged are cleared.
    • The two refusals. A second CMPX leaves lane_active different from skip_narrowing->lane. A mask saved inside the region has the narrowed lane, not the skip guard. Both still reach "EXEC write inside a lane-skip block".
    • A value_limits-tagged register unchanged since the CMPX stays in partial_writes, where the old path dropped it. That refuses more, not less.

Tests: skip-narrow-merge proves one added select and the restore. skip-narrow-new-register, -twice and -inner-save pin the three refusals. None copies a merged value. native_direct_mesh is in the pr preset filter, and CI ran it green at this head.

A merged value is exact for the guard's lanes only in the register it was
merged into. Keying the merges by value let a copy of a merged value into
another register pass the restore as exact. Record the merged value per
register instead, and add the copy fixture from review plus a second merge
into the same register.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mumba17

mumba17 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@lukaslw-lab Thanks, you're right. merged was keyed by value, so a copy of a merged value passed the restore as exact.

Fixed in de321a36:

  • skip_narrowing->merged is now std::map<unsigned,string>: register → the value merged into it.
  • The restore keeps a register only when its current value is still its own merged entry (or unchanged since the CMPX).
  • The merge branch accepts an old value on the same per-register test, so a later write to a copy no longer merges against M.

Tests:

  • skip-narrow-merged-copy is your fixture (v_mov_b32 v11, v10 after the merge, then read v11). It refuses with graphics register undefined on an incoming path: 267. On 5625b205 the same fixture compiles, so it pins the bug.
  • skip-narrow-merge-twice merges into v10 twice and expects exactly two added selects, so a register's own entry still carries through a second merge.

ctest -R "native_direct_mesh|mesh": 210/210 (the two mesh_two_exits hardware tests skip as usual).
Census on the combined stack with this fix: still 845 of 1,569, none lost.

AI-assisted.

@lukaslw-lab lukaslw-lab 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.

Reviewed de321a36 by reading the diff; not built or run. Re-review after our change request at 5625b205.

Verdict: approve. Merges are now recorded per register, which closes the copy case from our MUST. No MUST.

  • de321a36 keeps skip_narrowing->merged as register → merged value.
    • Restore (narrowed_restore, the partial_writes loop): a register is kept only if it is unchanged since the CMPX, or if its current value is still its own merged entry. v_mov_b32 v11, v10 after the merge leaves v11 = M with no entry, so v11 is dropped.
    • Merge branch: it accepts an old value on the same per-register test, so a later write to the copy does not merge against M.
  • Checked, no finding:
    • A merged register that is later overwritten without a merge no longer matches its entry, so it is dropped at the restore.
    • Writing the copy back (v_mov_b32 v10, v11 under the narrowed mask) leaves v10 = M by name. That is correct: the lanes outside the narrowed mask still hold the old part of M, and the guard lanes inside hold the copy. The register keeps its own entry.
    • A second merge into the same register replaces its entry, so the register still carries through to the restore.

Tests:

  • skip-narrow-merged-copy is the copy case. It refuses with undefined on an incoming path: 267; per the author, the same fixture compiles at 5625b205, so it pins the fix.
  • skip-narrow-merge-twice expects exactly two added selects for two merges into v10, so it covers the entry update.

native_direct_mesh is in the pr preset filter, and CI is green at this head. The branch merges cleanly with main 158ef794.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants