Commit d1dbf690

d1dbf6902bfaMerged

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

Description

Type a single-class nullable object reference as that object, not poly

A self-referential nullable object reference (@nxt of a linked-list node, typed LNode or nil) widened to poly, so every access went through a boxed sp_RbVal and a cls_id switch. Legacy types it sp_LNode * with NULL encoding nil and dereferences directly. This left rbtree at 1.57x and linked_list at 1.47x.

ty_unify now keeps an object type when it also sees nil (the pointer's NULL is nil). A nil arg/write flows as TY_NIL instead of being pre-widened to poly, so it can narrow against an object slot; a slot that only ever saw nil is mapped to poly by the pre-cache backstop. The crucial fix was not widening a param that is already TY_NIL (only-nil-so-far, still able to become an object) to poly.

Supporting changes:

  • codegen obj.nil? on a heap object emits (obj == NULL) rather than the constant false used for non-nullable objects (a value-type stays false).
  • detect_value_types disqualifies a class whose instances are assigned nil: value types have no NULL encoding, so such a class must be a heap object.
  • after the nil->poly backstop, re-run ivar inference so an ivar fed by a now-poly purely-nil param widens to match (else it kept a stale int? and mis-converted a poly nil to 0).

bm_rbtree 1.57x -> 1.05x, bm_linked_list 1.47x -> 1.03x, output matches. make test 845, make bench 57, optcarrot 59662, self-host IR+C fixpoint OK.

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

Details

Commit
d1dbf6902bfa
Parent
3b1f6171d54e

src/analyze.cmodifiedView file

@@ -1376,8 +1376,23 @@
13761376 for (int i = 0; i < c->scopes[s].nlocals; i++) {
13771377 LocalVar *blv = &c->scopes[s].locals[i];
13781378 if (!blv->is_param && blv->type == TY_UNKNOWN) blv->type = TY_POLY;
1379+ /* A slot left at TY_NIL only ever saw nil (it never narrowed against an
1380+ object). It has no object class, so represent it as a boxed-nil poly.
1381+ Applies to params too (a purely-nil param). */
1382+ if (blv->type == TY_NIL) blv->type = TY_POLY;
13791383 }
13801384
1385+ /* Re-run ivar inference now that purely-nil params/locals became poly: an
1386+ ivar fed by such a param (`@x = idx` where every `set` call passed nil)
1387+ was skipped during the fixpoint (its value read as TY_NIL) and may have
1388+ stayed a narrower scalar; with the param now poly the write contributes
1389+ poly so the ivar widens to match. */
1390+ for (int it = 0; it < 8; it++) {
1391+ int ch = infer_ivar_types(c);
1392+ ch |= infer_inherited_ivars(c);
1393+ if (!ch) break;
1394+ }
1395+
13811396 /* finalize: gc-root needs + full node type cache */
13821397 for (int s = 0; s < c->nscopes; s++)
13831398 for (int i = 0; i < c->scopes[s].nlocals; i++)
@@ -1567,6 +1582,31 @@
15671582 }
15681583 }
15691584 }
1585+ /* A nil assignment to a value-object-typed slot makes it nullable. Value
1586+ types are stack values with no NULL encoding, so the class must be a
1587+ heap object instead. (ty_unify keeps an object type when it also sees
1588+ nil; this disqualifies the value-type representation for such a class.) */
1589+ if (!strcmp(ty, "LocalVariableWriteNode") || !strcmp(ty, "InstanceVariableWriteNode")) {
1590+ int v = nt_ref(c->nt, id, "value");
1591+ if (v >= 0 && nt_type(c->nt, v) && !strcmp(nt_type(c->nt, v), "NilNode")) {
1592+ TyKind st = TY_UNKNOWN;
1593+ Scope *s = comp_scope_of(c, id);
1594+ const char *nm = nt_str(c->nt, id, "name");
1595+ if (!strcmp(ty, "LocalVariableWriteNode")) {
1596+ LocalVar *lv = (nm && s) ? scope_local(s, nm) : NULL;
1597+ if (lv) st = lv->type;
1598+ }
1599+ else {
1600+ int cid2 = s ? s->class_id : -1;
1601+ if (cid2 < 0 && c->node_cbody[id] >= 0) cid2 = c->node_cbody[id];
1602+ if (cid2 >= 0 && cid2 < c->nclasses && nm) {
1603+ int iv = comp_ivar_index(&c->classes[cid2], nm);
1604+ if (iv >= 0) st = c->classes[cid2].ivar_types[iv];
1605+ }
1606+ }
1607+ if (ty_is_object(st)) { int q = ty_object_class(st); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
1608+ }
1609+ }
15701610 }
15711611 /* An instance built inside a poly-returning method is liable to be boxed at
15721612 the (poly) return -- sp_box_obj would carry a stack pointer. And an