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
112 changes: 89 additions & 23 deletions src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned,unsigned> 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);
};
Expand Down Expand Up @@ -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<ArmScan> arms{{head.pc,outer.then_begin}};
std::optional<unsigned> scalar_branch, mask_copy_at;
while (!arms.empty()) {
Expand Down Expand Up @@ -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});
Expand All @@ -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)) ||
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -1464,35 +1499,61 @@ 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"<<pc<<", label %mesh_empty"<<pc
<<"\nmesh_empty"<<pc<<":\n br label %mesh_resume"<<pc<<"\nmesh_work"<<pc<<":\n";
} else if (i.name=="s_cbranch_scc0" || i.name=="s_cbranch_scc1" ||
} else if ((branch && saveexec_vcc_tests.count(i.next)) ||
i.name=="s_cbranch_scc0" || i.name=="s_cbranch_scc1" ||
i.name=="s_cbranch_vccz" || i.name=="s_cbranch_vccnz") {
const auto other=i.target();
if ((branch && branch->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;at<other;) {
const auto test=saveexec_vcc_tests.find(i.next);
const bool masked_vcc=branch && saveexec_shapes.count(branch->start) && 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;at<other;) {
const auto next=at+gfx1013::instruction(code,at).size;
if (next<=at || next>other) 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;
else {
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 "<<take<<", label %"<<edge<<", label %"<<fall<<"\n"<<fall<<":\n";
if (masked_vcc) {
pc=jump.next;
continue;
}
} else if (i.name=="s_branch" && (!branch || !branch->lane_else) && i.target()>=i.next && i.target()<code.size() &&
(!exec_skip || i.target()<=exec_skip->end)) {
const auto target=i.target();
Expand Down Expand Up @@ -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 "<<take<<", label %mesh_then"<<pc<<", label %mesh_else"<<pc
<<"\nmesh_then"<<pc<<":\n";
Expand Down
Loading
Loading