mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 23:08:36 +00:00
1342 lines
53 KiB
Python
1342 lines
53 KiB
Python
"""Exact CFG/dataflow optimizations for --wave64-emulate (Issue #884).
|
|
|
|
Two conservative, bit-exact analyses over a compute/mesh shader's basic blocks:
|
|
|
|
1. `scc_live_outs(blocks, succ)` -> set of `id(ins)` whose SCC write can reach
|
|
any SCC read before being unconditionally overwritten. Under --wave64-emulate,
|
|
mask ops (`s_and_saveexec_b64`, `s_andn2_b64`, `s_or_b64`, `s_not_b64`,
|
|
`s_wqm_b64`, ...) otherwise pay a full cross-lane `x_round` + `x_split`
|
|
(workgroup barrier + OpSwitch case) to compute SCC even when SCC is dead.
|
|
|
|
2. `find_local_exec_folds(blocks, raw_edges, succ, lane_masks, scc_live)` ->
|
|
`(folds, fold_chain, absorbed)`: identifies single-entry, single-exit
|
|
regions guarded by `s_cbranch_execz` / `s_cbranch_execnz` whose body has:
|
|
- no cross-lane operations, no `s_barrier`, no live-SCC mask votes,
|
|
- no memory or LDS writes/atomics and no unmasked side effects,
|
|
- no EXEC-widening writes (an inactive lane stays inactive across the region),
|
|
- no scalar (`SGPR`, `VCC`, `SCC`, `M0`) written in the region that is live
|
|
at the join block (`skip`),
|
|
- reducible internal control flow (`cfg_structure.structure` succeeds).
|
|
|
|
Each such region can be emitted inline as a structured `if (%active) { R }`
|
|
inside its guarding block with zero `x_round` / `x_split` / `OpControlBarrier`,
|
|
keeping all 64 lanes of the wave in the exact same scheduler round on arrival
|
|
at `skip`. When `skip` has no predecessors outside `{B} | R`, `skip` is also
|
|
chained inline after the fold, collapsing chains of thousands of unrolled
|
|
`s_and_saveexec_b64; s_cbranch_execz` diamonds into a single scheduler block.
|
|
|
|
Standard library only.
|
|
"""
|
|
import collections
|
|
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
|
|
import cfg_structure # noqa: E402
|
|
import lane_masks as _lane_masks # noqa: E402
|
|
|
|
BRANCHES = frozenset({
|
|
"s_branch", "s_cbranch_scc0", "s_cbranch_scc1",
|
|
"s_cbranch_vccz", "s_cbranch_vccnz",
|
|
"s_cbranch_execz", "s_cbranch_execnz",
|
|
})
|
|
|
|
# Instructions that implicitly read SCC (in addition to explicit `scc` operands).
|
|
_SCC_IMPLICIT_READS = frozenset({
|
|
"s_cbranch_scc0", "s_cbranch_scc1",
|
|
"s_cselect_b32", "s_cselect_b64",
|
|
"s_cmov_b32", "s_cmov_b64", "s_cmovk_i32",
|
|
"s_addc_u32", "s_subb_u32",
|
|
})
|
|
|
|
# Instructions verified in tools/rdna2spirv.py to unconditionally overwrite %scc.
|
|
# Conservatively omitting an op only keeps SCC live across it (never unsafe).
|
|
_SAVEEXEC_KINDS = ("and", "or", "xor", "andn1", "andn2", "orn1", "orn2", "nand", "nor", "xnor")
|
|
_ALL_SAVEEXEC_WREXEC = frozenset(
|
|
[f"s_{k}_saveexec_b{w}" for k in _SAVEEXEC_KINDS for w in (32, 64)]
|
|
+ [f"s_{k}_wrexec_b{w}" for k in ("andn1", "andn2") for w in (32, 64)]
|
|
)
|
|
|
|
_SCC_UNCONDITIONAL_WRITES = _ALL_SAVEEXEC_WREXEC | frozenset({
|
|
"s_and_b64", "s_or_b64", "s_xor_b64", "s_andn2_b64", "s_orn2_b64",
|
|
"s_nand_b64", "s_nor_b64", "s_xnor_b64", "s_not_b64",
|
|
"s_and_b32", "s_or_b32", "s_xor_b32", "s_andn2_b32", "s_orn2_b32",
|
|
"s_nand_b32", "s_nor_b32", "s_xnor_b32", "s_not_b32",
|
|
"s_and_b32_mask", "s_or_b32_mask",
|
|
"s_add_u32", "s_addc_u32", "s_sub_u32", "s_subb_u32",
|
|
"s_add_i32", "s_sub_i32", "s_addk_i32",
|
|
"s_lshl1_add_u32", "s_lshl2_add_u32", "s_lshl3_add_u32", "s_lshl4_add_u32",
|
|
"s_min_u32", "s_max_u32", "s_min_i32", "s_max_i32",
|
|
"s_abs_i32", "s_absdiff_i32",
|
|
"s_lshl_b32", "s_lshr_b32", "s_ashr_i32", "s_lshl_b64",
|
|
"s_bfe_u32", "s_bfe_i32", "s_bfe_u64", "s_bfe_i64",
|
|
"s_bcnt0_i32_b32", "s_bcnt1_i32_b32", "s_bcnt0_i32_b64", "s_bcnt1_i32_b64",
|
|
"s_wqm_b32", "s_wqm_b64", "s_quadmask_b32", "s_quadmask_b64",
|
|
"s_cmp_eq_u32", "s_cmp_lg_u32", "s_cmp_gt_u32", "s_cmp_ge_u32", "s_cmp_lt_u32", "s_cmp_le_u32",
|
|
"s_cmp_eq_i32", "s_cmp_lg_i32", "s_cmp_gt_i32", "s_cmp_ge_i32", "s_cmp_lt_i32", "s_cmp_le_i32",
|
|
"s_cmp_eq_u64", "s_cmp_lg_u64",
|
|
"s_bitcmp0_b32", "s_bitcmp1_b32", "s_bitcmp0_b64", "s_bitcmp1_b64",
|
|
})
|
|
|
|
# Mask ops that emit a cross-lane vote in --wave64-emulate when SCC is live.
|
|
_MASK_SCC_VOTE_OPS = _ALL_SAVEEXEC_WREXEC | frozenset({
|
|
"s_and_b64", "s_or_b64", "s_xor_b64", "s_andn2_b64", "s_orn2_b64",
|
|
"s_nand_b64", "s_nor_b64", "s_xnor_b64", "s_not_b64",
|
|
"s_and_b32_mask", "s_or_b32_mask",
|
|
"s_wqm_b32", "s_wqm_b64",
|
|
})
|
|
|
|
# Scalar register bit positions for fast integer-bitmask backward liveness.
|
|
_BIT_VCC = 128
|
|
_BIT_SCC = 129
|
|
_BIT_M0 = 130
|
|
_ALL_SGPRS = (1 << 128) - 1
|
|
|
|
_IMPLICIT_VCC_READS = frozenset({
|
|
"v_cndmask_b32", "v_addc_co_u32", "v_subb_co_u32", "v_subbrev_co_u32",
|
|
"v_add_co_ci_u32", "v_sub_co_ci_u32", "v_subrev_co_ci_u32",
|
|
"v_addc_u32", "v_subb_u32", "v_div_fmas_f32", "v_div_fmas_f64",
|
|
"s_cbranch_vccz", "s_cbranch_vccnz",
|
|
})
|
|
|
|
_NO_DEST_PREFIXES = (
|
|
"s_cmp", "s_bitcmp", "v_cmpx", "exp",
|
|
"buffer_store", "global_store", "flat_store", "scratch_store", "tbuffer_store", "image_store",
|
|
"ds_write", "ds_store", "s_store", "s_buffer_store",
|
|
"s_cbranch", "s_branch", "s_waitcnt", "s_nop", "s_endpgm", "s_barrier", "s_sendmsg",
|
|
"s_setprio", "s_inst_prefetch", "s_clause", "s_trap", "s_icache", "s_dcache", "s_sleep",
|
|
"s_code_end", "s_denorm_mode", "s_round_mode", "s_ttracedata", "v_nop", "v_clrexcp",
|
|
"v_pipeflush", "s_set", "s_sethalt", "s_setkill", "s_wakeup", "s_version",
|
|
"buffer_gl", "buffer_wbinvl", "ds_gws", "ds_nop",
|
|
)
|
|
|
|
|
|
def base_op(op):
|
|
for suffix in ("_e32", "_e64", "_sdwa", "_dpp"):
|
|
if op.endswith(suffix):
|
|
return op[:-len(suffix)]
|
|
return op
|
|
|
|
|
|
def _active_insns(blk):
|
|
"""Instructions of a basic block up to and including its terminator (branch or s_endpgm)."""
|
|
out = []
|
|
for ins in blk:
|
|
out.append(ins)
|
|
op = base_op(ins["op"])
|
|
if op == "s_endpgm" or op in BRANCHES:
|
|
break
|
|
return out
|
|
|
|
|
|
def reads_scc(ins):
|
|
op = base_op(ins["op"])
|
|
if op in _SCC_IMPLICIT_READS or op.startswith(("s_getreg", "s_setreg", "s_save_status")):
|
|
return True
|
|
for x in ins["args"]:
|
|
if x.get("k") == "special" and x.get("t") == "scc":
|
|
return True
|
|
return False
|
|
|
|
|
|
def writes_scc(ins):
|
|
return base_op(ins["op"]) in _SCC_UNCONDITIONAL_WRITES
|
|
|
|
|
|
def scc_live_outs(blocks, succ):
|
|
"""Return the set of `id(ins)` whose SCC result may be read before being unconditionally overwritten."""
|
|
n = len(blocks)
|
|
if not n:
|
|
return set()
|
|
preds = [[] for _ in range(n)]
|
|
for b in range(n):
|
|
for s in succ[b]:
|
|
preds[s].append(b)
|
|
use = [False] * n
|
|
kill = [False] * n
|
|
active = [_active_insns(blk) for blk in blocks]
|
|
for b, insns in enumerate(active):
|
|
k = False
|
|
u = False
|
|
for ins in insns:
|
|
if not k and reads_scc(ins):
|
|
u = True
|
|
break
|
|
if writes_scc(ins):
|
|
k = True
|
|
use[b] = u
|
|
kill[b] = k
|
|
live_in = list(use)
|
|
live_out = [False] * n
|
|
work = [b for b in range(n) if live_in[b]]
|
|
while work:
|
|
b = work.pop()
|
|
for p in preds[b]:
|
|
if not live_out[p]:
|
|
live_out[p] = True
|
|
if not kill[p] and not live_in[p]:
|
|
live_in[p] = True
|
|
work.append(p)
|
|
out = set()
|
|
for b, insns in enumerate(active):
|
|
live = live_out[b]
|
|
for ins in reversed(insns):
|
|
if live:
|
|
out.add(id(ins))
|
|
live = (live and not writes_scc(ins)) or reads_scc(ins)
|
|
return out
|
|
|
|
|
|
def _operand_scalar_mask(x):
|
|
"""Bitmask of scalar registers (s0..s127, vcc, scc, m0) named by operand `x`."""
|
|
k = x.get("k")
|
|
if k == "sgpr":
|
|
r = x["r"]
|
|
cnt = x.get("n", 1)
|
|
m = 0
|
|
for i in range(cnt):
|
|
if 0 <= r + i < 128:
|
|
m |= 1 << (r + i)
|
|
return m
|
|
if k == "special":
|
|
t = x.get("t", "")
|
|
if t.startswith("vcc"):
|
|
return 1 << _BIT_VCC
|
|
if t == "scc":
|
|
return 1 << _BIT_SCC
|
|
if t == "m0":
|
|
return 1 << _BIT_M0
|
|
return 0
|
|
|
|
|
|
def _scalar_rw(ins):
|
|
"""Return `(read_mask, may_write_mask, must_kill_mask)` over scalar bits 0..130 for `ins`."""
|
|
op = base_op(ins["op"])
|
|
a = ins["args"]
|
|
mods = ins.get("mods") or ()
|
|
# Dynamic SGPR indexing reads/writes arbitrary SGPRs + m0: never kill, conservatively read/write all SGPRs.
|
|
if op.startswith("s_movrel"):
|
|
return _ALL_SGPRS | (1 << _BIT_M0), _ALL_SGPRS, 0
|
|
if op == "s_endpgm" or op in BRANCHES:
|
|
rd = 0
|
|
if op in ("s_cbranch_scc0", "s_cbranch_scc1"):
|
|
rd |= 1 << _BIT_SCC
|
|
elif op in ("s_cbranch_vccz", "s_cbranch_vccnz"):
|
|
rd |= 1 << _BIT_VCC
|
|
return rd, 0, 0
|
|
# Split destination vs source operands conservatively.
|
|
dests, srcs = [], list(a)
|
|
if op.startswith(("s_cmp", "s_bitcmp")) or op.startswith("v_cmpx") or op.startswith(_NO_DEST_PREFIXES):
|
|
dests, srcs = [], list(a)
|
|
elif op.startswith("v_cmp"):
|
|
if len(a) == 2:
|
|
dests, srcs = [], list(a)
|
|
elif a:
|
|
dests, srcs = [a[0]], list(a[1:])
|
|
elif op.startswith(("buffer_atomic", "global_atomic", "flat_atomic", "image_atomic")):
|
|
dests = [a[0]] if ("glc" in mods and a) else []
|
|
srcs = list(a)
|
|
elif op.startswith("ds_"):
|
|
returns = (op.startswith(("ds_read", "ds_bpermute", "ds_permute", "ds_swizzle", "ds_consume", "ds_append"))
|
|
or "_rtn" in op)
|
|
dests, srcs = ([a[0]], list(a[1:])) if (returns and a) else ([], list(a))
|
|
elif op in ("s_addk_i32", "s_mulk_i32", "s_cmovk_i32", "s_movk_i32") and a:
|
|
# SOPK reads and writes a[0] (except s_movk_i32 which only writes a[0]).
|
|
dests = [a[0]]
|
|
srcs = list(a[1:]) if op == "s_movk_i32" else list(a)
|
|
elif a:
|
|
dests, srcs = [a[0]], list(a[1:])
|
|
if op.startswith("v_") and len(a) > 2 and (
|
|
"_co_" in op or op in ("v_mad_u64_u32", "v_mad_i64_i32", "v_div_scale_f32", "v_div_scale_f64")) \
|
|
and a[1].get("k") in ("sgpr", "special"):
|
|
dests, srcs = [a[0], a[1]], list(a[2:])
|
|
|
|
rd = 0
|
|
for x in srcs:
|
|
rd |= _operand_scalar_mask(x)
|
|
if op in _IMPLICIT_VCC_READS:
|
|
rd |= 1 << _BIT_VCC
|
|
if reads_scc(ins):
|
|
rd |= 1 << _BIT_SCC
|
|
if op.startswith(("ds_", "s_sendmsg", "v_interp", "v_movrel", "v_swaprel")):
|
|
rd |= 1 << _BIT_M0
|
|
if op == "s_cmov_b32" or op == "s_cmov_b64":
|
|
# Conditional move preserves destination when SCC == 0, so it also reads destination.
|
|
for d in dests:
|
|
rd |= _operand_scalar_mask(d)
|
|
|
|
may_w = 0
|
|
for d in dests:
|
|
may_w |= _operand_scalar_mask(d)
|
|
if op.startswith("v_cmp") and not op.startswith("v_cmpx") and len(a) == 2:
|
|
may_w |= 1 << _BIT_VCC
|
|
if writes_scc(ins):
|
|
may_w |= 1 << _BIT_SCC
|
|
elif op.startswith("s_") and not op.startswith(("s_mov", "s_cselect", "s_load", "s_buffer_load", "s_getpc",
|
|
"s_nop", "s_waitcnt", "s_clause", "s_inst_prefetch", "s_pack",
|
|
"s_mul_i32", "s_mul_hi", "s_mulk", "s_ff", "s_flbit", "s_bfm",
|
|
"s_get_waveid")):
|
|
# Any uncategorized scalar op could touch SCC: include in may_w (never in must_kill).
|
|
may_w |= 1 << _BIT_SCC
|
|
|
|
# Only kill registers unconditionally overwritten (never conditional moves or half-VCC writes).
|
|
must_kill = 0
|
|
if op not in ("s_cmov_b32", "s_cmov_b64", "s_cmovk_i32"):
|
|
for d in dests:
|
|
if d.get("k") == "special" and d.get("t") in ("vcc_lo", "vcc_hi"):
|
|
continue # half-write preserves the other half in wave64
|
|
must_kill |= _operand_scalar_mask(d)
|
|
if op.startswith("v_cmp") and not op.startswith("v_cmpx") and len(a) == 2:
|
|
must_kill |= 1 << _BIT_VCC
|
|
if writes_scc(ins):
|
|
must_kill |= 1 << _BIT_SCC
|
|
return rd, may_w, must_kill
|
|
|
|
|
|
def _scalar_liveness(active_blocks, succ):
|
|
"""Exact conservative backward scalar liveness `live_in[b]` (int bitmask) for each block."""
|
|
n = len(active_blocks)
|
|
preds = [[] for _ in range(n)]
|
|
for b in range(n):
|
|
for s in succ[b]:
|
|
preds[s].append(b)
|
|
use = [0] * n
|
|
kill = [0] * n
|
|
for b, insns in enumerate(active_blocks):
|
|
u = 0
|
|
k = 0
|
|
for ins in insns:
|
|
rd, _, mk = _scalar_rw(ins)
|
|
u |= rd & ~k
|
|
k |= mk
|
|
use[b] = u
|
|
kill[b] = k
|
|
live_in = list(use)
|
|
live_out = [0] * n
|
|
in_work = [bool(u) for u in live_in]
|
|
work = collections.deque(b for b in range(n) if in_work[b])
|
|
while work:
|
|
b = work.popleft()
|
|
in_work[b] = False
|
|
bi = live_in[b]
|
|
for p in preds[b]:
|
|
new_out = live_out[p] | bi
|
|
if new_out != live_out[p]:
|
|
live_out[p] = new_out
|
|
new_in = use[p] | (new_out & ~kill[p])
|
|
if new_in != live_in[p]:
|
|
live_in[p] = new_in
|
|
if not in_work[p]:
|
|
in_work[p] = True
|
|
work.append(p)
|
|
return live_in
|
|
|
|
|
|
def _postdominators(succ):
|
|
"""Immediate post-dominator `ipdom[b]` (Cooper-Harvey-Kennedy on reversed CFG with virtual exit `n`)."""
|
|
n = len(succ)
|
|
exit_ = n
|
|
rev = [[] for _ in range(n + 1)]
|
|
for b in range(n):
|
|
if succ[b]:
|
|
for s in succ[b]:
|
|
rev[s].append(b)
|
|
else:
|
|
rev[exit_].append(b)
|
|
# Iterative RPO from exit_ on reversed CFG.
|
|
state = {exit_: 1}
|
|
order = []
|
|
stack = [(exit_, iter(rev[exit_]))]
|
|
while stack:
|
|
v, it = stack[-1]
|
|
for w in it:
|
|
if w not in state:
|
|
state[w] = 1
|
|
stack.append((w, iter(rev[w])))
|
|
break
|
|
else:
|
|
state[v] = 2
|
|
order.append(v)
|
|
stack.pop()
|
|
rpo = order[::-1]
|
|
pos = {v: i for i, v in enumerate(rpo)}
|
|
ipdom = {exit_: exit_}
|
|
# Original successors (with [] -> [exit_]) are the predecessors in the reversed CFG.
|
|
orig_succ = [list(s) if s else [exit_] for s in succ] + [[]]
|
|
changed = True
|
|
while changed:
|
|
changed = False
|
|
for v in rpo[1:]:
|
|
new = None
|
|
for p in orig_succ[v]:
|
|
if p not in ipdom:
|
|
continue
|
|
if new is None:
|
|
new = p
|
|
continue
|
|
a, b = p, new
|
|
while a != b:
|
|
while pos[a] > pos[b]:
|
|
a = ipdom[a]
|
|
while pos[b] > pos[a]:
|
|
b = ipdom[b]
|
|
new = a
|
|
if ipdom.get(v) != new:
|
|
ipdom[v] = new
|
|
changed = True
|
|
return ipdom
|
|
|
|
|
|
def _holds_lane_mask(ins, x, lm_map):
|
|
k = x.get("k")
|
|
if k == "special":
|
|
t = x.get("t", "")
|
|
return t.startswith("exec") or (t.startswith("vcc") and "vcc" in lm_map)
|
|
if k == "sgpr":
|
|
return all(x["r"] + i in lm_map for i in range(x.get("n", 1)))
|
|
return False
|
|
|
|
|
|
def _is_mask_op(ins, x, lm_map):
|
|
"""Matches Gen.is_mask_op in tools/rdna2spirv.py (special exec/vcc operands always take the mask path)."""
|
|
if _holds_lane_mask(ins, x, lm_map):
|
|
return True
|
|
return x.get("k") == "special" and x.get("t", "").startswith(("exec", "vcc"))
|
|
|
|
|
|
def _is_exec_op_arg(x):
|
|
return x.get("k") == "special" and x.get("t", "") == "exec"
|
|
|
|
|
|
def _sgpr_pair(x):
|
|
if x.get("k") == "sgpr" and x.get("n", 1) == 2:
|
|
return x["r"]
|
|
return None
|
|
|
|
|
|
def _x_memory_order_access(op):
|
|
"""Exact mirror of Gen.x_memory_order(op) in tools/rdna2spirv.py -> (kind, reads, writes) or None."""
|
|
if op in ("ds_swizzle_b32", "ds_bpermute_b32", "ds_permute_b32", "ds_nop"):
|
|
return None
|
|
if op.startswith("ds_"):
|
|
kind = "lds"
|
|
elif op.startswith(("buffer_", "tbuffer_", "global_", "flat_", "image_load", "image_store", "image_atomic",
|
|
"s_load_", "s_buffer_load_")):
|
|
kind = "mem"
|
|
else:
|
|
return None
|
|
store = "store" in op or op.startswith("ds_write")
|
|
reads = not store
|
|
writes = store or "atomic" in op or (kind == "lds" and not op.startswith("ds_read"))
|
|
return kind, reads, writes
|
|
|
|
|
|
def _classify_block_body(insns, lane_masks, scc_live, mask_writes):
|
|
"""Inspect non-terminator instructions of a block for lane-local region safety.
|
|
|
|
Returns `(clean, mem_stats, may_write_scalars, exec_writes, sgpr_write_ops)` where:
|
|
- `clean`: True if no instruction does a cross-lane op, barrier, or unmasked side effect.
|
|
- `mem_stats`: `(mem_ops, mem_r, mem_w, lds_ops, lds_r, lds_w)` counting EXEC-masked memory/LDS ops.
|
|
- `may_write_scalars`: int bitmask of scalar regs possibly written.
|
|
- `exec_writes`: list of `(op, args)` that write EXEC.
|
|
- `sgpr_write_ops`: list of `(sgpr_bitmask, op, args)` for every instruction writing any SGPR.
|
|
"""
|
|
mem_ops = lds_ops = 0
|
|
mem_r = mem_w = lds_r = lds_w = False
|
|
may_w = 0
|
|
exec_writes = []
|
|
sgpr_write_ops = []
|
|
empty_stats = (0, False, False, 0, False, False)
|
|
|
|
for ins in insns:
|
|
raw_op = ins["op"]
|
|
op = base_op(raw_op)
|
|
if op == "s_endpgm" or op in BRANCHES:
|
|
break
|
|
if id(ins) in mask_writes:
|
|
# write_all_lanes VGPR store (parked lane mask): runs even when EXEC == 0, so must not be skipped.
|
|
return False, empty_stats, 0, (), ()
|
|
a = ins["args"]
|
|
lm = lane_masks.get(id(ins), {})
|
|
|
|
# 1. Strictly forbidden opcodes inside a lane-local folded region (cross-lane, barrier, unmasked writes).
|
|
if raw_op.endswith(("_dpp", "_dpp8")):
|
|
return False, empty_stats, 0, (), ()
|
|
if op in ("s_barrier", "s_sendmsg", "s_setpc_b64", "s_swappc_b64", "s_trap",
|
|
"s_sethalt", "s_setkill", "s_sleep", "s_setprio",
|
|
"s_round_mode", "s_denorm_mode",
|
|
"v_readlane_b32", "v_readfirstlane_b32", "v_writelane_b32",
|
|
"v_permlane16_b32", "v_permlanex16_b32",
|
|
"ds_bpermute_b32", "ds_permute_b32", "ds_swizzle_b32",
|
|
"ds_gws_init", "ds_gws_sema_v", "ds_gws_sema_br", "ds_gws_sema_p", "ds_gws_barrier",
|
|
"ds_consume", "ds_append", "ds_ordered_count",
|
|
"s_wqm_b32", "s_wqm_b64", "s_quadmask_b32", "s_quadmask_b64"):
|
|
return False, empty_stats, 0, (), ()
|
|
if op.startswith(("s_movrel", "s_setreg", "s_getreg", "s_store", "s_buffer_store", "s_atomic",
|
|
"ds_gws", "exp")):
|
|
return False, empty_stats, 0, (), ()
|
|
|
|
acc = _x_memory_order_access(op)
|
|
if acc is not None:
|
|
kind, r, w = acc
|
|
if kind == "lds":
|
|
lds_ops += 1
|
|
lds_r = lds_r or r
|
|
lds_w = lds_w or w
|
|
else:
|
|
mem_ops += 1
|
|
mem_r = mem_r or r
|
|
mem_w = mem_w or w
|
|
elif op.startswith("scratch_"):
|
|
if "store" in op or "atomic" in op:
|
|
mem_ops += 1
|
|
mem_w = True
|
|
if "atomic" in op:
|
|
mem_r = True
|
|
else:
|
|
mem_ops += 1
|
|
mem_r = True
|
|
elif op.startswith(("image_sample", "image_gather", "image_bvh")):
|
|
pass # read-only sampled-image / BVH queries: not ordered against buffer/image stores by x_memory_order
|
|
elif "store" in op or "atomic" in op:
|
|
return False, empty_stats, 0, (), ()
|
|
|
|
# 2. Cross-lane mask readers (`wave_mask`, `wave_equal`, live-SCC `scc_from_mask`).
|
|
if op in ("s_bcnt0_i32_b32", "s_bcnt1_i32_b32", "s_bcnt0_i32_b64", "s_bcnt1_i32_b64",
|
|
"s_ff0_i32_b32", "s_ff1_i32_b32", "s_ff0_i32_b64", "s_ff1_i32_b64"):
|
|
if len(a) > 1 and _holds_lane_mask(ins, a[1], lm):
|
|
return False, empty_stats, 0, (), ()
|
|
elif op in ("v_mbcnt_lo_u32_b32", "v_mbcnt_hi_u32_b32"):
|
|
if len(a) > 1 and a[1].get("k") != "imm" and _holds_lane_mask(ins, a[1], lm):
|
|
return False, empty_stats, 0, (), ()
|
|
elif op.startswith(("s_cmp", "s_bitcmp")):
|
|
if any(_holds_lane_mask(ins, x, lm) for x in a):
|
|
return False, empty_stats, 0, (), ()
|
|
elif op in _MASK_SCC_VOTE_OPS and id(ins) in scc_live:
|
|
# Would emit scc_from_mask -> x_round if operating on a lane mask (matches Gen.is_mask_op).
|
|
is_mask_dst = bool(a) and a[0].get("k") == "special" and a[0].get("t", "").startswith(("exec", "vcc"))
|
|
if "saveexec" in op or "wrexec" in op or op.endswith("_mask") or is_mask_dst \
|
|
or any(_is_mask_op(ins, x, lm) for x in a[1:]):
|
|
return False, empty_stats, 0, (), ()
|
|
|
|
# 3. Track scalar writes and EXEC writes.
|
|
_, ins_mw, _ = _scalar_rw(ins)
|
|
may_w |= ins_mw
|
|
s_bits = ins_mw & _ALL_SGPRS
|
|
if s_bits:
|
|
sgpr_write_ops.append((s_bits, op, a))
|
|
writes_exec = (op.startswith("v_cmpx") or "saveexec" in op or "wrexec" in op
|
|
or (bool(a) and a[0].get("k") == "special" and a[0].get("t", "").startswith("exec")))
|
|
if writes_exec:
|
|
exec_writes.append((op, a))
|
|
|
|
return True, (mem_ops, mem_r, mem_w, lds_ops, lds_r, lds_w), may_w, exec_writes, sgpr_write_ops
|
|
|
|
|
|
def _is_zero_val(x, zero_pairs):
|
|
if _is_exec_op_arg(x):
|
|
return True
|
|
if x.get("k") == "imm" and x.get("v", 1) == 0:
|
|
return True
|
|
return _sgpr_pair(x) in zero_pairs
|
|
|
|
|
|
def _write_preserves_zero(op, a, zero_pairs, *, for_exec):
|
|
"""True iff the write produces 0 whenever EXEC == 0 and every pair in `zero_pairs` == 0."""
|
|
if for_exec:
|
|
if op.startswith("v_cmpx") or op in (
|
|
"s_and_saveexec_b64", "s_and_saveexec_b32",
|
|
"s_andn1_saveexec_b64", "s_andn1_saveexec_b32",
|
|
"s_andn1_wrexec_b64", "s_andn1_wrexec_b32"):
|
|
return True
|
|
if op in ("s_andn2_saveexec_b64", "s_or_saveexec_b64", "s_xor_saveexec_b64", "s_andn2_wrexec_b64"):
|
|
return len(a) >= 2 and _is_zero_val(a[1], zero_pairs)
|
|
if not a or not _is_exec_op_arg(a[0]):
|
|
return False # partial exec_lo/exec_hi or unknown EXEC write
|
|
else:
|
|
if op.endswith("_saveexec_b64") or op == "s_andn1_wrexec_b64":
|
|
return True # saves old EXEC (or ~S0 & EXEC), both 0 when EXEC == 0
|
|
if op == "s_andn2_wrexec_b64":
|
|
return len(a) >= 2 and _is_zero_val(a[1], zero_pairs)
|
|
if op == "s_and_b64" and len(a) == 3:
|
|
return _is_zero_val(a[1], zero_pairs) or _is_zero_val(a[2], zero_pairs)
|
|
if op == "s_andn2_b64" and len(a) == 3:
|
|
return _is_zero_val(a[1], zero_pairs)
|
|
if op in ("s_or_b64", "s_xor_b64") and len(a) == 3:
|
|
return _is_zero_val(a[1], zero_pairs) and _is_zero_val(a[2], zero_pairs)
|
|
if op == "s_mov_b64" and len(a) == 2:
|
|
return _is_zero_val(a[1], zero_pairs)
|
|
return False
|
|
|
|
|
|
def _exec_writes_stay_inactive(region_blocks, blk_info, run_live_in):
|
|
"""Return True iff every EXEC write in `region_blocks` preserves `active == False` for inactive lanes.
|
|
|
|
Inductive proof over all execution steps in `region_blocks`: any SGPR pair `s[r:r+1]` not live-in at
|
|
the region entry (`run_live_in`) cannot be read before being written in the region. If every write to
|
|
`s[r:r+1]` and every write to `EXEC` produces 0 whenever `EXEC == 0` and all such pairs are 0, then
|
|
an inactive lane stays inactive across arbitrary nested / reused saveexec if-else diamonds.
|
|
"""
|
|
sgpr_writers = collections.defaultdict(list)
|
|
for u in region_blocks:
|
|
for item in blk_info[u][4]:
|
|
s_bits = item[0]
|
|
while s_bits:
|
|
lsb = s_bits & -s_bits
|
|
sgpr_writers[lsb.bit_length() - 1].append(item)
|
|
s_bits ^= lsb
|
|
zero_pairs = set()
|
|
for r in range(127):
|
|
if (3 << r) & run_live_in:
|
|
continue # entry value could be read before first write in R
|
|
w0 = sgpr_writers.get(r)
|
|
if not w0 or w0 != sgpr_writers.get(r + 1):
|
|
continue
|
|
pair_mask = 3 << r
|
|
if all(s_bits == pair_mask and bool(a) and _sgpr_pair(a[0]) == r for s_bits, _, a in w0):
|
|
zero_pairs.add(r)
|
|
changed = True
|
|
while changed:
|
|
changed = False
|
|
for r in list(zero_pairs):
|
|
if not all(_write_preserves_zero(op, a, zero_pairs, for_exec=False) for _, op, a in sgpr_writers[r]):
|
|
zero_pairs.remove(r)
|
|
changed = True
|
|
for u in region_blocks:
|
|
for op, a in blk_info[u][3]:
|
|
if not _write_preserves_zero(op, a, zero_pairs, for_exec=True):
|
|
return False
|
|
return True
|
|
|
|
|
|
def _tree_has_loop(tree):
|
|
"""Iterative check whether a cfg_structure tree contains any loop node."""
|
|
if not tree:
|
|
return False
|
|
stack = [tree]
|
|
while stack:
|
|
for it in stack.pop():
|
|
if it[0] == "loop":
|
|
return True
|
|
if it[0] == "guard":
|
|
stack.append(it[2])
|
|
return False
|
|
|
|
|
|
def _blocks_in_tree(tree):
|
|
"""Iterative set of all block ids appearing in a cfg_structure tree (or single item)."""
|
|
out = set()
|
|
stack = [tree if isinstance(tree, list) else [tree]]
|
|
while stack:
|
|
for it in stack.pop():
|
|
if it[0] == "block":
|
|
out.add(it[1])
|
|
else:
|
|
stack.append(it[2])
|
|
return out
|
|
|
|
|
|
def _map_tree_ids(tree, mapping):
|
|
"""Iterative copy of a cfg_structure tree replacing every compact id with `mapping[id]`."""
|
|
out = []
|
|
stack = [(tree, out)]
|
|
while stack:
|
|
src, dst = stack.pop()
|
|
for it in src:
|
|
if it[0] == "block":
|
|
dst.append(("block", mapping[it[1]]))
|
|
else:
|
|
child = []
|
|
dst.append((it[0], mapping[it[1]], child))
|
|
stack.append((it[2], child))
|
|
return out
|
|
|
|
|
|
def _unwrap_known_pc_guards(tree, out_pcs, entry_pc=0):
|
|
"""Unwrap `("guard", g, body)` nodes whenever abstract interpretation proves `%pc == g` on all incoming paths.
|
|
|
|
In `cfg_structure.structure`, every post-dominating join block `g` after a conditional diamond or loop is
|
|
still wrapped in `("guard", g, ...)`. Because `%pc` is a Function-scope variable in SPIR-V, LLVM/NVVM's
|
|
`SROA`/`Mem2Reg` promotes `%pc` at the same time as `%vgpr[0..255]` and `%sgpr[0..127]`, before `%pc == g`
|
|
can be constant-folded, forcing ~384 redundant Phi nodes at every spine guard merge block (and preventing
|
|
early block merging across convergent `OpControlBarrier`s).
|
|
|
|
Tracks the exact finite set `cur_pc` of values `%pc` can hold before each item in `tree`:
|
|
- `("block", b)` overwrites `%pc` with `out_pcs[b]`.
|
|
- `("guard", g, gbody)`: if `cur_pc == {g}`, `%pc == g` is tautologically true on every path reaching
|
|
this item, so the guard unwraps in place; otherwise `gbody` is processed starting from `{g}` and its
|
|
exit `%pc` set merges with the skip path `cur_pc - {g}`.
|
|
- `("loop", h, lbody)`: `lbody` is processed starting from `cur_pc | {h}`; exiting the loop proves
|
|
`%pc != h`, leaving `pc_after_lbody - {h}`.
|
|
"""
|
|
root_out = []
|
|
stack = [[collections.deque(tree), root_out, {entry_pc}, None]]
|
|
while stack:
|
|
frame = stack[-1]
|
|
q, dst, cur_pc, _ = frame
|
|
if not q:
|
|
stack.pop()
|
|
if not stack:
|
|
break
|
|
parent = stack[-1]
|
|
tag = frame[3]
|
|
if tag[0] == "guard":
|
|
_, g, child_dst, skip_pc = tag
|
|
parent[1].append(("guard", g, child_dst))
|
|
parent[2] = skip_pc | cur_pc
|
|
else:
|
|
_, h, child_dst = tag
|
|
parent[1].append(("loop", h, child_dst))
|
|
parent[2] = cur_pc - {h}
|
|
continue
|
|
it = q.popleft()
|
|
kind = it[0]
|
|
if kind == "block":
|
|
b = it[1]
|
|
dst.append(it)
|
|
frame[2] = set(out_pcs[b])
|
|
elif kind == "guard":
|
|
g, gbody = it[1], it[2]
|
|
if cur_pc == {g}:
|
|
for child in reversed(gbody):
|
|
q.appendleft(child)
|
|
else:
|
|
child_dst = []
|
|
skip_pc = set(cur_pc)
|
|
skip_pc.discard(g)
|
|
stack.append([collections.deque(gbody), child_dst, {g}, ("guard", g, child_dst, skip_pc)])
|
|
else:
|
|
h, lbody = it[1], it[2]
|
|
child_dst = []
|
|
stack.append([collections.deque(lbody), child_dst, set(cur_pc) | {h}, ("loop", h, child_dst)])
|
|
return root_out
|
|
|
|
|
|
def _region_has_cycle(region, succ):
|
|
"""Iterative 3-color cycle check over the subgraph induced by `region`."""
|
|
color = {}
|
|
for start in region:
|
|
if start in color:
|
|
continue
|
|
color[start] = 1
|
|
stack = [(start, iter(succ[start]))]
|
|
while stack:
|
|
u, it = stack[-1]
|
|
for v in it:
|
|
if v not in region:
|
|
continue
|
|
c = color.get(v, 0)
|
|
if c == 1:
|
|
return True
|
|
if c == 0:
|
|
color[v] = 1
|
|
stack.append((v, iter(succ[v])))
|
|
break
|
|
else:
|
|
color[u] = 2
|
|
stack.pop()
|
|
return False
|
|
|
|
|
|
def find_local_exec_folds(blocks, raw_edges, succ, lane_masks, scc_live, mask_writes=frozenset()):
|
|
"""Find safe lane-local `s_cbranch_execz`/`nz` regions to fold inline under --wave64-emulate.
|
|
|
|
Allows read-only regions (including regions with internal scalar-counted loops) AND acyclic
|
|
single-write-per-domain regions (at most one store/atomic touching `"mem"` or `"lds"` with no
|
|
other access to that domain inside the region).
|
|
|
|
Returns `(folds, fold_chain, absorbed)`:
|
|
- `folds`: dict `head_block -> {"run": int, "skip": int, "region": frozenset[int],
|
|
"order": list[int], "sub_tree": list | None, "mem_reads": frozenset[str],
|
|
"mem_writes": frozenset[str], "mem_stats": tuple}`
|
|
containing only the maximal (outermost) folded regions.
|
|
- `fold_chain`: dict `head_block -> skip_block` when `skip_block` has no predecessors outside
|
|
`{head_block} | region` and can be emitted directly after the fold in the same block.
|
|
- `absorbed`: set of all block indices emitted inline (inside a folded region or chained as `skip`).
|
|
"""
|
|
n = len(blocks)
|
|
if n < 2:
|
|
return {}, {}, set()
|
|
|
|
active_blocks = [_active_insns(blk) for blk in blocks]
|
|
preds = [set() for _ in range(n)]
|
|
for b in range(n):
|
|
for s in succ[b]:
|
|
preds[s].add(b)
|
|
|
|
ipdom = _postdominators(succ)
|
|
scalar_live_in = _scalar_liveness(active_blocks, succ)
|
|
|
|
blk_info = []
|
|
terms = []
|
|
for b, insns in enumerate(active_blocks):
|
|
blk_info.append(_classify_block_body(insns, lane_masks, scc_live, mask_writes))
|
|
last = insns[-1] if insns else None
|
|
op = base_op(last["op"]) if last is not None else None
|
|
if op == "s_endpgm":
|
|
terms.append(("end", None))
|
|
elif op in BRANCHES:
|
|
terms.append(("br", op))
|
|
else:
|
|
terms.append(("fall", None))
|
|
|
|
# Gather candidate s_cbranch_execz / s_cbranch_execnz blocks with single-entry/single-exit post-dominated regions.
|
|
candidates = []
|
|
for b in range(n):
|
|
kind, op = terms[b]
|
|
if kind != "br" or op not in ("s_cbranch_execz", "s_cbranch_execnz"):
|
|
continue
|
|
edges = raw_edges[b]
|
|
if len(edges) < 2:
|
|
continue
|
|
tgt, fall = edges[0], edges[1]
|
|
run, skip = (fall, tgt) if op == "s_cbranch_execz" else (tgt, fall)
|
|
if run == skip or skip >= n or ipdom.get(b) != skip:
|
|
continue
|
|
# Collect region R reachable from `run` stopping at `skip`.
|
|
region = set()
|
|
work = [run]
|
|
ok = True
|
|
while work:
|
|
u = work.pop()
|
|
if u == skip or u in region:
|
|
continue
|
|
if u == b or u == 0 or not succ[u] or not blk_info[u][0]:
|
|
ok = False
|
|
break
|
|
region.add(u)
|
|
for s in succ[u]:
|
|
if s != skip and s not in region:
|
|
work.append(s)
|
|
if not ok or not region:
|
|
continue
|
|
# Single-entry check: no external edge into any block of R except B -> run.
|
|
for u in region:
|
|
allowed_ext = {b} if u == run else set()
|
|
if not preds[u] <= (region | allowed_ext):
|
|
ok = False
|
|
break
|
|
if not ok:
|
|
continue
|
|
# Memory-write safety: if R writes "mem" (or "lds"), R must be acyclic and touch that domain at most once.
|
|
mem_ops = lds_ops = 0
|
|
mem_r = mem_w = lds_r = lds_w = False
|
|
for u in region:
|
|
mo, mr, mw, lo, lr, lw = blk_info[u][1]
|
|
mem_ops += mo
|
|
mem_r = mem_r or mr
|
|
mem_w = mem_w or mw
|
|
lds_ops += lo
|
|
lds_r = lds_r or lr
|
|
lds_w = lds_w or lw
|
|
if (mem_w and mem_ops > 1) or (lds_w and lds_ops > 1):
|
|
continue
|
|
if (mem_w or lds_w) and _region_has_cycle(region, succ):
|
|
continue
|
|
# No scalar written in R may be live-in at `skip`.
|
|
region_may_w = 0
|
|
for u in region:
|
|
region_may_w |= blk_info[u][2]
|
|
if region_may_w & scalar_live_in[skip]:
|
|
continue
|
|
# EXEC must stay inactive for any lane that skipped R.
|
|
if not _exec_writes_stay_inactive(region, blk_info, scalar_live_in[run]):
|
|
continue
|
|
candidates.append((len(region), b, run, skip, frozenset(region),
|
|
(mem_ops, mem_r, mem_w, lds_ops, lds_r, lds_w)))
|
|
|
|
# Validate innermost regions first so nested s_cbranch_execz/nz inside an outer region are already verified.
|
|
candidates.sort(key=lambda item: (item[0], item[1]))
|
|
valid = {}
|
|
for _, b, run, skip, region, mem_stats in candidates:
|
|
ok = True
|
|
for u in region:
|
|
tkind, top = terms[u]
|
|
if tkind == "end":
|
|
ok = False
|
|
break
|
|
if tkind == "br":
|
|
if top in ("s_cbranch_vccz", "s_cbranch_vccnz"):
|
|
ok = False
|
|
break
|
|
if top in ("s_cbranch_execz", "s_cbranch_execnz"):
|
|
if u not in valid or valid[u]["skip"] not in (region | {skip}):
|
|
ok = False
|
|
break
|
|
if not ok:
|
|
continue
|
|
# Build local sub-CFG and verify reducibility (multi-block regions use cfg_structure.structure).
|
|
if len(region) == 1:
|
|
order = [run]
|
|
sub_tree = None
|
|
else:
|
|
order = [run] + sorted(region - {run})
|
|
idx_of = {u: i for i, u in enumerate(order)}
|
|
sub_succ = [[idx_of[s] for s in succ[u] if s in idx_of] for u in order]
|
|
sub_tree = cfg_structure.structure(sub_succ)
|
|
if sub_tree is None:
|
|
continue
|
|
sub_end = len(order)
|
|
sub_out_pcs = {}
|
|
for i, u in enumerate(order):
|
|
uedges = raw_edges[u]
|
|
tkind, top = terms[u]
|
|
if tkind in ("fall", "end"):
|
|
s = {idx_of.get(uedges[0], sub_end) if (tkind == "fall" and uedges) else sub_end}
|
|
elif top == "s_branch":
|
|
s = {idx_of.get(uedges[0], sub_end)}
|
|
else:
|
|
s = {idx_of.get(uedges[0], sub_end),
|
|
idx_of.get(uedges[1], sub_end) if len(uedges) > 1 else sub_end}
|
|
sub_out_pcs[i] = frozenset(s)
|
|
sub_tree = _unwrap_known_pc_guards(sub_tree, sub_out_pcs, 0)
|
|
_, mem_r, mem_w, _, lds_r, lds_w = mem_stats
|
|
mem_reads = frozenset(k for k, flag in (("mem", mem_r), ("lds", lds_r)) if flag)
|
|
mem_writes = frozenset(k for k, flag in (("mem", mem_w), ("lds", lds_w)) if flag)
|
|
valid[b] = {
|
|
"run": run,
|
|
"skip": skip,
|
|
"region": region,
|
|
"order": order,
|
|
"sub_tree": sub_tree,
|
|
"mem_reads": mem_reads,
|
|
"mem_writes": mem_writes,
|
|
"mem_stats": mem_stats,
|
|
}
|
|
|
|
# Select maximal (non-overlapping outermost) folded regions.
|
|
selected = {}
|
|
in_region = set()
|
|
for _, b, _, _, _ in sorted(
|
|
((len(v["region"]), k, v["run"], v["skip"], v["region"]) for k, v in valid.items()),
|
|
key=lambda x: (-x[0], x[1])):
|
|
fd = valid[b]
|
|
if b in in_region or (fd["region"] & in_region):
|
|
continue
|
|
selected[b] = fd
|
|
in_region.update(fd["region"])
|
|
|
|
# Chain single-predecessor `skip` continuations directly after their fold (cycle-free by construction).
|
|
fold_chain = {}
|
|
chained = set()
|
|
parent_chain = {}
|
|
|
|
def chain_root(x):
|
|
while x in parent_chain:
|
|
x = parent_chain[x]
|
|
return x
|
|
|
|
for b in sorted(selected):
|
|
fd = selected[b]
|
|
skip = fd["skip"]
|
|
if skip == 0 or skip in in_region or skip in chained:
|
|
continue
|
|
if not preds[skip] <= ({b} | fd["region"]):
|
|
continue
|
|
if chain_root(skip) == chain_root(b):
|
|
continue
|
|
fold_chain[b] = skip
|
|
parent_chain[skip] = b
|
|
chained.add(skip)
|
|
|
|
absorbed = in_region | chained
|
|
return selected, fold_chain, absorbed
|
|
|
|
|
|
_SOP2_B32_LOGIC = frozenset({
|
|
"s_and_b32", "s_or_b32", "s_xor_b32", "s_andn2_b32", "s_orn2_b32",
|
|
"s_nand_b32", "s_nor_b32", "s_xnor_b32", "s_not_b32",
|
|
})
|
|
|
|
|
|
def _is_uniform_scc_safe(active_blocks, lane_masks, scc_live):
|
|
"""True iff no instruction triggers wave64's per-lane 32-bit half-mask SCC write (rdna2spirv.py scc_logic)
|
|
with live SCC. When True, %scc is guaranteed identical across all 64 lanes of each wave."""
|
|
for insns in active_blocks:
|
|
for ins in insns:
|
|
op = base_op(ins["op"])
|
|
if op not in _SOP2_B32_LOGIC or id(ins) not in scc_live:
|
|
continue
|
|
a = ins["args"]
|
|
lm = lane_masks.get(id(ins), {})
|
|
writes_exec = bool(a) and a[0].get("k") == "special" and a[0].get("t", "").startswith("exec")
|
|
if writes_exec or any(_holds_lane_mask(ins, x, lm) for x in a[1:]):
|
|
return False
|
|
return True
|
|
|
|
|
|
def _insn_may_barrier(ins, lane_masks, scc_live):
|
|
"""Conservative (over-approximate) check: True if `ins` may emit x_round or OpControlBarrier in --wave64-emulate."""
|
|
raw_op = ins["op"]
|
|
op = base_op(raw_op)
|
|
if op == "s_endpgm" or op in BRANCHES:
|
|
return False
|
|
if raw_op.endswith(("_dpp", "_dpp8")):
|
|
return True
|
|
if op in ("s_barrier", "v_readlane_b32", "v_readfirstlane_b32",
|
|
"v_permlane16_b32", "v_permlanex16_b32",
|
|
"ds_bpermute_b32", "ds_permute_b32", "ds_swizzle_b32",
|
|
"s_wqm_b32", "s_wqm_b64"):
|
|
return True
|
|
if op.startswith("s_movrel"):
|
|
return True
|
|
a = ins["args"]
|
|
lm = lane_masks.get(id(ins), {})
|
|
if op in ("s_bcnt0_i32_b32", "s_bcnt1_i32_b32", "s_bcnt0_i32_b64", "s_bcnt1_i32_b64",
|
|
"s_ff0_i32_b32", "s_ff1_i32_b32", "s_ff0_i32_b64", "s_ff1_i32_b64"):
|
|
return len(a) > 1 and _holds_lane_mask(ins, a[1], lm)
|
|
if op in ("v_mbcnt_lo_u32_b32", "v_mbcnt_hi_u32_b32"):
|
|
return len(a) > 1 and a[1].get("k") != "imm" and _holds_lane_mask(ins, a[1], lm)
|
|
if op.startswith(("s_cmp", "s_bitcmp")):
|
|
return any(_holds_lane_mask(ins, x, lm) for x in a)
|
|
if op in _MASK_SCC_VOTE_OPS and id(ins) in scc_live:
|
|
is_mask_dst = bool(a) and a[0].get("k") == "special" and a[0].get("t", "").startswith(("exec", "vcc"))
|
|
return ("saveexec" in op or "wrexec" in op or op.endswith("_mask") or is_mask_dst
|
|
or any(_is_mask_op(ins, x, lm) for x in a[1:]))
|
|
return False
|
|
|
|
|
|
def _dirty_conflicts(dirty, reads, writes):
|
|
return (any((k, "w") in dirty for k in reads)
|
|
or any((k, "r") in dirty or (k, "w") in dirty for k in writes))
|
|
|
|
|
|
def plan_structured_emulation(blocks, raw_edges, succ, folds, fold_chain, absorbed, lane_masks, scc_live, x_lanes):
|
|
"""Plan structured --wave64-emulate emission eliminating the giant global round-loop OpSwitch.
|
|
|
|
- Single-wave workgroups (`x_lanes == 64`): every reducible CFG emits 100% inline structured SPIR-V
|
|
with direct workgroup barriers (`0` OpSwitch cases, `0` x_spill variables, `0` scheduler atomics).
|
|
- Multi-wave workgroups (`x_lanes > 64`): decomposes top-level post-dominating spine blocks and
|
|
barrier-free guards/loops (including live-out scalars, single-write stores, EXEC-widening, and
|
|
read-only loops) into inline structured SPIR-V, isolating any remaining wave-divergent barrier
|
|
regions into tiny local mini-schedulers instead of one global K_{1,N} switch clique.
|
|
|
|
Returns `None` when the shader already collapsed to 1 superblock, is irreducible, uses non-uniform
|
|
32-bit half-mask SCC, or (multi-wave) has a divergent `s_barrier` or no decomposition gain.
|
|
"""
|
|
n = len(blocks)
|
|
eff_blocks = [b for b in range(n) if b not in absorbed]
|
|
if len(eff_blocks) < 2:
|
|
return None # straight-line or fully folded to 1 superblock: existing x_scheduler already has <=1 case
|
|
|
|
active_blocks = [_active_insns(blk) for blk in blocks]
|
|
if not _is_uniform_scc_safe(active_blocks, lane_masks, scc_live):
|
|
return None
|
|
|
|
# Build superblock chains and effective post-fold CFG over `eff_blocks`.
|
|
eff_idx = {b: i for i, b in enumerate(eff_blocks)}
|
|
chain_of = {}
|
|
eff_succ = {}
|
|
eff_succ_compact = []
|
|
for b in eff_blocks:
|
|
chain = []
|
|
cur = b
|
|
while True:
|
|
chain.append(cur)
|
|
if cur in folds and cur in fold_chain:
|
|
cur = fold_chain[cur]
|
|
continue
|
|
break
|
|
chain_of[b] = chain
|
|
tail = chain[-1]
|
|
if tail in folds:
|
|
nxt = [folds[tail]["skip"]]
|
|
else:
|
|
nxt = succ[tail]
|
|
if any(s not in eff_idx for s in nxt):
|
|
return None # defensive: every effective successor must be a non-absorbed block
|
|
eff_succ[b] = nxt
|
|
eff_succ_compact.append([eff_idx[s] for s in nxt])
|
|
|
|
compact_tree = cfg_structure.structure(eff_succ_compact)
|
|
if compact_tree is None:
|
|
return None
|
|
tree = _map_tree_ids(compact_tree, eff_blocks)
|
|
|
|
out_pcs = {}
|
|
for b in eff_blocks:
|
|
tail = chain_of[b][-1]
|
|
s = set(eff_succ[b])
|
|
if not s:
|
|
s.add(-1)
|
|
elif tail not in folds and len(raw_edges[tail]) == 1:
|
|
last = active_blocks[tail][-1] if active_blocks[tail] else None
|
|
op = base_op(last["op"]) if last is not None else None
|
|
if op in BRANCHES and op != "s_branch":
|
|
s.add(-1)
|
|
out_pcs[b] = frozenset(s)
|
|
tree = _unwrap_known_pc_guards(tree, out_pcs, eff_blocks[0])
|
|
|
|
# Precompute per-superblock instruction summaries (for dirty transfer, sync-free check, s_barrier, s_endpgm).
|
|
sb_has_sbarrier = {}
|
|
sb_has_endpgm = {}
|
|
sb_may_barrier = {}
|
|
sb_mem_stats = {}
|
|
|
|
for b in eff_blocks:
|
|
has_sb = False
|
|
has_end = False
|
|
may_bar = False
|
|
mem_ops = lds_ops = 0
|
|
mem_r = mem_w = lds_r = lds_w = False
|
|
chain = chain_of[b]
|
|
for idx_c, cur in enumerate(chain):
|
|
insns = active_blocks[cur]
|
|
for ins in insns:
|
|
op = base_op(ins["op"])
|
|
if op == "s_endpgm":
|
|
has_end = True
|
|
break
|
|
if op in BRANCHES:
|
|
if idx_c == len(chain) - 1 and cur not in folds:
|
|
if op in ("s_cbranch_execz", "s_cbranch_execnz", "s_cbranch_vccz", "s_cbranch_vccnz"):
|
|
may_bar = True
|
|
break
|
|
if op == "s_barrier":
|
|
has_sb = True
|
|
may_bar = True
|
|
elif _insn_may_barrier(ins, lane_masks, scc_live):
|
|
may_bar = True
|
|
acc = _x_memory_order_access(op)
|
|
if acc is not None:
|
|
kind, r, w = acc
|
|
if kind == "lds":
|
|
lds_ops += 1
|
|
lds_r = lds_r or r
|
|
lds_w = lds_w or w
|
|
else:
|
|
mem_ops += 1
|
|
mem_r = mem_r or r
|
|
mem_w = mem_w or w
|
|
elif op.startswith("scratch_"):
|
|
mem_ops += 1
|
|
if "store" in op or "atomic" in op:
|
|
mem_w = True
|
|
if "atomic" in op:
|
|
mem_r = True
|
|
else:
|
|
mem_r = True
|
|
if cur in folds:
|
|
mo, mr, mw, lo, lr, lw = folds[cur]["mem_stats"]
|
|
mem_ops += mo
|
|
mem_r = mem_r or mr
|
|
mem_w = mem_w or mw
|
|
lds_ops += lo
|
|
lds_r = lds_r or lr
|
|
lds_w = lds_w or lw
|
|
sb_has_sbarrier[b] = has_sb
|
|
sb_has_endpgm[b] = has_end
|
|
sb_may_barrier[b] = may_bar
|
|
sb_mem_stats[b] = (mem_ops, mem_r, mem_w, lds_ops, lds_r, lds_w)
|
|
|
|
def transfer_superblock(b, d_in):
|
|
"""Strictly monotone GEN/KILL dirty transfer over superblock `b`.
|
|
|
|
Only unconditional barriers (`s_barrier` and voted branches `s_cbranch_execz/nz/vccz/nz`) clear `d`.
|
|
Never clears on `d_in`-induced or intra-block memory conflicts: if an earlier `x_round` (e.g.
|
|
`v_readlane_b32`) or an earlier conflict cleared `g.x_dirty` in codegen, a phantom clear here could
|
|
wipe a live dirty domain written afterward and drop a required cross-block `OpControlBarrier`.
|
|
"""
|
|
d = set(d_in)
|
|
chain = chain_of[b]
|
|
for idx_c, cur in enumerate(chain):
|
|
insns = active_blocks[cur]
|
|
for ins in insns:
|
|
op = base_op(ins["op"])
|
|
if op == "s_endpgm":
|
|
break
|
|
if op in BRANCHES:
|
|
if idx_c == len(chain) - 1 and cur not in folds:
|
|
if op in ("s_cbranch_execz", "s_cbranch_execnz", "s_cbranch_vccz", "s_cbranch_vccnz"):
|
|
d.clear() # branch_condition unconditionally runs g_vote -> x_round -> barrier
|
|
break
|
|
if op == "s_barrier":
|
|
d.clear()
|
|
continue
|
|
acc = _x_memory_order_access(op)
|
|
if acc is not None:
|
|
kind, r, w = acc
|
|
if r:
|
|
d.add((kind, "r"))
|
|
if w:
|
|
d.add((kind, "w"))
|
|
if cur in folds:
|
|
fd = folds[cur]
|
|
for k in fd["mem_reads"]:
|
|
d.add((k, "r"))
|
|
for k in fd["mem_writes"]:
|
|
d.add((k, "w"))
|
|
return frozenset(d)
|
|
|
|
# ---- Mode 1: Single-wave (`x_lanes == 64`) -> 100% inline structured SPIR-V, 0 OpSwitch ----
|
|
if x_lanes == 64:
|
|
dirty_in = {b: set() for b in eff_blocks}
|
|
work = collections.deque(eff_blocks)
|
|
in_work = set(eff_blocks)
|
|
while work:
|
|
u = work.popleft()
|
|
in_work.discard(u)
|
|
d_out = transfer_superblock(u, dirty_in[u])
|
|
for v in eff_succ[u]:
|
|
if not d_out <= dirty_in[v]:
|
|
dirty_in[v].update(d_out)
|
|
if v not in in_work:
|
|
in_work.add(v)
|
|
work.append(v)
|
|
return {
|
|
"mode": "inline_all",
|
|
"tree": tree,
|
|
"dirty_in": {b: frozenset(d) for b, d in dirty_in.items()},
|
|
"inline_blocks": set(eff_blocks),
|
|
"sync_free_blocks": set(),
|
|
"mini_of_block": {},
|
|
"mini_blocks": {},
|
|
"tail_blocks": set(),
|
|
}
|
|
|
|
# ---- Mode 2: Multi-wave (`x_lanes > 64`) -> top-level uniform inline + sync-free + local mini-schedulers ----
|
|
ipdom_c = _postdominators(eff_succ_compact)
|
|
m_eff = len(eff_blocks)
|
|
# Require every effective block to reach exit (no infinite-loop sink SCCs bypassing post-dominators).
|
|
entry_pdoms = set()
|
|
if len(ipdom_c) == m_eff + 1 and 0 in ipdom_c:
|
|
cur_c = 0
|
|
while cur_c != m_eff:
|
|
entry_pdoms.add(eff_blocks[cur_c])
|
|
nxt_c = ipdom_c.get(cur_c)
|
|
if nxt_c is None or nxt_c == cur_c:
|
|
break
|
|
cur_c = nxt_c
|
|
|
|
eff_preds = {b: set() for b in eff_blocks}
|
|
for u in eff_blocks:
|
|
for v in eff_succ[u]:
|
|
eff_preds[v].add(u)
|
|
|
|
# Unwrap top-level guards whose entry strictly post-dominates entry 0 and whose non-back-edge
|
|
# predecessors have all been emitted, exposing the full post-dominating spine at top level.
|
|
flat_top = []
|
|
q = collections.deque(tree)
|
|
seen_before = set()
|
|
saw_endpgm = False
|
|
while q:
|
|
it = q.popleft()
|
|
if not saw_endpgm and it[0] == "guard":
|
|
entry_b = it[1]
|
|
sub = it[2]
|
|
if entry_b in entry_pdoms and sub:
|
|
first_kind = sub[0][0]
|
|
if first_kind == "block" and sub[0][1] == entry_b:
|
|
need_preds = eff_preds[entry_b]
|
|
elif first_kind == "loop" and sub[0][1] == entry_b:
|
|
loop_nodes = _blocks_in_tree(sub[0])
|
|
need_preds = eff_preds[entry_b] - loop_nodes
|
|
else:
|
|
need_preds = None
|
|
if need_preds is not None and need_preds <= seen_before:
|
|
for child in reversed(sub):
|
|
q.appendleft(child)
|
|
continue
|
|
flat_top.append(it)
|
|
it_blocks = _blocks_in_tree(it)
|
|
seen_before.update(it_blocks)
|
|
if any(sb_has_endpgm[u] for u in it_blocks):
|
|
saw_endpgm = True
|
|
|
|
# Multi-wave soundness guard: any `s_barrier` inside a non-top-level guard/loop must stay in global x_scheduler
|
|
# so waves on different paths can meet at different s_barrier sites (cs_barrier_divergent).
|
|
for it in flat_top:
|
|
if it[0] != "block":
|
|
if any(sb_has_sbarrier[u] for u in _blocks_in_tree(it)):
|
|
return None
|
|
|
|
def check_sync_free(it):
|
|
sub_blocks = _blocks_in_tree(it)
|
|
has_loop = _tree_has_loop([it])
|
|
mem_ops = lds_ops = 0
|
|
mem_r = mem_w = lds_r = lds_w = False
|
|
for u in sub_blocks:
|
|
if sb_may_barrier[u] or sb_has_endpgm[u] or sb_has_sbarrier[u]:
|
|
return False, frozenset(), frozenset()
|
|
mo, mr, mw, lo, lr, lw = sb_mem_stats[u]
|
|
mem_ops += mo
|
|
mem_r = mem_r or mr
|
|
mem_w = mem_w or mw
|
|
lds_ops += lo
|
|
lds_r = lds_r or lr
|
|
lds_w = lds_w or lw
|
|
if has_loop:
|
|
if mem_w or lds_w:
|
|
return False, frozenset(), frozenset()
|
|
else:
|
|
if (mem_w and mem_ops > 1) or (lds_w and lds_ops > 1):
|
|
return False, frozenset(), frozenset()
|
|
reads = frozenset(k for k, f in (("mem", mem_r), ("lds", lds_r)) if f)
|
|
writes = frozenset(k for k, f in (("mem", mem_w), ("lds", lds_w)) if f)
|
|
return True, reads, writes
|
|
|
|
steps = []
|
|
dirty_in = {}
|
|
inline_blocks = set()
|
|
sync_free_blocks = set()
|
|
mini_of_block = {}
|
|
mini_blocks = {}
|
|
tail_blocks = set()
|
|
cur_dirty = frozenset()
|
|
mini_idx = 0
|
|
|
|
for idx_it, it in enumerate(flat_top):
|
|
it_blocks = _blocks_in_tree(it)
|
|
has_end = any(sb_has_endpgm[u] for u in it_blocks)
|
|
if it[0] == "block" and (not has_end or idx_it == len(flat_top) - 1):
|
|
b = it[1]
|
|
dirty_in[b] = cur_dirty
|
|
inline_blocks.add(b)
|
|
cur_dirty = transfer_superblock(b, cur_dirty)
|
|
steps.append(("inline_block", b))
|
|
continue
|
|
if has_end:
|
|
# Early s_endpgm inside a wave-divergent guard/loop (or non-final block): hand remaining tail to x_scheduler.
|
|
rem = set()
|
|
for rem_it in flat_top[idx_it:]:
|
|
rem.update(_blocks_in_tree(rem_it))
|
|
tail_blocks = rem
|
|
steps.append(("tail_scheduler", rem, bool(cur_dirty)))
|
|
cur_dirty = frozenset()
|
|
break
|
|
ok_sf, sf_reads, sf_writes = check_sync_free(it)
|
|
if ok_sf:
|
|
pre_bar = _dirty_conflicts(cur_dirty, sf_reads, sf_writes)
|
|
if pre_bar:
|
|
cur_dirty = frozenset()
|
|
for u in it_blocks:
|
|
dirty_in[u] = frozenset() # pre_bar cleared conflicts before entering sync-free region
|
|
inline_blocks.add(u)
|
|
sync_free_blocks.add(u)
|
|
cur_dirty = frozenset(set(cur_dirty)
|
|
| {(k, "r") for k in sf_reads}
|
|
| {(k, "w") for k in sf_writes})
|
|
steps.append(("inline_sync_free", it, pre_bar))
|
|
else:
|
|
pre_bar = bool(cur_dirty)
|
|
cur_dirty = frozenset() # mini-scheduler ends with OpControlBarrier %c2 %c2 %c948
|
|
mini_blocks[mini_idx] = it_blocks
|
|
for u in it_blocks:
|
|
mini_of_block[u] = mini_idx
|
|
steps.append(("mini_scheduler", mini_idx, it_blocks, pre_bar))
|
|
mini_idx += 1
|
|
|
|
# Only engage structured_hybrid when it actually eliminates global-switch blocks or splits into smaller regions.
|
|
if len(steps) == 1 and steps[0][0] in ("tail_scheduler", "mini_scheduler"):
|
|
return None
|
|
return {
|
|
"mode": "structured_hybrid",
|
|
"steps": steps,
|
|
"dirty_in": dirty_in,
|
|
"inline_blocks": inline_blocks,
|
|
"sync_free_blocks": sync_free_blocks,
|
|
"mini_of_block": mini_of_block,
|
|
"mini_blocks": mini_blocks,
|
|
"tail_blocks": tail_blocks,
|
|
}
|