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.
-
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 asTBL.lengthreturning 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. -
compile_bracket_assign:
recv[start, len] = srcwith 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 throughcompile_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
compile_poly_slice_assign(nid, recv, rc, arg_ids) return end # Slice assignment with poly_array recv and poly src: the recv # is statically typed (sp_PolyArray *) but src is sp_RbVal — # fold into compile_poly_slice_assign by pre-boxing the recv to # sp_RbVal so the runtime cls_id dispatch can pick the right # copy loop. This is the shape `@bg_pixels[xfine, 8] = # @bg_pattern_lut[@bg_pattern]` ends up in once a 3-level array # constant (TILE_LUT) types its inner-of-inner reads as poly. if arg_ids.length == 3 && rt == "poly_array" src_t_check2 = infer_type(arg_ids[2]) if src_t_check2 == "poly" @needs_rb_value = 1 compile_poly_slice_assign(nid, recv, "sp_box_poly_array(" + rc + ")", arg_ids) return end end idx = "0" val = "0" if arg_ids.length >= 1 end rt = infer_type(@nd_receiver[nid]) rc_expr = compile_expr(@nd_receiver[nid]) # Store receiver in a temp to avoid re-evaluation # Store receiver in a temp to avoid re-evaluation, and root it # when the type is a GC-allocated pointer. Without the root, an # array-literal receiver (`[1,2,3].map { ... }`) is held only as # a C-stack local; a sp_gc_collect inside the block (e.g. when # the block body itself does .map and grows allocations enough # to cross the threshold) frees the receiver mid-loop, and the # next `sp_*Array_length(rc)` reads a recycled IntArray's len. rc_tmp = new_temp emit(" " + c_type(rt) + " " + rc_tmp + " = " + rc_expr + ";") if type_needs_transient_root(rt) == 1 @needs_gc = 1 emit(" SP_GC_ROOT(" + rc_tmp + ");") end rc = rc_tmp bp1 = get_block_param(nid, 0) if bp1 == ""test/three_level_array_outer_index.rbaddedView file
Loading diff…
test/three_level_array_outer_index.rb.expectedaddedView file
Loading diff…