Commit f40d6c98
f40d6c982af6Merged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): heterogeneous IntArray + Method dispatch through poly_array
Master PR #312 added heap-allocated Method objects but left a hole in
poly_array element dispatch: @arr[i] = method(:foo) mixed with
@arr[i] = some_int_array had no widen-to-poly_array path on the
write side, no Method-cls_id arm in the read-side [] dispatch on
poly recv, and the finalize_ivar_heterogeneity aggregator collapsed
the multi-array observation list to plain poly (sp_RbVal slot) which
discards element-array semantics.
Three changes:
-
scan_writer_calls[]=widen: extend the existing int_array → ptr_array widening to also widenobj_Method_ptr_array→poly_arraywhen a non-Method element type lands on the same slot. Restricted to Method to avoid false widening on<X>_ptr_arrayslots that have downstream codepaths only in stmt context (e.g.@sp_map.clear). -
finalize_ivar_heterogeneityarray-aware widen: when every observed type for a slot is itself an array, widen topoly_array(notpoly). Optcarrot's@fetch[a][a]only dispatches correctly when @fetch ispoly_array(sp_PolyArray storage) —poly(sp_RbVal slot) just box-and-unboxes the whole ivar value. -
emit_poly_builtin_dispatchMethod arm: when bm came through a heterogeneous poly_array, the static type atbm[arg]is poly, so the typed-obj_Method dispatch at compile_call_expr line 20420 doesn't fire. Add an explicitcls_id == <Method-class-idx>arm that performs the function- pointer cast + indirect call inline, guarded by a null fn_ptr check (Ruby's NoMethodError analogue). Handles 1+ args by threadingarg_typesthrough and unboxing poly args.
Optcarrot's natural CPU @fetch[addr][addr] (and @store[addr] [addr, val]) memory dispatch now compiles and runs correctly
end-to-end without the cpu_peek/cpu_poke case-dispatch workaround.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- f40d6c982af6
- Parent
- a545e8e0bc93
spinel_codegen.rbmodifiedView file
if ivk < types.length && ivk < obs.length && types[ivk] != "poly" distinct = obs[ivk].split(",") if distinct.length >= 2 types[ivk] = "poly" @needs_rb_value = 1 changed = 1 # When every observed type is itself an array AND the # current type is already `poly_array` (set by an earlier # `[]=` widening — see scan_writer_calls), keep it as # `poly_array` rather than collapsing to `poly`. The # latter discards element-array semantics: reads on a # `poly` slot dont dispatch through cls_id_to_storage, # they box-and-unbox the entire ivar value. Optcarrot's # `@fetch[a][a]` (heterogeneous IntArray + Method) only # works when @fetch stays `poly_array`. For other # combinations (mix of array and non-array, or arrays # without prior poly_array marker), keep the existing # widen-to-poly behavior. all_arrays = 1 di = 0 while di < distinct.length if is_array_type(distinct[di]) == 0 all_arrays = 0 end di = di + 1 end if all_arrays == 1 && types[ivk] == "poly_array" # Already poly_array; do nothing — keep it. elsif all_arrays == 1 # Compatible array shapes but not yet poly_array; widen. types[ivk] = "poly_array" @needs_rb_value = 1 @needs_gc = 1 changed = 1 else types[ivk] = "poly" @needs_rb_value = 1 changed = 1 end end end ivk = ivk + 1 if mname == "[]=" && @current_class_idx >= 0 && recv >= 0 && @nd_type[recv] == "InstanceVariableReadNode" iname = @nd_name[recv] cur_t = cls_ivar_type(@current_class_idx, iname) promoted = "" if cur_t == "int_array" args_id = @nd_arguments[nid] if args_id >= 0 if ai.length >= 2 vt = infer_type(ai[ai.length - 1]) promoted = empty_array_promotion_for([vt]) if promoted != "" && promoted != cur_t replace_ivar_type(@current_class_idx, iname, promoted) if promoted == "str_array" @needs_str_array = 1 elsif promoted == "float_array" @needs_float_array = 1 elsif promoted == "sym_array" @needs_int_array = 1 elsif promoted == "poly_array" @needs_rb_value = 1 @needs_gc = 1 elsif is_ptr_array_type(promoted) == 1 # ptr_array slots hold object pointers, so the # owning class needs gc_scan emitted; without # this flag the captured pointers leak and the # collector misses them. Gemini #241 review. @needs_gc = 1 end end end elsif cur_t == "obj_Method_ptr_array" # Specific case: `<X>_ptr_array<Method>` widened from int_array # by the prior empty_array_promotion_for. A subsequent write # of a different element type (e.g. optcarrots `@fetch[i] = # @ram` where @ram is IntArray) would silently void*-cast # into the typed Method slot. Widen to poly_array so the # cls_id dispatch catches both Method and the other type at # runtime. Restricted to Method here because the other # `<X>_ptr_array` shapes (e.g. obj_IntArray_ptr_array for # `@sp_map = [@sp_map_buffer[0], ...]`) dont need this and # widening them too aggressively breaks downstream # operations (`clear` etc.) that have separate poly_array # codepaths only in stmt context. args_id = @nd_arguments[nid] if args_id >= 0 ai = get_args(args_id) if ai.length >= 2 vt = infer_type(ai[ai.length - 1]) if vt != "nil" && vt != "obj_Method" promoted = "poly_array" end end end end if promoted != "" && promoted != cur_t replace_ivar_type(@current_class_idx, iname, promoted) if promoted == "str_array" @needs_str_array = 1 elsif promoted == "float_array" @needs_float_array = 1 elsif promoted == "sym_array" @needs_int_array = 1 elsif promoted == "poly_array" @needs_rb_value = 1 @needs_gc = 1 elsif is_ptr_array_type(promoted) == 1 # ptr_array slots hold object pointers, so the # owning class needs gc_scan emitted; without # this flag the captured pointers leak and the # collector misses them. Gemini #241 review. @needs_gc = 1 end end end end # Recurse via the centralized child walker (push_child_ids covers polyc = "sp_PolyArray_get((sp_PolyArray *)" + recv_tmp + ".v.p, " + a0 + ")" polyrhs = is_poly_ret == 1 ? polyc : "(" + polyc + ").v.i" emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) " + result_tmp + " = " + polyrhs + ";") # Method dispatch — `bm[arg]` / `bm[a1, a2]` on a Method instance # is `bm.call(...)`. When bm came through a heterogeneous # poly_array (e.g. optcarrots `@fetch[i] = method(:peek_X)` mixed # with `@fetch[i] = @ram` where @fetch is widened to poly_array), # the static type at the call site is poly, so the typed # obj_Method path at line 20367 doesnt fire. Generate the # function-pointer cast + indirect call inline, guarded by a # null fn_ptr check (Rubys NoMethodError analogue). Handles up # to N args by reusing the same arg_compiled list and unboxing # any that came in as poly. method_idx = find_class_idx("Method") if method_idx >= 0 mp = "((sp_Method *)" + recv_tmp + ".v.p)" sig_args = "void *" call_args = "(void *)" + mp + "->iv_self_obj" mi = 0 while mi < arg_compiled.length sig_args = sig_args + ", mrb_int" ai_val = arg_compiled[mi] if mi < arg_types.length && arg_types[mi] == "poly" ai_val = "(" + ai_val + ").v.i" end call_args = call_args + ", " + ai_val mi = mi + 1 end mc = "(" + mp + " && " + mp + "->iv_fn_ptr ? ((mrb_int (*)(" + sig_args + "))(uintptr_t)" + mp + "->iv_fn_ptr)(" + call_args + ") : 0)" mrhs = is_poly_ret == 1 ? "sp_box_int(" + mc + ")" : mc emit(" if (" + recv_tmp + ".cls_id == " + method_idx.to_s + ") " + result_tmp + " = " + mrhs + ";") end end # `length` / `size` — every built-in array exposes its own # `_length` helper (sym_array shares IntArray's). PtrArray istest/method_dispatch_poly_array.rbaddedView file
Loading diff…
test/method_dispatch_poly_array.rb.expectedaddedView file
Loading diff…