upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/cfg_structure.py
blob: 9b7b4121276d2dc38d47c9f91284debca65cad74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""Arrange a control-flow graph as nested loops and conditionals driven by a block-number variable.

    structure(succ) -> tree, or None when the graph is irreducible

`succ[b]` lists the blocks that block b can go to next (none = the program ends there); block 0 is the entry.
Every block ends by storing its successor's number in a variable `pc` (END when it has none). The tree says where
each block's code goes:

    ("block", b)             run block b
    ("guard", b, [items])    if pc == b: run the items (they start with block b, or with the loop headed by b)
    ("loop", b, [items])     repeat the items, which start with block b, for as long as pc == b afterwards

Why this instead of one loop around a switch: a shader compiler has to rediscover the real control flow from a
switch on a variable that every block writes, which takes it minutes on shaders with a thousand blocks. Here the
loops are loops, and the guards are tests against a value that was just assigned, which fold at once.

How it is built (reducible graphs only, i.e. every loop has a single entry):
  - Each natural loop becomes a "loop" item. Leaving it needs no break: pc simply no longer names the header.
  - Inside a loop (and at the top level) the members are nested by dominance: after a member come the members it
    immediately dominates, each behind a guard, in an order where every forward edge points later. A jump to a
    member that isn't below the current one falls out of the guards it is in, to the level where a later guard
    matches. A jump out of the loop falls out of all of them.
