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 instead
100100 # 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 params
104+ # across closures yet, so the helpers thread per-walk parameters
105+ # through ivars.
106+ @reinfer_lv_target = ""
107+ @reinfer_lv_cur = ""
108+ @reinfer_lv_found = ""
102109 # 1 once analysis has converged and freeze_analysis has filled
103110 # @nd_inferred_type. Switches infer_type to consult the cache
104111 # first; analysis iterations themselves keep recomputing because
@@ -20594,6 +20601,88 @@
2059420601 end
2059520602 end
2059620603
20604+ # Post-precompute_all_scope_decls pass. Widens LV slots whose
20605+ # declared *_ptr_array gets a wider rhs under promote (typically
20606+ # a .map call whose block now returns bigint or a poly_array
20607+ # literal).
20608+ def promote_reinfer_lv_scope_types
20609+ if @int_overflow_mode != "promote"
20610+ return
20611+ end
20612+ sj = 0
20613+ while sj < @nd_scope_types.length
20614+ 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 = 0
20620+ ppj = 0
20621+ while ppj < psj.length && ppj < pnj.length
20622+ 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_t
20626+ psj[ppj] = new_t
20627+ @needs_rb_value = 1
20628+ chj = 1
20629+ end
20630+ end
20631+ ppj = ppj + 1
20632+ end
20633+ if chj == 1
20634+ @nd_scope_types[sj] = psj.join("|")
20635+ end
20636+ end
20637+ sj = sj + 1
20638+ end
20639+ end
20640+
20641+ # Walk every LocalVariableWriteNode in the AST whose name is
20642+ # `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 first
20644+ # writer; under promote the same writer's rhs may now infer wider
20645+ # (e.g. a .map call's bret widens int_array -> poly_array). Uses
20646+ # @reinfer_lv_observed as an accumulator instead of a block param
20647+ # 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 < 0
20650+ return ""
20651+ end
20652+ @reinfer_lv_target = lv_name
20653+ @reinfer_lv_cur = cur_t
20654+ @reinfer_lv_found = ""
20655+ walk_reinfer_lv_writes(scope_nid)
20656+ @reinfer_lv_found
20657+ end
20658+
20659+ def walk_reinfer_lv_writes(nid)
20660+ if nid < 0
20661+ return
20662+ end
20663+ if @nd_type[nid] == "LocalVariableWriteNode" && @nd_name[nid] == @reinfer_lv_target
20664+ expr = @nd_expression[nid]
20665+ if expr >= 0
20666+ 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+ end
20670+ end
20671+ end
20672+ if @nd_body[nid] >= 0
20673+ walk_reinfer_lv_writes(@nd_body[nid])
20674+ end
20675+ if @nd_subsequent[nid] >= 0
20676+ walk_reinfer_lv_writes(@nd_subsequent[nid])
20677+ end
20678+ sub_stmts = parse_id_list(@nd_stmts[nid])
20679+ kk = 0
20680+ while kk < sub_stmts.length
20681+ walk_reinfer_lv_writes(sub_stmts[kk])
20682+ kk = kk + 1
20683+ end
20684+ end
20685+
2059720686 def promote_reinfer_array_returns_table(ci, ret_tbl, body_tbl)
2059820687 if ci >= ret_tbl.length
2059920688 return
@@ -21749,6 +21838,13 @@
2174921838 compute_live_instance_methods
2175021839 @analysis_frozen = 1
2175121840 precompute_all_scope_decls
21841+ # Widen LV slots whose declared *_ptr_array gets a wider rhs
21842+ # under promote (.map call whose block now returns bigint /
21843+ # poly_array literal). Must run after precompute populates
21844+ # @nd_scope_types / @nd_scope_names, but before the int -> bigint
21845+ # sweep below so the widened slot stays poly_array and isn't
21846+ # touched by the scalar-only int -> bigint rule.
21847+ promote_reinfer_lv_scope_types
2175221848 # precompute_all_scope_decls re-derives scope_types from
2175321849 # @meth_param_types and from the local refinement pass; in promote
2175421850 # mode, the latter may have left some method-body locals as "int"