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
28 changes: 28 additions & 0 deletions src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,30 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
return full_exec || (exec_skip && !exec_skip->per_lane && exec_skip->guard==lane_active &&
!wave_active.empty());
};
// A MUBUF load without OFFEN, with a uniform IDXEN index if any and a constant or uniform SOFFSET, reads
// the same address in every lane. Read before the load: its destination may be its own index register.
auto uniform_buffer_address=[&](const Inst &i) {
if ((i.a&0x1000) || memory_stored) return false;
if (i.a&0x2000) {
const auto index=alu.read(256+(i.b&255));
if (index.empty() || uniform(index)!=index) return false;
}
const unsigned soffset=(i.b>>24)&255;
if (constant32(i,soffset)) return true;
if (soffset>=106) return false;
const auto value=alu.read(soffset);
return !value.empty() && uniform(value)==value;
};
// Until the shader stores to memory, such a load returns the same dwords in every lane. Mark them uniform
// under the same conditions as the heap typed load, so v_readfirstlane can read them.
auto mark_uniform_buffer_load=[&](const Inst &i,unsigned count) {
for (unsigned j=0;j<count;++j) {
const auto word=alu.read(256+i.dst+j);
if (word.empty()) continue;
if (full_exec) uniform_values.insert(word);
else if (whole_wave_execution() && !branch) active_uniform[word]={lane_active,word};
}
};
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) {
Expand Down Expand Up @@ -1848,8 +1872,10 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
state[((i.b>>16)&31)*4].type==Symbol::Type::descriptor) {
// A V# read whole from a bound descriptor table: the shared typed-buffer SRV path checks the
// table binding and all four descriptor words, as it does for vertex and pixel shaders.
const bool uniform_address=uniform_buffer_address(i);
alu.memory(i,state);
if (!full_exec) partial_writes.insert(256+i.dst);
if (uniform_address) mark_uniform_buffer_load(i,1);
} else if (i.name=="buffer_load_format" && i.op==0 && vector_exec &&
((i.a&0x3ffff)==0x2000 || (i.a&0x3ffff)==0)) {
const auto base=((i.b>>16)&31)*4;
Expand Down Expand Up @@ -1930,6 +1956,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
alu.formatted_byte_load(i,state,window);
if (!full_exec) for (unsigned j=0;j<=i.op;++j) partial_writes.insert(256+i.dst+j);
} else if (i.name=="buffer_load_dword" && vector_exec) {
const bool uniform_address=uniform_buffer_address(i);
constexpr unsigned counts[]{1,2,4,3};
const unsigned count=counts[i.op&3];
if ((i.a&0x3000)==0x1000)
Expand All @@ -1938,6 +1965,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
alu.memory(i,state);
if (!full_exec)
for (unsigned j=0;j<count;++j) partial_writes.insert(256+i.dst+j);
if (uniform_address) mark_uniform_buffer_load(i,count);
} else if (i.enc==Encoding::Vopc && i.name.substr(0,6)=="v_cmp_" && i.dst<=106 &&
!i.dpp_control && !i.scale && !i.clamp && (full_exec || !lane_active.empty())) {
auto next=transfer(i, state, !layout.wave32, true, false, true);
Expand Down
25 changes: 25 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,19 @@ def run_offen(name, words, error=None):
text, re.DOTALL), text
text = run('scalar-buffer-vcc-branch', words[:3]+words[7:]+compare_root+choose_output+controlled)
assert 'rawBufferLoad' in text and 'br i1' in text
# A buffer load whose address is the same in every lane is uniform, so READFIRSTLANE reads it
# directly instead of broadcasting lane 0 through groupshared memory.
uniform_read = [0x7e280506] # v_readfirstlane_b32 s20, v6
for name, load in (('uniform-load-off', [0xe0300014, 0x80060600]), # buffer_load_dword v6, off, s[24:27], 0 offset:20
('uniform-load-root-soffset', [0xe0300014, 0x15060600])): # ... s[24:27], s21 offset:20
text = run(name, words[:3] + load + uniform_read + controlled)
loaded = re.search(r'(%[\w.]+) = extractvalue %dx\.types\.ResRet\.i32 %[\w.]+, 0\n', text)
assert 'rawBufferLoad' in text and loaded and '@meshReadFirstLane' not in text, text
assert re.search(r'uitofp i32 ' + re.escape(loaded[1]) + r' to float', text), text
# A per-lane offset is not uniform, even from a uniform VGPR, so lane 0 is broadcast.
text = run('uniform-load-offen', words[:3] + [0x7e020215, 0xe0301014, 0x80060601] + # v1 = s21; ... v1 offen
uniform_read + controlled)
assert '@meshReadFirstLane' in text, text
# Convert a whole-wave scalar through a VGPR before READFIRSTLANE.
wave_body = words[7:] + [0x7e340e14, 0x7e30051a] + compare_root + choose_output
wave_range = [0xb0140003, 0xbe8c047e, 0x7da80014, 0xbf880000 | len(wave_body)]
Expand Down Expand Up @@ -1185,6 +1198,18 @@ def table_load(body, seed=True):
'unsupported mesh instruction or EXEC state: buffer_load_format_xy')
run('typed-table-no-skip', table_setup + exported[:7] + [0x7e140287, 0x7da40081, *table_idxen, 0xbefe04c1] +
exported[7:], 'unsupported mesh instruction or EXEC state: buffer_load_format_x')
# Under full EXEC a table load at a uniform index is uniform, so READFIRSTLANE may read it; the index is
# read before the load, which may overwrite it.
for name, load in (('typed-table-uniform-index', [0x7e020215] + table_idxen + [0x7e24050a]), # v1 = s21
('typed-table-uniform-index-in-place', [0x7e020215, 0xe0002000, 0x80060101, # v1 = load at v1
0x7e240501])): # v_readfirstlane_b32 s18, v1
text = run(name, table_setup + load + readfirst_exported)
loaded = re.search(r'(%[\w.]+) = extractvalue %dx\.types\.ResRet\.i32 %[\w.]+, 0\n', text)
assert 'bufferLoad.i32' in text and loaded and '@meshReadFirstLane' not in text, text
assert re.search(r'uitofp i32 ' + re.escape(loaded[1]) + r' to float', text), text
text = run('typed-table-lane-index-readfirstlane', table_setup + readfirst_exported[:7] +
[0xe0002000, 0x80060a00, 0x7e24050a] + readfirst_exported[7:]) # index v0, the lane's thread
assert '@meshReadFirstLane' in text, text
layout.write_text(heap_layout)
run('typed-heap-offset-skip', skip_load([0xe0002004, 0x80080a01]),
'formatted load requires zero or a single linear index')
Expand Down
Loading