From a358540e7129bc5cce87af558e6762419147b271 Mon Sep 17 00:00:00 2001 From: timbot103 <306940367+timbot103@users.noreply.github.com> Date: Wed, 2 Dec 2026 08:55:01 +1000 Subject: [PATCH] shader: lower uniform VCC branches and shared-target nested SAVEEXEC in mesh SAVEEXEC then arms (Part of #3703) --- src/gfx1013_direct_mesh.inl | 112 +++++++++++++++++++------ tests/native_direct_mesh_check.py | 132 ++++++++++++++++++++++++++++-- 2 files changed, 213 insertions(+), 31 deletions(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 6db74c887..d1d6665ce 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -488,13 +488,24 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout } return {}; }; - auto match_saveexec=[&](const Inst &i,unsigned boundary,bool can_join_skip) { + // Indexed by the branch PC, so an edge cannot enter the test after the AND. + std::map saveexec_vcc_tests; + auto match_saveexec=[&](const Inst &i,unsigned boundary,bool can_join_skip,bool can_share_exit,bool parent_has_inverse=false) { if (i.dst>104 || (i.dst&1) || i.src[0]>106 || (i.src[0]&1)) refuse(i.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()>=boundary) + if (a.name!="s_cbranch_execz" || a.target()<=a.next || a.target()>boundary || + (a.target()==boundary && can_join_skip)) refuse(i.pc,"mesh SAVEEXEC then arm lacks an EXECZ skip"); + if (a.target()==boundary && !can_share_exit) + refuse(i.pc,parent_has_inverse ? + "nested mesh SAVEEXEC cannot share exit with an enclosing inverse" : + "nested mesh SAVEEXEC cannot share exit without a direct parent restore"); SaveExecShape shape{i.dst,a.next,a.target()}; + if (shape.alternate==boundary) { + shape.join=shape.end=boundary; + return shape; + } const auto pair=[&](const Inst &op) { return (op.src[0]==126 && op.src[1]==i.dst) || (op.src[0]==i.dst && op.src[1]==126); }; @@ -558,7 +569,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout // Parse only complete regions, innermost first for scalar clobbers. // Their inverse, EXECZ and restore words are never visited as arm instructions. struct ArmScan { unsigned start, at; }; - const auto &outer=saveexec_shapes.emplace(head.pc,match_saveexec(head,end,true)).first->second; + const auto &outer=saveexec_shapes.emplace(head.pc,match_saveexec(head,end,true,false)).first->second; std::vector arms{{head.pc,outer.then_begin}}; std::optional scalar_branch, mask_copy_at; while (!arms.empty()) { @@ -603,7 +614,12 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout if (op.next<=at || op.next>shape.alternate) refuse(at,"mesh SAVEEXEC then arm splits an instruction"); if (op.name=="s_and_saveexec_b64") { - const auto &child=saveexec_shapes.emplace(at,match_saveexec(op,shape.alternate,false)).first->second; + // A direct parent restore discards the child's EXEC. An inverse + // would use it and route the child's inactive lanes into the else arm. + const bool can_share_exit=shape.reset==shape.alternate && + decode(code,shape.reset,true,true).name=="s_mov_b64"; + const auto child_shape=match_saveexec(op,shape.alternate,false,can_share_exit,shape.else_begin!=0); + const auto &child=saveexec_shapes.emplace(at,child_shape).first->second; shape.contains_child=true; scan.at=child.end; arms.push_back({at,child.then_begin}); @@ -630,8 +646,24 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout 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; const auto mask_reg=op.src[0]==126 ? op.src[1] : op.src[0]; + const bool and_exec=op.src[0]==126 || op.src[1]==126; + const bool vcc_test=op.dst==106 && and_exec && + ((name=="s_and_b64" && mask_reg<=104 && !(mask_reg&1)) || + (layout.wave32 && name=="s_and_b32" && mask_reg<106)); + if (vcc_test) { + const auto jump=decode(code,op.next,true,true); + if (jump.name!="s_cbranch_vccnz" && jump.name!="s_cbranch_vccz") + refuse(at,"mesh SAVEEXEC VCC test requires an immediate VCC branch"); + if (jump.target()<=jump.next || jump.target()>shape.alternate) + refuse(jump.pc,"mesh SAVEEXEC VCC branch must jump forward within the then arm"); + if (!scalar_branch) scalar_branch=jump.pc; + saveexec_vcc_tests.emplace(jump.pc,mask_reg); + writes.insert(106); writes.insert(107); + scan.at=jump.next; + continue; + } const bool mask_copy=name=="s_and_b64" && op.dst<=104 && !(op.dst&1) && mask_reg<=106 && - !(mask_reg&1) && (op.src[0]==126 || op.src[1]==126); + !(mask_reg&1) && and_exec; if (mask_copy && !mask_copy_at) mask_copy_at=at; const auto carry=carry_destination(op); if ((scalar && (op.dst==126 || op.dst==127)) || (carry && (*carry==125 || *carry==126 || *carry==127)) || @@ -930,19 +962,22 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout const auto closed=saveexec_shapes.find(branch->start); if (closed!=saveexec_shapes.end() && closed->second.reset) pending_saveexec_restore=branch->start; - // A CMPX region that runs to the end of its enclosing arm joins where that arm ends. - const bool chain_closed=closed!=saveexec_shapes.end() && closed->second.cmpx && !closed->second.reset; + // A nested SAVEEXEC or CMPX region that runs to the end of its enclosing arm + // joins where that arm ends, without a local restore of its own. + const bool shared_exit=closed!=saveexec_shapes.end() && !closed->second.reset && !closed->second.else_begin; + const bool chain_cmpx=shared_exit && closed->second.cmpx; branch.reset(); if (!branch_parents.empty()) { branch=std::move(branch_parents.back()); branch_parents.pop_back(); } // A join at the skip end is followed by the skip close, not by a saved-mask restore. - diamond_joined=!join_at_skip_end; + diamond_joined=!join_at_skip_end && !shared_exit; // The enclosing arm ends here too: visit this address again to close it. - if (chain_closed) { + if (shared_exit) { if (!branch || (pc!=branch->alternate && pc!=branch->join)) - refuse(pc,"mesh CMPX region outlives its enclosing arm"); + refuse(pc,chain_cmpx ? "mesh CMPX region outlives its enclosing arm" : + "nested mesh SAVEEXEC outlives its enclosing arm"); continue; } } @@ -1464,20 +1499,42 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout exec_skip=Skip{pc,i.target(),save_edge(),wave_active.empty(),lane_active,wave_active}; ir.body<<" br i1 "<<(wave_active.empty() ? lane_active : wave_active)<<", label %mesh_work"<lane_else) || (!full_exec && !exec_skip) || allocated || other<=i.next || other>=code.size() || - (exec_skip && other>exec_skip->end)) - refuse(pc,"unsupported mesh scalar branch destination"); - for (unsigned at=i.next;atstart) && test!=saveexec_vcc_tests.end(); + const auto jump=masked_vcc ? decode(code,i.next,true,true) : i; + if (saveexec_vcc_tests.count(pc) || (masked_vcc && forward.count(jump.pc))) + refuse(jump.pc,"mesh scalar branch enters a SAVEEXEC VCC test"); + const auto other=jump.target(); + // A paired VCC test stays within its validated then arm, including + // after allocation or when that arm is followed by an else arm. + if ((!masked_vcc && (allocated || (branch && branch->lane_else))) || (!full_exec && !exec_skip) || + other<=jump.next || other>=code.size() || (exec_skip && other>exec_skip->end)) + refuse(jump.pc,"unsupported mesh scalar branch destination"); + for (unsigned at=jump.next;atother) refuse(pc,"mesh branch target splits instruction"); + if (next<=at || next>other) refuse(jump.pc,"mesh branch target splits instruction"); at=next; } - const bool vcc=i.name=="s_cbranch_vccz" || i.name=="s_cbranch_vccnz"; + const bool vcc=jump.name=="s_cbranch_vccz" || jump.name=="s_cbranch_vccnz"; string value; - if (!vcc) value=alu.read(128); + if (masked_vcc) { + constexpr auto failure="mesh SAVEEXEC VCC branch requires a wave-uniform mask"; + const auto src=test->second; + value=alu.graphics_lane_mask(i,state,src,failure); + if (state[src].type!=Symbol::Type::uniform_mask) { + const auto proof=mask_uniform.find(value); + if (proof==mask_uniform.end() || !covered_by(lane_active,proof->second.first)) refuse(pc,failure); + } + // The arm is reached with nonempty EXEC. Its wave-wide AND is + // nonempty exactly when the proven uniform mask bit is set. + alu.write(128,value); + alu.write(106,{}); alu.write(107,{}); + alu.write(vcc_words,{}); alu.write(vcc_words+1,{}); alu.write(vcc_nonempty,{}); + state[106].type=state[107].type=Symbol::Type::unbound; + } else if (!vcc) value=alu.read(128); else { auto nonempty=alu.graphics_registers.find(vcc_nonempty); if (nonempty!=alu.graphics_registers.end() && !nonempty->second.empty()) value=nonempty->second; @@ -1485,14 +1542,18 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout auto lo=alu.graphics_registers.find(vcc_words), hi=alu.graphics_registers.find(vcc_words+1); if (lo==alu.graphics_registers.end() || lo->second.empty() || hi==alu.graphics_registers.end() || hi->second.empty()) - refuse(pc,"mesh VCC branch lacks a proven guest whole-wave mask"); + refuse(jump.pc,"mesh VCC branch lacks a proven guest whole-wave mask"); value=ir.binary("or",lo->second,hi->second); } } if (vcc || !group_uniform_value(value)) group_flow=false; - auto take=alu.cmp(i.name=="s_cbranch_scc0" || i.name=="s_cbranch_vccz" ? "eq" : "ne",value,"0"); - const auto edge=arrive(other), fall="mesh_fall"+to_string(pc); + auto take=alu.cmp(jump.name=="s_cbranch_scc0" || jump.name=="s_cbranch_vccz" ? "eq" : "ne",value,"0"); + const auto edge=arrive(other), fall="mesh_fall"+to_string(jump.pc); ir.body<<" br i1 "<lane_else) && i.target()>=i.next && i.target()end)) { const auto target=i.target(); @@ -1898,9 +1959,14 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout alu.write(1024+pc,ir.value("zext i1 "+parent.lane+" to i32")); alu.write(128,{}); if (branch) branch_parents.push_back(std::move(*branch)); - branch=Diamond{pc,shape.alternate,shape.join,save_edge(),{},false,!shape.reset,shape.else_begin}; + branch=Diamond{pc,shape.alternate,shape.join,save_edge(),{},false,shape.join==exec_skip->end,shape.else_begin}; branch->masks=std::move(masks); for (auto r : shape.then_writes) if (r<=107) branch->scalar_clobbers.insert(r); + if (shape.then_writes.count(106) || shape.then_writes.count(107)) { + branch->scalar_clobbers.insert(vcc_words); + branch->scalar_clobbers.insert(vcc_words+1); + branch->scalar_clobbers.insert(vcc_nonempty); + } branch->scalar_clobbers.insert(128); ir.body<<" br i1 "<{value}) = icmp {predicate} i32 {value}, 0\n' + r'\s*br i1 (?P=take), label %(mesh_edge\d+), label %mesh_fall\d+', text) + assert dispatch, text + joined = re.search(rf'({value}) = phi i32 \[ 3, %{dispatch[2]} \], \[ 7, %mesh_edge\d+ \]', text) + assert joined, text + assert re.search(rf'select i1 {value}, i32 {joined[1]}, i32 {value}', text), text + + checked_vcc_skip('saveexec-vccnz-uniform-skip', uniform_mask, and_vcc, vccnz, 'ne') + checked_vcc_skip('saveexec-vccz-uniform-skip', uniform_mask, and_vcc_reverse, vccz, 'eq') + scalar_compare_mask = [0xd4c4001c, 0x00010014] # v_cmp_gt_u32_e64 s[28:29], s20, 0 + checked_vcc_skip('saveexec-vccnz-uniform-compare', scalar_compare_mask, and_vcc_reverse, vccnz, 'ne') + layout.write_text('mesh1 128 3 1 2 128 32\nconstants 20 0 2') + checked_vcc_skip('saveexec-vccz-wave32', uniform_mask, 0x876a1c7e, vccz, 'eq') # s_and_b32 vcc_lo, exec_lo, s28 + layout.write_text('mesh1 128 3 1 2 128 64\nconstants 20 0 2') + + choose_scalar = uniform_mask + [and_vcc, vccnz | 2, + 0xbe9e0387, 0xbf820001, # s30 = 7; s_branch to the join + 0xbe9e0384, 0x7e02021e] # s30 = 4; join: v1 = s30 + text = saveexec_skip('saveexec-vccnz-local-diamond', outer_if(choose_scalar)) + joined = re.search(rf'({value}) = phi i32 \[ 7, %mesh_edge\d+ \], \[ 4, %mesh_edge\d+ \]', text) + assert joined and re.search(rf'select i1 {value}, i32 {joined[1]}, i32 {value}', text), text + + exit_test = uniform_mask + else_v1 + [and_vcc, vccnz | 1] + then_v1 + text = saveexec_skip('saveexec-vccnz-then-exit', saveexec_open(exit_test, [0x7e020284])) # else v1 = 4 + taken = re.search(rf'(?P{value}) = select i1 (?P{value}), i32 3, i32 (?P{value})\n.*?' + rf'br i1 {value}, label %(?Pmesh_edge\d+), label %mesh_fall\d+\n.*?' + rf'(?P{value}) = select i1 (?P=guard), i32 2, i32 (?P=skip)\n.*?' + rf'(?P{value}) = phi i32 \[ (?P=skip), %(?P=edge) \], \[ (?P=fall), %mesh_edge\d+ \]\n.*?' + rf'(?P{value}) = select i1 (?P=guard), i32 4, i32 (?P=old)\n.*?' + rf'(?P{value}) = phi i32 \[ (?P=join), %mesh_then_end\d+ \], ' + r'\[ (?P=other), %mesh_else_end\d+ \]', text, re.S) + assert taken, text + assert_position_x(text, rf'phi i32 \[ {taken["old"]}, %mesh_empty\d+ \], \[ {taken["outer"]}, %mesh_done\d+ \]') + + # Also pin a paired-EXECZ else arm (branch->lane_else != 0, after GS_ALLOC_REQ) and a pre-allocation arm. + paired_vcc_then = uniform_mask + scalar_skip + paired_vcc = saveexec_head + [0xbf880000 | len(paired_vcc_then), *paired_vcc_then, + 0x8afe7e16, 0xbf880001, *else_v1, 0xbefe0416, 0xbf800000] + text = saveexec_skip('saveexec-vccnz-paired-else', paired_vcc) + joined = re.search(rf'({value}) = phi i32 \[ 3, %mesh_edge\d+ \], \[ 7, %mesh_edge\d+ \]', text) + assert joined, text + assert re.search(rf'(?P{value}) = select i1 {value}, i32 {re.escape(joined[1])}, i32 {value}\n.*?' + rf'(?P{value}) = select i1 {value}, i32 3, i32 {value}\n.*?' + r'= phi i32 \[ (?P=then), %mesh_then_end\d+ \], \[ (?P=other), %mesh_else_end\d+ \]', + text, re.S), text + pre_alloc_body = outer_if(uniform_mask + scalar_skip) + pre_alloc_skip = [0x7da40081, 0xbf880000 | len(pre_alloc_body), *pre_alloc_body, 0xbefe04c1] + text = run('saveexec-vccnz-pre-alloc', program[3:7] + pre_alloc_skip + program[:3] + program[7:]) + joined = re.search(rf'({value}) = phi i32 \[ 3, %mesh_edge\d+ \], \[ 7, %mesh_edge\d+ \]', text) + assert joined and re.search(rf'select i1 {value}, i32 {joined[1]}, i32 {value}', text), text + + lane_compare_mask = [0xd484001c, 0x00020014] # v_cmp_gt_i32_e64 s[28:29], s20, v0 + saveexec_skip('saveexec-vccnz-lane-mask', outer_if(lane_compare_mask + scalar_skip), + 'mesh SAVEEXEC VCC branch requires a wave-uniform mask') + for name, then, reason in ( + ('vcc-backward', uniform_mask + [and_vcc, vccnz | 0xfffe], + 'mesh SAVEEXEC VCC branch must jump forward within the then arm'), + ('scc-backward', uniform_mask + [0xbf84fffe], 'mesh SAVEEXEC then arm branches out of the arm'), + ('vcc-escape', uniform_mask + [and_vcc, vccnz | 2] + then_v1, + 'mesh SAVEEXEC VCC branch must jump forward within the then arm'), + ('vcc-split', uniform_mask + [and_vcc, vccnz | 1] + lane_compare_mask, + 'mesh branch target splits instruction'), + ('vcc-no-branch', uniform_mask + [and_vcc, 0xbf800000, vccnz | 1] + then_v1, + 'mesh SAVEEXEC VCC test requires an immediate VCC branch'), + ('vcc-enter-test', uniform_mask + [0xbf820001, and_vcc, vccnz | 1] + then_v1, + 'mesh scalar branch enters a SAVEEXEC VCC test'), + ('vcc-enter-test-with-fallthrough', uniform_mask + [and_vcc, vccnz | 1, and_vcc, vccnz | 1] + then_v1, + 'mesh scalar branch enters a SAVEEXEC VCC test'), + ('vcc-use-after-test', uniform_mask + [and_vcc, vccnz | 1, 0x02020285] + then_v1, + 'graphics register undefined on an incoming path: 106'), + ): + saveexec_skip(f'saveexec-{name}', outer_if(then), reason) + saveexec_skip('saveexec-vcc-use-after-join', outer_if(exit_test) + [0x02020285], + 'graphics register undefined on an incoming path: 106') + stale_vcc_body = outer_if(exit_test) + [vccnz | 1, 0xbf800000] + stale_vcc_skip = [0xbeea04c1, 0x7da40081, 0xbf880000 | len(stale_vcc_body), *stale_vcc_body, 0xbefe04c1] + run('saveexec-vcc-stale-branch-after-join', + program[3:7] + stale_vcc_skip + program[:3] + program[7:], + 'mesh VCC branch lacks a proven guest whole-wave mask') + inner_compare = 0x7d080014 # v_cmp_gt_i32 vcc, s20, v0 inner_head = [inner_compare, 0xbe9a246a] # s_and_saveexec_b64 s[26:27], vcc inner_reset = 0xbefe041a # s_mov_b64 exec, s[26:27] @@ -1380,12 +1474,11 @@ def nested_pair(then, other): return inner_head + [0xbf880000 | len(then), *then, inner_xor, 0xbf880000 | len(other), *other, inner_or] - def checked_nested(name, body, paired_inner=False, outer_else=True): + def checked_nested(name, body, paired_inner=False, outer_else=True, shared_exit=False): # This THEN instruction must read the inner join's v1, and write - # under the restored outer THEN mask. ELSE must read the entry v1. - then = body + [0x36020287] # v_and_b32 v1, 7, v1 - outer = (saveexec_open(then, [0x36020283]) if outer_else else - saveexec_head + [0xbf880000 | len(then), *then, 0xbefe0416]) + # under the restored outer THEN mask. A shared exit joins directly. + then = body if shared_exit else body + [0x36020287] # v_and_b32 v1, 7, v1 + outer = saveexec_open(then, [0x36020283]) if outer_else else outer_if(then) text = saveexec_skip(name, outer, guard=0x7da40481) # v_cmpx_eq_u32 1, v2 ssa = r'%[\w.]+' def match(pattern): @@ -1406,8 +1499,10 @@ def match(pattern): inner_other = match(rf'({ssa}) = select i1 {mask}, i32 3, i32 {old}\n')[1] joined = match(rf'({ssa}) = phi i32 \[ {inner_then}, %mesh_then_end{inner_tag} \], ' rf'\[ {inner_other}, %mesh_else_end{inner_tag} \]')[1] - after = match(rf'({ssa}) = and i32 7, {joined}\n')[1] - outer_then = match(rf'({ssa}) = select i1 {outer_mask}, i32 {after}, i32 {joined}\n')[1] + outer_then = joined + if not shared_exit: + after = match(rf'({ssa}) = and i32 7, {joined}\n')[1] + outer_then = match(rf'({ssa}) = select i1 {outer_mask}, i32 {after}, i32 {joined}\n')[1] outer_other = old if outer_else: other = match(rf'({ssa}) = and i32 3, {old}\n')[1] @@ -1419,6 +1514,23 @@ def match(pattern): assert_position_x(text, rf'phi i32 \[ {old}, %mesh_empty\d+ \], \[ {result}, %mesh_done\d+ \]') checked_nested('saveexec-nested-if', nested_if(then_v1)) + shared_then = nested_if(then_v1)[:-1] # Inner EXECZ lands on the outer s_mov_b64 exec, s[22:23]. + checked_nested('saveexec-nested-shared-exit', shared_then, outer_else=False, shared_exit=True) + saveexec_skip('saveexec-nested-shared-inverse', saveexec_open(shared_then, else_v1), + 'nested mesh SAVEEXEC cannot share exit with an enclosing inverse') + saveexec_skip('saveexec-nested-shared-gap', + saveexec_head + [0xbf880000 | len(shared_then), *shared_then, 0xbf800000, 0xbefe0416], + 'nested mesh SAVEEXEC cannot share exit without a direct parent restore') + saveexec_skip('saveexec-vcc-crosses-nested', + outer_if(uniform_mask + [and_vcc, vccnz | len(nested_if(then_v1)), *nested_if(then_v1), *then_v1]), + 'mesh scalar branch crosses a nested SAVEEXEC region') + saveexec_skip('saveexec-vcc-crosses-shared-nested', + outer_if(uniform_mask + [and_vcc, vccnz | len(shared_then), *shared_then]), + 'mesh scalar branch crosses a nested SAVEEXEC region') + saveexec_skip('saveexec-nested-shared-overwrite-outer', outer_if(nested_if([0xbe960380] + then_v1)[:-1]), + 'mesh SAVEEXEC then arm overwrites the saved EXEC') # inner THEN writes s22 + saveexec_skip('saveexec-nested-shared-extra-exec-write', outer_if(nested_if(then_v1 + [inner_reset])[:-1]), + 'mesh SAVEEXEC then arm depends on EXEC') checked_nested('saveexec-nested-pair', nested_pair(then_v1, else_v1), paired_inner=True) unpaired_inner = nested_pair(then_v1, else_v1) del unpaired_inner[5] # Omit the ELSE EXECZ and retain the local restore. @@ -1454,6 +1566,10 @@ def nested_control(name, body, reason, other=else_v1): escape = nested_if(then_v1) escape[2] = 0xbf880002 # Inner EXECZ lands on the outer inverse. nested_control('saveexec-nested-escaping-then', escape, + 'nested mesh SAVEEXEC cannot share exit with an enclosing inverse') + escape_past = nested_if(then_v1) + escape_past[2] = 0xbf880003 # Inner EXECZ lands past the outer inverse. + nested_control('saveexec-nested-escaping-past-inverse', escape_past, 'mesh SAVEEXEC then arm lacks an EXECZ skip') escape = nested_pair(then_v1, else_v1) escape[5] = 0xbf880002 # Inner else EXECZ lands on the outer inverse.