mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 13:58:43 +00:00
wave64_emulate_opt: coalesce hoisted spine guards across early exits and loop headers
This commit is contained in:
@ -1395,6 +1395,38 @@ class StructuredEmulationTests(unittest.TestCase):
|
||||
v4 += 31
|
||||
self.assertEqual(mem[l * 4], v4, f"lane {l}")
|
||||
|
||||
# 2) Guard-spine coalescing after an early-exit branch (0 -> [1 (end), 2]) and inside a header-exit loop:
|
||||
# cfg_structure.structure hoists spine guards into the enclosing list where skip_pc holds the early-exit
|
||||
# target. _unwrap_known_pc_guards must coalesce and unwrap all hoisted post-dominating spine guards.
|
||||
import cfg_structure
|
||||
succ_early = [[1, 2], []] # block 0 -> early exit 1 or main body 2
|
||||
cur = 2
|
||||
for _ in range(10):
|
||||
succ_early.append([cur + 1, cur + 2])
|
||||
succ_early.append([cur + 2])
|
||||
cur += 2
|
||||
succ_early.append([])
|
||||
raw_tree = cfg_structure.structure(succ_early)
|
||||
out_pcs = {b: (frozenset(s) if s else frozenset({-1})) for b, s in enumerate(succ_early)}
|
||||
unwrapped_tree = wave64_emulate_opt._unwrap_known_pc_guards(raw_tree, out_pcs, 0)
|
||||
|
||||
def count_guards(t):
|
||||
c = 0
|
||||
st = [t]
|
||||
while st:
|
||||
for item in st.pop():
|
||||
if item[0] == "guard":
|
||||
c += 1
|
||||
st.append(item[2])
|
||||
elif item[0] == "loop":
|
||||
st.append(item[2])
|
||||
return c
|
||||
|
||||
# Raw tree has 22 guards (guard 1, guard 2, plus 10 arm guards and 10 hoisted spine guards);
|
||||
# coalesced unwrap removes all 10 hoisted spine guards -> 12 guards.
|
||||
self.assertEqual(count_guards(raw_tree), 22)
|
||||
self.assertEqual(count_guards(unwrapped_tree), 12)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -673,13 +673,20 @@ def _unwrap_known_pc_guards(tree, out_pcs, entry_pc=0):
|
||||
stack = [[collections.deque(tree), root_out, {entry_pc}, None]]
|
||||
while stack:
|
||||
frame = stack[-1]
|
||||
q, dst, cur_pc, _ = frame
|
||||
q, dst, cur_pc, tag = frame
|
||||
if not q:
|
||||
if len(stack) > 1 and tag[0] == "guard" and len(cur_pc) == 1:
|
||||
nxt_g = next(iter(cur_pc))
|
||||
skip_pc = tag[3]
|
||||
parent_q = stack[-2][0]
|
||||
if parent_q and parent_q[0][0] == "guard" and parent_q[0][1] == nxt_g and nxt_g not in skip_pc:
|
||||
for child in reversed(parent_q.popleft()[2]):
|
||||
q.appendleft(child)
|
||||
continue
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user