Commit b2dc028b

b2dc028b2595Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Make GC.start collect; root interpolation and concat-chain operands

Comparing the gc_*.rb C output against the legacy backend surfaced that spinelc emitted GC.start as a no-op (0LL) while legacy emits (sp_gc_collect(), 0). The rooting tests therefore never forced a collection at their GC.start points — they only collected incidentally inside churn() — which masked two real rooting holes in string-building expressions:

  • String interpolation "#{a}#{b}" lowered to sp_sprintf(fmt, conv_a, conv_b) with the conversions inline. A conversion that allocates (sp_poly_to_s of an int/float, *_inspect, sp_float_to_s, …) left its fresh string in an unrooted temporary while a sibling conversion triggered a GC, freeing it before sp_sprintf read it (gc_root_heap_param's "#{gc_then}-#{body}"). Hoist each %s arg into a rooted temp inside a self-contained statement-expression (({ const char *_t = conv; SP_GC_ROOT(_t); … sp_sprintf(…); })), so it works in any expression position without polluting g_pre. Int (%lld) args are plain values and stay inline.

  • Chained a + b + c lowered to nested sp_str_concat with operands evaluated inline; a fresh operand could be swept while a sibling operand allocated or forced a GC (concat_chain_operand_gc_root). When either operand may allocate, evaluate both into rooted temps first.

Then make GC.start (and GC.compact) actually call sp_gc_collect, matching Ruby/legacy semantics and making the gc_*.rb tests genuinely exercise a forced collection.

All seven gc_*.rb tests now pass under ASAN with SPINEL_GC_STRESS=1 and a forced GC.start. make test (845, normal and stress+verify) and make bench (57) stay green.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
b2dc028b2595
Parent
75ab7d970562

src/codegen_call.cmodifiedView file

@@ -2289,8 +2289,8 @@
22892289 /* GC module methods */
22902290 if (recv >= 0 && nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode") &&
22912291 nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "GC")) {
2292- if (!strcmp(name, "start") && argc == 0) { buf_puts(b, "0LL"); return; }
2293- if (!strcmp(name, "compact") && argc == 0) { buf_puts(b, "0LL"); return; }
2292+ if (!strcmp(name, "start") && argc == 0) { buf_puts(b, "(sp_gc_collect(), (mrb_int)0)"); return; }
2293+ if (!strcmp(name, "compact") && argc == 0) { buf_puts(b, "(sp_gc_collect(), (mrb_int)0)"); return; }
22942294 if (!strcmp(name, "stat") && argc == 0) { buf_puts(b, "sp_gc_stat()"); return; }
22952295 }
22962296
@@ -3379,9 +3379,22 @@
33793379
33803380 if (recv >= 0 && argc == 1 && int_arith_fn(name) && !ty_is_object(rt) && !ty_is_array(rt)) {
33813381 if (rt == TY_STRING && !strcmp(name, "+")) {
3382- buf_puts(b, "sp_str_concat(");
3383- emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);
3384- buf_puts(b, ")");
3382+ /* Root both operands when either may allocate: `a + b` evaluates both,
3383+ and a fresh heap string from one can be swept while the other
3384+ allocates or forces a GC (chained `a + b + c` with side-effecting
3385+ operands — concat_chain_operand_gc_root). Recurses naturally: a
3386+ chain's left operand is itself a `+` and gets its own rooted block.
3387+ Pure literal / bare-read operands need no rooting. */
3388+ if (subtree_may_allocate(nt, recv) || subtree_may_allocate(nt, argv[0])) {
3389+ int ta = ++g_tmp, tb = ++g_tmp;
3390+ buf_printf(b, "({ const char *_t%d = ", ta); emit_expr(c, recv, b);
3391+ buf_printf(b, "; SP_GC_ROOT(_t%d); const char *_t%d = ", ta, tb); emit_expr(c, argv[0], b);
3392+ buf_printf(b, "; SP_GC_ROOT(_t%d); sp_str_concat(_t%d, _t%d); })", tb, ta, tb);
3393+ } else {
3394+ buf_puts(b, "sp_str_concat(");
3395+ emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);
3396+ buf_puts(b, ")");
3397+ }
33853398 return;
33863399 }
33873400 if (rt == TY_STRING && !strcmp(name, "*")) {