A block is never skipped or run twice in one pass, so any path through the graph is followed exactly.
"""
import collections

END = -1


def _order(succ):
    """Reverse postorder from block 0, the retreating edges of that walk, and the predecessors of each block."""
    state, order, back = {0: 1}, [], []
    stack = [(0, iter(succ[0]))]
    while stack:
        v, it = stack[-1]
        for w in it:
            if w not in state:
                state[w] = 1
                stack.append((w, iter(succ[w])))
                break
            if state[w] == 1:
                back.append((v, w))
        else:
            state[v] = 2
            order.append(v)
            stack.pop()
    rpo = order[::-1]
    preds = collections.defaultdict(list)
    for v in rpo:
        for w in succ[v]:
            preds[w].append(v)
    return rpo, back, preds


def _dominators(rpo, preds):
    """Immediate dominator of each reachable block (Cooper, Harvey and Kennedy)."""
    pos = {v: i for i, v in enumerate(rpo)}
    idom = {rpo[0]: rpo[0]}
    changed = True
    while changed:
        changed = False
        for v in rpo[1:]:
            new = None
            for p in preds[v]:
                if p not in idom:
                    continue
                if new is None:
                    new = p
                    continue
                a, b = p, new
                while a != b:
                    while pos[a] > pos[b]:
                        a = idom[a]
                    while pos[b] > pos[a]:
                        b = idom[b]
                new = a
            if idom.get(v) != new:
                idom[v] = new
                changed = True
    return idom, pos


def structure(succ, out_pcs=None):
    rpo, back, preds = _order(succ)
    idom, pos = _dominators(rpo, preds)

    def dominates(a, b):
        while pos[b] > pos[a]:
            b = idom[b]
        return a == b

    if not all(dominates(w, v) for v, w in back):
        return None                                   # a loop with two entries: no nesting expresses it

    # Natural loops, and the innermost loop each block is in (named by its header; None = not in a loop).
    bodies = {}
    for v, h in back:
        body = bodies.setdefault(h, {h})
        work = [v]
        while work:
            x = work.pop()
            if x not in body:
                body.add(x)
                work.extend(preds[x])
    inner = {}
    for h in sorted(bodies, key=lambda h: len(bodies[h])):
        for x in bodies[h]:
            inner.setdefault(x, h)
    outer = {}                                        # loop -> the loop around it
    for h in bodies:
        around = [g for g in bodies if g != h and h in bodies[g]]
        outer[h] = min(around, key=lambda g: len(bodies[g])) if around else None

    def member(x, region):
        """The member of `region` that block x belongs to: x itself, or the loop inside the region holding it.
        Members are ("b", block) and ("l", header)."""
        loop = inner.get(x)
        if loop == region:
            return ("b", x)
        while outer[loop] != region:
            loop = outer[loop]
        return ("l", loop)

    # Dominance among the members of each region. A member's parent is the member holding the immediate
    # dominator of its first block; that dominator is always inside the same region.
    children = collections.defaultdict(list)
    for x in rpo[1:]:
        region = inner.get(x)
        if region == x:                               # a loop header: it is placed as its loop, in the outer region
            children[member(idom[x], outer[x])].append(("l", x))
        else:
            children[member(idom[x], region)].append(("b", x))

    # Flatten dominated tail continuations out of enclosing guards and build iteratively so long sequential
    # diamond/loop chains stay O(1) deep instead of nesting O(N) OpSelectionMerge constructs (and never hit
    # Python's recursion limit on multi-thousand-block shaders).
    # Hoisting the last child c_last of a guarded member m into m's enclosing list is exact: m's head strictly
    # dominates c_last's head, so no block outside m's subtree can jump to c_last's head. When m's guard is skipped
    # (pc != m_head), pc cannot equal c_last_head either, so c_last's guard is skipped too; when m's guard runs,
    # c_last runs right after m's earlier children, in the same order as before.
    max_guard_depth = 16
    root = []
    start = ("l", 0) if 0 in bodies else ("b", 0)
    stack = [("emit", root, None, start, 0)]
    while stack:
        task = stack.pop()
        if task[0] == "open_guard":
            _, parent_list, m, depth = task
            gbody = []
            parent_list.append(("guard", m[1], gbody))
            stack.append(("emit", gbody, parent_list, m, depth))
            continue
        _, out_list, parent_list, m, depth = task
        kind, x = m
        below = sorted(children[m], key=lambda c: pos[c[1]])
        if kind == "l":
            lbody = []
            out_list.append(("loop", x, lbody))
        else:
            out_list.append(("block", x))
        if (kind == "b" and len(below) == 1 and list(succ[x]) == [below[0][1]]
                and (out_pcs is None or set(out_pcs[x]) == {below[0][1]})):
            stack.append(("emit", out_list, parent_list, below[0], depth))
        else:
            hoist = (parent_list is not None and below
                     and (len(below) >= 2 or kind == "l" or depth >= max_guard_depth))
            if hoist:
                stack.append(("open_guard", parent_list, below[-1], depth))
                inner = below[:-1]
            else:
                inner = below
            for c in reversed(inner):
                stack.append(("open_guard", out_list, c, depth + 1))
        if kind == "l":
            stack.append(("emit", lbody, None, ("b", x), 0))
    return root



def depth(tree):
    """Nesting depth of a tree from structure(): a block is depth 0 and each guard or loop adds one."""
    best = 0
    stack = [(tree, 0)]
    while stack:
        items, d = stack.pop()
        for it in items:
            if it[0] == "block":
                best = max(best, d)
            else:
                best = max(best, d + 1)
                stack.append((it[2], d + 1))
    return best


def _sccs(nodes, adj):
    """Iterative Tarjan's algorithm over `nodes` with adjacency `adj`; returns SCCs with >= 2 nodes."""
    index, low, on_stack, stack, out = {}, {}, set(), [], []
    nxt = 0
    for start in nodes:
        if start in index:
            continue
        index[start] = low[start] = nxt
        nxt += 1
        stack.append(start)
        on_stack.add(start)
        call = [(start, iter(adj[start]))]
        while call:
            v, it = call[-1]
            for w in it:
                if w not in index:
                    index[w] = low[w] = nxt
                    nxt += 1
                    stack.append(w)
                    on_stack.add(w)
                    call.append((w, iter(adj[w])))
                    break
                if w in on_stack and index[w] < low[v]:
                    low[v] = index[w]
            else:
                if low[v] == index[v]:
                    comp = []
                    while True:
                        x = stack.pop()
                        on_stack.remove(x)
                        comp.append(x)
                        if x == v:
                            break
                    if len(comp) >= 2:
                        out.append(set(comp))
                call.pop()
                if call:
                    p = call[-1][0]
                    if low[v] < low[p]:
                        low[p] = low[v]
    return out


