Commit a545e8e0

a545e8e0bc93Merged

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

Description

fix(codegen): runtime IntArray→PolyArray widening on poly-recv []= + multi-write destructure poly_array RHS

Two complementary additions enabling option-A runtime widening:

  1. Runtime widening on poly-recv []= (compile_bracket_assign, rt == "poly" branch). When the slot's runtime storage is IntArray but the value carries a non-int cls_id (i.e. a typed pointer / box that loses cls_id info if cast through int), emit a fresh PolyArray, copy each existing IntArray element as sp_box_int(elem), set the new slot with the boxed value, and reassign the slot to hold the new PolyArray.

    Restricted to ivar / local recv (where the slot expression can be re-assigned in C). The widening preserves cls_id chains through subsequent reads via the existing poly-recv [] dispatch.

  2. Multi-write destructure with poly_array RHS. The compile_multi_write fall-through path assumed the RHS is always int_array. Now dispatch by the RHS's static type: sp_PolyArray_get for poly_array, sp_IntArray_get for the default. Targets receive the matching value type.

These let optcarrots @io_addr, @bg_pattern_lut_fetched, _ = @attr_lut[i] (where @attr_lut is poly_array of 3-tuple poly_arrays) work correctly when the type cascade succeeds. The existing optcarrot still hits a separate inference issue — the entries[key] ||= [...] Hash dedupe pattern types entries as str_int_hash, collapsing @attr_lut.map { entries[key] } to IntArray-of-pointers-as-int and bypassing this fix. Fixing that needs either spinel's local hash inference widening or an optcarrot-side dedupe rewrite (both attempted, both ran into secondary cascades).

CPU end-state still matches Ruby exactly (pc=35368 a=177 etc.); PPU rendering still produces uniform 0x40 (checksum 16384 vs target 59662) due to the @attr_lut → IntArray cascade. Method object dispatch itself works end-to-end through @fetch[a][a].

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

Details

Commit
a545e8e0bc93
Parent
ab64a279d511

spinel_codegen.rbmodifiedView file

