Commit 0cc69bdf

0cc69bdfe6d7Merged

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

Description

feat(analyze,codegen): full promote-mode coverage for params, returns, ivars

Followup to 8b90d13. --int-overflow=promote now widens every remaining int slot in the program, not just local variables:

  • promote_int_to_bigint_globally (new analyze pass): runs after the inference fixpoint and rewrites every int slot found in @meth_param_types, @meth_return_types, @cls_meth_ptypes, @cls_meth_returns, @cls_cmeth_ptypes, @cls_cmeth_returns, and @cls_ivar_types to bigint. The existing bigint codegen path takes over for arithmetic, comparison, and stringification on those slots.

  • refine_method_body_locals: same promote sweep that already fired inside refine_locals_multi_pass_full for main-body locals now also runs for each method body. Without it, def f(n); acc = 1; while ...; acc *= n; ...; acc; end failed to compile because the method's return type was rewritten to bigint but the body local acc stayed mrb_int.

Codegen-side glue so the promoted ABI links cleanly:

  • compile_expr_for_expected_type: new bigint <-> int coercion pair. Int → bigint boxes through sp_bigint_new_int. Bigint → int unboxes through sp_bigint_to_int for the few sites that still want a native int (FFI args, array indices, sp_*_get / _set helpers).

  • compile_typed_call_args: now routes through compile_expr_for_expected_type when either side is bigint, so a call passing an int literal into a now-bigint param boxes correctly. Previously fell through to the bare compile_expr arm and emitted a raw mrb_int that gcc rejected with the usual pointer-from-integer warning + a runtime segfault on the first deref.

  • compile_arg0_as_int: new bigint arm. Indices, sp_*_get args, runtime helpers still expecting mrb_int get a sp_bigint_to_int unbox when the source expression is bigint-typed.

  • compile_upto_block: when the block param's scope type is bigint (promote mode), box the upto counter through sp_bigint_new_int on entry. The for-loop counter stays mrb_int (the bounds are int-literal in the canonical 1.upto(n) { |x| ... } shape).

  • Initialize-body ivar write path: ivar slot typed bigint with a literal int rhs (@n = 0 against a now-bigint @n) boxes through sp_bigint_new_int so the C assign matches the field type.

Verified end-to-end against several shapes that previously failed under promote mode:

  • def square(n); n * n; end; puts square(10_000_000_000) → 100000000000000000000 (10^20, exceeds int64)
  • class Acc; def initialize; @n = 0; end; def add(x); @n += x; end; end; 5.times { a.add(3e18) }; puts a.value → 15000000000000000000 (1.5e19)
  • def factorial(n); acc = 1; (1..n).each { acc *= ... }; acc; end; puts factorial(50) → 30414093201713378043612608166064768844377641568960512000000000000

618 tests pass, optcarrot checksum 59662 (~670 fps in wrap mode via the Makefile rule from 8b90d13), bootstrap fixpoint OK.

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

Details

Commit
0cc69bdfe6d7
Parent
8d087c0f84eb

spinel_analyze.rbmodifiedView file

