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_backedgesis called fromMethod.__init__, which completes only afterrun_passreturns, so at guard time every method'sbackedgesis 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 | |
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 | |
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 | |
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 | |
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 | |