Commit 99731a4b
99731a4b9a34Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
fix(analyze,codegen): N.times.map + poly_array literal accumulator under promote
Extends the array-promote-mode plumbing for nested .map chains:
codegen (compile_map_expr's N.times.map branch):
- When block returns bigint, build a PolyArray (matches the analyzer's infer_call_type widening that fires on bret==bigint). The previous IntArray accumulator with sp_bigint_to_int wrap type-mismatched the downstream chain's PolyArray reads.
- Per-element push for the new PolyArray branch boxes via sp_box_int(sp_bigint_to_int(...)).
codegen (compile_array_literal's poly_array arm):
- Stale-int promote detection now also catches arith CallNodes
whose recv/arg infers as "poly" (not just "bigint"). Without
this, an inner
[x - y, i]literal where x, y came from a PolyArray destruct emitted sp_box_int(sp_poly_sub(...)) -- an sp_RbVal where sp_box_int wants mrb_int.
analyze:
- New promote_reinfer_lv_scope_types pass runs after precompute_all_scope_decls and walks every LV slot typed as ptr_array, re-inferring the rhs of each LocalVariableWriteNode and widening to poly_array when the slot's writer's static type now lands on poly_array. Threads state via @reinfer_lv ivars since self-host doesn't yet support block-closure capture.
Picks up the each_cons_with_index_map_chain_fusion test under promote; each_cons_with_index_map_no_double_gets still has a top-level LV slot whose first-writer rhs walk lands on int_array_ptr_array before the destructure block's poly_array literal is fully resolved -- a deeper structural fix is left for a follow-up.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 99731a4b9a34
- Parent
- 77274f842b45
spinel_analyze.rbmodifiedView file
@@ -99,6 +99,13 @@
9999
# entry so the codegen path becomes O(1) per node lookup instead100100
# of recursively re-walking the subtree on every infer_type call.101101
@nd_inferred_type = "".split(",")102+
# Scratch state used by promote_reinfer_array_returns +103+
# walk_reinfer_lv_writes. Self-host can't capture block params104+
# across closures yet, so the helpers thread per-walk parameters105+
# through ivars.106+
@reinfer_lv_target = ""107+
@reinfer_lv_cur = ""108+
@reinfer_lv_found = ""102109
# 1 once analysis has converged and freeze_analysis has filled103110
# @nd_inferred_type. Switches infer_type to consult the cache104111
# first; analysis iterations themselves keep recomputing because@@ -20594,6 +20601,88 @@
2059420601
end2059520602
end2059620603
20604+
# Post-precompute_all_scope_decls pass. Widens LV slots whose20605+
# declared *_ptr_array gets a wider rhs under promote (typically20606+
# a .map call whose block now returns bigint or a poly_array20607+
# literal).20608+
def promote_reinfer_lv_scope_types20609+
if @int_overflow_mode != "promote"20610+
return20611+
end20612+
sj = 020613+
while sj < @nd_scope_types.length20614+
stj = @nd_scope_types[sj]20615+
snj = sj < @nd_scope_names.length ? @nd_scope_names[sj] : ""20616+
if stj != "" && snj != ""20617+
psj = stj.split("|")20618+
pnj = snj.split("|")20619+
chj = 020620+
ppj = 020621+
while ppj < psj.length && ppj < pnj.length20622+
cur_t = psj[ppj]20623+
if cur_t == "int_array_ptr_array" || cur_t == "str_array_ptr_array" || cur_t == "float_array_ptr_array" || cur_t == "sym_array_ptr_array"20624+
new_t = reinfer_lv_array_type_from_scope(sj, pnj[ppj], cur_t)20625+
if new_t != "" && new_t != cur_t20626+
psj[ppj] = new_t20627+
@needs_rb_value = 120628+
chj = 120629+
end20630+
end20631+
ppj = ppj + 120632+
end20633+
if chj == 120634+
@nd_scope_types[sj] = psj.join("|")20635+
end20636+
end20637+
sj = sj + 120638+
end20639+
end20640+
20641+
# Walk every LocalVariableWriteNode in the AST whose name is20642+
# `lv_name`, re-infer the rhs, and return the widest type observed.20643+
# The default scope_types entry was set by scan_locals on the first20644+
# writer; under promote the same writer's rhs may now infer wider20645+
# (e.g. a .map call's bret widens int_array -> poly_array). Uses20646+
# @reinfer_lv_observed as an accumulator instead of a block param20647+
# because self-host doesn't yet support closures.20648+
def reinfer_lv_array_type_from_scope(scope_nid, lv_name, cur_t)20649+
if scope_nid < 020650+
return ""20651+
end20652+
@reinfer_lv_target = lv_name20653+
@reinfer_lv_cur = cur_t20654+
@reinfer_lv_found = ""20655+
walk_reinfer_lv_writes(scope_nid)20656+
@reinfer_lv_found20657+
end20658+
20659+
def walk_reinfer_lv_writes(nid)20660+
if nid < 020661+
return20662+
end20663+
if @nd_type[nid] == "LocalVariableWriteNode" && @nd_name[nid] == @reinfer_lv_target20664+
expr = @nd_expression[nid]20665+
if expr >= 020666+
rhs_t = infer_type(expr)20667+
if rhs_t == "poly_array" || (is_ptr_array_type(rhs_t) == 1 && rhs_t != @reinfer_lv_cur)20668+
@reinfer_lv_found = "poly_array"20669+
end20670+
end20671+
end20672+
if @nd_body[nid] >= 020673+
walk_reinfer_lv_writes(@nd_body[nid])20674+
end20675+
if @nd_subsequent[nid] >= 020676+
walk_reinfer_lv_writes(@nd_subsequent[nid])20677+
end20678+
sub_stmts = parse_id_list(@nd_stmts[nid])20679+
kk = 020680+
while kk < sub_stmts.length20681+
walk_reinfer_lv_writes(sub_stmts[kk])20682+
kk = kk + 120683+
end20684+
end20685+
2059720686
def promote_reinfer_array_returns_table(ci, ret_tbl, body_tbl)2059820687
if ci >= ret_tbl.length2059920688
return@@ -21749,6 +21838,13 @@
2174921838
compute_live_instance_methods2175021839
@analysis_frozen = 12175121840
precompute_all_scope_decls21841+
# Widen LV slots whose declared *_ptr_array gets a wider rhs21842+
# under promote (.map call whose block now returns bigint /21843+
# poly_array literal). Must run after precompute populates21844+
# @nd_scope_types / @nd_scope_names, but before the int -> bigint21845+
# sweep below so the widened slot stays poly_array and isn't21846+
# touched by the scalar-only int -> bigint rule.21847+
promote_reinfer_lv_scope_types2175221848
# precompute_all_scope_decls re-derives scope_types from2175321849
# @meth_param_types and from the local refinement pass; in promote2175421850
# mode, the latter may have left some method-body locals as "int"spinel_codegen.rbmodifiedView file
Loading diff…