From 546e9b22df1c55851fdb66b7ce2407c852a1b189 Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Thu, 26 Nov 2026 12:22:28 +0100 Subject: [PATCH] startup: accept the early exit of a kill in the slot 57 pixel stage (Part of #2413) A slot 57 colour pixel stage whose only null export is the early exit LLVM SILateBranchLowering and Mesa ACO place after the program's s_endpgm was refused as a mixed null export. kill_exit_block accepts that block when it is `exp null off, off, off, off done vm` then the final s_endpgm, and every branch into it enters with EXEC zero: s_cbranch_execz, or s_cbranch_scc0 directly after s_cselect exec, exec, 0 (the demote form). All other mixed forms stay refused with the existing message. Checks: startup_mesh57_depth_only_check.py (both forms plus ten near misses) and startup_mesh57_skeleton_kill_exit_{warp,hardware}, which draw both pairs against a CPU rasteriser without the killed triangles; the pair without the kill must differ at the 115 killed pixels. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 5 ++ docs/experiments.md | 27 ++++++++- tests/startup_mesh57_depth_only_check.py | 62 ++++++++++++++++++++ tests/startup_mesh57_skeleton_check.py | 72 ++++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 30 +++++++++- 5 files changed, 194 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 25c7f646c2..1e0a6ee642 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9977,6 +9977,11 @@ if(EXISTS "${INTAKE_PYTHON}") "${CMAKE_SOURCE_DIR}/tests/startup_mesh57_skeleton_check.py" depth "${DXC_EXECUTABLE}" "$" ${adapter}) set_tests_properties(startup_mesh57_skeleton_depth_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77) + # A slot 57 colour pair whose pixel stage leaves a kill through the early-exit null export (#2413). + add_test(NAME startup_mesh57_skeleton_kill_exit_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_mesh57_skeleton_check.py" killexit "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_mesh57_skeleton_kill_exit_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77) add_test(NAME startup_mesh57_formatted_load_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_mesh57_skeleton_check.py" formatted "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/docs/experiments.md b/docs/experiments.md index 9941e98f33..ae32d66181 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -10137,7 +10137,32 @@ the empty `targets` line, a control without the EXEC narrowing (no discard) and a null export followed by a position export (still refused). A slot 57 pixel stage that has the null export next to an `mrt0`, `mrtz` or `param0` export, in either order, is refused before any source or manifest is written; -the general pixel path keeps those mixed forms. The +the general pixel path keeps those mixed forms. + +One mixed form is accepted: the early exit of a kill. LLVM +`SILateBranchLowering` and Mesa ACO place it after the program's `s_endpgm`: +`exp null off, off, off, off done vm` then the final `s_endpgm`, and every +branch into the block enters with EXEC zero, so the export writes nothing +(`kill_exit_block` in `gfx2hlsl.py`). An entry is `s_cbranch_execz`, or the +demote form: `s_cbranch_scc0` directly after `s_cselect_b32 exec_lo, exec_lo, 0` +or `s_cselect_b64 exec, exec, 0`. `s_cselect` leaves SCC, so the taken branch +follows the select of 0; no branch may land on the `s_cbranch_scc0` itself. The +rest of the stage, here an `mrt0` export after the kill, is translated as usual +and keeps its `vm` discard. `startup_mesh57_depth_only_check.py` checks both +forms and refuses ten near misses with the mixed-export message: the colour +export falling through into the null export, a block no branch reaches, a block +entered by `s_branch`, an exit export without `vm`, another instruction in the +block, and for the demote form `s_cbranch_scc0` without the `s_cselect`, an +`s_cselect` that keeps EXEC, one into another register, an instruction between +the two, and `s_cbranch_scc1`. The +`startup_mesh57_skeleton_kill_exit_{warp,hardware}` CTest checks draw both +colour pairs with `offscreen_check --mesh-indexed`; lanes whose flat attribute 0 +red is at or below 0.5 are killed, so two of the four triangles are dropped. +Each draw must match the CPU rasteriser without them, and the same mesh with the +pixel stage that does not kill must differ at exactly the 115 killed pixels. +Nothing here shows that a runtime path loads the pair. + +The `startup_mesh57_skeleton_depth_{warp,hardware}` CTest checks draw a source-built depth-only pair (`v_interp_mov_f32`, `v_cmpx_lt_f32 0.5, v0`, `exp null off, off, off, off done vm`, `s_endpgm`) twice with diff --git a/tests/startup_mesh57_depth_only_check.py b/tests/startup_mesh57_depth_only_check.py index 5fc61785f0..7ca3c62ab6 100644 --- a/tests/startup_mesh57_depth_only_check.py +++ b/tests/startup_mesh57_depth_only_check.py @@ -10,6 +10,10 @@ an empty `targets` line. Controls: without the EXEC narrowing there is no discard, a null export followed by a position export is still refused by the role check, and a null export before or after an mrt0, mrtz or param0 export is refused with no source or manifest written. + +A colour stage whose null export is only the early exit of a kill (after the main s_endpgm, reached only by +s_cbranch_execz, or by s_cbranch_scc0 directly after s_cselect exec, exec, 0 in the demote form) translates with its +colour target and discard; five near misses of each form stay refused. """ import os from pathlib import Path @@ -31,6 +35,63 @@ def translate(pixel): return (result,) + tuple(f.read_text() if f.exists() else '' for f in files) +MIXES = 'unsupported: slot 57 pixel stage mixes the null export with another export' + + +def kill_exit(): + """A colour stage whose null export is only the early exit of a kill translates; near misses stay refused.""" + words = S.KILL_EXIT_PIXEL + exit_at = len(words) - 3 # the null export's first word; it and s_endpgm follow the main s_endpgm + S.expect('kill-exit words decode', [x[3] for x in S.G.decode(S.code(words))], + ['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', + 'v_cmpx_lt_f32_e32 0.5, v0', 's_cbranch_execz label_0x24', 'exp mrt0 v0, v1, v2, v3 done vm', 's_endpgm', + 'exp null off, off, off, off done vm', 's_endpgm']) + result, hlsl, manifest = translate(words) + S.expect('kill-exit colour stage translates', result, 'ok') + S.expect('kill-exit stage writes its colour', 'SV_Target' in hlsl, True) + S.expect('kill-exit stage keeps the vm discard', 'if (!(exec & 1)) discard;' in hlsl, True) + _, _, colour_manifest = translate(S.PIXEL) + S.expect('kill-exit manifest names the colour target', manifest.splitlines()[-1:], colour_manifest.splitlines()[-1:]) + branch = words.index('bf880003') + for label, mutant in ( + # The main s_endpgm removed: the colour export falls through into the null export. + ('a null export the colour export falls into', + words[:branch] + [S.branch(0xbf88, branch, exit_at - 1)] + words[branch + 1:exit_at - 1] + words[exit_at:]), + # No branch reaches the block. + ('an exit block no branch reaches', words[:branch] + ['bf800000'] + words[branch + 1:]), + # s_branch enters with lanes still alive. + ('an exit block entered by s_branch', words[:branch] + ['bf820003'] + words[branch + 1:]), + # The exit export without vm kills nothing. + ('an exit export without vm', words[:exit_at] + ['f8000890'] + words[exit_at + 1:]), + # Anything else in the block: v_mov_b32 v0, 0 before the null export. + ('an exit block with another instruction', + words[:branch] + ['bf880004'] + words[branch + 1:exit_at] + ['7e000280'] + words[exit_at:])): + S.expect(label + ' is refused', translate(mutant)[0], MIXES) + demote_exit() + + +def demote_exit(): + """The demote form enters the exit with s_cbranch_scc0 right after s_cselect exec, exec, 0; near misses stay refused.""" + words = S.DEMOTE_EXIT_PIXEL + result, hlsl, manifest = translate(words) + S.expect('demote-exit colour stage translates', result, 'ok') + S.expect('demote-exit stage writes its colour', 'SV_Target' in hlsl, True) + S.expect('demote-exit stage keeps the vm discard', 'if (!(exec & 1)) discard;' in hlsl, True) + select, branch = words.index('85fe807e'), words.index('bf840004') + for label, mutant in ( + # SCC still comes from the live mask, but EXEC is not cleared before the branch. + ('s_cbranch_scc0 without the s_cselect', words[:select] + ['bf800000'] + words[select + 1:]), + ('an s_cselect that keeps EXEC', words[:select] + ['85fec17e'] + words[select + 1:]), + ('an s_cselect into another register', words[:select] + ['858e807e'] + words[select + 1:]), + # An instruction between s_cselect and the branch (s_mov_b32 s20, 0) breaks the adjacency. + ('an instruction between s_cselect and s_cbranch_scc0', + words[:branch] + ['be940380', 'bf840004'] + words[branch + 1:]), + # s_cbranch_scc1 enters the exit when lanes are still live. + ('s_cbranch_scc1 into the exit', words[:branch] + ['bf850004'] + words[branch + 1:])): + S.expect(label + ' is refused', translate(mutant)[0], MIXES) + + def main(): os.environ['X2W_FXC'] = '' # source and manifest only; no compiler needed S.expect('alpha-test words decode', [x[3] for x in S.G.decode(S.code(ALPHA_TEST))], @@ -68,6 +129,7 @@ def main(): S.expect(label + ' leaves only the failure', sorted(p.name for p in Path(tmp).iterdir() if not p.name.startswith('req_')), ['%016x.fail' % key]) + kill_exit() if S.FAILED: return 1 print('ok') diff --git a/tests/startup_mesh57_skeleton_check.py b/tests/startup_mesh57_skeleton_check.py index 2427a283ac..ad4d0f2d5c 100644 --- a/tests/startup_mesh57_skeleton_check.py +++ b/tests/startup_mesh57_skeleton_check.py @@ -4,6 +4,7 @@ startup_mesh57_skeleton_check.py binary startup_mesh57_skeleton_check.py draw startup_mesh57_skeleton_check.py depth + startup_mesh57_skeleton_check.py killexit startup_mesh57_skeleton_check.py formatted startup_mesh57_skeleton_check.py shift startup_mesh57_skeleton_check.py exchange @@ -22,6 +23,10 @@ twice into a depth buffer cleared to the sentinel 1.0 with depth test LESS, and compares colour (left cleared) and depth with the CPU rasteriser; a reference without the depth writes, and the same mesh with a null-export stage that does not discard, must each fail. +`killexit` draws the colour pair whose pixel stage kills lanes with red at or below 0.5 and leaves through the +early-exit null export (entered by s_cbranch_execz, and in the demote form by s_cbranch_scc0 after s_cselect), +and compares both with the CPU rasteriser without the killed triangles; the same mesh with the pixel stage that +does not kill must differ at exactly the killed pixels. The exchange mode uses the runtime-published request and the same pixel comparison. """ import os @@ -59,6 +64,25 @@ # The drawn stage with exp mrt0 v0, v0, v0, v0 inserted before its null export (after the v_cmpx), then the drawn # stage reading attribute 1, which the mesh does not export. The other mixed forms (mrtz, param0, pos0, both orders, # no EXEC narrowing) are covered by startup_mesh57_depth_only_check.py. +# A colour stage with the early exit of a kill (LLVM SILateBranchLowering, Mesa ACO): when no lane is left after +# v_cmpx, s_cbranch_execz jumps past the main s_endpgm to a block that is only the null export and s_endpgm. +# Lanes whose flat attribute 0 red is not above 0.5 are killed; the others write their colour. +KILL_EXIT_PIXEL_ASM = ('v_interp_mov_f32 v0, p0, attr0.x\nv_interp_mov_f32 v1, p0, attr0.y\n' + 'v_interp_mov_f32 v2, p0, attr0.z\nv_interp_mov_f32 v3, p0, attr0.w\n' + 'v_cmpx_lt_f32_e32 0.5, v0\ns_cbranch_execz .exit\nexp mrt0 v0, v1, v2, v3 done vm\ns_endpgm\n' + '.exit:\nexp null off, off, off, off done vm\ns_endpgm\n') +KILL_EXIT_PIXEL = ('c8020002 c8060102 c80a0202 c80e0302 7c2200f0 bf880003 f800180f 03020100 bf810000' + ' f8001890 00000000 bf810000').split() +# The demote form of the same exit (LLVM SILateBranchLowering): the live mask leaves SCC, s_cselect sets EXEC to 0 +# when no lane is live, and s_cbranch_scc0 then enters the exit with EXEC zero. Same kill as KILL_EXIT_PIXEL. +DEMOTE_EXIT_PIXEL_ASM = ('v_interp_mov_f32 v0, p0, attr0.x\nv_interp_mov_f32 v1, p0, attr0.y\n' + 'v_interp_mov_f32 v2, p0, attr0.z\nv_interp_mov_f32 v3, p0, attr0.w\n' + 's_mov_b64 s[12:13], exec\nv_cmp_gt_f32_e32 vcc, 0.5, v0\ns_andn2_b64 s[12:13], s[12:13], vcc\n' + 's_cselect_b64 exec, exec, 0\ns_cbranch_scc0 .exit\ns_mov_b64 exec, s[12:13]\n' + 'exp mrt0 v0, v1, v2, v3 done vm\ns_endpgm\n' + '.exit:\nexp null off, off, off, off done vm\ns_endpgm\n') +DEMOTE_EXIT_PIXEL = ('c8020002 c8060102 c80a0202 c80e0302 be8c047e 7c0800f0 8a8c6a0c 85fe807e bf840004 befe040c' + ' f800180f 03020100 bf810000 f8001890 00000000 bf810000').split() DEPTH_PIXEL_MUTANTS = ( ('a colour write next to the drawn null export', DEPTH_PIXEL[:NULL_EXPORT] + ['f800000f', '00000000'] + DEPTH_PIXEL[NULL_EXPORT:], 'slot 57 pixel stage mixes the null export with another export'), @@ -1046,6 +1070,52 @@ def draw_depth(dxc, renderer, adapter): return 0 +def draw_kill_exit(dxc, renderer, adapter): + """The colour pair whose pixel stage leaves through the early exit of a kill, against a CPU reference without + the killed triangles; the same mesh with the pixel stage that does not kill must fail against it.""" + os.environ['X2W_DXC'] = dxc + os.environ.pop('X2W_FXC', None) + points, colours, indices = geometry() + kept = [triangle for triangle in indices if colours[triangle[0]][0] / 255 > 0.5] + image, unkilled = rasterise(points, colours, kept, 12, len(kept)), rasterise(points, colours, indices, 12, 4) + killed = sum(a != b for a, b in zip(image, unkilled)) + assert 0 < len(kept) < len(indices) and killed > 50, (len(kept), killed) + with tempfile.TemporaryDirectory() as tmp: + key, result = request(tmp, MESH, KILL_EXIT_PIXEL) + expect('the kill-exit pair translates and compiles', result, 'ok') + demote, demote_result = request(tmp, MESH, DEMOTE_EXIT_PIXEL) + expect('the demote-exit pair translates and compiles', demote_result, 'ok') + plain, plain_result = request(tmp, MESH, PIXEL) + expect('the pair without the kill translates and compiles', plain_result, 'ok') + if result != 'ok' or demote_result != 'ok' or plain_result != 'ok': + return 0 + uds = G.MeshPipeline(code(MESH), code(KILL_EXIT_PIXEL), root()).uds + constants = bytearray(1024) + struct.pack_into(' 0) + and 'pixels=1024 mismatches=%d' % mismatches in run.stdout) + print('%s: mismatches want %d, exit=%d -> %s' % (label, mismatches, run.returncode, + 'as expected' if ok else 'UNEXPECTED')) + if not ok: + print(run.stdout + run.stderr) + expect('kill-exit draw: ' + label, ok, True) + return 0 + + def draw_formatted(dxc, renderer, adapter): os.environ['X2W_DXC'] = dxc os.environ.pop('X2W_FXC', None) @@ -1151,6 +1221,8 @@ def main(argv): status = draw(*argv[2:]) elif argv[1:2] == ['depth'] and len(argv) == 5: status = draw_depth(*argv[2:]) + elif argv[1:2] == ['killexit'] and len(argv) == 5: + status = draw_kill_exit(*argv[2:]) elif argv[1:2] == ['formatted'] and len(argv) == 5: status = draw_formatted(*argv[2:]) elif argv[1:2] == ['shift'] and len(argv) == 5: diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 760b6e7289..63acaf6701 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -10993,6 +10993,33 @@ def mesh57_key(ms, ps, root): return value +def kill_exit_block(pixel): + """True when the only null export of a decoded pixel stage is the early exit of a kill. + + LLVM SILateBranchLowering and Mesa ACO place that exit after the program's s_endpgm: the block is + `exp null off, off, off, off done vm` then the final s_endpgm, and every branch into it enters with EXEC zero, + so the export writes nothing. The stage's other lanes end at the earlier s_endpgm. An entry is s_cbranch_execz, + or the demote form: s_cbranch_scc0 directly after `s_cselect exec, exec, 0` (s_cselect leaves SCC, so the taken + branch follows the select of 0) with no branch landing on the s_cbranch_scc0 itself.""" + at = [i for i, (_, _, op, a) in enumerate(pixel) if op == 'exp' and a.split()[1] == 'null'] + if len(at) != 1 or at[0] != len(pixel) - 2 or pixel[at[0] - 1][2] != 's_endpgm': + return False + if pixel[at[0]][3] != 'exp null off, off, off, off done vm': + return False + label = 'label_0x%x' % pixel[at[0]][0] + end = 'label_0x%x' % pixel[-1][0] + targets = {a.split()[-1] for _, _, op, a in pixel if op.startswith('s_cbranch') or op == 's_branch'} + entries = [i for i, (_, _, _, a) in enumerate(pixel) if a.split()[-1:] in ([label], [end])] + + def zero_exec(i): + if pixel[i][2] == 's_cbranch_execz': + return True + return (pixel[i][2] == 's_cbranch_scc0' and i > 0 + and pixel[i - 1][3] in ('s_cselect_b64 exec, exec, 0', 's_cselect_b32 exec_lo, exec_lo, 0') + and 'label_0x%x' % pixel[i][0] not in targets) + return bool(entries) and all(zero_exec(i) for i in entries) + + def translate_mesh57(out_dir, key): """Translate a fixed-descriptor mesh request of the accepted shape (mesh_skeleton), or raise Unsupported. @@ -11017,7 +11044,8 @@ def translate_mesh57(out_dir, key): if not pixel or pixel[-1][2] != 's_endpgm' or classify_decoded(pixel) != 'ps' and not null_only: raise Unsupported('slot 57 pixel stage has an incompatible export or termination') # The slot 57 depth-only profile is the null export alone; the general pixel path keeps its mixed forms. - if 'null' in targets and not null_only: + # A colour stage may also hold the null export as the early exit of a kill (kill_exit_block). + if 'null' in targets and not null_only and not kill_exit_block(pixel): raise Unsupported('slot 57 pixel stage mixes the null export with another export') pipe = MeshPipeline(ms, ps or None, root) sources = [('ms', 'ms_6_5', pipe.hlsl_ms())]