Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 123 additions & 4 deletions src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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<Diamond> branch;
unsigned lane_else=0;
Expand Down Expand Up @@ -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"<<tag<<"\nmesh_done"<<tag<<":\n br label %mesh_resume"<<tag
<<"\nmesh_resume"<<tag<<":\n";
Expand All @@ -522,10 +524,15 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
exec_skip.reset();
if (!skip_parents.empty()) {
exec_skip=std::move(skip_parents.back());skip_parents.pop_back();
if (pc==exec_skip->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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1289,6 +1298,116 @@ 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");
// 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)
refuse(pc,"mesh SAVEEXEC then arm lacks an EXECZ skip");
const auto inv=decode(code,a.target(),true,true);
unsigned else_begin=inv.next, join=exec_skip->end;
bool restores=false;
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);
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<unsigned> then_writes;
for (unsigned at=a.next;at<a.target();) {
const auto op=decode(code,at,true,true);
const string name(op.name);
if (op.next<=at || op.next>a.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");
// 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, 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 (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) &&
(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;j<memory_count(op);++j) then_writes.insert(op.dst+j);
} else if (scalar && op.dst<=107) {
then_writes.insert(op.dst); then_writes.insert(op.dst+1);
}
at=op.next;
}
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;else_begin && at<join;) {
const auto op=decode(code,at,true,true);
const string name(op.name);
// Implicit VCC/EXEC readers and cross-lane moves would see the wave, not this lane.
if (op.dst<256 || (op.enc!=Encoding::Vop1 && op.enc!=Encoding::Vop2 && op.enc!=Encoding::Vop3) ||
op.dpp_control || op.next<=at || op.next>join || 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<=107) lane_scalar_clobbers.insert(r);
lane_scalar_clobbers.insert(128);
lane_else=else_begin;
ir.body<<" br i1 "<<take<<", label %mesh_then"<<pc<<", label %mesh_else"<<pc
<<"\nmesh_then"<<pc<<":\n";
pc=a.next;
continue;
} else if (i.name=="s_and_saveexec_b64" && i.dst<=104 && !(i.dst&1) &&
!branch && !exec_skip && (full_exec || !lane_active.empty())) {
const auto src=i.src[0];
Expand Down
93 changes: 93 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,99 @@ def gap_refused(name, gap):
gap_refused('gap-saved-mask', scalar_gap + [0xbe960380]) # s_mov_b32 s22, 0
gap_refused('gap-exec-read', scalar_gap + [0xbe9c047e]) # s_mov_b64 s[28:29], exec
layout.write_text('mesh1 128 3 1 2 128 64\nconstants 21 0 1')
# Inside a lane skip, SAVEEXEC opens an if/else on s[24:25]. Body words start at +0x24
# (after the outer CMPX and EXECZ); the SAVEEXEC is at +0x2c.
def saveexec_skip(name, body, expected=None, end=0xbefe04c1):
wrapped = [0x7da40081, 0xbf880000 | len(body), *body, end]
return run(name, program[:7]+wrapped+program[7:], expected)
saveexec_head = [0x7d080015, # v_cmp_gt_i32_e32 vcc, s21, v0
0x87987e6a, # s_and_b64 s[24:25], vcc, exec
0xbe962418] # s_and_saveexec_b64 s[22:23], s[24:25]
def saveexec_open(then, other):
# The else arm writes only VGPRs and joins at the skip's full EXEC restore.
return saveexec_head + [0xbf880000 | len(then), *then,
0x8afe7e16, # s_andn2_b64 exec, s[22:23], exec
*other]
then_v1, else_v1 = [0x7e020282], [0x7e020283] # v_mov_b32_e32 v1, 2 / 3
text = saveexec_skip('saveexec-skip-end', saveexec_open(then_v1, else_v1))
arms = r'= phi i32 \[ 2, %mesh_then_end\d+ \], \[ 3, %mesh_else_end\d+ \]'
# The s[24:25] lanes branch to the then arm (2); the others to the else arm (3).
take = r'br i1 %\w+, label %mesh_then(\d+), label %mesh_else\1\n'
assert re.search(arms, text) and re.search(take, text), text
# The paired-EXECZ form restores the saved mask before the skip ends.
paired = saveexec_head + [0xbf880001, *then_v1, 0x8afe7e16,
0xbf880001, *else_v1,
0xbefe0416, # s_mov_b64 exec, s[22:23]
0xbf800000] # s_nop 0 keeps the join inside the skip
text = saveexec_skip('saveexec-paired', paired)
assert re.search(arms, text) and re.search(take, text), text
# A scalar written in the then arm only stays out of the else arm.
then_scalar = [0xbe9e0387, 0x7e02021e] # s_mov_b32 s30, 7; v_mov_b32_e32 v1, s30
assert 'phi i32' in saveexec_skip('saveexec-then-scalar', saveexec_open(then_scalar, else_v1))
# A then-arm VCC write leaves VCC clobbered at the join; the arm itself may still use it.
for name, vcc_write in (('saveexec-then-vopc', [0x7d080015]), # v_cmp_gt_i32_e32 vcc, s21, v0
('saveexec-then-vop3-vcc', [0xd484006a, 0x00020015]), # v_cmp_gt_i32_e64 vcc, s21, v0
('saveexec-then-carry', [0x50020281])): # v_add_co_ci_u32_e32 v1, vcc, 1, v1, vcc
text = saveexec_skip(name, saveexec_open(vcc_write + then_v1, else_v1))
assert re.search(arms, text) and re.search(take, text), text
vcc_after = saveexec_head + [0xbf880002, 0x7d080015, *then_v1, 0x8afe7e16,
0xbf880001, *else_v1,
0xbefe0416, # s_mov_b64 exec, s[22:23]
0x02020285] # v_cndmask_b32 v1, 5, v1, vcc: reads the clobbered VCC
saveexec_skip('saveexec-then-vcc-read-after', vcc_after, '+0x4c: graphics register undefined on an incoming path: 106')
for name, then, other, at, reason in (
('saveexec-then-exec-read', [0xbe9c047e] + then_v1, else_v1, 0x34, # s_mov_b64 s[28:29], exec
'mesh SAVEEXEC then arm depends on EXEC'),
('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-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
'mesh SAVEEXEC else arm reads a scalar the then arm writes'),
('saveexec-else-scalar', then_v1, [0xbe9e0381], 0x3c, # s_mov_b32 s30, 1
'mesh SAVEEXEC else arm is not vector-only'),
('saveexec-else-cndmask', then_v1, [0x02020285], 0x3c, # v_cndmask_b32 v1, 5, v1, vcc
'mesh SAVEEXEC else arm is not vector-only'),
):
saveexec_skip(name, saveexec_open(then, other), f'+0x{at:x}: {reason}')
no_inverse = saveexec_open(then_v1, else_v1)
no_inverse[5] = 0xbf800000 # s_nop 0 in place of the inverse
saveexec_skip('saveexec-no-inverse', no_inverse, '+0x2c: mesh SAVEEXEC else arm lacks the saved-mask inverse')
saveexec_skip('saveexec-skip-end-restores-saved', saveexec_open(then_v1, else_v1),
'+0x2c: mesh SAVEEXEC else arm does not end at a full EXEC restore', end=0xbefe0416)
wrong_restore = paired.copy()
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')
# An if without an else: EXECZ skips the then arm straight to the saved-mask restore,
# with or without scalar waits between them. Untaken lanes keep their old v1.
def saveexec_if(gap, restore=0xbefe0416): # s_mov_b64 exec, s[22:23]
return saveexec_head + [0xbf880000 | len(then_v1), *then_v1, *gap, restore]
kept = (r'(?P<then>%[\w.]+) = select i1 %[\w.]+, i32 2, i32 (?P<old>%[\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')
# Without an else arm only the clobber keeps a then-arm VCC write from reaching a later read.
vcc_if = saveexec_head + [0xbf880002, 0x7d080015, *then_v1, # v_cmp_gt_i32_e32 vcc, s21, v0
0xbefe0416] # s_mov_b64 exec, s[22:23]
assert re.search(take, saveexec_skip('saveexec-if-then-vcc', vcc_if))
saveexec_skip('saveexec-if-vcc-read-after', vcc_if + [0x02020285], # v_cndmask_b32 v1, 5, v1, vcc
'+0x40: graphics register undefined on an incoming path: 106')

# No diamond follows either write. The generic mask ALU must use
# the narrowed lane predicate for the following v1 update.
Expand Down
Loading