Skip to content

Codegen: a closure created in a loop gets its own environment per iteration (#1471) - #1583

Merged
ehartford merged 1 commit into
mainfrom
fix-1471-loop-closure-capture
Sep 24, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1471-loop-closure-capture

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1471. Stack bottom (base main); #1486's PR stacks on this.

Spec §12.4: "For Copy values, default capture copies the value." A closure created in a loop body copies the Copy local at its own creation.

Root cause src/CodegenDispatch.w gen_closure, "Build capture struct on stack": the environment was create_entry_alloca(cap_struct_type) — one slot per closure site, hoisted to function entry. Every iteration re-stored its captures into that slot and handed out a fat pointer to it, so all closures from the site read the last iteration's values. Evidence: pre-optimize IR (WITH_DUMP_LLIR_PRE=1) of the repro has one alloca { i32 } in main's entry and one store per iteration into it; run --debug-alloc printed 20 20 20.

Fix MirLower tags the CK_CLOSURE constant (d2 = 1, printed as const closure(nodeN, in-loop) by --dump-mir) when the closure expression sits inside a loop; gen_closure then allocates the environment at the creation point, so each evaluation owns its storage for the rest of the frame. A site outside a loop keeps its entry slot. The non-escaping mark could not select the case: a direct argument is non-escaping by §12.3 even when the callee stores it (#1566).

Matrix (stage1 run --debug-alloc, all leak count=0): for binding / let in body / while counter / Copy struct / f64 / two captures / nested loop / via a user fn / let-bound in loop → 0 10 20 each (were 20 20 20); non-escaping closure in a loop 33; four spawn_os threads in a loop sum 600 (every thread ran with the last index before).

Fixtures test/behavior/behav_closure_loop_env_per_iteration.w, test/behavior/behav_closure_loop_spawn_os_index.w. analyze audit:all on the fixture: violations=0.

Audits (stage1 vs pinned seed): tools/drop_audit.w 176 cells, 0 regressions; tools/move_audit.w 73 cells, 0 vs-expected failures. All existing closure/spawn behavior, debug_alloc and compile_errors fixtures pass on stage1. Iterate tier only — no battery yet.

Filed, not fixed #1566 (direct-arg closure classified non-escaping even when the callee stores/returns it), #1567 (a closure returned from its creating function points at the dead frame — needs an env-ownership ruling).

…ration — every closure from one site shared one entry-block slot, so all saw the last iteration's value (#1471)

Spec §12.4: "`move ||` transfers ownership, which for a Copy value is a
copy." A `move` closure created in a loop body copies the Copy local at
ITS creation, so `for i in 0..3: fs.push(move () => i * 10)` makes three
closures over 0, 10, 20, and threads spawned in a loop each run with
their own index. (A non-move closure holds the iteration's local by
place; storing it beyond the iteration is a view escape, not this bug.)

Root cause (src/CodegenDispatch.w gen_closure, the "Build capture struct
on stack" block): the environment was `create_entry_alloca(cap_struct_type)`
— one slot per closure SITE, hoisted to the function entry. Each iteration
re-stored its captures into the same slot and handed out a fat pointer to
it, so every closure from the site read the final iteration's environment.
Evidence: the pre-optimize IR of the repro (WITH_DUMP_LLIR_PRE=1) has one
`alloca { i32 }` in main's entry for the site and one `store` per
iteration into it; `run --debug-alloc` printed `20 20 20`.

Fix: MirLower tags the CK_CLOSURE constant (d2 = 1) when the closure
expression sits inside a loop of the body (`loop_break_bbs` non-empty), and
gen_closure allocates such an environment where the closure is created
(`wl_build_alloca` at the insertion point), so each evaluation owns its
storage for the rest of the frame. A site outside a loop runs once per
frame and keeps its entry slot; a non-escaping closure inside a loop pays
one alloca per iteration, which the frame bounds. The non-escaping mark
could not select the case: a direct argument is non-escaping by §12.3 even
when the callee stores it (#1566), and `Vec.push`/`spawn_os` closures are
exactly the ones that must not share. `--dump-mir` prints the tag as
`const closure(nodeN, in-loop)`.

Matrix (stage1 run --debug-alloc, all leak count=0): for binding / let in
body / while counter / Copy struct / f64 / two captures / nested loop /
through a user fn / let-bound in loop, each 0 10 20 (was 20 20 20);
non-escaping closure in a loop 33; four spawn_os threads in a loop sum
600 (was every thread with the last index). Fixtures:
test/behavior/behav_closure_loop_env_per_iteration.w,
test/behavior/behav_closure_loop_spawn_os_index.w. `analyze audit:all` on
the fixture: violations=0. tools/drop_audit.w stage1 vs seed: 176 cells,
0 regressions; tools/move_audit.w: 73 cells, 0 vs-expected failures.

Not covered here: a closure returned from its creating function still
points at that dead frame (#1567), and the direct-argument escape
classification (#1566).
@ehartford
ehartford force-pushed the fix-1471-loop-closure-capture branch from 35510a5 to f96156c Compare September 24, 2026 14:59
@ehartford
ehartford merged commit a8fa679 into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1471-loop-closure-capture branch September 24, 2026 16:56
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.

Closures made in a loop capture a Copy local by place: every closure sees the last iteration's value (spec §12.4: Copy capture copies)

1 participant