Commit 5faaa776

5faaa776133bMerged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): root .map receiver and slice-assign poly_array<-poly so 3-level arrays index correctly

Two related codegen fixes that surface together when a 3-level nested array literal [a, b, c, d].map { (0...M).map { (0..N).map { ... } } } is large enough to trigger an inner-loop GC.

  1. compile_map_expr: stash the receiver in a C-stack temp without GC-rooting it. When the block body's nested allocations cross the GC threshold, the unrooted receiver is freed mid-loop and the next iteration's sp_*Array_length(rc) reads a recycled array's len. Visible as TBL.length returning 8 instead of 4 for the optcarrot-shaped [0x0, 0x4, 0x8, 0xc].map { (0...0x10000).map { ... } }.

    Fix: when the static recv type is a GC-allocated pointer, emit SP_GC_ROOT(temp) after the temp assignment.

  2. compile_bracket_assign: recv[start, len] = src with recv statically poly_array and src statically poly fell through to the single-elem-write path, silently dropping the rhs. Pre-box the recv to sp_RbVal and dispatch through compile_poly_slice_assign's runtime cls_id loop. Surfaces for @bg_pixels[xfine, 8] = @bg_pattern_lut[idx] once the 3-level constant TILE_LUT is no longer split into 4 separate 2-level shards (the latter was the workaround in optcarrot_build.sh).

Test added: test/three_level_array_outer_index.rb covers both fixes.

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

Details

Commit
5faaa776133b
Parent
2ff7ba46856e

spinel_codegen.rbmodifiedView file

@@ -32989,6 +32989,21 @@
3298932989 compile_poly_slice_assign(nid, recv, rc, arg_ids)
3299032990 return
3299132991 end
32992+ # Slice assignment with poly_array recv and poly src: the recv
32993+ # is statically typed (sp_PolyArray *) but src is sp_RbVal —
32994+ # fold into compile_poly_slice_assign by pre-boxing the recv to
32995+ # sp_RbVal so the runtime cls_id dispatch can pick the right
32996+ # copy loop. This is the shape `@bg_pixels[xfine, 8] =
32997+ # @bg_pattern_lut[@bg_pattern]` ends up in once a 3-level array
32998+ # constant (TILE_LUT) types its inner-of-inner reads as poly.
32999+ if arg_ids.length == 3 && rt == "poly_array"
33000+ src_t_check2 = infer_type(arg_ids[2])
33001+ if src_t_check2 == "poly"
33002+ @needs_rb_value = 1
33003+ compile_poly_slice_assign(nid, recv, "sp_box_poly_array(" + rc + ")", arg_ids)
33004+ return
33005+ end
33006+ end
3299233007 idx = "0"
3299333008 val = "0"
3299433009 if arg_ids.length >= 1
@@ -35191,9 +35206,19 @@
3519135206 end
3519235207 rt = infer_type(@nd_receiver[nid])
3519335208 rc_expr = compile_expr(@nd_receiver[nid])
35194- # Store receiver in a temp to avoid re-evaluation
35209+ # Store receiver in a temp to avoid re-evaluation, and root it
35210+ # when the type is a GC-allocated pointer. Without the root, an
35211+ # array-literal receiver (`[1,2,3].map { ... }`) is held only as
35212+ # a C-stack local; a sp_gc_collect inside the block (e.g. when
35213+ # the block body itself does .map and grows allocations enough
35214+ # to cross the threshold) frees the receiver mid-loop, and the
35215+ # next `sp_*Array_length(rc)` reads a recycled IntArray's len.
3519535216 rc_tmp = new_temp
3519635217 emit(" " + c_type(rt) + " " + rc_tmp + " = " + rc_expr + ";")
35218+ if type_needs_transient_root(rt) == 1
35219+ @needs_gc = 1
35220+ emit(" SP_GC_ROOT(" + rc_tmp + ");")
35221+ end
3519735222 rc = rc_tmp
3519835223 bp1 = get_block_param(nid, 0)
3519935224 if bp1 == ""