From 29404d760cb91ed24c4db3e5d537897748cf0c7e Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Mon, 30 Nov 2026 21:36:51 +0100 Subject: [PATCH 1/5] shader: lower s_and_saveexec_b64 if/else diamonds inside a mesh lane 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 #2413. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 103 ++++++++++++++++++++++++++++-- tests/native_direct_mesh_check.py | 54 ++++++++++++++++ 2 files changed, 153 insertions(+), 4 deletions(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 131b6a4969..4cd69a8e6d 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -180,6 +180,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout unsigned start, alternate, join; Edge entry, first; bool restores_exec=false; + // Joins where the enclosing lane skip ends (an in-skip saveexec else arm). + bool joins_skip_end=false; }; std::optional branch; unsigned lane_else=0; @@ -498,7 +500,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout live=true; } bool closed_nested_skip=false; - if (exec_skip && pc==exec_skip->end) { + auto close_skip=[&]() { const auto tag=to_string(exec_skip->start); ir.body<<" br label %mesh_done"<end) continue; + if (pc==exec_skip->end) return true; closed_nested_skip=true; } - } + return false; + }; + // A saveexec diamond may join where its enclosing lane skip ends. Merge + // the diamond first so the skip closes over the joined register file. + const bool join_at_skip_end=branch && branch->joins_skip_end && exec_skip && pc==exec_skip->end && pc==branch->join; + if (exec_skip && pc==exec_skip->end && !join_at_skip_end && close_skip()) continue; if (branch && pc==branch->alternate) { if (pc==branch->join || lane_else) { branch->first=save_edge(); @@ -569,8 +576,10 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout } lane_scalar_clobbers.clear(); branch.reset(); - diamond_joined=true; + // A join at the skip end is followed by the skip close, not by a saved-mask restore. + diamond_joined=!join_at_skip_end; } + if (join_at_skip_end && close_skip()) continue; // A scheduler may place scalar work between a diamond's join and the restore of the EXEC it saved: // scalar loads, scalar ALU and their waits run once per wave whatever EXEC holds, so the restore still // follows the join. Each must leave EXEC, VCC and control flow alone; the restore must still name the @@ -1289,6 +1298,92 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout (i.name=="s_mov_b64" && i.src[0]<=104 && !(i.src[0]&1))))) { // An open skip cannot execute a write for lanes outside its guard. refuse(pc,"mesh EXEC write inside a lane-skip block"); + } else if (i.name=="s_and_saveexec_b64" && exec_skip) { + // Inside a lane skip the compiler opens an if/else with SAVEEXEC: EXECZ skips the then arm, + // and s_andn2_b64 exec, sD, exec starts the else arm. The else arm either has its own EXECZ + // and restores EXEC from sD (the s_and_b64 diamond shape above), or writes only VGPRs and + // ends at the enclosing skip's end, where full EXEC is restored. Lower it as a per-lane + // if/else on sS. The arms run as one thread each, so neither arm may observe EXEC. + if (full_exec || branch || lane_active.empty()) + refuse(pc,"mesh SAVEEXEC diamond needs one open lane skip"); + if (i.dst>104 || (i.dst&1) || i.src[0]>104 || (i.src[0]&1)) + refuse(pc,"unsupported mesh SAVEEXEC operands"); + const auto a=decode(code,i.next,true,true); + if (a.name!="s_cbranch_execz" || a.target()<=a.next || a.target()>=exec_skip->end) + refuse(pc,"mesh SAVEEXEC then arm lacks an EXECZ skip"); + const auto inv=decode(code,a.target(),true,true); + if (inv.name!="s_andn2_b64" || inv.dst!=126 || inv.src[0]!=i.dst || inv.src[1]!=126) + refuse(pc,"mesh SAVEEXEC else arm lacks the saved-mask inverse"); + unsigned else_begin=inv.next, join=exec_skip->end; + bool restores=false; + if (const auto b=decode(code,inv.next,true,true); b.name=="s_cbranch_execz") { + if (b.target()<=b.next || b.target()>=exec_skip->end) + refuse(pc,"mesh SAVEEXEC else arm leaves the lane skip"); + const auto reset=decode(code,b.target(),true,true); + if (reset.name!="s_mov_b64" || reset.dst!=126 || reset.src[0]!=i.dst) + refuse(pc,"mesh SAVEEXEC else arm does not restore the saved EXEC"); + else_begin=b.next; join=b.target(); restores=true; + } else if (join+4>code.size() || word(code,join)!=0xbefe04c1) { + refuse(pc,"mesh SAVEEXEC else arm does not end at a full EXEC restore"); + } + std::set then_writes; + for (unsigned at=a.next;ata.target()) refuse(at,"mesh SAVEEXEC then arm splits an instruction"); + const bool scalar=op.enc==Encoding::Sop1 || op.enc==Encoding::Sop2 || op.enc==Encoding::Sopk || + op.enc==Encoding::Vop1 || op.enc==Encoding::Vop3 || op.enc==Encoding::Vopc; + if ((scalar && (op.dst==126 || op.dst==127)) || name.rfind("v_cmpx",0)==0 || + name.find("exec")!=string::npos || op.dpp_control || + std::any_of(op.src.begin(),op.src.end(),[](unsigned s) { return s==126 || s==127; })) + refuse(at,"mesh SAVEEXEC then arm depends on EXEC"); + if (op.enc==Encoding::Export || op.enc==Encoding::Lds || name.rfind("s_sendmsg",0)==0 || + name=="s_barrier" || name.rfind("s_endpgm",0)==0 || name.find("pc_")!=string::npos || + ((op.enc==Encoding::Mubuf || op.enc==Encoding::Image) && + (name.find("store")!=string::npos || name.find("atomic")!=string::npos))) + refuse(at,"mesh SAVEEXEC then arm has a wave or memory side effect"); + if (op.branch() && (op.target()<=at || op.target()>a.target())) + refuse(at,"mesh SAVEEXEC then arm branches out of the arm"); + if (op.enc==Encoding::Smem) { + for (unsigned j=0;jjoin || name.find("cndmask")!=string::npos || + name.find("_co_")!=string::npos || name.find("div_")!=string::npos || name.find("lane")!=string::npos) + refuse(at,"mesh SAVEEXEC else arm is not vector-only"); + // VOP2's second source is always a VGPR. + for (unsigned j=0;j<(op.enc==Encoding::Vop3 ? 3u : 1u);++j) + if (then_writes.count(op.src[j])) + refuse(at,"mesh SAVEEXEC else arm reads a scalar the then arm writes"); + at=op.next; + } + const auto take=alu.cmp("ne",alu.graphics_lane_mask(i,state,i.src[0],"unproven mesh SAVEEXEC mask"),"0"); + // sD holds the parent EXEC in both arms. The inverse and the restore read it. + saved_exec[pc]={false,lane_active,wave_active,exec_value[0],exec_value[1],alu.graphics_registers,partial_writes}; + alu.write(i.dst,exec_value[0]); alu.write(i.dst+1,exec_value[1]); + state[i.dst]={Symbol::Type::lane_mask,pc,0}; + state[i.dst+1]={Symbol::Type::lane_mask,pc,1}; + alu.write(1024+pc,ir.value("zext i1 "+lane_active+" to i32")); + alu.write(128,{}); + branch=Diamond{pc,a.target(),join,save_edge(),{},restores}; + branch->joins_skip_end=!restores; + for (auto r : then_writes) if (r<106) lane_scalar_clobbers.insert(r); + lane_scalar_clobbers.insert(128); + lane_else=else_begin; + ir.body<<" br i1 "< Date: Mon, 30 Nov 2026 22:28:41 +0100 Subject: [PATCH 2/5] shader: refuse VCC writes and cross-lane reads in a mesh SAVEEXEC then 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 --- src/gfx1013_direct_mesh.inl | 10 ++++++++++ tests/native_direct_mesh_check.py | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 4cd69a8e6d..2174f6644f 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -1337,6 +1337,16 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout name.find("exec")!=string::npos || op.dpp_control || std::any_of(op.src.begin(),op.src.end(),[](unsigned s) { return s==126 || s==127; })) refuse(at,"mesh SAVEEXEC then arm depends on EXEC"); + // A VCC write runs under the then-arm EXEC and zeroes the else lanes' bits, which a per-lane + // VCC cannot show after the join. Cross-lane reads would see lanes inactive in this arm; + // only readfirstlane agrees, because both sides pick the first then-arm lane. + const auto carry=carry_destination(op); + if (op.enc==Encoding::Vopc || (scalar && (op.dst==106 || op.dst==107)) || + (carry && *carry>=106) || name.rfind("v_div_scale",0)==0) + refuse(at,"mesh SAVEEXEC then arm writes VCC"); + if (carry) { then_writes.insert(*carry); then_writes.insert(*carry+1); } + if (name.find("lane")!=string::npos && name!="v_readfirstlane_b32") + refuse(at,"mesh SAVEEXEC then arm reads another lane"); if (op.enc==Encoding::Export || op.enc==Encoding::Lds || name.rfind("s_sendmsg",0)==0 || name=="s_barrier" || name.rfind("s_endpgm",0)==0 || name.find("pc_")!=string::npos || ((op.enc==Encoding::Mubuf || op.enc==Encoding::Image) && diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index b2a98ce429..e78e7fab93 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -624,6 +624,14 @@ def saveexec_open(then, other): ('saveexec-then-cmpx', [0x7da40081] + then_v1, else_v1, 0x34, 'mesh SAVEEXEC then arm depends on EXEC'), ('saveexec-then-lds', [0xd8340008, 0x500] + then_v1, else_v1, 0x34, # ds_write 'mesh SAVEEXEC then arm has a wave or memory side effect'), + ('saveexec-then-vopc', [0x7d080015] + then_v1, else_v1, 0x34, # v_cmp_gt_i32_e32 vcc, s21, v0 + 'mesh SAVEEXEC then arm writes VCC'), + ('saveexec-then-vop3-vcc', [0xd484006a, 0x00020015] + then_v1, else_v1, 0x34, # v_cmp_gt_i32_e64 vcc + 'mesh SAVEEXEC then arm writes VCC'), + ('saveexec-then-carry', [0x50020281] + then_v1, else_v1, 0x34, # v_add_co_ci_u32_e32 v1, vcc, 1, v1, vcc + 'mesh SAVEEXEC then arm writes VCC'), + ('saveexec-then-readlane', [0xd760001e, 0x00010101] + then_v1, else_v1, 0x34, # v_readlane_b32 s30, v1, 0 + 'mesh SAVEEXEC then arm reads another lane'), ('saveexec-then-saved-mask', [0xbe960380] + then_v1, else_v1, 0x2c, # s_mov_b32 s22, 0 'mesh SAVEEXEC then arm overwrites the saved EXEC'), ('saveexec-else-reads-then', then_scalar, [0x7e02021e], 0x40, # v_mov_b32_e32 v1, s30 From 7010388403fdf1c147b77bfc65a38112225df14b Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Mon, 30 Nov 2026 23:10:24 +0100 Subject: [PATCH 3/5] shader: accept VCC as the mask of a mesh SAVEEXEC inside a lane skip The compiler often saves EXEC against a compare's VCC directly (s_and_saveexec_b64 sD, vcc) instead of copying it to an SGPR pair first. The shared mask reader already returns this lane's VCC bit, so accept source 106 as well as an even SGPR pair. vcc_hi stays refused. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 3 ++- tests/native_direct_mesh_check.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 2174f6644f..39a6ce090e 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -1306,7 +1306,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout // if/else on sS. The arms run as one thread each, so neither arm may observe EXEC. if (full_exec || branch || lane_active.empty()) refuse(pc,"mesh SAVEEXEC diamond needs one open lane skip"); - if (i.dst>104 || (i.dst&1) || i.src[0]>104 || (i.src[0]&1)) + // The mask is an SGPR pair or VCC; both carry this lane's compare bit. + if (i.dst>104 || (i.dst&1) || (i.src[0]>104 && i.src[0]!=106) || (i.src[0]&1)) refuse(pc,"unsupported mesh SAVEEXEC operands"); const auto a=decode(code,i.next,true,true); if (a.name!="s_cbranch_execz" || a.target()<=a.next || a.target()>=exec_skip->end) diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index e78e7fab93..d58ad41dc3 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -651,6 +651,13 @@ def saveexec_open(then, other): wrong_restore[8] = 0xbefe0418 # s_mov_b64 exec, s[24:25]: not the saved mask saveexec_skip('saveexec-paired-wrong-restore', wrong_restore, '+0x2c: mesh SAVEEXEC else arm does not restore the saved EXEC') + # The compiler also saves EXEC against VCC directly, without the s_and_b64 copy. + vcc_open = [0x7d080015, 0xbe96246a] + saveexec_open(then_v1, else_v1)[3:] # s_and_saveexec_b64 s[22:23], vcc + text = saveexec_skip('saveexec-vcc', vcc_open) + assert re.search(arms, text) and re.search(take, text), text + vcc_hi = vcc_open.copy() + vcc_hi[1] = 0xbe96246b # s_and_saveexec_b64 s[22:23], vcc_hi: not a mask pair + saveexec_skip('saveexec-vcc-hi', vcc_hi, '+0x28: unsupported mesh SAVEEXEC operands') # No diamond follows either write. The generic mask ALU must use # the narrowed lane predicate for the following v1 update. From 9cccc933e381a584d2c2d875d661ba20b1d53e4e Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Mon, 30 Nov 2026 23:24:39 +0100 Subject: [PATCH 4/5] shader: lower an in-skip mesh SAVEEXEC if without an else When the EXECZ target restores sD (s_mov_b64 exec, sD) instead of starting an else arm, the SAVEEXEC opens a plain if. Accept it as a then-only diamond that joins at the EXECZ target. Only scalar loads, waits and clauses may sit between the target and the restore; the existing diamond-join gap lowers them once for the wave. Tests: an if with and without a wait/nop gap (then edge selects 2, the else edge keeps the old v1), and refusals for a SALU gap and a restore from another pair. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 21 +++++++++++++++++---- tests/native_direct_mesh_check.py | 13 +++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 39a6ce090e..432e8c061e 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -1313,11 +1313,24 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout if (a.name!="s_cbranch_execz" || a.target()<=a.next || a.target()>=exec_skip->end) refuse(pc,"mesh SAVEEXEC then arm lacks an EXECZ skip"); const auto inv=decode(code,a.target(),true,true); - if (inv.name!="s_andn2_b64" || inv.dst!=126 || inv.src[0]!=i.dst || inv.src[1]!=126) - refuse(pc,"mesh SAVEEXEC else arm lacks the saved-mask inverse"); unsigned else_begin=inv.next, join=exec_skip->end; bool restores=false; - if (const auto b=decode(code,inv.next,true,true); b.name=="s_cbranch_execz") { + if (inv.name!="s_andn2_b64" || inv.dst!=126 || inv.src[0]!=i.dst || inv.src[1]!=126) { + // An if without an else: the EXECZ target restores sD, after at most scalar loads, waits + // and clauses, which the diamond-join gap below lowers once for the wave. + unsigned at=a.target(); + for (;;) { + const auto g=decode(code,at,true,true); + if (g.name=="s_mov_b64" && g.dst==126 && g.src[0]==i.dst) break; + const string gname(g.name); + const bool gap=(g.enc==Encoding::Smem && (g.op<=4 || (g.op>=8 && g.op<=12))) || + gname=="s_waitcnt" || gname=="s_clause" || gname=="s_nop"; + if (!gap || g.next<=at || g.next>=exec_skip->end) + refuse(pc,"mesh SAVEEXEC else arm lacks the saved-mask inverse"); + at=g.next; + } + else_begin=0; join=a.target(); restores=true; + } else if (const auto b=decode(code,inv.next,true,true); b.name=="s_cbranch_execz") { if (b.target()<=b.next || b.target()>=exec_skip->end) refuse(pc,"mesh SAVEEXEC else arm leaves the lane skip"); const auto reset=decode(code,b.target(),true,true); @@ -1364,7 +1377,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout } if (then_writes.count(i.dst) || then_writes.count(i.dst+1)) refuse(pc,"mesh SAVEEXEC then arm overwrites the saved EXEC"); - for (unsigned at=else_begin;at%[\w.]+) = select i1 %[\w.]+, i32 2, i32 (?P%[\w.]+)\n.*?' + r'= phi i32 \[ (?P=then), %mesh_then_end\d+ \], \[ (?P=old), %mesh_else_end\d+ \]') + for name, gap in (('saveexec-if', []), ('saveexec-if-gap', [0xbf8c3f70, 0xbf800000])): # s_waitcnt; s_nop + text = saveexec_skip(name, saveexec_if(gap)) + assert re.search(kept, text, re.S) and re.search(take, text), text + saveexec_skip('saveexec-if-salu-gap', saveexec_if([0xbe9e0381]), # s_mov_b32 s30, 1 + '+0x2c: mesh SAVEEXEC else arm lacks the saved-mask inverse') + saveexec_skip('saveexec-if-wrong-restore', saveexec_if([], 0xbefe0418), # s_mov_b64 exec, s[24:25] + '+0x2c: mesh SAVEEXEC else arm lacks the saved-mask inverse') # No diamond follows either write. The generic mask ALU must use # the narrowed lane predicate for the following v1 update. From 319ec35e5f16bc048058f7ea9e9d90ad28b1b890 Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Mon, 30 Nov 2026 23:44:07 +0100 Subject: [PATCH 5/5] shader: clobber VCC at a mesh SAVEEXEC join instead of refusing then-arm VCC writes A then-arm VCC write zeroes the else lanes' bits on hardware, which a per-lane VCC cannot show after the join. Instead of refusing the write, clobber VCC (and its wave words) at the join, as other then-arm scalar writes already are: the arm still reads its own lane's bit, and any VCC read after the join refuses. v_div_scale stays refused. The three VCC-write controls become positives. Two new controls read VCC after the join, one for the if/else form and one for the if-only form; the if-only one fails without the clobber. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 10 +++++----- tests/native_direct_mesh_check.py | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 432e8c061e..d1ef4519d7 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -1352,11 +1352,11 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout std::any_of(op.src.begin(),op.src.end(),[](unsigned s) { return s==126 || s==127; })) refuse(at,"mesh SAVEEXEC then arm depends on EXEC"); // A VCC write runs under the then-arm EXEC and zeroes the else lanes' bits, which a per-lane - // VCC cannot show after the join. Cross-lane reads would see lanes inactive in this arm; - // only readfirstlane agrees, because both sides pick the first then-arm lane. + // VCC cannot show after the join, so VCC is clobbered there: the then arm still reads its own + // lane's bit, and any later VCC read refuses. Cross-lane reads would see lanes inactive in + // this arm; only readfirstlane agrees, because both sides pick the first then-arm lane. const auto carry=carry_destination(op); - if (op.enc==Encoding::Vopc || (scalar && (op.dst==106 || op.dst==107)) || - (carry && *carry>=106) || name.rfind("v_div_scale",0)==0) + if (name.rfind("v_div_scale",0)==0) refuse(at,"mesh SAVEEXEC then arm writes VCC"); if (carry) { then_writes.insert(*carry); then_writes.insert(*carry+1); } if (name.find("lane")!=string::npos && name!="v_readfirstlane_b32") @@ -1401,7 +1401,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout alu.write(128,{}); branch=Diamond{pc,a.target(),join,save_edge(),{},restores}; branch->joins_skip_end=!restores; - for (auto r : then_writes) if (r<106) lane_scalar_clobbers.insert(r); + for (auto r : then_writes) if (r<=107) lane_scalar_clobbers.insert(r); lane_scalar_clobbers.insert(128); lane_else=else_begin; ir.body<<" br i1 "<