From 6c16fcd62ecc3f706e6ebd7faa2e1ef8b3ff19e9 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 02:23:22 +0100 Subject: [PATCH 1/5] feat: lower SGPR spills on wave64 lanes and in vertex stages Symptom: the constant-lane spill idiom v_writelane_b32 vN, sM, k / v_readlane_b32 sD, vN, k was refused for k >= 32 ("lane 40 outside 0..31") and in every vertex stage ("lowered in pixel stages only"), although a wave64 program spills into lanes 0..63 and a vertex stage uses the same idiom. Root cause: the slot model (one scalar per (VGPR, lane)) was limited to wave32 lanes and pixel stages. Neither limit is needed by the model: the two instructions touch only lane k and ignore EXEC in every stage. Change: lanes 0..63 and vertex stages are accepted. The translator does not know the wave size and a wave32 program takes the lane select modulo 32, so a spill VGPR whose lanes include k and k + 32 (one slot in wave32, two in wave64) is refused by name. Lane 64+, compute stages, register lanes, branches (open #2524 territory) stay refused. Test: tests/startup_gfx2hlsl_spill_wave64_check.py (controls fail before: lanes 32..63 and the vertex stage refused). The existing sgpr_spill controls now expect lane 64 and the k/k+32 pair refused and the vertex stage accepted. Win2025 WARP dev and release, DXC and FXC: the pixel draw matches the slot reference on 256/256 pixels, the vertex draw exports the reference param0, and all five wrong-lane/ignored-overwrite mutations fail. (cherry picked from commit c7fe3e44b10ffb4ebdf40caab7d8306926b3a881) --- CMakeLists.txt | 9 + docs/experiments.md | 2 + tests/startup_gfx2hlsl_sgpr_spill_check.py | 9 +- tests/startup_gfx2hlsl_spill_wave64_check.py | 300 +++++++++++++++++++ tools/startup/gfx2hlsl.py | 35 ++- 5 files changed, 346 insertions(+), 9 deletions(-) create mode 100644 tests/startup_gfx2hlsl_spill_wave64_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 4571ab2b06..56ff2ef5d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3948,6 +3948,15 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter} $<$:${STARTUP_FXC}>) set_tests_properties(startup_gfx2hlsl_sgpr_spill_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) endforeach() + # The same spill slots on wave64 lanes 32..63 and in a vertex stage (k and k + 32 on one VGPR refused). + add_test(NAME startup_gfx2hlsl_spill_wave64_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_spill_wave64_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_spill_wave64_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_spill_wave64_check.py" draw "${DXC_EXECUTABLE}" + "$" ${adapter} $<$:${STARTUP_FXC}>) + set_tests_properties(startup_gfx2hlsl_spill_wave64_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) + endforeach() # gfx2hlsl 24-bit multiplies, v_xnor_b32 and the funnel shifts: CPU evaluation of the translated text # against an independent reference built from unbounded integers. add_test(NAME startup_gfx2hlsl_mul24_align COMMAND "${INTAKE_PYTHON}" -B diff --git a/docs/experiments.md b/docs/experiments.md index 228af5c6b9..f445b84c00 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15042,6 +15042,8 @@ The startup pixel stage lowers constant-lane SGPR spills (#2125 item E): `v_writ The same slots are lowered in pixel stages with branches. The slot variable is assigned on the pixel's own path, while the hardware lane holds the last write on the wave's path; the two differ only inside the region of a branch not proven uniform (`s_cbranch_execz`/`_vccz` and their `nz` forms, or an SCC branch on varying data: the blocks between the branch and its immediate post-dominator, or the loop range of a backward branch), where the wave also runs blocks the pixel skips. A forward must-dataflow over the basic blocks (intersection at joins, fixed point through loops) gives, for each `v_readlane_b32`, the writes of its (VGPR, lane) that can supply it. One write: every path into the read ran it, so it is accepted unless the write lies in such a region and the read is outside it (a loop or arm the wave can run after the pixel left it). Several writes: accepted only when none lies in such a region; the reload keeps a constant only if every write stored it and is not claimed uniform. Refused, each by name: no write on some path (including another lane on another path), a write later in a loop that reaches the read through the back edge, a single write in a non-uniform region the read is outside, joined writes with one in a non-uniform region, and a reaching write that was itself refused. The lane-mask, descriptor, lane-range and vector-use refusals above are unchanged; the lane-mask test still follows program order, not the joins. On a local set of 79 vertex/pixel requests whose first refusal was the straight-line requirement under a development translator, 32 reach that refusal on main; with this change all 32 pass it and stop next at a lane-mask or descriptor spill source (30) or a spill VGPR also used as a vector (2). The other 47 stop earlier on main (`v_fma_mix_f32` 39, a joined symbol 8). `tests/startup_gfx2hlsl_spill_branches_check.py` (`startup_gfx2hlsl_spill_branches`) translates synthetic programs through `Stage.translate()`: six accepted shapes with their exact emitted lines, seven refusals with their exact reasons, and six translator mutations (union at joins, one pass instead of the fixed point, lanes keyed by their spelling, each region rule removed, every branch counted uniform) that must each turn a control red. One whole pixel stage is also generated: the slot is declared once and assigned on both arms of a uniform branch before the read after the join. The same file also draws it (`startup_gfx2hlsl_spill_branches_warp`/`_hardware`, FXC 5_1 and DXC 6_0): two uniform SCC choices write different binary32 words to one slot, the join reloads it and exports it OR-ed with the packed screen position, and the readback of all 16x16 pixels must equal the selected arm; the opposite arm's reference must mismatch. +Wave64 lanes and vertex stages: the same slot model now accepts lanes 0..63 and vertex stages. A wave32 program takes the lane select modulo 32, so lanes `k` and `k + 32` of one VGPR would be one slot there and two in wave64; the translator does not know the wave size and refuses that pair. Lane 64 and above, compute stages and register-selected lanes stay refused. `tests/startup_gfx2hlsl_spill_wave64_check.py` draws a pixel program with slots on lanes 32, 33, 40 and 63 (16 x 16 pixels) and a vertex program with slots on lanes 0 and 48 whose reloads reach a flat parameter (one pixel), each compared with a reference replayed from the slot rule; wrong-lane and ignored-overwrite translations must fail. Run `ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_spill_wave64" -V`. + The binary32 table (`VEC_F`) applies the same operand-count rule. Before this change, a form one source short (`v_add_f32 v1, v2`) raised `IndexError` in `Stage.one` instead of `Unsupported`. A form with one source too many (`v_med3_f32 v1, v2, v3, v4, v5`) translated with the extra source dropped. `tests/startup_gfx2hlsl_operand_count_check.py` (`startup_gfx2hlsl_operand_count`) checks every operation of both tables: one source fewer and one source more are refused, and the exact count translates. It also checks that the constant and accumulator forms (`v_fmac_f32`, `v_mac_f32`, `v_fmamk_f32`, `v_fmaak_f32`, `v_madmk_f32`, `v_madak_f32`) keep translating with their usual operands. As a control, it loads the translator with the new `VEC_F` guard removed and requires both old failures to come back. ## Startup translator: binary32 rounding diff --git a/tests/startup_gfx2hlsl_sgpr_spill_check.py b/tests/startup_gfx2hlsl_sgpr_spill_check.py index 5f4d8ef9d1..6ef33105f4 100644 --- a/tests/startup_gfx2hlsl_sgpr_spill_check.py +++ b/tests/startup_gfx2hlsl_sgpr_spill_check.py @@ -165,7 +165,9 @@ def controls(): ('read of a lane nothing wrote', ['v_writelane_b32 v10, s0, 3', 'v_readlane_b32 s1, v10, 4']), ('read of another VGPR at the written lane', ['v_writelane_b32 v10, s0, 3', 'v_readlane_b32 s1, v11, 3']), ('lane chosen by an SGPR', ['v_writelane_b32 v10, s0, s2', 'v_readlane_b32 s1, v10, 3']), - ('lane 32', ['v_writelane_b32 v10, s0, 32']), + ('lane 64', ['v_writelane_b32 v10, s0, 64']), + ('lanes 8 and 40 of one VGPR (one slot in wave32, two in wave64)', + ['v_writelane_b32 v10, s0, 8', 'v_writelane_b32 v10, s1, 40', 'v_readlane_b32 s2, v10, 8']), ('lane mask value (vcc_lo)', ['v_writelane_b32 v10, vcc_lo, 3']), ('lane mask value (exec_lo)', ['v_writelane_b32 v10, exec_lo, 3']), ('destination outside the plain SGPRs', ['v_writelane_b32 v10, s0, 3', 'v_readlane_b32 vcc_lo, v10, 3']), @@ -178,7 +180,10 @@ def controls(): for label, program in cases: reason = refused(program) check('refused: %s (%s)' % (label, reason), reason is not None) - check('refused: vertex stage (%s)' % refused(ok, kind='vs'), refused(ok, kind='vs') is not None) + check('a vertex stage is accepted', refused(ok, kind='vs') is None, str(refused(ok, kind='vs'))) + check('refused: compute stage (%s)' % refused(ok, kind='cs'), refused(ok, kind='cs') is not None) + wave64 = ['v_writelane_b32 v10, s0, 40', 'v_writelane_b32 v10, s1, 63', 'v_readlane_b32 s2, v10, 40'] + check('wave64 lanes 40 and 63 are accepted', refused(wave64) is None, str(refused(wave64))) check('refused: a shader with branches (%s)' % refused(ok, branches=True), refused(ok, branches=True) is not None) # 5. The reload keeps the constant the write stored, so later scalar folding sees the same word. diff --git a/tests/startup_gfx2hlsl_spill_wave64_check.py b/tests/startup_gfx2hlsl_spill_wave64_check.py new file mode 100644 index 0000000000..fefc80dec7 --- /dev/null +++ b/tests/startup_gfx2hlsl_spill_wave64_check.py @@ -0,0 +1,300 @@ +"""SGPR spills to VGPR lanes 32..63 (wave64) and in vertex stages, tools/startup/gfx2hlsl.py, read back from a real +D3D12 draw. + + (no arguments) translation controls and refusals, pure Python + binary assemble the fixture sources with LLVM (gfx1013) and compare with the words here + draw [fxc] + compile the translated stages with DXC (and FXC when given), draw and compare + every exported word with the slot reference below + +`v_writelane_b32 vN, sM, k` writes SGPR sM into lane k of vN and `v_readlane_b32 sD, vN, k` reads lane k back (AMD +RDNA 2 ISA reference, VOP3 lane operations). Both ignore EXEC and touch only lane k, so with a constant k and a VGPR +no other instruction names, vN is a table of scalar slots (tests/startup_gfx2hlsl_sgpr_spill_check.py has the +lanes 0..31 pixel case). A wave64 program spills into lanes 0..63, and a vertex stage spills the same way. The +translator does not know the wave size: a wave32 program takes the lane select modulo 32, so lanes k and k + 32 of one +VGPR are one slot there and two in wave64, and that pair is refused; lane 64 and above stay refused. + +Fixtures: + PS pixel stage, slots v10[40], v10[63], v11[32], v12[33]; the SGPRs are clobbered, reloaded (v10[40] after a + second write) and ORed into the packed screen position, 16 x 16 pixels compared word by word. + VS vertex stage, slots v40[0], v40[48], v41[0]; reloads (v40[48] after a second write) go to param0, which the + flat pixel stage FLATPS exports; one pixel compared. +The reference is the last value written to (VGPR, lane), computed here by replaying the lane writes on a dictionary. +Mutations of the translated text (lane 63 read as lane 40, the overwrite ignored, another VGPR's slot) must fail. +""" +import re +import struct +import subprocess +import sys +import tempfile +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools')) +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +sys.path.insert(0, str(HERE)) +import gfx2hlsl as G # noqa: E402 +from startup_flat_param_check import assemble, draw as draw_flat # noqa: E402 +from startup_image_3d_check import FULLSCREEN_VS, VS_WORDS, compile_hlsl, root_blob # noqa: E402 + +WIDTH = HEIGHT = 16 +PACKED = (0x8000, 0x8000) # SPI_PS_INPUT_ENA/ADDR: the packed screen position only, in v0 +failures = [] + +PS_TEXT = '''s_mov_b32 s0, 0x40200000 +s_mov_b32 s1, 0x40400000 +s_mov_b32 s2, 4.0 +v_writelane_b32 v10, s0, 40 +v_writelane_b32 v10, s1, 63 +v_writelane_b32 v11, s2, 32 +s_or_b32 s8, s0, s1 +v_writelane_b32 v12, s8, 33 +s_mov_b32 s0, 0 +s_mov_b32 s1, 0 +s_mov_b32 s2, 0 +s_mov_b32 s8, 0 +v_readlane_b32 s3, v10, 40 +v_readlane_b32 s4, v10, 63 +v_readlane_b32 s5, v11, 32 +v_writelane_b32 v10, s5, 40 +v_readlane_b32 s6, v10, 40 +v_readlane_b32 s9, v12, 33 +v_or_b32_e32 v1, s3, v0 +v_or_b32_e32 v2, s4, v0 +v_or_b32_e32 v3, s6, v0 +v_or_b32_e32 v4, s9, v0 +exp mrt0 v1, v2, v3, v4 done vm +s_endpgm +''' +PS_WORDS = bytes.fromhex( + 'ff0380be00002040ff0381be00004040f60382be0a0061d7005001000a0061d7017e01000b0061d702400100000108880c0061d708420100' + '800380be800381be800382be800388be030060d70a510100040060d70a7f0100050060d70b4101000a0061d705500100060060d70a510100' + '090060d70c430100030002380400043806000638090008380f1800f801020304000081bf') +VS_TEXT = '''v_mov_b32_e32 v3, 0x40400000 +v_mov_b32_e32 v10, 0xc0400000 +v_cmp_eq_u32_e32 vcc_lo, 1, v0 +v_cndmask_b32_e32 v1, -1.0, v3, vcc_lo +v_cmp_eq_u32_e32 vcc_lo, 2, v0 +v_cndmask_b32_e32 v2, 1.0, v10, vcc_lo +v_mov_b32_e32 v4, 1.0 +v_mov_b32_e32 v5, 0 +s_mov_b32 s20, 0x12345678 +s_mov_b32 s21, 0x9abcdef0 +v_writelane_b32 v40, s20, 0 +v_writelane_b32 v40, s21, 48 +v_writelane_b32 v41, s21, 0 +s_mov_b32 s20, 0 +s_mov_b32 s21, 0 +v_readlane_b32 s22, v40, 0 +v_readlane_b32 s23, v40, 48 +v_readlane_b32 s24, v41, 0 +v_writelane_b32 v40, s22, 48 +v_readlane_b32 s25, v40, 48 +v_mov_b32_e32 v11, s22 +v_mov_b32_e32 v12, s23 +v_mov_b32_e32 v13, s24 +v_mov_b32_e32 v14, s25 +exp pos0 v1, v2, v5, v4 done +exp param0 v11, v12, v13, v14 +s_endpgm +''' +VS_FIXTURE = bytes.fromhex( + 'ff02067e00004040ff02147e000040c08100847df30602028200847df2140402f202087e80020a7eff0394be78563412ff0395bef0debc9a' + '280061d714000100280061d715600100290061d715000100800394be800395be160060d728010100170060d728610100180060d729010100' + '280061d716600100190060d7286101001602167e1702187e18021a7e19021c7ecf0800f8010205040f0200f80b0c0d0e000081bf') +FLAT_PS_TEXT = '''s_mov_b32 m0, s2 +v_interp_mov_f32_e32 v0, p0, attr0.x +v_interp_mov_f32_e32 v1, p0, attr0.y +v_interp_mov_f32_e32 v2, p0, attr0.z +v_interp_mov_f32_e32 v3, p0, attr0.w +exp mrt0 v0, v1, v2, v3 done vm +s_endpgm +''' +FLAT_PS_WORDS = bytes.fromhex('0203fcbe020002c8020106c802020ac802030ec80f1800f800010203000081bf') + + +def check(label, ok, detail=''): + print('%s: %s' % (label, 'ok' if ok else 'MISMATCH ' + str(detail)[:1500])) + if not ok: + failures.append(label) + + +# ---- reference: replay the lane writes and reads on a dictionary ------------------------------------------------------ +def replay(text, outputs): + lanes, scalars = {}, {} + for line in text.splitlines(): + op, _, rest = line.partition(' ') + args = [a.strip() for a in rest.split(',')] + if op == 's_mov_b32': + scalars[args[0]] = 0x40800000 if args[1] == '4.0' else int(args[1], 0) + elif op == 's_or_b32': + scalars[args[0]] = scalars[args[1]] | scalars[args[2]] + elif op == 'v_writelane_b32': + lanes[(args[0], int(args[2]))] = scalars[args[1]] + elif op == 'v_readlane_b32': + scalars[args[0]] = lanes[(args[1], int(args[2]))] + return [scalars[r] for r in outputs] + + +PS_SLOTS = replay(PS_TEXT, ('s3', 's4', 's6', 's9')) +VS_SLOTS = replay(VS_TEXT, ('s22', 's23', 's24', 's25')) + + +def ps_image(): + return [(x | y << 16) | value for y in range(HEIGHT) for x in range(WIDTH) for value in PS_SLOTS] + + +def ps_text(): + return G.Pipeline(VS_WORDS, PS_WORDS, root_blob(), ps_hw_inputs=PACKED).hlsl_ps() + + +def vs_pipeline(): + return G.Pipeline(VS_FIXTURE, FLAT_PS_WORDS, root_blob()) + + +def mutate(text, old, new): + mutated = re.sub(old, new, text) + assert mutated != text, old + return mutated + + +# Each wrong lowering, written on the folded text (a constant slot folds into the export). +PS_MUTATIONS = { + 'lane 63 reads lane 40 of the same VGPR': (r'v2 = 0x40400000u', 'v2 = 0x40200000u'), + 'the overwritten slot keeps its first value': (r'sp10_40 = s5;', 'sp10_40 = sp10_40;'), + 'v11 lane 32 reads v10 lane 40': (r'= sp11_32;', '= sp10_40;'), +} +VS_MUTATIONS = { + 'lane 48 reads lane 0 of the same VGPR': (r'v12 = 0x9abcdef0u', 'v12 = 0x12345678u'), + 'the overwritten slot keeps its first value': (r'v14 = 0x12345678u', 'v14 = 0x9abcdef0u'), +} + + +# ---- controls --------------------------------------------------------------------------------------------------------- +def refused(program, kind='ps'): + stage = G.Stage.__new__(G.Stage) + stage.kind, stage.lines, stage.sym = kind, [], {} + stage.used_s, stage.used_v, stage.uniform_v, stage.varying_s = set(), set(), set(), set() + stage.scc_uniform, stage.exec_known, stage.has_branches, stage.blocks, stage.masks = True, True, False, [], set() + stage.ins = [(4 * i, 1, re.sub(r'_e(32|64)$', '', a.split(' ', 1)[0]), a) for i, a in enumerate(program)] + for _, _, name, asm in stage.ins: + try: + stage.one(name, asm) + except G.Unsupported as reason: + return str(reason) + return None + + +def controls(): + check('pixel reference: v10[40], v10[63], v10[40] after the overwrite with v11[32], and the derived word', + PS_SLOTS == [0x40200000, 0x40400000, 0x40800000, 0x40600000], [hex(v) for v in PS_SLOTS]) + check('vertex reference: v40[0], v40[48], v41[0], v40[48] after the overwrite', + VS_SLOTS == [0x12345678, 0x9abcdef0, 0x9abcdef0, 0x12345678], [hex(v) for v in VS_SLOTS]) + for label, text, words in (('PS', PS_TEXT, PS_WORDS), ('VS', VS_TEXT, VS_FIXTURE), ('FLATPS', FLAT_PS_TEXT, + FLAT_PS_WORDS)): + got = [re.sub(r'_e(32|64)\b', '', asm) for _o, _n, _m, asm in G.decode(words)] + want = [re.sub(r'_e(32|64)\b', '', line) for line in text.splitlines()] + check('%s fixture decodes to its source' % label, got == want, '%s\n%s' % (got, want)) + try: + text = ps_text() + check('wave64 pixel slots translate to one scalar per (VGPR, lane)', + all(re.search(r'\b%s = 0' % s, text) for s in ('sp10_40', 'sp10_63', 'sp11_32', 'sp12_33')), text) + for label, (old, new) in PS_MUTATIONS.items(): + check('PS mutation "%s" changes the text' % label, mutate(text, old, new) != text) + except G.Unsupported as reason: + check('wave64 pixel slots translate', False, str(reason)) + try: + text = vs_pipeline().hlsl_vs() + check('vertex stage slots translate', all(re.search(r'\b%s = 0' % s, text) for s in ('sp40_0', 'sp40_48', 'sp41_0')), + text) + for label, (old, new) in VS_MUTATIONS.items(): + check('VS mutation "%s" changes the text' % label, mutate(text, old, new) != text) + except G.Unsupported as reason: + check('vertex stage slots translate', False, str(reason)) + for label, program, kind in ( + ('lane 64', ['v_writelane_b32 v10, s0, 64'], 'ps'), + ('lanes 8 and 40 of one VGPR', ['v_writelane_b32 v10, s0, 8', 'v_writelane_b32 v10, s1, 40'], 'ps'), + ('lanes 63 and 31 of one VGPR in a vertex stage', + ['v_writelane_b32 v10, s0, 63', 'v_readlane_b32 s1, v10, 31'], 'vs'), + ('a compute stage', ['v_writelane_b32 v10, s0, 40'], 'cs'), + ('a lane chosen by an SGPR', ['v_writelane_b32 v10, s0, s2'], 'vs')): + reason = refused(program, kind) + check('refused: %s (%s)' % (label, reason), reason is not None) + apart = ['v_writelane_b32 v10, s0, 8', 'v_writelane_b32 v11, s1, 40', 'v_readlane_b32 s2, v11, 40'] + check('lanes 8 and 40 of two different VGPRs are accepted', refused(apart) is None, str(refused(apart))) + + +# ---- draw ------------------------------------------------------------------------------------------------------------- +def draw_pixels(renderer, adapter, vs, ps, expected): + run = subprocess.run([renderer, adapter, '--startup-pixel-words', str(vs), str(ps), str(WIDTH), str(HEIGHT), + str(expected)], capture_output=True, text=True) + if run.returncode == 77: + print(run.stdout + run.stderr) + sys.exit(1 if failures else 77) + return run + + +def check_draw(dxc, renderer, adapter, fxc=None): + compilers = [('dxc', dxc, 'vs_6_0', 'ps_6_0')] + ([('fxc', fxc, 'vs_5_1', 'ps_5_1')] if fxc else []) + ps_source = ps_text() + pipe = vs_pipeline() + vs_source, flat_source = pipe.hlsl_vs(), pipe.hlsl_ps() + expected_vs = ','.join('%08x' % v for v in VS_SLOTS) + with tempfile.TemporaryDirectory() as tmp: + directory = Path(tmp) + reference = directory / 'reference.bin' + reference.write_bytes(struct.pack('<%dI' % (WIDTH * HEIGHT * 4), *ps_image())) + for kind, compiler, vs_profile, ps_profile in compilers: + vs = compile_hlsl(compiler, kind, FULLSCREEN_VS, 'vs_' + kind, vs_profile, directory) + ps = compile_hlsl(compiler, kind, ps_source, 'ps_' + kind, ps_profile, directory) + run = draw_pixels(renderer, adapter, vs, ps, reference) + check('%s %s PS: all %d pixels equal the slot reference' % (adapter, kind, WIDTH * HEIGHT), + run.returncode == 0 and 'all %d pixels matched' % (WIDTH * HEIGHT) in run.stdout, + run.stdout + run.stderr) + for label, (old, new) in PS_MUTATIONS.items(): + bad = compile_hlsl(compiler, kind, mutate(ps_source, old, new), 'bad_ps_%s_%d' % (kind, len(old)), + ps_profile, directory) + run = draw_pixels(renderer, adapter, vs, bad, reference) + check('%s %s PS mutation "%s" fails' % (adapter, kind, label), + run.returncode != 0 and 'pixel mismatch' in run.stderr, run.stdout + run.stderr) + vs2 = compile_hlsl(compiler, kind, vs_source, 'vs_spill_' + kind, vs_profile, directory) + flat = compile_hlsl(compiler, kind, flat_source, 'ps_flat_' + kind, ps_profile, directory) + run = draw_flat(renderer, adapter, vs2, flat, expected_vs) + check('%s %s VS: exported param0 equals the slot reference %s' % (adapter, kind, expected_vs), + run.returncode == 0, run.stdout + run.stderr) + for label, (old, new) in VS_MUTATIONS.items(): + bad = compile_hlsl(compiler, kind, mutate(vs_source, old, new), 'bad_vs_%s_%d' % (kind, len(old)), + vs_profile, directory) + run = draw_flat(renderer, adapter, bad, flat, expected_vs) + check('%s %s VS mutation "%s" fails' % (adapter, kind, label), run.returncode not in (0, 77), + run.stdout + run.stderr) + + +def check_binary(llvm_mc): + for label, text, words in (('PS', PS_TEXT, PS_WORDS), ('VS', VS_TEXT, VS_FIXTURE), + ('FLATPS', FLAT_PS_TEXT, FLAT_PS_WORDS)): + check('llvm-mc assembles the %s fixture to the same words' % label, assemble(llvm_mc, text) == words) + + +def main(): + if len(sys.argv) == 1: + controls() + else: + mode, *args = sys.argv[1:] + if mode == 'binary': + check_binary(*args) + elif mode == 'draw': + controls() + check_draw(*args) + else: + raise SystemExit(__doc__) + if failures: + print('FAIL') + for text in failures: + print(' ' + text) + sys.exit(1) + print('PASS') + + +if __name__ == '__main__': + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index d5e92922c1..d5c2a16936 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -6571,7 +6571,10 @@ def op_v_readfirstlane_b32(self, ops, mods): # AMDGPU spill lowering). Both ignore EXEC. With a constant k and a VGPR that no other instruction # touches, vN is only a table of scalar slots, so one scalar variable per (vN, k) is an exact model # and no other lane is ever read. Anything else keeps the one-lane model out of reach and is refused. - # Pixel stages are wave32 (lanes 0..31). Slot 57 mesh is wave64 (lanes 0..63), matching #2127. + # Lanes 0..63 cover a wave64 program. A wave32 program uses only bits 4:0 of the lane select (RDNA 2 ISA, + # v_readlane/v_writelane: the lane is taken modulo the wave size), so k and k + 32 name one slot there and two in + # wave64; the translator does not know the wave size, so a VGPR whose lanes include such a pair is refused. + # Slot 57 mesh is wave64 (#2127). Vertex stages use the same model: the two instructions touch no other lane and no stage input. # # With branches the slot variable is assigned on the pixel's own path, while the hardware slot holds the last # write on the wave's path. The two paths differ only inside the region of a branch not proven uniform (an @@ -6581,7 +6584,8 @@ def op_v_readfirstlane_b32(self, ops, mods): # the write lies in such a region that the read is outside (a loop or arm the wave can run after this pixel # left it). Several writes: the value is chosen by the path, so every write must lie outside every such region; # the reload then keeps a constant only if all of them stored it, and is not claimed uniform. - SPILL_LANES = 32 + SPILL_LANES = 64 + SPILL_STAGES = ('ps', 'vs', 'ms') def spill_reaching(self, cfg): """Instruction index of each v_readlane_b32 -> the v_writelane_b32 indices that can supply its (VGPR, lane), or @@ -6661,17 +6665,34 @@ def spill_operands(self, name, ops, mods): except ValueError: raise Unsupported('%s requires a constant lane, got %s' % (name, ops[2])) from None kind = getattr(self, 'kind', None) - lanes = 64 if kind == 'ms' else self.SPILL_LANES - if not 0 <= lane < lanes: - raise Unsupported('%s lane %d outside 0..%d' % (name, lane, lanes - 1)) - if kind not in ('ps', 'ms'): - raise Unsupported('%s is lowered in pixel and mesh stages only' % name) + if not 0 <= lane < self.SPILL_LANES: + raise Unsupported('%s lane %d outside 0..%d' % (name, lane, self.SPILL_LANES - 1)) + if kind not in self.SPILL_STAGES: + raise Unsupported('%s is lowered in pixel, vertex and mesh stages only' % name) if not self.tracks_uniformity() or (getattr(self, 'has_branches', True) and (kind != 'ps' or getattr(self, 'spill_reach', None) is None)): raise Unsupported('%s needs straight-line code to prove write-before-read' % name) self.require_spill_only(vreg[1], name) + self.require_spill_lanes_apart(vreg[1], name) return vreg[1], sreg, lane + def require_spill_lanes_apart(self, n, name): + """Refuse when two lanes of spill VGPR v differ by 32: one slot in wave32, two in wave64.""" + lanes = set() + for _, _, op, asm in getattr(self, 'ins', ()): + if re.sub(r'_e(32|64)$', '', op) not in ('v_writelane_b32', 'v_readlane_b32'): + continue + ops = parse(asm)[1] + if len(ops) == 3 and n in [r[1] for r in map(reg_range, ops[:2]) if r and r[0] == 'v']: + try: + lanes.add(int(ops[2], 0)) + except ValueError: + continue # a register lane is refused where it is read + for lane in lanes: + if lane + 32 in lanes: + raise Unsupported('%s: v%d lanes %d and %d are one slot in wave32 and two in wave64' + % (name, n, lane, lane + 32)) + def require_spill_only(self, n, name): """Refuse unless every instruction naming v is a lane write or read of it.""" # Mesh: scan the pre-skeleton program. The accepted group-memory exchanges rewrite to From bcb42f9150e12dccd87506e3befcd8c2191075e4 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 04:24:16 +0100 Subject: [PATCH 2/5] fix: apply the k / k + 32 spill lane rule to pixel and vertex stages only, slot 57 mesh is always wave64 --- tests/startup_mesh57_lane_spill_check.py | 12 ++++++++++++ tools/startup/gfx2hlsl.py | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/startup_mesh57_lane_spill_check.py b/tests/startup_mesh57_lane_spill_check.py index ea1f94db6d..66091ef852 100644 --- a/tests/startup_mesh57_lane_spill_check.py +++ b/tests/startup_mesh57_lane_spill_check.py @@ -93,6 +93,18 @@ def controls(): expect('reload feeds the SGPR', 's40 = sp30_3' in text, True) expect('spill VGPR is not a vector temporary', not re.search(r'\bv30\b', text), True) + # Slot 57 mesh is always wave64: lanes 3 and 35 of one VGPR are two slots there (the k / k + 32 pair rule of the + # pixel and vertex stages, whose wave size is unknown, must not refuse them). + with tempfile.TemporaryDirectory() as tmp: + pair = (MESH[:POST_COUNT] + ['bea803f2'] + writelane(30, 40, 3) + ['bea80380'] + + ['bea90391'] + writelane(30, 41, 35) + ['bea90380'] + readlane(41, 30, 35) + + readlane(40, 30, 3) + ['7e0e0228'] + MESH[POST_COUNT:]) + key, result = request(tmp, pair) + expect('mesh lanes 3 and 35 of one VGPR are two slots', result, 'ok') + if result == 'ok': + text = (Path(tmp) / ('%016x.ms57.hlsl' % key)).read_text() + expect('both slots are declared', 'sp30_3' in text and 'sp30_35' in text, True) + refusals = ( ('unwritten spill slot', MESH[:POST_COUNT] + ['bea803f2'] + writelane(30, 40, 3) + ['bea80380'] diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index d5c2a16936..99a4c0a13e 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -6673,7 +6673,8 @@ def spill_operands(self, name, ops, mods): (kind != 'ps' or getattr(self, 'spill_reach', None) is None)): raise Unsupported('%s needs straight-line code to prove write-before-read' % name) self.require_spill_only(vreg[1], name) - self.require_spill_lanes_apart(vreg[1], name) + if kind in ('ps', 'vs'): # the wave size is unknown there; slot 57 mesh is always wave64, so k and k + 32 are two slots + self.require_spill_lanes_apart(vreg[1], name) return vreg[1], sreg, lane def require_spill_lanes_apart(self, n, name): From 6bbc8e4efd3e8901e5dc84fa2159625d45457e42 Mon Sep 17 00:00:00 2001 From: Ybucaille <83926195+Ybucaille@users.noreply.github.com> Date: Thu, 26 Nov 2026 07:26:55 +0100 Subject: [PATCH 3/5] startup: spill descriptor and pointer SGPRs to VGPR lanes in compute Symptom: compute programs that park a descriptor or a pointer in a VGPR lane (v_writelane_b32 vN, sM, k) around branches and reload it with v_readlane_b32 were refused: the spill is lowered in pixel, vertex and mesh stages only, and a source holding a descriptor was refused as "lane mask or descriptor". Change: cs joins SPILL_STAGES and, like ps, may branch, through the same write-before-read proof (a write on every path, regions the wave runs while a thread skips them, loop-carried reads). A descriptor or pointer word has no run-time value in the translation, so the slot carries the symbol (desc, ptr, ptrhi) and the reload restores the same one; a word whose symbol does not name one value (modified descriptor word, pointer with a non-numeric offset) and different symbols joined at a read stay refused. Lane masks keep their refusal. A reload also keeps the provenance the formatted loads require: the write records whether the word passed the entry-block or local table load proof where it was spilled, and formatted_descriptor_from_block accepts a reloaded word only if every write that can supply it did. Test: tests/startup_gfx2hlsl_compute_spill_check.py builds two gfx1013 compute fixtures from source, eight refused variants, probes, eleven translator mutations and a WARP/hardware dispatch. The two existing controls that expected cs to be refused now expect it to be accepted. Part of #1723. Refs #2022. --- .github/workflows/windows.yml | 2 + AGENTS.md | 1 + CMakeLists.txt | 16 + docs/experiments.md | 2 + tests/startup_gfx2hlsl_compute_spill_check.py | 511 ++++++++++++++++++ tests/startup_gfx2hlsl_sgpr_spill_check.py | 3 +- tests/startup_gfx2hlsl_spill_wave64_check.py | 2 +- tools/startup/gfx2hlsl.py | 84 ++- 8 files changed, 599 insertions(+), 22 deletions(-) create mode 100644 tests/startup_gfx2hlsl_compute_spill_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 6a9720799b..46652da866 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -135,6 +135,7 @@ jobs: (["tests/startup_gfx2hlsl_sgpr_spill_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_mad_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_compute_check.py", "binary", "llvm-mc-15"], {}), + (["tests/startup_gfx2hlsl_compute_spill_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_compute_lds_b64_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_compute_lds_b128_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_calculated_target_check.py", "binary", "llvm-mc-15"], {}), @@ -321,6 +322,7 @@ jobs: tests/startup_gfx2hlsl_mad_check.py \ tests/startup_gfx2hlsl_neg_check.py \ tests/startup_gfx2hlsl_compute_check.py \ + tests/startup_gfx2hlsl_compute_spill_check.py \ tests/startup_compute_lds_b64_check.py \ tests/startup_compute_lds_b128_check.py \ tests/startup_compute_translate_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 5be74bb03d..e1cb072889 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -168,6 +168,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_neg_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_interp_p2_hi_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_compute_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_compute_spill_check.py ./out/pe-env/bin/python -B tests/startup_compute_image_store_check.py ./out/pe-env/bin/python -B tests/startup_compute_image_store_d16_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_heap_record_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 56ff2ef5d3..e3cca0136e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4130,6 +4130,22 @@ if(EXISTS "${INTAKE_PYTHON}") PASS_REGULAR_EXPRESSION "control ${control}: rejected at word=[0-9]+ ") endforeach() endforeach() + # gfx2hlsl constant-lane SGPR spills of descriptor and pointer words in compute stages, across branches (#1723). + add_test(NAME startup_gfx2hlsl_compute_spill_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_spill_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_compute_spill_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_spill_check.py" dispatch "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_gfx2hlsl_compute_spill_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + foreach(control IN ITEMS s-arm-swapped s-store-dropped p-store-dropped) + add_test(NAME startup_gfx2hlsl_compute_spill_${control}_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_spill_check.py" control ${control} "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_gfx2hlsl_compute_spill_${control}_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77 + PASS_REGULAR_EXPRESSION "control ${control}: rejected at word=[0-9]+ ") + endforeach() + endforeach() # gfx2hlsl bounded 64-bit LDS exchanges (ds_write_b64, ds_read_b64) across s_barrier (Refs #2274, #1541). add_test(NAME startup_compute_lds_b64_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_compute_lds_b64_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index f445b84c00..0f7629860c 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15044,6 +15044,8 @@ The same slots are lowered in pixel stages with branches. The slot variable is a Wave64 lanes and vertex stages: the same slot model now accepts lanes 0..63 and vertex stages. A wave32 program takes the lane select modulo 32, so lanes `k` and `k + 32` of one VGPR would be one slot there and two in wave64; the translator does not know the wave size and refuses that pair. Lane 64 and above, compute stages and register-selected lanes stay refused. `tests/startup_gfx2hlsl_spill_wave64_check.py` draws a pixel program with slots on lanes 32, 33, 40 and 63 (16 x 16 pixels) and a vertex program with slots on lanes 0 and 48 whose reloads reach a flat parameter (one pixel), each compared with a reference replayed from the slot rule; wrong-lane and ignored-overwrite translations must fail. Run `ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_spill_wave64" -V`. +Compute stages: the same slots are lowered in `cs` (with branches, through the same write-before-read proof), and a spilled descriptor or pointer word (`desc`, `ptr`, `ptrhi` symbols) is carried by identity: the slot holds the symbol, nothing is emitted, and the reload restores the same symbol. A word whose symbol does not name one value (a modified descriptor word, a pointer with a non-numeric offset) is refused when it is spilled; writes of different symbols joined at a read are refused; lane masks keep their refusal. A reload also keeps the provenance of the word it restores: a formatted buffer load past the entry block accepts a reloaded descriptor only when every write that can supply it was spilled from the entry block or from one untouched local table load of a proven pointer, i.e. when the original would have been accepted at the write. `tests/startup_gfx2hlsl_compute_spill_check.py` (`startup_gfx2hlsl_compute_spill*`) builds two gfx1013 compute fixtures from source (a read descriptor spilled in the entry block and the store descriptor spilled on both arms of a uniform branch, reloaded inside an `s_cbranch_execz` region for a formatted load and a store; a table pointer pair spilled and reloaded to load a descriptor), eight refused variants each with its exact reason, probes for `ptrhi` and modified words, eleven translator mutations that must each turn a control red, and a WARP/hardware dispatch with three mutated translations that must mismatch. + The binary32 table (`VEC_F`) applies the same operand-count rule. Before this change, a form one source short (`v_add_f32 v1, v2`) raised `IndexError` in `Stage.one` instead of `Unsupported`. A form with one source too many (`v_med3_f32 v1, v2, v3, v4, v5`) translated with the extra source dropped. `tests/startup_gfx2hlsl_operand_count_check.py` (`startup_gfx2hlsl_operand_count`) checks every operation of both tables: one source fewer and one source more are refused, and the exact count translates. It also checks that the constant and accumulator forms (`v_fmac_f32`, `v_mac_f32`, `v_fmamk_f32`, `v_fmaak_f32`, `v_madmk_f32`, `v_madak_f32`) keep translating with their usual operands. As a control, it loads the translator with the new `VEC_F` guard removed and requires both old failures to come back. ## Startup translator: binary32 rounding diff --git a/tests/startup_gfx2hlsl_compute_spill_check.py b/tests/startup_gfx2hlsl_compute_spill_check.py new file mode 100644 index 0000000000..6a5bb821f1 --- /dev/null +++ b/tests/startup_gfx2hlsl_compute_spill_check.py @@ -0,0 +1,511 @@ +"""Constant-lane SGPR spills of descriptor and pointer words in compute stages of tools/startup/gfx2hlsl.py (#1723). + + (no arguments) translation controls and translator mutations, pure Python + binary assemble the fixture sources (gfx1013) and compare with the words here + dispatch + translate, compile with DXC cs_6_0, dispatch and compare every word + control + the same dispatch with a mutated translation, which must mismatch + +`v_writelane_b32 vN, sM, k` parks SGPR sM in lane k of vN and `v_readlane_b32 sD, vN, k` reads it back; both ignore +EXEC (AMD RDNA 2 ISA reference, VOP3 lane operations). A descriptor or pointer word has no run-time value in the +translation: it is a symbol (`('desc', table, offset, dword)`, `('ptr', ...)`), so the slot carries the symbol and the +reload restores the same one. The write-before-read proof of tests/startup_gfx2hlsl_spill_branches_check.py (a write +on every path, regions the wave runs while a thread skips them, loop-carried reads) decides whether a reload sees the +symbol, and a reload keeps the provenance of the word it restores: a formatted buffer load past the entry block +accepts a reloaded descriptor only when every write that can supply it was itself the word of an entry-block or +local table load. + +Fixtures (source-built gfx1013, root constants key and stride in s4-s5, one descriptor table in s6): +- S spills the table's read descriptor s[12:15] (formatted load) in the entry block and its store descriptor s[16:19] + in both arms of a uniform branch on the key, clobbers both, and inside an s_cbranch_execz region reloads them and + loads RG16_UINT words through one and stores through the other. 16 lanes, lanes below 12 store four words each. +- P spills the table pointer pair s[10:11] (the low word a table pointer, the high word the constant zero), clobbers + it, reloads it inside an s_cbranch_execz region and loads the store descriptor through it (shape of case J of + tests/startup_gfx2hlsl_compute_check.py). Four groups of eight lanes, lanes below 3 store one word. +""" +import re +import struct +import subprocess +import sys +import tempfile +import types +from pathlib import Path + +HERE = Path(__file__).resolve().parent +TOOLS = HERE.parent / 'tools' +sys.path.insert(0, str(TOOLS / 'startup')) +sys.path.insert(0, str(HERE)) +import gfx2hlsl as G # noqa: E402 +import startup_gfx2hlsl_compute_check as C # noqa: E402 + +failures = [] + + +def check(label, ok, detail=''): + print('%s: %s' % (label, 'ok' if ok else 'MISMATCH')) + if not ok: + failures.append(label) + if detail: + print(' ' + str(detail)[:1500]) + + +SOURCE_S = """\ +s_mov_b32 s10, s6 +s_mov_b32 s11, 0 +s_load_dwordx8 s[12:19], s[10:11], 0x0 +s_waitcnt lgkmcnt(0) +v_writelane_b32 v40, s12, 0 +v_writelane_b32 v40, s13, 1 +v_writelane_b32 v40, s14, 2 +v_writelane_b32 v40, s15, 3 +s_and_b32 s23, s4, 1 +s_cmp_lg_u32 s23, 0 +s_cbranch_scc0 Lelse +v_writelane_b32 v40, s16, 4 +v_writelane_b32 v40, s17, 5 +v_writelane_b32 v40, s18, 6 +v_writelane_b32 v40, s19, 7 +v_mov_b32 v1, 1 +s_branch Ljoin +Lelse: +v_writelane_b32 v40, s16, 4 +v_writelane_b32 v40, s17, 5 +v_writelane_b32 v40, s18, 6 +v_writelane_b32 v40, s19, 7 +v_mov_b32 v1, 0 +Ljoin: +s_mov_b32 s12, 0 +s_mov_b32 s13, 0 +s_mov_b32 s14, 0 +s_mov_b32 s15, 0 +s_mov_b32 s16, 0 +s_mov_b32 s17, 0 +s_mov_b32 s18, 0 +s_mov_b32 s19, 0 +v_lshlrev_b32 v4, 4, v0 +s_mov_b32 s21, exec_lo +v_cmp_gt_u32 vcc_lo, 12, v0 +s_mov_b32 exec_lo, vcc_lo +s_cbranch_execz Lend +v_readlane_b32 s12, v40, 0 +v_readlane_b32 s13, v40, 1 +v_readlane_b32 s14, v40, 2 +v_readlane_b32 s15, v40, 3 +v_readlane_b32 s16, v40, 4 +v_readlane_b32 s17, v40, 5 +v_readlane_b32 s18, v40, 6 +v_readlane_b32 s19, v40, 7 +buffer_load_format_xyzw v[5:8], v[0:1], s[12:15], 0 idxen offen offset:2 +s_waitcnt vmcnt(0) +buffer_store_dwordx4 v[5:8], v4, s[16:19], 0 offen +Lend: +s_mov_b32 exec_lo, s21 +s_endpgm +""" +SOURCE_P = """\ +s_mov_b32 s10, s6 +s_mov_b32 s11, 0 +s_load_dwordx4 s[12:15], s[10:11], 0x0 +s_waitcnt lgkmcnt(0) +v_writelane_b32 v41, s10, 0 +v_writelane_b32 v41, s11, 1 +s_mov_b32 s10, 0 +s_mov_b32 s11, 0 +s_lshl_b32 s20, s7, 3 +v_add_nc_u32 v3, s20, v0 +v_lshlrev_b32 v4, 2, v3 +s_mov_b32 s21, exec_lo +v_cmp_gt_u32 vcc_lo, 3, v0 +s_mov_b32 exec_lo, vcc_lo +s_cbranch_execz Lend +v_readlane_b32 s10, v41, 0 +v_readlane_b32 s11, v41, 1 +s_load_dwordx4 s[16:19], s[10:11], 0x10 +s_mul_i32 s22, s7, s5 +s_add_u32 s22, s22, s4 +buffer_load_dword v5, v4, s[12:15], 0 offen +s_waitcnt vmcnt(0) lgkmcnt(0) +v_xor_b32 v5, s22, v5 +buffer_store_dword v5, v4, s[16:19], 0 offen +Lend: +s_mov_b32 exec_lo, s21 +s_endpgm +""" +S_ARM = ('v_writelane_b32 v40, s16, 4\nv_writelane_b32 v40, s17, 5\nv_writelane_b32 v40, s18, 6\n' + 'v_writelane_b32 v40, s19, 7\n') +S_ENTRY = 'v_writelane_b32 v40, s12, 0\nv_writelane_b32 v40, s13, 1\nv_writelane_b32 v40, s14, 2\nv_writelane_b32 v40, s15, 3\n' + +# name -> (base source, [(old, new)], what the refusal must say, why it is refused). Each variant is a fixture with one +# change; it is assembled in binary mode like the fixtures and must be refused for that reason and no other. +VARIANTS = { + 'reload-before-write': (SOURCE_S, [(S_ENTRY, '')], 'no v_writelane_b32 of it on some path', + 'the read descriptor is never written to its lanes'), + 'two-descriptors-joined': (SOURCE_S, [(S_ARM + 'v_mov_b32 v1, 0', S_ARM.replace('s16', 's12').replace('s17', 's13') + .replace('s18', 's14').replace('s19', 's15') + 'v_mov_b32 v1, 0')], + 'joins different descriptor or pointer words', + 'one arm spills the read descriptor and the other the store descriptor into the same lanes'), + 'modified-word': (SOURCE_S, [('s_and_b32 s23, s4, 1', 's_and_b32 s17, s17, 0xc000ffff\ns_and_b32 s23, s4, 1')], + 'modified descriptor or pointer word', 'a stride word rewritten before the spill'), + 'lane-mask': (SOURCE_S, [('v_writelane_b32 v40, s16, 4\nv_writelane_b32 v40, s17, 5\nv_writelane_b32 v40, s18, 6\n' + 'v_writelane_b32 v40, s19, 7\nv_mov_b32 v1, 1', + 'v_cmp_gt_u32 s20, 12, v0\nv_writelane_b32 v40, s20, 4\nv_mov_b32 v1, 1')], + 'holds a lane mask or descriptor', 'a compare result held in an SGPR (step 2 of #2022)'), + 'wrong-lane': (SOURCE_S, [('v_readlane_b32 s13, v40, 1', 'v_readlane_b32 s13, v40, 5')], + 'descriptor whose words were rewritten', 'word 1 of the descriptor reloaded from the lane of word 5'), + 'unproven-descriptor': (SOURCE_S, [(S_ENTRY, ''), ('v_mov_b32 v1, 1', S_ENTRY + 'v_mov_b32 v1, 1'), + ('v_mov_b32 v1, 0', S_ENTRY + 'v_mov_b32 v1, 0')], + 'requires descriptor words proven in the entry block', + 'the read descriptor is spilled in the arms, where its load is not local'), + 'written-in-region-read-outside': (SOURCE_S, [ + (S_ARM + 'v_mov_b32 v1, 1', 'v_mov_b32 v1, 1'), (S_ARM + 'v_mov_b32 v1, 0', 'v_mov_b32 v1, 0'), + (''.join('s_mov_b32 s%d, 0\n' % r for r in range(16, 20)), ''), + (''.join('v_readlane_b32 s%d, v40, %d\n' % (r, r - 12) for r in range(16, 20)), ''), + ('s_cbranch_execz Lend\n', 's_cbranch_execz Lend\n' + S_ARM), ('Lend:\n', 'Lend:\nv_readlane_b32 s16, v40, 4\n')], + 'effect outside scalar registers', 'the store descriptor is written inside the ' + 'execz region (the lane write is a cross-lane effect there) and reloaded after it'), + 'loop-carried': (SOURCE_S, [('Ljoin:\n', 'Ljoin:\ns_mov_b32 s22, 0\nLloop:\nv_readlane_b32 s16, v40, 4\n' + 'v_writelane_b32 v40, s16, 4\ns_add_u32 s22, s22, 1\ns_cmp_lt_u32 s22, 2\n' + 's_cbranch_scc1 Lloop\n')], + 'written later in a loop', 'a reload whose lane is written again by the loop body'), +} +EXPECTED_ACCEPTED = {'S': SOURCE_S, 'P': SOURCE_P} + + +def variant_source(name): + base, edits = VARIANTS[name][:2] + for old, new in edits: + assert base.count(old) == 1, (name, old) + base = base.replace(old, new) + return base + + +def all_sources(): + return {**EXPECTED_ACCEPTED, **{name: variant_source(name) for name in VARIANTS}} + + +def assemble_labels(llvm_mc, source): + """llvm-mc leaves a branch label to the linker, so the words come from a first pass for the instruction sizes.""" + from startup_flat_param_check import assemble + lines = source.splitlines() + plain = [line for line in lines if not line.endswith(':')] + sizes = [] + for line in plain: + sizes.append(len(assemble(llvm_mc, line + '\n'))) + labels, position, at = {}, 0, 0 + for line in lines: + if line.endswith(':'): + labels[line[:-1]] = position + else: + position += sizes[at] + at += 1 + out, position = [], 0 + for line, size in zip(plain, sizes): + m = re.fullmatch(r'(s_c?branch\w*) (L\w+)', line) + if m: + line = '%s %d' % (m[1], (labels[m[2]] - (position + 4)) // 4) + out.append(line) + position += size + return assemble(llvm_mc, '\n'.join(out) + '\n') + + +# CASE: (source, words as hex, rsrc2, group size, group counts) +USER7, TGID_X, tidig = C.USER7, C.TGID_X, C.tidig +CASES_ENCODING = { + 'S': (USER7, (16, 1, 1), (1, 1, 1)), + 'P': (USER7 | TGID_X | tidig(1), (8, 1, 1), (4, 1, 1)), +} +WORDS = { + 'S': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' + '0f06010204811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' + '8102027e090082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e01028002027e80038cbe' + '80038dbe80038ebe80038fbe800390be800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf' + '0c0060d7280101020d0060d7280301020e0060d7280501020f0060d728070102100060d728090102110060d7280b0102' + '120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0040504801503febe000081bf'), + 'P': bytes.fromhex( + '06038abe80038bbe050308f4000000fa7fc08cbf290061d70a000102290061d70b02010280038abe80038bbe0783148f' + '1400064a820608347e0395be8300887d6a03febe0e0088bf0a0060d7290101020b0060d729030102050408f4100000fa' + '0705169316041680001030e00405038070008cbf160a0a3a001070e0040504801503febe000081bf'), + 'reload-before-write': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf04811787178007bf0a0084bf280061d710080102280061d7110a0102' + '280061d7120c0102280061d7130e01028102027e090082bf280061d710080102280061d7110a0102280061d7120c0102' + '280061d7130e01028002027e80038cbe80038dbe80038ebe80038fbe800390be800391be800392be800393be84000834' + '7e0395be8c00887d6a03febe150088bf0c0060d7280101020d0060d7280301020e0060d7280501020f0060d728070102' + '100060d728090102110060d7280b0102120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0' + '040504801503febe000081bf'), + 'two-descriptors-joined': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' + '0f06010204811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' + '8102027e090082bf280061d70c080102280061d70d0a0102280061d70e0c0102280061d70f0e01028002027e80038cbe' + '80038dbe80038ebe80038fbe800390be800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf' + '0c0060d7280101020d0060d7280301020e0060d7280501020f0060d728070102100060d728090102110060d7280b0102' + '120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0040504801503febe000081bf'), + 'modified-word': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' + '0f06010211ff1187ffff00c004811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102' + '280061d7130e01028102027e090082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' + '8002027e80038cbe80038dbe80038ebe80038fbe800390be800391be800392be800393be840008347e0395be8c00887d' + '6a03febe150088bf0c0060d7280101020d0060d7280301020e0060d7280501020f0060d728070102100060d728090102' + '110060d7280b0102120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0040504801503febe' + '000081bf'), + 'lane-mask': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' + '0f06010204811787178007bf060084bf1400c4d48c000202280061d7140801028102027e090082bf280061d710080102' + '280061d7110a0102280061d7120c0102280061d7130e01028002027e80038cbe80038dbe80038ebe80038fbe800390be' + '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101020d0060d728030102' + '0e0060d7280501020f0060d728070102100060d728090102110060d7280b0102120060d7280d0102130060d7280f0102' + '02300ce000050380703f8cbf001078e0040504801503febe000081bf'), + 'wrong-lane': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' + '0f06010204811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' + '8102027e090082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e01028002027e80038cbe' + '80038dbe80038ebe80038fbe800390be800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf' + '0c0060d7280101020d0060d7280b01020e0060d7280501020f0060d728070102100060d728090102110060d7280b0102' + '120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0040504801503febe000081bf'), + 'unproven-descriptor': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf04811787178007bf120084bf280061d710080102280061d7110a0102' + '280061d7120c0102280061d7130e0102280061d70c000102280061d70d020102280061d70e040102280061d70f060102' + '8102027e110082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102280061d70c000102' + '280061d70d020102280061d70e040102280061d70f0601028002027e80038cbe80038dbe80038ebe80038fbe800390be' + '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101020d0060d728030102' + '0e0060d7280501020f0060d728070102100060d728090102110060d7280b0102120060d7280d0102130060d7280f0102' + '02300ce000050380703f8cbf001078e0040504801503febe000081bf'), + 'written-in-region-read-outside': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' + '0f06010204811787178007bf020084bf8102027e010082bf8002027e80038cbe80038dbe80038ebe80038fbe84000834' + '7e0395be8c00887d6a03febe150088bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' + '0c0060d7280101020d0060d7280301020e0060d7280501020f0060d72807010202300ce000050380703f8cbf001078e0' + '04050480100060d7280901021503febe000081bf'), + 'loop-carried': bytes.fromhex( + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' + '0f06010204811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' + '8102027e090082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e01028002027e800396be' + '100060d728090102280061d7100801021681168016820abff9ff85bf80038cbe80038dbe80038ebe80038fbe800390be' + '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101020d0060d728030102' + '0e0060d7280501020f0060d728070102100060d728090102110060d7280b0102120060d7280d0102130060d7280f0102' + '02300ce000050380703f8cbf001078e0040504801503febe000081bf'), +} + + +KEY, STRIDE, SENTINEL, WORDS_OUT = C.KEY, C.STRIDE, C.SENTINEL, C.WORDS +LIVE_LANES = 12 + + +def expected(name): + out = [SENTINEL] * WORDS_OUT + if name == 'S': + data = C.formatted_input() + for lane in range(LIVE_LANES): # the same RG16_UINT words as case L-uint, the lanes the compare leaves active + low, high = struct.unpack_from(' the labels of the controls that failed (all printed unless quiet).""" + red = [] + + def verdict(label, ok, detail=''): + if not ok: + red.append(label) + if not quiet: + check(label, ok, detail) + + for name in ('S', 'P'): + reason = refusal(name, module) + verdict('fixture %s translates (%s)' % (name, reason), reason is None) + for name, (_, _, fragment, why) in VARIANTS.items(): + reason = refusal(name, module) + verdict('refused: %s (%s)' % (why, reason), reason is not None and fragment in reason, reason) + return red + + +def probe_refusals(): + """Unit probes on a compute Stage that has symbols a table program cannot give it: ptrhi, a pointer with an offset.""" + def reason(sym_text, source_sym, kind='cs'): + stage = G.Stage.__new__(G.Stage) + stage.kind, stage.lines, stage.sym = kind, [], {20: source_sym} if source_sym else {} + stage.used_s, stage.used_v, stage.uniform_v, stage.varying_s = set(), set(), set(), set() + stage.scc_uniform, stage.exec_known, stage.has_branches, stage.blocks, stage.masks = True, True, False, [], set() + stage.ins = [(4 * i, 1, a.split(' ', 1)[0], a) for i, a in enumerate(sym_text)] + for _, _, name, asm in stage.ins: + try: + stage.one(name, asm) + except G.Unsupported as why: + return str(why) + return stage + + spill = ['v_writelane_b32 v10, s20, 3', 'v_readlane_b32 s21, v10, 3'] + for label, sym in (('a table pointer', ('ptr', ('table', 0))), ('the high word of a root pointer', ('ptrhi', ('root', 2))), + ('a descriptor word', ('desc', ('table', 0), 0, 1))): + stage = reason(spill, sym) + check('compute stage spills %s and reloads the same symbol' % label, + not isinstance(stage, str) and stage.sym.get(21) == sym, stage) + for label, sym in (('a pointer with an offset', ('ptr', ('table', 0), 'added')), + ('a descriptor word with its stride cleared', ('desc', ('table', 0), 0, 1, 'stride0'))): + text = reason(spill, sym) + check('refused: %s (%s)' % (label, text), isinstance(text, str) and 'modified descriptor or pointer word' in text) + for label, sym in (('a table pointer', ('ptr', ('table', 0))), ('a descriptor word', ('desc', ('table', 0), 0, 1))): + text = reason(spill, sym, kind='ps') + check('refused: %s in a pixel stage (%s)' % (label, text), + isinstance(text, str) and 'holds a lane mask or descriptor' in text) + + +# Each translator mutation must turn at least one control red. +MUTATIONS = { + 'the reload restores no symbol': (' self.set_s(dst[1], \'sp%d_%d\' % (n, lane), sym=sym, uniform=uniform)', + ' self.set_s(dst[1], \'sp%d_%d\' % (n, lane), sym=None, uniform=uniform)'), + 'the reload drops the provenance': (" return all(\n getattr(self, 'reload_proven', {}).get(reloads[w], False)", + " return all(\n False and getattr(self, 'reload_proven', {}).get(reloads[w], False)"), + 'every reload counts as proven': (" return all(\n getattr(self, 'reload_proven', {}).get(reloads[w], False)", + " return all(\n True or getattr(self, 'reload_proven', {}).get(reloads[w], False)"), + 'a write counts as proven wherever the descriptor came from': ( + " if sym[0] != 'desc' or not getattr(self, 'past_entry', False):\n return True\n return self.formatted", + " if True:\n return True\n return self.formatted"), + 'the reload reads the neighbouring lane': (' reach[i] = state.get((vreg, lane))', + ' reach[i] = state.get((vreg, lane ^ 1))'), + 'different symbols join into none': (" if sym is None and any(s[0] is not None and s[0][0] in self.SPILL_SYMBOLS for s in stored):", + " if False:"), + 'a modified word is carried': (" if not names_one_value(held):", " if False:"), + 'no branches in a compute stage': ("(kind not in ('ps', 'cs') or getattr(self, 'spill_reach', None) is None)):", + "(kind != 'ps' or getattr(self, 'spill_reach', None) is None)):"), + 'compute stages refused': ("SPILL_STAGES = ('ps', 'vs', 'ms', 'cs')", "SPILL_STAGES = ('ps', 'vs', 'ms')"), + 'no loop-carried refusal': (" if max(writes) > i:", " if False:"), + 'no write-before-read refusal': (" if writes is None:\n raise Unsupported('v_readlane_b32 reads v%d lane %d with no", + " if writes is None and False:\n raise Unsupported('v_readlane_b32 reads v%d lane %d with no"), +} + + +def mutated(old, new): + source = (TOOLS / 'startup' / 'gfx2hlsl.py').read_text(encoding='utf-8').replace('\r\n', '\n') + assert source.count(old) == 1, old + module = types.ModuleType('gfx2hlsl_mutant') + module.__file__ = str(TOOLS / 'startup' / 'gfx2hlsl.py') + exec(compile(source.replace(old, new), module.__file__, 'exec'), module.__dict__) + return module + + +def check_mutations(): + for label, (old, new) in MUTATIONS.items(): + try: + red = controls(mutated(old, new), quiet=True) + except Exception as error: # a mutant that crashes the translator is caught as well + red = ['crash: %s' % error] + check('mutation "%s" turns %d control(s) red: %s' % (label, len(red), red[:3]), bool(red)) + + +def check_text(): + s, p = translate('S').hlsl_cs(), translate('P').hlsl_cs() + check('the descriptor slots are declared and never assigned', [l for l in s.splitlines() if 'sp40_' in l] == + [l for l in s.splitlines() if l.lstrip().startswith('uint sp40_')], s) + check('the formatted load reads word 3 of the table descriptor', 'switch ((ud3 >> 12) & 127u)' in s) + check('the pointer slot is never assigned, the constant high word is', '= sp41_0' not in p and 's11 = sp41_1;' in p, p) + check('the pointer fixture stores through the reloaded table', p.count('u0.Store(') == 1) + + +# ---- binary and dispatch --------------------------------------------------------------------------------------------- +def check_binary(llvm_mc): + for name, source in all_sources().items(): + built = assemble_labels(llvm_mc, source) + check('llvm-mc assembles %s to the recorded words' % name, built == WORDS[name], built.hex()) + + +# control -> (case, text the translation must contain once, replacement) +CONTROLS = { + 's-arm-swapped': ('S', 'pc = (!scc) ? 2 : 1;', 'pc = (!scc) ? 1 : 2;'), + 's-store-dropped': ('S', 'u0.Store4(a, uint4(v5, v6, v7, v8));', ''), + 'p-store-dropped': ('P', 'u0.Store(a, v5);', ''), +} + + +def run_case(name, dxc, runner, adapter, directory, control=None): + hlsl = translate(name).hlsl_cs() + if control: + _, old, new = CONTROLS[control] + assert hlsl.count(old) == 1, (control, hlsl) + hlsl = hlsl.replace(old, new) + src, cso, case = (directory / ('%s.%s' % (name, ext)) for ext in ('hlsl', 'cso', 'case')) + src.write_text(hlsl, encoding='ascii') + subprocess.run([dxc, '-T', 'cs_6_0', '-E', 'main', '-Fo', str(cso), str(src)], check=True) + groups = CASES_ENCODING[name][2] + constants = [KEY, STRIDE] + [0] * 14 + source = b'' + if name == 'S': + constants[2:4] = [C.L_STRIDE, C.L_WORD3['L-uint']] + source = C.formatted_input() + case.write_bytes(struct.pack(' 32-bit pattern from s_mov_b32 of an inline float constant self.has_branches = bool(targets) - self.spill_slots = {} # (vgpr, lane) -> (sym, uniform) for constant-lane SGPR spills + self.spill_slots = {} # (vgpr, lane) -> (sym, uniform, proven) for constant-lane SGPR spills self.vcc_events = {} self.entry_table_pointers = { reg: sym[1] for reg, sym in init_sym.items() @@ -2994,7 +2994,7 @@ def translate(self): self.descriptor_block_first = 0 self.uniform_expr = {} # proven VGPR -> uniform HLSL expression over scalars that defined it self.uniform_branches = set() # s_cbranch_* offsets proven uniform in this pass (spill-slot proof) - self.spill_writes = {} # instruction index of a v_writelane_b32 translated in this pass -> (sym, uniform) + self.spill_writes = {} # instruction index of a v_writelane_b32 translated in this pass -> (sym, uniform, proven) self.heap_index = {} # SGPR -> what its word is for a resource-heap s_load (see heap_record) self.heap_index_was = {} # the marks the current instruction's destinations held before it self.since_join = None # SGPRs proven uniform since the last branch target (None: none reached) @@ -5375,9 +5375,32 @@ def formatted_pointer_copy(self, pointer, before): return None return source[1], index + def spill_word_proven(self, reg, sym): + """True when the descriptor word held in s would pass the formatted-load provenance proof here: it is + still in the entry block, or it is a word of one untouched local table load (or an already proven reload).""" + if sym[0] != 'desc' or not getattr(self, 'past_entry', False): + return True + return self.formatted_descriptor_from_block(('s', reg, reg), sym) + def formatted_descriptor_from_block(self, reg, first): - """Require one untouched local load and a table pointer that no incoming path can rewrite.""" + """Require one untouched local load and a table pointer that no incoming path can rewrite. A word written + by a v_readlane_b32 reload counts when every v_writelane_b32 that can supply it passed this same proof.""" + if not first: + return False start, end = self.descriptor_block_first, self.descriptor_instruction_index + reloads = {} + for w in range(reg[1], reg[2] + 1): + for at in range(end - 1, start - 1, -1): + _, _, name, asm = self.ins[at] + if w in self.descriptor_scalar_writes(name, parse(asm)[1]): + if re.sub(r'_e(32|64)$', '', name) == 'v_readlane_b32': + reloads[w] = at + break + if reloads: + return all( + getattr(self, 'reload_proven', {}).get(reloads[w], False) if w in reloads else + self.formatted_descriptor_from_block(('s', w, w), self.sym.get(w)) for w in range(reg[1], reg[2] + 1)) + width = reg[2] - reg[1] + 1 block = self.ins[start:end] words = set(range(reg[1], reg[2] + 1)) for index in range(len(block) - 1, -1, -1): @@ -5387,7 +5410,7 @@ def formatted_descriptor_from_block(self, reg, first): continue m = re.fullmatch(r's_load_dwordx(4|8|16)', name) load = reg_range(args[0]) - if not m or not load or load[1] + first[3] != reg[1] or int(m.group(1)) < first[3] + 4: + if not m or not load or load[1] + first[3] != reg[1] or int(m.group(1)) < first[3] + width: return False base = reg_range(args[1]) if not base or base[0] != 's': @@ -6584,8 +6607,13 @@ def op_v_readfirstlane_b32(self, ops, mods): # the write lies in such a region that the read is outside (a loop or arm the wave can run after this pixel # left it). Several writes: the value is chosen by the path, so every write must lie outside every such region; # the reload then keeps a constant only if all of them stored it, and is not claimed uniform. + # Compute stages (cs) use the same proof, branches included. A spilled descriptor or pointer word (SPILL_SYMBOLS) is a + # symbol with no run-time value: the slot carries the symbol, the reload restores it, and the word keeps the + # provenance the formatted loads require (formatted_descriptor_from_block follows a reload to the writes that supply it). SPILL_LANES = 64 - SPILL_STAGES = ('ps', 'vs', 'ms') + SPILL_STAGES = ('ps', 'vs', 'ms', 'cs') + # Symbols a spill carries by identity: the SGPR holds no run-time value in the translation, so the slot holds none either. + SPILL_SYMBOLS = ('desc', 'ptr', 'ptrhi') def spill_reaching(self, cfg): """Instruction index of each v_readlane_b32 -> the v_writelane_b32 indices that can supply its (VGPR, lane), or @@ -6649,8 +6677,11 @@ def spill_join(self, n, lane): raise Unsupported('v_readlane_b32 reads v%d lane %d outside the region of the branch at %#x that writes ' 'it, which is not proven uniform' % (n, lane, off)) stored = [self.spill_writes[w] for w in sorted(writes)] - sym = stored[0][0] if all(s == stored[0][0] for s, _ in stored) else None - return sym, len(stored) == 1 and stored[0][1] + sym = stored[0][0] if all(s[0] == stored[0][0] for s in stored) else None + if sym is None and any(s[0] is not None and s[0][0] in self.SPILL_SYMBOLS for s in stored): + # A descriptor or pointer word has no run-time variable: only the same symbol on every path can be reloaded. + raise Unsupported('v_readlane_b32 joins different descriptor or pointer words of v%d lane %d' % (n, lane)) + return sym, len(stored) == 1 and stored[0][1], all(s[2] for s in stored) def spill_operands(self, name, ops, mods): if mods or len(ops) != 3: @@ -6668,12 +6699,12 @@ def spill_operands(self, name, ops, mods): if not 0 <= lane < self.SPILL_LANES: raise Unsupported('%s lane %d outside 0..%d' % (name, lane, self.SPILL_LANES - 1)) if kind not in self.SPILL_STAGES: - raise Unsupported('%s is lowered in pixel, vertex and mesh stages only' % name) + raise Unsupported('%s is lowered in pixel, vertex, mesh and compute stages only' % name) if not self.tracks_uniformity() or (getattr(self, 'has_branches', True) and - (kind != 'ps' or getattr(self, 'spill_reach', None) is None)): + (kind not in ('ps', 'cs') or getattr(self, 'spill_reach', None) is None)): raise Unsupported('%s needs straight-line code to prove write-before-read' % name) self.require_spill_only(vreg[1], name) - if kind in ('ps', 'vs'): # the wave size is unknown there; slot 57 mesh is always wave64, so k and k + 32 are two slots + if kind in ('ps', 'vs', 'cs'): # the wave size is unknown there; slot 57 mesh is always wave64, so k and k + 32 are two slots self.require_spill_lanes_apart(vreg[1], name) return vreg[1], sreg, lane @@ -6716,20 +6747,31 @@ def op_v_writelane_b32(self, ops, mods): if ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'): raise Unsupported('v_writelane_b32 source %s is a lane mask or m0' % ops[1]) else: - value, sym, uniform = self.u(ops[1]), None, True + value, sym, uniform, proven = self.u(ops[1]), None, True, False if re.fullmatch(r'0x[0-9a-f]{8}u', value): sym = ('const', int(value[2:-1], 16)) else: - if self.is_lane_mask(ops[1]) or (self.sym.get(src[1]) or ('',))[0] not in ('', 'const', 'ud', 'fixed_buffer_word'): + held = self.sym.get(src[1]) or ('',) + carried = held[0] in self.SPILL_SYMBOLS and getattr(self, 'kind', None) == 'cs' + if not carried and (self.is_lane_mask(ops[1]) or held[0] not in ('', 'const', 'ud', 'fixed_buffer_word')): raise Unsupported('v_writelane_b32 source s%d holds a lane mask or descriptor' % src[1]) - value = self.S(src[1]) - sym = self.sym.get(src[1]) if (self.sym.get(src[1]) or ('',))[0] == 'const' else None - uniform = self.uniform_source(ops[1]) + if carried: + # A descriptor or pointer word of a compute stage: the slot holds the symbol itself, so the reload + # restores the same word. Nothing is emitted: the translation has no run-time value for it. + if not names_one_value(held): + raise Unsupported('v_writelane_b32 source s%d holds a modified descriptor or pointer word' % src[1]) + value, sym, uniform, proven = None, held, True, self.spill_word_proven(src[1], held) + else: + value = self.S(src[1]) + sym = held if held[0] == 'const' else None + uniform = self.uniform_source(ops[1]) + proven = False slots = self.__dict__.setdefault('spill_slots', {}) - slots[(n, lane)] = (sym, uniform) + slots[(n, lane)] = (sym, uniform, proven) if getattr(self, 'spill_reach', None) is not None: - self.spill_writes[self.descriptor_instruction_index] = (sym, uniform) - self.emit('sp%d_%d = %s;' % (n, lane, value)) + self.spill_writes[self.descriptor_instruction_index] = (sym, uniform, proven) + if value is not None: + self.emit('sp%d_%d = %s;' % (n, lane, value)) def op_v_readlane_b32(self, ops, mods): if len(ops) != 3 or reg_range(ops[1]) is None or reg_range(ops[1])[0] != 'v': @@ -6738,13 +6780,15 @@ def op_v_readlane_b32(self, ops, mods): if dst is None: raise Unsupported('v_readlane_b32 requires an SGPR destination') if getattr(self, 'has_branches', False): - sym, uniform = self.spill_join(n, lane) + sym, uniform, proven = self.spill_join(n, lane) else: slot = getattr(self, 'spill_slots', {}).get((n, lane)) if slot is None: raise Unsupported('v_readlane_b32 reads v%d lane %d before any v_writelane_b32 wrote it' % (n, lane)) - sym, uniform = slot + sym, uniform, proven = slot self.set_s(dst[1], 'sp%d_%d' % (n, lane), sym=sym, uniform=uniform) + if getattr(self, 'descriptor_instruction_index', None) is not None: # unit probes build a Stage without translate() + self.__dict__.setdefault('reload_proven', {})[self.descriptor_instruction_index] = proven def op_v_mov_b32(self, ops, mods): if len(ops) != 2: From 1ce4ae912cdd9fb44aba0a727f4132e19d18f13e Mon Sep 17 00:00:00 2001 From: Ybucaille <83926195+Ybucaille@users.noreply.github.com> Date: Thu, 26 Nov 2026 07:38:47 +0100 Subject: [PATCH 4/5] startup: spill lane masks to VGPR lanes in compute Accept v_writelane_b32 of vcc_lo or of an SGPR tracked as a lane mask in compute stages, and the matching v_readlane_b32 into an SGPR or vcc_lo. The translator models a lane mask per invocation as that invocation's own bit. The write and the reload both ignore EXEC and copy the whole wave mask, so a reload that is only used as a mask has, in every invocation, the bit that invocation had at the write: the spill is the identity on the per-invocation bit. The slot variable takes the mask's variable at the write and the reload copies it back and marks the destination a mask again. The existing write-before-read proof decides what a reload sees; a reload that may see two writes of which one is a mask is refused, as are exec, vcc_hi and m0 sources, a mask not proven on every path, and a reload that an instruction reads as data (the translator does not refuse every numeric read of a marked register). Part of #1723. --- .github/workflows/windows.yml | 2 + AGENTS.md | 1 + CMakeLists.txt | 16 + docs/experiments.md | 4 +- ...artup_gfx2hlsl_compute_mask_spill_check.py | 455 ++++++++++++++++++ tests/startup_gfx2hlsl_compute_spill_check.py | 3 +- tools/startup/gfx2hlsl.py | 94 +++- 7 files changed, 560 insertions(+), 15 deletions(-) create mode 100644 tests/startup_gfx2hlsl_compute_mask_spill_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 46652da866..8068b680f0 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -136,6 +136,7 @@ jobs: (["tests/startup_gfx2hlsl_mad_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_compute_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_compute_spill_check.py", "binary", "llvm-mc-15"], {}), + (["tests/startup_gfx2hlsl_compute_mask_spill_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_compute_lds_b64_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_compute_lds_b128_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_calculated_target_check.py", "binary", "llvm-mc-15"], {}), @@ -323,6 +324,7 @@ jobs: tests/startup_gfx2hlsl_neg_check.py \ tests/startup_gfx2hlsl_compute_check.py \ tests/startup_gfx2hlsl_compute_spill_check.py \ + tests/startup_gfx2hlsl_compute_mask_spill_check.py \ tests/startup_compute_lds_b64_check.py \ tests/startup_compute_lds_b128_check.py \ tests/startup_compute_translate_check.py \ diff --git a/AGENTS.md b/AGENTS.md index e1cb072889..2a5e335326 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -169,6 +169,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_interp_p2_hi_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_compute_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_compute_spill_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_compute_mask_spill_check.py ./out/pe-env/bin/python -B tests/startup_compute_image_store_check.py ./out/pe-env/bin/python -B tests/startup_compute_image_store_d16_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_heap_record_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index e3cca0136e..9421dd60c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4146,6 +4146,22 @@ if(EXISTS "${INTAKE_PYTHON}") PASS_REGULAR_EXPRESSION "control ${control}: rejected at word=[0-9]+ ") endforeach() endforeach() + # gfx2hlsl constant-lane SGPR spills of lane masks in compute stages (#1723). + add_test(NAME startup_gfx2hlsl_compute_mask_spill_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_mask_spill_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_compute_mask_spill_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_mask_spill_check.py" dispatch "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_gfx2hlsl_compute_mask_spill_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + foreach(control IN ITEMS m-saveexec-dropped m-mask-inverted v-branch-inverted) + add_test(NAME startup_gfx2hlsl_compute_mask_spill_${control}_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_mask_spill_check.py" control ${control} "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_gfx2hlsl_compute_mask_spill_${control}_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77 + PASS_REGULAR_EXPRESSION "control ${control}: rejected at word=[0-9]+ ") + endforeach() + endforeach() # gfx2hlsl bounded 64-bit LDS exchanges (ds_write_b64, ds_read_b64) across s_barrier (Refs #2274, #1541). add_test(NAME startup_compute_lds_b64_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_compute_lds_b64_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index 0f7629860c..2fd83dc35a 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15044,7 +15044,9 @@ The same slots are lowered in pixel stages with branches. The slot variable is a Wave64 lanes and vertex stages: the same slot model now accepts lanes 0..63 and vertex stages. A wave32 program takes the lane select modulo 32, so lanes `k` and `k + 32` of one VGPR would be one slot there and two in wave64; the translator does not know the wave size and refuses that pair. Lane 64 and above, compute stages and register-selected lanes stay refused. `tests/startup_gfx2hlsl_spill_wave64_check.py` draws a pixel program with slots on lanes 32, 33, 40 and 63 (16 x 16 pixels) and a vertex program with slots on lanes 0 and 48 whose reloads reach a flat parameter (one pixel), each compared with a reference replayed from the slot rule; wrong-lane and ignored-overwrite translations must fail. Run `ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_spill_wave64" -V`. -Compute stages: the same slots are lowered in `cs` (with branches, through the same write-before-read proof), and a spilled descriptor or pointer word (`desc`, `ptr`, `ptrhi` symbols) is carried by identity: the slot holds the symbol, nothing is emitted, and the reload restores the same symbol. A word whose symbol does not name one value (a modified descriptor word, a pointer with a non-numeric offset) is refused when it is spilled; writes of different symbols joined at a read are refused; lane masks keep their refusal. A reload also keeps the provenance of the word it restores: a formatted buffer load past the entry block accepts a reloaded descriptor only when every write that can supply it was spilled from the entry block or from one untouched local table load of a proven pointer, i.e. when the original would have been accepted at the write. `tests/startup_gfx2hlsl_compute_spill_check.py` (`startup_gfx2hlsl_compute_spill*`) builds two gfx1013 compute fixtures from source (a read descriptor spilled in the entry block and the store descriptor spilled on both arms of a uniform branch, reloaded inside an `s_cbranch_execz` region for a formatted load and a store; a table pointer pair spilled and reloaded to load a descriptor), eight refused variants each with its exact reason, probes for `ptrhi` and modified words, eleven translator mutations that must each turn a control red, and a WARP/hardware dispatch with three mutated translations that must mismatch. +Compute stages: the same slots are lowered in `cs` (with branches, through the same write-before-read proof), and a spilled descriptor or pointer word (`desc`, `ptr`, `ptrhi` symbols) is carried by identity: the slot holds the symbol, nothing is emitted, and the reload restores the same symbol. A word whose symbol does not name one value (a modified descriptor word, a pointer with a non-numeric offset) is refused when it is spilled; writes of different symbols joined at a read are refused; lane masks are the next step (`startup_gfx2hlsl_compute_mask_spill`, below). A reload also keeps the provenance of the word it restores: a formatted buffer load past the entry block accepts a reloaded descriptor only when every write that can supply it was spilled from the entry block or from one untouched local table load of a proven pointer, i.e. when the original would have been accepted at the write. `tests/startup_gfx2hlsl_compute_spill_check.py` (`startup_gfx2hlsl_compute_spill*`) builds two gfx1013 compute fixtures from source (a read descriptor spilled in the entry block and the store descriptor spilled on both arms of a uniform branch, reloaded inside an `s_cbranch_execz` region for a formatted load and a store; a table pointer pair spilled and reloaded to load a descriptor), eight refused variants each with its exact reason, probes for `ptrhi` and modified words, eleven translator mutations that must each turn a control red, and a WARP/hardware dispatch with three mutated translations that must mismatch. + +Lane masks in compute: `v_writelane_b32` accepts `vcc_lo` or an SGPR the translator tracks as a mask (not proven-on-every-path marks, exec, `vcc_hi` and m0 stay refused), and `v_readlane_b32` reloads it into an SGPR (marked a mask again) or into `vcc_lo`. The translator models a mask per invocation as that invocation's own bit (a v_cmp stores `w`, or `exec & w` once EXEC is not known), and the write and the reload both ignore EXEC and copy the whole wave mask, so a reload used only as a mask has, in every invocation, the bit the invocation had at the write: the slot variable takes the mask's variable at the write and the reload copies it back. A reload is refused when an instruction reachable before its next write reads it as anything but a lane select or carry-in, an `s_cbranch_vccz` / `_vccnz` condition, a copy into `exec_lo` / `vcc_lo` or another spill, and when it may see two writes of which one is a mask. `tests/startup_gfx2hlsl_compute_mask_spill_check.py` (`startup_gfx2hlsl_compute_mask_spill*`) builds two gfx1013 compute fixtures from source (a `vcc_lo` and an SGPR compare result spilled, a uniform branch, both reloaded into SGPRs and used by `v_cndmask_b32_e64` and `s_and_saveexec_b32`; a `vcc_lo` reloaded into `vcc_lo` and used by `v_cndmask_b32_e32` and `s_cbranch_vccz`), twelve refused variants, translator mutations that must each turn a control red, and a WARP/hardware dispatch with three mutated translations that must mismatch. The binary32 table (`VEC_F`) applies the same operand-count rule. Before this change, a form one source short (`v_add_f32 v1, v2`) raised `IndexError` in `Stage.one` instead of `Unsupported`. A form with one source too many (`v_med3_f32 v1, v2, v3, v4, v5`) translated with the extra source dropped. `tests/startup_gfx2hlsl_operand_count_check.py` (`startup_gfx2hlsl_operand_count`) checks every operation of both tables: one source fewer and one source more are refused, and the exact count translates. It also checks that the constant and accumulator forms (`v_fmac_f32`, `v_mac_f32`, `v_fmamk_f32`, `v_fmaak_f32`, `v_madmk_f32`, `v_madak_f32`) keep translating with their usual operands. As a control, it loads the translator with the new `VEC_F` guard removed and requires both old failures to come back. diff --git a/tests/startup_gfx2hlsl_compute_mask_spill_check.py b/tests/startup_gfx2hlsl_compute_mask_spill_check.py new file mode 100644 index 0000000000..2836147fbe --- /dev/null +++ b/tests/startup_gfx2hlsl_compute_mask_spill_check.py @@ -0,0 +1,455 @@ +"""Constant-lane SGPR spills of lane masks in compute stages of tools/startup/gfx2hlsl.py (#1723). + + (no arguments) translation controls and translator mutations, pure Python + binary assemble the fixture sources (gfx1013) and compare with the words here + dispatch + translate, compile with DXC cs_6_0, dispatch and compare every word + control + the same dispatch with a mutated translation, which must mismatch + +`v_writelane_b32 vN, M, k` copies the whole wave mask M (vcc_lo, or an SGPR holding a lane mask) into lane k of vN +whatever EXEC is, and `v_readlane_b32 D, vN, k` copies it back to every lane (AMD RDNA 2 ISA reference, VOP3 lane +operations). The translator models a lane mask per invocation as that invocation's own bit (a v_cmp stores `w`, or +`exec & w` once EXEC is not known, so an inactive lane's bit is 0). A reload that is only used as a mask afterwards +sees, in every invocation, the bit that invocation had at the write: the spill is the identity on that bit, so the slot +variable takes the mask's own variable at the write and the reload marks the destination a mask again. + +Fixtures (source-built gfx1013, 4 groups of 8 lanes, root constants key and stride in s4-s5, one descriptor table in s6): +- M spills the compare result vcc_lo (lanes below 5, lane 0 of v40) and an SGPR compare result s26 (lanes below 3, + lane 1), branches on the key, clobbers both, reloads them into SGPRs, selects with v_cndmask_b32_e64 and runs a + masked xor through s_and_saveexec_b32. +- V spills a vcc_lo that is the same in every lane of a group (2 > group index), clobbers vcc_lo, reloads it into + vcc_lo itself, selects with v_cndmask_b32_e32 and skips an add with s_cbranch_vccz. +""" +import re +import struct +import subprocess +import sys +import tempfile +import types +from pathlib import Path + +HERE = Path(__file__).resolve().parent +TOOLS = HERE.parent / 'tools' +sys.path.insert(0, str(TOOLS / 'startup')) +sys.path.insert(0, str(HERE)) +import gfx2hlsl as G # noqa: E402 +import startup_gfx2hlsl_compute_check as C # noqa: E402 +import startup_gfx2hlsl_compute_spill_check as S # noqa: E402 + +failures = [] + + +def check(label, ok, detail=''): + print('%s: %s' % (label, 'ok' if ok else 'MISMATCH')) + if not ok: + failures.append(label) + if detail: + print(' ' + str(detail)[:1500]) + + +PROLOGUE = """\ +s_mov_b32 s10, s6 +s_mov_b32 s11, 0 +s_load_dwordx4 s[12:15], s[10:11], 0x0 +s_load_dwordx4 s[16:19], s[10:11], 0x10 +s_waitcnt lgkmcnt(0) +s_lshl_b32 s20, s7, 3 +v_add_nc_u32 v3, s20, v0 +v_lshlrev_b32 v4, 2, v3 +buffer_load_dword v5, v4, s[12:15], 0 offen +s_waitcnt vmcnt(0) +""" +M_SPILL = """\ +v_cmp_gt_u32 vcc_lo, 5, v0 +v_writelane_b32 v40, vcc_lo, 0 +v_cmp_gt_u32 s26, 3, v0 +v_writelane_b32 v40, s26, 1 +""" +M_BRANCH = """\ +s_and_b32 s22, s4, 1 +s_cmp_lg_u32 s22, 0 +s_cbranch_scc0 Lelse +v_mov_b32 v7, 100 +s_branch Ljoin +Lelse: +v_mov_b32 v7, 200 +Ljoin: +""" +M_CLOBBER = """\ +v_cmp_gt_u32 vcc_lo, 1, v0 +s_mov_b32 s26, 0 +""" +M_RELOAD = """\ +v_readlane_b32 s25, v40, 0 +v_readlane_b32 s27, v40, 1 +""" +M_USE = """\ +v_cndmask_b32_e64 v6, v5, v7, s25 +v_cndmask_b32_e64 v6, v6, 7, s27 +s_and_saveexec_b32 s23, s25 +v_xor_b32 v6, 0x55, v6 +s_mov_b32 exec_lo, s23 +buffer_store_dword v6, v4, s[16:19], 0 offen +s_endpgm +""" +SOURCE_M = PROLOGUE + M_SPILL + M_BRANCH + M_CLOBBER + M_RELOAD + M_USE +SOURCE_V = PROLOGUE + """\ +v_mov_b32 v8, s7 +v_cmp_gt_u32 vcc_lo, 2, v8 +v_writelane_b32 v40, vcc_lo, 0 +v_cmp_gt_u32 vcc_lo, 1, v0 +v_readlane_b32 vcc_lo, v40, 0 +v_cndmask_b32_e32 v6, v5, v8, vcc_lo +s_cbranch_vccz Lskip +v_add_nc_u32 v6, 1000, v6 +Lskip: +buffer_store_dword v6, v4, s[16:19], 0 offen +s_endpgm +""" +EXPECTED_ACCEPTED = {'M': SOURCE_M, 'V': SOURCE_V} + +# name -> (base source, [(old, new)], what the refusal must say, why it is refused). Each variant is a fixture with one +# change; it is assembled in binary mode like the fixtures and must be refused for that reason and no other. +VARIANTS = { + 'm0-source': (SOURCE_M, [('v_writelane_b32 v40, s26, 1\n', 's_mov_b32 m0, 5\nv_writelane_b32 v40, m0, 1\n')], + 'source m0 is a lane mask or m0', 'm0 is not a mask: it is refused as a spill source'), + 'exec-source': (SOURCE_M, [('v_writelane_b32 v40, s26, 1\n', 'v_writelane_b32 v40, exec_lo, 1\n')], + 'source exec_lo is a lane mask or m0', 'exec is refused as a spill source'), + 'vcc-hi-source': (SOURCE_M, [('v_writelane_b32 v40, vcc_lo, 0\n', 'v_writelane_b32 v40, vcc_hi, 0\n')], + 'source vcc_hi is a lane mask or m0', 'vcc_hi is not the wave32 mask'), + 'numeric-use': (SOURCE_M, [(M_RELOAD, M_RELOAD + 'v_add_nc_u32 v9, s25, v5\n')], 'reads as data', + 'the reloaded mask is read as numeric data'), + 'numeric-use-vcc': (SOURCE_V, [('Lskip:\n', 'Lskip:\nv_add_nc_u32 v9, vcc_lo, v5\n')], 'reads as data', + 'the reloaded vcc_lo is read as numeric data'), + 'masks-joined': (SOURCE_M, [(M_BRANCH, M_BRANCH.replace('v_mov_b32 v7, 100\n', 'v_mov_b32 v7, 100\n' + 'v_writelane_b32 v40, s26, 0\n'))], + 'one of them a lane mask', 'one arm overwrites lane 0 with a different mask'), + 'mask-and-number-joined': (SOURCE_M, [(M_BRANCH, M_BRANCH.replace('v_mov_b32 v7, 100\n', 'v_mov_b32 v7, 100\n' + 's_mov_b32 s28, 9\nv_writelane_b32 v40, s28, 0\n'))], + 'one of them a lane mask', 'one arm overwrites lane 0 with a number'), + 'reload-before-write': (SOURCE_M, [('v_writelane_b32 v40, s26, 1\n', '')], 'no v_writelane_b32 of it on some path', + 'lane 1 is never written'), + 'mask-reloaded-into-vcc-from-number': (SOURCE_V, [('v_writelane_b32 v40, vcc_lo, 0\n', 's_mov_b32 s28, 1\n' + 'v_writelane_b32 v40, s28, 0\n')], + 'requires an SGPR destination', 'a number is reloaded into vcc_lo'), + 'mask-from-one-arm': (SOURCE_M, [('v_cmp_gt_u32 s26, 3, v0\nv_writelane_b32 v40, s26, 1\n', 's_mov_b32 s26, 0\n'), + ('v_mov_b32 v7, 100\n', 'v_mov_b32 v7, 100\nv_cmp_gt_u32 s26, 3, v0\n'), + ('Ljoin:\n', 'Ljoin:\nv_writelane_b32 v40, s26, 1\n')], + 'lane mask not proven on every path', 'the mask is computed in one arm only'), + 'write-in-execz': (SOURCE_M, [(M_CLOBBER, M_CLOBBER + 's_mov_b32 s21, exec_lo\nv_cmp_gt_u32 vcc_lo, 5, v0\n' + 's_mov_b32 exec_lo, vcc_lo\ns_cbranch_execz Lz\nv_writelane_b32 v40, vcc_lo, 2\n' + 'Lz:\ns_mov_b32 exec_lo, s21\n')], + 'inside a block s_cbranch_execz skips per lane', 'a mask written inside an execz region'), + 'loop-carried': (SOURCE_M, [('Ljoin:\n', 'Ljoin:\ns_mov_b32 s22, 0\nLloop:\nv_readlane_b32 s25, v40, 0\n' + 'v_writelane_b32 v40, s25, 0\ns_add_u32 s22, s22, 1\ns_cmp_lt_u32 s22, 2\n' + 's_cbranch_scc1 Lloop\n')], + 'written later in a loop', 'a mask reloaded by a loop body that writes it again'), +} + + +def variant_source(name): + base, edits = VARIANTS[name][:2] + for old, new in edits: + assert base.count(old) == 1, (name, old) + base = base.replace(old, new) + return base + + +def all_sources(): + return {**EXPECTED_ACCEPTED, **{name: variant_source(name) for name in VARIANTS}} + + +ENCODING = (C.USER7 | C.TGID_X | C.tidig(1), (8, 1, 1), (4, 1, 1)) +WORDS = { + 'M': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5050f6600' + '060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'V': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf0702107e8210887d280061d76a0001028100887d6a0060d72801010205110c02020086bfff0c0c4ae8030000' + '001070e004060480000081bf'), + 'm0-source': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d4830002028503fcbe280061d77c02010204811687168007bf030084bf' + 'ff020e7e64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5' + '050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'exec-source': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d77e02010204811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5050f6600' + '060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'vcc-hi-source': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76b0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5050f6600' + '060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'numeric-use': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102190a124a060001d5' + '050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'numeric-use-vcc': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf0702107e8210887d280061d76a0001028100887d6a0060d72801010205110c02020086bfff0c0c4ae8030000' + '6a0a124a001070e004060480000081bf'), + 'masks-joined': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf050084bfff020e7e' + '64000000280061d71a000102020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102' + '060001d5050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'mask-and-number-joined': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf060084bfff020e7e' + '6400000089039cbe280061d71c000102020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d7' + '28030102060001d5050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'reload-before-write': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d48300020204811687168007bf030084bfff020e7e64000000020082bf' + 'ff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5050f6600060001d5060f6d00' + '193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'mask-reloaded-into-vcc-from-number': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf0702107e8210887d81039cbe280061d71c0001028100887d6a0060d72801010205110c02020086bfff0c0c4a' + 'e8030000001070e004060480000081bf'), + 'mask-from-one-arm': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a00010280039abe04811687168007bf050084bfff020e7e640000001a00c4d483000202' + '020082bfff020e7ec8000000280061d71a0201028100887d80039abe190060d7280101021b0060d728030102060001d5' + '050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + 'write-in-execz': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe7e0395be8500887d6a03febe020088bf280061d76a040102' + '1503febe190060d7280101021b0060d728030102060001d5050f6600060001d5060f6d00193c97beff0c0c3a55000000' + '1703febe001070e004060480000081bf'), + 'loop-carried': bytes.fromhex( + '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' + '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec8000000800396be190060d728010102280061d7190001021681168016820abff9ff85bf' + '8100887d80039abe190060d7280101021b0060d728030102060001d5050f6600060001d5060f6d00193c97beff0c0c3a' + '550000001703febe001070e004060480000081bf'), +} +WORDS_OUT = C.WORDS +SENTINEL = C.SENTINEL +KEY = C.KEY + + +def translate(name, code=None, module=G): + return module.ComputePipeline(code or WORDS[name], C.root_blob(), ENCODING[0], ENCODING[1]) + + +def refusal(name, module=G): + try: + translate(name, module=module).hlsl_cs() + except module.Unsupported as reason: + return str(reason) + return None + + +def expected(name): + out = [SENTINEL] * WORDS_OUT + for gx in range(4): + for tx in range(8): + i, word = gx * 8 + tx, C.input_word(gx * 8 + tx) + if name == 'M': + v7 = 100 if KEY & 1 else 200 + a, b = tx < 5, tx < 3 + word = v7 if a else word + word = 7 if b else word + out[i] = word ^ 0x55 if a else word + else: + word = gx if gx < 2 else word + out[i] = word + 1000 if gx < 2 else word + return out + + +def controls(module=G, quiet=False): + """-> the labels of the controls that failed (all printed unless quiet).""" + red = [] + + def verdict(label, ok, detail=''): + if not ok: + red.append(label) + if not quiet: + check(label, ok, detail) + + for name in EXPECTED_ACCEPTED: + reason = refusal(name, module) + verdict('fixture %s translates (%s)' % (name, reason), reason is None) + for name, (_, _, fragment, why) in VARIANTS.items(): + reason = refusal(name, module) + verdict('refused: %s (%s)' % (why, reason), reason is not None and fragment in reason, reason) + verdict('mask spills stay refused in a pixel stage (%s)' % probe_pixel(module), 'holds a lane mask or descriptor' in + probe_pixel(module)) + return red + + +def probe_pixel(module): + """A pixel stage that spills a lane mask keeps the refusal of step 1: only compute stages model the mask this way.""" + stage = module.Stage.__new__(module.Stage) + stage.kind, stage.lines, stage.sym = 'ps', [], {} + stage.used_s, stage.used_v, stage.uniform_v, stage.varying_s = set(), set(), set(), set() + stage.scc_uniform, stage.exec_known, stage.has_branches, stage.blocks, stage.masks = True, True, False, [], {20} + stage.join_masks = set() + stage.ins = [(0, 1, 'v_writelane_b32', 'v_writelane_b32 v10, s20, 3')] + try: + stage.one('v_writelane_b32', 'v_writelane_b32 v10, s20, 3') + except module.Unsupported as why: + return str(why) + return 'accepted' + + +# Each translator mutation must turn at least one control red. +MUTATIONS = { + 'a mask is not marked again after the reload': (' if mask:\n self.masks.add(dst[1])', + ' if False:\n self.masks.add(dst[1])'), + 'a reloaded mask is read as data': (" if not (self.per_lane_read(j, key) or copy or", " if not (True or copy or"), + 'a lane select is not a mask read': (" if not (self.per_lane_read(j, key) or copy or", " if not (copy or"), + 'two writes with a mask join': (' if any(s[3] for s in stored) and len(stored) > 1:', ' if False:'), + 'm0 is a mask source': (" elif ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'):", + " elif ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi'):"), + 'exec is a mask source': (" elif ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'):", + " elif ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'm0'):"), + 'vcc_hi is a mask source': (" if ops[1] == 'vcc_lo' and self.spills_lane_masks():", + " if ops[1] in ('vcc_hi', 'vcc_lo') and self.spills_lane_masks():"), + 'a mask not proven on every path is spilled': (' if src[1] in self.join_masks:', ' if False:'), + 'the write stores the inverted bit': ("value, sym, uniform, proven, mask = 'vcc', None", + "value, sym, uniform, proven, mask = '(vcc ^ 1u)', None"), + 'the reload restores the neighbouring slot': (" self.set_s(dst[1], 'sp%d_%d' % (n, lane), sym=None if mask else sym,", + " self.set_s(dst[1], 'sp%d_%d' % (n, lane ^ int(mask)), sym=None if mask else sym,"), + 'every stage spills masks': (" return getattr(self, 'kind', None) == 'cs'", ' return True'), + 'a number is reloaded into vcc_lo': (" if not (mask and ops[0] == 'vcc_lo'):", " if ops[0] != 'vcc_lo':"), + 'no loop-carried refusal': (" if max(writes) > i:", " if False:"), + 'no write-before-read refusal': (" if writes is None:\n raise Unsupported('v_readlane_b32 reads v%d lane %d with no", + " if writes is None and False:\n raise Unsupported('v_readlane_b32 reads v%d lane %d with no"), +} + + +def mutated(old, new): + source = (TOOLS / 'startup' / 'gfx2hlsl.py').read_text(encoding='utf-8').replace('\r\n', '\n') + assert source.count(old) == 1, old + module = types.ModuleType('gfx2hlsl_mutant') + module.__file__ = str(TOOLS / 'startup' / 'gfx2hlsl.py') + exec(compile(source.replace(old, new), module.__file__, 'exec'), module.__dict__) + return module + + +def check_mutations(): + for label, (old, new) in MUTATIONS.items(): + try: + module = mutated(old, new) + red = controls(module, quiet=True) + check_text(module, quiet=True) + except Exception as error: # a mutant that crashes the translator is caught as well + red = ['crash: %s' % error] + check('mutation "%s" turns %d control(s) red: %s' % (label, len(red), red[:3]), bool(red)) + + +def check_text(module=G, quiet=False): + """-> the labels of the text checks that failed. The slot takes the mask's variable at the write and the reload copies + it back, which is what makes the spill the identity on each invocation's bit.""" + red = [] + + def verdict(label, ok, detail=''): + if not ok: + red.append(label) + if not quiet: + check(label, ok, detail) + + m, v = translate('M', module=module).hlsl_cs(), translate('V', module=module).hlsl_cs() + lines = [line.strip() for line in m.splitlines()] + verdict('M: the slots take the mask variables, the reloads copy them back', + 'sp40_0 = vcc;' in lines and 'sp40_1 = s26;' in lines and 's25 = sp40_0;' in lines and 's27 = sp40_1;' in lines, m) + verdict('M: the compare writes the bit of its own invocation', 'vcc = ((0x00000005u > v0) ? 1u : 0u);' in lines, m) + verdict('M: the reloaded masks select and narrow EXEC', 'v6 = (s25 & 1) ? v7 : v5;' in lines and + 'exec = (exec & s25) & 1; scc = exec != 0;' in lines, m) + vl = [line.strip() for line in v.splitlines()] + verdict('V: vcc_lo is reloaded into vcc and drives the branch', + 'sp40_0 = vcc;' in vl and 'vcc = sp40_0;' in vl and 'pc = ((vcc & 1) == 0) ? 2 : 1;' in vl, v) + return red + + +# ---- binary and dispatch --------------------------------------------------------------------------------------------- +def check_binary(llvm_mc): + for name, source in all_sources().items(): + built = S.assemble_labels(llvm_mc, source) + check('llvm-mc assembles %s to the recorded words' % name, built == WORDS[name], built.hex()) + + +# control -> (case, text the translation must contain once, replacement) +CONTROLS = { + 'm-saveexec-dropped': ('M', 'exec = (exec & s25) & 1; scc = exec != 0;', 'scc = exec != 0;'), + 'm-mask-inverted': ('M', 'sp40_0 = vcc;', 'sp40_0 = vcc ^ 1u;'), + 'v-branch-inverted': ('V', 'pc = ((vcc & 1) == 0) ? 2 : 1;', 'pc = ((vcc & 1) != 0) ? 2 : 1;'), +} + + +def run_case(name, dxc, runner, adapter, directory, control=None): + hlsl = translate(name).hlsl_cs() + if control: + _, old, new = CONTROLS[control] + assert hlsl.count(old) == 1, (control, hlsl) + hlsl = hlsl.replace(old, new) + src, cso, case = (directory / ('%s.%s' % (name, ext)) for ext in ('hlsl', 'cso', 'case')) + src.write_text(hlsl, encoding='ascii') + subprocess.run([dxc, '-T', 'cs_6_0', '-E', 'main', '-Fo', str(cso), str(src)], check=True) + groups = ENCODING[2] + constants = [KEY, C.STRIDE] + [0] * 14 + case.write_bytes(struct.pack(' 32-bit pattern from s_mov_b32 of an inline float constant self.has_branches = bool(targets) - self.spill_slots = {} # (vgpr, lane) -> (sym, uniform, proven) for constant-lane SGPR spills + self.spill_slots = {} # (vgpr, lane) -> (sym, uniform, proven, mask) for constant-lane SGPR spills self.vcc_events = {} self.entry_table_pointers = { reg: sym[1] for reg, sym in init_sym.items() @@ -2994,7 +2994,7 @@ def translate(self): self.descriptor_block_first = 0 self.uniform_expr = {} # proven VGPR -> uniform HLSL expression over scalars that defined it self.uniform_branches = set() # s_cbranch_* offsets proven uniform in this pass (spill-slot proof) - self.spill_writes = {} # instruction index of a v_writelane_b32 translated in this pass -> (sym, uniform, proven) + self.spill_writes = {} # instruction index of a v_writelane_b32 translated in this pass -> (sym, uniform, proven, mask) self.heap_index = {} # SGPR -> what its word is for a resource-heap s_load (see heap_record) self.heap_index_was = {} # the marks the current instruction's destinations held before it self.since_join = None # SGPRs proven uniform since the last branch target (None: none reached) @@ -6610,6 +6610,19 @@ def op_v_readfirstlane_b32(self, ops, mods): # Compute stages (cs) use the same proof, branches included. A spilled descriptor or pointer word (SPILL_SYMBOLS) is a # symbol with no run-time value: the slot carries the symbol, the reload restores it, and the word keeps the # provenance the formatted loads require (formatted_descriptor_from_block follows a reload to the writes that supply it). + # Lane masks (compute only). The translator models a lane mask (vcc_lo, or an SGPR in self.masks) per invocation + # as that invocation's own bit: `vcc` / `sN` hold bit 0 of the wave mask, and a v_cmp stores `w` (all lanes active) + # or `exec & w`, so an inactive lane's bit is already 0 in the variable. `v_writelane_b32 vN, M, k` copies the whole + # wave mask into lane k whatever EXEC is, `v_readlane_b32 D, vN, k` copies it back to every lane, so a mask that is + # only ever used as a mask afterwards (reject_lane_mask_data and the mask rules refuse every numeric read of a marked + # register, vcc included) has, after the reload, each invocation's bit equal to its bit at the write: the spill is + # the identity on the per-invocation bit. The slot variable spN_k is therefore assigned the mask's own variable at + # the write, in every invocation, and the reload copies it into the destination and marks it a mask again (or + # assigns vcc for a `vcc_lo` destination). The slot proof of spill_join (written on every path, no loop-carried + # read, no write in a region the wave runs while the invocation skips it; writes in s_cbranch_execz blocks are + # refused by execz_refusals) decides what a reload sees. A reload that may see two writes of which one is a mask + # is refused: nothing proves they store the same mask. exec, vcc and vcc_hi (64-bit forms), and m0 stay refused + # as sources, a mask register in join_masks (mark not proven on every path) too. SPILL_LANES = 64 SPILL_STAGES = ('ps', 'vs', 'ms', 'cs') # Symbols a spill carries by identity: the SGPR holds no run-time value in the translation, so the slot holds none either. @@ -6677,11 +6690,49 @@ def spill_join(self, n, lane): raise Unsupported('v_readlane_b32 reads v%d lane %d outside the region of the branch at %#x that writes ' 'it, which is not proven uniform' % (n, lane, off)) stored = [self.spill_writes[w] for w in sorted(writes)] + if any(s[3] for s in stored) and len(stored) > 1: + # Which mask the reload holds would depend on the path; nothing proves the writes store the same one. + raise Unsupported('v_readlane_b32 joins %d writes of v%d lane %d, one of them a lane mask' % (len(stored), n, lane)) sym = stored[0][0] if all(s[0] == stored[0][0] for s in stored) else None if sym is None and any(s[0] is not None and s[0][0] in self.SPILL_SYMBOLS for s in stored): # A descriptor or pointer word has no run-time variable: only the same symbol on every path can be reloaded. raise Unsupported('v_readlane_b32 joins different descriptor or pointer words of v%d lane %d' % (n, lane)) - return sym, len(stored) == 1 and stored[0][1], all(s[2] for s in stored) + return sym, len(stored) == 1 and stored[0][1], all(s[2] for s in stored), stored[0][3] + + def require_mask_reads_only(self, key): + """Refuse a reloaded lane mask (scalar state `key`: 'vcc' or 'sN') that an instruction reachable before its next + write reads as anything but a mask. The translator marks the register a mask but does not refuse every numeric + read of one, and a numeric read would see the whole wave mask where the model has one bit. Accepted reads: a lane + select or carry-in (per_lane_read), a read by s_cbranch_vccz / _vccnz, a copy into exec_lo or vcc_lo, and another + spill of it.""" + effects = [scalar_effects(name, asm) for _, _, name, asm in self.ins] + index = {off: i for i, (off, *_) in enumerate(self.ins)} + todo, seen = [self.descriptor_instruction_index + 1], set() + while todo: + j = todo.pop() + if j in seen or j >= len(self.ins): + continue + seen.add(j) + _, _, name, asm = self.ins[j] + base = re.sub(r'_e(32|64)$', '', name) + ops = parse(asm)[1] + if key in effects[j][0]: + copy = base == 's_mov_b32' and len(ops) == 2 and ops[0] in ('exec_lo', 'vcc_lo') and key in scalar_tokens(ops[1]) + if not (self.per_lane_read(j, key) or copy or base == 'v_writelane_b32' or + (key == 'vcc' and base in ('s_cbranch_vccz', 's_cbranch_vccnz'))): + raise Unsupported('v_readlane_b32 reloads a lane mask that %s reads as data' % name) + if key in effects[j][1]: + continue + target = None + if base == 's_branch' or base.startswith('s_cbranch'): + target = index.get(int(ops[0].split('_')[1], 16)) + if base not in ('s_branch', 's_endpgm'): + todo.append(j + 1) + if target is not None: + todo.append(target) + + def spills_lane_masks(self): + return getattr(self, 'kind', None) == 'cs' def spill_operands(self, name, ops, mods): if mods or len(ops) != 3: @@ -6740,11 +6791,14 @@ def require_spill_only(self, n, name): def op_v_writelane_b32(self, ops, mods): n, src, lane = self.spill_operands('v_writelane_b32', ops, mods) + mask = False if src is None: # Slot 57 mesh keeps ordinary scalar spills only (R0Y00 on #2637). Raw EXEC words are not # proven under the one-lane model (exec_hi would be wrong under full EXEC; exec_lo under # a lane-0 mask would reload per thread). mesh_entry_aot still models EXEC for #2127. - if ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'): + if ops[1] == 'vcc_lo' and self.spills_lane_masks(): + value, sym, uniform, proven, mask = 'vcc', None, False, False, True + elif ops[1] in ('vcc', 'vcc_lo', 'vcc_hi', 'exec', 'exec_lo', 'exec_hi', 'm0'): raise Unsupported('v_writelane_b32 source %s is a lane mask or m0' % ops[1]) else: value, sym, uniform, proven = self.u(ops[1]), None, True, False @@ -6753,9 +6807,14 @@ def op_v_writelane_b32(self, ops, mods): else: held = self.sym.get(src[1]) or ('',) carried = held[0] in self.SPILL_SYMBOLS and getattr(self, 'kind', None) == 'cs' - if not carried and (self.is_lane_mask(ops[1]) or held[0] not in ('', 'const', 'ud', 'fixed_buffer_word')): + mask = self.spills_lane_masks() and src[1] in self.masks + if mask: + if src[1] in self.join_masks: + raise Unsupported('v_writelane_b32 source s%d holds a lane mask not proven on every path' % src[1]) + value, sym, uniform, proven = self.S(src[1]), (held if held[0] == 'execcopy' else None), False, False + elif not carried and (self.is_lane_mask(ops[1]) or held[0] not in ('', 'const', 'ud', 'fixed_buffer_word')): raise Unsupported('v_writelane_b32 source s%d holds a lane mask or descriptor' % src[1]) - if carried: + elif carried: # A descriptor or pointer word of a compute stage: the slot holds the symbol itself, so the reload # restores the same word. Nothing is emitted: the translation has no run-time value for it. if not names_one_value(held): @@ -6767,9 +6826,9 @@ def op_v_writelane_b32(self, ops, mods): uniform = self.uniform_source(ops[1]) proven = False slots = self.__dict__.setdefault('spill_slots', {}) - slots[(n, lane)] = (sym, uniform, proven) + slots[(n, lane)] = (sym, uniform, proven, mask) if getattr(self, 'spill_reach', None) is not None: - self.spill_writes[self.descriptor_instruction_index] = (sym, uniform, proven) + self.spill_writes[self.descriptor_instruction_index] = (sym, uniform, proven, mask) if value is not None: self.emit('sp%d_%d = %s;' % (n, lane, value)) @@ -6777,16 +6836,25 @@ def op_v_readlane_b32(self, ops, mods): if len(ops) != 3 or reg_range(ops[1]) is None or reg_range(ops[1])[0] != 'v': raise Unsupported('v_readlane_b32 requires a VGPR source') n, dst, lane = self.spill_operands('v_readlane_b32', ops, mods) - if dst is None: - raise Unsupported('v_readlane_b32 requires an SGPR destination') if getattr(self, 'has_branches', False): - sym, uniform, proven = self.spill_join(n, lane) + sym, uniform, proven, mask = self.spill_join(n, lane) else: slot = getattr(self, 'spill_slots', {}).get((n, lane)) if slot is None: raise Unsupported('v_readlane_b32 reads v%d lane %d before any v_writelane_b32 wrote it' % (n, lane)) - sym, uniform, proven = slot - self.set_s(dst[1], 'sp%d_%d' % (n, lane), sym=sym, uniform=uniform) + sym, uniform, proven, mask = slot + if dst is None: + if not (mask and ops[0] == 'vcc_lo'): + raise Unsupported('v_readlane_b32 requires an SGPR destination') + self.emit('vcc = sp%d_%d;' % (n, lane)) + self.require_mask_reads_only('vcc') + return + self.set_s(dst[1], 'sp%d_%d' % (n, lane), sym=None if mask else sym, uniform=uniform) + if mask: + self.masks.add(dst[1]) + if sym is not None: + self.sym[dst[1]] = sym + self.require_mask_reads_only('s%d' % dst[1]) if getattr(self, 'descriptor_instruction_index', None) is not None: # unit probes build a Stage without translate() self.__dict__.setdefault('reload_proven', {})[self.descriptor_instruction_index] = proven From dc8e0f34b167f9367cb1b6e24603de5eeb844bf0 Mon Sep 17 00:00:00 2001 From: Ybucaille <83926195+Ybucaille@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:26:30 +0100 Subject: [PATCH 5/5] startup: record compute spill fixtures in the LLVM 15 encoding CI assembles with LLVM 15, but the words were recorded from a newer llvm-mc. For VOP3 v_readlane_b32, v_writelane_b32 and v_cmp the unused src2 field is 0 in LLVM 15 and the inline constant 0 (128) in newer LLVM, so every fixture holding one mismatched in CI. assemble_labels now clears that unused field, and the recorded words are the LLVM 15 output, so the check holds with both versions. All other bits are still compared. --- ...artup_gfx2hlsl_compute_mask_spill_check.py | 56 ++++----- tests/startup_gfx2hlsl_compute_spill_check.py | 110 ++++++++++-------- 2 files changed, 88 insertions(+), 78 deletions(-) diff --git a/tests/startup_gfx2hlsl_compute_mask_spill_check.py b/tests/startup_gfx2hlsl_compute_mask_spill_check.py index 2836147fbe..d1d6f02def 100644 --- a/tests/startup_gfx2hlsl_compute_mask_spill_check.py +++ b/tests/startup_gfx2hlsl_compute_mask_spill_check.py @@ -164,72 +164,72 @@ def all_sources(): WORDS = { 'M': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' - '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5050f6600' + '703f8cbf8500887d280061d76a0001001a00c4d483000200280061d71a02010004811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101001b0060d728030100060001d5050f6600' '060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'V': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf0702107e8210887d280061d76a0001028100887d6a0060d72801010205110c02020086bfff0c0c4ae8030000' + '703f8cbf0702107e8210887d280061d76a0001008100887d6a0060d72801010005110c02020086bfff0c0c4ae8030000' '001070e004060480000081bf'), 'm0-source': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d4830002028503fcbe280061d77c02010204811687168007bf030084bf' - 'ff020e7e64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5' + '703f8cbf8500887d280061d76a0001001a00c4d4830002008503fcbe280061d77c02010004811687168007bf030084bf' + 'ff020e7e64000000020082bfff020e7ec80000008100887d80039abe190060d7280101001b0060d728030100060001d5' '050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'exec-source': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d77e02010204811687168007bf030084bfff020e7e' - '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5050f6600' + '703f8cbf8500887d280061d76a0001001a00c4d483000200280061d77e02010004811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101001b0060d728030100060001d5050f6600' '060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'vcc-hi-source': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76b0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' - '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5050f6600' + '703f8cbf8500887d280061d76b0001001a00c4d483000200280061d71a02010004811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101001b0060d728030100060001d5050f6600' '060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'numeric-use': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' - '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102190a124a060001d5' + '703f8cbf8500887d280061d76a0001001a00c4d483000200280061d71a02010004811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe190060d7280101001b0060d728030100190a124a060001d5' '050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'numeric-use-vcc': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf0702107e8210887d280061d76a0001028100887d6a0060d72801010205110c02020086bfff0c0c4ae8030000' + '703f8cbf0702107e8210887d280061d76a0001008100887d6a0060d72801010005110c02020086bfff0c0c4ae8030000' '6a0a124a001070e004060480000081bf'), 'masks-joined': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf050084bfff020e7e' - '64000000280061d71a000102020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102' + '703f8cbf8500887d280061d76a0001001a00c4d483000200280061d71a02010004811687168007bf050084bfff020e7e' + '64000000280061d71a000100020082bfff020e7ec80000008100887d80039abe190060d7280101001b0060d728030100' '060001d5050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'mask-and-number-joined': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf060084bfff020e7e' - '6400000089039cbe280061d71c000102020082bfff020e7ec80000008100887d80039abe190060d7280101021b0060d7' - '28030102060001d5050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), + '703f8cbf8500887d280061d76a0001001a00c4d483000200280061d71a02010004811687168007bf060084bfff020e7e' + '6400000089039cbe280061d71c000100020082bfff020e7ec80000008100887d80039abe190060d7280101001b0060d7' + '28030100060001d5050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'reload-before-write': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d48300020204811687168007bf030084bfff020e7e64000000020082bf' - 'ff020e7ec80000008100887d80039abe190060d7280101021b0060d728030102060001d5050f6600060001d5060f6d00' + '703f8cbf8500887d280061d76a0001001a00c4d48300020004811687168007bf030084bfff020e7e64000000020082bf' + 'ff020e7ec80000008100887d80039abe190060d7280101001b0060d728030100060001d5050f6600060001d5060f6d00' '193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'mask-reloaded-into-vcc-from-number': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf0702107e8210887d81039cbe280061d71c0001028100887d6a0060d72801010205110c02020086bfff0c0c4a' + '703f8cbf0702107e8210887d81039cbe280061d71c0001008100887d6a0060d72801010005110c02020086bfff0c0c4a' 'e8030000001070e004060480000081bf'), 'mask-from-one-arm': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a00010280039abe04811687168007bf050084bfff020e7e640000001a00c4d483000202' - '020082bfff020e7ec8000000280061d71a0201028100887d80039abe190060d7280101021b0060d728030102060001d5' + '703f8cbf8500887d280061d76a00010080039abe04811687168007bf050084bfff020e7e640000001a00c4d483000200' + '020082bfff020e7ec8000000280061d71a0201008100887d80039abe190060d7280101001b0060d728030100060001d5' '050f6600060001d5060f6d00193c97beff0c0c3a550000001703febe001070e004060480000081bf'), 'write-in-execz': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' - '64000000020082bfff020e7ec80000008100887d80039abe7e0395be8500887d6a03febe020088bf280061d76a040102' - '1503febe190060d7280101021b0060d728030102060001d5050f6600060001d5060f6d00193c97beff0c0c3a55000000' + '703f8cbf8500887d280061d76a0001001a00c4d483000200280061d71a02010004811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec80000008100887d80039abe7e0395be8500887d6a03febe020088bf280061d76a040100' + '1503febe190060d7280101001b0060d728030100060001d5050f6600060001d5060f6d00193c97beff0c0c3a55000000' '1703febe001070e004060480000081bf'), 'loop-carried': bytes.fromhex( '06038abe80038bbe050308f4000000fa050408f4100000fa7fc08cbf0783148f1400064a82060834001030e004050380' - '703f8cbf8500887d280061d76a0001021a00c4d483000202280061d71a02010204811687168007bf030084bfff020e7e' - '64000000020082bfff020e7ec8000000800396be190060d728010102280061d7190001021681168016820abff9ff85bf' - '8100887d80039abe190060d7280101021b0060d728030102060001d5050f6600060001d5060f6d00193c97beff0c0c3a' + '703f8cbf8500887d280061d76a0001001a00c4d483000200280061d71a02010004811687168007bf030084bfff020e7e' + '64000000020082bfff020e7ec8000000800396be190060d728010100280061d7190001001681168016820abff9ff85bf' + '8100887d80039abe190060d7280101001b0060d728030100060001d5050f6600060001d5060f6d00193c97beff0c0c3a' '550000001703febe001070e004060480000081bf'), } WORDS_OUT = C.WORDS diff --git a/tests/startup_gfx2hlsl_compute_spill_check.py b/tests/startup_gfx2hlsl_compute_spill_check.py index af0e71ae2d..4bf0015e59 100644 --- a/tests/startup_gfx2hlsl_compute_spill_check.py +++ b/tests/startup_gfx2hlsl_compute_spill_check.py @@ -207,7 +207,17 @@ def assemble_labels(llvm_mc, source): line = '%s %d' % (m[1], (labels[m[2]] - (position + 4)) // 4) out.append(line) position += size - return assemble(llvm_mc, '\n'.join(out) + '\n') + words = bytearray() + for line in out: + built = bytearray(assemble(llvm_mc, line + '\n')) + if re.match(r'v_(read|write)lane_b32\b|v_cmp\w*\s', line) and len(built) == 8: + # src2 of the lane operations and of the VOP3 compares is unused: LLVM 15 encodes it as 0, newer LLVM as the + # inline constant 0 (128). + # Clear it so the recorded words hold for both. + dword = int.from_bytes(built[4:8], 'little') & ~(0x1ff << 18) + built[4:8] = dword.to_bytes(4, 'little') + words += built + return bytes(words) # CASE: (source, words as hex, rsrc2, group size, group counts) @@ -218,73 +228,73 @@ def assemble_labels(llvm_mc, source): } WORDS = { 'S': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' - '0f06010204811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' - '8102027e090082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e01028002027e80038cbe' + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000100280061d70d020100280061d70e040100280061d7' + '0f06010004811787178007bf0a0084bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e0100' + '8102027e090082bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e01008002027e80038cbe' '80038dbe80038ebe80038fbe800390be800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf' - '0c0060d7280101020d0060d7280301020e0060d7280501020f0060d728070102100060d728090102110060d7280b0102' - '120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0040504801503febe000081bf'), + '0c0060d7280101000d0060d7280301000e0060d7280501000f0060d728070100100060d728090100110060d7280b0100' + '120060d7280d0100130060d7280f010002300ce000050380703f8cbf001078e0040504801503febe000081bf'), 'P': bytes.fromhex( - '06038abe80038bbe050308f4000000fa7fc08cbf290061d70a000102290061d70b02010280038abe80038bbe0783148f' - '1400064a820608347e0395be8300887d6a03febe0e0088bf0a0060d7290101020b0060d729030102050408f4100000fa' + '06038abe80038bbe050308f4000000fa7fc08cbf290061d70a000100290061d70b02010080038abe80038bbe0783148f' + '1400064a820608347e0395be8300887d6a03febe0e0088bf0a0060d7290101000b0060d729030100050408f4100000fa' '0705169316041680001030e00405038070008cbf160a0a3a001070e0040504801503febe000081bf'), 'reload-before-write': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf04811787178007bf0a0084bf280061d710080102280061d7110a0102' - '280061d7120c0102280061d7130e01028102027e090082bf280061d710080102280061d7110a0102280061d7120c0102' - '280061d7130e01028002027e80038cbe80038dbe80038ebe80038fbe800390be800391be800392be800393be84000834' - '7e0395be8c00887d6a03febe150088bf0c0060d7280101020d0060d7280301020e0060d7280501020f0060d728070102' - '100060d728090102110060d7280b0102120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0' + '06038abe80038bbe05030cf4000000fa7fc08cbf04811787178007bf0a0084bf280061d710080100280061d7110a0100' + '280061d7120c0100280061d7130e01008102027e090082bf280061d710080100280061d7110a0100280061d7120c0100' + '280061d7130e01008002027e80038cbe80038dbe80038ebe80038fbe800390be800391be800392be800393be84000834' + '7e0395be8c00887d6a03febe150088bf0c0060d7280101000d0060d7280301000e0060d7280501000f0060d728070100' + '100060d728090100110060d7280b0100120060d7280d0100130060d7280f010002300ce000050380703f8cbf001078e0' '040504801503febe000081bf'), 'two-descriptors-joined': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' - '0f06010204811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' - '8102027e090082bf280061d70c080102280061d70d0a0102280061d70e0c0102280061d70f0e01028002027e80038cbe' + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000100280061d70d020100280061d70e040100280061d7' + '0f06010004811787178007bf0a0084bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e0100' + '8102027e090082bf280061d70c080100280061d70d0a0100280061d70e0c0100280061d70f0e01008002027e80038cbe' '80038dbe80038ebe80038fbe800390be800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf' - '0c0060d7280101020d0060d7280301020e0060d7280501020f0060d728070102100060d728090102110060d7280b0102' - '120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0040504801503febe000081bf'), + '0c0060d7280101000d0060d7280301000e0060d7280501000f0060d728070100100060d728090100110060d7280b0100' + '120060d7280d0100130060d7280f010002300ce000050380703f8cbf001078e0040504801503febe000081bf'), 'modified-word': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' - '0f06010211ff1187ffff00c004811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102' - '280061d7130e01028102027e090082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000100280061d70d020100280061d70e040100280061d7' + '0f06010011ff1187ffff00c004811787178007bf0a0084bf280061d710080100280061d7110a0100280061d7120c0100' + '280061d7130e01008102027e090082bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e0100' '8002027e80038cbe80038dbe80038ebe80038fbe800390be800391be800392be800393be840008347e0395be8c00887d' - '6a03febe150088bf0c0060d7280101020d0060d7280301020e0060d7280501020f0060d728070102100060d728090102' - '110060d7280b0102120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0040504801503febe' + '6a03febe150088bf0c0060d7280101000d0060d7280301000e0060d7280501000f0060d728070100100060d728090100' + '110060d7280b0100120060d7280d0100130060d7280f010002300ce000050380703f8cbf001078e0040504801503febe' '000081bf'), 'lane-mask': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' - '0f06010204811787178007bf060084bf1400c4d48c000202280061d7140801028102027e090082bf280061d710080102' - '280061d7110a0102280061d7120c0102280061d7130e01028002027e80038cbe80038dbe80038ebe80038fbe800390be' - '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101020d0060d728030102' - '0e0060d7280501020f0060d728070102100060d728090102110060d7280b0102120060d7280d0102130060d7280f0102' + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000100280061d70d020100280061d70e040100280061d7' + '0f06010004811787178007bf060084bf1400c4d48c000200280061d7140801008102027e090082bf280061d710080100' + '280061d7110a0100280061d7120c0100280061d7130e01008002027e80038cbe80038dbe80038ebe80038fbe800390be' + '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101000d0060d728030100' + '0e0060d7280501000f0060d728070100100060d728090100110060d7280b0100120060d7280d0100130060d7280f0100' '02300ce000050380703f8cbf001078e0040504801503febe000081bf'), 'wrong-lane': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' - '0f06010204811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' - '8102027e090082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e01028002027e80038cbe' + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000100280061d70d020100280061d70e040100280061d7' + '0f06010004811787178007bf0a0084bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e0100' + '8102027e090082bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e01008002027e80038cbe' '80038dbe80038ebe80038fbe800390be800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf' - '0c0060d7280101020d0060d7280b01020e0060d7280501020f0060d728070102100060d728090102110060d7280b0102' - '120060d7280d0102130060d7280f010202300ce000050380703f8cbf001078e0040504801503febe000081bf'), + '0c0060d7280101000d0060d7280b01000e0060d7280501000f0060d728070100100060d728090100110060d7280b0100' + '120060d7280d0100130060d7280f010002300ce000050380703f8cbf001078e0040504801503febe000081bf'), 'unproven-descriptor': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf04811787178007bf120084bf280061d710080102280061d7110a0102' - '280061d7120c0102280061d7130e0102280061d70c000102280061d70d020102280061d70e040102280061d70f060102' - '8102027e110082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102280061d70c000102' - '280061d70d020102280061d70e040102280061d70f0601028002027e80038cbe80038dbe80038ebe80038fbe800390be' - '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101020d0060d728030102' - '0e0060d7280501020f0060d728070102100060d728090102110060d7280b0102120060d7280d0102130060d7280f0102' + '06038abe80038bbe05030cf4000000fa7fc08cbf04811787178007bf120084bf280061d710080100280061d7110a0100' + '280061d7120c0100280061d7130e0100280061d70c000100280061d70d020100280061d70e040100280061d70f060100' + '8102027e110082bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e0100280061d70c000100' + '280061d70d020100280061d70e040100280061d70f0601008002027e80038cbe80038dbe80038ebe80038fbe800390be' + '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101000d0060d728030100' + '0e0060d7280501000f0060d728070100100060d728090100110060d7280b0100120060d7280d0100130060d7280f0100' '02300ce000050380703f8cbf001078e0040504801503febe000081bf'), 'written-in-region-read-outside': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' - '0f06010204811787178007bf020084bf8102027e010082bf8002027e80038cbe80038dbe80038ebe80038fbe84000834' - '7e0395be8c00887d6a03febe150088bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' - '0c0060d7280101020d0060d7280301020e0060d7280501020f0060d72807010202300ce000050380703f8cbf001078e0' - '04050480100060d7280901021503febe000081bf'), + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000100280061d70d020100280061d70e040100280061d7' + '0f06010004811787178007bf020084bf8102027e010082bf8002027e80038cbe80038dbe80038ebe80038fbe84000834' + '7e0395be8c00887d6a03febe150088bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e0100' + '0c0060d7280101000d0060d7280301000e0060d7280501000f0060d72807010002300ce000050380703f8cbf001078e0' + '04050480100060d7280901001503febe000081bf'), 'loop-carried': bytes.fromhex( - '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000102280061d70d020102280061d70e040102280061d7' - '0f06010204811787178007bf0a0084bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e0102' - '8102027e090082bf280061d710080102280061d7110a0102280061d7120c0102280061d7130e01028002027e800396be' - '100060d728090102280061d7100801021681168016820abff9ff85bf80038cbe80038dbe80038ebe80038fbe800390be' - '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101020d0060d728030102' - '0e0060d7280501020f0060d728070102100060d728090102110060d7280b0102120060d7280d0102130060d7280f0102' + '06038abe80038bbe05030cf4000000fa7fc08cbf280061d70c000100280061d70d020100280061d70e040100280061d7' + '0f06010004811787178007bf0a0084bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e0100' + '8102027e090082bf280061d710080100280061d7110a0100280061d7120c0100280061d7130e01008002027e800396be' + '100060d728090100280061d7100801001681168016820abff9ff85bf80038cbe80038dbe80038ebe80038fbe800390be' + '800391be800392be800393be840008347e0395be8c00887d6a03febe150088bf0c0060d7280101000d0060d728030100' + '0e0060d7280501000f0060d728070100100060d728090100110060d7280b0100120060d7280d0100130060d7280f0100' '02300ce000050380703f8cbf001078e0040504801503febe000081bf'), }