Skip to content
Open
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
35 changes: 31 additions & 4 deletions src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
if (scalar_branch && outer.contains_child)
refuse(*scalar_branch,"mesh scalar branch crosses a nested SAVEEXEC region");
};
struct Skip { unsigned start,end; Edge entry; bool per_lane; string guard, wave_guard; };
// narrowed_from: the parent skip's guard, when this per-lane skip opened right after one CMPX narrowed it.
struct Skip { unsigned start,end; Edge entry; bool per_lane; string guard, wave_guard, narrowed_from{}; };
std::vector<Skip> skip_parents;
std::optional<Skip> exec_skip;
alu.check_graphics_read=[&](unsigned reg,const string &value) {
Expand Down Expand Up @@ -1040,6 +1041,9 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
};
std::optional<unsigned> pending_saveexec_restore;
unsigned pc = 0;
string last_name, carried_lane; // the last fully lowered instruction; a lane set carried to a parent skip close
unsigned carried_at=~0u; // the pc whose skip closes set carried_lane; it describes no other pc
unsigned last_end=~0u;
while (pc < code.size()) {
// All handled scalar branches are forward, so every predecessor is
// available when we reach its target. Funnel arbitrary fan-in into
Expand All @@ -1053,6 +1057,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
diamond_joined=false; // another path arrives between the join and the restore
if (live) ir.body<<" br label %"<<arrive(pc)<<"\n";
auto incoming=std::move(forward.at(pc)); forward.erase(pc);
carried_lane.clear(); // another path arrives: the carried lane set no longer describes the state
for (const auto &in : incoming)
if (in.full!=full_exec || in.lane!=lane_active || in.wave!=wave_active)
refuse(pc,"mesh scalar branch merges different EXEC definitions");
Expand Down Expand Up @@ -1090,12 +1095,21 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
}
}
const auto limit=lane_limits.find(exec_skip->guard);
const bool retain=skip_parents.empty() && exec_skip->guard==lane_active &&
// A nested skip that closed at this same instruction (see Skip::narrowed_from) leaves its
// narrowed mask in lane_active, but every register is exact again for the guard's lanes.
const bool guard_lanes=exec_skip->guard==lane_active ||
(carried_at==pc && !carried_lane.empty() && carried_lane==exec_skip->guard);
carried_lane.clear();
const bool retain=skip_parents.empty() && guard_lanes &&
!branch && forward.empty() && limit!=lane_limits.end();
merge_edges(exec_skip->entry,"mesh_empty"+tag,"mesh_done"+tag,retain ? limit->second : string{});
// Exact for the guard's lanes, a skip opened straight after a CMPX makes the parent exact too,
// so the lane set carries through any depth of skips closing here.
const auto narrowed_from=exec_skip->per_lane && guard_lanes ? exec_skip->narrowed_from : string{};
exec_skip.reset();
if (!skip_parents.empty()) {
exec_skip=std::move(skip_parents.back());skip_parents.pop_back();
carried_lane=narrowed_from; carried_at=pc; // read by the parent's close or an EXEC restore at pc
if (pc==exec_skip->end) return true;
closed_nested_skip=true;
}
Expand Down Expand Up @@ -1693,11 +1707,19 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
full_exec=false; wave_active.clear(); exec_value[0].clear();exec_value[1].clear();
} else if (i.name=="s_cbranch_execz" && !full_exec && !lane_active.empty() && !branch &&
i.target()>i.next && i.target()<code.size()) {
// A nested per-lane skip that opens straight after a CMPX narrowing the parent's guard: no
// instruction ran under the narrowed mask outside this skip, so once it closes every
// register is exact again for each of the parent's lanes.
string narrowed_from;
if (exec_skip && wave_active.empty() && last_end==pc && last_name.rfind("v_cmpx",0)==0) {
const auto parent=mask_parent.find(lane_active);
if (parent!=mask_parent.end() && parent->second==exec_skip->guard) narrowed_from=parent->second;
}
if (exec_skip) {
if (i.target()>exec_skip->end) refuse(pc,"overlapping mesh EXECZ exits");
skip_parents.push_back(std::move(*exec_skip));
}
exec_skip=Skip{pc,i.target(),save_edge(),wave_active.empty(),lane_active,wave_active};
exec_skip=Skip{pc,i.target(),save_edge(),wave_active.empty(),lane_active,wave_active,narrowed_from};
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 ((branch && saveexec_vcc_tests.count(i.next)) ||
Expand Down Expand Up @@ -2372,6 +2394,10 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
mask->wave==exec_skip->wave_guard))
refuse(pc,"mesh EXEC write inside a lane-skip block");
if (!mask) refuse(pc,"mesh EXEC restore lacks a still-valid reaching mask");
// Nested CMPX skips that closed here carried the enclosing skip's guard (see close_skip):
// each skip merged per lane, so every register is already exact for the restored lanes.
const bool carried=closed_nested_skip && exec_skip && carried_at==pc && !carried_lane.empty() &&
carried_lane==exec_skip->guard && mask->lane==carried_lane && mask->wave==exec_skip->wave_guard;
if (narrowed_restore) {
std::set<unsigned> remaining;
for (auto r:partial_writes) {
Expand All @@ -2384,7 +2410,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
}
partial_writes=std::move(remaining);
skip_narrowing.reset();
} else if (mask->lane!=lane_active) {
} else if (mask->lane!=lane_active && !carried) {
auto at_narrow=before_narrowing.find(mask->lane);
const auto &regs=at_narrow==before_narrowing.end() ? mask->regs : at_narrow->second.regs;
const auto &valid=at_narrow==before_narrowing.end() ? mask->partial : at_narrow->second.partial;
Expand Down Expand Up @@ -2687,6 +2713,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
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();
last_name=iname; last_end=i.next;
pc=i.next;
}
refuse(pc,"unterminated mesh shader");
Expand Down
68 changes: 68 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,74 @@ def narrowed(region, restore=0xbefe0426, after=(0x061414f2,)): # v_add_f32 v10,
# So is a restore of a mask saved under the narrowed EXEC.
run('skip-narrow-inner-save', narrowed([0xbea8047e, 0x061414f2], restore=0xbefe0428), lane_skip_exec)

