0043. Destination passing for large results and the copies report
- Status: Accepted
- Date: 2026-10-31
- Plan: M5.6
Context
M5.6 promises that a record or union returned from a function is built where the caller wants it, and that tests can pin this. C compilers usually elide such copies, but only as an unobservable optimization. Interpreter and C must agree, and programs have no way to observe a copy, so the guarantee lives in the IR.
Decision
regions::destinations runs after placement. A
function whose record or union result exceeds 16 bytes
(LayoutSizes, mirroring C layout; a 24-byte sequence
handle counts as 24) has dest: it returns
void and takes T *restrict hrt_dst
first, then any hrt_ret. Results of at most 16 bytes,
option<i64> included, return in registers
unchanged.
Func::slots lists locals stored directly in
*hrt_dst:
- a local bound by the statement immediately before the
Returnof it, in every branch; and - the one remaining local, if a single non-parameter local is
all that the other
Returns return.
The plan named only the second rule. The first is needed so
that functions with several
return <literal or call> statements, such as
the base and recursive cases of dest-recursive, are
zero-copy. A bound-then-returned local has no lifetime that could
overlap another slot's: its right side is fully evaluated, then
control leaves.
Slots are safe because nothing but the caller can name
hrt_dst, the caller cannot look until the call
returns, a trap never returns, and every argument is passed by
value, so p = f(p) reads its own copy. Reassigning a
slot is assigning a variable. Decl of a slot emits
nothing.
The interpreter is untouched: dest and
slots are backend storage facts.
Slot invariants, all relied on by cgen and enforced or structural:
- Only
LetandSetwrite locals, and each stores a whole value ((*hrt_dst) = <value>or a destination call); no partial slot write exists. Arguments, including fields of a slot, are copied before a call, sohn_g(hrt_dst, t, (*hrt_dst))reads before it writes. - Slots are ordinary block-scoped locals; a bound-then-returned
slot is dead after its
Return. verifyrejects a function in which more than one slot is written other than by aLetdirectly followed byReturnof it, so at most one slot is live across statements.dest-slot-read-as-arganddest-slot-match-trypin the behaviour against the interpreter.
Vectors, str and bytes remain by-value 24-byte handles
(data, len, cap on the
supported 64-bit C targets), not destination-pointer results.
LayoutSizes includes these sizes and alignment when
laying out enclosing records/unions. For a freshly built returned
vector, M5.2 places the backing buffer in ret:
returning or forwarding its handle does not copy that buffer.
Scratch values escaping an iteration may still require promotion;
the guarantee does not remove construction work such as literal
initialization or push-capacity growth.
--report=copies reads the IR, as a static site
count: every Promote, every Concat with
in_place = false (tip-miss),
Let/Set of a local, a Field
or a Payload yielding a type over 16 bytes
(aggregate), and Return of a large
non-slot local (return). These last two kinds cover
records/unions only; sequence-handle moves are uncounted.
set counts each VecSet, including a set
returned directly or assigned to a distinct local. The regions
pass rewrites proven whole-function unaliased self-sets to
VecSetUnique (ADR-0040); those do not copy a buffer
and are excluded. Reporting consumes that proof rather than
implementing a second alias analysis. Not counted: by-value
arguments and record or variant literals that embed large fields
(C copies them, the report does not).
copies-return-vec-built and
copies-return-vec-loop pin zero static copy sites and
zero promotions. Runtime stats pin 8 and 0 copied bytes
respectively: the former copies one i64 prefix while growing the
pushed vector; neither copies backing storage on return.
copies-set pins three copying sites and 72 bytes
(three three-i64 buffers), excluding the proven self-set loop.
These fixtures match the interpreter under plain gcc and
poison/ASan/UBSan; dest-vec-record also exercises
returned records/unions holding vectors and loop scratch churn.
copies-return-vec-map waits for M5.5 non-escaping
blocks; the explicit-loop fixture covers the available
construction path without claiming map support.
Consequences
--digestmoves for exactly the twelve rows with such a function and no others:bench/generics,bench/inputs/generics-100.hn, andtests/programs/generic-from-expected,generic-struct,try-chain,try-changes-ok-type,try-contexts,try-in-args-order,try-in-generic,try-in-interpolation,try-result,try-skips-later-effects. Theirresult<i64, str>and generic-pair layouts exceed 16 bytes. Program outputs do not change.ir 1gains an optionaldestline. The plan expected existing snapshots to stay identical, but printing the flag is whatdest-flagtests, sogeneric-layoutsandtry-resulteach gain it. No other line moves.expected.copiesplaceholders written in M5.2 become asserted. Four change: three fixtures contain aPromote, andretained-constructorhas real aggregate moves.- A
matchon a large local snapshots it first, because arms may reassign the source. That is a real copy and is reported asaggregate; the snapshot is out of M5.6's scope, andcopies-return-matchmatches a 16-byte union. - Runtime is neutral under gcc
-O2onbench/dest, because the x86-64 ABI already returns such structs through a hidden pointer. The value is a guarantee that does not depend on the optimizer, and a report that can pin it.
Alternatives rejected
- Only the plan's single-slot rule: literal returns on two paths would report copies that do not exist and would be emitted as ones.
- Rewriting return-bound
Lets to a newRvalueform: changes every backend and IR snapshot for no extra precision.