@@ -20404,6 +20404,128 @@
2040420404 # disagreement and that RBS won, and can fix the source or the
2040520405 # RBS as needed. Restores the RBS-declared type as the final
2040620406 # inferred type either way.
20407+ # `--int-overflow=promote` final sweep: every slot that the
20408+ # inference passes settled on `int` becomes `bigint`. Locals
20409+ # already got rewritten inside refine_locals_multi_pass_full;
20410+ # the slots covered here are the ones the locals pass can't
20411+ # reach -- method params (top-level / class instance / class
20412+ # singleton), method return types, instance variables. The
20413+ # codegen's existing bigint path takes over for arithmetic,
20414+ # comparison, and stringification on those slots; the few
20415+ # genuinely-int sites that can't accept bigint (FFI numeric
20416+ # args, sp_sym storage, array indices) coerce via the existing
20417+ # int<->bigint cast helpers spinel already emits when an
20418+ # explicit bigint local crosses one of those boundaries.
20419+ #
20420+ # No-op when mode != promote.
20421+ def promote_int_to_bigint_globally
20422+ if @int_overflow_mode != "promote"
20423+ return
20424+ end
20425+ # Top-level methods.
20426+ mi = 0
20427+ while mi < @meth_param_types.length
20428+ pt = @meth_param_types[mi].split(",")
20429+ changed = 0
20430+ pk = 0
20431+ while pk < pt.length
20432+ if pt[pk] == "int"
20433+ pt[pk] = "bigint"
20434+ @needs_bigint = 1
20435+ changed = 1
20436+ end
20437+ pk = pk + 1
20438+ end
20439+ if changed == 1
20440+ @meth_param_types[mi] = pt.join(",")
20441+ end
20442+ mi = mi + 1
20443+ end
20444+ mi = 0
20445+ while mi < @meth_return_types.length
20446+ if @meth_return_types[mi] == "int"
20447+ @meth_return_types[mi] = "bigint"
20448+ @needs_bigint = 1
20449+ end
20450+ mi = mi + 1
20451+ end
20452+ # Class instance + class methods.
20453+ ci = 0
20454+ while ci < @cls_names.length
20455+ promote_meth_ptypes_table(ci, @cls_meth_ptypes)
20456+ promote_meth_returns_table(ci, @cls_meth_returns)
20457+ if ci < @cls_cmeth_ptypes.length
20458+ promote_meth_ptypes_table(ci, @cls_cmeth_ptypes)
20459+ end
20460+ if ci < @cls_cmeth_returns.length
20461+ promote_meth_returns_table(ci, @cls_cmeth_returns)
20462+ end
20463+ # Ivars.
20464+ ivt = @cls_ivar_types[ci].split(";")
20465+ changed_iv = 0
20466+ ik = 0
20467+ while ik < ivt.length
20468+ if ivt[ik] == "int"
20469+ ivt[ik] = "bigint"
20470+ @needs_bigint = 1
20471+ changed_iv = 1
20472+ end
20473+ ik = ik + 1
20474+ end
20475+ if changed_iv == 1
20476+ @cls_ivar_types[ci] = ivt.join(";")
20477+ @cls_ivar_types_version = @cls_ivar_types_version + 1
20478+ end
20479+ ci = ci + 1
20480+ end
20481+ end
20482+
20483+ def promote_meth_ptypes_table(ci, tbl)
20484+ if ci >= tbl.length
20485+ return
20486+ end
20487+ all_pt = tbl[ci].split("|")
20488+ changed = 0
20489+ mj = 0
20490+ while mj < all_pt.length
20491+ pts = all_pt[mj].split(",")
20492+ pk = 0
20493+ while pk < pts.length
20494+ if pts[pk] == "int"
20495+ pts[pk] = "bigint"
20496+ @needs_bigint = 1
20497+ changed = 1
20498+ end
20499+ pk = pk + 1
20500+ end
20501+ all_pt[mj] = pts.join(",")
20502+ mj = mj + 1
20503+ end
20504+ if changed == 1
20505+ tbl[ci] = all_pt.join("|")
20506+ end
20507+ end
20508+
20509+ def promote_meth_returns_table(ci, tbl)
20510+ if ci >= tbl.length
20511+ return
20512+ end
20513+ rets = tbl[ci].split(";")
20514+ changed = 0
20515+ mj = 0
20516+ while mj < rets.length
20517+ if rets[mj] == "int"
20518+ rets[mj] = "bigint"
20519+ @needs_bigint = 1
20520+ changed = 1
20521+ end
20522+ mj = mj + 1
20523+ end
20524+ if changed == 1
20525+ tbl[ci] = rets.join(";")
20526+ end
20527+ end
20528+
2040720529 def arbitrate_rbs_vs_inferred_methods
2040820530 # Definite-conflict detection for returns. A LITERAL return whose
2040920531 # type can't unify with the RBS-declared return errors out before
@@ -21309,6 +21431,15 @@
2130921431 # inference pass so the slot we're comparing against is final.
2131021432 arbitrate_rbs_vs_inferred_ivars
2131121433 arbitrate_rbs_vs_inferred_methods
21434+ # `--int-overflow=promote`: force-promote every remaining `int`-
21435+ # typed slot to `bigint` across the whole program. The locals
21436+ # path is already covered by refine_locals_multi_pass_full's
21437+ # promote branch; this sweep finishes the job for ivars,
21438+ # method params, method returns, and class methods. Without it
21439+ # promote mode misses arithmetic that flows through `def f(n);
21440+ # n + huge; end` because the param `n` stays mrb_int and the
21441+ # call site can't widen across the function boundary.
21442+ promote_int_to_bigint_globally
2131221443 # Pre-detect bigint variables before feature detection
2131321444 pre_detect_bigint
2131421445 detect_features
@@ -27427,6 +27558,24 @@
2742727558 # codegen would otherwise compute itself.
2742827559 def refine_method_body_locals(bid, lnames, ltypes, params)
2742927560 scan_locals(bid, lnames, ltypes, params)
27561+ # `--int-overflow=promote`: every int body local becomes bigint
27562+ # before the refinement passes see it. Mirrors the same step in
27563+ # refine_locals_multi_pass_full but applied to method bodies
27564+ # (where the caller passes the lnames / ltypes through this
27565+ # path instead of the multi-pass full one). Without this,
27566+ # `def f(n); acc = 1; while ...; acc *= n; ...; acc; end` had
27567+ # the body local `acc` stuck at int and the C compile failed
27568+ # to return mrb_int from a sp_Bigint *-returning function.
27569+ if @int_overflow_mode == "promote"
27570+ pj = 0
27571+ while pj < lnames.length
27572+ if ltypes[pj] == "int"
27573+ ltypes[pj] = "bigint"
27574+ @needs_bigint = 1
27575+ end
27576+ pj = pj + 1
27577+ end
27578+ end
2743027579 j = 0
2743127580 while j < lnames.length
2743227581 declare_var(lnames[j], ltypes[j])