# Item 6 of #3703: a nested CMPX skip inside the vertex skip overwrites v10 for thread 0, and
# both EXECZ exits land on the same restore. Each thread below V still holds an exact v10 there
# (1.0, or 2.0 for thread 0), so the export under the count guard is accepted.
def nested(narrow=(0x7da40080,), # v_cmpx_eq_u32 0, v0
save=(), restore=0xbefe04c1, guard=(0x7d280022, 0xbf880002)):
inner = list(narrow) + [0xbf880001, # s_cbranch_execz to the restore
0x7e1402f4] # v_mov_b32 v10, 2.0
return (count_setup[:4] + [0x8717ff15, 0x3, # s_and_b32 s23, s21, 3: V
0xbf068017, 0x85221780, # 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
list(save) +
[0x7da80017, 0xbf880000 | (1 + len(inner)), # v_cmpx_gt_u32 s23, v0; s_cbranch_execz
0x7e1402f2] + inner + # v_mov_b32 v10, 1.0, then the nested skip
[restore] + allocate + # both exits restore EXEC, then the count exchange
list(guard) +
[0xf8000201, 0x0000000a, # exp param0 v10
0xbefe04c1] + program[3:])
for name, nested_words in (('nested-skip-export', nested()),
# The title restores a saved full EXEC instead of exec = -1.
('nested-skip-saved-exec', nested(save=(0xbea4047e,), restore=0xbefe0424))):
text = run(name, nested_words) # s_mov_b64 s[36:37], exec / s_mov_b64 exec, s[36:37]
assert 'storeVertexOutput' in text and '1073741824' in text and '1065353216' in text, text
undefined = 'graphics register undefined on an incoming path: 266'
# A write between the nested CMPX and its EXECZ runs for every thread below V with a value only
# valid under the narrowed mask, so nothing is carried to the outer skip.
run('nested-skip-write-before-execz', nested(narrow=(0x7da40080, 0x7e1402f6)), undefined) # v10 = 4.0
# A narrowing that is not a CMPX directly before the EXECZ is not carried either.
run('nested-skip-and-exec', nested(narrow=(0x7d840080, 0x87fe6a7e)), undefined) # v_cmp; s_and_b64 exec
# The export still needs the count guard.
run('nested-skip-full-exec', nested(guard=()), lane_range)

# A chain of nested CMPX skips inside the vertex skip, each opened straight after a CMPX that
# narrows the one around it. All of them exit to one restore of the vertex mask saved in
# s[38:39], and the vertex skip then goes on: v10 is read and written again under the vertex
# mask before the export. Each thread below V holds an exact v10 at that restore.
def chain(depth, restore=True, narrow=(0x7da40080,)): # v_cmpx_eq_u32 0, v0
body = [0xbea6047e, # s_mov_b64 s[38:39], exec (the vertex mask)
0x7e1402f2] # v_mov_b32 v10, 1.0
exits = []
for level in range(depth):
body += list(narrow) if level == depth - 1 else [0x7da40080]
exits.append(len(body))
body += [0, 0x061414f2] # s_cbranch_execz; v_add_f32 v10, 1.0, v10
end = len(body)
for at in exits:
body[at] = 0xbf880000 | (end - at - 1) # every exit lands on the restore (or the skip end)
if restore:
body += [0xbefe0426, # s_mov_b64 exec, s[38:39]
0x061414f2] # v_add_f32 v10, 1.0, v10 under the vertex mask
return (count_setup[:4] + [0x8717ff15, 0x3, 0xbf068017, 0x85221780, 0xbea30381, 0x8f1c8616,
0xd7650000, 0x000038c1, 0xd7660000, 0x000200c1] +
[0x7da80017, 0xbf880000 | len(body)] + body + # v_cmpx_gt_u32 s23, v0; the vertex skip
[0xbefe04c1] + allocate + [0x7d280022, 0xbf880002,
0xf8000201, 0x0000000a, # exp param0 v10
0xbefe04c1] + program[3:])
for depth in (1, 3):
text = run(f'nested-skip-chain-restore-{depth}', chain(depth))
assert 'storeVertexOutput' in text and text.count('fadd') >= depth + 1, text
# Two nested skips that close where the vertex skip closes carry the lane set through both.
assert 'storeVertexOutput' in run('nested-skip-chain-end', chain(2, restore=False))
# A write between the innermost CMPX and its EXECZ runs for every thread of the skip around it
# with a value only valid under the narrowed mask, so the chain is broken at the restore.
run('nested-skip-chain-write-before-execz', chain(3, narrow=(0x7da40080, 0x7e1402f6)), undefined)
# So is a narrowing that is not a CMPX (v_cmp; s_and_b64 exec, vcc).
run('nested-skip-chain-and-exec', chain(3, narrow=(0x7d840080, 0x87fe6a7e)), undefined)

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