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
18 changes: 16 additions & 2 deletions tests/startup_gfx2hlsl_compute_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ def saveexec_fixture():
'06038abe' '80038bbe' '05030cf4000000fa' '7fc08cbf' '8102027e' '02300ce000050380'
'703f8cbf' '84000834' '001078e004050480' '000081bf')
LOAD_L = bytes.fromhex('02300ce000050380')
SOURCE_L_JOIN = SOURCE_L.replace('buffer_load_format_xyzw',
's_cmp_eq_u32 s4, 0\ns_cbranch_scc0 1\ns_nop 0\nbuffer_load_format_xyzw')
WORDS_L_JOIN = WORDS_L.replace(LOAD_L, bytes.fromhex('048006bf010084bf000080bf') + LOAD_L, 1)
L_STRIDE = 6
L_WORD3 = {
'L-uint': (27 << 12) | 5 | (4 << 3) | (1 << 6), # Y, X, one, zero
Expand Down Expand Up @@ -1758,8 +1761,18 @@ def check_formatted_loads():
# The indexed formatted load is refused because the descriptor is modified.
refused('indexed load through a modified descriptor',
lambda: translate('L-uint', code=prefix + bytes.fromhex('0dff1587ffff00c0') + LOAD_L + end))
# At a branch target, there is no local descriptor load to prove.
refused('local table load', lambda: translate('L-uint', code=prefix + bytes.fromhex('000082bf') + LOAD_L + end))
# The table load dominates the branch target and neither path writes any descriptor word.
joined = translate('L-uint', code=WORDS_L_JOIN)
assert 'a = v0 * ud2 + v1 + 2;' in joined.hlsl_cs()
assert 'switch ((ud3 >> 12) & 127u)' in joined.hlsl_cs()
assert joined.manifest() == manifest
# A second writer of word 3 on one path keeps its symbol, so reaching definitions must reject it.
word_write = WORDS_L_JOIN.replace(bytes.fromhex('000080bf') + LOAD_L, bytes.fromhex('0f038fbe') + LOAD_L, 1)
refused('local table load', lambda: translate('L-uint', code=word_write))
high_write = WORDS_L_JOIN.replace(bytes.fromhex('80038bbe'), bytes.fromhex('81038bbe'), 1)
refused('local table load', lambda: translate('L-uint', code=high_write))
root_write = bytes.fromhex('060386be') + WORDS_L_JOIN # s_mov_b32 s6, s6 before the pointer copy
refused('local table load', lambda: translate('L-uint', code=root_write))
local_load = bytes.fromhex('000082bf') + prefix + LOAD_L + end
assert 'switch ((ud3 >> 12) & 127u)' in translate('L-uint', code=local_load).hlsl_cs()
check_formatted_pointer_copy()
Expand Down Expand Up @@ -2535,6 +2548,7 @@ def assemble(llvm_mc, source):
def check_binary(llvm_mc):
assert assemble(llvm_mc, LOOP_BREAK_SOURCE) == LOOP_BREAK_WORDS
assert assemble(llvm_mc, EXIT_RUN_SOURCE) == EXIT_RUN_WORDS
assert assemble(llvm_mc, SOURCE_L_JOIN) == WORDS_L_JOIN
for name, (source, words, *_rest) in CASES.items():
built = assemble(llvm_mc, source)
assert built == words, '%s: assembled %s, recorded %s' % (name, built.hex(), words.hex())
Expand Down
26 changes: 25 additions & 1 deletion tests/startup_gfx2hlsl_join_symbols_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,21 @@

EXPORT = 'exp mrt0 v0, v0, v0, v0 done vm\ns_endpgm\n'
EXPORT_WORDS = '0f1800f800000000' '000081bf'
FORMATTED = ('s_mov_b32 s9, 0\ns_load_dwordx8 s[16:23], s[8:9], 0x20\ns_waitcnt lgkmcnt(0)\nv_mov_b32 v0, 0\n'
's_cmp_eq_u32 s10, 0\ns_cbranch_scc0 1\ns_nop 0\n'
'buffer_load_format_xyzw v[4:7], v0, s[20:23], 0 idxen\ns_waitcnt vmcnt(0)\n'
'exp mrt0 v4, v5, v6, v7 done vm\ns_endpgm\n')
FORMATTED_WORDS = ('800389be' '04040cf4200000fa' '7fc08cbf' '8002007e' '0a8006bf' '010084bf' '000080bf'
'00200ce000040580' '703f8cbf' '0f1800f804050607' '000081bf')
SOURCES = {
'FORMATTED': (FORMATTED, FORMATTED_WORDS),
'FORMATTED_REWRITE': (FORMATTED.replace('s_nop 0', 's_mov_b32 s23, s23'),
FORMATTED_WORDS.replace('000080bf', '170397be')),
'FORMATTED_BACKEDGE': (FORMATTED.replace('s_cmp_eq_u32 s10', 's_mov_b32 s24, 0\ns_cmp_eq_u32 s10')
.replace('s_waitcnt vmcnt(0)\n', 's_waitcnt vmcnt(0)\ns_mov_b32 s23, s23\n'
's_add_u32 s24, s24, 1\ns_cmp_lt_u32 s24, 2\ns_cbranch_scc1 65529\n'),
FORMATTED_WORDS.replace('0a8006bf', '800398be0a8006bf')
.replace('703f8cbf', '703f8cbf170397be1881188018820abff9ff85bf')),
# A proven s_setpc_b64 jumps over s23 = 2 to the read: the target is a branch target, and the decoy never runs.
'CALC': ('s_mov_b32 s23, 1\ns_getpc_b64 s[20:21]\ns_add_u32 s20, s20, 16\ns_addc_u32 s21, s21, 0\n'
's_setpc_b64 s[20:21]\ns_mov_b32 s23, 2\nv_mov_b32 v0, s23\n' + EXPORT,
Expand Down Expand Up @@ -221,7 +235,8 @@ def root(*params):
'DESC_WORD': ('t', 1), 'DESC_SAME': ('t', 1), 'DESC_ONE_PATH': ('t', 1), 'EXEC_DESC_MASKED': ('t', 1),
'EXEC_DESC_AFTER': ('t', 1), 'EXEC_DESC_SCALAR_JOIN': ('t', 1), 'EXEC_DESC_RELOAD': ('t', 1),
'EXEC_DESC_JOIN_BRANCH': ('t', 1), 'EXEC_DESC_JOIN_SCC': ('t', 1), 'EXEC_DESC_BRANCH_IN': ('t', 1),
'PTR_OFFSETS': ('t', 1)}
'PTR_OFFSETS': ('t', 1), 'FORMATTED': ('t', 2), 'FORMATTED_REWRITE': ('t', 2),
'FORMATTED_BACKEDGE': ('t', 2)}


def translate(name):
Expand Down Expand Up @@ -285,6 +300,15 @@ def check_controls():
assert ' v0 = s23;' in lines('JOIN_SAME') # #2279 drops the constant at any join another path writes
same = translate('DESC_SAME')
assert ' t = r0.Load(int3(v0, v1, 0));' in same.hlsl_ps().splitlines(), same.hlsl_ps()
# The second descriptor in the wide load supplies the address stride and the format after the join.
formatted = translate('FORMATTED')
stride = formatted.uds.index(('stride', 0))
word3 = formatted.uds.index(('descword3', 0))
assert 'srv 0 buf table 0 48' in formatted.manifest().splitlines()
assert f'a = v0 * ud{stride} + 0;' in formatted.hlsl_ps()
assert f'switch ((ud{word3} >> 12) & 127u)' in formatted.hlsl_ps()
refused('FORMATTED_REWRITE', 'local table load')
refused('FORMATTED_BACKEDGE', 'local table load')
# Rule R1: a descriptor loaded in an s_cbranch_execz arm is kept for a read before the exec restore ...
masked = translate('EXEC_DESC_MASKED').hlsl_ps().splitlines()
assert ' t = r0.Load(int3(v0, v1, 0));' in masked, masked
Expand Down
81 changes: 63 additions & 18 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7755,8 +7755,8 @@ def descriptor_scalar_writes(name, ops):
regs.update((106, 107))
return regs

def formatted_pointer_reaches(self, pointer, before):
"""Refuse a write to the original root pointer on any predecessor path of this read."""
def prepare_descriptor_flow(self):
"""Build the instruction predecessors and scalar writes for descriptor proofs."""
if not hasattr(self, 'descriptor_predecessors'):
by_offset = {off: i for i, (off, *_rest) in enumerate(self.ins)}
self.descriptor_predecessors = [[] for _ in self.ins]
Expand All @@ -7773,6 +7773,10 @@ def formatted_pointer_reaches(self, pointer, before):
target = by_offset.get(int(args[0].split('_')[1], 16))
if target is not None:
self.descriptor_predecessors[target].append(index)

def formatted_pointer_reaches(self, pointer, before):
"""Refuse a write to the original root pointer on any predecessor path of this read."""
self.prepare_descriptor_flow()
seen = set()
pending = list(self.descriptor_predecessors[before])
while pending:
Expand All @@ -7785,31 +7789,35 @@ def formatted_pointer_reaches(self, pointer, before):
pending.extend(self.descriptor_predecessors[earlier])
return True

def formatted_pointer_copy(self, pointer, before):
"""(source register, instruction index) of the one s_mov_b32 that is the last write to this pointer on
every path from the entry to this read, or None.

One writer found walking back, with no path that reaches the entry without a write, means that the copy is
on every path to the read and that no path from the copy to the read writes the pointer again."""
self.formatted_pointer_reaches(pointer, before) # builds the predecessor tables
writers, seen, expand = set(), set(), [before]
while expand:
def descriptor_last_writers(self, regs, before):
"""Find the last write to any of regs on each incoming path, stopping at disagreement.

None marks a path carrying the entry value. At instruction zero, back edges can also supply writes.
"""
self.prepare_descriptor_flow()
writers, seen, expand = set(), {before}, [before]
while expand and len(writers) < 2:
at = expand.pop()
if at == 0 or not self.descriptor_predecessors[at]:
return None # the entry value of the register reaches the read
writers.add(None)
for earlier in self.descriptor_predecessors[at]:
if earlier in seen:
continue
seen.add(earlier)
_, _, name, asm = self.ins[earlier]
wrote = self.descriptor_writes_by_instruction[earlier] | scalar_destinations(name, asm)
if pointer in wrote or 'any' in wrote:
if 'any' in wrote or not regs.isdisjoint(wrote):
writers.add(earlier)
else:
elif earlier not in seen:
seen.add(earlier)
expand.append(earlier)
return writers

def formatted_pointer_copy(self, pointer, before):
"""(source register, instruction index) when every path brings the same s_mov_b32 of the pointer."""
writers = self.descriptor_last_writers({pointer}, before)
if len(writers) != 1:
return None
index, = writers
if index is None:
return None
_, _, name, asm = self.ins[index]
_, args, _ = parse(asm)
source = reg_range(args[1]) if name == 's_mov_b32' and len(args) == 2 else None
Expand Down Expand Up @@ -7877,6 +7885,42 @@ def formatted_descriptor_from_block(self, reg, first):
return self.entry_table_pointers.get(pointer) == first[1] and self.formatted_pointer_reaches(pointer, read)
return False

def formatted_descriptor_from_predecessor(self, reg, first):
"""Prove one literal table load supplies the unchanged V# on every path to this use."""
writers = self.descriptor_last_writers(set(range(reg[1], reg[2] + 1)), self.descriptor_instruction_index)
if len(writers) != 1 or None in writers:
return False
read, = writers
if read >= self.descriptor_block_first:
return False # the local-load proof already handled this block
_, _, name, asm = self.ins[read]
_, args, mods = parse(asm)
if not re.fullmatch(r's_load_dwordx(4|8|16)', name) or len(args) != 3:
return False
dst, base = map(reg_range, args[:2])
offset = _lit_u32(smem_offset(args[2]))
if (not dst or dst[0] != 's' or dst[1] + first[3] != reg[1] or dst[2] < reg[2]
or not base or base[0] != 's' or base[2] != base[1] + 1 or base[2] > 105
or offset is None or offset != first[2] or any(m not in ('glc', 'dlc') for m in mods)):
return False

# Match the bindable 32-bit table address, including the high word at the point of the scalar load.
high = self.descriptor_last_writers({base[2]}, read)
if len(high) != 1 or None in high:
return False
high_at, = high
_, _, high_name, high_asm = self.ins[high_at]
_, high_args, high_mods = parse(high_asm)
if high_name != 's_mov_b32' or high_args != ['s%d' % base[2], '0'] or high_mods:
return False

pointer, before = base[1], read
copy = self.formatted_pointer_copy(pointer, before)
if copy:
pointer, before = copy
return (self.entry_table_pointers.get(pointer) == first[1]
and self.descriptor_last_writers({pointer}, before) == {None})

def mubuf_format(self, ops, mods, count):
"""buffer_load_format_x/xy/xyz/xyzw: FORMAT and DST_SEL come from word 3 of the descriptor, known at draw time.

Expand All @@ -7901,7 +7945,8 @@ def mubuf_format(self, ops, mods, count):
raise Unsupported('formatted buffer load through a descriptor whose words were rewritten')
if first[1][0] != 'table':
raise Unsupported('formatted buffer load requires a hardware table descriptor')
if self.past_entry and not self.formatted_descriptor_from_block(r, first):
if (self.past_entry and not self.formatted_descriptor_from_block(r, first)
and not self.formatted_descriptor_from_predecessor(r, first)):
raise Unsupported('formatted buffer load requires descriptor words proven in the entry block or a local table load')
word3 = 'ud%d' % self.pipe.ud(('descword3', self.pipe.resources[self.descriptor(ops[2])]['index']))
self.emit('a = %s;' % address)
Expand Down
Loading