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 >= 211313-
types[ivk] = "poly"11314-
@needs_rb_value = 111315-
changed = 111313+
# When every observed type is itself an array AND the11314+
# current type is already `poly_array` (set by an earlier11315+
# `[]=` widening — see scan_writer_calls), keep it as11316+
# `poly_array` rather than collapsing to `poly`. The11317+
# latter discards element-array semantics: reads on a11318+
# `poly` slot dont dispatch through cls_id_to_storage,11319+
# they box-and-unbox the entire ivar value. Optcarrot's11320+
# `@fetch[a][a]` (heterogeneous IntArray + Method) only11321+
# works when @fetch stays `poly_array`. For other11322+
# combinations (mix of array and non-array, or arrays11323+
# without prior poly_array marker), keep the existing11324+
# widen-to-poly behavior.11325+
all_arrays = 111326+
di = 011327+
while di < distinct.length11328+
if is_array_type(distinct[di]) == 011329+
all_arrays = 011330+
end11331+
di = di + 111332+
end11333+
if all_arrays == 1 && types[ivk] == "poly_array"11334+
# Already poly_array; do nothing — keep it.11335+
elsif all_arrays == 111336+
# Compatible array shapes but not yet poly_array; widen.11337+
types[ivk] = "poly_array"11338+
@needs_rb_value = 111339+
@needs_gc = 111340+
changed = 111341+
else11342+
types[ivk] = "poly"11343+
@needs_rb_value = 111344+
changed = 111345+
end1131611346
end1131711347
end1131811348
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 >= 21158411615
vt = infer_type(ai[ai.length - 1])1158511616
promoted = empty_array_promotion_for([vt])11586-
if promoted != "" && promoted != cur_t11587-
replace_ivar_type(@current_class_idx, iname, promoted)11588-
if promoted == "str_array"11589-
@needs_str_array = 111590-
elsif promoted == "float_array"11591-
@needs_float_array = 111592-
elsif promoted == "sym_array"11593-
@needs_int_array = 111594-
elsif promoted == "poly_array"11595-
@needs_rb_value = 111596-
@needs_gc = 111597-
elsif is_ptr_array_type(promoted) == 111598-
# ptr_array slots hold object pointers, so the11599-
# owning class needs gc_scan emitted; without11600-
# this flag the captured pointers leak and the11601-
# collector misses them. Gemini #241 review.11602-
@needs_gc = 111603-
end11617+
end11618+
end11619+
elsif cur_t == "obj_Method_ptr_array"11620+
# Specific case: `<X>_ptr_array<Method>` widened from int_array11621+
# by the prior empty_array_promotion_for. A subsequent write11622+
# of a different element type (e.g. optcarrots `@fetch[i] =11623+
# @ram` where @ram is IntArray) would silently void*-cast11624+
# into the typed Method slot. Widen to poly_array so the11625+
# cls_id dispatch catches both Method and the other type at11626+
# runtime. Restricted to Method here because the other11627+
# `<X>_ptr_array` shapes (e.g. obj_IntArray_ptr_array for11628+
# `@sp_map = [@sp_map_buffer[0], ...]`) dont need this and11629+
# widening them too aggressively breaks downstream11630+
# operations (`clear` etc.) that have separate poly_array11631+
# codepaths only in stmt context.11632+
args_id = @nd_arguments[nid]11633+
if args_id >= 011634+
ai = get_args(args_id)11635+
if ai.length >= 211636+
vt = infer_type(ai[ai.length - 1])11637+
if vt != "nil" && vt != "obj_Method"11638+
promoted = "poly_array"1160411639
end1160511640
end1160611641
end1160711642
end11643+
if promoted != "" && promoted != cur_t11644+
replace_ivar_type(@current_class_idx, iname, promoted)11645+
if promoted == "str_array"11646+
@needs_str_array = 111647+
elsif promoted == "float_array"11648+
@needs_float_array = 111649+
elsif promoted == "sym_array"11650+
@needs_int_array = 111651+
elsif promoted == "poly_array"11652+
@needs_rb_value = 111653+
@needs_gc = 111654+
elsif is_ptr_array_type(promoted) == 111655+
# ptr_array slots hold object pointers, so the11656+
# owning class needs gc_scan emitted; without11657+
# this flag the captured pointers leak and the11658+
# collector misses them. Gemini #241 review.11659+
@needs_gc = 111660+
end11661+
end1160811662
end1160911663
end1161011664
# 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 instance26064+
# is `bm.call(...)`. When bm came through a heterogeneous26065+
# poly_array (e.g. optcarrots `@fetch[i] = method(:peek_X)` mixed26066+
# with `@fetch[i] = @ram` where @fetch is widened to poly_array),26067+
# the static type at the call site is poly, so the typed26068+
# obj_Method path at line 20367 doesnt fire. Generate the26069+
# function-pointer cast + indirect call inline, guarded by a26070+
# null fn_ptr check (Rubys NoMethodError analogue). Handles up26071+
# to N args by reusing the same arg_compiled list and unboxing26072+
# any that came in as poly.26073+
method_idx = find_class_idx("Method")26074+
if method_idx >= 026075+
mp = "((sp_Method *)" + recv_tmp + ".v.p)"26076+
sig_args = "void *"26077+
call_args = "(void *)" + mp + "->iv_self_obj"26078+
mi = 026079+
while mi < arg_compiled.length26080+
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+
end26085+
call_args = call_args + ", " + ai_val26086+
mi = mi + 126087+
end26088+
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 + ")" : mc26090+
emit(" if (" + recv_tmp + ".cls_id == " + method_idx.to_s + ") " + result_tmp + " = " + mrhs + ";")26091+
end2600926092
end2601026093
# `length` / `size` — every built-in array exposes its own2601126094
# `_length` helper (sym_array shares IntArray's). PtrArray is@@ -28844,25 +28927,41 @@
2884428927
k = k + 12884528928
end2884628929
else28847-
# RHS is a function call returning int_array28848-
@needs_int_array = 128930+
# RHS is a function call returning a typed array. Dispatch by28931+
# the RHS's static type — IntArray default, but PolyArray when28932+
# the inner is heterogeneous (optcarrots `@io_addr,28933+
# @bg_pattern_lut_fetched, _ = @attr_lut[i]` where attr_lut28934+
# elements are 3-tuples of mixed types).28935+
val_t_local = infer_type(val_id)2884928936
@needs_gc = 12885028937
tmp = new_temp28851-
emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")28938+
if val_t_local == "poly_array"28939+
@needs_rb_value = 128940+
emit(" sp_PolyArray *" + tmp + " = " + compile_expr(val_id) + ";")28941+
else28942+
@needs_int_array = 128943+
emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")28944+
end2885228945
emit(" SP_GC_ROOT(" + tmp + ");")2885328946
k = 02885428947
while k < targets.length2885528948
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+
else28953+
rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"28954+
val_t_for_target = "int"28955+
end2885728956
# Route ivar / local writes through emit_multi_write_target so2885828957
# the box-when-slot-is-poly logic fires. Without boxing, an int2885928958
# RHS into a sp_RbVal slot fails C compile (`@x, @y = arr` where2886028959
# @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
end2886428963
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
end2886728966
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 poly3212032219
# (rather than poly_array) by the heterogeneous IntArray + Method32121-
# writes. Dispatch by cls_id, mirroring sp_PolyArray_set's32122-
# interface for the POLY_ARRAY case and using element-typed32123-
# setters for the homogeneous storage variants. Extract the32124-
# 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 value32223+
# 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 at32225+
# runtime: allocate a fresh PolyArray, copy the existing int32226+
# elements as boxed-int, then store the new value boxed. Update32227+
# the slot to hold the new PolyArray. Requires knowing where32228+
# the slot is — only fires when recv is an ivar / local / cvar32229+
# / gvar that we can reassign in C.3212532230
vbox = val3212632231
vt = "int"3212732232
if arg_ids.length >= 2@@ -32132,9 +32237,45 @@
3213232237
end3213332238
vbox_tmp = new_temp3213432239
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 order32241+
# to replace the storage. Only ivar / local / cvar / gvar are32242+
# supported; anything else falls back to no-widen (still32243+
# 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+
end32250+
if slot_lhs != ""32251+
@needs_gc = 132252+
@needs_rb_value = 132253+
cur = new_temp32254+
new_arr = new_temp32255+
old_int = new_temp32256+
ii = new_temp32257+
emit(" sp_RbVal " + cur + " = " + rc + ";")32258+
# Widen path: if storage is IntArray but value can't fit int32259+
# (i.e. value carries cls_id != 0 or tag != INT, meaning it's32260+
# 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+
else32275+
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+
end3213832279
return3213932280
end3214032281
if rt == "str_int_hash"test/method_dispatch_poly_array.rbaddedView file
Loading diff…
test/method_dispatch_poly_array.rb.expectedaddedView file
Loading diff…
test/runtime_widen_int_to_poly_array.rbaddedView file
Loading diff…
test/runtime_widen_int_to_poly_array.rb.expectedaddedView file
Loading diff…