diff --git a/docs/experiments.md b/docs/experiments.md index 2583c92274..98b4bd63fe 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15484,6 +15484,14 @@ ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_readfirstlane_(unifor python -B tests/startup_compute_readfirstlane_check.py --llvm-mc llvm-mc-15 ``` +The same checks exercise a [four-thread resource-index waterfall](../tests/startup_compute_waterfall.py) for #2644. +`v_readfirstlane_b32` selects a key and the following `v_cmpx_eq_i32` enables the lanes sharing that key. A saved +copy of `exec_lo`, `s_andn2_wrexec_b32` and a backward `s_cbranch_scc1` serve the remaining lanes. Each participating +lane can execute one visit with its own key when the body performs proven lane-local operations and its temporary +scalars are dead at exit. The fixtures vary the active count from zero to four, restore EXEC, and check both an +arithmetic result and a key-dependent byte offset in one fixed raw buffer. A read of the group's active mask, +a store inside the loop, a live scalar or a key-selected descriptor keeps an instruction-specific refusal. + ## Desktop audio render sink `startup_audio_output` drives a small WASAPI shared-mode render sink ([`audio_sink.h`](../src/startup_stubs/audio_sink.h)) with generated PCM blocks. It is the first slice of [issue #1145](https://github.com/R0Y00/Xbox2Windows/issues/1145). The sink takes `submit(format, frames, bytes)`, converts the block to the endpoint mix format (32-bit float or 16-bit PCM only) and records the first accepted block, the submitted format, per-block frame counts and peaks, consumed frames (submitted minus `GetCurrentPadding`), the `IAudioClock` position, the drained interval and the recovery. The fixture runs three cases in one process: no submission; eight 40 ms blocks with distinct amplitudes, one of them intentionally silent; and a producer that withholds the next block until the queue is empty, then resumes. diff --git a/tests/startup_compute_readfirstlane_check.py b/tests/startup_compute_readfirstlane_check.py index c229be9e2f..f7326e1f77 100644 --- a/tests/startup_compute_readfirstlane_check.py +++ b/tests/startup_compute_readfirstlane_check.py @@ -8,7 +8,8 @@ reads it through ``v_readfirstlane_b32`` at the join, runs a four-iteration uniform loop whose counter ``s22`` is incremented uniformly and read back via ``v_readfirstlane_b32`` both inside the loop and after loop exit, and stores one combined word per thread across two four-thread groups ``(4, 1, 1) x (2, 1, 1)`` so both arms of the branch -run on the GPU. +run on the GPU. The waterfall fixtures also exercise a lane-varying key. They restore the incoming threads +after selecting zero to four lanes, then read back per-key arithmetic and a load from one fixed raw buffer. """ import argparse from pathlib import Path @@ -23,6 +24,7 @@ sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) import disasm_rdna2 as D # noqa: E402 +import startup_compute_waterfall as waterfall from startup_gfx2hlsl_compute_check import ( # noqa: E402 CASES, G, KEY, MASK, SENTINEL, STRIDE, WORDS, patch, refused, root_blob, ) @@ -194,7 +196,8 @@ def controls(llvm_mc=None): if llvm_mc: check_binary(llvm_mc) - print('Compute readfirstlane one-path, loop-counter and refusal checks passed') + waterfall.controls(llvm_mc) + print('Compute readfirstlane one-path, loop-counter, waterfall and refusal checks passed') CONTROLS = { @@ -231,18 +234,24 @@ def dispatch(dxc, runner, adapter): print(proc.stdout, end='') if proc.returncode == 77: return 77 + diagnostics = [] + for line in proc.stderr.splitlines(): + if adapter == 'hardware' and line.startswith('XBOX2WINDOWS_ADAPTER: skipping '): + print(line, file=sys.stderr) + else: + diagnostics.append(line) if mutation: want = CONTROLS[mutation][2] - first = proc.stderr.strip().splitlines()[:1] + first = diagnostics[:1] if proc.returncode != 1 or first != [want]: raise AssertionError('control %s: expected exit 1 with %r, got exit %d %r' % (mutation, want, proc.returncode, proc.stderr)) print('control %s (%s): rejected at %s' % (mutation, adapter, first[0])) - elif proc.returncode or proc.stderr: + elif proc.returncode or diagnostics: raise AssertionError('baseline (%s): %s' % (adapter, proc.stderr or 'exit %d' % proc.returncode)) else: print('baseline (%s): all %d words match' % (adapter, WORDS)) - return 0 + return waterfall.dispatch(td, dxc, runner, adapter) def main(argv): diff --git a/tests/startup_compute_waterfall.py b/tests/startup_compute_waterfall.py new file mode 100644 index 0000000000..1646999c69 --- /dev/null +++ b/tests/startup_compute_waterfall.py @@ -0,0 +1,107 @@ +"""Source-built four-lane numeric waterfall for the compute readfirstlane fixture (#2644).""" +import struct +import subprocess + +from compute_load_format_check import check_references +from startup_gfx2hlsl_compute_check import G, MASK, SENTINEL, USER7, WORDS, assemble, refused, root_blob + + +# The input buffer contains the keys. Root constant s4 selects how many lanes enter the loop. +# Every lane writes its result after the original EXEC is restored, including the initial 13 for inactive lanes. +PREFIX = ( + ('s_mov_b32 s10, s6', '06038abe'), ('s_mov_b32 s11, 0', '80038bbe'), + ('s_load_dwordx8 s[12:19], s[10:11], 0', '05030cf4000000fa'), ('s_waitcnt lgkmcnt(0)', '7fc08cbf'), + ('v_lshlrev_b32_e32 v4, 2, v0', '82000834'), ('buffer_load_dword v5, v4, s[12:15], 0 offen', '001030e004050380'), + ('s_waitcnt vmcnt(0)', '703f8cbf'), ('v_mov_b32_e32 v7, 13', '8d020e7e'), + ('s_mov_b32 s26, exec_lo', '7e039abe'), ('v_cmpx_gt_u32_e32 s4, v0', '0400a87d'), +) +SAVE = ('s_mov_b32 s21, exec_lo', '7e0395be') +HEAD = ('v_readfirstlane_b32 s22, v5', '05052c7e') +COMPARE = ('v_cmpx_eq_i32_e32 s22, v5', '160a247d') +ADD = ('s_add_u32 s23, s22, 17', '16911780') +XOR = ('v_xor_b32_e32 v7, s23, v0', '17000e3a') +CLOSE = ('s_andn2_wrexec_b32 s21, s21', '154795be') +RESTORE = ('s_mov_b32 exec_lo, s26', '1a03febe') +STORE = ('buffer_store_dword v7, v4, s[16:19], 0 offen', '001070e004070480') +END = ('s_endpgm', '000081bf') +FIXED_LOAD = (('s_load_dwordx4 s[28:31], s[10:11], 0', '050708f4000000fa'), ('s_waitcnt lgkmcnt(0)', '7fc08cbf'), + ('v_mov_b32_e32 v8, s22', '1602107e'), ('buffer_load_dword v7, v8, s[28:31], 0 offen', '001030e008070780'), + ('s_waitcnt vmcnt(0)', '703f8cbf')) + + +def program(*, before_save=(), after_save=(), compare=COMPARE, body=(ADD, XOR), after_restore=()): + loop = (HEAD, compare, *body, CLOSE) + offset = -(sum(len(bytes.fromhex(code)) for _, code in loop) // 4 + 1) + branch = ('s_cbranch_scc1 %d' % offset, struct.pack(' 32: + return 'b32 remaining mask requires at most 32 threads per group' + copy = None + for i in reversed(range(head)): + _, _, op, asm = ins[i] + if op.startswith(('s_branch', 's_cbranch', 's_setpc', 's_endpgm', 's_movrel')): + break + if remaining in effects[i][2] or effects[i][2] & EXEC_KEYS: + if op == 's_mov_b32' and parse(asm)[1] == [remaining, 'exec_lo']: + copy = i + break + if copy is None: + return 'remaining mask is not an unchanged copy of EXEC' + if any(ins[copy][0] < target <= ins[close][0] for branch, target in jumps if branch != close): + return 'another branch enters the saved mask or loop' + # Only ordinary lane-local operations and reads of a bound raw buffer. An instruction + # that observes the other lanes sharing this key cannot use this scalarization (e.g. mbcnt of EXEC). + body_writes = {key, 'scc'} # WREXEC supplies SCC for the next iteration. + numeric_writes = {key} + descriptor_inputs, buffer_descriptors = set(), set() + reads_before_write = set() + defined = {key} + for j in range(head + 2, close - 1): + base, ops, mods = parse(ins[j][3]) + base = re.sub(r'_e(32|64)$', '', base) + if base not in ('s_add_u32', 's_mov_b32', 's_and_b32', 's_or_b32', 's_lshl_b32', 's_waitcnt', + 'v_mov_b32', 'v_xor_b32', 'v_add_nc_u32', 's_load_dwordx4', 'buffer_load_dword'): + return 'body instruction %s is not proven lane-local' % base + if mods and not (base == 'buffer_load_dword' and mods == ['offen']): + return 'body instruction %s has unproved modifiers' % base + reads, writes, may_write = effects[j] + if (reads | may_write) & (EXEC_KEYS | {'vcc', 'vcc_hi', 'm0', remaining}): + return 'body reads or changes a lane mask or special register' + if base == 's_load_dwordx4': + dst = reg_range(ops[0]) if len(ops) == 3 else None + if not dst or dst[0] != 's' or dst[2] > 105: + return 'descriptor load overlaps a special register' + descriptor_inputs.update(reads) + else: + numeric_writes.update(may_write) + if base == 'buffer_load_dword': + if len(ops) < 3: + return 'buffer load has no descriptor' + buffer_descriptors.update(scalar_tokens(ops[2])) + reads_before_write.update(reads - defined) + defined.update(writes) + body_writes.update(may_write) + if reads_before_write & body_writes: + return 'body reads loop-carried scalar state' + # The data byte offset can use the key. Loading or modifying the resource descriptor + # with the key needs a separate proof that the runtime binds the whole range. + if descriptor_inputs & body_writes or buffer_descriptors & numeric_writes: + return 'per-lane descriptor selection requires a proven bound heap range' + # The close leaves EXEC and SCC zero. The last physical pass's other scalar + # values can differ per lane in this model and must be dead on every successor. + if close + 1 < len(ins) and (body_writes - {'scc'}) & live_in[close + 1]: + return 'body scalar is live at the loop exit' + if any(name.startswith('s_movrel') for _, _, name, _ in ins): + return 'relative scalar indexing can observe a loop register' + return None + def phi_mask_ok(self, key, b, t, effects, succ, targets, index): """True when lane mask `key` ('vcc' for vcc_lo, or 'sN'), live at the join t of the s_cbranch_execz at b, is the zeroed-then-set-from-exec shape execz_refusals accepts (see its docstring): every write of it in the @@ -6119,6 +6209,13 @@ def proven_uniform_v(self, n): def op_v_readfirstlane_b32(self, ops, mods): src, dst = reg_range(ops[1]), reg_range(ops[0]) past_entry = getattr(self, 'past_entry', True) + waterfalls = getattr(self, 'waterfall_reads', {}) + if getattr(self, 'pc', None) in waterfalls and not (src and self.proven_uniform_v(src[1])): + reason = waterfalls[self.pc] + if reason: + raise Unsupported('compute readfirstlane waterfall: ' + reason) + self.set_s(dst[1], self.u(ops[1]), uniform=False) # the selected key varies between lanes + return if (mods or not dst or dst[0] != 's' or dst[1] != dst[2] or not src or src[0] != 'v' or src[1] != src[2] or not self.proven_uniform_v(src[1])): raise Unsupported('v_readfirstlane_b32 requires a proven uniform source')