Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9981,6 +9981,11 @@ if(EXISTS "${INTAKE_PYTHON}")
"${CMAKE_SOURCE_DIR}/tests/startup_mesh57_skeleton_check.py" depth "${DXC_EXECUTABLE}"
"$<TARGET_FILE:offscreen_check>" ${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}"
"$<TARGET_FILE:offscreen_check>" ${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}"
"$<TARGET_FILE:offscreen_check>" ${adapter})
Expand Down
27 changes: 26 additions & 1 deletion docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions tests/startup_mesh57_depth_only_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))],
Expand Down Expand Up @@ -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')
Expand Down
72 changes: 72 additions & 0 deletions tests/startup_mesh57_skeleton_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
startup_mesh57_skeleton_check.py binary <llvm-mc>
startup_mesh57_skeleton_check.py draw <dxc> <offscreen_check> <warp|hardware>
startup_mesh57_skeleton_check.py depth <dxc> <offscreen_check> <warp|hardware>
startup_mesh57_skeleton_check.py killexit <dxc> <offscreen_check> <warp|hardware>
startup_mesh57_skeleton_check.py formatted <dxc> <offscreen_check> <warp|hardware>
startup_mesh57_skeleton_check.py shift <dxc> <offscreen_check> <warp|hardware>
startup_mesh57_skeleton_check.py exchange <dxc> <offscreen_check> <warp|hardware> <publisher>
Expand All @@ -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
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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('<I', constants, 4 * uds.index(('rootconst', 0, 0)), len(indices))
struct.pack_into('<I', constants, 4 * uds.index(('rootconst', 0, 1)), len(points))
words = [a | b << 10 | c << 20 for a, b, c in indices]
path = Path(tmp) / 'kill.bin'
path.write_bytes(bytes(constants) + buffer(points, colours, words) + b'\xcd' * 80
+ b''.join(bytes(pixel) for pixel in image))
print('kept %d of %d triangles; %d pixels differ from a draw without the kill' % (len(kept), len(indices), killed))
for label, stem, mismatches in (('kill-exit pair', key, 0), ('demote-exit pair', demote, 0),
('the pair without the kill', plain, killed)):
name = str(Path(tmp) / ('%016x' % stem))
run = subprocess.run([renderer, adapter, '--mesh-indexed', name + '.ms57.cso', name + '.ps57.cso', str(path)],
capture_output=True, text=True)
if run.returncode == 77:
print(run.stdout + run.stderr)
return 77
ok = (run.returncode == int(mismatches > 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)
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 29 additions & 1 deletion tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10997,6 +10997,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.

Expand All @@ -11021,7 +11048,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())]
Expand Down
Loading