Skip to content

Commit ee9a047

Browse files
authored
Merge pull request #501 from thisisthepy/feat/tp-o2b-regions
TypedPython AOT tier O2 step 2: scope regions (N-12b/N-12c)
2 parents 17774bc + f2abb8f commit ee9a047

9 files changed

Lines changed: 1519 additions & 49 deletions

File tree

‎.github/workflows/typedpython.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ jobs:
145145
run: |
146146
env $(.venv/bin/python tools/sanitizers.py env) \
147147
.venv/bin/python -m pytest -q -m "not measurement and not race_mutant" \
148-
src/test/python/test_cgen.py src/test/python/test_runtime.py src/test/python/test_pipeline.py src/test/python/test_interval_proofs.py src/test/python/test_virtual_objects.py src/test/python/test_aot_build.py src/test/python/test_aot_lib.py src/test/python/test_aot_int.py src/test/python/test_aot_repr.py src/test/python/test_aot_tier_o1.py src/test/python/test_aot_tier_o2a.py
148+
src/test/python/test_cgen.py src/test/python/test_runtime.py src/test/python/test_pipeline.py src/test/python/test_interval_proofs.py src/test/python/test_virtual_objects.py src/test/python/test_aot_build.py src/test/python/test_aot_lib.py src/test/python/test_aot_int.py src/test/python/test_aot_repr.py src/test/python/test_aot_tier_o1.py src/test/python/test_aot_tier_o2a.py src/test/python/test_aot_tier_o2b.py
149149
150150
app-fixture:
151151
# #45: a TypedPython-compiled module calling Kotlin through the upcall table, in the app

‎docs/SPEC.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,33 @@ All in `PM/commonTest`, so they run wherever the interpreter loads.
15011501
(return, call, non-inlined method call, identity `is`, alias, reassignment/branch merge, or
15021502
temporary), plus `typedpython.aot.explain_alloc(...)` and `python -m typedpython aot-build
15031503
--explain-alloc`.
1504+
- **Tier O PR-O2b (N-12b/c step 2, scope regions)** (agent proposal, not owner-confirmed): adds scope
1505+
bump regions (`aot/alloc.py` region analysis, `tpa_region` in `runtime/tp_aot.h`). In a function `F`,
1506+
every allocation site of a class `C` that is not virtual, where `C` has a simple `__init__`, no
1507+
`__del__` and at least one field (a class with no fields stays heap-rc: a zero-size region object
1508+
would give every object one address, and `is` must tell them apart), is region-allocated (a distinct C type `struct tpa_reg_<F>__<C>` with no reference count,
1509+
bump-allocated in one region per `(F, C)` that `F`'s body owns, or one iteration of one loop of `F`
1510+
owns, and freed together at `tp_cleanup:` on every exit: the end of the body, an early `return`, a
1511+
failing operation; a per-iteration region is released and reset at the top of every iteration) if and
1512+
only if, in `F`: no region pointer of `C` is returned (R1), stored into a heap object, a virtual
1513+
object, a container or a region object whose region outlives `C`'s (R2), or passed to any call, a
1514+
method call included (R3); no heap pointer of `C` (a parameter, a call result, a field of a heap or
1515+
virtual object) is assigned to a local of `C` or stored into a region object's field of class `C`
1516+
(so a local of `C` has exactly one C type); and `is` never compares a region pointer with a heap
1517+
pointer. A region object's field of class `K` is a region pointer when `(F, K)` is region, else an
1518+
owned heap reference; owned heap references held by region objects (lists, big ints, heap-rc objects)
1519+
are released by a per-class walk when the region is freed or reset (R4 relaxed to that; a region
1520+
pointer is never stored where a heap object could reach it). The scope is the innermost loop whose
1521+
body holds every use of `C` in `F` and in which every local of `C` is definitely assigned before it
1522+
is read, widened to the loop of any region object that holds `C` pointers, else the function body.
1523+
Region pointers never reach a library export (R5 holds by R1 and R3). `--explain-alloc` reports
1524+
`region (function scope)` or `region (per-iteration scope of the <for|while> loop, depth <k>)` for a
1525+
region site and, after a heap-rc site's reason, `[region: R1 ...]`. The escape rules see every
1526+
sub-expression of a statement, the length and fill of a `[v] * n` and the callee and arguments of
1527+
an object call included.
1528+
Not in this step: region specialisations of callees (R3 with `make__reg` / `check__reg`), so
1529+
`benchmarks/typedpython/py/binary_trees.py` stays heap-rc (`[region: R1: escapes via return]`), and
1530+
the cycle collector (step 3).
15041531

15051532
---
15061533

‎docs/design/typedpython-aot.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,28 @@ matches CPython.
450450
exit block, covering both normal return and error (`rc != 0`) paths. Exceptions in AOT carry only
451451
`(class_id, message_string)` in the thread-local error record (`tpa_exc`), with no traceback frame
452452
objects (owner decision D4), so a region pointer can never be retained by an exception or traceback.
453+
- *Step 2 as implemented (PR-O2b, N-12b/c; agent proposal, not owner-confirmed)*: regions are
454+
intraprocedural. The unit of the decision is `(F, C)`: every non-virtual site of `C` in `F` and every
455+
local of `C` in `F` are region-typed together (`struct tpa_reg_<F>__<C>`, one region per `(F, C)`),
456+
or none is, so a local has one C type. R1 and R2 as written (a store into a virtual object counts as
457+
a heap store); R3 in its strict form, no callee at all (region specialisations such as `check__reg`
458+
are a later step, so `binary_trees.py` stays heap-rc with the reason reported); R4 relaxed: a region
459+
object's field of class `K` is a region pointer when `(F, K)` is region and an owned heap pointer
460+
otherwise, and lists and big ints are owned heap references; a generated per-class walk releases
461+
them when the region is freed or reset, which keeps "no heap object points into a region" and adds
462+
"a region object's heap references are released". The scope is the function body or one iteration of
463+
one loop (the innermost loop whose body holds every use of `C`, in which every local of `C` is
464+
definitely assigned before it is read, widened to the loop of any region object that holds pointers
465+
of `C`); a per-iteration region is reset at the top of each iteration and freed at `tp_cleanup:`,
466+
so `break`, `continue`, `return` and a failing operation need no path-specific code. Heap pointers of
467+
`C` flowing into `F` (a parameter, a call result, a field of a heap or virtual object) assigned to a
468+
local of `C`, or compared with `is` against a region pointer, make `(F, C)` heap-rc. A class with
469+
no fields is never region (review of #501): its region struct would be empty, every
470+
`tpa_region_alloc` of size 0 returns the same address, so `a is b` would be true for two objects
471+
(and MSVC rejects an empty struct); heap-rc is correct and costs nothing anyone measures. The
472+
expression walkers of `aot/alloc.py` and `aot/classes.py` share one list of child attributes
473+
(`EXPR_CHILD_ATTRS` / `EXPR_CHILD_SEQS`), so a node such as `NewArray` (`length`, `fill`) or
474+
`CallObject` (`callee`) cannot be skipped by one walker and seen by another.
453475
3. **Reference counting + small cycle collector (D2) + acyclic `__del__`.**
454476
- *Pay only for what the program uses*: classes proved acyclic by the shared `aot/classes.py`
455477
analysis (PR-O1) keep zero GC overhead and do not link `runtime/tp_aot_gc.h`. Only when a program

0 commit comments

Comments
 (0)