On this page

0043. Destination passing for large results and the copies report

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:

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:

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

Alternatives rejected