Commit ae761170

ae761170a648Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(analyze): N.times.map LV slot widens to poly_array under promote

r4 = 3000.times.map { |i| ...; i + 1 } previously declared lv_r4 as sp_IntArray * while compile_map_expr's bigint arm built the accumulator as sp_PolyArray * -- C compile failed on the assignment, and downstream r4.length / r4[i] dispatched sp_IntArray helpers against the PolyArray slot under -Werror.

Root cause: under promote, the block param i is widened to bigint in scope_types post-precompute, but walk_and_cache's block-body annotation declared i as int (block_param_type_at's fallback for times.map) and cached i + 1 as int. The reinfer pass's bret_late == "bigint" check missed the case because the cache stayed at "int".

Three changes in promote_reinfer_lv_scope_types:

  1. New block_body_has_promote_widening_arith helper: walks the block body for arith CallNodes (+ - * / % **). Under promote these always emit bigint, so the LV slot has to widen.

  2. walk_reinfer_lv_writes' map check now adds: when expr is N.times.map { ... } under promote and bret_late is "int", probe the block body via the helper. Match → widen LV to poly_array.

  3. New refresh_inferred_type_for_lv_walk: when a scope_types entry widens, refresh every LV read/write of that name to the new type, refresh the LV write's RHS-expression cache to the same type (so codegen's set_var_type(lname, rhs_t) after the assignment doesn't revert the widening with a stale narrower rhs_t), and clear cached return types of CallNodes whose recv is the widened LV (so dispatch falls back to fresh infer and picks the poly_array arm).

Verified:

  • Default tests 649/0/0 unchanged.
  • Promote tests 647/0/0 (was 645/0/2; both ERR tests now PASS).
  • optcarrot checksum 59662 unchanged.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
ae761170a648
Parent
cad6f26b3bf1

spinel_analyze.rbmodifiedView file

