diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d24b1632b..98768b6018 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4006,6 +4006,22 @@ if(EXISTS "${INTAKE_PYTHON}") PASS_REGULAR_EXPRESSION "control ${control}: rejected at word=[0-9]+ ") endforeach() endforeach() + # gfx2hlsl COMPUTE_PGM_RSRC2.TG_SIZE_EN wave-ID masking for single-wave groups <= 32 threads (Refs #2274). + add_test(NAME startup_compute_tg_size_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_compute_tg_size_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_compute_tg_size_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_compute_tg_size_check.py" dispatch "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_compute_tg_size_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + foreach(control IN ITEMS dropped-base swapped-mask) + add_test(NAME startup_compute_tg_size_${control}_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_compute_tg_size_check.py" control ${control} "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_compute_tg_size_${control}_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77 + PASS_REGULAR_EXPRESSION "control ${control}: rejected at word=[0-9]+ ") + endforeach() + endforeach() # gfx2hlsl bounded 128-bit LDS exchanges (ds_write_b128, ds_read_b128) and mixed 32/64/128-bit ops (Refs #1541). add_test(NAME startup_compute_lds_b128_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_compute_lds_b128_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index 7e69d617fa..891d6c2d4b 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -16128,6 +16128,16 @@ Building on #2443 (Slice 1), `X2W_D3D12_RELEASE_RETIRED=1` extends desktop objec ```powershell ctest --preset dev --no-tests=error -R '^startup_resource_(retire|budget)' -V + +## Single-wave `TG_SIZE_EN` wave-ID folding in the startup compute translator + +[The startup translator](../tools/startup/gfx2hlsl.py) accepts `COMPUTE_PGM_RSRC2.TG_SIZE_EN` (bit 10) in `ComputePipeline` when the thread-group size is at most 32 threads (`x * y * z <= 32`, one wave per workgroup). The `TG_SIZE` entry SGPR sits immediately after `USER_SGPR` and any enabled `TGID_X/Y/Z` SGPRs (`user + len(groups)`) and is tagged `('tg_size',)`. Only `s_and_b32 R, TG, 0xfc0` (or `0xfc0, TG`) into an SGPR `R <= 105` is accepted for an untouched `TG_SIZE` register: because a single-wave group has wave index `TG_SIZE[11:6] == 0`, it lowers to `R = 0u; scc = false;` with `R` tracked as `('const', 0)` (feeding `v_mbcnt_lo_u32_b32` as a known zero base). Every other read of an untouched `('tg_size',)` register (other masks, moves, arithmetic, vector instructions, or direct `v_mbcnt_lo_u32_b32`) is refused, while overwriting `TG` restores ordinary scalar behavior. Larger groups (`> 32` threads) with `TG_SIZE_EN` remain refused in `ComputePipeline`, and `tools/compute_store_aot.py` continues to refuse `TG_SIZE_EN` unconditionally. + +[The `TG_SIZE_EN` check](../tests/startup_compute_tg_size_check.py) verifies SGPR placement, `s_and_b32` folding, `v_mbcnt_lo_u32_b32` lowering, overwrite/branch/refusal controls, and a two-group `4x2x2` WARP dispatch (`320` words compared, `0` mismatches on Microsoft Basic Render Driver `10.0.26100.9818`, with `dropped-base` rejected at word 5 and `swapped-mask` rejected at word 8). + +```powershell +.\out\pe-env\Scripts\python.exe -B tests/startup_compute_tg_size_check.py +ctest --preset dev --no-tests=error -R '^startup_compute_tg_size_' -V ``` ## Refusing unmodelled modifiers in the startup translator diff --git a/tests/startup_compute_tg_size_check.py b/tests/startup_compute_tg_size_check.py new file mode 100644 index 0000000000..916d9cecfd --- /dev/null +++ b/tests/startup_compute_tg_size_check.py @@ -0,0 +1,423 @@ +#!/usr/bin/env python3 +"""Verify COMPUTE_PGM_RSRC2.TG_SIZE_EN (#2274 Part A) in tools/startup/gfx2hlsl.py. + + (no arguments) pure-Python translation, uniformity and refusal controls + dispatch + translate, compile with DXC cs_6_0, dispatch and compare all 320 words + control + mutated translation must mismatch on readback + +For groups of at most 32 threads (one wave), TG_SIZE[11:6] (wave ID, mask 0xfc0) is identically 0. +TG_SIZE sits in the SGPR immediately following USER_SGPR and any enabled TGID_X/Y/Z SGPRs. +Only `s_and_b32 R, TG, 0xfc0` (or `0xfc0, TG`) into a plain SGPR <= 105 is accepted, lowering to +`R = 0u; scc = false;` with `R` tracked as `('const', 0)`; every other read of the untouched +TG_SIZE SGPR is refused. +""" + +import struct +import subprocess +import sys +import tempfile +from pathlib import Path + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parent +sys.path.insert(0, str(ROOT / 'tools')) +sys.path.insert(0, str(ROOT / 'tools' / 'startup')) +sys.path.insert(0, str(HERE)) + +import gfx2hlsl as G # noqa: E402 +from startup_gfx2hlsl_compute_check import ( # noqa: E402 + SENTINEL, + TGID_X, + TGID_Y, + TGID_Z, + TG_SIZE_EN, + USER7, + WORDS, + refused, + root_blob, + tidig, +) + + +def le32(word): + return struct.pack(' s7..s10). + for rsrc2, tg_reg in ( + (USER7 | TG_SIZE_EN, 7), + (USER7 | TGID_X | TG_SIZE_EN, 8), + (USER7 | TGID_X | TGID_Z | TG_SIZE_EN, 9), + (USER7 | TGID_X | TGID_Y | TGID_Z | TG_SIZE_EN, 10), + ): + pipe = translate(code=S_ENDPGM, rsrc2=rsrc2, group=(8, 4, 1)) + assert pipe.tg_size_sgpr == tg_reg, (hex(rsrc2), pipe.tg_size_sgpr, tg_reg) + assert pipe.cs.sym.get(tg_reg) == ('tg_size',), (hex(rsrc2), pipe.cs.sym) + assert not pipe.cs.uniform_source('s%d' % tg_reg) + assert pipe.cs.is_const(tg_reg) is None + assert pipe.cs.const_u32('s%d' % tg_reg) is None + + # 2. Standalone s_and_b32 R, TG, 0xfc0 (both operand orders and in-place R == TG). + for and_insn in ( + sop2(S_AND_B32, 20, 8, LIT, 0xfc0), + sop2(S_AND_B32, 20, LIT, 8, 0xfc0), + ): + pipe = translate(code=and_insn + S_ENDPGM) + hlsl = pipe.hlsl_cs() + assert ' s20 = 0u;\n scc = false;' in hlsl, hlsl + assert 'uint s8' not in hlsl, hlsl + assert pipe.cs.sym.get(20) == ('const', 0) + assert pipe.cs.sym.get(8) == ('tg_size',) + assert pipe.cs.scc_uniform is True + + # In-place overwrite `s_and_b32 s8, s8, 0xfc0` lowers to 0u and turns s8 into ('const', 0). + inplace = translate(code=sop2(S_AND_B32, 8, 8, LIT, 0xfc0) + sop1(S_MOV_B32, 20, 8) + S_ENDPGM) + inplace_hlsl = inplace.hlsl_cs() + assert ' s8 = 0u;\n scc = false;\n s20 = 0x00000000u;' in inplace_hlsl, inplace_hlsl + assert inplace.cs.sym.get(8) == ('const', 0) + + # 3. Full program: s_and_b32 R, TG, 0xfc0 feeds R into v_mbcnt_lo_u32_b32, plus standalone v_mbcnt_lo/hi. + main_hlsl = translate().hlsl_cs() + assert ' s20 = 0u;\n scc = false;' in main_hlsl, main_hlsl + assert 'countbits(0x85241249u & ((1u << (tid.x + 4u * (tid.y + 2u * tid.z))) - 1u)) + 0x00000000u' in main_hlsl, main_hlsl + assert 'countbits(0x0f0f0f0fu & ((1u << (tid.x + 4u * (tid.y + 2u * tid.z))) - 1u)) + v8' in main_hlsl, main_hlsl + + hi_prog = b''.join([ + sop2(S_AND_B32, 20, 8, LIT, 0xfc0), + vop3(V_MBCNT_HI_U32_B32, 8, INLINE_NEG1, 20), + S_ENDPGM, + ]) + hi_hlsl = translate(code=hi_prog).hlsl_cs() + assert ' v8 = 0x00000000u;' in hi_hlsl, hi_hlsl + + # 4. >32 threads per group with TG_SIZE_EN is refused. + for bad_group in ((8, 4, 2), (33, 1, 1), (64, 1, 1)): + refused('TG_SIZE_EN requires at most 32 threads per group', + lambda g=bad_group: translate(code=S_ENDPGM, group=g)) + + # 5. Disallowed reads of TG_SIZE are refused. + bad_tg_reads = [ + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_AND_B32, 20, 8, LIT, 0x3f)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_AND_B32, 20, 8, LIT, 0)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_AND_B32, 20, 8, 8)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_AND_B32, M0, 8, LIT, 0xfc0)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_AND_B32, EXEC_LO, 8, LIT, 0xfc0)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_AND_B32, VCC_LO, 8, LIT, 0xfc0)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_AND_B32, 106, 8, LIT, 0xfc0)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop1(S_MOV_B32, 20, 8)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_OR_B32, 20, 8, INLINE_0)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_LSHR_B32, 20, 8, INLINE_6)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop2(S_LSHL_B32, 20, 8, INLINE_2)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sop1(S_MOV_B64, 20, 8)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', sopc(S_CMP_EQ_U32, 8, INLINE_0)), + ('s_addk_i32 on tg_size data', sopk(S_ADDK_I32, 8, 1)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', vop2(V_ADD_NC_U32, 3, 8, 0)), + ('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', vop1(V_MOV_B32, 3, 8)), + ('v_mbcnt_lo_u32_b32 requires a VGPR or known base', vop3(V_MBCNT_LO_U32_B32, 8, INLINE_NEG1, 8)), + ('v_mbcnt_lo_u32_b32 requires a known mask', vop3(V_MBCNT_LO_U32_B32, 8, 8, INLINE_0)), + ] + for msg, insn in bad_tg_reads: + refused(msg, lambda c=insn + S_ENDPGM: translate(code=c).hlsl_cs()) + + # Modified s_and_b32 (e.g. non-empty mods on unit probe) is also refused. + probe = G.Stage.__new__(G.Stage) + probe.sym = {8: ('tg_size',)} + probe.used_s = set() + probe.lines = [] + probe.exec_known = True + refused('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', + lambda: probe.scalar('s_and_b32', ['s20', 's8', '0xfc0'], ['clamp'])) + + # Descriptor-path protection: mixing descriptor and tg_size in s_add_u32 refuses tg_size instead of silently dropping it. + desc_mix = b''.join([ + sop1(S_MOV_B32, 10, 6), + sop1(S_MOV_B32, 11, INLINE_0), + S_LOAD_X8_S12_S10, + sop2(S_ADD_U32, 12, 12, 8), + S_ENDPGM, + ]) + refused('TG_SIZE register s8 read outside s_and_b32 with 0xfc0', lambda: translate(code=desc_mix).hlsl_cs()) + + # 6. Overwriting TG first restores ordinary scalar behavior. + overwrite_const = b''.join([ + sop1(S_MOV_B32, 8, 128 + 5), + sop2(S_AND_B32, 20, 8, LIT, 0x3f), + vop2(V_ADD_NC_U32, 3, 8, 0), + S_ENDPGM, + ]) + ov_const_hlsl = translate(code=overwrite_const).hlsl_cs() + assert ' s8 = 0x00000005u;\n s20 = 0x00000005u & 0x0000003fu;' in ov_const_hlsl, ov_const_hlsl + assert ' v3 = 0x00000005u + v0;' in ov_const_hlsl, ov_const_hlsl + + overwrite_runtime = b''.join([ + sop1(S_MOV_B32, 8, 7), + sop2(S_AND_B32, 20, 8, LIT, 0x3f), + vop2(V_ADD_NC_U32, 3, 8, 0), + S_ENDPGM, + ]) + ov_rt_hlsl = translate(code=overwrite_runtime).hlsl_cs() + assert ' s8 = s7;\n s20 = s8 & 0x0000003fu;' in ov_rt_hlsl, ov_rt_hlsl + assert ' v3 = s8 + v0;' in ov_rt_hlsl, ov_rt_hlsl + + # 7. Branch / execz interactions: s_and_b32 inside s_cbranch_execz live-out is refused; dead-out is accepted. + execz_live = b''.join([ + sopp(0x08, 2), # s_cbranch_execz +2 dwords (skips 8-byte s_and_b32 with literal 0xfc0) + sop2(S_AND_B32, 20, 8, LIT, 0xfc0), + vop1(V_MOV_B32, 3, 20), + S_ENDPGM, + ]) + refused('definition of s20, live where the block joins', lambda: translate(code=execz_live).hlsl_cs()) + + execz_dead = b''.join([ + sopp(0x08, 2), # s_cbranch_execz +2 dwords + sop2(S_AND_B32, 20, 8, LIT, 0xfc0), + S_ENDPGM, + ]) + translate(code=execz_dead).hlsl_cs() + + # Overwriting TG on one branch arm and reading it at join refuses (never leaks ('tg_size',) across a join). + join_leak = b''.join([ + sopc(S_CMP_EQ_U32, 7, INLINE_0), + sopp(0x04, 2), # s_cbranch_scc0 +2 dwords + sop2(S_AND_B32, 8, 8, LIT, 0xfc0), + sop1(S_MOV_B32, 20, 8), + S_ENDPGM, + ]) + refused('holding joined at a branch target', lambda: translate(code=join_leak).hlsl_cs()) + + print('tg_size_en: entry layout, s_and_b32 0xfc0 folding, v_mbcnt_lo base, overwrite and refusals pass') + + +CONTROLS = { + 'dropped-base': (' + v8', ''), + 'swapped-mask': ('v8 = (countbits(0x85241249u', 'v8 = (countbits(0x0f0f0f0fu'), +} + +CONTROL_MISMATCH = { + 'dropped-base': 'word=5 expected=00000002 actual=00000001', + 'swapped-mask': 'word=8 expected=00000001 actual=00000002', +} + + +def mutate(hlsl, control): + old, new = CONTROLS[control] + assert hlsl.count(old) == 1, (control, hlsl) + return hlsl.replace(old, new) + + +def check_dispatch(dxc, runner, adapter, control=None): + runner = str(Path(runner).resolve()) + out = ROOT / 'out' + out.mkdir(exist_ok=True) + hlsl = translate().hlsl_cs() + if control: + if control not in CONTROLS: + raise SystemExit(f'unknown control {control!r}') + hlsl = mutate(hlsl, control) + with tempfile.TemporaryDirectory(dir=out) as tmp: + d = Path(tmp) + src, cso, case = d / 'tg_size.hlsl', d / 'tg_size.cso', d / 'tg_size.case' + src.write_text(hlsl, encoding='ascii') + subprocess.run([dxc, '-T', 'cs_6_0', '-E', 'main', '-Fo', str(cso), str(src)], check=True) + gx, gy, gz = DISPATCH_GROUPS + constants = [0] * 16 + case.write_bytes( + struct.pack(' user: raise Unsupported('root signature needs s4-s%d, COMPUTE_PGM_RSRC2 gives %d user SGPRs' % (root_end - 1, user)) @@ -1947,6 +1948,9 @@ def setup_compute_entry(self, root_end): self.used_s.add(n) self.entry_uniform_s.add(n) self.entry.append('s%d = gid.%s;' % (n, 'xyz'[axis])) + tg_size = getattr(self.pipe, 'tg_size_sgpr', None) + if tg_size is not None: + self.sym[tg_size] = ('tg_size',) for i in range(threads): self.used_v.add(i) self.entry.append('v%d = tid.%s;' % (i, 'xyz'[i])) @@ -1963,6 +1967,8 @@ def S(self, n): if s[0] in ('execcopy', 'fixed_buffer_word'): self.used_s.add(n) return 's%d' % n + if s[0] == 'tg_size': + raise Unsupported('TG_SIZE register s%d read outside s_and_b32 with 0xfc0' % n) raise Unsupported('numeric use of s%d holding %s' % (n, s[0])) self.used_s.add(n) return 's%d' % n @@ -3330,7 +3336,7 @@ def lower(self, base, asm, ops, mods): pipe.uses_sad.add(base) return self.set_v(reg_range(ops[0])[1], text.format(*args)) if base in SCA: - return self.scalar(base, ops) + return self.scalar(base, ops, mods) if mods else 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 @@ -3668,7 +3674,7 @@ def exec_absorbed_source(self, base, srcs): return i return None - def scalar(self, base, ops): + def scalar(self, base, ops, mods=()): # Every SALU form in SCA is dst, src (the two templates below use {0} and {1}); a short list would # index past the end and leave `translator crash in : IndexError` in the census instead of a # refusal. s_movk_i32 keeps its own exact check for the immediate it folds. @@ -3736,6 +3742,21 @@ def src_sym(t): r = reg_range(t) return self.sym.get(r[1]) if r and r[0] == 's' else None syms = [src_sym(t) for t in srcs] + tg_srcs = [ + reg for tok in ops[1:] for r in (reg_range(tok),) if r and r[0] == 's' + for reg in range(r[1], r[2] + 1) if self.sym.get(reg) == ('tg_size',) + ] + if tg_srcs: + # Wave-ID bits TG_SIZE[11:6] (mask 0xfc0) are identically 0 for <= 32 threads per group (1 wave). + if (base == 's_and_b32' and not mods and len(srcs) == 2 + and dst and dst[0] == 's' and dst[1] == dst[2] and dst[1] <= 105): + other = (srcs[1] if syms[0] == ('tg_size',) and syms[1] != ('tg_size',) + else srcs[0] if syms[1] == ('tg_size',) and syms[0] != ('tg_size',) + else None) + if other is not None and self.const_u32(other) == 0xfc0: + self.set_s(dst[1], '0u', ('const', 0), scc='false') + return None + raise Unsupported('TG_SIZE register s%d read outside s_and_b32 with 0xfc0' % tg_srcs[0]) if any(s and s[0] in ('desc', 'descnum', 'ptr', 'ptrhi') for s in syms): if scc or base in SCC_UNMODELLED: self.scc_effect = 'descriptor' @@ -7877,10 +7898,16 @@ class ComputePipeline(Pipeline): def __init__(self, cs_code, root_blob, rsrc2, group, sampler_heap=None, refusals=None, decoded=None): from compute_store_aot import checked_group, entry_registers try: - self.entry = entry_registers(rsrc2) # (user SGPRs, {axis: group-ID SGPR}, thread-ID VGPRs) + tg_size_en = (isinstance(rsrc2, int) and not isinstance(rsrc2, bool) + and 0 <= rsrc2 < (1 << 32) and bool((rsrc2 >> 10) & 1)) + self.entry = entry_registers(rsrc2 & ~(1 << 10) if tg_size_en else rsrc2) self.group = checked_group(group) except ValueError as reason: raise Unsupported(str(reason)) from None + if tg_size_en and self.group[0] * self.group[1] * self.group[2] > 32: + raise Unsupported('COMPUTE_PGM_RSRC2 TG_SIZE_EN requires at most 32 threads per group') + user, groups, _ = self.entry + self.tg_size_sgpr = (user + len(groups)) if tg_size_en else None self.root = parse_root(root_blob) self.vs_fetch, self.packed_position_vgpr, self.rtv_formats = None, None, () self.sampler_heap = os.environ.get('X2W_SAMPLER_HEAP') == '1' if sampler_heap is None else sampler_heap