Commit 705f5df8

705f5df8df2fMerged

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

Description

feat(codegen): map block returning nested array preserves cls_id chain

Range#map { array_block } previously fell through to a 0 placeholder when the block returned anything other than int / string / float. int_array.map { array_block } boxed inner sp_PtrArray results via sp_box_ptr_array(val) — the resulting cls_id PTR_ARRAY erased the elem-type info, so 3D-and-deeper indexing fell through every poly-builtin dispatch arm to sp_box_nil().

Two fixes:

  1. Range#map's array-block path now emits the typed <bret>_ptr_array (1D inner) or sp_PolyArray with box_value_to_poly boxing (deeper nesting). The block_is_typed_array predicate gates which storage to pick. Inferred type matches the runtime storage.

  2. int_array.map's ret_is_deep_arr2 branch (block returns <X>_ptr_array) walks the inner sp_PtrArray and rebuilds it as sp_PolyArray with sp_box_<elem> per element — preserving the cls_id chain. The outer push then uses sp_box_poly_array(converted) so arr[i][j][k]... dispatches correctly at every depth.

Together this lets (0..n).map { (0..m).map { ... } } and nested forms like [A].map { (0..n).map { (0..m).map { ... } }.transpose } (optcarrot's TILE_LUT shape) compile and read at every depth.

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

Details

Commit
705f5df8df2f
Parent
59ff8d7a4f30

spinel_codegen.rbmodifiedView file

@@ -3573,21 +3573,22 @@
35733573 if is_obj_type(bret) == 1
35743574 return bret + "_ptr_array"
35753575 end
3576- # Block returns an array (e.g.
3577- # `[1, 6].map { (0..n).map { ... } }` — each outer
3578- # element is itself an int_array). The outer map's
3579- # result is then an array of arrays, encoded as
3580- # `<inner>_ptr_array`. Only fire this for array-shaped
3581- # recv (where the elements have a definite shape and
3582- # ivar typing reaches the ptr_array placeholder); for
3583- # range/int recv we fall through to the int_array
3584- # placeholder below so a follow-up int-typed assignment
3585- # to the same ivar still type-checks.
3576+ # Block returns a 1D array (e.g.
3577+ # `[1, 6].map { (0..n).map { i } }` or
3578+ # `(0..3).map { |i| (0..3).map { ... } }`) — each
3579+ # outer element is itself a 1D typed array. Encode
3580+ # as `<inner>_ptr_array` for the standard
3581+ # `arr[i][j]` dispatch shape.
35863582 if bret == "int_array" || bret == "float_array" || bret == "str_array" || bret == "sym_array"
3587- rt_recv_p = infer_type(recv)
3588- if rt_recv_p != "range" && rt_recv_p != "int"
3589- return bret + "_ptr_array"
3590- end
3583+ return bret + "_ptr_array"
3584+ end
3585+ # Block returns a deeper-nested array
3586+ # (`int_array_ptr_array`, `poly_array`). Encode as
3587+ # `poly_array` — cls_id tagging chain on the inner
3588+ # PolyArray pushes preserves elem type for
3589+ # `arr[i][j][k]` 3D dispatch.
3590+ if is_ptr_array_type(bret) == 1 || bret == "poly_array"
3591+ return "poly_array"
35913592 end
35923593 # poly_array bret intentionally falls through. Returning
35933594 # poly_array_ptr_array would be more accurate, but ivars
@@ -31409,13 +31410,15 @@
3140931410 end
3141031411 end
3141131412 end
31412- # Only handle scalar block returns inline; an array-returning
31413- # block (`(0..n).map { |i| [i, ...] }`) would make this an
31414- # array-of-arrays, which the int/str/float push helpers can't
31415- # express. Fall through to the "0" placeholder for those — the
31416- # generic int_array typing keeps any follow-up `@x = [...]`
31417- # assignment to the same slot type-checking.
31418- if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float"
31413+ # Scalar block returns push into the matching typed array.
31414+ # Array-returning blocks (`(0..n).map { |i| [...] }`) produce
31415+ # an array-of-arrays — store each block result as a pointer
31416+ # in `<X>_ptr_array`. Block return types we can't express
31417+ # fall through to a "0" placeholder.
31418+ block_is_typed_array = (block_ret_r == "int_array" || block_ret_r == "str_array" ||
31419+ block_ret_r == "float_array" || block_ret_r == "sym_array" ||
31420+ is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array")
31421+ if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float" && block_is_typed_array == false
3141931422 return "0"
3142031423 end
3142131424 @needs_int_array = 1
@@ -31427,6 +31430,19 @@
3142731430 elsif block_ret_r == "float"
3142831431 @needs_float_array = 1
3142931432 emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
31433+ elsif block_is_typed_array
31434+ # Array-of-<X>: 1D-block-return uses PtrArray (matches
31435+ # the inferred `<bret>_ptr_array` type). Deeper nesting
31436+ # (block already returns ptr_array / poly_array) uses
31437+ # PolyArray with `box_value_to_poly` boxing — the
31438+ # cls_id chain stays tagged for `arr[i][j][k]...`
31439+ # dispatch.
31440+ if is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array"
31441+ @needs_rb_value = 1
31442+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
31443+ else
31444+ emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
31445+ end
3143031446 else
3143131447 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
3143231448 end
@@ -31490,6 +31506,12 @@
3149031506 emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")
3149131507 elsif block_ret_r == "float"
3149231508 emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")
31509+ elsif block_is_typed_array
31510+ if is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array"
31511+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_r, lastv_r) + ");")
31512+ else
31513+ emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)" + lastv_r + ");")
31514+ end
3149331515 else
3149431516 emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_r + ");")
3149531517 end
@@ -31588,9 +31610,15 @@
3158831610 # `[1, 6].map { (0..n).map { ... } }`) — accumulator must
3158931611 # be a PtrArray so GC keeps each element alive (an IntArray
3159031612 # accumulator with pointer-payload mrb_ints would let the
31591- # inner arrays be collected).
31613+ # inner arrays be collected). Deeper nesting (block_ret is
31614+ # already a ptr_array / poly_array) uses PolyArray boxing
31615+ # so the cls_id chain stays tagged for `arr[i][j][k]...`.
3159231616 ret_is_arr2 = (block_ret == "int_array" || block_ret == "float_array" || block_ret == "str_array" || block_ret == "sym_array")
31593- if ret_is_arr2
31617+ ret_is_deep_arr2 = (is_ptr_array_type(block_ret) == 1 || block_ret == "poly_array")
31618+ if ret_is_deep_arr2
31619+ @needs_rb_value = 1
31620+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
31621+ elsif ret_is_arr2
3159431622 @needs_ptr_array = 1
3159531623 emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
3159631624 else
@@ -31611,7 +31639,28 @@
3161131639 if stmts3.length > 0
3161231640 last = stmts3[stmts3.length - 1]
3161331641 val = compile_expr(last)
31614- if ret_is_arr2
31642+ if ret_is_deep_arr2
31643+ # The block returns a typed `<X>_ptr_array` (e.g.
31644+ # `int_array_ptr_array`). The static type carries
31645+ # elem-type info but `sp_box_ptr_array(val)` would
31646+ # erase it (cls_id PTR_ARRAY only). Convert to
31647+ # PolyArray inline so each inner element gets its
31648+ # proper `sp_box_<elem>` tag — `arr[i][j][k]`
31649+ # dispatch then chains correctly.
31650+ if is_ptr_array_type(block_ret) == 1
31651+ inner_t = ptr_array_elem_type(block_ret)
31652+ conv = new_temp
31653+ ii = new_temp
31654+ emit(" sp_PolyArray *" + conv + " = sp_PolyArray_new();")
31655+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < sp_PtrArray_length(" + val + "); " + ii + "++) {")
31656+ inner_get = "(" + c_type(inner_t) + ")sp_PtrArray_get(" + val + ", " + ii + ")"
31657+ emit(" sp_PolyArray_push(" + conv + ", " + box_value_to_poly(inner_t, inner_get) + ");")
31658+ emit(" }")
31659+ emit(" sp_PolyArray_push(" + tmp_arr + ", sp_box_poly_array(" + conv + "));")
31660+ else
31661+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret, val) + ");")
31662+ end
31663+ elsif ret_is_arr2
3161531664 emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + val + "));")
3161631665 else
3161731666 emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")