From e7cb106c0843dfb7edc358c50f9b4af74234f7ee Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 00:56:51 +0100 Subject: [PATCH 01/11] fix: carry descriptor pointers through s_add_u32/s_addc_u32 in gfx2hlsl A table or root pointer advanced by a literal offset now keeps its source and byte offset, so a following s_load reads the advanced descriptor. A register addend on a pointer is refused instead of silently keeping the old pointer. Part of #1723 --- CMakeLists.txt | 4 + tests/startup_gfx2hlsl_desc_ptr_prop_check.py | 101 ++++++++++++++++++ tools/startup/gfx2hlsl.py | 50 ++++++++- 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 tests/startup_gfx2hlsl_desc_ptr_prop_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 05927f896a..774c00cf49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2663,6 +2663,10 @@ if(EXISTS "${INTAKE_PYTHON}") add_test(NAME startup_gfx2hlsl_op_refusals COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_op_refusals_check.py") set_tests_properties(startup_gfx2hlsl_op_refusals PROPERTIES TIMEOUT 60) + # gfx2hlsl carries a descriptor pointer through s_add_u32 / s_addc_u32 with a literal offset (#1723). + add_test(NAME startup_gfx2hlsl_desc_ptr_prop COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_desc_ptr_prop_check.py") + set_tests_properties(startup_gfx2hlsl_desc_ptr_prop PROPERTIES TIMEOUT 60) # gfx2hlsl refuses a binary32 or integer vector operation with a source too few or too many. add_test(NAME startup_gfx2hlsl_operand_count COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_operand_count_check.py") diff --git a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py new file mode 100644 index 0000000000..fcc4828520 --- /dev/null +++ b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py @@ -0,0 +1,101 @@ +"""Check descriptor pointer propagation through s_add_u32 / s_addc_u32 in tools/startup/gfx2hlsl.py (#1723). + +usage: startup_gfx2hlsl_desc_ptr_prop_check.py + +A shader advances a descriptor table or root pointer with `s_add_u32 sD, sP, ` and +`s_addc_u32 sD+1, sP+1, 0`, into the same or another register pair, then loads descriptors through +the result. The load must read at the advanced byte offset. A non-literal addend must be refused, +never silently treated as the unchanged pointer. +""" +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'tools', 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH ' + detail}") + if not condition: + failures.append(label) + + +class Pipe: + sampler_heap = False + + def __init__(self): + self.keys = [] + self.modified = set() + + def resource(self, key, kind): + if (key, kind) not in self.keys: + self.keys.append((key, kind)) + return 'r%d' % self.keys.index((key, kind)) + + +def stage(symbols): + s = G.Stage.__new__(G.Stage) + s.pipe, s.sym, s.lines = Pipe(), dict(symbols), [] + s.used_s, s.used_v, s.uniform_v, s.exec_known = set(), set(), set(), True + s.masks, s.varying_s = set(), set() + s.scc_uniform = True + return s + + +def run(symbols, program): + """Symbols after the (op, operands) steps, or the Unsupported message.""" + s = stage(symbols) + try: + for op, operands in program: + if op == 's_load_dwordx4': + s.op_s_load_dwordx4(operands.split(', '), []) + else: + s.lower(op, '%s %s' % (op, operands), operands.split(', '), []) + except G.Unsupported as error: + return 'refused: %s' % error + return s + + +TABLE = {8: ('ptr', ('table', 0)), 9: ('ptrhi', ('table', 0))} +ROOT = {8: ('ptr', ('root', 1)), 9: ('ptrhi', ('root', 1))} +ADVANCE = [('s_add_u32', 's20, s8, 0x40'), ('s_addc_u32', 's21, s9, 0')] +LOAD = [('s_load_dwordx4', 's[4:7], s[20:21], 0x10')] + +s = run(TABLE, ADVANCE + LOAD) +check('table pointer advanced into another pair, descriptor read at pointer + 0x40 + 0x10', + not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0x50, 0), str(s if isinstance(s, str) else s.sym)) + +same = [('s_add_u32', 's8, s8, 0x20'), ('s_addc_u32', 's9, s9, 0')] +s = run(TABLE, same + [('s_load_dwordx4', 's[4:7], s[8:9], 0')]) +check('table pointer advanced in place', + not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0x20, 0), str(s if isinstance(s, str) else s.sym)) + +twice = [('s_add_u32', 's20, s8, 0x40'), ('s_addc_u32', 's21, s9, 0'), + ('s_add_u32', 's22, s20, 0x10'), ('s_addc_u32', 's23, s21, 0')] +s = run(TABLE, twice + [('s_load_dwordx4', 's[4:7], s[22:23], 0x4')]) +check('two advances add up', + not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0x54, 0), str(s if isinstance(s, str) else s.sym)) + +s = run(ROOT, ADVANCE + LOAD) +check('root constant-buffer pointer advanced, buffer read at the advanced offset', + not isinstance(s, str) and any('Load(80)' in line for line in s.lines), str(s if isinstance(s, str) else s.lines)) + +s = run(TABLE, [('s_add_u32', 's8, s8, s2')]) +check('a register addend on a pointer is refused', isinstance(s, str) and 'non-literal' in s, str(s)) + +s = run(TABLE, ADVANCE + [('s_load_dwordx4', 's[4:7], s[20:21], s2')]) +check('a register offset through an advanced pointer is still refused', isinstance(s, str), str(s)) + +s = run(TABLE, ADVANCE) +try: + s.descriptor('s[20:21]') + check('an advanced pointer used whole as a descriptor is refused', False, 'accepted') +except G.Unsupported: + check('an advanced pointer used whole as a descriptor is refused', True) + +if failures: + print('FAILED: %s' % ', '.join(failures)) + sys.exit(1) +print('startup_gfx2hlsl_desc_ptr_prop: ok') diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 36890a6ec7..941ba16be7 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1053,6 +1053,8 @@ def descriptor(self, tok): # descriptors): its offset is the load's offset plus the first register's dword index. return s[1], (None if s[2] is None else s[2] + 4 * s[3]) if s and s[0] == 'ptr': + if len(s) > 2 and s[2]: + raise Unsupported('descriptor operand %s is a pointer with offset %#x' % (tok, s[2])) return s[1], None raise Unsupported('descriptor operand %s holds %s' % (tok, s)) @@ -1416,6 +1418,14 @@ def lower(self, base, asm, ops, mods): return self.set_v(reg_range(ops[0])[1], text.format(*args)) if base in SCA: return self.scalar(base, ops) + if base == 's_addc_u32': + # Only the high word of an advanced descriptor pointer; the carry is not modelled. + hi = reg_range(ops[1]) if len(ops) == 3 else None + if hasattr(self, 'scc_uniform') and self.tracks_uniformity(): + self.scc_uniform = False + if hi and hi[0] == 's' and self.pointer_add(base, ops[0], ops[1:], [self.sym.get(hi[1]), None]): + return None + raise Unsupported(base) if base in ('buffer_load_format_x', 'buffer_load_format_xy', 'buffer_load_format_xyz', 'buffer_load_format_xyzw'): # MUBUF formatted loads take both fields from V# word 3. The startup request carries @@ -1588,6 +1598,35 @@ def sdwa(self, base, ops, mods): destination in self.low16_only_v) self.set_v(destination, sdwa_dest(result, select, unused, 'v%d' % destination), low16_only=keep_low16) + def pointer_add(self, base, dname, srcs, syms): + """Propagate a descriptor pointer through s_add_u32 sD, sP, / s_addc_u32 sD+1, sP+1, 0. + + The pair advances a table or root pointer by a literal byte offset, into the same or + another register pair. The offset rides in the low word as ('ptr', source, offset); the + high word keeps ('ptrhi', source). The low-word carry is taken as zero: the base is a + table or root address and the offset is below 1 MiB. Anything else returns False. + """ + if base not in ('s_add_u32', 's_addc_u32') or len(srcs) != 2 or not syms[0]: + return False + reg = reg_range(dname) + if not reg or reg[0] != 's' or reg[1] != reg[2] or reg[1] > 105: + return False + d = reg[1] + if base == 's_add_u32': + if syms[0][0] != 'ptr' or not re.fullmatch(r'(?:0[xX][0-9a-fA-F]+|\d+)', srcs[1]): + return False + added = int(srcs[1], 0) + if added > 0xfffff: + raise Unsupported('descriptor pointer offset %s' % srcs[1]) + self.sym[d] = ('ptr', syms[0][1], (syms[0][2] if len(syms[0]) > 2 else 0) + added) + return True + low = self.sym.get(d - 1) + if (syms[0][0] != 'ptrhi' or srcs[1] != '0' or not low or low[0] != 'ptr' + or low[1] != syms[0][1]): + return False + self.sym[d] = ('ptrhi', syms[0][1]) + return True + # scalar ALU with descriptor-word bookkeeping def scalar(self, base, ops): if base == 's_movk_i32' and len(ops) != 2: @@ -1641,6 +1680,8 @@ def src_sym(t): raise Unsupported('scc from descriptor data') if word_pack: raise Unsupported('%s on descriptor data' % base) + if self.pointer_add(base, dname, srcs, syms): + return None if dname == 'm0': if scc: raise Unsupported('scc from descriptor data') @@ -1660,6 +1701,8 @@ def src_sym(t): self.sym.pop(d + 1, None) if self.tracks_uniformity(): self.varying_s.add(d + 1) + elif own and own[0] == 'ptr' and base in ('s_add_u32', 's_addc_u32'): + raise Unsupported('%s adds a non-literal to a descriptor pointer' % base) elif own and own[0] in ('desc', 'ptr', 'ptrhi') and any(s == own for s in syms): if own[0] == 'desc': self.pipe.modified.add(desc_key(own)) @@ -1930,6 +1973,7 @@ def op_s_load_dwordx4(self, ops, mods, count=4): p = self.sym.get(base[1]) if not p or p[0] != 'ptr': raise Unsupported('s_load through s%d holding %s' % (base[1], p)) + base_off = p[2] if len(p) > 2 else 0 # literal bytes added by s_add_u32 on the pointer # 'off' names no offset operand at all, so the byte offset is zero, as for the literal 0. token = smem_offset(ops[2]) if re.fullmatch(r'-?(?:0[xX][0-9a-fA-F]+|\d+)', token): @@ -1941,11 +1985,13 @@ def op_s_load_dwordx4(self, ops, mods, count=4): if p[1][0] == 'root': buf = self.pipe.resource((p[1], None), 'buf') for i in range(count): - self.set_s(dst[1] + i, '%s.Load(%d)' % (buf, off + 4 * i)) + self.set_s(dst[1] + i, '%s.Load(%d)' % (buf, base_off + off + 4 * i)) return for i in range(count): - self.sym[dst[1] + i] = ('desc', p[1], off, i) + self.sym[dst[1] + i] = ('desc', p[1], base_off + off, i) return + if base_off: + raise Unsupported('s_load through %s advanced by %#x' % (p[1], base_off)) if p[1] == ('smpheap',): if self.pipe.sampler_heap and count == 4: # A sampler picked at run time from the sampler heap: keep the byte offset in the From 50a8ebed8b471cfc626baa3b6e88cf4ad5640b52 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 11:56:51 +0100 Subject: [PATCH 02/11] shader: refuse descriptor pointer advances across branches and detached s_addc_u32 in gfx2hlsl (Part of #1723) --- tests/startup_gfx2hlsl_desc_ptr_prop_check.py | 17 +++++++++++++++-- tools/startup/gfx2hlsl.py | 9 ++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py index fcc4828520..1f224dbf01 100644 --- a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py +++ b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py @@ -44,11 +44,14 @@ def stage(symbols): return s -def run(symbols, program): +def run(symbols, program, branches=False): """Symbols after the (op, operands) steps, or the Unsupported message.""" s = stage(symbols) + s.has_branches = branches + s.ins = [(4 * i, 4, op, '') for i, (op, _operands) in enumerate(program)] try: - for op, operands in program: + for i, (op, operands) in enumerate(program): + s.pc = 4 * i if op == 's_load_dwordx4': s.op_s_load_dwordx4(operands.split(', '), []) else: @@ -95,6 +98,16 @@ def run(symbols, program): except G.Unsupported: check('an advanced pointer used whole as a descriptor is refused', True) +s = run(TABLE, ADVANCE, branches=True) +check('a pointer advance in a program with branches is refused', isinstance(s, str), str(s)) + +# The high-word add is the carry of the low-word add right before it, never of an unrelated carry in SCC. +s = run(TABLE, [('s_cmp_eq_u32', '0, 0'), ('s_addc_u32', 's9, s9, 0')]) +check('s_addc_u32 without the low-word add is refused', isinstance(s, str), str(s)) + +s = run(TABLE, [ADVANCE[0], ('s_cmp_eq_u32', '0, 0'), ADVANCE[1]]) +check('s_addc_u32 separated from its low-word add is refused', isinstance(s, str), str(s)) + if failures: print('FAILED: %s' % ', '.join(failures)) sys.exit(1) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 941ba16be7..f5c6c4f581 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1608,6 +1608,9 @@ def pointer_add(self, base, dname, srcs, syms): """ if base not in ('s_add_u32', 's_addc_u32') or len(srcs) != 2 or not syms[0]: return False + if self.has_branches: + # The symbolic update runs once in disassembly order; a branch could skip it or repeat it at run time. + return False reg = reg_range(dname) if not reg or reg[0] != 's' or reg[1] != reg[2] or reg[1] > 105: return False @@ -1619,10 +1622,14 @@ def pointer_add(self, base, dname, srcs, syms): if added > 0xfffff: raise Unsupported('descriptor pointer offset %s' % srcs[1]) self.sym[d] = ('ptr', syms[0][1], (syms[0][2] if len(syms[0]) > 2 else 0) + added) + # The carry-in of an s_addc_u32 is only the carry of this very add: remember where the next + # instruction starts so that the high-word add is accepted right behind it and nowhere else. + size = next(n for off, n, _name, _asm in self.ins if off == self.pc) + self.ptr_low_add = (d, self.pc + size) return True low = self.sym.get(d - 1) if (syms[0][0] != 'ptrhi' or srcs[1] != '0' or not low or low[0] != 'ptr' - or low[1] != syms[0][1]): + or low[1] != syms[0][1] or getattr(self, 'ptr_low_add', None) != (d - 1, self.pc)): return False self.sym[d] = ('ptrhi', syms[0][1]) return True From 72eee114a01eb852610ac15b43970ccba6094334 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 11:57:05 +0100 Subject: [PATCH 03/11] shader: keep the pointer-advance bookkeeping safe for stages without branch state (Part of #1723) --- tools/startup/gfx2hlsl.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index f5c6c4f581..a5b79637c4 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1608,7 +1608,7 @@ def pointer_add(self, base, dname, srcs, syms): """ if base not in ('s_add_u32', 's_addc_u32') or len(srcs) != 2 or not syms[0]: return False - if self.has_branches: + if getattr(self, 'has_branches', False): # The symbolic update runs once in disassembly order; a branch could skip it or repeat it at run time. return False reg = reg_range(dname) @@ -1624,12 +1624,12 @@ def pointer_add(self, base, dname, srcs, syms): self.sym[d] = ('ptr', syms[0][1], (syms[0][2] if len(syms[0]) > 2 else 0) + added) # The carry-in of an s_addc_u32 is only the carry of this very add: remember where the next # instruction starts so that the high-word add is accepted right behind it and nowhere else. - size = next(n for off, n, _name, _asm in self.ins if off == self.pc) + size = next((n for off, n, _name, _asm in getattr(self, 'ins', ()) if off == self.pc), 0) self.ptr_low_add = (d, self.pc + size) return True low = self.sym.get(d - 1) if (syms[0][0] != 'ptrhi' or srcs[1] != '0' or not low or low[0] != 'ptr' - or low[1] != syms[0][1] or getattr(self, 'ptr_low_add', None) != (d - 1, self.pc)): + or low[1] != syms[0][1] or getattr(self, 'ptr_low_add', None) != (d - 1, getattr(self, 'pc', 0))): return False self.sym[d] = ('ptrhi', syms[0][1]) return True From 49f5fabe6cd06e7daa8e90f0724d6f8b3e179bc3 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 15:47:00 +0100 Subject: [PATCH 04/11] fix: keep the branch-join refusal for pointer adds in branching shaders --- tools/startup/gfx2hlsl.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index f8b9540f48..b5401bcc54 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2481,7 +2481,9 @@ def src_sym(t): self.sym[d + 1] = hi elif self.tracks_uniformity(): self.varying_s.add(d + 1) - elif own and own[0] == 'ptr' and base in ('s_add_u32', 's_addc_u32'): + elif (own and own[0] == 'ptr' and base in ('s_add_u32', 's_addc_u32') + and not getattr(self, 'has_branches', False)): + # With branches the offset is not tracked: the 'added' mark below stays, and the join refuses it. raise Unsupported('%s adds a non-literal to a descriptor pointer' % base) elif own and own[0] in ('desc', 'ptr', 'ptrhi') and any(s == own for s in syms): if own[0] == 'desc': From 41599ffd3a4c7b87a72417febde9cc2e46bf53e0 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 16:03:37 +0100 Subject: [PATCH 05/11] fix: require the matching high-word add before folding an advanced descriptor pointer --- tests/startup_gfx2hlsl_desc_ptr_prop_check.py | 23 +++++++++++++++++++ tools/startup/gfx2hlsl.py | 19 +++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py index 1f224dbf01..1953f29a15 100644 --- a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py +++ b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py @@ -108,6 +108,29 @@ def run(symbols, program, branches=False): s = run(TABLE, [ADVANCE[0], ('s_cmp_eq_u32', '0, 0'), ADVANCE[1]]) check('s_addc_u32 separated from its low-word add is refused', isinstance(s, str), str(s)) +# A fold needs the complete matching pair: the load must not trust the low word alone. +LOW = ADVANCE[0] +s = run(TABLE, [LOW] + LOAD) +check('missing high-word add is refused', isinstance(s, str), str(s)) + +s = run(TABLE, [('s_add_u32', 's8, s8, 0x20'), ('s_load_dwordx4', 's[4:7], s[8:9], 0')]) +check('missing high-word add in place is refused', isinstance(s, str), str(s)) + +s = run(TABLE, ADVANCE + [('s_mov_b32', 's21, 0')] + LOAD) +check('overwritten high word is refused', isinstance(s, str), str(s)) + +mixed = dict(TABLE) +mixed[10], mixed[11] = ('ptr', ('table', 1)), ('ptrhi', ('table', 1)) +s = run(mixed, [LOW, ('s_addc_u32', 's21, s11, 0')] + LOAD) +check('high word from another pointer is refused', isinstance(s, str), str(s)) + +s = run(mixed, [LOW, ('s_addc_u32', 's21, s10, 0')] + LOAD) +check('high word from a different register pair is refused', isinstance(s, str), str(s)) + +s = run(TABLE, [('s_add_u32', 's20, s8, 0xfffff'), ('s_addc_u32', 's21, s9, 0')] + LOAD) +check('complete pair with a large low-word add is folded', + not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0xfffff + 0x10, 0), str(s)) + if failures: print('FAILED: %s' % ', '.join(failures)) sys.exit(1) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index b5401bcc54..c6419db82b 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2391,13 +2391,20 @@ def pointer_add(self, base, dname, srcs, syms): # The carry-in of an s_addc_u32 is only the carry of this very add: remember where the next # instruction starts so that the high-word add is accepted right behind it and nowhere else. size = next((n for off, n, _name, _asm in getattr(self, 'ins', ()) if off == self.pc), 0) - self.ptr_low_add = (d, self.pc + size) + src = reg_range(srcs[0]) + self.ptr_low_add = (d, self.pc + size, src[1] if src and src[0] == 's' else None, + self.sym.get(src[1] + 1) if src and src[0] == 's' else None, syms[0][1]) + # The high word is stale until the matching s_addc_u32 completes the pair. + self.sym[d + 1] = ('ptrhi', syms[0][1], 'pending') return True low = self.sym.get(d - 1) - if (syms[0][0] != 'ptrhi' or srcs[1] != '0' or not low or low[0] != 'ptr' - or low[1] != syms[0][1] or getattr(self, 'ptr_low_add', None) != (d - 1, getattr(self, 'pc', 0))): + mark = getattr(self, 'ptr_low_add', None) + hi = reg_range(srcs[0]) + if (not mark or srcs[1] != '0' or not low or low[0] != 'ptr' or not hi or hi[0] != 's' + or mark[:2] != (d - 1, getattr(self, 'pc', 0)) or mark[2] is None or hi[1] != mark[2] + 1 + or mark[3] != ('ptrhi', mark[4]) or low[1] != mark[4]): return False - self.sym[d] = ('ptrhi', syms[0][1]) + self.sym[d] = ('ptrhi', mark[4]) return True # scalar ALU with descriptor-word bookkeeping @@ -2789,6 +2796,10 @@ def op_s_load_dwordx4(self, ops, mods, count=4): if not p or p[0] != 'ptr': raise Unsupported('s_load through s%d holding %s' % (base[1], p)) base_off = p[2] if len(p) > 2 else 0 # literal bytes added by s_add_u32 on the pointer + if base_off or len(p) > 2: + # An advanced pointer needs its high word completed by the matching s_addc_u32. + if self.sym.get(base[1] + 1) != ('ptrhi', p[1]): + raise Unsupported('s_load through s%d: its high word is not the matching advanced pair' % base[1]) # 'off' names no offset operand at all, so the byte offset is zero, as for the literal 0. token = smem_offset(ops[2]) if re.fullmatch(r'-?(?:0[xX][0-9a-fA-F]+|\d+)', token): From 91f4b2145997b6d84d8c5971edd583ca9bab766b Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 16:18:39 +0100 Subject: [PATCH 06/11] fix: advance 32-bit table pointers without a high word and judge the high operand by content --- tests/startup_gfx2hlsl_desc_ptr_prop_check.py | 72 ++++++++++++++----- tools/startup/gfx2hlsl.py | 31 ++++---- 2 files changed, 73 insertions(+), 30 deletions(-) diff --git a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py index 1953f29a15..413b328310 100644 --- a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py +++ b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py @@ -29,6 +29,9 @@ def __init__(self): self.keys = [] self.modified = set() + def ud(self, key): + return 'ud_%s' % '_'.join(map(str, key)) + def resource(self, key, kind): if (key, kind) not in self.keys: self.keys.append((key, kind)) @@ -61,22 +64,22 @@ def run(symbols, program, branches=False): return s -TABLE = {8: ('ptr', ('table', 0)), 9: ('ptrhi', ('table', 0))} +TABLE = {8: ('ptr', ('table', 0))} # a table pointer is 32 bits wide: no high word ROOT = {8: ('ptr', ('root', 1)), 9: ('ptrhi', ('root', 1))} ADVANCE = [('s_add_u32', 's20, s8, 0x40'), ('s_addc_u32', 's21, s9, 0')] +TABLE_ADVANCE = [('s_add_u32', 's20, s8, 0x40')] LOAD = [('s_load_dwordx4', 's[4:7], s[20:21], 0x10')] -s = run(TABLE, ADVANCE + LOAD) +s = run(TABLE, TABLE_ADVANCE + LOAD) check('table pointer advanced into another pair, descriptor read at pointer + 0x40 + 0x10', not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0x50, 0), str(s if isinstance(s, str) else s.sym)) -same = [('s_add_u32', 's8, s8, 0x20'), ('s_addc_u32', 's9, s9, 0')] +same = [('s_add_u32', 's8, s8, 0x20')] s = run(TABLE, same + [('s_load_dwordx4', 's[4:7], s[8:9], 0')]) check('table pointer advanced in place', not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0x20, 0), str(s if isinstance(s, str) else s.sym)) -twice = [('s_add_u32', 's20, s8, 0x40'), ('s_addc_u32', 's21, s9, 0'), - ('s_add_u32', 's22, s20, 0x10'), ('s_addc_u32', 's23, s21, 0')] +twice = [('s_add_u32', 's20, s8, 0x40'), ('s_add_u32', 's22, s20, 0x10')] s = run(TABLE, twice + [('s_load_dwordx4', 's[4:7], s[22:23], 0x4')]) check('two advances add up', not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0x54, 0), str(s if isinstance(s, str) else s.sym)) @@ -88,48 +91,83 @@ def run(symbols, program, branches=False): s = run(TABLE, [('s_add_u32', 's8, s8, s2')]) check('a register addend on a pointer is refused', isinstance(s, str) and 'non-literal' in s, str(s)) -s = run(TABLE, ADVANCE + [('s_load_dwordx4', 's[4:7], s[20:21], s2')]) +s = run(ROOT, ADVANCE + [('s_load_dwordx4', 's[4:7], s[20:21], s2')]) check('a register offset through an advanced pointer is still refused', isinstance(s, str), str(s)) -s = run(TABLE, ADVANCE) +s = run(ROOT, ADVANCE) try: s.descriptor('s[20:21]') check('an advanced pointer used whole as a descriptor is refused', False, 'accepted') except G.Unsupported: check('an advanced pointer used whole as a descriptor is refused', True) -s = run(TABLE, ADVANCE, branches=True) +s = run(ROOT, ADVANCE, branches=True) check('a pointer advance in a program with branches is refused', isinstance(s, str), str(s)) # The high-word add is the carry of the low-word add right before it, never of an unrelated carry in SCC. -s = run(TABLE, [('s_cmp_eq_u32', '0, 0'), ('s_addc_u32', 's9, s9, 0')]) +s = run(ROOT, [('s_cmp_eq_u32', '0, 0'), ('s_addc_u32', 's9, s9, 0')]) check('s_addc_u32 without the low-word add is refused', isinstance(s, str), str(s)) -s = run(TABLE, [ADVANCE[0], ('s_cmp_eq_u32', '0, 0'), ADVANCE[1]]) +s = run(ROOT, [ADVANCE[0], ('s_cmp_eq_u32', '0, 0'), ADVANCE[1]]) check('s_addc_u32 separated from its low-word add is refused', isinstance(s, str), str(s)) # A fold needs the complete matching pair: the load must not trust the low word alone. LOW = ADVANCE[0] -s = run(TABLE, [LOW] + LOAD) +s = run(ROOT, [LOW] + LOAD) check('missing high-word add is refused', isinstance(s, str), str(s)) -s = run(TABLE, [('s_add_u32', 's8, s8, 0x20'), ('s_load_dwordx4', 's[4:7], s[8:9], 0')]) +s = run(ROOT, [('s_add_u32', 's8, s8, 0x20'), ('s_load_dwordx4', 's[4:7], s[8:9], 0')]) check('missing high-word add in place is refused', isinstance(s, str), str(s)) -s = run(TABLE, ADVANCE + [('s_mov_b32', 's21, 0')] + LOAD) +s = run(ROOT, ADVANCE + [('s_mov_b32', 's21, 0')] + LOAD) check('overwritten high word is refused', isinstance(s, str), str(s)) -mixed = dict(TABLE) -mixed[10], mixed[11] = ('ptr', ('table', 1)), ('ptrhi', ('table', 1)) +mixed = dict(ROOT) +mixed[10], mixed[11] = ('ptr', ('root', 2)), ('ptrhi', ('root', 2)) s = run(mixed, [LOW, ('s_addc_u32', 's21, s11, 0')] + LOAD) check('high word from another pointer is refused', isinstance(s, str), str(s)) s = run(mixed, [LOW, ('s_addc_u32', 's21, s10, 0')] + LOAD) check('high word from a different register pair is refused', isinstance(s, str), str(s)) -s = run(TABLE, [('s_add_u32', 's20, s8, 0xfffff'), ('s_addc_u32', 's21, s9, 0')] + LOAD) +s = run(ROOT, [('s_add_u32', 's20, s8, 0xfffff'), ('s_addc_u32', 's21, s9, 0')] + LOAD) check('complete pair with a large low-word add is folded', - not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0xfffff + 0x10, 0), str(s)) + not isinstance(s, str) and any('Load(%d)' % (0xfffff + 0x10) in line for line in s.lines), str(s)) + +# Real entry state: a table pointer is one 32-bit register with no high word, so it advances with +# the low add alone and its neighbour register stays untouched. +def entry(params, kind='vs'): + s = stage({}) + s.kind, s.pipe.root = kind, {'params': params, 'flags': 0} + s.setup_entry() + return s.sym + + +REAL = entry([{'type': 0}, {'type': 2}]) +s = run(REAL, [('s_add_u32', 's20, s8, 0x40')] + LOAD) +check('real table pointer advanced by the low add alone, descriptor read at +0x50', + not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0x50, 0), str(s)) + +s = run(REAL, [('s_add_u32', 's8, s8, 0x40'), ('s_load_dwordx4', 's[4:7], s[8:9], 0x10')]) +check('real table pointer advanced in place keeps the next register symbol', + not isinstance(s, str) and s.sym.get(9) == REAL[9] and s.sym.get(4) == ('desc', ('table', 0), 0x50, 0), str(s)) + +s = run(REAL, [('s_add_u32', 's20, s9, 0x40'), ('s_addc_u32', 's21, s10, 0')] + LOAD) +check('real root pointer pair advanced through the load', + not isinstance(s, str) and any('Load(80)' in line for line in s.lines), str(s)) + +s = run(REAL, [('s_add_u32', 's20, s9, 0x40')] + LOAD) +check('real root pointer without its high add is refused', isinstance(s, str), str(s)) + +copied = dict(ROOT) +copied[30] = ('ptrhi', ('root', 1)) +s = run(copied, [LOW, ('s_addc_u32', 's21, s30, 0')] + LOAD) +check('a copied high word completes the pair', not isinstance(s, str), str(s)) + +viam0 = dict(ROOT) +viam0['m0'] = ('ptr', ('root', 1)) +s = run(viam0, [('s_add_u32', 's20, m0, 0x40'), ('s_addc_u32', 's21, s9, 0')] + LOAD) +check('a low word held in m0 completes the pair', not isinstance(s, str), str(s)) if failures: print('FAILED: %s' % ', '.join(failures)) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index c6419db82b..91194c934f 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -453,6 +453,10 @@ def fbits(x): return struct.unpack(' ('s', 8, 11); 's8' -> ('s', 8, 8); 'v[1:3]' -> ('v', 1, 3); 'exec'/'vcc' pairs too.""" m = re.fullmatch(r'([sv])\[(\d+):(\d+)\]', tok) @@ -2388,23 +2392,23 @@ def pointer_add(self, base, dname, srcs, syms): if added > 0xfffff: raise Unsupported('descriptor pointer offset %s' % srcs[1]) self.sym[d] = ('ptr', syms[0][1], (syms[0][2] if len(syms[0]) > 2 else 0) + added) - # The carry-in of an s_addc_u32 is only the carry of this very add: remember where the next - # instruction starts so that the high-word add is accepted right behind it and nowhere else. - size = next((n for off, n, _name, _asm in getattr(self, 'ins', ()) if off == self.pc), 0) - src = reg_range(srcs[0]) - self.ptr_low_add = (d, self.pc + size, src[1] if src and src[0] == 's' else None, - self.sym.get(src[1] + 1) if src and src[0] == 's' else None, syms[0][1]) - # The high word is stale until the matching s_addc_u32 completes the pair. - self.sym[d + 1] = ('ptrhi', syms[0][1], 'pending') + if syms[0][1][0] in PTR64_SOURCES and d + 1 <= 105: + # The carry-in of an s_addc_u32 is only the carry of this very add: remember where the next + # instruction starts so that the high-word add is accepted right behind it and nowhere else. + size = next((n for off, n, _name, _asm in getattr(self, 'ins', ()) if off == self.pc), 0) + self.ptr_low_add = (d, self.pc + size, syms[0][1]) + # The high word is stale until the matching s_addc_u32 completes the pair. + self.sym[d + 1] = ('ptrhi', syms[0][1], 'pending') return True low = self.sym.get(d - 1) mark = getattr(self, 'ptr_low_add', None) hi = reg_range(srcs[0]) + # The high operand is judged by what it holds, not by where it sits: a copied high word still counts. if (not mark or srcs[1] != '0' or not low or low[0] != 'ptr' or not hi or hi[0] != 's' - or mark[:2] != (d - 1, getattr(self, 'pc', 0)) or mark[2] is None or hi[1] != mark[2] + 1 - or mark[3] != ('ptrhi', mark[4]) or low[1] != mark[4]): + or mark[:2] != (d - 1, getattr(self, 'pc', 0)) or low[1] != mark[2] + or self.sym.get(hi[1]) != ('ptrhi', mark[2])): return False - self.sym[d] = ('ptrhi', mark[4]) + self.sym[d] = ('ptrhi', mark[2]) return True # scalar ALU with descriptor-word bookkeeping @@ -2796,8 +2800,9 @@ def op_s_load_dwordx4(self, ops, mods, count=4): if not p or p[0] != 'ptr': raise Unsupported('s_load through s%d holding %s' % (base[1], p)) base_off = p[2] if len(p) > 2 else 0 # literal bytes added by s_add_u32 on the pointer - if base_off or len(p) > 2: - # An advanced pointer needs its high word completed by the matching s_addc_u32. + if (base_off or len(p) > 2) and p[1][0] in PTR64_SOURCES: + # An advanced 64-bit pointer needs its high word completed by the matching s_addc_u32; + # a table pointer is 32 bits wide and has no high word. if self.sym.get(base[1] + 1) != ('ptrhi', p[1]): raise Unsupported('s_load through s%d: its high word is not the matching advanced pair' % base[1]) # 'off' names no offset operand at all, so the byte offset is zero, as for the literal 0. From 2641fe0dd1d8f101d89b879ae3238d702590e793 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 21:29:57 +0100 Subject: [PATCH 07/11] fix: keep descriptor pointer pair open until its matching s_addc_u32 --- tests/startup_gfx2hlsl_desc_ptr_prop_check.py | 14 ++++++++++++++ tools/startup/gfx2hlsl.py | 8 +++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py index 207f9844dd..e15d4f9c54 100644 --- a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py +++ b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py @@ -180,6 +180,20 @@ def entry(params, kind='vs'): check('the same in-place advance without branches still folds to +0x50', not isinstance(s, str) and s.sym.get(4) == ('desc', ('table', 0), 0x50, 0), str(s if isinstance(s, str) else s.sym)) +# The pair is complete only after its s_addc_u32: an in-place pair folds, and a high word that was only +# copied (never advanced) must not stand in for it. +s = run(ROOT, [('s_add_u32', 's8, s8, 0x20'), ('s_addc_u32', 's9, s9, 0'), + ('s_load_dwordx4', 's[4:7], s[8:9], 0x10')]) +check('complete in-place pair folds to +0x30', + not isinstance(s, str) and any('Load(%d)' % 0x30 in line for line in s.lines), str(s if isinstance(s, str) else s.lines)) + +s = run(ROOT, [ADVANCE[0], ('s_mov_b32', 's21, s9')] + LOAD) +check('out-of-place low add with a copied, unadvanced high word is refused', isinstance(s, str), str(s)) + +s = run(ROOT, [ADVANCE[0], ADVANCE[1]] + LOAD) +check('complete out-of-place pair leaves the original high source untouched', + not isinstance(s, str) and s.sym.get(9) == ('ptrhi', ('root', 1)), str(s if isinstance(s, str) else s.sym)) + if failures: print('FAILED: %s' % ', '.join(failures)) sys.exit(1) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 05a58fdb44..71d6aabd75 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2412,8 +2412,9 @@ def pointer_add(self, base, dname, srcs, syms): # instruction starts so that the high-word add is accepted right behind it and nowhere else. size = next((n for off, n, _name, _asm in getattr(self, 'ins', ()) if off == self.pc), 0) self.ptr_low_add = (d, self.pc + size, syms[0][1]) - # The high word is stale until the matching s_addc_u32 completes the pair. - self.sym[d + 1] = ('ptrhi', syms[0][1], 'pending') + # The pair stays open until the matching s_addc_u32 completes it. The high register is left as it + # is (in place it is still the original high source); a load through an open pair is refused. + self.__dict__.setdefault('ptr_open_pairs', set()).add(d) return True low = self.sym.get(d - 1) mark = getattr(self, 'ptr_low_add', None) @@ -2424,6 +2425,7 @@ def pointer_add(self, base, dname, srcs, syms): or self.sym.get(hi[1]) != ('ptrhi', mark[2])): return False self.sym[d] = ('ptrhi', mark[2]) + getattr(self, 'ptr_open_pairs', set()).discard(d - 1) return True # scalar ALU with descriptor-word bookkeeping @@ -2826,7 +2828,7 @@ def op_s_load_dwordx4(self, ops, mods, count=4): if (base_off or len(p) > 2) and p[1][0] in PTR64_SOURCES: # An advanced 64-bit pointer needs its high word completed by the matching s_addc_u32; # a table pointer is 32 bits wide and has no high word. - if self.sym.get(base[1] + 1) != ('ptrhi', p[1]): + if base[1] in getattr(self, 'ptr_open_pairs', ()) or self.sym.get(base[1] + 1) != ('ptrhi', p[1]): raise Unsupported('s_load through s%d: its high word is not the matching advanced pair' % base[1]) # 'off' names no offset operand at all, so the byte offset is zero, as for the literal 0. token = smem_offset(ops[2]) From 6158c9881bbc9da1940c6abf774466acf745e8ff Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 23:07:02 +0100 Subject: [PATCH 08/11] fix: let the descriptor pointer pair pass translate's SCC check, add WARP readback case --- tests/startup_smem_address_check.py | 43 +++++++++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 5 +++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/startup_smem_address_check.py b/tests/startup_smem_address_check.py index e2d53f0f3f..4f7175f0fe 100644 --- a/tests/startup_smem_address_check.py +++ b/tests/startup_smem_address_check.py @@ -291,6 +291,22 @@ def controls(): for word in words[:64]: assert 0 < ((word >> 23) & 0xff) < 0xff, hex(word) + # 10. A complete s_add_u32/s_addc_u32 root-pointer pair goes through the full translate() (with its SCC + # check) and reads the same bytes as the single load at the summed offset; any other SCC reader is refused. + assert build_smem_pointer_pair_pipeline().hlsl_vs() == build_smem_dwordx4_pipeline().hlsl_vs() + vs_scc = b''.join(( + encode_sop2(0x00, 20, 0, 128 + 8), + encode_sop2(0x04, 21, 1, 128 + 0), + encode_sop2(0x0a, 30, 129, 128), # s_cselect_b32 s30, 1, 0 reads the carry + encode_smem(SMEM_LOAD_DWORDX4, 16, 20, soffset=125, imm=0x8), + struct.pack('<3I', 0xf80008cf, 0x07060504, 0xbf810000), + )) + try: + G.Pipeline(vs_scc, struct.pack('<3I', 0xf800180f, 0, 0xbf810000), root_with_one_constant()).hlsl_vs() + raise AssertionError('an independent SCC read after the pointer pair was accepted') + except G.Unsupported as error: + assert 'scc' in str(error), error + # ---- D3D12 GPU readback cases ------------------------------------------------------------------------- @@ -309,6 +325,24 @@ def build_smem_dwordx4_pipeline(): return G.Pipeline(vs, ps, root_with_one_constant()) +def build_smem_pointer_pair_pipeline(): + """Same loads as build_smem_dwordx4_pipeline, but the root pointer is advanced by a complete + s_add_u32 s20, s0, 8 / s_addc_u32 s21, s1, 0 pair and the load adds offset:0x8 (total 0x10).""" + vs = b''.join(( + encode_sop2(0x00, 20, 0, 128 + 8), # s_add_u32 s20, s0, 8 + encode_sop2(0x04, 21, 1, 128 + 0), # s_addc_u32 s21, s1, 0 + encode_smem(SMEM_LOAD_DWORDX4, 16, 20, soffset=125, imm=0x8), + encode_smem(SMEM_BUFFER_LOAD_DWORDX4, 20, 16, soffset=8, imm=0x18, glc=1), + encode_vop1(0x01, 4, 20), + encode_vop1(0x01, 5, 21), + encode_vop1(0x01, 6, 22), + encode_vop1(0x01, 7, 23), + struct.pack('<3I', 0xf80008cf, 0x07060504, 0xbf810000), + )) + ps = struct.pack('<3I', 0xf800180f, 0, 0xbf810000) + return G.Pipeline(vs, ps, root_with_one_constant()) + + def build_smem_m0_and_overlap_pipeline(): """VS: m0 + offset:0x14 dlc, const SGPR + offset:0x08, and overlapping runtime SGPR s[20:21] + offset:0x1c glc.""" vs = b''.join(( @@ -416,6 +450,15 @@ def check_gpu(directory, compiler, consumer, adapter, fxc=None): print(f's_buffer_load_dwordx4 (soffset={soffset}, offset:0x18):', [l for l in res.stdout.splitlines() if 'mismatches=' in l][0]) + # 1b. Root pointer advanced by a complete s_add_u32/s_addc_u32 pair must read the same bytes as case 1. + p_pair = build_smem_pointer_pair_pipeline() + res_pair = run_buffer_case(directory, compiler, consumer, adapter, p_pair, p_pair.hlsl_vs(), + expected_smem_dwordx4(soffset=32), soffset=32) + if res_pair.returncode: + raise RuntimeError('advanced root pointer pair:\n' + res_pair.stdout + res_pair.stderr) + print('advanced root pointer (s_add_u32 + s_addc_u32, offset:0x8):', + [l for l in res_pair.stdout.splitlines() if 'mismatches=' in l][0]) + # 2. m0 + offset:0x14 dlc and destination-overlapping runtime SGPR s[20:21] + offset:0x1c glc. p_m0 = build_smem_m0_and_overlap_pipeline() src_m0 = p_m0.hlsl_vs() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 1d15d22b78..9a8440f302 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2602,7 +2602,8 @@ def descriptor_scc_reads(self, branches, effects): continue seen.add((off, stale)) size, name, asm = instructions[off] - if stale and off not in self.calculated_pc and reads_scc(name, asm): + if (stale and off not in self.calculated_pc and off not in getattr(self, 'ptr_addc_pcs', ()) + and reads_scc(name, asm)): yield stale, name if off in effects: stale = effects[off] if effects[off] != 'emitted' else None @@ -3219,6 +3220,8 @@ def pointer_add(self, base, dname, srcs, syms): return False self.sym[d] = ('ptrhi', mark[2]) getattr(self, 'ptr_open_pairs', set()).discard(d - 1) + # This s_addc_u32 is the only reader of the low add's elided carry; any other SCC reader stays refused. + self.__dict__.setdefault('ptr_addc_pcs', set()).add(self.pc) return True # scalar ALU with descriptor-word bookkeeping From 6688f74e12eb937c24b861bea9c260399bacc1a2 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 23:13:11 +0100 Subject: [PATCH 09/11] fix: refuse an untracked-offset pointer as a descriptor, run the pointer check in Linux CI --- .github/workflows/windows.yml | 1 + tests/startup_gfx2hlsl_desc_ptr_prop_check.py | 8 ++++++++ tests/startup_smem_address_check.py | 10 +++++----- tools/startup/gfx2hlsl.py | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 1837fd4118..a61f5c8583 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -320,6 +320,7 @@ jobs: tests/startup_gfx2hlsl_getpc_refusal_check.py \ tests/startup_gfx2hlsl_image_arity_check.py \ tests/startup_compute_lds_dual_atomics_check.py \ + tests/startup_gfx2hlsl_desc_ptr_prop_check.py \ tests/run_checks_parallel_check.py build-and-test: name: Build and test on Windows diff --git a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py index e15d4f9c54..4584ca763c 100644 --- a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py +++ b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py @@ -94,6 +94,14 @@ def run(symbols, program, branches=False): s = run(ROOT, ADVANCE + [('s_load_dwordx4', 's[4:7], s[20:21], s2')]) check('a register offset through an advanced pointer is still refused', isinstance(s, str), str(s)) +# The untracked-offset mark of a branchy program must be refused here too, never a TypeError. +s = stage({8: ('ptr', ('root', 1), 'added'), 9: ('ptrhi', ('root', 1))}) +try: + s.descriptor('s[8:9]') + check('an untracked-offset pointer used whole as a descriptor is refused', False, 'accepted') +except G.Unsupported: + check('an untracked-offset pointer used whole as a descriptor is refused', True) + s = run(ROOT, ADVANCE) try: s.descriptor('s[20:21]') diff --git a/tests/startup_smem_address_check.py b/tests/startup_smem_address_check.py index 4f7175f0fe..ba169a9e82 100644 --- a/tests/startup_smem_address_check.py +++ b/tests/startup_smem_address_check.py @@ -291,7 +291,7 @@ def controls(): for word in words[:64]: assert 0 < ((word >> 23) & 0xff) < 0xff, hex(word) - # 10. A complete s_add_u32/s_addc_u32 root-pointer pair goes through the full translate() (with its SCC + # 10. A complete s_add_u32/s_addc_u32 table-pointer pair goes through the full translate() (with its SCC # check) and reads the same bytes as the single load at the summed offset; any other SCC reader is refused. assert build_smem_pointer_pair_pipeline().hlsl_vs() == build_smem_dwordx4_pipeline().hlsl_vs() vs_scc = b''.join(( @@ -326,7 +326,7 @@ def build_smem_dwordx4_pipeline(): def build_smem_pointer_pair_pipeline(): - """Same loads as build_smem_dwordx4_pipeline, but the root pointer is advanced by a complete + """Same loads as build_smem_dwordx4_pipeline, but the vertex-buffer table pointer s[0:1] is advanced by a complete s_add_u32 s20, s0, 8 / s_addc_u32 s21, s1, 0 pair and the load adds offset:0x8 (total 0x10).""" vs = b''.join(( encode_sop2(0x00, 20, 0, 128 + 8), # s_add_u32 s20, s0, 8 @@ -450,13 +450,13 @@ def check_gpu(directory, compiler, consumer, adapter, fxc=None): print(f's_buffer_load_dwordx4 (soffset={soffset}, offset:0x18):', [l for l in res.stdout.splitlines() if 'mismatches=' in l][0]) - # 1b. Root pointer advanced by a complete s_add_u32/s_addc_u32 pair must read the same bytes as case 1. + # 1b. Vertex-buffer table pointer advanced by a complete s_add_u32/s_addc_u32 pair must read the same bytes as case 1. p_pair = build_smem_pointer_pair_pipeline() res_pair = run_buffer_case(directory, compiler, consumer, adapter, p_pair, p_pair.hlsl_vs(), expected_smem_dwordx4(soffset=32), soffset=32) if res_pair.returncode: - raise RuntimeError('advanced root pointer pair:\n' + res_pair.stdout + res_pair.stderr) - print('advanced root pointer (s_add_u32 + s_addc_u32, offset:0x8):', + raise RuntimeError('advanced vertex-buffer table pointer pair:\n' + res_pair.stdout + res_pair.stderr) + print('advanced vertex-buffer table pointer (s_add_u32 + s_addc_u32, offset:0x8):', [l for l in res_pair.stdout.splitlines() if 'mismatches=' in l][0]) # 2. m0 + offset:0x14 dlc and destination-overlapping runtime SGPR s[20:21] + offset:0x1c glc. diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 9a8440f302..97bd3fbe2f 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1967,7 +1967,7 @@ def descriptor(self, tok): return s[1], (None if s[2] is None else s[2] + 4 * s[3]) if s and s[0] == 'ptr': if len(s) > 2 and s[2]: - raise Unsupported('descriptor operand %s is a pointer with offset %#x' % (tok, s[2])) + raise Unsupported('descriptor operand %s is a pointer with offset %s' % (tok, hex(s[2]) if isinstance(s[2], int) else s[2])) return s[1], None raise Unsupported('descriptor operand %s holds %s' % (tok, s)) From 0f226c5cb04d401951ef0dded42ce9f8c4f528f5 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 00:34:22 +0100 Subject: [PATCH 10/11] fix: refuse a root pointer load whose high word was overwritten --- tests/startup_gfx2hlsl_desc_ptr_prop_check.py | 6 ++++++ tools/startup/gfx2hlsl.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py index 96b082b725..d57d3da348 100644 --- a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py +++ b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py @@ -159,6 +159,12 @@ def run(symbols, program, branches=False): check('in-place advance twice with each completed high word is folded', not isinstance(s, str) and any('Load(64)' in line for line in s.lines), str(s)) +s = run(ROOT, [('s_mov_b32', 's9, 0'), ('s_load_dwordx4', 's[4:7], s[8:9], 0x10')]) +check('untouched root pair with an overwritten high word is refused', isinstance(s, str), str(s)) + +s = run(ROOT, [('s_load_dwordx4', 's[4:7], s[8:9], 0x10')]) +check('untouched root pair still loads', not isinstance(s, str) and any('Load(16)' in l for l in s.lines), str(s)) + # Real entry state: a table pointer is one 32-bit register with no high word, so it advances with # the low add alone and its neighbour register stays untouched. def entry(params, kind='vs'): diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index c9ef06824c..3f2b2ff8b5 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3914,7 +3914,7 @@ def op_s_load_dwordx4(self, ops, mods, count=4): # The 'added' mark (an add whose offset is not tracked, as in a program with branches): no byte offset. raise Unsupported('s_load through s%d: the pointer was advanced by an untracked offset' % base[1]) base_off = p[2] if len(p) > 2 else 0 # literal bytes added by s_add_u32 on the pointer - if (base_off or len(p) > 2) and p[1][0] in PTR64_SOURCES: + if p[1][0] in PTR64_SOURCES: # An advanced 64-bit pointer needs its high word completed by the matching s_addc_u32; # a table pointer is 32 bits wide and has no high word. if base[1] in getattr(self, 'ptr_open_pairs', ()) or self.sym.get(base[1] + 1) != ptr_high_symbol(p[1], base_off): From ef3425a3bead85ab1259764352e1283bfa52c45e Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 00:37:37 +0100 Subject: [PATCH 11/11] Revert "fix: refuse a root pointer load whose high word was overwritten" This reverts commit 0f226c5cb04d401951ef0dded42ce9f8c4f528f5. --- tests/startup_gfx2hlsl_desc_ptr_prop_check.py | 6 ------ tools/startup/gfx2hlsl.py | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py index d57d3da348..96b082b725 100644 --- a/tests/startup_gfx2hlsl_desc_ptr_prop_check.py +++ b/tests/startup_gfx2hlsl_desc_ptr_prop_check.py @@ -159,12 +159,6 @@ def run(symbols, program, branches=False): check('in-place advance twice with each completed high word is folded', not isinstance(s, str) and any('Load(64)' in line for line in s.lines), str(s)) -s = run(ROOT, [('s_mov_b32', 's9, 0'), ('s_load_dwordx4', 's[4:7], s[8:9], 0x10')]) -check('untouched root pair with an overwritten high word is refused', isinstance(s, str), str(s)) - -s = run(ROOT, [('s_load_dwordx4', 's[4:7], s[8:9], 0x10')]) -check('untouched root pair still loads', not isinstance(s, str) and any('Load(16)' in l for l in s.lines), str(s)) - # Real entry state: a table pointer is one 32-bit register with no high word, so it advances with # the low add alone and its neighbour register stays untouched. def entry(params, kind='vs'): diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 3f2b2ff8b5..c9ef06824c 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3914,7 +3914,7 @@ def op_s_load_dwordx4(self, ops, mods, count=4): # The 'added' mark (an add whose offset is not tracked, as in a program with branches): no byte offset. raise Unsupported('s_load through s%d: the pointer was advanced by an untracked offset' % base[1]) base_off = p[2] if len(p) > 2 else 0 # literal bytes added by s_add_u32 on the pointer - if p[1][0] in PTR64_SOURCES: + if (base_off or len(p) > 2) and p[1][0] in PTR64_SOURCES: # An advanced 64-bit pointer needs its high word completed by the matching s_addc_u32; # a table pointer is 32 bits wide and has no high word. if base[1] in getattr(self, 'ptr_open_pairs', ()) or self.sym.get(base[1] + 1) != ptr_high_symbol(p[1], base_off):