Repository navigation
shader: lower s_and_saveexec_b64 if/else diamonds inside a mesh lane skip (Part of #2413) - #3645
Conversation
…skip Inside a lane-skip block, SAVEEXEC with an EXECZ-skipped then arm and an s_andn2_b64 inverse becomes a per-lane if/else on the source mask. The else arm either restores the saved mask after its own EXECZ, or writes only VGPRs and joins at the skip end, where full EXEC is restored. That diamond joins before the skip closes. The then arm may not depend on EXEC, write the saved mask, export, use LDS, barriers or s_sendmsg, store, or branch out of the arm. The else arm must be vector-only and may not read a scalar the then arm writes. Source-built fixtures cover both shapes and each refusal. Part of R0Y00#2413. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keeps the diamond join's diamond_joined line untouched, so this merges cleanly with R0Y00#3645. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukaslw-lab
left a comment
There was a problem hiding this comment.
Reviewed 29404d76 by reading the diff; not built or run.
Verdict: approve. The shape match and the per-lane if/else lowering are right for the census shapes, and the change fails closed as described. No MUST. Two SHOULDs extend the then-arm filter the same way the else arm is already filtered. Both are cheap refusals that can follow.
- SHOULD Then-arm VCC writes are not clobbered at the join (
src/gfx1013_direct_mesh.inl,for (auto r : then_writes) if (r<106)). A VOPC e32, or a VOP3 compare intovcc, puts 106/107 intothen_writes, but onlyr < 106reacheslane_scalar_clobbers. On hardware the compare runs with the then-arm EXEC and writes 0 into the else lanes' bits whenever any lane took the then arm. If the per-lane model carries VCC across the join as a per-lane value, else lanes keep their pre-diamond bit. A per-lane VCC read after the join (v_cndmask,_co_ci) would then differ for else lanes. Either clobber 106/107 too, or refuse VCC destinations in the then arm. - SHOULD The then arm does not refuse cross-lane reads; the else arm does (
name.find("lane")).v_readlane_b32orv_permlane16/x16in the then arm read lanes that are inactive there. On hardware that is defined: the register holds whatever the else lanes held. In a divergent per-lane branch a lane read of an inactive lane is undefined.v_readfirstlanehappens to agree, because both sides take the first active then-lane. Refusing*lane*exceptreadfirstlanein the then arm would keep the fail-closed promise. - Checked, no finding:
- Shape:
0xbefe04c1iss_mov_b64 exec, -1at the skip end. The inverse must bes_andn2_b64 exec, sD, exec, the paired restore must come fromsD, and both EXECZ targets must stay inside the skip. - Then arm: EXEC reads/writes, CMPX, nested EXECZ, DPP, export/LDS/barrier/sendmsg/stores and out-of-arm branches are refused. SGPR and SMEM writes are clobbered at the join, and
sDoverwrites are refused. - Else arm: VGPR-only, with no implicit VCC readers and no then-arm scalars read.
joins_skip_endis set only here. Merging the diamond beforeclose_skip()leaves every other diamond's order unchanged, anddiamond_joined = !join_at_skip_endavoids opening the #3633 gap.
- Shape:
Tests: two positive shapes check the join phi [2, then] [3, else] and the branch direction, plus 10 refusal controls and five mutations. Neither SHOULD case has a control. native_direct_mesh is outside the Windows pr preset filter, so it has only your run on d5970858.
…n arm Review follow-up. A then-arm VCC write runs under the then-arm EXEC and zeroes the else lanes' bits, which a per-lane VCC cannot show after the join. Refuse VOPC, VOP3 compares into VCC, VCC carry-outs and v_div_scale; record SGPR carry-outs as then-arm writes. Refuse cross-lane reads other than v_readfirstlane_b32, as the else arm already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks. Both SHOULDs are in
New controls, each at +0x34: The slot 57 census (454 requests) gives the same result line for every request before and after, so no real then arm hit either refusal. |
lukaslw-lab
left a comment
There was a problem hiding this comment.
Reviewed b0ee8b5b by reading the diff; not built or run.
Verdict: approve. b0ee8b5b fixes both points from our review at 29404d76, and the PR still merges cleanly with main a3bfd019. No MUST.
- NIT No control for a VOP3B carry into an ordinary SGPR pair.
if (carry) { then_writes.insert(...) }(the then-arm loop insrc/gfx1013_direct_mesh.inl) is the only place that keeps the else arm from reading it, and no test fails if that line goes. Asaveexec-else-reads-thenvariant withv_add_co_u32_e64 v1, s[30:31], ...in the then arm andv_mov_b32 v2, s30in the else arm would cover it. - Checked, no finding:
- VCC: the then arm refuses VOPC, scalar destinations 106/107,
carry_destination≥ 106 andv_div_scale*.carry_destinationdoes not listv_mad_i64_i32, but the mesh frontend does not lower it, so it is refused anyway. - Cross-lane:
*lane*exceptv_readfirstlane_b32. readfirstlane writes an SGPR, so it lands inthen_writesand is clobbered at the join. - Merged with #3646: the scalar gap check runs only when
diamond_joinedis set. A join at the skip end sets it false and closes the skip right after, so the #3633 gap is not opened. A join at aforwardtarget still resets it. - Merged with #3635 / #3655:
s_movk_i32ands_xor_b32in the then arm go through the SOPK/SOP2then_writespath.v_bfm_b32is VOP3, which the else arm allows.
- VCC: the then arm refuses VOPC, scalar destinations 106/107,
Tests: four new refusal controls at +0x34 reach the two new refusals (VOPC, VOP3 compare, VOP2 carry, readlane). Your mutation run shows each clause is needed. CI at this head ran before #3644 put native_direct_mesh into the pr filter, so these controls have not run on CI yet. A rerun or a merge of main would cover them.
R0Y00
left a comment
There was a problem hiding this comment.
Reviewed the paired-EXECZ and enclosing-skip-end shapes, including the saved-mask, cross-lane and side-effect guards. ctest --test-dir <build> --no-tests=error -j1 -R '^native_direct_mesh$' --output-on-failure passed. Two extra source-built v_mov_b32 cases reading exec_lo or exec_hi in the else arm were refused as unproven entry registers 126 and 127.
Part of #2413 (claim: #2413 (comment)).
The
s_and_saveexec_b64handler from #3638 covers the instruction outside a lane skip. Inside one, the C++ mesh frontend still refused it asunsupported mesh instruction or EXEC state: s_and_saveexec_b64. On main that is the first refusal for 47 of the 454 cached slot 57 requests in my local census.All 47 are a compiler if/else:
s_and_saveexec_b64 sD, sS,s_cbranch_execzover the then arm, thens_andn2_b64 exec, sD, execfor the else arm. The else arm takes one of two shapes:s_cbranch_execz, and its target restoress_mov_b64 exec, sD. This is the same join the existings_and_b64gather diamond uses.s_mov_b64 exec, -1restores full EXEC.Change (
src/gfx1013_direct_mesh.inl)A new
s_and_saveexec_b64case for an open lane skip. It lowers the if/else as a per-lane branch onsSwith the existingDiamond/lane_elsemachinery.sDgets the parent EXEC, recorded as a saved mask, so the inverse and the paired restore resolve to it.For the skip-end shape, the diamond is merged before the skip closes. The skip-close block is now a lambda, and a
Diamond::joins_skip_endflag reorders the two. That flag is only set here, so the order for every existing diamond is unchanged. Such a join also does not open the shader: accept scalar work between a mesh diamond join and its EXEC restore (Part of #3416) #3633 restore gap.Fails closed with a named refusal:
mesh SAVEEXEC then arm depends on EXECmesh SAVEEXEC then arm has a wave or memory side effects_sendmsg,s_endpgm, PC ops, buffer/image store or atomicmesh SAVEEXEC then arm branches out of the armmesh SAVEEXEC then arm overwrites the saved EXECsDmesh SAVEEXEC else arm is not vector-onlymesh SAVEEXEC else arm reads a scalar the then arm writesTests (
tests/native_direct_mesh_check.py)saveexec-skip-endandsaveexec-paired:v1is 2 in the then arm and 3 in the else arm. The test asserts the join phi[2, then] [3, else]and that the mask branch goes to the then arm.saveexec-then-scalar: an SGPR written and used only in the then arm is accepted.v_cndmask;Mutation checks, each caught by
native_direct_mesh:I ran these on the branch before I merged main with #3633; the only change since then is where the skip close sits after the join.
Verification
ctest --preset release -R "mesh|compile_dxil" -j 4on top of maind5970858: 204 passed (the twomesh_two_exitscases skip, as on main).s_xor_b32(shader: lower s_xor_b32 in the mesh frontend, with s_movk_i32/s_xor_b32 tests (Part of #2413) #3635).then arm depends on EXEC(a nested CMPX/EXECZ in the then arm).graphics register undefined on an incoming path(v10–v13). Those are VGPRs written in both arms and read by the position export after the vertex-count exchange, under a different guard. That is the existing refusal class, already the most common one on main; it is not part of this PR.🤖 Generated with Claude Code