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
49 changes: 48 additions & 1 deletion tests/startup_gfx2hlsl_readfirstlane_uniform_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ def resource(self, key, kind):
ALIAS_LOAD = 's_buffer_load_dwordx2 s[2:3], s[4:7], s2'


def run(*instructions, exec_known=True, kind=None):
def run(*instructions, exec_known=True, kind=None, group=None):
"""Translate disassembly text using the same dispatch and branch scan as a real stage."""
s = G.Stage.__new__(G.Stage)
s.pipe, s.lines = Pipe(), []
if group:
s.pipe.group = group
s.attrs = {}
s.sym = {4 + i: ('desc', ('table', 0), 0, i) for i in range(4)}
s.sym[3] = ('fixed_buffer_word',)
Expand Down Expand Up @@ -239,6 +241,51 @@ def run(*instructions, exec_known=True, kind=None):
finally:
G.UNIFORM_LOADS = original

# tid.x >> n is 0 in every lane when the group's X size is at most 1 << n (the thread id VGPR is untouched).
WAVE = 'v_lshrrev_b32_e32 v1, 6, v0'
READ1 = 'v_readfirstlane_b32 s9, v1'
for size in (1, 33, 64):
got = run(WAVE, READ1, kind='cs', group=(size, 1, 1))
check(f'wave index of a group of {size} accepted', got[-1] == 's9 = v1;', repr(got))
got = run(WAVE, 's_cbranch_scc0 label_0010', READ1, 's_endpgm', 's_endpgm', kind='cs', group=(64, 1, 1))
check('wave index read past the entry block recomputed as zero', got[-1] == 's9 = 0u;', repr(got))
for label, instructions, kind, group in (
('wave index of a group of 65', (WAVE, READ1), 'cs', (65, 1, 1)),
('wave index of a group of 128', (WAVE, READ1), 'cs', (128, 1, 1)),
('wave index of a group with no recorded size', (WAVE, READ1), 'cs', None),
('shift by 5 of a group of 64', ('v_lshrrev_b32_e32 v1, 5, v0', READ1), 'cs', (64, 1, 1)),
('wave index in a graphics stage', (WAVE, READ1), 'ps', (64, 1, 1)),
('wave index without a stage kind', (WAVE, READ1), None, (64, 1, 1)),
('wave index of v1', ('v_lshrrev_b32_e32 v1, 6, v1', READ1), 'cs', (64, 1, 1)),
('wave index after v0 is overwritten', ('v_mov_b32_e32 v0, 200', WAVE, READ1), 'cs', (64, 1, 1)),
('wave index after v0 is loaded', ('buffer_load_dword v[0:1], off, s[4:7], s2', WAVE, READ1), 'cs', (64, 1, 1)),
('wave index after v0 is swapped', ('v_swap_b32 v2, v0', WAVE, READ1), 'cs', (64, 1, 1)),
('wave index with an output modifier', ('v_lshrrev_b32_e64 v1, 6, v0 clamp', READ1), 'cs', (64, 1, 1)),
('wave index of a register shift', ('v_lshrrev_b32_e32 v1, v2, v0', READ1), 'cs', (64, 1, 1))):
got = run(*instructions, kind=kind, group=group)
check(f'{label} refused', got == ("v_lshrrev_b32 with ['clamp']" if 'modifier' in label else REFUSAL), repr(got))

# The global thread id of a 64-wide group, (uniform << 6) + tid.x, shifted back by 6 is the uniform part.
GID = 'v_lshl_add_u32 v0, s2, 6, v0'
got = run(GID, WAVE, READ1, kind='cs', group=(64, 1, 1))
check('global thread id shifted back accepted', got[-1] == 's9 = v1;', repr(got))
got = run(GID, WAVE, 's_cbranch_scc0 label_0014', READ1, 's_endpgm', 's_endpgm', kind='cs', group=(64, 1, 1))
check('thread id base shifted back recomputed past the entry block', got[-1] == 's9 = ((s2 << 6u) >> 6u);', repr(got))
got = run(GID, WAVE, 's_mov_b32 s2, 5', 's_cbranch_scc0 label_0018', READ1, 's_endpgm', 's_endpgm', kind='cs', group=(64, 1, 1))
check('thread id base shifted back refused past the entry block after the scalar changed', got == REFUSAL, repr(got))
for label, instructions, group in (
('thread id base of a group of 128', (GID, WAVE, READ1), (128, 1, 1)),
('thread id base shifted by more than the scale', ('v_lshl_add_u32 v0, s2, 6, v0', 'v_lshrrev_b32_e32 v1, 7, v0', READ1), (64, 1, 1)),
('thread id base from a varying scalar', (COMPARE, 'v_lshl_add_u32 v0, s2, 6, v0', WAVE, READ1), (64, 1, 1)),
('thread id base with the scalar rewritten', (GID, 's_mov_b32 s2, vcc_lo', WAVE, READ1), (64, 1, 1)),
('thread id base from a vector', ('v_lshl_add_u32 v0, v5, 6, v0', WAVE, READ1), (64, 1, 1)),
('thread id base added to another vector', ('v_lshl_add_u32 v0, s2, 6, v4', WAVE, READ1), (64, 1, 1)),
('thread id base added to a rewritten v0', ('v_mov_b32_e32 v0, 3', GID, WAVE, READ1), (64, 1, 1)),
('thread id base rewritten after', (GID, 'v_add_nc_u32_e32 v0, s3, v0', WAVE, READ1), (64, 1, 1)),
('thread id base scaled by 32', ('v_lshl_add_u32 v0, s2, 5, v0', WAVE, READ1), (64, 1, 1))):
got = run(*instructions, kind='cs', group=group)
check(f'{label} refused', got == REFUSAL, repr(got))

