Commit abedaac8

abedaac8402eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): map recv emitted twice in compile_call_expr dispatch

cielavenir's n, m = gets.split.map &:to_i segfaulted on 0 0\n input. The generated C had two back-to-back sp_gets(); sp_str_split_ws(...) chains -- one from compile_call_expr's rc = compile_expr_gc_rooted(recv) pre-compute (line 12622), one from compile_map_expr's own compile_expr(@nd_receiver[nid]). For a pure recv the duplicate is wasted work; for gets it consumed two stdin lines per iteration and the break if n == 0 check never saw the intended row.

Thread the pre-computed rc through:

  • compile_call_expr's compile_enumerable_expr(nid, mname) -> compile_enumerable_expr(nid, mname, rc)
  • compile_enumerable_expr's compile_map_expr(nid) -> compile_map_expr(nid, precomputed_rc)
  • compile_map_expr accepts an optional precomputed_rc = "" parameter. Non-empty: reuse the temp from compile_call_expr's pre-compute. Empty: keep the existing self-compile path (compile_body_return's tail-map call site still uses this).

Re-root in the non-empty branch: compile_expr_gc_rooted only roots when the recv is a CallNode, so an ArrayNode recv ([10,20,30,40].map { ... }) arrives unrooted. The original compile_map_expr's SP_GC_ROOT(rc_tmp) covered this; the precomputed_rc branch preserves it. Without the re-root, the three_level_array_outer_index regression test's inner GC threshold (~272 inner rows) frees the outer array literal mid-loop and the next sp_IntArray_get(rc, _t3) reads a recycled slot.

Test: test/map_recv_emitted_once.rb uses a $counter-bumping helper to verify the recv is evaluated exactly once across the multi-assign, plus the array-literal-recv nested-map shape that the GC-rooting path needs.

Closes #540

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
abedaac8402e
Parent
649feee0e99f

spinel_codegen.rbmodifiedView file

@@ -12855,8 +12855,15 @@
1285512855 return r
1285612856 end
1285712857
12858- # map/select/reject/reduce as expression
12859- r = compile_enumerable_expr(nid, mname)
12858+ # map/select/reject/reduce as expression. Pass through the
12859+ # already-compiled `rc` so compile_map_expr (etc.) doesn't
12860+ # re-emit the recv's side-effecting chain. Issue #540: `gets`
12861+ # in `n, m = gets.split.map &:to_i` was getting emitted twice --
12862+ # once via compile_call_expr's `rc = compile_expr_gc_rooted` and
12863+ # once via compile_map_expr's own `compile_expr(recv)`. With
12864+ # gets reading from stdin, the duplicate consumed two input
12865+ # lines and the loop dispatched on the wrong values.
12866+ r = compile_enumerable_expr(nid, mname, rc)
1286012867 if r != ""
1286112868 return r
1286212869 end
@@ -17425,11 +17432,11 @@
1742517432 emit(" if (" + acc + ".cls_id == " + cls_id + ") { " + cstruct + " *_dh = (" + cstruct + " *)" + acc + ".v.p; " + acc + " = " + cstruct + "_has_key(_dh, " + key_expr + ") ? " + box_fn + "(" + cstruct + "_get(_dh, " + key_expr + ")) : sp_box_nil(); } else")
1742617433 end
1742717434
17428- def compile_enumerable_expr(nid, mname)
17435+ def compile_enumerable_expr(nid, mname, precomputed_rc = "")
1742917436 # map as expression
1743017437 if mname == "map"
1743117438 if @nd_block[nid] >= 0
17432- return compile_map_expr(nid)
17439+ return compile_map_expr(nid, precomputed_rc)
1743317440 end
1743417441 end
1743517442
@@ -29408,7 +29415,7 @@
2940829415 end
2940929416 end
2941029417
29411- def compile_map_expr(nid)
29418+ def compile_map_expr(nid, precomputed_rc = "")
2941229419 # N.times.map { |i| ... } -> loop 0..N-1 building an array
2941329420 recv_n = @nd_receiver[nid]
2941429421 if recv_n >= 0 && @nd_type[recv_n] == "CallNode" && @nd_name[recv_n] == "times" && @nd_block[recv_n] < 0
@@ -29479,7 +29486,26 @@
2947929486 return tmp_arrn
2948029487 end
2948129488 rt = infer_type(@nd_receiver[nid])
29482- rc_expr = compile_expr(@nd_receiver[nid])
29489+ # `precomputed_rc` comes from compile_call_expr's pre-compute
29490+ # (issue #540). When non-empty it's already a typed-and-rooted
29491+ # temp from compile_expr_gc_rooted; reusing it avoids a second
29492+ # compile_expr of the recv chain. When empty we're on the
29493+ # compile_body_return tail path (or another caller that hasn't
29494+ # pre-compiled) and have to compile the recv ourselves.
29495+ if precomputed_rc != ""
29496+ # compile_expr_gc_rooted only roots when the recv is a CallNode;
29497+ # an ArrayNode recv (e.g. `[10,20,30,40].map { ... }`) reaches
29498+ # us as an unrooted temp. Re-root here so a GC mid-loop (the
29499+ # block body's nested allocations cross the threshold) can't
29500+ # sweep the array literal. Idempotent for already-rooted temps.
29501+ rc_tmp = precomputed_rc
29502+ if type_needs_transient_root(rt) == 1
29503+ @needs_gc = 1
29504+ emit(" SP_GC_ROOT(" + rc_tmp + ");")
29505+ end
29506+ rc = rc_tmp
29507+ else
29508+ rc_expr = compile_expr(@nd_receiver[nid])
2948329509 # Store receiver in a temp to avoid re-evaluation, and root it
2948429510 # when the type is a GC-allocated pointer. Without the root, an
2948529511 # array-literal receiver (`[1,2,3].map { ... }`) is held only as
@@ -29487,13 +29513,14 @@
2948729513 # the block body itself does .map and grows allocations enough
2948829514 # to cross the threshold) frees the receiver mid-loop, and the
2948929515 # next `sp_*Array_length(rc)` reads a recycled IntArray's len.
29490- rc_tmp = new_temp
29491- emit(" " + c_type(rt) + " " + rc_tmp + " = " + rc_expr + ";")
29492- if type_needs_transient_root(rt) == 1
29493- @needs_gc = 1
29494- emit(" SP_GC_ROOT(" + rc_tmp + ");")
29516+ rc_tmp = new_temp
29517+ emit(" " + c_type(rt) + " " + rc_tmp + " = " + rc_expr + ";")
29518+ if type_needs_transient_root(rt) == 1
29519+ @needs_gc = 1
29520+ emit(" SP_GC_ROOT(" + rc_tmp + ");")
29521+ end
29522+ rc = rc_tmp
2949529523 end
29496- rc = rc_tmp
2949729524 bp1 = get_block_param(nid, 0)
2949829525 if bp1 == ""
2949929526 bp1 = "_x"