Skip to content

Recursion

Reject Gemini kernels whose call graph contains a cycle.

A Gemini kernel is lowered to a fixed sequence of physical atom moves, so a recursive call graph has no finite lowering and cannot be compiled. Rejecting it is therefore not a limitation we are working around but the correct answer.

Failing early matters for a second reason: every pass downstream diverges on a cyclic call graph rather than erroring. AddressAnalysis re-analyses each callee at every call site with no memoisation, so a branching cycle costs phi ** max_depth interpreter calls -- and because kirin's depth guard returns bottom instead of raising, the compile presents as an unkillable hang with no diagnostic. See bloqade-lanes#921 and bloqade-circuit#852.

CallGraph below is deliberately self-contained rather than reusing kirin.analysis.CallGraph, which recurses without a visited set and raises RecursionError on precisely the inputs this module exists to diagnose (kirin#703). It also differs in two ways worth keeping if this is upstreamed: edges are keyed caller -> callee (matching how they read), and callees are found through the ir.StaticCall trait rather than an isinstance check against func.Invoke, so any dialect that declares the trait participates.

CallGraph dataclass

CallGraph(mt: Method)

Static call graph rooted at entry, keyed caller -> callees.

Construction is an iterative worklist, so a recursive kernel produces a graph containing a cycle instead of exhausting the Python stack.

Only statically resolved edges are followed. A dynamic func.Call through an SSA value is invisible here: resolving one needs the same constant propagation that diverges on cyclic input, so it cannot be used by a guard whose job is to run first.

ir.Method.backedges looks like it should make this class unnecessary, but cannot be used for any of the three things needed here:

  • It is empty when this runs. update_backedges is called from Method.__init__, which completes only after run_pass returns, so at guard time every method's backedges is still an empty set.
  • It goes stale. It is populated once at construction and never invalidated, so after the inliner has spliced a callee away the edge is still recorded. That matters for the post-fold re-check, which would then see cycles that no longer exist.
  • It points the wrong way. It records callers, but validating a kernel asks what that kernel reaches, rooted at the kernel itself.

It also derives from the same ir.StaticCall trait, so it would miss the unresolved self call for the same reason a plain forward walk does.

Source code in .venv/lib/python3.12/site-packages/bloqade/gemini/common/validation/recursion.py
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
def __init__(self, mt: ir.Method) -> None:
    self.entry = mt
    self.edges = {}

    worklist = [mt]
    while worklist:
        caller = worklist.pop()
        if caller in self.edges:
            continue

        callees: set[ir.Method] = set()
        self_value = _self_value(caller)
        for stmt in caller.code.walk():
            if trait := stmt.get_trait(ir.StaticCall):
                callees.add(trait.get_callee(stmt))
                continue

            if (
                self_value is not None
                and isinstance(stmt, func.Call)
                and stmt.callee is self_value
            ):
                # Not-yet-resolved self call; record it as a real self edge.
                callees.add(caller)
                continue

            for operand in _callable_operands(stmt):
                if self_value is not None and operand is self_value:
                    callees.add(caller)
                elif (callee := _constant_method(operand)) is not None:
                    callees.add(callee)

        self.edges[caller] = callees
        worklist.extend(callee for callee in callees if callee not in self.edges)

callees

callees(method: Method) -> list[ir.Method]

Callees of method, in a deterministic order.

Source code in .venv/lib/python3.12/site-packages/bloqade/gemini/common/validation/recursion.py
227
228
229
def callees(self, method: ir.Method) -> list[ir.Method]:
    """Callees of ``method``, in a deterministic order."""
    return sorted(self.edges.get(method, ()), key=_sort_key)

find_cycles

find_cycles() -> list[Cycle]

Return one representative cycle per distinct set of mutual callers.

An empty list means the call graph is acyclic. The traversal is an explicit-stack DFS so that deeply nested (but acyclic) call graphs stay safe too.

Source code in .venv/lib/python3.12/site-packages/bloqade/gemini/common/validation/recursion.py
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
def find_cycles(self) -> list[Cycle]:
    """Return one representative cycle per distinct set of mutual callers.

    An empty list means the call graph is acyclic. The traversal is an
    explicit-stack DFS so that deeply nested (but acyclic) call graphs stay
    safe too.
    """
    cycles: list[Cycle] = []
    reported: set[frozenset[ir.Method]] = set()
    visited: set[ir.Method] = set()

    for root in self._roots():
        if root in visited:
            continue

        visited.add(root)
        path = [root]
        on_path = {root}
        stack: list[tuple[ir.Method, Iterator[ir.Method]]] = [
            (root, iter(self.callees(root)))
        ]

        while stack:
            _, pending = stack[-1]
            descended = False

            for callee in pending:
                if callee in on_path:
                    # A back edge: `callee` is an ancestor of the node being
                    # expanded, so the walk has come back around to it. Cut
                    # the path at that ancestor -- from it onwards is the
                    # cycle, and everything before it is how `entry` reaches
                    # the cycle:
                    #
                    #     path = [main, a, b], callee = a
                    #       -> route   = (main,)
                    #       -> members = (a, b)   # b calls a, closing it
                    #
                    # Located by identity: `Method.__eq__` is generated by
                    # `@dataclass` while its `__hash__` is `id`, so `index`
                    # would compare fields where only identity is meant.
                    start = next(i for i, m in enumerate(path) if m is callee)
                    members = tuple(path[start:])

                    # Keyed on the member set, so one group of mutually
                    # recursive methods is reported once no matter how many
                    # back edges lead into it.
                    if (key := frozenset(members)) not in reported:
                        reported.add(key)
                        cycles.append(
                            Cycle(route=tuple(path[:start]), members=members)
                        )

                    # Recorded, never followed: descending into an ancestor
                    # is precisely what would loop forever.
                    continue

                if callee in visited:
                    continue

                visited.add(callee)
                path.append(callee)
                on_path.add(callee)
                stack.append((callee, iter(self.callees(callee))))
                descended = True
                break

            if not descended:
                stack.pop()
                on_path.discard(path.pop())

    return cycles

Cycle dataclass

Cycle(
    route: tuple[Method, ...], members: tuple[Method, ...]
)

A cycle, plus how the validated kernel reaches it.

members is [m0, ..., mn] where mn calls m0. route is the path from the entry method down to m0, excluding the cycle itself, and is empty when the entry method is part of the cycle.

The route matters because the cycle is often not in the kernel being validated. Any Gemini kernel containing a cycle is rejected at its own definition, so it can never become a callee -- but a kernel built with another dialect group (squin.kernel, say) runs no such guard, and a Gemini kernel may invoke one. Then the cycle sits one or more hops away and naming it alone would not say how it is reached.

NoOpaqueCallValidation dataclass

NoOpaqueCallValidation()

Bases: ValidationPass


              flowchart TD
              bloqade.gemini.common.validation.recursion.NoOpaqueCallValidation[NoOpaqueCallValidation]

              

              click bloqade.gemini.common.validation.recursion.NoOpaqueCallValidation href "" "bloqade.gemini.common.validation.recursion.NoOpaqueCallValidation"
            

Reject calls made through one of the kernel's own parameters.

CallGraph can only see statically resolved edges, so a call through a value is a hole in it: hand a kernel in as a value and call it and the result is a cycle no amount of graph walking will find, costing phi ** max_depth in the address analysis once the branching factor reaches two. Resolving the target instead would need the constant propagation that diverges on exactly that input, so the shape is rejected rather than resolved.

Both a func.Call and a higher-order statement's callable operand count: ilist.Map and friends invoke their fn without being a func.Call.

Known gap. Matching the parameter object is bypassed by routing the callable through anything at all before calling it -- an if/else makes it a block argument of a successor block, a list makes it a getitem result -- and neither is the parameter, so neither is caught. Rejecting every unresolved call instead was tried and is not viable: after inlining, a perfectly ordinary stdlib kernel calls a self value belonging to an inlined region rather than to the method being scanned, so it is reported as opaque and the physical pipeline stops compiling. Closing this properly needs provenance for callable values, which is the analysis scoped in #927.

Every statically reachable method is scanned, not just the entry. Checking only the entry leaves the hang reachable through a callee: a Gemini kernel can statically invoke a kernel from another dialect group, which never ran this guard and may make an opaque call of its own.

NoRecursionValidation dataclass

NoRecursionValidation()

Bases: ValidationPass


              flowchart TD
              bloqade.gemini.common.validation.recursion.NoRecursionValidation[NoRecursionValidation]

              

              click bloqade.gemini.common.validation.recursion.NoRecursionValidation href "" "bloqade.gemini.common.validation.recursion.NoRecursionValidation"
            

Report every recursive cycle reachable from the validated kernel.

check_call_graph

check_call_graph(method: Method) -> None

Require method's call graph to be statically resolvable and acyclic.

Raises ValidationErrorGroup listing every cycle and every opaque call found, rather than stopping at the first.

Callers should run this before any pass that walks callees, and must not gate it on a verify flag. A cyclic call graph has no finite lowering, and the inliner and address analysis diverge on one instead of erroring, so skipping the check turns a clear rejection back into an unkillable hang (bloqade-lanes#921).

Source code in .venv/lib/python3.12/site-packages/bloqade/gemini/common/validation/recursion.py
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
def check_call_graph(method: ir.Method) -> None:
    """Require ``method``'s call graph to be statically resolvable and acyclic.

    Raises ``ValidationErrorGroup`` listing every cycle and every opaque call
    found, rather than stopping at the first.

    Callers should run this *before* any pass that walks callees, and must not
    gate it on a `verify` flag. A cyclic call graph has no finite lowering, and
    the inliner and address analysis diverge on one instead of erroring, so
    skipping the check turns a clear rejection back into an unkillable hang
    (bloqade-lanes#921).
    """
    ValidationSuite([NoRecursionValidation, NoOpaqueCallValidation]).validate(
        method
    ).raise_if_invalid()

format_cycle

format_cycle(cycle: Cycle | list[Method]) -> str

Render a cycle as a -> b -> a, closing the loop for readability.

A cycle reached from elsewhere also reports the route, as b -> b (reached via main -> a -> b).

Source code in .venv/lib/python3.12/site-packages/bloqade/gemini/common/validation/recursion.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def format_cycle(cycle: Cycle | list[ir.Method]) -> str:
    """Render a cycle as ``a -> b -> a``, closing the loop for readability.

    A cycle reached from elsewhere also reports the route, as
    ``b -> b (reached via main -> a -> b)``.
    """
    if isinstance(cycle, Cycle):
        members, route = list(cycle.members), list(cycle.route)
    else:  # a bare list of members, for convenience at the call site
        members, route = list(cycle), []

    loop = " -> ".join([_name(method) for method in members] + [_name(members[0])])
    if not route:
        return loop

    via = " -> ".join(_name(method) for method in [*route, members[0]])
    return f"{loop} (reached via {via})"