@@ -368,8 +368,9 @@ review 2026-11-20, not owner-confirmed).
368368 2 . * Immutable-after-init* : ` C ` (and every class reachable in its SCC) is proved immutable-after-init by
369369 the shared analysis above (so every reference points strictly to an already-constructed object,
370370 forming a causal DAG, as in ` binary_trees.py ` 's ` Node ` ).
371- Any class that fails both conditions is a compile error (` aot/cycle-possible ` , naming the class and
372- the mutating field store outside ` __init__ ` ) until PR-O2 adds the small cycle collector.
371+ Any class that fails both conditions was a compile error (` aot/cycle-possible ` , naming the class and
372+ the mutating field store outside ` __init__ ` ) until PR-O2c added the small cycle collector (6.3.2 item
373+ 3, "Step 3 as implemented"): such a class now compiles, tracked by the collector.
373374- ** Reference counting and elision rules.** Ownership follows the same convention as native lists and
374375 ` tpa_int ` : parameters are ** borrowed** ; locals, return values and struct fields are ** owned** .
375376 Elision is permitted only under the following statically proved conditions, and every other site emits
@@ -489,12 +490,56 @@ matches CPython.
489490 borrowed reference.
490491 - * ` __del__ ` * : permitted only on acyclic classes (always exact RC, no virtual/region/elision). A
491492 ` __del__ ` on a cyclic-capable class is a compile error (` aot/cycle-del ` ).
493+ - * Step 3 as implemented (PR-O2c; agent proposal, not owner-confirmed)* : the cyclic-capable classes
494+ are every class of a strongly connected component of the type graph (edges ` C -> D ` for a field of
495+ type ` D ` or ` D | None ` ) that has a cycle and at least one member not immutable-after-init: the
496+ whole component, not only its mutable members, because trial deletion subtracts only the references
497+ between tracked objects, and an untracked class on a cycle would hold a reference that is never
498+ subtracted (a root), so the cycle would never be found. An untracked class cannot be on a cycle (its
499+ component has none, or is immutable-after-init throughout); its reference to a tracked object is a
500+ root while it lives, and it is freed by its count. The header is ` tpa_gc_head ` (` runtime/tp_aot_gc.h ` :
501+ ` gc_prev ` , ` gc_next ` , ` gc_refs ` , and ` gc_type ` , the class's traverse and clear functions, one word the
502+ list above did not have), the first member of ` struct tpa_cls_<C> ` before ` rc ` , so the collector reads
503+ the count through the common prefix ` tpa_gc_object ` . A region struct ` tpa_reg_<F>__<C> ` and a virtual
504+ object of a cyclic-capable class are unchanged: a region pointer is never counted and never reaches
505+ the heap (R2), so a cycle inside a region is freed with the region, and a region object's or virtual
506+ object's heap fields are counted (PR-O2a/b), so they are roots. The collector is CPython's one
507+ generation of trial deletion over the list of every tracked object alive: ` gc_refs = rc ` (an atomic
508+ load under ` TPA_THREADS ` ); for every tracked object, ` traverse ` visits its fields of cyclic-capable
509+ classes and decrements each referent's ` gc_refs ` ; an object left with ` gc_refs > 0 ` is a root
510+ (something outside the tracked set holds it) and the objects it reaches are moved back to the live
511+ list; the rest are garbage: each is held by one extra count while its ` clear ` releases every owned
512+ field (tracked objects, acyclic objects, lists, big ints; the slot is emptied before the release) and is
513+ freed when its count reaches zero (at once, or when the garbage that still held it is cleared). No
514+ user code runs during a collection (no ` __del__ ` in AOT), so it cannot re-enter; a flag guards it
515+ anyway. The safepoint is ` tpa_cls_<C>_new ` of a cyclic-capable class, before ` calloc ` : every
516+ constructor argument is then an owned temporary, an owned local, a borrowed parameter whose caller
517+ holds it, or a field of a live object, and ` __init__ ` has not started; ` __init__ ` allocating another
518+ tracked object sees the half-initialised object as a root (held by ` _new ` 's own local, fields NULL or
519+ owned). The elision rules (3) and (4) are off for every field of a cyclic-capable class
520+ (` ClassAnalysis.field_elidable ` ), and ` aot/cycle-del ` is reported beside the front end's refusal of
521+ ` __del__ ` . Trigger: tracked allocations net of tracked frees since the last collection reach
522+ ` max(TPA_GC_THRESHOLD, live tracked objects after the last collection) ` (2000 by default, CPython's
523+ first-generation threshold), which bounds the memory held by garbage to one more live set and keeps
524+ the amortised cost per allocation constant; ` -DTPA_GC_STRESS ` collects at every safepoint and
525+ ` -DTPA_GC_STATS ` prints the counts at exit (tests only). One more collection runs after ` __main__ `
526+ returns, when every frame has released its locals and only cycles can remain (CPython's shutdown
527+ collection; it keeps LeakSanitizer quiet). Under ` TPA_THREADS ` the collector's state is thread-local:
528+ a tracked object is touched only by the thread that created it (` aot/library-mutable-class ` keeps
529+ mutable classes, transitively, out of the library boundary; AOT has no mutable globals), so one
530+ thread's collection never sees another thread's objects. Tier T's shared objects (section 7) must add
531+ the shared-object locking before a tracked object may be shared; this collector is not a
532+ stop-the-world collector. The library output never emitted class structs (before this step), so the
533+ collector is linked by executables only today; the runtime compiles in both builds.
4925344 . ** Diagnostics (` --explain-alloc ` / ` aot-check ` ).**
493535 - Every allocation site's chosen strategy (` virtual ` , ` region ` , ` heap-rc ` , ` heap-gc ` ) and the
494536 concrete reason for any fallback from ` virtual ` or ` region ` to ` heap ` (e.g. non-inlined method,
495537 R1 return escape, R2 outer-region/heap store, R4 heap-owned field, ` __del__ ` ) are reported via
496538 ` python -m typedpython aot-build ENTRY.py --explain-alloc ` (and surfaced in ` aot-check ` ), so
497539 measurement runs can verify why an allocation did or did not stay off the heap.
540+ - PR-O2c: a heap site of a cyclic-capable class is reported as ` heap-gc ` with, after the virtual and
541+ region reasons, ` [gc: <why the class may form a cycle>] ` (the mutating store outside ` __init__ ` , or
542+ the ` self ` escape in ` __init__ ` , as ` aot/cycle-possible ` named it).
498543
499544### 6.4 Exceptions
500545
0 commit comments