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
29 changes: 28 additions & 1 deletion src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
};
std::map<unsigned,std::vector<Incoming>> forward;
bool live=true;
// A diamond joined and only EXEC-independent scalar work has run since (see the EXEC restore).
bool diamond_joined=false;
unsigned next_edge=0;
auto arrive=[&](unsigned target) {
string label="mesh_edge"+to_string(next_edge++);
Expand All @@ -316,6 +318,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
pc=next->first;
}
if (forward.count(pc)) {
diamond_joined=false; // another path arrives between the join and the restore
if (live) ir.body<<" br label %"<<arrive(pc)<<"\n";
auto inputs=std::move(forward.at(pc)); forward.erase(pc);
for (const auto &in : inputs)
Expand Down Expand Up @@ -403,6 +406,30 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
}
lane_scalar_clobbers.clear();
branch.reset();
diamond_joined=true;
}
// 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
// open skip's guard, and an instruction that overwrites the saved mask leaves it without a valid
// reaching mask.
const bool after_diamond_join=diamond_joined;
if (diamond_joined) {
const auto next=decode(code,pc,true,true);
const string name(next.name);
// SGPRs, M0, null, inline and literal constants, and SCC; not VCC, EXEC, VCCZ or EXECZ.
auto lane_free=[](unsigned s) { return s<106 || s==124 || s==125 || (s>=128 && s<=250) || s==253 || s==255; };
bool independent=false;
if (next.enc==Encoding::Smem) // opcodes 0-4: s_load_dword{,x2..x16}; 8-12: s_buffer_load_dword{,...}
independent=(next.op<=4 || (next.op>=8 && next.op<=12)) && next.dst+memory_count(next)<=106;
else if (next.enc==Encoding::Sopp)
independent=name=="s_waitcnt" || name=="s_clause" || name=="s_nop";
else if (next.enc==Encoding::Sop1 || next.enc==Encoding::Sop2 || next.enc==Encoding::Sopk ||
next.enc==Encoding::Sopc)
independent=name.find("exec")==string::npos && name.find("pc_")==string::npos &&
name.find("movrel")==string::npos && lane_free(next.src[0]) && lane_free(next.src[1]) &&
(next.enc==Encoding::Sopc || next.dst<106);
if (!independent) diamond_joined=false;
}
program.at[pc]=0;
alu.graphics_pc = pc;
Expand Down Expand Up @@ -1044,7 +1071,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
} else if (i.name=="s_mov_b64" && i.dst==126 && i.src[0]<=104 && !(i.src[0]&1)) {
const auto mask=saved_mask(i.src[0]);
if (exec_skip && !diamond_exec_restore &&
!(closed_nested_skip && mask && !mask->full && mask->lane==exec_skip->guard &&
!((closed_nested_skip || after_diamond_join) && mask && !mask->full && mask->lane==exec_skip->guard &&
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");
Expand Down
26 changes: 26 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,32 @@ def lane_skip(name, body):
0x7d280015] # v_cmpx_gt_i32_e32 s21, v0
assert 'phi i32' in lane_skip('signed-cmpx-exec-restore', cmpx+cmpx_tail)

# A compiler may place EXEC-independent scalar work between the join
# and the restore: a table load, its wait and SALU run once per wave
# whatever EXEC holds, so the restore still closes the diamond.
layout.write_text('mesh1 128 3 1 2 128 64\nconstants 21 0 1\ntable 16 1')
scalar_gap = [0xbe910380, # s_mov_b32 s17, 0
0xf4080608, 0xfa000000, # s_load_dwordx4 s[24:27], s[16:17], 0
0xbf8c3f70, # s_waitcnt
0x800c8415] # s_add_u32 s12, s21, 4
def gap_diamond(gap):
# The else arm's EXECZ target, index 7 of the tail, is the join.
return cmpx+cmpx_tail[:7]+gap+cmpx_tail[7:]
assert 'phi i32' in lane_skip('signed-cmpx-scalar-gap', gap_diamond(scalar_gap))
# The join is body word 9 after the outer CMPX and EXECZ at +0x1c.
def gap_refused(name, gap):
body = gap_diamond(gap)
wrapped = [0x7da40081, 0xbf880000 | len(body), *body, 0xbefe04c1]
restore = 0x24 + 4 * (9 + len(gap))
run(name, program[:7]+wrapped+program[7:], f'+0x{restore:x}: {exec_write}')
# Writes to VCC, a branch, an overwritten saved mask or a read of
# EXEC in the gap keep the restore refused.
gap_refused('gap-vcc-write', scalar_gap + [0xbeea04c1]) # s_mov_b64 vcc, -1
gap_refused('gap-branch', scalar_gap + [0xbf820000]) # s_branch to the next word
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')

# No diamond follows either write. The generic mask ALU must use
# the narrowed lane predicate for the following v1 update.
narrowing = [0x7d080015, # v_cmp_gt_i32_e32 vcc, s21, v0
Expand Down
Loading