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
for (int i = 0; i < c->scopes[s].nlocals; i++) { LocalVar *blv = &c->scopes[s].locals[i]; if (!blv->is_param && blv->type == TY_UNKNOWN) blv->type = TY_POLY; /* A slot left at TY_NIL only ever saw nil (it never narrowed against an object). It has no object class, so represent it as a boxed-nil poly. Applies to params too (a purely-nil param). */ if (blv->type == TY_NIL) blv->type = TY_POLY; } /* Re-run ivar inference now that purely-nil params/locals became poly: an ivar fed by such a param (`@x = idx` where every `set` call passed nil) was skipped during the fixpoint (its value read as TY_NIL) and may have stayed a narrower scalar; with the param now poly the write contributes poly so the ivar widens to match. */ for (int it = 0; it < 8; it++) { int ch = infer_ivar_types(c); ch |= infer_inherited_ivars(c); if (!ch) break; } /* finalize: gc-root needs + full node type cache */ for (int s = 0; s < c->nscopes; s++) for (int i = 0; i < c->scopes[s].nlocals; i++) } } } /* A nil assignment to a value-object-typed slot makes it nullable. Value types are stack values with no NULL encoding, so the class must be a heap object instead. (ty_unify keeps an object type when it also sees nil; this disqualifies the value-type representation for such a class.) */ if (!strcmp(ty, "LocalVariableWriteNode") || !strcmp(ty, "InstanceVariableWriteNode")) { int v = nt_ref(c->nt, id, "value"); if (v >= 0 && nt_type(c->nt, v) && !strcmp(nt_type(c->nt, v), "NilNode")) { TyKind st = TY_UNKNOWN; Scope *s = comp_scope_of(c, id); const char *nm = nt_str(c->nt, id, "name"); if (!strcmp(ty, "LocalVariableWriteNode")) { LocalVar *lv = (nm && s) ? scope_local(s, nm) : NULL; if (lv) st = lv->type; } else { int cid2 = s ? s->class_id : -1; if (cid2 < 0 && c->node_cbody[id] >= 0) cid2 = c->node_cbody[id]; if (cid2 >= 0 && cid2 < c->nclasses && nm) { int iv = comp_ivar_index(&c->classes[cid2], nm); if (iv >= 0) st = c->classes[cid2].ivar_types[iv]; } } if (ty_is_object(st)) { int q = ty_object_class(st); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; } } } } /* An instance built inside a poly-returning method is liable to be boxed at the (poly) return -- sp_box_obj would carry a stack pointer. And ansrc/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…