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
146 changes: 139 additions & 7 deletions src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,35 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
};
for (const auto &block : emitter.result.constants)
for (unsigned j=0;j<block.count;++j) remember_scalar(block.first_sgpr+j);
// Values equal in every thread of the group: root constants, the group identifier and scalar
// work on them. A merge phi joins them only while every scalar branch so far was taken on a
// group-uniform condition. Scalar loads count until the shader stores to memory.
std::set<string> group_uniform;
bool group_flow=true, memory_stored=false, uniform_join=false;
// Scalar loads whose descriptor table, offset and base were group-uniform.
std::set<unsigned> uniform_descriptor_defs;
auto group_uniform_value=[&](const string &v) {
return !v.empty() && (v.find_first_not_of("0123456789-")==string::npos || group_uniform.count(v));
};
for (const auto &block : emitter.result.constants)
for (unsigned j=0;j<block.count;++j) {
const auto it=alu.graphics_registers.find(block.first_sgpr+j);
if (it!=alu.graphics_registers.end() && !it->second.empty()) group_uniform.insert(it->second);
}
// Unsigned upper bounds between values: v <= each value in at_most[v].
std::map<string,std::set<string>> at_most;
auto bounded_by=[&](const string &low,const string &high) {
std::set<string> seen{low};
std::vector<string> work{low};
while (!work.empty()) {
const auto v=work.back(); work.pop_back();
if (v==high) return true;
auto it=at_most.find(v);
if (it==at_most.end()) continue;
for (const auto &u : it->second) if (seen.insert(u).second) work.push_back(u);
}
return false;
};
std::set<string> wave_bases, low_group, low_lane, whole_lane, linear_threads;
std::set<unsigned> partial_writes;
auto remember_partial=[&] {
Expand Down Expand Up @@ -139,6 +168,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
if (layout.group_stride) {
const auto group = ir.invoke("i32","groupId.i32",94,{{"i32","0"}},"nounwind readnone");
exchanged_group = ir.binary("mul",group,to_string(layout.group_stride));
group_uniform.insert(exchanged_group);
}
unsigned scope = 0;
auto guarded = [&](const string &limit, auto body) {
Expand Down Expand Up @@ -347,10 +377,13 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
alu.check_graphics_read=[&](unsigned reg,const string &value) {
const auto required=value_limits.find(value);
if (required==value_limits.end()) return;
if (exec_skip && exec_skip->per_lane && exec_skip->guard==lane_active) {
// In a per-lane or whole-wave skip on lane_active, lanes outside it execute no effect.
if (exec_skip && exec_skip->guard==lane_active) {
for (auto active=lane_active; !active.empty();) {
const auto limit=lane_limits.find(active);
if (limit!=lane_limits.end() && limit->second==required->second) return;
// A read under a smaller lane count stays inside the lanes that defined the value.
if (limit!=lane_limits.end() && (limit->second==required->second ||
bounded_by(limit->second,required->second))) return;
const auto parent=mask_parent.find(active);
active=parent==mask_parent.end() ? string{} : parent->second;
}
Expand All @@ -366,7 +399,11 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
};
unsigned mask_generation=0x1000000;
auto merge_edges=[&](const Edge &first, const string &l, const string &r, const string &limit=string{}) {
auto phi=[&](const string &a,const string &b) {return ir.value("phi i32 [ "+a+", %"+l+" ], [ "+b+", %"+r+" ]");};
auto phi=[&](const string &a,const string &b) {
const auto v=ir.value("phi i32 [ "+a+", %"+l+" ], [ "+b+", %"+r+" ]");
if (uniform_join && group_uniform_value(a) && group_uniform_value(b)) group_uniform.insert(v);
return v;
};
const bool partial_zero=!layout.texture_heap_slot_map && mesh_partial_zero();
for (auto reg : partial_writes) {
const auto it=alu.graphics_registers.find(reg);
Expand Down Expand Up @@ -449,6 +486,17 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
partial_writes.insert(first.partial.begin(),first.partial.end());
};
auto save_edge=[&]() { return Edge{state,alu.graphics_registers,exchanged_group,partial_writes}; };
// Full EXEC returns: a partial VGPR keeps only a value tagged with the lane count that defined
// it, which check_graphics_read enforces on every later read.
auto keep_bounded_partials=[&] {
std::set<unsigned> kept;
for (auto r : partial_writes) {
const auto it=alu.graphics_registers.find(r);
if (it!=alu.graphics_registers.end() && value_limits.count(it->second)) kept.insert(r);
else alu.write(r,{});
}
partial_writes=std::move(kept);
};
struct Incoming {
Edge edge;
string label, lane, wave;
Expand Down Expand Up @@ -492,7 +540,9 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
ir.body<<" br label %"<<join<<"\n"<<inputs[j].label<<":\n br label %"<<join<<"\n"<<join<<":\n";
const auto first=save_edge();
restore(inputs[j].edge);
uniform_join=group_flow;
merge_edges(first,current,inputs[j].label);
uniform_join=false;
current=join;
}
live=true;
Expand Down Expand Up @@ -636,6 +686,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
alu.write(128,ir.value("zext i1 "+alu.cmp("ne",merged,"0")+" to i32"));
if (exchanged_group.empty()) refuse(pc,"mesh exchange lost the entry group VGPR");
exchanged_group = ir.binary("lshr",exchanged_group,"7");
group_uniform.insert(exchanged_group);
alu.write(256+zero_vgpr,{});
alu.write(261,{});
full_exec=false;
Expand Down Expand Up @@ -691,8 +742,10 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
alu.write(temp,{}); alu.write(128,{});
full_exec=true; lane_active.clear(); wave_active.clear(); exec_value[0].clear(); exec_value[1].clear();
remember_partial();
for (auto r : partial_writes) alu.write(r,{});
partial_writes.clear();
keep_bounded_partials();
// Wave zero stored input_v; a group-uniform input_v is this thread's value too.
if (group_uniform_value(input_v)) at_most[vertex_count].insert(input_v);
if (group_uniform_value(input_p)) at_most[primitive_count].insert(input_p);
allocated=true; pc+=128; continue;
}
}
Expand Down Expand Up @@ -896,6 +949,67 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
if (sum<=UINT32_MAX) result_bound=static_cast<unsigned>(sum);
}
if (i.name=="s_cselect_b32") result_bound=(std::max)(bound(i,i.src[0]),bound(i,i.src[1]));
// Group uniformity of this scalar instruction's inputs, read before it writes.
const string iname(i.name);
auto uniform_source=[&](unsigned r) {
if (constant32(i,r) || r==125) return true;
if (r==253) {
const auto scc=alu.graphics_registers.find(128);
return scc!=alu.graphics_registers.end() && group_uniform_value(scc->second);
}
if (r>=106 && (r<256 || r>=512)) return false;
const auto it=alu.graphics_registers.find(r);
return it!=alu.graphics_registers.end() && group_uniform_value(it->second);
};
auto uniform_descriptor=[&](unsigned r) {
const auto &sym=state[r];
return uniform_source(r) || sym.type==Symbol::Type::table || sym.type==Symbol::Type::descriptor ||
sym.type==Symbol::Type::root_address || sym.type==Symbol::Type::constant ||
(sym.type==Symbol::Type::indexed_descriptor && uniform_descriptor_defs.count(sym.definition));
};
const bool wide_scalar=iname.size()>4 && iname.substr(iname.size()-4)=="_b64";
bool uniform_inputs=false;
if ((i.enc==Encoding::Sop1 || i.enc==Encoding::Sop2 || i.enc==Encoding::Sopk || i.enc==Encoding::Sopc) &&
(i.enc==Encoding::Sopc || i.dst<106) && iname.find("exec")==string::npos &&
iname.find("getreg")==string::npos && iname.find("setreg")==string::npos &&
iname.find("pc_")==string::npos && iname.find("movrel")==string::npos) {
const unsigned sources=i.enc==Encoding::Sop1 ? 1 : i.enc==Encoding::Sopk ? 0 : 2;
uniform_inputs=true;
for (unsigned j=0;j<sources;++j)
uniform_inputs=uniform_inputs && uniform_source(i.src[j]) &&
(!wide_scalar || i.src[j]>=106 || uniform_source(i.src[j]+1));
if (i.enc==Encoding::Sopk && iname!="s_movk_i32") uniform_inputs=uniform_inputs && uniform_source(i.dst);
if (iname.rfind("s_cselect",0)==0 || iname.rfind("s_cmov",0)==0 || iname.rfind("s_addc",0)==0 ||
iname.rfind("s_subb",0)==0)
uniform_inputs=uniform_inputs && uniform_source(253);
} else if (i.enc==Encoding::Smem && (i.op<=4 || (i.op>=8 && i.op<=12)) && !memory_stored && i.src[0]<105) {
const unsigned offset_reg=i.b>>25;
uniform_inputs=offset_reg==125 || uniform_source(offset_reg);
for (unsigned j=0;j<(i.op<8 ? 2u : 4u) && uniform_inputs;++j)
uniform_inputs=uniform_descriptor(i.src[0]+j);
} else if ((iname=="v_mov_b32" || iname=="v_readfirstlane_b32") && !i.dpp_control && !i.subword) {
// A copy of a group-uniform value; readfirstlane picks one lane of it.
uniform_inputs=uniform_source(i.src[0]);
} else if (i.enc==Encoding::Mubuf && iname.rfind("buffer_load_format",0)==0 && !memory_stored &&
!((i.a>>12)&1)) {
// A typed load at a group-uniform index from a group-uniform descriptor.
const unsigned resource=((i.b>>16)&31)*4, soffset=i.b>>24;
uniform_inputs=(!((i.a>>13)&1) || uniform_source(256+(i.b&255))) && uniform_source(soffset);
for (unsigned j=0;j<4 && uniform_inputs;++j) uniform_inputs=uniform_descriptor(resource+j);
}
const unsigned scalar_count=i.enc==Encoding::Smem ? memory_count(i) :
i.enc==Encoding::Sopc ? 0 : i.enc==Encoding::Mubuf ? 4 : (wide_scalar ? 2 : 1);
const unsigned uniform_dst=i.enc==Encoding::Mubuf ? 256+i.dst : i.dst;
std::array<string,17> before_scalar;
for (unsigned j=0;uniform_inputs && j<scalar_count && j<16;++j)
if (auto it=alu.graphics_registers.find(uniform_dst+j); it!=alu.graphics_registers.end()) before_scalar[j]=it->second;
if (uniform_inputs)
if (auto it=alu.graphics_registers.find(128); it!=alu.graphics_registers.end()) before_scalar[16]=it->second;
std::array<string,2> bound_operands;
if (iname=="s_cselect_b32" || iname=="s_min_u32")
for (unsigned j=0;j<2;++j)
if (auto it=alu.graphics_registers.find(i.src[j]); i.src[j]<106 && it!=alu.graphics_registers.end())
bound_operands[j]=it->second;
const unsigned vec_dst=i.enc==Encoding::Mubuf ? 256+i.dst : i.dst;
std::array<string,4> before_vec;
std::array<bool,4> before_partial{};
Expand Down Expand Up @@ -992,6 +1106,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
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);
ir.body<<" br i1 "<<take<<", label %"<<edge<<", label %"<<fall<<"\n"<<fall<<":\n";
Expand Down Expand Up @@ -1410,8 +1525,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
full_exec=true;
export_limit.clear();
remember_partial();
for (auto r : partial_writes) alu.write(r,{});
partial_writes.clear();
keep_bounded_partials();
wave_active.clear(); lane_active.clear();
exec_value[0].clear(); exec_value[1].clear();
} else if (i.name=="s_mov_b64" && i.dst==106 && (i.src[0]==128 || i.src[0]==193)) {
Expand Down Expand Up @@ -1697,6 +1811,24 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
}
if (i.dst<106 && i.enc!=Encoding::Smem) remember_scalar(i.dst);
if (i.name=="v_mov_b32" && i.src[0]<106) remember_scalar(i.src[0]);
if (uniform_inputs) {
// Only values this instruction produced: an unchanged register keeps its own provenance.
for (unsigned j=0;j<scalar_count && j<16;++j)
if (auto it=alu.graphics_registers.find(uniform_dst+j); it!=alu.graphics_registers.end() &&
!it->second.empty() && it->second!=before_scalar[j]) group_uniform.insert(it->second);
if (i.enc==Encoding::Smem) uniform_descriptor_defs.insert(pc);
if (auto it=alu.graphics_registers.find(128); it!=alu.graphics_registers.end() &&
!it->second.empty() && it->second!=before_scalar[16]) group_uniform.insert(it->second);
}
// s_cselect_b32 d, 0, x (or d, x, 0) is 0 or x; s_min_u32 is at most either source.
if ((iname=="s_cselect_b32" || iname=="s_min_u32") && i.dst<106) {
const bool zero_select=iname=="s_min_u32" || constant32(i,i.src[0])==0u || constant32(i,i.src[1])==0u;
auto it=alu.graphics_registers.find(i.dst);
if (zero_select && it!=alu.graphics_registers.end() && !it->second.empty())
for (const auto &x : bound_operands) if (!x.empty() && x!=it->second) at_most[it->second].insert(x);
}
if ((i.enc==Encoding::Mubuf || i.enc==Encoding::Image) &&
(iname.find("store")!=string::npos || iname.find("atomic")!=string::npos)) memory_stored=true;
if (i.dst==261 || (i.enc==Encoding::Mubuf && i.dst==5)) exchanged_group.clear();
pc=i.next;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,38 @@ def movv(v,s): return 0x7e000200|(v<<17)|s
name, result.stdout, result.stderr)
print(f'{name}: refused: {diagnostic}', flush=True)

