Commit 7acdafdd
7acdafdd6af6Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Fiber capture: pass outer-scope vars via GC-managed capture struct
Detect outer-scope variables referenced in fiber bodies (not defined by the fiber body itself). When captures exist, emit a per-fiber typed capture struct (_fib_cap_N) with a GC scan function, allocate it via sp_gc_alloc (so the fiber's existing user_data GC scan keeps it alive), and unpack captured vars at the top of the fiber body with SP_GC_ROOT for heap-pointer fields.
Fixes fiber_capture_no_leak_to_sibling, fiber_gc_root_isolation (+4 tests, 760 pass / 80 error).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 7acdafdd6af6
- Parent
- f3a8c100a0e0
src/codegen.cmodifiedView file
@@ -16078,6 +16078,14 @@
1607816078
fine only when it's a parameter of the enclosing method (passed by value).1607916079
Captured outer locals (is_cell) are not yet supported — those fibers will1608016080
produce a C compile error rather than silently miscompiling. */16081+
/* Returns 1 if a type needs a GC root when stored in a fiber capture struct. */16082+
static int fiber_cap_needs_root(TyKind t) {16083+
return t == TY_STRING || t == TY_BIGINT || ty_is_array(t) || ty_is_hash(t) ||16084+
ty_is_object(t) || t == TY_POLY || t == TY_PROC || t == TY_FIBER ||16085+
t == TY_EXCEPTION || t == TY_STRINGIO || t == TY_STRINGSCANNER ||16086+
t == TY_MATCHDATA || t == TY_REGEX || t == TY_TIME;16087+
}16088+
1608116089
static void emit_fiber_new(Compiler *c, int id, Buf *b) {1608216090
const NodeTable *nt = c->nt;1608316091
int blk = nt_ref(nt, id, "block");@@ -16103,6 +16111,51 @@
1610316111
NameSet fib_locals = {0};1610416112
if (body >= 0) proc_collect_locals(c, body, &fib_locals);1610516113
16114+
/* Compute captures: names used in the body that belong to the enclosing scope16115+
but are NOT defined by the fiber body itself and NOT the block param. */16116+
NameSet fib_used = {0};16117+
if (body >= 0) proc_collect_used(c, body, &fib_used);16118+
16119+
/* caps: outer-scope vars referenced in body but not written in body */16120+
NameSet caps = {0};16121+
if (encl) {16122+
for (int u = 0; u < fib_used.n; u++) {16123+
const char *nm = fib_used.v[u];16124+
if (bp0 && !strcmp(nm, bp0)) continue;16125+
if (nameset_has(&fib_locals, nm)) continue;16126+
LocalVar *lv = scope_local(encl, nm);16127+
if (!lv || lv->type == TY_UNKNOWN) continue;16128+
nameset_add(&caps, nm);16129+
}16130+
}16131+
free(fib_used.v);16132+
16133+
int ncap = caps.n;16134+
16135+
/* Emit capture struct + GC scan function when there are captured vars */16136+
if (ncap > 0) {16137+
buf_printf(&g_proc_protos, "typedef struct {");16138+
for (int i = 0; i < ncap; i++) {16139+
LocalVar *lv = encl ? scope_local(encl, caps.v[i]) : NULL;16140+
TyKind ct = lv ? lv->type : TY_POLY;16141+
buf_printf(&g_proc_protos, " "); emit_ctype(c, ct, &g_proc_protos);16142+
buf_printf(&g_proc_protos, " %s;", caps.v[i]);16143+
}16144+
buf_printf(&g_proc_protos, " } _fib_cap_%d;\n", fid);16145+
buf_printf(&g_proc_protos, "static void _fib_cap_scan_%d(void *p) {\n", fid);16146+
buf_printf(&g_proc_protos, " sp_gc_mark(p);\n");16147+
buf_printf(&g_proc_protos, " _fib_cap_%d *_c = (_fib_cap_%d *)p;\n", fid, fid);16148+
for (int i = 0; i < ncap; i++) {16149+
LocalVar *lv = encl ? scope_local(encl, caps.v[i]) : NULL;16150+
TyKind ct = lv ? lv->type : TY_POLY;16151+
if (fiber_cap_needs_root(ct)) {16152+
if (ct == TY_POLY) buf_printf(&g_proc_protos, " sp_gc_mark_rbval(_c->%s);\n", caps.v[i]);16153+
else buf_printf(&g_proc_protos, " if (_c->%s) sp_gc_mark((void *)_c->%s);\n", caps.v[i], caps.v[i]);16154+
}16155+
}16156+
buf_printf(&g_proc_protos, "}\n");16157+
}16158+
1610616159
/* Emit fiber body function prototype before main bodies */1610716160
buf_printf(&g_proc_protos, "static void %s(sp_Fiber *_fb);\n", fname);1610816161
@@ -16119,6 +16172,22 @@
1611916172
g_block_param_name = bp0; g_self = sv_self;1612016173
g_ret_type = TY_POLY; g_result_poly = 0; g_result_var = NULL;1612116174
16175+
/* Unpack capture struct */16176+
if (ncap > 0) {16177+
buf_printf(pb, " _fib_cap_%d *_fc = (_fib_cap_%d *)_fb->user_data;\n", fid, fid);16178+
for (int i = 0; i < ncap; i++) {16179+
LocalVar *lv = encl ? scope_local(encl, caps.v[i]) : NULL;16180+
TyKind ct = lv ? lv->type : TY_POLY;16181+
const char *rn = rename_local(caps.v[i]);16182+
buf_printf(pb, " "); emit_ctype(c, ct, pb);16183+
buf_printf(pb, " lv_%s = _fc->%s;\n", rn, caps.v[i]);16184+
if (fiber_cap_needs_root(ct)) {16185+
if (ct == TY_POLY) buf_printf(pb, " SP_GC_ROOT_RBVAL(lv_%s);\n", rn);16186+
else buf_printf(pb, " SP_GC_ROOT(lv_%s);\n", rn);16187+
}16188+
}16189+
}16190+
1612216191
/* Block param: first resume value (or nil on initial resume) */1612316192
if (bp0) {1612416193
const char *bpn = rename_local(bp0);@@ -16133,6 +16202,7 @@
1613316202
if (lv->is_param || lv->is_cell) continue;1613416203
if (!lv->name) continue;1613516204
if (bp0 && !strcmp(lv->name, bp0)) continue;16205+
if (nameset_has(&caps, lv->name)) continue;1613616206
if (!nameset_has(&fib_locals, lv->name)) continue;1613716207
if (lv->type == TY_UNKNOWN) continue;1613816208
declare_local(c, pb, lv, 0);@@ -16189,7 +16259,32 @@
1618916259
g_block_param_name = sv_bpn; g_self = sv_self; g_ret_type = sv_rt;1619016260
g_result_poly = sv_rp; g_result_var = sv_rv;1619116261
16192-
buf_printf(b, "sp_Fiber_new(%s)", fname);16262+
/* Emit creation expression:16263+
If there are captures, allocate a GC-managed capture struct, fill it,16264+
assign to fiber->user_data, then return the fiber.16265+
Without captures: just sp_Fiber_new(fname). */16266+
if (ncap > 0) {16267+
int tf = ++g_tmp, tc = ++g_tmp;16268+
emit_indent(g_pre, g_indent);16269+
buf_printf(g_pre, "sp_Fiber *_t%d = sp_Fiber_new(%s);\n", tf, fname);16270+
emit_indent(g_pre, g_indent);16271+
buf_printf(g_pre, "SP_GC_ROOT(_t%d);\n", tf);16272+
emit_indent(g_pre, g_indent);16273+
buf_printf(g_pre, "_fib_cap_%d *_t%d = (_fib_cap_%d *)sp_gc_alloc(sizeof(_fib_cap_%d), NULL, _fib_cap_scan_%d);\n",16274+
fid, tc, fid, fid, fid);16275+
for (int i = 0; i < ncap; i++) {16276+
const char *rn = rename_local(caps.v[i]);16277+
emit_indent(g_pre, g_indent);16278+
buf_printf(g_pre, "_t%d->%s = lv_%s;\n", tc, caps.v[i], rn);16279+
}16280+
emit_indent(g_pre, g_indent);16281+
buf_printf(g_pre, "_t%d->user_data = _t%d;\n", tf, tc);16282+
buf_printf(b, "_t%d", tf);16283+
}16284+
else {16285+
buf_printf(b, "sp_Fiber_new(%s)", fname);16286+
}16287+
free(caps.v);1619316288
}1619416289
1619516290
/* Lower a `proc {}` / `lambda {}` / `Proc.new {}` / `->(){}` literal: emit a