Commit 20a6038f

20a6038f97f3Merged

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

Description

Optimistically re-narrow poly slots after the fixpoint

The monotonic fixpoint locks a slot to poly the first time it sees a transient poly -- a value read before its type settled, or an index/hash promotion driven by a key that was poly at the time. Once an upstream value later resolves to int (e.g. @io_addr after the nested-array narrow), the downstream slots that locked on its earlier poly state stay stuck.

After the fixpoint, reset every poly ivar and poly non-block param to UNKNOWN once and re-run the monotonic passes. The non-poly slots are now stable, so a slot whose entire contribution closure is concrete re-narrows to that type; a genuinely heterogeneous slot re-widens to poly. This is a sound monotonic re-derivation over fixed inputs.

Dissolves the cleared transient-poly cycles (the PPU register/sprite slots that had locked on @io_addr's earlier poly). optcarrot ~527 -> ~626 fps, checksum 59662. make test 845, make bench 57.

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

Details

Commit
20a6038f97f3
Parent
1862c705e7fd

src/analyze.cmodifiedView file

@@ -984,6 +984,47 @@
984984 if (!ch) break;
985985 }
986986
987+ /* Optimistic re-narrow: the monotonic fixpoint locks a slot to poly the
988+ first time it sees a transient poly (a value read before its type settled,
989+ or an index/hash promotion driven by a then-poly key). Reset every poly
990+ ivar and poly (non-block) param to UNKNOWN once, then re-run the monotonic
991+ passes. With the now-stable concrete slots feeding them, a slot whose
992+ entire contribution closure is concrete re-narrows; one with a genuine
993+ heterogeneous contribution re-widens to poly. Sound (monotonic re-derive
994+ over fixed inputs); dissolves the cleared transient-poly cycles. */
995+ {
996+ int any = 0;
997+ for (int ci = 0; ci < c->nclasses; ci++)
998+ for (int iv = 0; iv < c->classes[ci].nivars; iv++)
999+ if (c->classes[ci].ivar_types[iv] == TY_POLY) { c->classes[ci].ivar_types[iv] = TY_UNKNOWN; any = 1; }
1000+ for (int s = 0; s < c->nscopes; s++) {
1001+ Scope *sc = &c->scopes[s];
1002+ for (int i = 0; i < sc->nparams; i++) {
1003+ LocalVar *p = scope_local(sc, sc->pnames[i]);
1004+ if (p && p->type == TY_POLY && !p->is_block_param) { p->type = TY_UNKNOWN; any = 1; }
1005+ }
1006+ }
1007+ if (any) {
1008+ for (int iter = 0; iter < 128; iter++) {
1009+ int ch = 0;
1010+ ch |= infer_write_types(c);
1011+ ch |= infer_param_types(c);
1012+ ch |= propagate_prep_params(c);
1013+ ch |= infer_string_params(c);
1014+ ch |= infer_default_param_types(c);
1015+ ch |= infer_block_params(c);
1016+ ch |= infer_for_index(c);
1017+ ch |= infer_global_const_types(c);
1018+ ch |= infer_multiwrite_const_types(c);
1019+ ch |= infer_ivar_types(c);
1020+ ch |= infer_cvar_types(c);
1021+ ch |= infer_inherited_ivars(c);
1022+ ch |= infer_return_types(c);
1023+ if (!ch) break;
1024+ }
1025+ }
1026+ }
1027+
9871028 /* Backstop: a parameter still unknown but with a `= nil` default is a
9881029 nullable param -- represent it as poly so it can hold nil or a value.
9891030 Also widen TY_SYMBOL/TY_BOOL params: those types have no nil sentinel