Files
Artem Chikin 11c74f2ab8 [NFC][SILOptimizer] Add ClosureGroupSchedule
AccessEnforcementSelection and DiagnoseStaticExclusivity are module passes because each needs a function and the non-escaping closures it captures into at the same pipeline position. Other mandatory module passes, such as CapturePromotion and MandatoryAllocBoxToStack, need the same for closures that capture a box.

ClosureGroupSchedule partitions a module's function definitions into closure groups that keep every closure with its scopes, and orders the groups callees first. An upcoming change will add SILClosureGroupTransform, which runs on one group at a time.

A closure link is a function_ref from a function (the scope) to a closure definition, a function that SILGen marks [closure] when it creates it. A link doesn't depend on what the closure captures or on how the reference is used, so escaping closures, closures that capture a box or a value, and closures that capture nothing all link to their scopes. Thunks never link, because a shared one would join unrelated functions.

A closure group is a connected component of the closure links. Usually it is one function and its closures, but scopes that share a closure share its group. Members keep their bottom-up order.

A capture never leaves its group. Only a closure receives one (a box or an @inout_aliasable address), and the function that passes it links to that closure, so a pass that follows a capture into the callee stays inside the group. SILGen output satisfies this, and the passes assert it; hand-written SIL must mark such a callee [closure]. No pass follows a capture through a dynamic_function_ref, so a dynamic_function_ref is not a link.

Groups run callees first along the call graph that BottomUpFunctionOrder follows (full applies, the destructors a release may run, and builtin "once" initializers), so a group pass that reads a callee in another group finds that callee already processed, as in the function pass pipeline. Groups on a call cycle have no callees-first order; they run in bottom-up order of their first members, and stay separate rather than merging. Every order derives from the bottom-up order, so the schedule is deterministic.

The constructor numbers the definitions in bottom-up order, scans every instruction once to build the closure components (llvm::IntEqClasses) and record the call edges, contracts the call graph to one node per component, and walks its SCCs callees first with llvm::scc_iterator. Each component is its own group; the components of one SCC go in bottom-up order.

Nothing uses the schedule yet.
2026-11-24 09:46:45 +00:00
..