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:

  1. scan_writer_calls []= widen: extend the existing int_array → ptr_array widening to also widen obj_Method_ptr_array → poly_array when a non-Method element type lands on the same slot. Restricted to Method to avoid false widening on <X>_ptr_array slots that have downstream codepaths only in stmt context (e.g. @sp_map.clear).

  2. finalize_ivar_heterogeneity array-aware widen: when every observed type for a slot is itself an array, widen to poly_array (not poly). Optcarrot's @fetch[a][a] only dispatches correctly when @fetch is poly_array (sp_PolyArray storage) — poly (sp_RbVal slot) just box-and-unboxes the whole ivar value.

  3. emit_poly_builtin_dispatch Method arm: when bm came through a heterogeneous poly_array, the static type at bm[arg] is poly, so the typed-obj_Method dispatch at compile_call_expr line 20420 doesn't fire. Add an explicit cls_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 threading arg_types through 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

@@ -11223,9 +11223,39 @@
1122311223 if ivk < types.length && ivk < obs.length && types[ivk] != "poly"
1122411224 distinct = obs[ivk].split(",")
1122511225 if distinct.length >= 2
11226- types[ivk] = "poly"
11227- @needs_rb_value = 1
11228- changed = 1
11226+ # When every observed type is itself an array AND the
11227+ # current type is already `poly_array` (set by an earlier
11228+ # `[]=` widening — see scan_writer_calls), keep it as
11229+ # `poly_array` rather than collapsing to `poly`. The
11230+ # latter discards element-array semantics: reads on a
11231+ # `poly` slot dont dispatch through cls_id_to_storage,
11232+ # they box-and-unbox the entire ivar value. Optcarrot's
11233+ # `@fetch[a][a]` (heterogeneous IntArray + Method) only
11234+ # works when @fetch stays `poly_array`. For other
11235+ # combinations (mix of array and non-array, or arrays
11236+ # without prior poly_array marker), keep the existing
11237+ # widen-to-poly behavior.
11238+ all_arrays = 1
11239+ di = 0
11240+ while di < distinct.length
11241+ if is_array_type(distinct[di]) == 0
11242+ all_arrays = 0
11243+ end
11244+ di = di + 1
11245+ end
11246+ if all_arrays == 1 && types[ivk] == "poly_array"
11247+ # Already poly_array; do nothing — keep it.
11248+ elsif all_arrays == 1
11249+ # Compatible array shapes but not yet poly_array; widen.
11250+ types[ivk] = "poly_array"
11251+ @needs_rb_value = 1
11252+ @needs_gc = 1
11253+ changed = 1
11254+ else
11255+ types[ivk] = "poly"
11256+ @needs_rb_value = 1
11257+ changed = 1
11258+ end
1122911259 end
1123011260 end
1123111261 ivk = ivk + 1
@@ -11489,6 +11519,7 @@
1148911519 if mname == "[]=" && @current_class_idx >= 0 && recv >= 0 && @nd_type[recv] == "InstanceVariableReadNode"
1149011520 iname = @nd_name[recv]
1149111521 cur_t = cls_ivar_type(@current_class_idx, iname)
11522+ promoted = ""
1149211523 if cur_t == "int_array"
1149311524 args_id = @nd_arguments[nid]
1149411525 if args_id >= 0
@@ -11496,28 +11527,51 @@
1149611527 if ai.length >= 2
1149711528 vt = infer_type(ai[ai.length - 1])
1149811529 promoted = empty_array_promotion_for([vt])
11499- if promoted != "" && promoted != cur_t
11500- replace_ivar_type(@current_class_idx, iname, promoted)
11501- if promoted == "str_array"
11502- @needs_str_array = 1
11503- elsif promoted == "float_array"
11504- @needs_float_array = 1
11505- elsif promoted == "sym_array"
11506- @needs_int_array = 1
11507- elsif promoted == "poly_array"
11508- @needs_rb_value = 1
11509- @needs_gc = 1
11510- elsif is_ptr_array_type(promoted) == 1
11511- # ptr_array slots hold object pointers, so the
11512- # owning class needs gc_scan emitted; without
11513- # this flag the captured pointers leak and the
11514- # collector misses them. Gemini #241 review.
11515- @needs_gc = 1
11516- end
11530+ end
11531+ end
11532+ elsif cur_t == "obj_Method_ptr_array"
11533+ # Specific case: `<X>_ptr_array<Method>` widened from int_array
11534+ # by the prior empty_array_promotion_for. A subsequent write
11535+ # of a different element type (e.g. optcarrots `@fetch[i] =
11536+ # @ram` where @ram is IntArray) would silently void*-cast
11537+ # into the typed Method slot. Widen to poly_array so the
11538+ # cls_id dispatch catches both Method and the other type at
11539+ # runtime. Restricted to Method here because the other
11540+ # `<X>_ptr_array` shapes (e.g. obj_IntArray_ptr_array for
11541+ # `@sp_map = [@sp_map_buffer[0], ...]`) dont need this and
11542+ # widening them too aggressively breaks downstream
11543+ # operations (`clear` etc.) that have separate poly_array
11544+ # codepaths only in stmt context.
11545+ args_id = @nd_arguments[nid]
11546+ if args_id >= 0
11547+ ai = get_args(args_id)
11548+ if ai.length >= 2
11549+ vt = infer_type(ai[ai.length - 1])
11550+ if vt != "nil" && vt != "obj_Method"
11551+ promoted = "poly_array"
1151711552 end
1151811553 end
1151911554 end
1152011555 end
11556+ if promoted != "" && promoted != cur_t
11557+ replace_ivar_type(@current_class_idx, iname, promoted)
11558+ if promoted == "str_array"
11559+ @needs_str_array = 1
11560+ elsif promoted == "float_array"
11561+ @needs_float_array = 1
11562+ elsif promoted == "sym_array"
11563+ @needs_int_array = 1
11564+ elsif promoted == "poly_array"
11565+ @needs_rb_value = 1
11566+ @needs_gc = 1
11567+ elsif is_ptr_array_type(promoted) == 1
11568+ # ptr_array slots hold object pointers, so the
11569+ # owning class needs gc_scan emitted; without
11570+ # this flag the captured pointers leak and the
11571+ # collector misses them. Gemini #241 review.
11572+ @needs_gc = 1
11573+ end
11574+ end
1152111575 end
1152211576 end
1152311577 # Recurse via the centralized child walker (push_child_ids covers
@@ -25884,6 +25938,35 @@
2588425938 polyc = "sp_PolyArray_get((sp_PolyArray *)" + recv_tmp + ".v.p, " + a0 + ")"
2588525939 polyrhs = is_poly_ret == 1 ? polyc : "(" + polyc + ").v.i"
2588625940 emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) " + result_tmp + " = " + polyrhs + ";")
25941+ # Method dispatch — `bm[arg]` / `bm[a1, a2]` on a Method instance
25942+ # is `bm.call(...)`. When bm came through a heterogeneous
25943+ # poly_array (e.g. optcarrots `@fetch[i] = method(:peek_X)` mixed
25944+ # with `@fetch[i] = @ram` where @fetch is widened to poly_array),
25945+ # the static type at the call site is poly, so the typed
25946+ # obj_Method path at line 20367 doesnt fire. Generate the
25947+ # function-pointer cast + indirect call inline, guarded by a
25948+ # null fn_ptr check (Rubys NoMethodError analogue). Handles up
25949+ # to N args by reusing the same arg_compiled list and unboxing
25950+ # any that came in as poly.
25951+ method_idx = find_class_idx("Method")
25952+ if method_idx >= 0
25953+ mp = "((sp_Method *)" + recv_tmp + ".v.p)"
25954+ sig_args = "void *"
25955+ call_args = "(void *)" + mp + "->iv_self_obj"
25956+ mi = 0
25957+ while mi < arg_compiled.length
25958+ sig_args = sig_args + ", mrb_int"
25959+ ai_val = arg_compiled[mi]
25960+ if mi < arg_types.length && arg_types[mi] == "poly"
25961+ ai_val = "(" + ai_val + ").v.i"
25962+ end
25963+ call_args = call_args + ", " + ai_val
25964+ mi = mi + 1
25965+ end
25966+ mc = "(" + mp + " && " + mp + "->iv_fn_ptr ? ((mrb_int (*)(" + sig_args + "))(uintptr_t)" + mp + "->iv_fn_ptr)(" + call_args + ") : 0)"
25967+ mrhs = is_poly_ret == 1 ? "sp_box_int(" + mc + ")" : mc
25968+ emit(" if (" + recv_tmp + ".cls_id == " + method_idx.to_s + ") " + result_tmp + " = " + mrhs + ";")
25969+ end
2588725970 end
2588825971 # `length` / `size` — every built-in array exposes its own
2588925972 # `_length` helper (sym_array shares IntArray's). PtrArray is