diff --git a/CMakeLists.txt b/CMakeLists.txt index 8021175afc..b5c7113192 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4532,7 +4532,7 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter}) set_tests_properties(startup_gfx2hlsl_compute_mbcnt_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) foreach(control IN ITEMS swapped-axes dropped-store b-offset c-unpredicated f-peer f-broadcast f-second-fill g-swapped-offsets g-overlap - j-scalar-dropped h-inner-unpredicated k-block-entered m-else-unswitched n-unmasked o-mask-kept + j-scalar-dropped h-inner-unpredicated k-block-entered m-else-unswitched n-unmasked o-mask-kept o-subset-remask-dropped n-dropped-broadcast p-dropped-andn2 p-dropped-or p-block-entered q-block-entered q-not-all-ones s-min-strict s-borrow-flipped) add_test(NAME startup_gfx2hlsl_compute_${control}_${adapter} COMMAND "${INTAKE_PYTHON}" -B diff --git a/docs/experiments.md b/docs/experiments.md index 928518b4f8..6395302617 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15992,6 +15992,8 @@ guarded block and take the latch until the counter reaches 4. The translator rep Run `python -B tests/startup_gfx2hlsl_compute_check.py` for the host controls and add `binary llvm-mc-15` to reassemble the fixture. +`subset_after` tracks pre-branch mask-subset facts (`sM <= EXEC`) across instructions and joins into `Stage.execz_refusals`, so saving `s22 <= EXEC` (or `s_mov_b32 exec_lo, s22` before `s_cbranch_execz`) proves `s_mov_b32 exec_lo, s22` inside the skipped block (#2645 T1). On the no-exit variant (`O-loop-subset`) DXC `cs_6_0` produces `[9, 1, 2, 9, 9, 9, 9, 9]` while `LOOP_BREAK_WORDS` still refuses `+0x30` with `loop exit crosses a backward s_cbranch_scc0`, and pre-branch narrowing (`s_and_saveexec_b32`, `v_cmpx_gt_u32`), mask overwrites, and unproven joins remain refused as `exec write`. + ### Scalar run before the EXEC restore at a compute exit target (#2645 T3) The 104-byte gfx1013 fixture `EXIT_RUN_*` in `tests/startup_gfx2hlsl_compute_check.py` narrows diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index fbd2436afd..928589a0c9 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -960,6 +960,9 @@ def expected(name): for i in range(16): out[i] = input_word(i // 8) return out + if name == 'O-loop-subset': + out[:8] = [9, 1, 2, 9, 9, 9, 9, 9] + return out if name == 'O-dword': for group in range(4): out[2 + group] = KEY ^ group @@ -1116,6 +1119,8 @@ def patch(words, old, new): words = patch(WORDS_G, '100034d804050000', '000034d804050000') words = patch(words, '0524dcd806000006', offset_bytes + 'dcd806000006') CASES[f'G-{field}-only'] = (source, words, *CASES['G'][2:]) +CASES['O-loop-subset'] = (LOOP_BREAK_SOURCE.replace('s_cbranch_scc0 6\n', 's_nop 0\n', 1), + patch(LOOP_BREAK_WORDS, '060084bf', '000080bf'), USER7 | TGID_X | tidig(1), (8, 1, 1), (1, 1, 1)) MBCNT_REFUSALS = ( @@ -1249,6 +1254,130 @@ def swap(at, new, code=EXIT_RUN_WORDS): assert any(reason in text for text, _ in refusals), refusals +def check_execz_mask_subset(): + """Pre-branch EXEC subset tracking (#2645 T1): sM <= EXEC carried into an execz block lets remasks preserve skipped bits.""" + # With the early exit branch replaced by s_nop, the counted loop still exercises s_mov_b32 exec_lo, s22 before + # s_cbranch_execz and s_andn2_b32 + s_mov_b32 exec_lo, s22 inside the skipped block. + def trace_loop(lanes, narrow_before=False): + outer = {lane for lane in lanes} + if narrow_before: + # Saving s22 before narrowing EXEC makes s22 a superset of EXEC at s_cbranch_execz, not a subset. + saved = set(outer) + exec_mask = {lane for lane in outer if lane < 1} + else: + saved = {lane for lane in outer if lane < 3} + exec_mask = set(saved) + values = dict.fromkeys(lanes, 9) + for counter in range(1, 4): + if exec_mask: + breaking = {lane for lane in exec_mask if counter > lane} + saved -= breaking + exec_mask = set(saved) # s_mov_b32 exec_lo, saved + values.update(dict.fromkeys(exec_mask, counter)) + exec_mask = set(saved) + return values + + lanes = range(8) + wave = trace_loop(lanes) + singles = [trace_loop([lane]) for lane in lanes] + assert list(wave.values()) == [s[lane] for lane, s in enumerate(singles)] == [9, 1, 2, 9, 9, 9, 9, 9] + # Near-miss wave-vs-singleton disagreement when the saved mask holds skipped lanes: lane 1 skips the block in the + # singleton model (9) while the wave enters via lane 0 and re-enables lane 1 through the stale saved mask (1). + bad_wave = trace_loop(lanes, narrow_before=True) + bad_singles = [trace_loop([lane], narrow_before=True) for lane in lanes] + assert bad_wave[1] == 1 and bad_singles[1][1] == 9 + + hlsl = translate('O-loop-subset').hlsl_cs() + assert 'exec = (s22) & 1;\n if (exec & 1) v5 = s24;' in hlsl, hlsl + # LOOP_BREAK_WORDS drops the T1 exec write refusal while keeping T2 (loop exit crosses a backward s_cbranch_scc0). + st = object.__new__(G.Stage) + st.kind = 'cs' + st.ins = G.decode(LOOP_BREAK_WORDS) + st.calculated_pc = {} + assert st.execz_refusals() == { + 0x30: 's_cbranch_scc0 inside a block s_cbranch_execz skips per lane: loop exit crosses a backward s_cbranch_scc0' + } + + # Build straight-line execz checks from llvm-mc-15 verified instructions in SPLICE / LOOP_BREAK_WORDS. + W = dict(SPLICE) + W.update({ + 's_nop 0': '000080bf', + 's_and_b32 s22, exec_lo, vcc_lo': '7e6a1687', + 's_mov_b32 s23, s22': '160397be', + 's_mov_b32 s24, 1': '810398be', + 'v_cmpx_gt_u32 s24, v0': '1800a87d', + 's_andn2_b32 s22, s22, exec_lo': '167e168a', + 's_andn2_b32 s23, s23, exec_lo': '177e178a', + 's_mov_b32 exec_lo, s22': '1603febe', + 's_mov_b32 exec_lo, s23': '1703febe', + 'v_mov_b32 v5, s24': '18020a7e', + 's_and_b32 exec_lo, exec_lo, vcc_lo': '7e6a7e87', + 's_mov_b32 s23, 0': '800397be', + 's_mov_b32 vcc_lo, -1': 'c103eabe', + }) + end = bytes.fromhex('000081bf') + def seq(*lines): + return WORDS_A[:STORE_A] + b''.join(bytes.fromhex(W[line]) for line in lines) + end + + # Accepted pre-branch subset setup without re-assigning EXEC directly before s_cbranch_execz. + pre_and_copy = seq( + 's_mov_b32 s21, exec_lo', 'v_cmp_gt_u32 vcc_lo, 3, v0', + 's_and_b32 s22, exec_lo, vcc_lo', 's_mov_b32 s23, s22', 's_mov_b32 s24, 1', + 's_cbranch_execz 4', 'v_cmpx_gt_u32 s24, v0', 's_andn2_b32 s23, s23, exec_lo', + 's_mov_b32 exec_lo, s23', 'v_mov_b32 v5, s24', 's_mov_b32 exec_lo, s21', + 'buffer_store_dword v5, v4, s[16:19], 0 offen', + ) + assert 'exec = (s23) & 1;' in translate('A', code=pre_and_copy).hlsl_cs() + pre_mov = seq( + 's_mov_b32 s21, exec_lo', 's_cbranch_execz 3', 's_mov_b32 exec_lo, s21', + 'v_mov_b32 v5, s24', 's_nop 0', 's_mov_b32 exec_lo, s21', + 'buffer_store_dword v5, v4, s[16:19], 0 offen', + ) + assert 'exec = (s21) & 1;' in translate('A', code=pre_mov).hlsl_cs() + + # Near-miss controls: each violates subset provenance and must stay refused as an exec write. + narrowed_saveexec = seq( + 's_mov_b32 s21, exec_lo', 'v_cmp_gt_u32 vcc_lo, 3, v0', 's_and_saveexec_b32 s0, vcc_lo', + 's_cbranch_execz 3', 's_mov_b32 exec_lo, s21', 'v_mov_b32 v5, s24', 's_nop 0', + 's_or_b32 exec_lo, exec_lo, s0', 'buffer_store_dword v5, v4, s[16:19], 0 offen', + ) + narrowed_cmpx = seq( + 's_mov_b32 s24, 1', 's_mov_b32 s21, exec_lo', 'v_cmpx_gt_u32 s24, v0', + 's_cbranch_execz 3', 's_mov_b32 exec_lo, s21', 's_nop 0', 's_nop 0', + 's_mov_b32 exec_lo, s21', + ) + overwritten_before = seq( + 's_mov_b32 s21, exec_lo', 's_mov_b32 exec_lo, vcc_lo', 's_mov_b32 vcc_lo, -1', + 's_cbranch_execz 3', 's_mov_b32 exec_lo, vcc_lo', 's_nop 0', 's_nop 0', + 's_mov_b32 exec_lo, s21', + ) + overwritten_inside = seq( + 's_mov_b32 s21, exec_lo', 's_mov_b32 exec_lo, vcc_lo', + 's_cbranch_execz 3', 's_mov_b32 vcc_lo, -1', 's_mov_b32 exec_lo, vcc_lo', 's_nop 0', + 's_mov_b32 exec_lo, s21', + ) + join_one_arm = seq( + 's_mov_b32 s21, exec_lo', 's_cbranch_scc1 2', 's_mov_b32 exec_lo, vcc_lo', 's_branch 2', + 's_mov_b32 vcc_lo, -1', 's_nop 0', + 's_cbranch_execz 3', 's_mov_b32 exec_lo, vcc_lo', 's_nop 0', 's_nop 0', + 's_mov_b32 exec_lo, s21', + ) + for near_miss in (narrowed_saveexec, narrowed_cmpx, overwritten_before, overwritten_inside, join_one_arm): + refused('s_mov_b32 inside a block s_cbranch_execz skips per lane: exec write', + lambda code=near_miss: translate('A', code=code).hlsl_cs()) + + # Unit checks for 64-bit moves and non-narrowing/invalidating steps in subset_after(). + assert G.subset_after('s_mov_b64', 's_mov_b64 exec, s[22:23]', {'exec', 'exec_hi', 'scc'}, G.EXEC_SUBSET) == ( + G.EXEC_SUBSET | {('s22', 0), ('s23', 1)}) + both = G.subset_after('s_mov_b64', 's_mov_b64 s[22:23], exec', {'s22', 's23', 'scc'}, G.EXEC_SUBSET) + assert both == (G.EXEC_SUBSET | {('s22', 0), ('s23', 1)}), both + assert G.subset_after('v_cmpx_gt_u32_e32', 'v_cmpx_gt_u32_e32 s24, v0', G.EXEC_KEYS, both) == G.EXEC_SUBSET + assert G.subset_after('s_movreld_b32', 's_movreld_b32 s0, s1', {'m0'}, both) == G.EXEC_SUBSET + assert G.subset_after('s_or_saveexec_b32', 's_or_saveexec_b32 s21, vcc_lo', + {'s21', 'exec', 'exec_hi', 'scc'}, {('exec', 0), ('exec_hi', 1), ('s22', 0)}) == ( + {('exec', 0), ('exec_hi', 1), ('s21', 0), ('s22', 0)}) + + def check_mbcnt(): for _, words, reason in MBCNT_REFUSALS: refused(reason, lambda: translate('D', code=bytes.fromhex(words + '000081bf'))) @@ -1802,6 +1931,7 @@ def check_controls(): check_mbcnt() check_execz_exit_run() check_execz_loop_break() + check_execz_mask_subset() check_lane_mask_data() check_lane_mask_high_word() check_lane_mask_compare_pair() @@ -2443,6 +2573,8 @@ def check_binary(llvm_mc): 'm-else-unswitched': ('M', 'exec = (s0 & ~exec) & 1;', 'exec = (s0) & 1;'), # the then lanes run the else arm 'n-unmasked': ('NM', 's23 = (vcc & exec) & 1;', 's23 = (vcc) & 1;'), # lanes 6 and 7 store with their stale vcc 'o-mask-kept': ('O', 's0 = (s0 & ~exec) & 1;', 's0 = (s0) & 1;'), # lanes 0-2 keep their bit and store + 'o-subset-remask-dropped': ('O-loop-subset', 'exec = (s22) & 1;\n if (exec & 1) v5 = s24;', + 'exec = 1u;\n if (exec & 1) v5 = s24;'), # broken lanes overwrite v5 'k-block-entered': ('K', 'pc = ((exec & 1) == 0) ? 2 : 1;', 'pc = 1; exec = 1;'), # skipped waves run it 'p-dropped-andn2': ('P', 's23 = (s23 & ~exec) & 1;', 's23 = (s23) & 1;'), # an entering lane keeps its old bit 'p-dropped-or': ('P', 's23 = (s23 | s22) & 1;', 's23 = (s23) & 1;'), # an entering lane loses its compare bit diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index bedd84bb45..309a84e793 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -790,6 +790,41 @@ def clear_after(name, asm, may, clear): return out | {(word, h) for word, held in writes for h in held} +EXEC_SUBSET = frozenset({('exec', 0), ('exec_hi', 1)}) + + +def subset_after(name, asm, may, clear): + """Forward step before an s_cbranch_execz: scalar words known to have bit 0 wherever current EXEC has bit 0. + + When EXEC is unchanged, bitwise mask instructions propagate those subset facts via clear_after and non-mask + writes drop them. When EXEC is written, facts on the written wave half are dropped unless a move assigns EXEC + from a scalar mask word (so that word equals the new EXEC) or an OR widens EXEC with an existing bitmask. + Indirect scalar writes (s_movrel*) drop all non-EXEC facts.""" + base = re.sub(r'_e(32|64)$', '', name) + if base.startswith('s_movrel'): + return EXEC_SUBSET + if not (may & EXEC_KEYS): + return frozenset(clear_after(name, asm, may, clear)) | EXEC_SUBSET + kept = ({0} if 'exec' not in may else set()) | ({1} if 'exec_hi' not in may else set()) + shape = clear_shape(name, asm) + if shape and shape != 'cmpx': + op, saving, count, words = shape + if op == 'or' and (saving == '_saveexec' or any( + all(src[p] == ('exec', 'exec_hi')[p] for p in range(count)) for src in words[1:])): + return frozenset(clear_after(name, asm, may, clear)) | EXEC_SUBSET + out = {fact for fact in clear if fact[1] in kept and fact[0] not in may - EXEC_KEYS} + if op == 'mov' and saving is None: + dest, src = words + for p, dw in enumerate(dest): + if dw in EXEC_KEYS: + h = 0 if dw == 'exec' else 1 + sw = src[p] + if re.fullmatch(r's\d+|vcc(_hi)?', sw): + out.add((sw, h)) + return frozenset(out) | EXEC_SUBSET + return frozenset(fact for fact in clear if fact[1] in kept and fact[0] not in may - EXEC_KEYS) | EXEC_SUBSET + + def mask_lanes(name, asm): """(operation, destination words, source word lists) of a bitwise scalar mask instruction, or None. Word p of every operand holds the same lanes: wave half p of a 64-bit form.""" @@ -3117,6 +3152,18 @@ def execz_refusals(self): and self.ins[head][2] == 'v_readfirstlane_b32' and self.ins[close - 1][2] == 's_andn2_wrexec_b32'): self.waterfall_reads[target] = self.waterfall_refusal(head, close, effects, live_in, jumps) + pre_sub = [EXEC_SUBSET] + [None] * (len(self.ins) - 1) + changed = bool(self.ins) + while changed: + changed = False + for i in range(len(self.ins)): + if pre_sub[i] is None: + continue + after = subset_after(*self.ins[i][2:], effects[i][2], pre_sub[i]) + for s in succ[i]: + if pre_sub[s] is None or not pre_sub[s] <= after: + pre_sub[s] = after if pre_sub[s] is None else (pre_sub[s] & after) + changed = True refused, into = {}, {} for k, target in jumps: into.setdefault(index.get(target), []).append(k) @@ -3129,7 +3176,7 @@ def execz_refusals(self): continue live = live_in[t] | {'m0'} # m0 is also read implicitly; treat it as live # Facts before each instruction the wave can reach from the branch: the largest sets every path keeps. - entry = frozenset({('exec', 0), ('exec_hi', 1)}) + entry = pre_sub[b] or EXEC_SUBSET before = {b + 1: entry} if b + 1 < t else {} entered = any(index.get(target) in range(b + 1, t) for k, target in jumps if not b < k < t) changed = not entered @@ -3137,7 +3184,7 @@ def execz_refusals(self): changed = False for i in range(b + 1, t): if i in before: - after = frozenset(clear_after(*self.ins[i][2:], effects[i][2], before[i])) | entry + after = frozenset(clear_after(*self.ins[i][2:], effects[i][2], before[i])) | EXEC_SUBSET for s in succ[i]: if b < s < t and before.get(s, after) & after != before.get(s): before[s], changed = before.get(s, after) & after, True @@ -3177,7 +3224,7 @@ def quiet(j): # exec unchanged since the join on every path to j: a straight ru self.execz_scalar_exit(b, i, t, exit_index, effects, live_in, into)): reason = 'branch out of the block' elif may & EXEC_KEYS and (entered or i in before and - not entry <= clear_after(inner, inner_asm, may, before[i])): + not EXEC_SUBSET <= clear_after(inner, inner_asm, may, before[i])): reason = 'exec write' elif words := [word for word in sorted((may - EXEC_KEYS - ok_masks) & live) if self.execz_live_read(t, word, kept[word], effects, succ, quiet, unread) is not None]: