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 d9fe245fdb2fc2914d563c343049a2ea34c1b242 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 03:00:38 +0100 Subject: [PATCH 2/2] test: WARP readback for readfirstlane of a uniform buffer load Compute case N: v_readfirstlane_b32 of a buffer load at a uniform address, xored into each lane's own word, dispatched on D3D12 and compared with a CPU reference. A per-lane address is refused. Control n-dropped-broadcast must mismatch. --- CMakeLists.txt | 2 +- tests/startup_gfx2hlsl_compute_check.py | 34 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59d7ca2445..2a6ffbb45f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3467,7 +3467,7 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter}) set_tests_properties(startup_gfx2hlsl_compute_mbcnt_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) foreach(control IN ITEMS swapped-axes dropped-store b-offset c-unpredicated f-peer f-broadcast g-swapped-offsets g-overlap - j-scalar-dropped h-inner-unpredicated k-block-entered m-else-unswitched) + j-scalar-dropped h-inner-unpredicated k-block-entered m-else-unswitched n-dropped-broadcast) add_test(NAME startup_gfx2hlsl_compute_${control}_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_check.py" control ${control} "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index b88fe0d480..75bfcc2b4e 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -393,6 +393,32 @@ def saveexec_fixture(): 'L-unorm': (1 << 12) | 1 | (4 << 3) | (7 << 6) | (5 << 9), # one, X, W, Y } +# Case N: v_readfirstlane_b32 of a buffer load whose address is the same for every lane (offset 0, no VGPR), xored +# into each lane's own read-buffer word. Lane i stores word0 ^ word[i]. SOURCE_N_VARYING loads the readfirstlane +# source at a per-lane offset (v0), which is not provably uniform and must be refused. +SOURCE_N = """\ +s_mov_b32 s10, s6 +s_mov_b32 s11, 0 +s_load_dwordx8 s[12:19], s[10:11], 0x0 +s_waitcnt lgkmcnt(0) +buffer_load_dword v1, off, s[12:15], 0 +s_waitcnt vmcnt(0) +v_readfirstlane_b32 s20, v1 +v_lshlrev_b32 v4, 2, v0 +buffer_load_dword v2, v4, s[12:15], 0 offen +s_waitcnt vmcnt(0) +v_xor_b32 v3, s20, v2 +buffer_store_dword v3, v4, s[16:19], 0 offen +s_endpgm +""" +WORDS_N = bytes.fromhex( + '06038abe' '80038bbe' '05030cf4000000fa' '7fc08cbf' '000030e000010380' + '703f8cbf' '0105287e' '82000834' '001030e004020380' + '703f8cbf' '1404063a' '001070e004030480' '000081bf') +N_LOAD = bytes.fromhex('000030e000010380') +N_LOAD_VARYING = bytes.fromhex('001030e000010380') +WORDS_N_VARYING = WORDS_N.replace(N_LOAD, N_LOAD_VARYING, 1) + CASES = { # name: (source, words, rsrc2, group size, group counts) 'A': (SOURCE_A, WORDS_A, USER7 | TGID_X | TGID_Y | TGID_Z | tidig(3), (2, 3, 4), (3, 2, 2)), @@ -408,6 +434,7 @@ def saveexec_fixture(): 'K': (SOURCE_K, WORDS_K, USER7 | TGID_X | tidig(1), (8, 1, 1), (4, 1, 1)), 'G': (SOURCE_G, WORDS_G, USER7 | TGID_X | (1 << 15), (64, 1, 1), (2, 1, 1)), **{name: (SOURCE_L, WORDS_L, USER7, (16, 1, 1), (1, 1, 1)) for name in L_WORD3}, + 'N': (SOURCE_N, WORDS_N, USER7 | tidig(1), (16, 1, 1), (1, 1, 1)), } # Single instructions the controls splice into case A before its store, recorded from llvm-mc 15. @@ -530,6 +557,9 @@ def expected(name): result = (one, one if data[at] == 255 else 0, one, 0) out[4 * lane:4 * lane + 4] = result return out + if name == 'N': + out[:16] = [input_word(0) ^ input_word(lane) for lane in range(16)] + return out producer_offsets = {'G': (1, 32), 'G-offset0-only': (31, 0), 'G-offset1-only': (0, 31)} if name in producer_offsets: # The original fill begins at dword 4, the single-field variants at dword 0. @@ -1211,6 +1241,9 @@ def phi(zero, write, read, between=(), inside=()): refused('compute d16 image load requires validated SRV formats', lambda: pipeline.cs.image(['v12', 'v[0:1]', 's[24:31]'], ['dim:SQ_RSRC_IMG_2D', 'dmask:0x3', 'd16'], 'load')) + # A readfirstlane of a buffer load at a uniform address translates; at a per-lane address it is refused. + assert 's20 = v1;' in translate('N').hlsl_cs() + refused('v_readfirstlane_b32 requires a proven uniform source', lambda: translate('N', code=WORDS_N_VARYING).hlsl_cs()) print('compute entry, numthreads, masked bit counts, manifest, offen store forms and the store, descriptor, execz and sample ' 'refusals pass') @@ -1255,6 +1288,7 @@ def check_binary(llvm_mc): 'g-swapped-offsets': ('G', 'v6 = lds[(a + 20u) >> 2];\n v7 = lds[(a + 144u) >> 2];', 'v6 = lds[(a + 144u) >> 2];\n v7 = lds[(a + 20u) >> 2];'), 'g-overlap': ('G', 'v7 = lds[(a + 144u)', 'v7 = lds[(v6 + 144u)'), + 'n-dropped-broadcast': ('N', 'v3 = s20 ^ v2;', 'v3 = v2;'), # lane i stores word[i], not word0 ^ word[i] 'l-byte-offset': ('L-uint', 'a = v0 * ud2 + v1 + 2;', 'a = v0 * ud2 + 2;'), 'l-swizzle': ('L-unorm', 'v6 = ((ud3 >> 3)', 'v6 = ((0u >> 3)'), 'l-integer-one': ('L-uint', 'c1 = w >> 16; c2 = 0u; c3 = 1u; one = 1u;', 'c1 = w >> 16; c2 = 0u; c3 = 1u;'),