Commit fd62cb0f

fd62cb0f0a02Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): widen str_poly_hash for IndexOrWriteNode + map block returning poly + poly slice-assign

Local- and ivar-Hash widening now also fires on h[k] ||= v (IndexOrWriteNode) — previously only the regular h[k] = v CallNode path triggered the str_int_hash → str_poly_hash promotion.

(range).map { ... } with a block returning poly now produces a PolyArray; previously fell through to int_array and silently dropped the block body.

arr[i, n] = src slice-assign with a poly-typed receiver now dispatches by runtime cls_id; previously the fall-through []= path used arg[1] (the slice length) as the assignment value and silently dropped the rhs.

Also pushes @scan_*_flags placeholders for ForNode loop vars, RescueNode references, NumberedParametersNode block params, and regular block params — keeps the flag arrays in lockstep with names[] so empty-hash promotion picks the right index.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
fd62cb0f0a02
Parent
a8d909e5a878

spinel_codegen.rbmodifiedView file

@@ -3825,6 +3825,15 @@
38253825 if is_ptr_array_type(bret) == 1 || bret == "poly_array"
38263826 return "poly_array"
38273827 end
3828+ # Block returns a generic `poly` (e.g. `entries[key]`
3829+ # where `entries` is `str_poly_hash`) — the resulting
3830+ # array is heterogeneous, so encode as poly_array. The
3831+ # @needs_rb_value bookkeeping stays in scan-pass
3832+ # widening; this branch only types the .map result.
3833+ if bret == "poly"
3834+ @needs_rb_value = 1
3835+ return "poly_array"
3836+ end
38283837 # poly_array bret intentionally falls through. Returning
38293838 # poly_array_ptr_array would be more accurate, but ivars
38303839 # holding the result (and the corresponding `[nil] *
@@ -11305,6 +11314,49 @@
1130511314 end
1130611315 end
1130711316 end
11317+ # IndexOrWriteNode (`recv[idx] ||= val`) — scan widens the hash type
11318+ # the same way the regular `[]=` CallNode path does. Without this,
11319+ # optcarrots `entries = {}; entries[k] ||= [...]` leaves `entries`
11320+ # at the str_int_hash default, so reads return int (lost cls_id)
11321+ # and downstream `(0..N).map { entries[k] }` collapses to IntArray.
11322+ if @nd_type[nid] == "IndexOrWriteNode" || @nd_type[nid] == "IndexAndWriteNode" || @nd_type[nid] == "IndexOperatorWriteNode"
11323+ iow_recv = @nd_receiver[nid]
11324+ iow_args_id = @nd_arguments[nid]
11325+ iow_val = @nd_expression[nid]
11326+ if iow_recv >= 0 && iow_args_id >= 0 && iow_val >= 0
11327+ iow_args = get_args(iow_args_id)
11328+ if iow_args.length >= 1
11329+ iow_kt = infer_type(iow_args[0])
11330+ iow_vt = infer_type(iow_val)
11331+ # Widen the recv's hash type if it's still the empty-default
11332+ if @nd_type[iow_recv] == "InstanceVariableReadNode" && @current_class_idx >= 0
11333+ iow_iname = @nd_name[iow_recv]
11334+ iow_cur = cls_ivar_type(@current_class_idx, iow_iname)
11335+ if iow_cur == "str_int_hash"
11336+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
11337+ if iow_promoted != "" && iow_promoted != iow_cur
11338+ replace_ivar_type(@current_class_idx, iow_iname, iow_promoted)
11339+ if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
11340+ @needs_rb_value = 1
11341+ end
11342+ end
11343+ end
11344+ elsif @nd_type[iow_recv] == "LocalVariableReadNode"
11345+ iow_lname = @nd_name[iow_recv]
11346+ iow_cur = find_var_type(iow_lname)
11347+ if iow_cur == "str_int_hash"
11348+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
11349+ if iow_promoted != "" && iow_promoted != iow_cur
11350+ set_var_type(iow_lname, iow_promoted)
11351+ if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
11352+ @needs_rb_value = 1
11353+ end
11354+ end
11355+ end
11356+ end
11357+ end
11358+ end
11359+ end
1130811360 if @nd_type[nid] == "CallNode"
1130911361 mname = @nd_name[nid]
1131011362 recv = @nd_receiver[nid]
@@ -18258,6 +18310,64 @@
1825818310 # literal — concretely-typed hashes (`h = {"a" => 1}`) keep
1825918311 # their declared type even when later []= writes mix value
1826018312 # types.
18313+ # IndexOrWriteNode (`h[k] ||= v`), IndexAndWriteNode (`h[k] &&= v`),
18314+ # IndexOperatorWriteNode (`h[k] += v` etc.) — each implicitly does
18315+ # an `h[k] = ...` write, so widen the local hash type the same way
18316+ # as the regular `[]=` CallNode below. Without this, optcarrots
18317+ # `entries = {}; entries[key] ||= [...]` leaves `entries` at the
18318+ # str_int_hash default — `entries[key]` reads return mrb_int and
18319+ # the surrounding `.map { entries[key] }` collapses to IntArray,
18320+ # losing the cls_id chain on the value pointers.
18321+ if @nd_type[nid] == "IndexOrWriteNode" || @nd_type[nid] == "IndexAndWriteNode" || @nd_type[nid] == "IndexOperatorWriteNode"
18322+ iow_recv = @nd_receiver[nid]
18323+ iow_args_id = @nd_arguments[nid]
18324+ iow_val = @nd_expression[nid]
18325+ if iow_recv >= 0 && iow_args_id >= 0 && iow_val >= 0 && @nd_type[iow_recv] == "LocalVariableReadNode"
18326+ iow_hname = @nd_name[iow_recv]
18327+ iow_aargs = get_args(iow_args_id)
18328+ if iow_aargs.length >= 1
18329+ iow_ki = 0
18330+ while iow_ki < names.length
18331+ if names[iow_ki] == iow_hname
18332+ iow_cur = types[iow_ki]
18333+ iow_promotable = (iow_cur == "str_int_hash" ||
18334+ iow_cur == "str_str_hash" ||
18335+ iow_cur == "int_str_hash" ||
18336+ iow_cur == "sym_int_hash" ||
18337+ iow_cur == "sym_str_hash")
18338+ if iow_promotable && iow_ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[iow_ki] == "1"
18339+ iow_kt = scan_locals_arg_type(iow_aargs[0], names, types, params)
18340+ iow_vt = scan_locals_arg_type(iow_val, names, types, params)
18341+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
18342+ if iow_promoted != "" && iow_promoted != iow_cur
18343+ types[iow_ki] = iow_promoted
18344+ if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
18345+ @scan_empty_hash_flags[iow_ki] = ""
18346+ end
18347+ if iow_promoted == "str_str_hash"
18348+ @needs_str_str_hash = 1
18349+ elsif iow_promoted == "int_str_hash"
18350+ @needs_int_str_hash = 1
18351+ @needs_int_array = 1
18352+ elsif iow_promoted == "sym_int_hash"
18353+ @needs_sym_int_hash = 1
18354+ elsif iow_promoted == "sym_str_hash"
18355+ @needs_sym_str_hash = 1
18356+ elsif iow_promoted == "str_poly_hash"
18357+ @needs_str_poly_hash = 1
18358+ @needs_rb_value = 1
18359+ elsif iow_promoted == "sym_poly_hash"
18360+ @needs_sym_poly_hash = 1
18361+ @needs_rb_value = 1
18362+ end
18363+ end
18364+ end
18365+ end
18366+ iow_ki = iow_ki + 1
18367+ end
18368+ end
18369+ end
18370+ end
1826118371 if @nd_type[nid] == "CallNode"
1826218372 if @nd_name[nid] == "[]="
1826318373 recv = @nd_receiver[nid]
@@ -18352,6 +18462,9 @@
1835218462 end
1835318463 names.push(lname)
1835418464 types.push(elem_type)
18465+ @scan_literal_flags.push("")
18466+ @scan_empty_flags.push("")
18467+ @scan_empty_hash_flags.push("")
1835518468 end
1835618469 end
1835718470 end
@@ -18366,6 +18479,9 @@
1836618479 if not_in(lname, params) == 1
1836718480 names.push(lname)
1836818481 types.push("string")
18482+ @scan_literal_flags.push("")
18483+ @scan_empty_flags.push("")
18484+ @scan_empty_hash_flags.push("")
1836918485 end
1837018486 end
1837118487 end
@@ -18440,6 +18556,9 @@
1844018556 else
1844118557 types.push("int")
1844218558 end
18559+ @scan_literal_flags.push("")
18560+ @scan_empty_flags.push("")
18561+ @scan_empty_hash_flags.push("")
1844318562 end
1844418563 end
1844518564 nk = nk + 1
@@ -18455,6 +18574,9 @@
1845518574 if not_in(bname, names) == 1
1845618575 if not_in(bname, params) == 1
1845718576 names.push(bname)
18577+ @scan_literal_flags.push("")
18578+ @scan_empty_flags.push("")
18579+ @scan_empty_hash_flags.push("")
1845818580 # Infer type from receiver context
1845918581 recv_type = ""
1846018582 if @nd_receiver[nid] >= 0
@@ -31674,6 +31796,17 @@
3167431796 return
3167531797 end
3167631798 end
31799+ # Slice assignment with poly recv: `@bg_pixels[xfine, 8] =
31800+ # pattern_lut[bg_pattern]` against an iv typed as poly (because
31801+ # widening saw it as either an int_array or a poly_array at
31802+ # different points). Without this, the fall-through `[]=` path
31803+ # would treat arg_ids[1] (the slice length) as the value to
31804+ # store and silently drop the actual rhs. Dispatch by runtime
31805+ # cls_id and copy len elements from src to dest.
31806+ if arg_ids.length == 3 && rt == "poly"
31807+ compile_poly_slice_assign(nid, recv, rc, arg_ids)
31808+ return
31809+ end
3167731810 idx = "0"
3167831811 val = "0"
3167931812 if arg_ids.length >= 1
@@ -31785,6 +31918,48 @@
3178531918 end
3178631919 end
3178731920
31921+ # `arr[start, len] = src` where arr is a poly-typed slot (sp_RbVal).
31922+ # Dispatches by runtime cls_id of arr and src and emits a per-index
31923+ # copy. Same-length only — `src.length` must equal `len`. Falls
31924+ # back silently when src isn't an array (no copy emitted).
31925+ def compile_poly_slice_assign(nid, recv, rc, arg_ids)
31926+ @needs_rb_value = 1
31927+ start_id = arg_ids[0]
31928+ len_id = arg_ids[1]
31929+ src_id = arg_ids[2]
31930+ start_e = compile_expr(start_id)
31931+ len_e = compile_expr(len_id)
31932+ src_e = compile_expr_gc_rooted(src_id)
31933+ src_t = infer_type(src_id)
31934+ # Box src to poly so we can switch on cls_id at runtime even when
31935+ # spinel statically inferred a concrete array kind (e.g. when src
31936+ # is `pattern_lut[bg_pattern]` returning a poly value boxed from
31937+ # a typed array push).
31938+ if src_t != "poly"
31939+ src_e = box_value_to_poly(src_t, src_e)
31940+ end
31941+ rc_t = new_temp
31942+ src_t_v = new_temp
31943+ start_t = new_temp
31944+ len_t = new_temp
31945+ ii = new_temp
31946+ emit(" {")
31947+ emit(" sp_RbVal " + rc_t + " = " + rc + ";")
31948+ emit(" sp_RbVal " + src_t_v + " = " + src_e + ";")
31949+ emit(" mrb_int " + start_t + " = " + start_e + ";")
31950+ emit(" mrb_int " + len_t + " = " + len_e + ";")
31951+ emit(" if (" + rc_t + ".cls_id == SP_BUILTIN_INT_ARRAY && " + src_t_v + ".cls_id == SP_BUILTIN_INT_ARRAY) {")
31952+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < " + len_t + "; " + ii + "++) sp_IntArray_set((sp_IntArray *)" + rc_t + ".v.p, " + start_t + " + " + ii + ", sp_IntArray_get((sp_IntArray *)" + src_t_v + ".v.p, " + ii + "));")
31953+ emit(" } else if (" + rc_t + ".cls_id == SP_BUILTIN_POLY_ARRAY && " + src_t_v + ".cls_id == SP_BUILTIN_POLY_ARRAY) {")
31954+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < " + len_t + "; " + ii + "++) sp_PolyArray_set((sp_PolyArray *)" + rc_t + ".v.p, " + start_t + " + " + ii + ", sp_PolyArray_get((sp_PolyArray *)" + src_t_v + ".v.p, " + ii + "));")
31955+ emit(" } else if (" + rc_t + ".cls_id == SP_BUILTIN_INT_ARRAY && " + src_t_v + ".cls_id == SP_BUILTIN_POLY_ARRAY) {")
31956+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < " + len_t + "; " + ii + "++) sp_IntArray_set((sp_IntArray *)" + rc_t + ".v.p, " + start_t + " + " + ii + ", sp_PolyArray_get((sp_PolyArray *)" + src_t_v + ".v.p, " + ii + ").v.i);")
31957+ emit(" } else if (" + rc_t + ".cls_id == SP_BUILTIN_POLY_ARRAY && " + src_t_v + ".cls_id == SP_BUILTIN_INT_ARRAY) {")
31958+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < " + len_t + "; " + ii + "++) sp_PolyArray_set((sp_PolyArray *)" + rc_t + ".v.p, " + start_t + " + " + ii + ", sp_box_int(sp_IntArray_get((sp_IntArray *)" + src_t_v + ".v.p, " + ii + ")));")
31959+ emit(" }")
31960+ emit(" }")
31961+ end
31962+
3178831963 # Compile `arr[start, len] = src` slice assignment. Replaces
3178931964 # `len` elements of `arr` starting at `start` with the elements
3179031965 # of `src`. Same-length only — `src.length` must equal `len` at
@@ -33773,7 +33948,7 @@
3377333948 block_is_typed_array = (block_ret_r == "int_array" || block_ret_r == "str_array" ||
3377433949 block_ret_r == "float_array" || block_ret_r == "sym_array" ||
3377533950 is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array")
33776- if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float" && block_is_typed_array == false
33951+ if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float" && block_ret_r != "poly" && block_is_typed_array == false
3377733952 return "0"
3377833953 end
3377933954 @needs_int_array = 1
@@ -33785,6 +33960,12 @@
3378533960 elsif block_ret_r == "float"
3378633961 @needs_float_array = 1
3378733962 emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
33963+ elsif block_ret_r == "poly"
33964+ # Block returns a generic poly value (e.g. `entries[key]`
33965+ # over a str_poly_hash) — accumulator must be PolyArray
33966+ # since IntArray would lose the cls_id tag on the value.
33967+ @needs_rb_value = 1
33968+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
3378833969 elsif block_is_typed_array
3378933970 # Array-of-<X>: 1D-block-return uses PtrArray (matches
3379033971 # the inferred `<bret>_ptr_array` type). Deeper nesting
@@ -33861,6 +34042,8 @@
3386134042 emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")
3386234043 elsif block_ret_r == "float"
3386334044 emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")
34045+ elsif block_ret_r == "poly"
34046+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + lastv_r + ");")
3386434047 elsif block_is_typed_array
3386534048 if is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array"
3386634049 emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_r, lastv_r) + ");")