def split_irreducible(succ, max_factor=2, max_splits=64):
    """Make an irreducible CFG reducible by controlled node splitting (cloning the closed region R reachable from a secondary entry e without passing through primary entry h
    in an irreducible SCC, and redirecting e's external predecessors to clone[e]).

    Returns `(new_succ, orig_of)` where `orig_of[b]` is the original block index that block `b` executes
    and `new_succ[b]` preserves the exact positional successor order of `succ[orig_of[b]]`.
    Returns `(succ, list(range(len(succ))))` unchanged if `succ` is already reducible, or `None` if
    splitting would exceed `max_factor` * len(succ) blocks or `max_splits` duplications.
    """
    n0 = len(succ)
    if n0 == 0:
        return succ, []
    max_blocks = min(int(n0 * max_factor), n0 + max_splits)
    g = None
    orig = None
    while True:
        cur = succ if g is None else g
        rpo, back, preds = _order(cur)
        idom, pos = _dominators(rpo, preds)

        def dominates(a, b):
            while pos[b] > pos[a]:
                b = idom[b]
            return a == b

        if all(dominates(w, v) for v, w in back):
            if g is None:
                return succ, list(range(n0))
            return g, orig
        if g is None:
            g = [list(s) for s in succ]
            orig = list(range(n0))
        if len(g) >= max_blocks:
            return None

        # Strip dominating back-edges so nested irreducible cycles inside reducible loops surface as SCCs.
        adj = {v: [w for w in g[v] if not dominates(w, v)] for v in rpo}
        sccs = _sccs(rpo, adj)
        if not sccs:
            return None
        # Pick the smallest irreducible SCC (tie-break by earliest RPO position).
        scc = min(sccs, key=lambda c: (len(c), min(pos[x] for x in c)))
        entries = [x for x in scc if x == 0 or any(p not in scc for p in preds[x])]
        # Choose a pair (h, e) of SCC entries (e != h, e != 0) minimizing |R|, where R is the closed region of
        # SCC nodes reachable from e without passing through h. Cloning all of R at once keeps internal cycles
        # inside the clone instead of re-entering the original SCC and unrolling infinitely.
        best = None
        for h in entries:
            for e in entries:
                if e == h or e == 0:
                    continue
                region = {e}
                work = [e]
                while work:
                    u = work.pop()
                    for w in g[u]:
                        if w in scc and w != h and w not in region:
                            region.add(w)
                            work.append(w)
                key = (len(region), pos[h], -pos[e])
                if best is None or key < best[0]:
                    best = (key, h, e, region)
        if best is None:
            return None
        _, h, e, region = best
        if len(g) + len(region) > max_blocks:
            return None
        # Order cloned nodes deterministically by RPO position.
        reg_ordered = sorted(region, key=lambda x: pos[x])
        clone_of = {}
        for u in reg_ordered:
            clone_of[u] = len(g)
            orig.append(orig[u])
            g.append([])
        for u in reg_ordered:
            g[clone_of[u]] = [clone_of[w] if w in clone_of else w for w in g[u]]
        ext_preds = {p for p in preds[e] if p not in scc}
        ce = clone_of[e]
        for p in ext_preds:
            g[p] = [ce if w == e else w for w in g[p]]

def run(tree, choose, limit):
    """Interpret a tree: the blocks it executes, in order, stopping after `limit`. `choose(b)` gives the block
    that b goes to next (END to finish) and stands for whatever b stores in pc."""
    trace, pc = [], [0]
    stack = [("items", tree, 0)]
    while stack:
        frame = stack[-1]
        if frame[0] == "items":
            _, items, idx = frame
            if idx >= len(items):
                stack.pop()
                continue
            stack[-1] = ("items", items, idx + 1)
            it = items[idx]
            if it[0] == "block":
                if len(trace) >= limit:
                    break
                trace.append(it[1])
                pc[0] = choose(it[1])
            elif it[0] == "guard":
                if pc[0] == it[1]:
                    stack.append(("items", it[2], 0))
            else:
                stack.append(("loop", it[1], it[2]))
                stack.append(("items", it[2], 0))
        else:
            _, head, body = frame
            if pc[0] == head:
                stack.append(("items", body, 0))
            else:
                stack.pop()
    return trace


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.

    `structure()` wraps post-dominating join blocks after a diamond or loop in `("guard", g, ...)`. When every
    incoming path guarantees `%pc == g` (e.g. a merge block with `s_barrier` after a wave-divergent branch like
    `ms_wave_info_s3`), unwrapping the tautological guard keeps `OpControlBarrier` in uniform top-level control
    flow (avoiding an AMD Windows LLPC 0xC0000005 pipeline-creation crash in MeshEXT and preventing false
    `needs_barrier_rounds` triggers) and avoids redundant `%pc` merge phis.

    `out_pcs[b]` is the finite set of `%pc` values block `b` may write (`{END}` when `b` terminates).
    """
    root_out = []
    stack = [[collections.deque(tree), root_out, {entry_pc}, None]]
    while stack:
        frame = stack[-1]
        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]
            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 = []
            # In a structure() tree lbody[0] is always ("block", h), which overwrites cur_pc with out_pcs[h];
            # keep cur_pc | {h} defensively so back-edge iterations stay sound even on non-structure() trees.
            stack.append([collections.deque(lbody), child_dst, set(cur_pc) | {h}, ("loop", h, child_dst)])
    return root_out