From 8a69d81cb29e29751b03e7c82c03bc73247273b2 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 17:58:41 +0100 Subject: [PATCH 1/2] gfx2hlsl: prove a compute buffer load with a uniform address uniform v_readfirstlane_b32 needs a vector source with the same value in every active lane. A buffer_load_dword or buffer_load_format_x in a compute program reads a read-only descriptor (a descriptor used for a load and a store is already refused), so with a uniform VGPR index or offset, a uniform scalar offset and exec known in the entry block, every lane reads the same dword. Mark the destination uniform there. The value has no recomputable expression, so reads past the entry block still refuse. Tests: positive cases plus negative controls for a varying VGPR offset, a varying scalar offset, a graphics stage, no stage kind, unknown exec, a read after the entry block and a varying overwrite. Co-Authored-By: Claude Sonnet 5.5 --- ...up_gfx2hlsl_readfirstlane_uniform_check.py | 32 ++++++++++++++++++- tools/startup/gfx2hlsl.py | 18 ++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index eef9354837..cbf2fd3af6 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -28,7 +28,7 @@ def resource(self, key, kind): ALIAS_LOAD = 's_buffer_load_dwordx2 s[2:3], s[4:7], s2' -def run(*instructions, exec_known=True): +def run(*instructions, exec_known=True, kind=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(), [] @@ -37,6 +37,8 @@ def run(*instructions, exec_known=True): s.sym[3] = ('fixed_buffer_word',) s.used_s, s.used_v, s.masks = set(), set(), set() s.exec_known = exec_known + if kind: + s.kind = kind s.ins = [(4 * i, 4, asm.split()[0], asm) for i, asm in enumerate(instructions)] try: s.translate() @@ -200,6 +202,34 @@ def run(*instructions, exec_known=True): got = run('v_mov_b32_e32 v7, s2', READ, exec_known=False) check('unknown exec refused', got == REFUSAL, repr(got)) +# A compute load through a read-only descriptor with a uniform address is uniform; the value has no recomputable +# expression, so only the entry block reads it. +LOAD_OFF = 'buffer_load_dword v7, off, s[4:7], s2' +LOAD_OFFEN = 'buffer_load_dword v8, v7, s[4:7], 0 offen' +got = run(LOAD_OFF, READ, kind='cs') +check('uniform scalar offset load accepted', got[-2:] == ['v7 = r0.Load(a + 0);', 's9 = v7;'], repr(got)) +got = run(MOVE, LOAD_OFFEN, 'v_readfirstlane_b32 s9, v8', kind='cs') +check('uniform vgpr offset load accepted', got[-1] == 's9 = v8;' and len(got) == 4, repr(got)) +got = run(MOVE, 'buffer_load_dword v7, v7, s[4:7], 0 offen', READ, kind='cs') +check('load overwriting its uniform address accepted', got[-1] == 's9 = v7;', repr(got)) +for label, instructions, kind, known in ( + ('load with a varying vgpr offset', ('buffer_load_dword v8, v0, s[4:7], 0 offen', 'v_readfirstlane_b32 s9, v8'), 'cs', True), + ('load with a varying scalar offset', (COMPARE, 'buffer_load_dword v7, off, s[4:7], s2', READ), 'cs', True), + ('load in a graphics stage', (LOAD_OFF, READ), 'ps', True), + ('load without a stage kind', (LOAD_OFF, READ), None, True), + ('load with unknown exec', (LOAD_OFF, READ), 'cs', False), + ('load after the entry block', (LOAD_OFF, 's_cbranch_scc0 label_0010', READ, 's_endpgm', 's_endpgm'), 'cs', True), + ('load then a varying overwrite', (LOAD_OFF, 'v_mov_b32_e32 v7, v0', READ), 'cs', True)): + got = run(*instructions, kind=kind, exec_known=known) + check(f'{label} refused', got == REFUSAL, repr(got)) +original = G.UNIFORM_LOADS +G.UNIFORM_LOADS = frozenset() +try: + got = run(LOAD_OFF, READ, kind='cs') + check('control: load refused without tracking', got == REFUSAL, repr(got)) +finally: + G.UNIFORM_LOADS = original + # 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) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 221cedb43b..e3d9a9605a 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -654,6 +654,8 @@ def smem_mods(mods): 'v_fma_f32', 'v_fmaak_f32', 'v_rcp_f32', 'v_med3_f32', 'v_mov_b32', )) +UNIFORM_LOADS = frozenset(('buffer_load_dword', 'buffer_load_format_x')) + # DPP quad_perm selections that exchange lanes of a 2x2 pixel quad (lane = 2 * y + x), with the SM 6.0 intrinsic. QUAD_SWAPS = {(1, 0, 3, 2): 'QuadReadAcrossX', (2, 3, 0, 1): 'QuadReadAcrossY', (3, 2, 1, 0): 'QuadReadAcrossDiagonal'} @@ -1941,6 +1943,20 @@ def one(self, name, asm): sources = [re.sub(r'^-(?=[^\d.])', '', tok).strip('|') for tok in sources] if sources and all(self.uniform_source(tok) for tok in sources): dst = candidate[1] + # A compute load through a read-only descriptor (a descriptor is never both loaded and stored) with a uniform + # address reads the same dword in every lane. Entry block only: the value has no recomputable expression. + loaded = (dst is None and base in UNIFORM_LOADS and len(ops) == 4 and self.tracks_uniformity() and + getattr(self, 'kind', None) == 'cs' and not getattr(self, 'past_entry', True) and + getattr(self, 'exec_known', False) and + all(m in ('idxen', 'offen', 'glc', 'slc', 'dlc') or m.startswith('offset:') for m in mods) and + not ('idxen' in mods and 'offen' in mods) and (ops[1] == 'off') == ('idxen' not in mods and 'offen' not in mods) and + (ops[1] == 'off' or self.uniform_source(ops[1])) and self.uniform_source(ops[3])) + if loaded: + candidate = reg_range(ops[0]) + if candidate and candidate[0] == 'v' and candidate[1] == candidate[2]: + dst = candidate[1] + else: + loaded = False self.last_v = None term = self.lower(base, asm, ops, mods) uniform_written = self._uniform_written @@ -1961,7 +1977,7 @@ 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 any(n not in self.uniform_expr for n in vs): + if 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])], From c30127d368d2cdc7617f3590c8f3839252a3088d Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 18:09:04 +0100 Subject: [PATCH 2/2] gfx2hlsl: prove a thread-id shift uniform when it clears every thread-id bit In a compute entry block, v_lshrrev_b32 by n of the X thread id (v0, each local id has its own VGPR) is 0 in every lane when the group's X size is at most 1 << n. The same holds for (uniform << K) + v0 built by v_lshl_add_u32 with n <= K and size <= 1 << K: the low K bits of the base are clear, the sum does not carry, and the shifted value is the uniform part. The nearest earlier writer is the definition (program order is execution order in the entry block), v0 must be unwritten at it and the scalar unwritten after it. The value records a recomputable expression, so a read past the entry block is accepted too, until the scalar changes. Everything else keeps refusing by name: a larger group, a shift larger than the scale, a varying or rewritten scalar, a vector base, a rewritten v0, another stage and any modifier. Co-Authored-By: Claude Sonnet 5.5 --- ...up_gfx2hlsl_readfirstlane_uniform_check.py | 49 +++++++++++++- tools/startup/gfx2hlsl.py | 64 ++++++++++++++++++- 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index cbf2fd3af6..9e91bc4e44 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -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',) @@ -230,6 +232,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) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index e3d9a9605a..b5f31ed423 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1894,6 +1894,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: @@ -1957,6 +2006,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 term = self.lower(base, asm, ops, mods) uniform_written = self._uniform_written @@ -1977,7 +2037,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])],