Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e7cb106
fix: carry descriptor pointers through s_add_u32/s_addc_u32 in gfx2hlsl
tedin7 Nov 24, 2026
50a8ebe
shader: refuse descriptor pointer advances across branches and detach…
tedin7 Nov 25, 2026
72eee11
shader: keep the pointer-advance bookkeeping safe for stages without …
tedin7 Nov 25, 2026
e28fd3e
merge: bring origin/main into desc-ptr-prop
tedin7 Nov 25, 2026
49f5fab
fix: keep the branch-join refusal for pointer adds in branching shaders
tedin7 Nov 25, 2026
41599ff
fix: require the matching high-word add before folding an advanced de…
tedin7 Nov 25, 2026
91f4b21
fix: advance 32-bit table pointers without a high word and judge the …
tedin7 Nov 25, 2026
99e1c69
chore: merge origin/main into x2w-r23-desc-ptr-prop
tedin7 Nov 25, 2026
a747241
chore: merge origin/main into x2w-r23-desc-ptr-prop; fix: refuse a lo…
tedin7 Nov 25, 2026
62bf81d
chore: merge origin/main
tedin7 Nov 25, 2026
2641fe0
fix: keep descriptor pointer pair open until its matching s_addc_u32
tedin7 Nov 25, 2026
77abd82
chore: merge origin/main
tedin7 Nov 25, 2026
1ecf3bc
chore: merge main into x2w-r23-desc-ptr-prop
tedin7 Nov 25, 2026
6fd1971
chore: merge origin/main
tedin7 Nov 25, 2026
6158c98
fix: let the descriptor pointer pair pass translate's SCC check, add …
tedin7 Nov 25, 2026
6688f74
fix: refuse an untracked-offset pointer as a descriptor, run the poin…
tedin7 Nov 25, 2026
de83569
fix: bind the advanced pointer high word to its accumulated offset
tedin7 Nov 25, 2026
0f226c5
fix: refuse a root pointer load whose high word was overwritten
tedin7 Nov 25, 2026
ef3425a
Revert "fix: refuse a root pointer load whose high word was overwritten"
tedin7 Nov 25, 2026
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
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,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
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3157,6 +3157,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")
Expand Down
225 changes: 225 additions & 0 deletions tests/startup_gfx2hlsl_desc_ptr_prop_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
"""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, <literal>` 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 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))
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, 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 i, (op, operands) in enumerate(program):
s.pc = 4 * i
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))} # 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, 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 = 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_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))

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(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]')
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(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(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(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(ROOT, [LOW] + LOAD)
check('missing high-word add is refused', isinstance(s, str), str(s))

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(ROOT, ADVANCE + [('s_mov_b32', 's21, 0')] + LOAD)
check('overwritten high word is refused', isinstance(s, str), str(s))

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(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 any('Load(%d)' % (0xfffff + 0x10) in line for line in s.lines), str(s))

# The high word is bound to the accumulated offset: the original word never stands in for an advanced one.
s = run(ROOT, ADVANCE + [('s_mov_b32', 's21, s9')] + LOAD)
check('completed high word overwritten by the original one is refused', isinstance(s, str), str(s))

SECOND = [('s_add_u32', 's22, s20, 0x20')]
s = run(ROOT, ADVANCE + SECOND + [('s_addc_u32', 's23, s9, 0'), ('s_load_dwordx4', 's[4:7], s[22:23], 0x10')])
check('second advance with the original high word is refused', isinstance(s, str), str(s))

s = run(ROOT, ADVANCE + SECOND + [('s_addc_u32', 's23, s21, 0'), ('s_load_dwordx4', 's[4:7], s[22:23], 0x10')])
check('second advance with the completed high word of the first pair is folded',
not isinstance(s, str) and any('Load(%d)' % (0x40 + 0x20 + 0x10) in line for line in s.lines), str(s))

s = run(ROOT, [('s_add_u32', 's8, s8, 0x20'), ('s_addc_u32', 's9, s9, 0'), ('s_add_u32', 's8, s8, 0x20'),
('s_addc_u32', 's9, s9, 0'), ('s_load_dwordx4', 's[4:7], s[8:9], 0')])
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))

# 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))

# A program with branches does not track the added offset: the load through the advanced pointer is a refusal
# (never a crash), and the same straight-line shape keeps folding.
IN_PLACE = [('s_add_u32', 's8, s8, 0x40'), ('s_load_dwordx4', 's[4:7], s[8:9], 0x10')]
s = run(TABLE, IN_PLACE, branches=True)
check('in-place pointer advance then load in a program with branches is refused, not a crash',
isinstance(s, str) and 'untracked offset' in s, str(s))

s = run(TABLE, IN_PLACE)
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)
print('startup_gfx2hlsl_desc_ptr_prop: ok')
43 changes: 43 additions & 0 deletions tests/startup_smem_address_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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((
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 -------------------------------------------------------------------------

Expand All @@ -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 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
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((
Expand Down Expand Up @@ -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. 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 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.
p_m0 = build_smem_m0_and_overlap_pipeline()
src_m0 = p_m0.hlsl_vs()
Expand Down
Loading
Loading