Commit 6730228c

6730228c053cMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified

Description

Merge pull request #339 from mame/pr/method-dispatch-poly-array

fix(codegen): heterogeneous IntArray + Method dispatch through poly_array

Details

Commit
6730228c053c
Parent
af8932544e54
Merge parent
f40d6c982af6

spinel_codegen.rbmodifiedView file

@@ -11310,9 +11310,39 @@
1131011310 if ivk < types.length && ivk < obs.length && types[ivk] != "poly"
1131111311 distinct = obs[ivk].split(",")
1131211312 if distinct.length >= 2
11313- types[ivk] = "poly"
11314- @needs_rb_value = 1
11315- changed = 1
11313+ # When every observed type is itself an array AND the
11314+ # current type is already `poly_array` (set by an earlier
11315+ # `[]=` widening — see scan_writer_calls), keep it as
11316+ # `poly_array` rather than collapsing to `poly`. The
11317+ # latter discards element-array semantics: reads on a
11318+ # `poly` slot dont dispatch through cls_id_to_storage,
11319+ # they box-and-unbox the entire ivar value. Optcarrot's
11320+ # `@fetch[a][a]` (heterogeneous IntArray + Method) only
11321+ # works when @fetch stays `poly_array`. For other
11322+ # combinations (mix of array and non-array, or arrays
11323+ # without prior poly_array marker), keep the existing
11324+ # widen-to-poly behavior.
11325+ all_arrays = 1
11326+ di = 0
11327+ while di < distinct.length
11328+ if is_array_type(distinct[di]) == 0
11329+ all_arrays = 0
11330+ end
11331+ di = di + 1
11332+ end
11333+ if all_arrays == 1 && types[ivk] == "poly_array"
11334+ # Already poly_array; do nothing — keep it.
11335+ elsif all_arrays == 1
11336+ # Compatible array shapes but not yet poly_array; widen.
11337+ types[ivk] = "poly_array"
11338+ @needs_rb_value = 1
11339+ @needs_gc = 1
11340+ changed = 1
11341+ else
11342+ types[ivk] = "poly"
11343+ @needs_rb_value = 1
11344+ changed = 1
11345+ end
1131611346 end
1131711347 end
1131811348 ivk = ivk + 1
@@ -11576,6 +11606,7 @@
1157611606 if mname == "[]=" && @current_class_idx >= 0 && recv >= 0 && @nd_type[recv] == "InstanceVariableReadNode"
1157711607 iname = @nd_name[recv]
1157811608 cur_t = cls_ivar_type(@current_class_idx, iname)
11609+ promoted = ""
1157911610 if cur_t == "int_array"
1158011611 args_id = @nd_arguments[nid]
1158111612 if args_id >= 0
@@ -11583,28 +11614,51 @@
1158311614 if ai.length >= 2
1158411615 vt = infer_type(ai[ai.length - 1])
1158511616 promoted = empty_array_promotion_for([vt])
11586- if promoted != "" && promoted != cur_t
11587- replace_ivar_type(@current_class_idx, iname, promoted)
11588- if promoted == "str_array"
11589- @needs_str_array = 1
11590- elsif promoted == "float_array"
11591- @needs_float_array = 1
11592- elsif promoted == "sym_array"
11593- @needs_int_array = 1
11594- elsif promoted == "poly_array"
11595- @needs_rb_value = 1
11596- @needs_gc = 1
11597- elsif is_ptr_array_type(promoted) == 1
11598- # ptr_array slots hold object pointers, so the
11599- # owning class needs gc_scan emitted; without
11600- # this flag the captured pointers leak and the
11601- # collector misses them. Gemini #241 review.
11602- @needs_gc = 1
11603- end
11617+ end
11618+ end
11619+ elsif cur_t == "obj_Method_ptr_array"
11620+ # Specific case: `<X>_ptr_array<Method>` widened from int_array
11621+ # by the prior empty_array_promotion_for. A subsequent write
11622+ # of a different element type (e.g. optcarrots `@fetch[i] =
11623+ # @ram` where @ram is IntArray) would silently void*-cast
11624+ # into the typed Method slot. Widen to poly_array so the
11625+ # cls_id dispatch catches both Method and the other type at
11626+ # runtime. Restricted to Method here because the other
11627+ # `<X>_ptr_array` shapes (e.g. obj_IntArray_ptr_array for
11628+ # `@sp_map = [@sp_map_buffer[0], ...]`) dont need this and
11629+ # widening them too aggressively breaks downstream
11630+ # operations (`clear` etc.) that have separate poly_array
11631+ # codepaths only in stmt context.
11632+ args_id = @nd_arguments[nid]
11633+ if args_id >= 0
11634+ ai = get_args(args_id)
11635+ if ai.length >= 2
11636+ vt = infer_type(ai[ai.length - 1])
11637+ if vt != "nil" && vt != "obj_Method"
11638+ promoted = "poly_array"
1160411639 end
1160511640 end
1160611641 end
1160711642 end
11643+ if promoted != "" && promoted != cur_t
11644+ replace_ivar_type(@current_class_idx, iname, promoted)
11645+ if promoted == "str_array"
11646+ @needs_str_array = 1
11647+ elsif promoted == "float_array"
11648+ @needs_float_array = 1
11649+ elsif promoted == "sym_array"
11650+ @needs_int_array = 1
11651+ elsif promoted == "poly_array"
11652+ @needs_rb_value = 1
11653+ @needs_gc = 1
11654+ elsif is_ptr_array_type(promoted) == 1
11655+ # ptr_array slots hold object pointers, so the
11656+ # owning class needs gc_scan emitted; without
11657+ # this flag the captured pointers leak and the
11658+ # collector misses them. Gemini #241 review.
11659+ @needs_gc = 1
11660+ end
11661+ end
1160811662 end
1160911663 end
1161011664 # Recurse via the centralized child walker (push_child_ids covers
@@ -26006,6 +26060,35 @@
2600626060 polyc = "sp_PolyArray_get((sp_PolyArray *)" + recv_tmp + ".v.p, " + a0 + ")"
2600726061 polyrhs = is_poly_ret == 1 ? polyc : "(" + polyc + ").v.i"
2600826062 emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) " + result_tmp + " = " + polyrhs + ";")
26063+ # Method dispatch — `bm[arg]` / `bm[a1, a2]` on a Method instance
26064+ # is `bm.call(...)`. When bm came through a heterogeneous
26065+ # poly_array (e.g. optcarrots `@fetch[i] = method(:peek_X)` mixed
26066+ # with `@fetch[i] = @ram` where @fetch is widened to poly_array),
26067+ # the static type at the call site is poly, so the typed
26068+ # obj_Method path at line 20367 doesnt fire. Generate the
26069+ # function-pointer cast + indirect call inline, guarded by a
26070+ # null fn_ptr check (Rubys NoMethodError analogue). Handles up
26071+ # to N args by reusing the same arg_compiled list and unboxing
26072+ # any that came in as poly.
26073+ method_idx = find_class_idx("Method")
26074+ if method_idx >= 0
26075+ mp = "((sp_Method *)" + recv_tmp + ".v.p)"
26076+ sig_args = "void *"
26077+ call_args = "(void *)" + mp + "->iv_self_obj"
26078+ mi = 0
26079+ while mi < arg_compiled.length
26080+ sig_args = sig_args + ", mrb_int"
26081+ ai_val = arg_compiled[mi]
26082+ if mi < arg_types.length && arg_types[mi] == "poly"
26083+ ai_val = "(" + ai_val + ").v.i"
26084+ end
26085+ call_args = call_args + ", " + ai_val
26086+ mi = mi + 1
26087+ end
26088+ mc = "(" + mp + " && " + mp + "->iv_fn_ptr ? ((mrb_int (*)(" + sig_args + "))(uintptr_t)" + mp + "->iv_fn_ptr)(" + call_args + ") : 0)"
26089+ mrhs = is_poly_ret == 1 ? "sp_box_int(" + mc + ")" : mc
26090+ emit(" if (" + recv_tmp + ".cls_id == " + method_idx.to_s + ") " + result_tmp + " = " + mrhs + ";")
26091+ end
2600926092 end
2601026093 # `length` / `size` — every built-in array exposes its own
2601126094 # `_length` helper (sym_array shares IntArray's). PtrArray is
@@ -28844,25 +28927,41 @@
2884428927 k = k + 1
2884528928 end
2884628929 else
28847- # RHS is a function call returning int_array
28848- @needs_int_array = 1
28930+ # RHS is a function call returning a typed array. Dispatch by
28931+ # the RHS's static type — IntArray default, but PolyArray when
28932+ # the inner is heterogeneous (optcarrots `@io_addr,
28933+ # @bg_pattern_lut_fetched, _ = @attr_lut[i]` where attr_lut
28934+ # elements are 3-tuples of mixed types).
28935+ val_t_local = infer_type(val_id)
2884928936 @needs_gc = 1
2885028937 tmp = new_temp
28851- emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")
28938+ if val_t_local == "poly_array"
28939+ @needs_rb_value = 1
28940+ emit(" sp_PolyArray *" + tmp + " = " + compile_expr(val_id) + ";")
28941+ else
28942+ @needs_int_array = 1
28943+ emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")
28944+ end
2885228945 emit(" SP_GC_ROOT(" + tmp + ");")
2885328946 k = 0
2885428947 while k < targets.length
2885528948 tid = targets[k]
28856- rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"
28949+ if val_t_local == "poly_array"
28950+ rhs = "sp_PolyArray_get(" + tmp + ", " + k.to_s + ")"
28951+ val_t_for_target = "poly"
28952+ else
28953+ rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"
28954+ val_t_for_target = "int"
28955+ end
2885728956 # Route ivar / local writes through emit_multi_write_target so
2885828957 # the box-when-slot-is-poly logic fires. Without boxing, an int
2885928958 # RHS into a sp_RbVal slot fails C compile (`@x, @y = arr` where
2886028959 # @y is poly because other writers store other types).
2886128960 if @nd_type[tid] == "LocalVariableTargetNode"
28862- emit_multi_write_target(tid, rhs, "int")
28961+ emit_multi_write_target(tid, rhs, val_t_for_target)
2886328962 end
2886428963 if @nd_type[tid] == "InstanceVariableTargetNode"
28865- emit_multi_write_target(tid, rhs, "int")
28964+ emit_multi_write_target(tid, rhs, val_t_for_target)
2886628965 end
2886728966 if @nd_type[tid] == "ConstantTargetNode"
2886828967 if find_const_idx(@nd_name[tid]) >= 0
@@ -32118,10 +32217,16 @@
3211832217 # the underlying pointer points at. Optcarrot's `@fetch[a] =
3211932218 # method(:peek_X)` lands here when @fetch was widened to poly
3212032219 # (rather than poly_array) by the heterogeneous IntArray + Method
32121- # writes. Dispatch by cls_id, mirroring sp_PolyArray_set's
32122- # interface for the POLY_ARRAY case and using element-typed
32123- # setters for the homogeneous storage variants. Extract the
32124- # appropriate payload from the boxed value via .v.p / .v.i.
32220+ # writes. Dispatch by cls_id at runtime.
32221+ #
32222+ # When the slot's runtime storage is IntArray but the value
32223+ # being stored is non-int (a pointer / poly with cls_id != 0),
32224+ # we'd lose the cls_id by storing as int. Widen the storage at
32225+ # runtime: allocate a fresh PolyArray, copy the existing int
32226+ # elements as boxed-int, then store the new value boxed. Update
32227+ # the slot to hold the new PolyArray. Requires knowing where
32228+ # the slot is — only fires when recv is an ivar / local / cvar
32229+ # / gvar that we can reassign in C.
3212532230 vbox = val
3212632231 vt = "int"
3212732232 if arg_ids.length >= 2
@@ -32132,9 +32237,45 @@
3213232237 end
3213332238 vbox_tmp = new_temp
3213432239 emit(" sp_RbVal " + vbox_tmp + " = " + vbox + ";")
32135- emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");")
32136- emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);")
32137- emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")
32240+ # Determine the slot expression — what we'd assign to in order
32241+ # to replace the storage. Only ivar / local / cvar / gvar are
32242+ # supported; anything else falls back to no-widen (still
32243+ # correct for the homogeneous cases, just not heterogeneous).
32244+ slot_lhs = ""
32245+ if @nd_type[recv] == "InstanceVariableReadNode"
32246+ slot_lhs = self_arrow + sanitize_ivar(@nd_name[recv])
32247+ elsif @nd_type[recv] == "LocalVariableReadNode"
32248+ slot_lhs = fiber_var_ref(@nd_name[recv])
32249+ end
32250+ if slot_lhs != ""
32251+ @needs_gc = 1
32252+ @needs_rb_value = 1
32253+ cur = new_temp
32254+ new_arr = new_temp
32255+ old_int = new_temp
32256+ ii = new_temp
32257+ emit(" sp_RbVal " + cur + " = " + rc + ";")
32258+ # Widen path: if storage is IntArray but value can't fit int
32259+ # (i.e. value carries cls_id != 0 or tag != INT, meaning it's
32260+ # a typed pointer/box that loses info if cast to int).
32261+ emit(" if (" + cur + ".cls_id == SP_BUILTIN_INT_ARRAY && (" + vbox_tmp + ".tag != SP_TAG_INT || " + vbox_tmp + ".cls_id != 0)) {")
32262+ emit(" sp_IntArray *" + old_int + " = (sp_IntArray *)" + cur + ".v.p;")
32263+ emit(" sp_PolyArray *" + new_arr + " = sp_PolyArray_new();")
32264+ 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 + ")));")
32265+ emit(" sp_PolyArray_set(" + new_arr + ", " + idx + ", " + vbox_tmp + ");")
32266+ emit(" " + slot_lhs + " = sp_box_poly_array(" + new_arr + ");")
32267+ emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_POLY_ARRAY) {")
32268+ emit(" sp_PolyArray_set((sp_PolyArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ");")
32269+ emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_PTR_ARRAY) {")
32270+ emit(" sp_PtrArray_set((sp_PtrArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);")
32271+ emit(" } else if (" + cur + ".cls_id == SP_BUILTIN_INT_ARRAY) {")
32272+ emit(" sp_IntArray_set((sp_IntArray *)" + cur + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")
32273+ emit(" }")
32274+ else
32275+ emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");")
32276+ emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);")
32277+ emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")
32278+ end
3213832279 return
3213932280 end
3214032281 if rt == "str_int_hash"