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:
-
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 assp_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. -
Multi-write destructure with poly_array RHS. The
compile_multi_writefall-through path assumed the RHS is always int_array. Now dispatch by the RHS's static type:sp_PolyArray_getfor poly_array,sp_IntArray_getfor 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
k = k + 1 end else # RHS is a function call returning int_array @needs_int_array = 1 # RHS is a function call returning a typed array. Dispatch by # the RHS's static type — IntArray default, but PolyArray when # the inner is heterogeneous (optcarrots `@io_addr, # @bg_pattern_lut_fetched, _ = @attr_lut[i]` where attr_lut # elements are 3-tuples of mixed types). val_t_local = infer_type(val_id) @needs_gc = 1 tmp = new_temp emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";") if val_t_local == "poly_array" @needs_rb_value = 1 emit(" sp_PolyArray *" + tmp + " = " + compile_expr(val_id) + ";") else @needs_int_array = 1 emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";") end emit(" SP_GC_ROOT(" + tmp + ");") k = 0 while k < targets.length tid = targets[k] rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")" if val_t_local == "poly_array" rhs = "sp_PolyArray_get(" + tmp + ", " + k.to_s + ")" val_t_for_target = "poly" else rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")" val_t_for_target = "int" end # Route ivar / local writes through emit_multi_write_target so # the box-when-slot-is-poly logic fires. Without boxing, an int # RHS into a sp_RbVal slot fails C compile (`@x, @y = arr` where # @y is poly because other writers store other types). if @nd_type[tid] == "LocalVariableTargetNode" emit_multi_write_target(tid, rhs, "int") emit_multi_write_target(tid, rhs, val_t_for_target) end if @nd_type[tid] == "InstanceVariableTargetNode" emit_multi_write_target(tid, rhs, "int") emit_multi_write_target(tid, rhs, val_t_for_target) end if @nd_type[tid] == "ConstantTargetNode" if find_const_idx(@nd_name[tid]) >= 0 # the underlying pointer points at. Optcarrot's `@fetch[a] = # method(:peek_X)` lands here when @fetch was widened to poly # (rather than poly_array) by the heterogeneous IntArray + Method # writes. Dispatch by cls_id, mirroring sp_PolyArray_set's # interface for the POLY_ARRAY case and using element-typed # setters for the homogeneous storage variants. Extract the # appropriate payload from the boxed value via .v.p / .v.i. # writes. Dispatch by cls_id at runtime. # # When the slot's runtime storage is IntArray but the value # being stored is non-int (a pointer / poly with cls_id != 0), # we'd lose the cls_id by storing as int. Widen the storage at # runtime: allocate a fresh PolyArray, copy the existing int # elements as boxed-int, then store the new value boxed. Update # the slot to hold the new PolyArray. Requires knowing where # the slot is — only fires when recv is an ivar / local / cvar # / gvar that we can reassign in C. vbox = val vt = "int" if arg_ids.length >= 2 end vbox_tmp = new_temp emit(" sp_RbVal " + vbox_tmp + " = " + vbox + ";") emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");") emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);") emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);") # Determine the slot expression — what we'd assign to in order # to replace the storage. Only ivar / local / cvar / gvar are # supported; anything else falls back to no-widen (still # correct for the homogeneous cases, just not heterogeneous). slot_lhs = "" if @nd_type[recv] == "InstanceVariableReadNode" slot_lhs = self_arrow + sanitize_ivar(@nd_name[recv]) elsif @nd_type[recv] == "LocalVariableReadNode" slot_lhs = fiber_var_ref(@nd_name[recv]) end if slot_lhs != "" @needs_gc = 1 @needs_rb_value = 1 cur = new_temp new_arr = new_temp old_int = new_temp ii = new_temp emit(" sp_RbVal " + cur + " = " + rc + ";") # Widen path: if storage is IntArray but value can't fit int # (i.e. value carries cls_id != 0 or tag != INT, meaning it's # a typed pointer/box that loses info if cast to int). emit(" if (" + cur + ".cls_id == SP_BUILTIN_INT_ARRAY && (" + vbox_tmp + ".tag != SP_TAG_INT || " + vbox_tmp + ".cls_id != 0)) {") emit(" sp_IntArray *" + old_int + " = (sp_IntArray *)" + cur + ".v.p;") emit(" sp_PolyArray *" + new_arr + " = sp_PolyArray_new();") 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 + ")));") emit(" sp_PolyArray_set(" + new_arr + ", " + idx + ", " + vbox_tmp + ");") emit(" " + slot_lhs + " = sp_box_poly_array(" + new_arr + ");") emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_POLY_ARRAY) {") emit(" sp_PolyArray_set((sp_PolyArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ");") emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_PTR_ARRAY) {") emit(" sp_PtrArray_set((sp_PtrArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);") emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_INT_ARRAY) {") emit(" sp_IntArray_set((sp_IntArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);") emit(" }") else emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");") emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);") emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);") end return end if rt == "str_int_hash"test/runtime_widen_int_to_poly_array.rbaddedView file
Loading diff…
test/runtime_widen_int_to_poly_array.rb.expectedaddedView file
Loading diff…