Commit 604165c6

604165c6025dMerged

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

Description

Fix three inference gaps hit by the yjit-bench suite

  • Register top-level define_method(:name) { ... } as a method scope. The registration was gated on class scope, so a top-level define_method's block params stayed untyped (bm_send_bmethod).

  • Stop treating a 2-arg [] as hash-read evidence in the empty-hash promotion pass: hash reads take one key (fetch keeps (key, default)); s[from, len] is a string/array slice. Reading @tmpl[i, n] was promoting a string ivar to poly_poly_hash, which then unified with the string writes to poly and broke .strip inference (bm_jekyll_lite).

  • Guard ivar [] -read promotion against fixpoint ordering: a param-fed ivar (@s = s) reads UNKNOWN before its call-site type arrives, and a 1-arg int read like @s[i] mis-promoted it to a hash. Promote an ivar from reads only when every assignment to it is an empty {} literal — the actual empty-hash case, and a syntactic test that is stable across fixpoint iterations (bm_jekyll_lite's Template, bm_getivar_module).

All three benchmarks now pass end-to-end. make test stays green (845 pass), including the empty-hash promotion tests.

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

Details

Commit
604165c6025d
Parent
e07a18051bab

src/analyze_pass.cmodifiedView file

@@ -571,9 +571,12 @@
571571 else if (name && !strcmp(name, "[]=") && an == 2) {
572572 is_idx_write = 1; kt = infer_type(c, argv[0]); vt = infer_type(c, argv[1]);
573573 }
574- else if (name && (!strcmp(name, "fetch") || !strcmp(name, "[]")) && an >= 1) {
574+ else if (name && (!strcmp(name, "fetch") ||
575+ (!strcmp(name, "[]") && an == 1)) && an >= 1) {
575576 /* hash.fetch(key,..) / hash[key]: promote TY_UNKNOWN local to a typed hash.
576- Only fires when the slot is currently TY_UNKNOWN (empty hash). */
577+ Only fires when the slot is currently TY_UNKNOWN (empty hash).
578+ A 2-arg [] is a string/array slice, never a hash read — only the
579+ 1-arg form is key-lookup evidence (fetch keeps >=1: (key, default)). */
577580 TyKind rslot = TY_UNKNOWN;
578581 const char *rrty = nt_type(nt, recv);
579582 const char *rnm2 = NULL;
@@ -587,6 +590,33 @@
587590 a str_str_hash with the promotion's str_poly target would widen the
588591 slot to poly. Only an untyped (empty-{}) ivar promotes here. */
589592 rslot = infer_type(c, recv);
593+ /* Fixpoint-ordering hazard: a param-fed ivar (`@s = s`) reads
594+ UNKNOWN before the param's call-site type arrives, and a read
595+ like `@s[i]` would mis-promote it to a hash. Promote from reads
596+ only when every assignment to the ivar is an empty `{}` literal
597+ (the actual empty-hash case) — a syntactic test that is stable
598+ across fixpoint iterations. */
599+ if (rslot == TY_UNKNOWN) {
600+ const char *pin = nt_str(nt, recv, "name");
601+ int blocked = 0;
602+ for (int wi = 0; wi < nt->count && !blocked; wi++) {
603+ const char *wty = nt_type(nt, wi);
604+ if (!wty) continue;
605+ if (strcmp(wty, "InstanceVariableWriteNode") &&
606+ strcmp(wty, "InstanceVariableOrWriteNode")) continue;
607+ const char *wnm = nt_str(nt, wi, "name");
608+ if (!wnm || !pin || strcmp(wnm, pin)) continue;
609+ int wv = nt_ref(nt, wi, "value");
610+ const char *wvty = wv >= 0 ? nt_type(nt, wv) : NULL;
611+ int is_empty_hash = 0;
612+ if (wvty && !strcmp(wvty, "HashNode")) {
613+ int hn = 0; nt_arr(nt, wv, "elements", &hn);
614+ if (hn == 0) is_empty_hash = 1;
615+ }
616+ if (!is_empty_hash) blocked = 1;
617+ }
618+ if (blocked) continue;
619+ }
590620 }
591621 if (rslot != TY_UNKNOWN) continue; /* already typed, skip */
592622 /* Only promote via [] read if the receiver local has at least one