@@ -28706,25 +28706,41 @@
2870628706 k = k + 1
2870728707 end
2870828708 else
28709- # RHS is a function call returning int_array
28710- @needs_int_array = 1
28709+ # RHS is a function call returning a typed array. Dispatch by
28710+ # the RHS's static type — IntArray default, but PolyArray when
28711+ # the inner is heterogeneous (optcarrots `@io_addr,
28712+ # @bg_pattern_lut_fetched, _ = @attr_lut[i]` where attr_lut
28713+ # elements are 3-tuples of mixed types).
28714+ val_t_local = infer_type(val_id)
2871128715 @needs_gc = 1
2871228716 tmp = new_temp
28713- emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")
28717+ if val_t_local == "poly_array"
28718+ @needs_rb_value = 1
28719+ emit(" sp_PolyArray *" + tmp + " = " + compile_expr(val_id) + ";")
28720+ else
28721+ @needs_int_array = 1
28722+ emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")
28723+ end
2871428724 emit(" SP_GC_ROOT(" + tmp + ");")
2871528725 k = 0
2871628726 while k < targets.length
2871728727 tid = targets[k]
28718- rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"
28728+ if val_t_local == "poly_array"
28729+ rhs = "sp_PolyArray_get(" + tmp + ", " + k.to_s + ")"
28730+ val_t_for_target = "poly"
28731+ else
28732+ rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"
28733+ val_t_for_target = "int"
28734+ end
2871928735 # Route ivar / local writes through emit_multi_write_target so
2872028736 # the box-when-slot-is-poly logic fires. Without boxing, an int
2872128737 # RHS into a sp_RbVal slot fails C compile (`@x, @y = arr` where
2872228738 # @y is poly because other writers store other types).
2872328739 if @nd_type[tid] == "LocalVariableTargetNode"
28724- emit_multi_write_target(tid, rhs, "int")
28740+ emit_multi_write_target(tid, rhs, val_t_for_target)
2872528741 end
2872628742 if @nd_type[tid] == "InstanceVariableTargetNode"
28727- emit_multi_write_target(tid, rhs, "int")
28743+ emit_multi_write_target(tid, rhs, val_t_for_target)
2872828744 end
2872928745 if @nd_type[tid] == "ConstantTargetNode"
2873028746 if find_const_idx(@nd_name[tid]) >= 0
@@ -31963,10 +31979,16 @@
3196331979 # the underlying pointer points at. Optcarrot's `@fetch[a] =
3196431980 # method(:peek_X)` lands here when @fetch was widened to poly
3196531981 # (rather than poly_array) by the heterogeneous IntArray + Method
31966- # writes. Dispatch by cls_id, mirroring sp_PolyArray_set's
31967- # interface for the POLY_ARRAY case and using element-typed
31968- # setters for the homogeneous storage variants. Extract the
31969- # appropriate payload from the boxed value via .v.p / .v.i.
31982+ # writes. Dispatch by cls_id at runtime.
31983+ #
31984+ # When the slot's runtime storage is IntArray but the value
31985+ # being stored is non-int (a pointer / poly with cls_id != 0),
31986+ # we'd lose the cls_id by storing as int. Widen the storage at
31987+ # runtime: allocate a fresh PolyArray, copy the existing int
31988+ # elements as boxed-int, then store the new value boxed. Update
31989+ # the slot to hold the new PolyArray. Requires knowing where
31990+ # the slot is — only fires when recv is an ivar / local / cvar
31991+ # / gvar that we can reassign in C.
3197031992 vbox = val
3197131993 vt = "int"
3197231994 if arg_ids.length >= 2
@@ -31977,9 +31999,45 @@
3197731999 end
3197832000 vbox_tmp = new_temp
3197932001 emit(" sp_RbVal " + vbox_tmp + " = " + vbox + ";")
31980- emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");")
31981- emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);")
31982- emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")
32002+ # Determine the slot expression — what we'd assign to in order
32003+ # to replace the storage. Only ivar / local / cvar / gvar are
32004+ # supported; anything else falls back to no-widen (still
32005+ # correct for the homogeneous cases, just not heterogeneous).
32006+ slot_lhs = ""
32007+ if @nd_type[recv] == "InstanceVariableReadNode"
32008+ slot_lhs = self_arrow + sanitize_ivar(@nd_name[recv])
32009+ elsif @nd_type[recv] == "LocalVariableReadNode"
32010+ slot_lhs = fiber_var_ref(@nd_name[recv])
32011+ end
32012+ if slot_lhs != ""
32013+ @needs_gc = 1
32014+ @needs_rb_value = 1
32015+ cur = new_temp
32016+ new_arr = new_temp
32017+ old_int = new_temp
32018+ ii = new_temp
32019+ emit(" sp_RbVal " + cur + " = " + rc + ";")
32020+ # Widen path: if storage is IntArray but value can't fit int
32021+ # (i.e. value carries cls_id != 0 or tag != INT, meaning it's
32022+ # a typed pointer/box that loses info if cast to int).
32023+ emit(" if (" + cur + ".cls_id == SP_BUILTIN_INT_ARRAY && (" + vbox_tmp + ".tag != SP_TAG_INT || " + vbox_tmp + ".cls_id != 0)) {")
32024+ emit(" sp_IntArray *" + old_int + " = (sp_IntArray *)" + cur + ".v.p;")
32025+ emit(" sp_PolyArray *" + new_arr + " = sp_PolyArray_new();")
32026+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < sp_IntArray_length(" + old_int + "); " + ii + "++) sp_PolyArray_push(" + new_arr + ", sp_box_int(sp_IntArray_get(" + old_int + ", " + ii + ")));")
32027+ emit(" sp_PolyArray_set(" + new_arr + ", " + idx + ", " + vbox_tmp + ");")
32028+ emit(" " + slot_lhs + " = sp_box_poly_array(" + new_arr + ");")
32029+ emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_POLY_ARRAY) {")
32030+ emit(" sp_PolyArray_set((sp_PolyArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ");")
32031+ emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_PTR_ARRAY) {")
32032+ emit(" sp_PtrArray_set((sp_PtrArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);")
32033+ emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_INT_ARRAY) {")
32034+ emit(" sp_IntArray_set((sp_IntArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")
32035+ emit(" }")
32036+ else
32037+ emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");")
32038+ emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);")
32039+ emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")
32040+ end
3198332041 return
3198432042 end
3198532043 if rt == "str_int_hash"