Skip to content

#732: destination-passing ABI, overwrite-dependence, and copy_count - #1367

Closed
godofecht wants to merge 2 commits into
mainfrom
cursor/copy-elision-732-remaining-ebae
Closed

godofecht wants to merge 2 commits into
mainfrom
cursor/copy-elision-732-remaining-ebae

Conversation

@godofecht

@godofecht godofecht commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Linked issue or roadmap item: #732 (parent #727; first slice landed in #1338).

What changed, and why is this the smallest useful change?

The remaining #732 lowering after record-update construction: single-module functions that return a named struct write through a hidden T* __flowc_sret (bundles keep the C return type and a callee-local slot); same-storage updates run overwrite-dependence certification before in-place writes; compiler-emitted aggregate copies increment copy_count in the #740 profile and the #728 harness records that metric; the MLIR emitter skips a second field-by-field materialize on record-update lets and returns. Coverage for arrays, closures, and a 2×2 matrix record. No source-level move syntax.

Pointer-ABI sret is withheld whenever FLOWC_BUNDLE=1, not only when a later module has already filled the signature table. The first module of a bundle has sigs_len == 0; applying pointer ABI there broke compiler self-host (flowc_make_tok vs later call sites).

Closes #732

Overlap check

  • I searched open pull requests and recent commits for overlapping work.
  • This does not duplicate another active implementation, or the relationship is explained below.

Overlap / dependency notes: #1338 (merged first slice) is the base. #1354 (value-semantics / move defaults) is adjacent but does not implement destination-passing or overwrite-dependence. #728 harness already existed; this only adds copy_count.

Evidence

Baseline before this change: #1338 constructed Name { ..base, f: v } into a let, a distinct assignment, or a last-use return, but still used a C value-return ABI, wrote same-storage fields in source order (unsound for swaps), counted only copy bytes, re-materialized record updates in MLIR, and had no matrix/closure structural cases.

Targeted proof that exercises the changed behaviour:

  • Host flowc emit + cc + run: copy_elision, copy_elision_dep, copy_elision_matrix, record_update, test_copy_elision — all exit 0.
  • FLOWC_BIN=<host> ./flow tool tests/cgen/run.flow on those cases plus mem_profile, dual_ops_sugar: all PASS.
  • Bundle smoke: FLOWC_BUNDLE=1 emit of copy_elision.flow keeps Vec8 move_return(Vec8 p) and __flowc_sret_slot; single-module keeps void move_return(Vec8* __flowc_sret, ...).
  • ./flow tool tests/bench_harness/run.flow: pass=47 fail=0.
  • ./flow tool tests/scripts/mem_profile.flow: 4 passed.
  • MLIR fixture matches host emit and contains // flowc.copy_elision record-update.

