Repository navigation
Compile constructor tails with destination passing and owned-node reuse - #1793
Closed
nicolas-abril wants to merge 3 commits into
Closed
nicolas-abril wants to merge 3 commits into
nicolas-abril wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Constructor tails currently wait for their children and then allocate/rebuild the parent. On sequential paths this retains constructor continuations down the recursive spine; on parallel paths results travel through the join before becoming constructor fields. This change allocates the parent first, lets children fill its fields, and reuses a consumed node when the existing ownership analysis permits it.
The selected implementation is destination passing with fused sibling continuations and owned-node reuse. It is enabled automatically in the C emitter; CPU, Metal and CUDA share the emitted runtime. The JS emitter and language/checker are unchanged.
Draft / merge blocker:
comp.tsis 67,278 tokens against the permanent 66,000-token cap. The repository gate reports that failure; the cap has not been changed. Removing the experimental switches saved 91 tokens, but another 1,278 must be removed by simplification before merging. This PR preserves the measured design for review rather than claiming the size gate passes.Transformation
For an eligible constructor tail:
the operational transformation is:
A unary constructor tail needs no sibling continuation: it stores the parent, updates the current destination and tail-jumps to its child. Self-tail calls become worker-loop iterations; cross-function tails use the existing tail-transfer machinery. Branching retains only pending sibling work. There is no subtree merging or shared scope-wide completion counter.
Chosen design: all pieces
emit_dps. The current segment and callees return one boxed value. The body is an unshared, non-native constructor whose boxed fields are exactly the call-result variables, once each and in order. Calls are statically known, have no explicit bang and no borrowed parameters. Constructor and argument arities leave room withinWIDE. Everything else keeps the existing lowering.node_allocextracts the existing size-class matching logic fromctr_build. Both ordinary constructors and destination parents use it. Arguments are captured and dead bindings processed before selecting a spare; selection happens before flushing the other spares. A possibly static node retains the existingHEAP_OFFcheck, so static storage is never overwritten. This adds no new ownership analysis or free list.WL_RETNmasks the id before dispatch. An existing destination is filled immediately and redirected to the final child's field. At an ordinary caller boundary, a two-word root frame saves the result root and its final destination. The compiler's existing function-id limit bounds the encoding.Dps~retstores the final result into its destination and returns the saved root. Its returned value is captured as a segment input beforeWL_OPENresets output registers.Dps~putframe or handler per child.TAG_DSTcontinuation identifies result delivery into the parent's field.task_deliverresolves that destination through the join's root, writes the result, and uses the existing release/acquire completion protocol. The ready join returns the root; a consumer never receives a partially filled tree.DPS_ASYNCis emitted from segment reachability so the delivery branch can disappear when unused. It is not a user-facing switch. There are no cloned destination entries, per-return root checks, cascading joins, benchmark-specific rules or additional compiler pass.Runtime measurements
Elapsed seconds, median of five measured runs after a warm-up. Each cell is production baseline → selected, followed by elapsed-time change; negative is better.
These compare the whole transformation against baseline commit
99a3d5fb42455f5c9cc75a6c2c459b2cd0a06172. Reuse's incremental contribution over fused destination passing is smaller: Metal matmul improves 4.6%, radix 2.0%, and kmeans 0.5% in the final paired batch. CPU radix improves another 2.9%/3.0% at one/eight threads.Single-thread matrix multiplication still regresses about 2.2% against production. Allocation reduction does not guarantee lower elapsed time. Bitonic's Metal result varies across batches, including a sign change in the incremental reuse comparison; the final number is not a robust universal gain. Small differences are exploratory.
Hardware: Apple M4 minis, 10 cores, 16 GiB, Apple clang 21,
-O3 -std=c11. Variants rotate/reverse order on the same leased mini for each comparison. Every run checks output. Times include process startup; Metal archives are built before timing. Metal uses eight host threads and 4 GB spans for matmul/radix, 1 GB otherwise. There is no thermal control.CPU data come from the phase2 reuse variant, whose full assembly matches the simplified candidate under clang 17 and 21. This PR's default-enabled source emits byte-identical C to that candidate for all 17 runtime benchmarks and three pure demos. Metal data come from the final paired baseline/fused/selected batch. Earlier clang 17 timing investigations are excluded from this table.
Profiles and compiler cost
Untimed, single-thread matrix instrumentation:
Reuse removes 48.7% of allocation/free calls. Relative to production, destination passing reduces matrix segment entries from 4,183,573,524 and stack writes from 5,806,776,711. These are semantic counters, not hardware instruction/cache measurements.
A fresh 100,000-node unary-chain check returns the same result with 5 explicit worker-stack words instead of 100,003. This bounds the recursive construction spine, not heap usage or all branching programs. The earlier Metal stress check also completed with the destination design where baseline exhausted its stack.
Fresh compiler timings for the default-enabled PR source use seven measured repetitions plus warm-up on the four workloads above and three pure demos. Sum of per-workload medians:
compile_book, after checkingThere is no claimed compiler-speed improvement. The choice favors runtime gains with one shared allocation helper and no specialization pass.
Tried and discarded
The four later optimizations—reuse, known-destination specialization, root-return inlining and cascading join completion—were profiled and timed individually and together. Further batches tested combinations and bounded forwarding. Comparisons below use each experiment's paired fused-DPS control unless stated otherwise.
Dps~putReuse alone is the selected additional optimization. No tested combination won on every workload/backend.
Validation