Commit 76e18f57

76e18f573934Merged

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

Description

WIP: experimental full int->poly widen under promote (g_promote_mode gate)

Widen every statically-int slot (param/return/local/ivar/cvar) to poly so a function-argument overflow promotes at runtime: def mul(x,y)=x*y; mul(10**15,10**15) now yields 10**30 (the plan's "本丸" #1). Small values stay inline (SP_TAG_INT, no heap), so this is far cheaper than legacy's all-bigint widen -- validating the poly direction.

Default gate intact (g_promote_mode gate). Promote suite regresses 993 -> 831 (32 fail / 130 error): the boundary buckets where a widened poly slot meets a non-poly site and codegen does not yet coerce. NOT for master until the buckets are closed.

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

Details

Commit
76e18f573934
Parent
3c24d8128da7

src/analyze.cmodifiedView file

@@ -2264,6 +2264,30 @@
22642264 if (!ch) break;
22652265 }
22662266
2267+ /* --int-overflow=promote: widen every statically-int slot (param / return /
2268+ local / ivar / cvar) to poly so an arithmetic result that overflows int64
2269+ can be carried as a boxed bigint (sp_poly_add/mul promote at runtime). A
2270+ poly slot still holds a small value inline (SP_TAG_INT, no heap), so this is
2271+ far cheaper than the legacy "everything becomes sp_Bigint*" widen. Done
2272+ after the fixpoint and before the final node-type cache so reads pick up the
2273+ widened slot types. TY_BIGINT loop vars (detect_bigint_loop_vars) stay
2274+ bigint. EXPERIMENTAL: gated by g_promote_mode; default/wrap untouched. */
2275+ if (g_promote_mode) {
2276+ for (int s = 0; s < c->nscopes; s++) {
2277+ Scope *sc = &c->scopes[s];
2278+ if (sc->ret == TY_INT) sc->ret = TY_POLY;
2279+ for (int i = 0; i < sc->nlocals; i++)
2280+ if (sc->locals[i].type == TY_INT) sc->locals[i].type = TY_POLY;
2281+ }
2282+ for (int ci = 0; ci < c->nclasses; ci++) {
2283+ ClassInfo *cl = &c->classes[ci];
2284+ for (int i = 0; i < cl->nivars; i++)
2285+ if (cl->ivar_types[i] == TY_INT) cl->ivar_types[i] = TY_POLY;
2286+ for (int i = 0; i < cl->ncvars; i++)
2287+ if (cl->cvar_types[i] == TY_INT) cl->cvar_types[i] = TY_POLY;
2288+ }
2289+ }
2290+
22672291 /* finalize: gc-root needs + full node type cache */
22682292 for (int s = 0; s < c->nscopes; s++)
22692293 for (int i = 0; i < c->scopes[s].nlocals; i++)