# Older probes may provide either no tracker or only part of the tracking state.
for present in ((), ('uniform_v',), ('uniform_v', 'varying_s')):
s = G.Stage.__new__(G.Stage)
Expand Down
64 changes: 63 additions & 1 deletion tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3011,6 +3011,55 @@ def join_writes(self, branches):
def tracks_uniformity(self):
return all(hasattr(self, name) for name in ('uniform_v', 'varying_s', 'scc_uniform'))

def vgpr_writer(self, reg, before, after=-1):
"""Index of the last instruction in self.ins[after + 1:before] that may write VGPR reg (the first operand,
and the second of v_swap_b32), or None."""
for i in range(before - 1, after, -1):
_, _, name, asm = self.ins[i]
ops = parse(asm)[1]
for tok in (ops[:2] if name.startswith('v_swap') else ops[:1]):
r = reg_range(tok)
if r and r[0] == 'v' and r[1] <= reg <= r[2]:
return i
return None

def tid_shift(self, count, source):
"""The uniform expression of `v_lshrrev_b32 vD, count, source` in the entry block of a compute program, or None.

The X thread id (v0, each local id has its own VGPR) is below the group's X size, so a right shift by n with
size <= 1 << n clears every thread-id bit. That gives 0 for the thread id itself, and for a vector built as
(uniform << K) + v0 with n <= K and size <= 1 << K the low K bits of the base are clear, the sum does not
carry, and the result is the same in every lane. Program order is execution order here, so the nearest
earlier writer is the definition; v0 must be unwritten at the definition and the scalar unwritten after it."""
src, now = reg_range(source), self.pc_index()
if (not re.fullmatch(r'0x[0-9a-f]+|\d+', count) or int(count, 0) >= 32 or not src or src[0] != 'v' or
src[1] != src[2] or now is None):
return None
n, size = int(count, 0), getattr(self.pipe, 'group', (1 << 31,))[0]
if size > 1 << n:
return None
defined = self.vgpr_writer(src[1], now)
if src[1] == 0 and defined is None:
return '0u'
if defined is None:
return None
_, _, name, asm = self.ins[defined]
_, d, mods = parse(asm)
base = re.sub(r'_e(32|64)$', '', name)
scalar = reg_range(d[1]) if len(d) == 4 else None
if (base != 'v_lshl_add_u32' or mods or not scalar or scalar[0] != 's' or scalar[1] != scalar[2] or
scalar[1] > 105 or d[3] != 'v0' or not re.fullmatch(r'0x[0-9a-f]+|\d+', d[2]) or
n > int(d[2], 0) or int(d[2], 0) >= 32 or size > 1 << int(d[2], 0) or
self.vgpr_writer(0, defined) is not None or not self.uniform_source(d[1])):
return None
for _, _, later, later_asm in self.ins[defined + 1:now]:
if scalar[1] in scalar_destinations(later, later_asm) or 'any' in scalar_destinations(later, later_asm):
return None
return '((s%d << %du) >> %du)' % (scalar[1], int(d[2], 0), n)

def pc_index(self):
return next((i for i, ins in enumerate(self.ins) if ins[0] == self.pc), None)

def uniform_source(self, tok):
r = reg_range(tok)
if r:
Expand Down Expand Up @@ -3076,6 +3125,17 @@ def one(self, name, asm):
dst = candidate[1]
else:
loaded = False
# A shift right of the X thread id, or of (uniform << K) + thread id, that leaves no thread-id bit.
shifted = None
if (dst is None and base == 'v_lshrrev_b32' and not mods and len(ops) == 3 and
self.tracks_uniformity() and hasattr(self, 'uniform_expr') and getattr(self, 'kind', None) == 'cs' and
not getattr(self, 'past_entry', True) and getattr(self, 'exec_known', False)):
candidate = reg_range(ops[0])
shifted = self.tid_shift(ops[1], ops[2])
if shifted is not None and candidate and candidate[0] == 'v' and candidate[1] == candidate[2]:
dst = candidate[1]
else:
shifted = None
self.last_v = None
if hasattr(self, 'heap_index'): # unit probes build a Stage without the tracking state
# Any write ends a register's mark; the lowering below sets the new one, if any.
Expand All @@ -3101,7 +3161,9 @@ def one(self, name, asm):
# Proven sources are scalars, constants and VGPRs; each VGPR needs its own current expression.
expr = self.last_v[1] if self.last_v and self.last_v[0] == dst else None
vs = [int(n) for n in re.findall(r'\bv(\d+)\b', expr or '')]
if expr is None or loaded or any(n not in self.uniform_expr for n in vs):
if shifted is not None:
self.uniform_expr[dst] = shifted
elif expr is None or loaded or any(n not in self.uniform_expr for n in vs):
self.uniform_expr.pop(dst, None)
else:
self.uniform_expr[dst] = re.sub(r'\bv(\d+)\b', lambda m: '(%s)' % self.uniform_expr[int(m[1])],
Expand Down
Loading