@@ -20726,6 +20726,17 @@
2072620726 psj[ppj] = new_t
2072720727 @needs_rb_value = 1
2072820728 chj = 1
20729+ # Refresh @nd_inferred_type entries for `pnj[ppj]` reads /
20730+ # writes / receiver-uses + the RHS expressions of LV writes
20731+ # to the widened type. Without this:
20732+ # - codegen-side compile_array_method_expr dispatches on the
20733+ # pre-widen cached recv type (sp_IntArray_length on a
20734+ # PolyArray slot)
20735+ # - the LV write's `set_var_type(lname, rhs_t)` reverts the
20736+ # scope widening when rhs_t is the cached narrower type
20737+ # `new_t` (typically "poly_array") matches what compile_map_expr's
20738+ # bigint arm actually builds.
20739+ refresh_inferred_type_for_lv_walk(sj, pnj[ppj], new_t)
2072920740 end
2073020741 end
2073120742 ppj = ppj + 1
@@ -20738,6 +20749,87 @@
2073820749 end
2073920750 end
2074020751
20752+ # True (1) if the block body contains any CallNode whose method
20753+ # is an arith op (`+ - * / % **`). Used by walk_reinfer_lv_writes
20754+ # to recognize the canonical `N.times.map { |i| ...; i + 1 }`
20755+ # shape under promote -- the cached "int" classification of the
20756+ # tail's `+` lags behind codegen, which actually widens to bigint
20757+ # and builds a PolyArray accumulator. Only checks the top-level
20758+ # stmts list (block bodies are flat StatementsNode); a deeper
20759+ # walk isn't needed because the promote-widening signal we care
20760+ # about is the tail expression, and arith there is enough to
20761+ # confirm the bigint path.
20762+ def block_body_has_promote_widening_arith(body_nid)
20763+ if body_nid < 0
20764+ return 0
20765+ end
20766+ stmts = parse_id_list(@nd_stmts[body_nid])
20767+ k = 0
20768+ while k < stmts.length
20769+ sid = stmts[k]
20770+ if @nd_type[sid] == "CallNode"
20771+ mn_arith = @nd_name[sid]
20772+ if mn_arith == "+" || mn_arith == "-" || mn_arith == "*" || mn_arith == "/" || mn_arith == "%" || mn_arith == "**"
20773+ return 1
20774+ end
20775+ end
20776+ k = k + 1
20777+ end
20778+ 0
20779+ end
20780+
20781+ # Clear cached @nd_inferred_type entries for every LV read / write
20782+ # of `lv_name` (and any CallNode whose receiver is that LV) inside
20783+ # the scope's body subtree. Pairs with promote_reinfer_lv_scope_types:
20784+ # when a scope-types entry widens from int_array to poly_array
20785+ # post-annotate, downstream call sites like `r4.length` keep the
20786+ # cached "int_array" classification and codegen dispatches the
20787+ # wrong array helper. Clearing forces codegen's infer_type to fall
20788+ # back to find_var_type, which reads the widened scope_types.
20789+ def refresh_inferred_type_for_lv_walk(nid, lv_name, new_t)
20790+ if nid < 0
20791+ return
20792+ end
20793+ nt_rfr = @nd_type[nid]
20794+ if (nt_rfr == "LocalVariableReadNode" || nt_rfr == "LocalVariableWriteNode" || nt_rfr == "LocalVariableOrWriteNode" || nt_rfr == "LocalVariableAndWriteNode" || nt_rfr == "LocalVariableOperatorWriteNode" || nt_rfr == "LocalVariableTargetNode") && @nd_name[nid] == lv_name
20795+ if nid < @nd_inferred_type.length
20796+ @nd_inferred_type[nid] = new_t
20797+ end
20798+ # Also refresh the LV write's RHS-expression cache. The codegen
20799+ # LV write path reads `rhs_t = infer_type(@nd_expression[nid])`
20800+ # and calls `set_var_type(lname, rhs_t)`; if rhs_t is the stale
20801+ # narrower type, set_var_type undoes the scope-widening and
20802+ # downstream `r4.length` dispatches the wrong array helper.
20803+ if nt_rfr == "LocalVariableWriteNode"
20804+ ex_rfr = @nd_expression[nid]
20805+ if ex_rfr >= 0 && ex_rfr < @nd_inferred_type.length
20806+ @nd_inferred_type[ex_rfr] = new_t
20807+ end
20808+ end
20809+ end
20810+ if nt_rfr == "CallNode"
20811+ # A CallNode whose recv is the widened LV: clear its own cached
20812+ # return type so codegen recomputes the dispatch based on the
20813+ # widened recv. We don't overwrite to `new_t` here because the
20814+ # call's RETURN type (e.g. length → int) is unrelated to the
20815+ # array variant; clearing makes infer_type fall back to a fresh
20816+ # compute that consults the refreshed recv.
20817+ rcv_rfr = @nd_receiver[nid]
20818+ if rcv_rfr >= 0 && @nd_type[rcv_rfr] == "LocalVariableReadNode" && @nd_name[rcv_rfr] == lv_name
20819+ if nid < @nd_inferred_type.length
20820+ @nd_inferred_type[nid] = ""
20821+ end
20822+ end
20823+ end
20824+ cs_rfr = []
20825+ push_child_ids(nid, cs_rfr)
20826+ k_rfr = 0
20827+ while k_rfr < cs_rfr.length
20828+ refresh_inferred_type_for_lv_walk(cs_rfr[k_rfr], lv_name, new_t)
20829+ k_rfr = k_rfr + 1
20830+ end
20831+ end
20832+
2074120833 # Walk every LocalVariableWriteNode in the AST whose name is
2074220834 # `lv_name`, re-infer the rhs, and return the widest type observed.
2074320835 # The default scope_types entry was set by scan_locals on the first
@@ -20782,6 +20874,21 @@
2078220874 if bret_late == "bigint"
2078320875 @reinfer_lv_found = "poly_array"
2078420876 end
20877+ # `N.times.map { |i| ...; i + 1 }` under promote: the block tail
20878+ # caches as "int" because walk_and_cache declared `i` as int (the
20879+ # block_param_type_at fallback for times.map). The codegen path
20880+ # widens `i` to bigint and builds a PolyArray. Walk the block body
20881+ # for arith CallNodes -- under promote those always emit bigint,
20882+ # so the LV slot must widen too. Issue: bundle_misc_b's
20883+ # t_gc_root_range_and_times_map.
20884+ if @reinfer_lv_found == "" && @int_overflow_mode == "promote" && bret_late == "int"
20885+ recv_chk = @nd_receiver[expr]
20886+ if recv_chk >= 0 && @nd_type[recv_chk] == "CallNode" && @nd_name[recv_chk] == "times" && @nd_block[recv_chk] < 0
20887+ if block_body_has_promote_widening_arith(map_body) == 1
20888+ @reinfer_lv_found = "poly_array"
20889+ end
20890+ end
20891+ end
2078520892 end
2078620893 end
2078720894 end