Why that proof would detect the old failure or regression: a value-return ABI would miss void move_return(Vec8* __flowc_sret; a naïve same-storage write order would fail assign_same_swap / cyclic_swap; a missing copy_count would fail mem_profile.expect and the harness JSON key; pointer ABI inside a bundle would fail bootstrap regen (gen2 C does not compile).

Repository gate(s) run:

  • Host selftest: PASS
  • Host cgen goldens listed above: PASS
  • bench_harness + mem_profile script tests: PASS
  • Bootstrap regen + --verify in a follow-up commit

Generated/bootstrap state, if any: source commits only so far. compiler/bootstrap/flowc_stage_a.c is regenerated in a separate commit.

Risk

Compatibility, performance, security, lifetime, backend, or release implications:

Pointer-ABI sret is withheld for main, exported / @flow_api functions, FLOWC_LIBRARY=1, FLOWC_BUNDLE=1, functions taken as values, and externs. Cyclic overwrite graphs keep a temporary. By-value struct parameters still copy (C ABI). Expression-position updates still materialize when there is no destination.

  • No unresolved language/API design decision is being implemented implicitly.
  • No unrelated generated artifacts or local state are included.
  • Docs changed when user-visible behaviour or syntax changed.
  • docs/project/Questions.md updated if this needs a language-design decision.

Compatibility

  • No user-visible compatibility change.
  • Compatibility change is documented in docs/project/CHANGELOG.md.

Single-module C ABI for functions that return a named user struct becomes void foo(T* __flowc_sret, args). Exported / library / bundle / value-taken functions keep the previous value-return signature.

Agent handoff

Agent/provider/session: Cursor cloud agent.

Any remaining blocker is recorded in GitHub rather than only in local/session state: none. Acceptance from #732 is met for representative matrix/record pipelines (zero avoidable full-object copies on let/sret paths), copy_count is a tracked #728 metric, and no source-level move syntax is required.

Open in Web Open in Cursor 

Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s, functions with a forward declaration or
an overload, keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
The pointer ABI no longer depends on w.sigs_len, which grows during
emission and let a prototype and its definition disagree.
Fixed point at gen2; bootstrap_from_c.sh --verify passes.
@godofecht
godofecht force-pushed the cursor/copy-elision-732-remaining-ebae branch from 19814a5 to dbbbb5b Compare October 7, 2026 17:58
@godofecht

Copy link
Copy Markdown
Contributor Author

Prepared for merge on main f313b641 (source 5f800f23, bootstrap dbbbb5b3).

Rebase conflicts in cgen.flow with the span data temps (#729/#1383) and the noalias clone flag (#731): the prologue now emits the sret slot, then the span temps, then the body; CgenBuf keeps na_clone, bounds_hi_* and sret.

Bugs found and fixed while qualifying (the PR as written failed roundtrip.flow):

  1. Separate module compilation broke: lexer_flowc.c called exported Token functions with the value-return signature while their own module emitted the pointer-sret one ("too few arguments, expected 7, have 6"). export functions now keep the plain C signature.
  2. region.flow declares ri_region_of_expr body-less for mutual recursion; the prototype from that declaration and the sret definition disagreed ("conflicting types"). A function whose name has a forward declaration or an overload keeps the plain signature.
  3. The pointer-ABI decision read w.sigs_len, which grows during emission, so a prototype and its definition could disagree. The check is gone; 1 and 2 cover the cross-module cases, and bundle and library modes still opt out.
  4. roundtrip.flow's stage_a_infer_struct check now also accepts Pair p; + make_pair(&p, 20, 22);.

Goldens re-recorded on top of #1418 (which also changed record-update MLIR): compiler/fixtures/mlir/record_update.mlir, the corpus digests of the three copy-elision programs plus the two new ones (no other corpus rows touched), tests/mlir_commands/expected/mlir_record_update.mlir, and the compile_hello / compile_isolated_root / python_source CLI .files hashes (darwin updated with --update; linux had the same hashes before and gets the same new ones).

Gates: selftest PASS; --regen fixed point gen2; --verify PASS; roundtrip ALL PASS; self_host_full ALL PASS; tests/cgen pass; test-lang pass; test-runtime pass; parity_mlir PASS (24/24 fixtures run through MLIR); tests/mlir_commands 88/0; parity_lowering pass; tests/bench_harness pass; tests/scripts/mem_profile pass; tests/cli 152/2 after the golden update (jit_hello, test_mlir: drift on main).

godofecht added a commit that referenced this pull request Oct 7, 2026
A record update `Name { ..p, f: v }` in expression position whose base
`p` is a uniquely owned function-scoped let or by-value parameter, at
its last use, mutates the dead binding and yields it instead of copying
into a temporary. Uniqueness and last use fail closed (captures, taken
addresses, loops that may iterate again, pending defers, unknown
statement forms all keep the copy). Observable value semantics do not
change. No move keyword and no copy-on-write, per the 2026-10-07
decision; this merges after #1367.

Rebased from PR #1354 onto #1367. The cgen block conflicts were resolved
by inserting this PR's three hunks at their anchors in the #1367 file
(the textual 3-way merge misaligned the two new function blocks). The
PR's open question in Questions.md is dropped because main records the
decision; value-semantics.md links to that entry.
godofecht added a commit that referenced this pull request Oct 7, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
godofecht added a commit that referenced this pull request Oct 7, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
godofecht added a commit that referenced this pull request Oct 7, 2026
A record update `Name { ..p, f: v }` in expression position whose base
`p` is a uniquely owned function-scoped let or by-value parameter, at
its last use, mutates the dead binding and yields it instead of copying
into a temporary. Uniqueness and last use fail closed (captures, taken
addresses, loops that may iterate again, pending defers, unknown
statement forms all keep the copy). Observable value semantics do not
change. No move keyword and no copy-on-write, per the 2026-10-07
decision; this merges after #1367.

Rebased from PR #1354 onto #1367. The cgen block conflicts were resolved
by inserting this PR's three hunks at their anchors in the #1367 file
(the textual 3-way merge misaligned the two new function blocks). The
PR's open question in Questions.md is dropped because main records the
decision; value-semantics.md links to that entry.
godofecht added a commit that referenced this pull request Oct 8, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
godofecht added a commit that referenced this pull request Oct 8, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
godofecht added a commit that referenced this pull request Oct 8, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
godofecht added a commit that referenced this pull request Oct 8, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
godofecht added a commit that referenced this pull request Oct 8, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
godofecht added a commit that referenced this pull request Oct 8, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
godofecht added a commit that referenced this pull request Oct 8, 2026
Single-module functions that return a named struct take a hidden
`T* __flowc_sret` destination; in bundle mode (FLOWC_BUNDLE=1) the
callee keeps a local sret slot so the pointer ABI is not exposed across
modules. Record updates whose destination overlaps a source read go
through a temporary (overwrite dependence); others build in place.
The runtime profile and the #728 harness report copy_count (aggregate
copy operations) next to copies (bytes).

Rebased from PR #1367 onto current main. cgen conflicts with the span
data temps (#729/#1383) and the noalias clone flag (#731): the function
prologue emits the sret slot, then the span temps, then the body; the
buffer keeps na_clone, bounds_hi_* and sret. Doc and harness comment
conflicts keep both sides.

On rebase: `export function`s keep the value-return C signature (no
pointer sret), because a module compiled on its own (roundtrip.flow,
stage_a_self_emit) only sees that declaration; the PR as written failed
roundtrip with "too few arguments" in lexer_flowc.c. The record-update
MLIR goldens (fixture, corpus digests for the three copy-elision
programs, mlir_commands) are re-recorded on top of #1418; parity_mlir
runs all 24 fixtures through MLIR and they pass.
@godofecht godofecht closed this Oct 8, 2026
godofecht added a commit that referenced this pull request Oct 8, 2026
A record update `Name { ..p, f: v }` in expression position whose base
`p` is a uniquely owned function-scoped let or by-value parameter, at
its last use, mutates the dead binding and yields it instead of copying
into a temporary. Uniqueness and last use fail closed (captures, taken
addresses, loops that may iterate again, pending defers, unknown
statement forms all keep the copy). Observable value semantics do not
change. No move keyword and no copy-on-write, per the 2026-10-07
decision; this merges after #1367.

Rebased from PR #1354 onto #1367. The cgen block conflicts were resolved
by inserting this PR's three hunks at their anchors in the #1367 file
(the textual 3-way merge misaligned the two new function blocks). The
PR's open question in Questions.md is dropped because main records the
decision; value-semantics.md links to that entry.
@godofecht
godofecht deleted the cursor/copy-elision-732-remaining-ebae branch October 12, 2026 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance #727] Elide aggregate/value copies after move inference in generated C

1 participant