From 29404d760cb91ed24c4db3e5d537897748cf0c7e Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Mon, 30 Nov 2026 21:36:51 +0100 Subject: [PATCH 1/2] 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/2] 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