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
over fixed inputs); dissolves the cleared transient-poly cycles. */ { int any = 0; /* Record the reset poly ivars so the re-run can re-clear them FRESH each iteration (a narrowing recompute), not just once. infer_ivar_types is monotonic (ty_unify only widens), so a one-shot reset still re-locks an ivar to poly the first time the re-run observes a transient poly from a not-yet-settled slot -- which is exactly what happens in a groundless self-referential poly cycle (clk_irq <- next_interrupt_clock(clk) <- clk <- clk_irq, whose real writes are all int). Re-clearing each iteration makes the ivar = unify-of-its-writes-this-iteration, so once the cycle's int anchors dominate it settles to int; a genuinely heterogeneous ivar re-widens and stays poly. Bounded narrowing over the lattice. */ int rcap = 16, nrec = 0; int *recCi = (int *)malloc(sizeof(int) * rcap), *recIv = (int *)malloc(sizeof(int) * rcap); for (int ci = 0; ci < c->nclasses; ci++) for (int iv = 0; iv < c->classes[ci].nivars; iv++) 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; } 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; if (nrec >= rcap) { rcap *= 2; recCi = realloc(recCi, sizeof(int) * rcap); recIv = realloc(recIv, sizeof(int) * rcap); } recCi[nrec] = ci; recIv[nrec] = iv; nrec++; } for (int s = 0; s < c->nscopes; s++) { Scope *sc = &c->scopes[s]; for (int i = 0; i < sc->nparams; i++) { } if (reset_locked_iter_block_params(c)) any = 1; if (any) { TyKind *prev = (TyKind *)malloc(sizeof(TyKind) * (nrec > 0 ? nrec : 1)); for (int iter = 0; iter < 128; iter++) { /* stash last-settled values, then re-clear the reset ivars so they recompute fresh (narrowing) this iteration. */ for (int k = 0; k < nrec; k++) prev[k] = c->classes[recCi[k]].ivar_types[recIv[k]]; for (int k = 0; k < nrec; k++) c->classes[recCi[k]].ivar_types[recIv[k]] = TY_UNKNOWN; sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */ int ch = 0; sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */ ch |= infer_write_types(c); ch |= infer_param_types(c); ch |= infer_param_hash_value(c); ch |= infer_cvar_types(c); ch |= infer_inherited_ivars(c); ch |= infer_return_types(c); if (!ch) break; /* With reset ivars, the re-clear makes infer_ivar_types report change every iteration, so converge on ivar value-stability instead. With none (only poly params/returns reset), fall back to the normal no-change fixpoint so the param/return passes fully settle. */ if (nrec > 0) { int stable = 1; for (int k = 0; k < nrec; k++) if (c->classes[recCi[k]].ivar_types[recIv[k]] != prev[k]) { stable = 0; break; } if (stable) break; } else if (!ch) break; } free(prev); } free(recCi); free(recIv); } /* Backstop: a parameter still unknown but with a `= nil` default is a