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
55 changes: 53 additions & 2 deletions src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
struct MaskState { bool full; string lane,wave,lo,hi; decltype(alu.graphics_registers) regs; std::set<unsigned> partial; };
std::map<unsigned,MaskState> saved_exec;
std::map<string,Edge> before_narrowing;
// One CMPX narrowing an open lane skip's guard, with no EXECZ: the registers when it ran (all exact
// for the guard's lanes) and, per register, the value merged per lane against them since (see the
// EXEC restore). A merged value is exact only in the register it was merged into, not in a copy.
struct SkipNarrowing { unsigned skip; string lane; decltype(alu.graphics_registers) regs; std::map<unsigned,string> merged; };
std::optional<SkipNarrowing> skip_narrowing;
auto saved_mask=[&](unsigned source)->const MaskState * {
if (source>104 || (source&1)) return nullptr;
const auto &lo=state[source], &hi=state[source+1];
Expand Down Expand Up @@ -1285,6 +1290,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
before_partial[j]=partial_writes.count(vec_dst+j)!=0;
}
}
const string lane_at_start=lane_active;
const bool export_cmp=i.name=="v_cmpx_gt_i32" && allocated && i.src[0]<106 && full_exec &&
(alu.read(i.src[0])==vertex_count || alu.read(i.src[0])==primitive_count);
if ((i.name=="v_cmpx_gt_u32" || export_cmp) && full_exec && !branch && !exec_skip && i.enc==Encoding::Vopc && !i.wide && !i.subword && !i.dpp_control &&
Expand Down Expand Up @@ -1949,12 +1955,29 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
saved_exec[pc]={full_exec,lane_active,wave_active,exec_value[0],exec_value[1],alu.graphics_registers,partial_writes};
} 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 &&
// The guard restored after one CMPX narrowed it inside the skip: each register is either
// unchanged since that CMPX or merged per lane against its value then.
const bool narrowed_restore=exec_skip && !branch && skip_narrowing && skip_narrowing->lane==lane_active &&
skip_narrowing->skip==exec_skip->start &&
mask && !mask->full && mask->lane==exec_skip->guard && mask->wave==exec_skip->wave_guard;
if (exec_skip && !diamond_exec_restore && !narrowed_restore &&
!((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");
if (mask->lane!=lane_active) {
if (narrowed_restore) {
std::set<unsigned> remaining;
for (auto r:partial_writes) {
const auto &now=alu.graphics_registers.at(r);
const auto at=skip_narrowing->regs.find(r);
const auto merged=skip_narrowing->merged.find(r);
if ((at!=skip_narrowing->regs.end() && !at->second.empty() && at->second==now) ||
(merged!=skip_narrowing->merged.end() && merged->second==now)) remaining.insert(r);
else alu.write(r,{});
}
partial_writes=std::move(remaining);
skip_narrowing.reset();
} else if (mask->lane!=lane_active) {
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 @@ -2177,8 +2200,36 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
alu.record_graphics_bits(value,bits);
if (!u.empty()) active_uniform[value]={lane_active,u};
partial_writes.erase(vec_dst+j);
} else if (skip_narrowing && skip_narrowing->lane==lane_active && exec_skip && !branch &&
skip_narrowing->skip==exec_skip->start &&
!before_vec[j].empty() && before_partial[j] && !value_limits.count(before_vec[j]) &&
alu.graphics_registers.at(vec_dst+j)!=before_vec[j] &&
[&] {
const auto merged=skip_narrowing->merged.find(vec_dst+j);
if (merged!=skip_narrowing->merged.end() && merged->second==before_vec[j]) return true;
const auto at=skip_narrowing->regs.find(vec_dst+j);
return at!=skip_narrowing->regs.end() && at->second==before_vec[j];
}()) {
// The old value is exact for the guard's lanes, so the merge is too; it stays partial
// against full EXEC.
auto &value=alu.graphics_registers.at(vec_dst+j);
auto u=uniform(value);
const auto bits=alu.graphics_bits(value)&alu.graphics_bits(before_vec[j]);
value=alu.select(lane_active,value,before_vec[j]);
alu.record_graphics_bits(value,bits);
if (!u.empty()) active_uniform[value]={lane_active,u};
skip_narrowing->merged[vec_dst+j]=value;
}
}
// A skip opened under the narrowed mask ends the straight-line region; its close is not a merge.
if (iname=="s_cbranch_execz") skip_narrowing.reset();
// A CMPX narrowing the open skip's guard starts a region the restore above can close.
if (iname.rfind("v_cmpx",0)==0 && exec_skip && !branch && lane_at_start==exec_skip->guard &&
lane_active!=lane_at_start) {
const auto parent=mask_parent.find(lane_active);
if (parent!=mask_parent.end() && parent->second==lane_at_start)
skip_narrowing=SkipNarrowing{exec_skip->start,lane_active,alu.graphics_registers,{}};
}
if (result_bound && *result_bound<UINT32_MAX && i.dst<106) {
auto it=alu.graphics_registers.find(i.dst);
if (it!=alu.graphics_registers.end() && !it->second.empty()) upper_bound[it->second]=*result_bound;
Expand Down
35 changes: 35 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,41 @@ def merged(condition):
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

# Inside the vertex skip, the title saves EXEC, narrows it with one CMPX and no EXECZ, runs
# straight-line VALU work and restores the saved vertex mask. Every register is exact for the
# vertex lanes when the CMPX runs, so a write under the narrowed mask is merged per lane with
# the old value and the restore keeps it. v10 is then read and written under the vertex mask.
lane_skip_exec = 'mesh EXEC write inside a lane-skip block'
def narrowed(region, restore=0xbefe0426, after=(0x061414f2,)): # v_add_f32 v10, 1.0, v10
body = ([0xbea6047e, # s_mov_b64 s[38:39], exec (the vertex mask)
0x7e1402f2, # v_mov_b32 v10, 1.0
0x7daa0080] + list(region) + # v_cmpx_ne_u32 0, v0
[restore] + list(after)) # s_mov_b64 exec, s[38:39]
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:])
base = run('skip-narrow-empty', narrowed([]))
text = run('skip-narrow-merge', narrowed([0x061414f2])) # v_add_f32 v10, 1.0, v10 under v0 != 0
assert 'storeVertexOutput' in text and text.count('select ') == base.count('select ') + 1, text
# A register first written under the narrowed mask has no value for thread 0 at the restore.
run('skip-narrow-new-register', narrowed([0x7e1602f4], # v_mov_b32 v11, 2.0
after=(0x0614150b,)), # v_add_f32 v10, v11, v10
'graphics register undefined on an incoming path: 267')
# A merged value is exact only in the register it was merged into: a copy of it is not.
run('skip-narrow-merged-copy', narrowed([0x061414f2, 0x7e16030a], # v_mov_b32 v11, v10
after=(0x0614150b,)), # v_add_f32 v10, v11, v10
'graphics register undefined on an incoming path: 267')
# The same register merged again stays exact.
text = run('skip-narrow-merge-twice', narrowed([0x061414f2, 0x061414f2]))
assert 'storeVertexOutput' in text and text.count('select ') == base.count('select ') + 2, text
# A second narrowing is not merged against the vertex mask, so its restore stays refused.
run('skip-narrow-twice', narrowed([0x7daa0080, 0x061414f2]), lane_skip_exec)
# 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)

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