# A vertex skip writes v10 for threads below V, full EXEC returns and the count exchange
# shares select(V == 0, 0, V). An export of v10 under that count reads only writing lanes,
# provided V is equal in every wave: here it comes from the root constant in s21.
layout.write_text('mesh1 128 4 1 2 128 64\nconstants 21 0 1')
def bounded(v=(0x8717ff15, 0x3), select=0x85221780, guard=(0x7d280022, 0xbf880002)):
return (count_setup[:4] + list(v) + # s_and_b32 s23, s21, 3: V
[0xbf068017, select, # s_cmp_eq_u32 s23, 0; s_cselect_b32 s34, 0, s23
0xbea30381, # s_mov_b32 s35, 1
0x8f1c8616, # s_lshl_b32 s28, s22, 6 (wave base)
0xd7650000, 0x000038c1, 0xd7660000, 0x000200c1, # v0 = thread
0x7da80017, 0xbf880001, # v_cmpx_gt_u32 s23, v0; s_cbranch_execz
0x7e1402f2, # v_mov_b32 v10, 1.0 for threads below V
0xbefe04c1] + allocate + # full EXEC, then the count exchange
list(guard) + # v_cmpx_gt_i32 s34, v0; s_cbranch_execz
[0xf8000201, 0x0000000a, # exp param0 v10
0xbefe04c1] + program[3:])
text = run('bounded-export', bounded())
assert 'storeVertexOutput' in text, text
lane_range = 'mesh register read outside its proven lane range: 266'
# V from the wave index differs between waves; wave 0's count says nothing about this wave.
run('bounded-export-wave-v', bounded(v=(0x8717ff16, 0x3)), lane_range) # s_and_b32 s23, s22, 3
# s_cselect_b32 s34, 4, s23 can exceed V.
run('bounded-export-select-4', bounded(select=0x85221784), lane_range)
# Without the count guard the export reads every lane.
run('bounded-export-full-exec', bounded(guard=()), lane_range)
# A merge keeps V group-uniform only behind a group-uniform branch condition.
def merged(condition):
return (0x8717ff15, 0x3, condition, 0xbf850001, # s_cbranch_scc1 over the next write
0xbe970382) # s_mov_b32 s23, 2
assert 'storeVertexOutput' in run('bounded-export-root-branch', bounded(v=merged(0xbf068015))) # s21 == 0
run('bounded-export-wave-branch', bounded(v=merged(0xbf068016)), lane_range) # s22 == 0

layout.write_text('mesh1 128 3 1 2 128 64\ntable 16 0')
run('mistyped-root',words+controlled,'root parameter kind')
layout.write_text('mesh1 128 3 1 2 128 64')
Expand Down
Loading