Commit 7ff9c36b

7ff9c36b718dMerged

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

Description

analyze: dissolve groundless poly cycles via narrowing re-derivation

The optimistic re-narrow reset each poly ivar/param to UNKNOWN once, then re-ran the monotonic passes. That dissolves a transient-poly lock fed by a one-way dependency, but not a self-referential poly cycle: infer_ivar_types is monotonic (ty_unify only widens), so the first re-run pass that observes the cycle's still-poly member re-locks the ivar to poly, and it re-converges on the poly fixed point. optcarrot's CPU clock is exactly this -- @clk_irq/@clk_nmi/ @clk_target and the clk params form a cycle (@clk_irq = next_interrupt_clock( clk); def next_interrupt_clock(c) ... c) whose real writes are all int (@clk is correctly int, FOREVER_CLOCK is int); the poly is groundless, sustained only by the cycle reading its own poly-typed members.

Make the reset ivars narrowing in the re-run: re-clear them to UNKNOWN at the top of every iteration so each is recomputed as unify-of-its-writes-this-pass, and converge on ivar value-stability. With the cycle's transient poly cleared each pass, the int anchors win and it settles to int; a genuinely heterogeneous ivar's writes still unify to poly within the pass, so it re-widens and stays poly. (When no ivars were reset -- only poly params/returns -- fall back to the normal no-change fixpoint so the param/return passes fully settle.)

@clk_irq/@clk_nmi/@clk_target now infer int, removing the poly arithmetic from the 6502 hot path: optcarrot +~20 fps (678 -> 698, interleaved best-of-8).

Verified: 928 tests pass under both gcc and clang, 57 benchmarks pass, optcarrot checksum 59662, clang clean. Emitted C is unchanged across the suite except three poly-dispatch tests that narrow further and still pass.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
7ff9c36b718d
Parent
a0c358afb94e

src/analyze.cmodifiedView file

@@ -1552,9 +1552,26 @@
15521552 over fixed inputs); dissolves the cleared transient-poly cycles. */
15531553 {
15541554 int any = 0;
1555+ /* Record the reset poly ivars so the re-run can re-clear them FRESH each
1556+ iteration (a narrowing recompute), not just once. infer_ivar_types is
1557+ monotonic (ty_unify only widens), so a one-shot reset still re-locks an
1558+ ivar to poly the first time the re-run observes a transient poly from a
1559+ not-yet-settled slot -- which is exactly what happens in a groundless
1560+ self-referential poly cycle (clk_irq <- next_interrupt_clock(clk) <- clk
1561+ <- clk_irq, whose real writes are all int). Re-clearing each iteration
1562+ makes the ivar = unify-of-its-writes-this-iteration, so once the cycle's
1563+ int anchors dominate it settles to int; a genuinely heterogeneous ivar
1564+ re-widens and stays poly. Bounded narrowing over the lattice. */
1565+ int rcap = 16, nrec = 0;
1566+ int *recCi = (int *)malloc(sizeof(int) * rcap), *recIv = (int *)malloc(sizeof(int) * rcap);
15551567 for (int ci = 0; ci < c->nclasses; ci++)
15561568 for (int iv = 0; iv < c->classes[ci].nivars; iv++)
1557- if (c->classes[ci].ivar_types[iv] == TY_POLY) { const char *_n = c->classes[ci].ivars[iv]; sp_ivwatch(_n && _n[0]=='@' ? _n+1 : _n, "renarrow_reset", TY_POLY, TY_UNKNOWN); c->classes[ci].ivar_types[iv] = TY_UNKNOWN; any = 1; }
1569+ if (c->classes[ci].ivar_types[iv] == TY_POLY) {
1570+ const char *_n = c->classes[ci].ivars[iv]; sp_ivwatch(_n && _n[0]=='@' ? _n+1 : _n, "renarrow_reset", TY_POLY, TY_UNKNOWN);
1571+ c->classes[ci].ivar_types[iv] = TY_UNKNOWN; any = 1;
1572+ if (nrec >= rcap) { rcap *= 2; recCi = realloc(recCi, sizeof(int) * rcap); recIv = realloc(recIv, sizeof(int) * rcap); }
1573+ recCi[nrec] = ci; recIv[nrec] = iv; nrec++;
1574+ }
15581575 for (int s = 0; s < c->nscopes; s++) {
15591576 Scope *sc = &c->scopes[s];
15601577 for (int i = 0; i < sc->nparams; i++) {
@@ -1564,9 +1581,14 @@
15641581 }
15651582 if (reset_locked_iter_block_params(c)) any = 1;
15661583 if (any) {
1584+ TyKind *prev = (TyKind *)malloc(sizeof(TyKind) * (nrec > 0 ? nrec : 1));
15671585 for (int iter = 0; iter < 128; iter++) {
1586+ /* stash last-settled values, then re-clear the reset ivars so they
1587+ recompute fresh (narrowing) this iteration. */
1588+ for (int k = 0; k < nrec; k++) prev[k] = c->classes[recCi[k]].ivar_types[recIv[k]];
1589+ for (int k = 0; k < nrec; k++) c->classes[recCi[k]].ivar_types[recIv[k]] = TY_UNKNOWN;
1590+ sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */
15681591 int ch = 0;
1569- sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */
15701592 ch |= infer_write_types(c);
15711593 ch |= infer_param_types(c);
15721594 ch |= infer_param_hash_value(c);
@@ -1581,9 +1603,20 @@
15811603 ch |= infer_cvar_types(c);
15821604 ch |= infer_inherited_ivars(c);
15831605 ch |= infer_return_types(c);
1584- if (!ch) break;
1606+ /* With reset ivars, the re-clear makes infer_ivar_types report change
1607+ every iteration, so converge on ivar value-stability instead. With
1608+ none (only poly params/returns reset), fall back to the normal
1609+ no-change fixpoint so the param/return passes fully settle. */
1610+ if (nrec > 0) {
1611+ int stable = 1;
1612+ for (int k = 0; k < nrec; k++)
1613+ if (c->classes[recCi[k]].ivar_types[recIv[k]] != prev[k]) { stable = 0; break; }
1614+ if (stable) break;
1615+ } else if (!ch) break;
15851616 }
1617+ free(prev);
15861618 }
1619+ free(recCi); free(recIv);
15871620 }
15881621
15891622 /* Backstop: a parameter still unknown but with a `= nil` default is a