Skip to content

emit-c lowers closures: env struct + static fn + the LLVM backend's {fn, ctx} pair; push and pulled generators go through the C backend (#1766) - #1959

Merged
ehartford merged 4 commits into
fix-1239from
fix-1766
Oct 2, 2026
Merged

ehartford merged 4 commits into
fix-1239from
fix-1766

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1766. Eight fixtures under test/emit_c (no-capture, by-value Copy/str, view, mutating D62, stored+called twice, passed to fn, push generator for v in nums(3), g.pull()) committed red (unsupported const kind 7), green after; each runs natively and as C with identical stdout via :emit-c-smoke (21 cases OK). Captures read Sema's facts (D65). Four defects fixed at the source: structural fn-type C names, empty-switch else {, dead-aggregate-temp skipping a return-slot aggregate, specialization source paths for __sema__ symbols. emit-c-smoke links the fiber runtime when referenced. Still unsupported, now loud with path:line: closure as a C fn pointer; async blocks. Gate green on 30f89aa.

@ehartford
ehartford changed the base branch from main to fix-1239 October 2, 2026 18:53
@ehartford
ehartford added this pull request to stack #1969 October 2, 2026 18:53
Eric Hartford added 4 commits October 2, 2026 12:48
A CK_CLOSURE constant had no C lowering, so every closure — including the
loop-body closure D69 passes to a generator's each, and std.task's
gen_pull — failed with 'unsupported const kind 7'. Each closure is now a
static C function over its retained MIR body taking the context first,
with an env struct per closure: pointers to the creating frame's slots for
a non-move closure (D62: by place regardless of Copy), the environment
inline in the context word for a move closure of Copy captures that fit,
and an owned heap cell {drop_fn, clone_fn, env} otherwise — the tag bits
of CodegenDispatch.gen_closure. By-place and cell captures are indirect
locals and read as (*_N), as a value-ref parameter does.

Found while making the generator cells green, fixed at the source:
- fn types are named structurally: Sema gives a closure its own fn type,
  and a typedef'd struct per tid made passing it to a structurally equal
  parameter a C type error;
- a switch with no arms (D69's exit-code switch after a loop body that
  neither breaks nor returns) emitted a bare else;
- the 'dead aggregate temp' skip dropped an aggregate built straight into
  _0, so a gen fn's state constructor returned zeroes;
- a __sema__ specialization has no declaring module of its own: Sema now
  records its template's (specialization_source_paths), and the C emitter
  resolves sizeof[PullCore] in gen_pull's instance from std/task.w.

A closure coerced to a C function pointer and an async block fail loudly
naming the construct.
…#1766)

The smoke's link line hard-coded fiber_stubs.o, so a program that pulls a
generator aborted at its first next() ('needs the fiber runtime, which this
program was linked without'). It now mirrors the native plan
(link_stage_undefined_symbols_need_fiber_runtime): a with_fiber_ or
with_channel_ reference on a non-extern line of the emitted C selects
channel_runtime.o, fiber_runtime.o, fiber.o and fiber_asm.o; otherwise the
stubs. The eight #1766 fixtures and the thirteen existing cases pass.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on stack top 7bdd704 (seed-driven, with run tools/battery.w, stack #1972 = #1966 → #1956 → #1961 → #1968 → #1959 → #1971 → #1970, on main 30f89aa): gate rc=0 wall=489s, battery-checks rc=0 wall=379s — fixpoint, drop and move audits, every corpus. Earlier reds on this stack were (a) #1239/#1955 fallout in fixtures the sweeps did not compile, fixed in-layer and in #1970, and (b) a stage2≠stage3 'self-miscompile' that turned out to be cross-worktree contamination through a shared out/gen symlink, root-caused with lldb and guarded in #1971; the bisect-reverted commit was innocent and is restored. Run from a worktree with its own out/gen.

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.

emit-c: closures are unsupported ("unsupported const kind 7"), so no generator program — push or pulled — goes through the C backend

1 participant