On this page

0037. Carry state and promotion

Context

Iteration scratch must be reclaimed without invalidating immutable values saved in carry state: a var declared outside a loop and assigned inside it. See ADR-0036.

Decision

Run lower → regions::place → verify. Without heap operations placement returns immediately. Whole-program return summaries converge in BTreeSet worklist order: ret_region means a heap-bearing result may allocate; ret_aliases records parameter indices reachable from the result, including through calls. Reporting summaries additionally distinguish allocations extending each parameter from unrelated fresh results, and preserve literal alternatives: appending to a literal may allocate an unrelated replacement. These facts converge with the same call-graph worklist and do not change ret_region or ret_aliases.

Scope 0 is the function scope; allocating loops gain nested scopes. A local's home starts at its declaration scope. Raise function-level Use chains reaching Return to Ret, then relax retaining consumers within each IR block only: Use, record/variant fields and Return (and sequence items when implemented). Conditional flows do not raise the source allocation's placement.

Place allocations and allocating calls in the destination home. Compute flow-insensitive provenance to a fixpoint: youngest origin scope plus parameter set; projections inherit provenance, and calls combine their destination with aliased argument origins. Literals are static; parameters outlive all scopes. A local's origin is capped at its home, because every flow into that home is allocated there or promoted; a promoted carry therefore stays append-capable. Parameters count as oldest although their storage may be younger than Ret; ret_aliases hands that case to the caller. Assigned parameters are homed at function level. Before a retaining flow into an older home insert Promote. Strings copy their visible bytes; per-layout helpers recursively copy heap-bearing fields of records and the active union payload. The interpreter treats Promote as identity. Tip append cannot reuse a source younger than its destination.

--report=promotions counts static promotion sites. It also lists retained fresh, non-accumulating replacements placed in carry regions. Replaced carry buffers remain until that region ends; this known M5 limitation is reported. Reports are derived from shared IR and never affect execution or fuel. Tip accumulation through helpers is excluded just like direct append. A helper that may also return an unrelated fresh result still reports a retained site; the presence of an aliased accumulating argument cannot suppress that result. Retaining-flow positions belong to individual IR statements, including branch tails and returns; a line/destination key cannot distinguish same-line writes. Heap-bearing types are computed once over the layout DAG, avoiding exponential expansion of repeated union payload types.

Alternatives considered

Consequences

Values in a region point only to that region or older storage. Flow-insensitive origins may over-promote temporaries. Poison/ASan checks and a promotion-removal mutation guard retaining flows; reports expose copying sites. ADR-0219 proposes amortized reclamation for qualifying single-record drivers.