Commit cd7f832d
cd7f832d64fcMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(analyze,codegen): fuse each_cons(n).with_index(off).map chain
Extend the each_cons fusion (previous commit) to also cover the
three-step arr.each_cons(n).with_index(off).map { ... } chain.
The downstream .map block sees an idx counter as the trailing
param; the fusion emits an mrb_int idx = off; before the
window loop, binds the trailing block param to it, and
post-increments idx at the end of each iteration.
- codegen compile_map_expr: detect recv =
with_index(off)blockless whose own recv iseach_cons(n)blockless. Same detection shape as the times.map fusion above. The window- build step and the destructure shortcut both still apply;with_indexonly adds the idx-counter bookkeeping. - analyze:
with_index(off)blockless on an iterable recv passes the recv's array type through (so a downstream.drop,.take,.maxchained on the result keeps inferring correctly via the existing pass-through rules). - analyze block-param typing: in both walk_and_cache's
block_param_type_at and scan_locals' parallel dispatch,
recognise the
.with_index(off).<consumer>pattern and pin the trailing block param tointinstead of inheriting the source's element shape. Without these,igot typed as the source's sub-array (int_array for an each_cons-of-int_array source), and a block body like[expr, i]widened the resulting array literal to poly_array via the mixed-types check, breaking the downstream consumer.
Refs #566.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cd7f832d64fc
- Parent
- df80cf402cbd
spinel_analyze.rbmodifiedView file
@@ -4566,6 +4566,20 @@
45664566
end45674567
return "int_array"45684568
end4569+
# `with_index(off)` blockless on an iterable receiver: in CRuby4570+
# the result is a fresh Enumerator yielding `[elem, idx]` pairs.4571+
# spinel's codegen consumes `<source>.with_index.map { |e, i| }`4572+
# by fusing the chain into a single loop with an idx counter, so4573+
# the analyzer just needs to pass the receiver's element shape4574+
# through to the downstream `.map` block param.4575+
if mname == "with_index"4576+
if recv >= 0 && @nd_block[nid] < 04577+
rt_wi = infer_type(recv)4578+
if is_array_type(rt_wi) == 1 || is_ptr_array_type(rt_wi) == 14579+
return rt_wi4580+
end4581+
end4582+
end45694583
# `each_cons(n)` / `each_slice(n)` blockless: returns an array of45704584
# sub-arrays of the same kind as the receiver. The chain-fusion45714585
# paths in codegen (compile_map_expr / compile_each_block for@@ -22637,6 +22651,16 @@
2263722651
end2263822652
if mname == "times" || mname == "upto" || mname == "downto"2263922653
types.push("int")22654+
# `<source>.with_index(off).<consumer> { |elem, idx| }`:22655+
# block param at bk=1 is the idx counter (int), not another22656+
# element of the source. Without this, the same elem_type-22657+
# of-array logic below would type idx as the source's22658+
# element shape (e.g. int_array), and a downstream22659+
# `[elem_expr, idx]` literal mixed-types check would widen22660+
# the array to poly_array, breaking the chain-fusion22661+
# consumer.22662+
elsif bk == 1 && @nd_receiver[nid] >= 0 && @nd_type[@nd_receiver[nid]] == "CallNode" && @nd_name[@nd_receiver[nid]] == "with_index" && @nd_block[@nd_receiver[nid]] < 022663+
types.push("int")2264022664
elsif mname == "each" || mname == "each_pair" || mname == "map" || mname == "select" || mname == "filter" || mname == "reject" || mname == "find" || mname == "detect" || mname == "any?" || mname == "all?" || mname == "none?" || mname == "one?" || mname == "count" || mname == "min" || mname == "max" || mname == "sum" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "flat_map" || mname == "filter_map" || mname == "cycle" || mname == "partition"2264122665
# Element iteration: infer block param from collection type2264222666
if recv_type == "str_array"@@ -24845,6 +24869,19 @@
2484524869
if recv >= 02484624870
recv_t = infer_type(recv)2484724871
end24872+
# `<source>.with_index(off).<consumer> { |elem, idx| }` -- the24873+
# block sees the source's element shape at pi=0 and the int idx24874+
# counter at pi=1. The receiver chain here is `with_index(off)`24875+
# whose blockless infer_type returns the source's array type, so24876+
# pi=0 falls through to the normal elem_type_of_array path; only24877+
# pi=1 needs the int override. Without this, pi=1 would inherit24878+
# elem_type_of_array(recv_t) too and the idx local would get24879+
# typed as a sub-array, cascading into `[expr, idx]` literal24880+
# widening to poly_array in the chain fusion's downstream24881+
# consumer.24882+
if pi == 1 && recv >= 0 && @nd_type[recv] == "CallNode" && @nd_name[recv] == "with_index" && @nd_block[recv] < 024883+
return "int"24884+
end2484824885
if mname == "each" || mname == "map" || mname == "flat_map" || mname == "filter" || mname == "select" || mname == "reject" || mname == "find" || mname == "detect" || mname == "find_index" || mname == "find_all" || mname == "count" || mname == "all?" || mname == "any?" || mname == "none?" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "group_by" || mname == "partition" || mname == "uniq" || mname == "tally" || mname == "drop_while" || mname == "take_while" || mname == "filter_map"2484924886
# Hash#each yields |k, v|. elem_type_of_array on a hash type2485024887
# falls back to "int" for both, which then types `puts k + ":"`spinel_codegen.rbmodifiedView file
Loading diff…
test/each_cons_with_index_map_chain_fusion.rbaddedView file
Loading diff…
test/each_cons_with_index_map_chain_fusion.rb.expectedaddedView file
Loading diff…