Commit 4819d64b

4819d64b94adMerged

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

Description

codegen: narrow monomorphic object arrays to an unboxed pointer array

A POLY_ARRAY local/param whose every element is an instance of one user class X, and whose every use is in a small supported op set (index, push, length, []=, first/last, empty?), is narrowed post-fixpoint to a new TY_OBJ_ARRAY(X) type backed by the runtime sp_PtrArray of unboxed sp_X*. Indexing then yields a typed sp_X * directly, so arr[i].field is a plain struct read instead of an sp_RbVal box plus a cls-id switch.

The new type is produced ONLY by the narrow_object_arrays pass, never by forward inference, so it cannot cascade through the type fixpoint. The pass is interprocedurally sound via optimistic-then-revoke union-find: a slot flowing (as a positional arg, or an alias b = a) into another slot shares one C container type, so the two are unioned and the whole component narrows together or not at all. Any unmodeled use, class conflict, unresolved flow, or absent object evidence kills the component, leaving it TY_POLY_ARRAY. A dependent local b = arr[i] is narrowed to the element object type so its field accesses unbox too.

bm_nbody: 1.64s -> 1.32s at n=20M (~19% faster); the O(n^2) inner loop's sp_PolyArray_get (15.8% of runtime) and per-field cls-id dispatch are gone. Gates intact: test 999/0/0, bench 57/57, optcarrot checksum 59662.

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

Details

Commit
4819d64b94ad
Parent
0f1f816c3a55

src/analyze.cmodifiedView file

@@ -1502,6 +1502,221 @@
15021502 return changed;
15031503 }
15041504
1505+/* ===== Post-fixpoint: narrow monomorphic object arrays to TY_OBJ_ARRAY =====
1506+ A POLY_ARRAY local/param whose every element is an instance of one user
1507+ class X, and whose every use is in a small supported op set (index, push,
1508+ length, ...), is narrowed to ty_obj_array(X) -- the runtime sp_PtrArray of
1509+ unboxed sp_X*, dropping the per-element boxing and cls-id dispatch that a
1510+ poly array pays on every `arr[i]` / `arr[i].field`. Interprocedural soundness
1511+ is by an optimistic-then-revoke union-find: a slot flowing (as a positional
1512+ arg, or an alias `b = a`) into another slot shares one C container type, so
1513+ the two are unioned and the whole component narrows together or not at all.
1514+ Strictly conservative: any unmodeled use, class conflict, unresolved flow, or
1515+ absent object evidence kills the component, leaving it TY_POLY_ARRAY. Runs
1516+ ONCE, after the fixpoint, so the new type never feeds forward inference. */
1517+typedef struct { int sidx; LocalVar *lv; int cls; int alive; int uf; } OAS;
1518+
1519+static int oa_find(OAS *sl, int n, int sidx, LocalVar *lv) {
1520+ for (int i = 0; i < n; i++) if (sl[i].sidx == sidx && sl[i].lv == lv) return i;
1521+ return -1;
1522+}
1523+static int oa_uf_find(OAS *sl, int i) {
1524+ while (sl[i].uf != i) { sl[i].uf = sl[sl[i].uf].uf; i = sl[i].uf; }
1525+ return i;
1526+}
1527+static void oa_uf_union(OAS *sl, int a, int b) {
1528+ int ra = oa_uf_find(sl, a), rb = oa_uf_find(sl, b);
1529+ if (ra != rb) sl[ra].uf = rb;
1530+}
1531+/* the single user-object class of a node's value, or -1 if not a lone object. */
1532+static int oa_obj_class_of(Compiler *c, int node) {
1533+ TyKind t = infer_type(c, node);
1534+ return ty_is_object(t) ? ty_object_class(t) : -1;
1535+}
1536+/* class evidence join: -1 = none seen, -2 = conflicting classes. */
1537+static int oa_cls_join(int a, int b) {
1538+ if (b == -1) return a;
1539+ if (a == -1) return b;
1540+ if (a == b) return a;
1541+ return -2;
1542+}
1543+static int oa_recv_op_ok(const char *nm, int argc, int has_block) {
1544+ if (!nm || has_block) return 0;
1545+ if ((!strcmp(nm, "[]") || !strcmp(nm, "at")) && argc == 1) return 1;
1546+ if (!strcmp(nm, "[]=") && argc == 2) return 1;
1547+ if ((!strcmp(nm, "push") || !strcmp(nm, "<<") || !strcmp(nm, "append")) && argc >= 1) return 1;
1548+ if ((!strcmp(nm, "length") || !strcmp(nm, "size")) && argc == 0) return 1;
1549+ if (!strcmp(nm, "empty?") && argc == 0) return 1;
1550+ if ((!strcmp(nm, "first") || !strcmp(nm, "last")) && argc == 0) return 1;
1551+ return 0;
1552+}
1553+
1554+static void narrow_object_arrays(Compiler *c) {
1555+ const NodeTable *nt = c->nt;
1556+ /* 1. candidate slots: POLY_ARRAY locals/params (skip block params + rbs). */
1557+ int cap = 16, n = 0;
1558+ OAS *sl = (OAS *)malloc(sizeof(OAS) * cap);
1559+ for (int s = 0; s < c->nscopes; s++) {
1560+ Scope *sc = &c->scopes[s];
1561+ for (int li = 0; li < sc->nlocals; li++) {
1562+ LocalVar *lv = &sc->locals[li];
1563+ if (lv->type != TY_POLY_ARRAY || lv->is_block_param || lv->rbs_seeded) continue;
1564+ if (n >= cap) { cap *= 2; sl = (OAS *)realloc(sl, sizeof(OAS) * cap); }
1565+ sl[n].sidx = s; sl[n].lv = lv; sl[n].cls = -1; sl[n].alive = 1; sl[n].uf = n; n++;
1566+ }
1567+ }
1568+ if (n == 0) { free(sl); return; }
1569+ int nc = nt->count ? nt->count : 1;
1570+ int *read_slot = (int *)malloc(sizeof(int) * nc);
1571+ char *claimed = (char *)calloc(nc, 1);
1572+
1573+ /* 2. map each LocalVariableReadNode of a slot to its slot index. */
1574+ for (int id = 0; id < nt->count; id++) {
1575+ read_slot[id] = -1;
1576+ const char *ty = nt_type(nt, id);
1577+ if (!ty || strcmp(ty, "LocalVariableReadNode")) continue;
1578+ int sidx = c->nscope[id];
1579+ const char *nm = nt_str(nt, id, "name");
1580+ LocalVar *lv = nm ? scope_local(&c->scopes[sidx], nm) : NULL;
1581+ if (lv) read_slot[id] = oa_find(sl, n, sidx, lv);
1582+ }
1583+
1584+ /* 3. op-assign / multi-target write forms on a slot are unmodeled -> kill. */
1585+ for (int id = 0; id < nt->count; id++) {
1586+ const char *ty = nt_type(nt, id);
1587+ if (!ty) continue;
1588+ if (strcmp(ty, "LocalVariableOperatorWriteNode") &&
1589+ strcmp(ty, "LocalVariableTargetNode") &&
1590+ strcmp(ty, "LocalVariableAndWriteNode") &&
1591+ strcmp(ty, "LocalVariableOrWriteNode")) continue;
1592+ int sidx = c->nscope[id];
1593+ const char *nm = nt_str(nt, id, "name");
1594+ LocalVar *lv = nm ? scope_local(&c->scopes[sidx], nm) : NULL;
1595+ int si = lv ? oa_find(sl, n, sidx, lv) : -1;
1596+ if (si >= 0) sl[si].alive = 0;
1597+ }
1598+
1599+ /* 4. CallNodes: classify slot-as-receiver (supported op + element evidence)
1600+ and slot-as-arg (positional into a resolvable free method -> edge). */
1601+ for (int id = 0; id < nt->count; id++) {
1602+ const char *ty = nt_type(nt, id);
1603+ if (!ty || strcmp(ty, "CallNode")) continue;
1604+ const char *name = nt_str(nt, id, "name");
1605+ int recv = nt_ref(nt, id, "receiver");
1606+ int has_block = nt_ref(nt, id, "block") >= 0;
1607+ int args = nt_ref(nt, id, "arguments");
1608+ int argc = 0; const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &argc) : NULL;
1609+ if (recv >= 0 && read_slot[recv] >= 0) {
1610+ int S = read_slot[recv];
1611+ if (oa_recv_op_ok(name, argc, has_block)) {
1612+ claimed[recv] = 1;
1613+ if (name && (!strcmp(name, "push") || !strcmp(name, "<<") || !strcmp(name, "append")))
1614+ for (int a = 0; a < argc; a++) sl[S].cls = oa_cls_join(sl[S].cls, oa_obj_class_of(c, argv[a]));
1615+ else if (name && !strcmp(name, "[]=") && argc == 2)
1616+ sl[S].cls = oa_cls_join(sl[S].cls, oa_obj_class_of(c, argv[1]));
1617+ }
1618+ else sl[S].alive = 0;
1619+ }
1620+ for (int k = 0; argv && k < argc; k++) {
1621+ int a = argv[k];
1622+ if (read_slot[a] < 0) continue;
1623+ int S = read_slot[a];
1624+ int mi = (recv < 0 && name) ? comp_method_index(c, name) : -1;
1625+ if (mi < 0) { sl[S].alive = 0; continue; }
1626+ Scope *M = &c->scopes[mi];
1627+ if (k >= M->nparams || (M->rest_idx >= 0 && k >= M->rest_idx)) { sl[S].alive = 0; continue; }
1628+ LocalVar *plv = M->pnames[k] ? scope_local(M, M->pnames[k]) : NULL;
1629+ int T = plv ? oa_find(sl, n, mi, plv) : -1;
1630+ if (T < 0) { sl[S].alive = 0; continue; }
1631+ claimed[a] = 1;
1632+ oa_uf_union(sl, S, T);
1633+ }
1634+ }
1635+
1636+ /* 5. LocalVariableWriteNode sources: object literal -> evidence; alias to
1637+ another slot -> edge; empty/nil -> neutral; anything else -> kill. */
1638+ for (int id = 0; id < nt->count; id++) {
1639+ const char *ty = nt_type(nt, id);
1640+ if (!ty || strcmp(ty, "LocalVariableWriteNode")) continue;
1641+ int sidx = c->nscope[id];
1642+ const char *nm = nt_str(nt, id, "name");
1643+ LocalVar *lv = nm ? scope_local(&c->scopes[sidx], nm) : NULL;
1644+ int S = lv ? oa_find(sl, n, sidx, lv) : -1;
1645+ if (S < 0) continue;
1646+ int v = nt_ref(nt, id, "value");
1647+ const char *vty = v >= 0 ? nt_type(nt, v) : NULL;
1648+ if (!vty) { sl[S].alive = 0; continue; }
1649+ if (!strcmp(vty, "ArrayNode")) {
1650+ int en = 0; const int *el = nt_arr(nt, v, "elements", &en);
1651+ for (int e = 0; e < en; e++) {
1652+ const char *ety = nt_type(nt, el[e]);
1653+ int ec = (ety && !strcmp(ety, "SplatNode")) ? -2 : oa_obj_class_of(c, el[e]);
1654+ if (ec < 0) { sl[S].alive = 0; break; }
1655+ sl[S].cls = oa_cls_join(sl[S].cls, ec);
1656+ }
1657+ }
1658+ else if (!strcmp(vty, "LocalVariableReadNode") && read_slot[v] >= 0) {
1659+ claimed[v] = 1; oa_uf_union(sl, S, read_slot[v]);
1660+ }
1661+ else if (!strcmp(vty, "NilNode")) { /* nullable, neutral */ }
1662+ else sl[S].alive = 0;
1663+ }
1664+
1665+ /* 6. a slot read left unclaimed escaped into an unmodeled context -> kill. */
1666+ for (int id = 0; id < nt->count; id++)
1667+ if (read_slot[id] >= 0 && !claimed[id]) sl[read_slot[id]].alive = 0;
1668+
1669+ /* 7. resolve components: roll member alive/cls up to the root, then a
1670+ component narrows only if every member is alive and one class is known. */
1671+ for (int i = 0; i < n; i++) {
1672+ int r = oa_uf_find(sl, i);
1673+ if (r == i) continue;
1674+ sl[r].cls = oa_cls_join(sl[r].cls, sl[i].cls);
1675+ if (!sl[i].alive) sl[r].alive = 0;
1676+ }
1677+ for (int i = 0; i < n; i++) {
1678+ int r = oa_uf_find(sl, i);
1679+ if (sl[r].alive && sl[r].cls >= 0)
1680+ sl[i].lv->type = ty_obj_array(sl[r].cls);
1681+ }
1682+
1683+ /* 8. dependent locals: `b = arr[i]` (arr now a narrowed obj array) makes `b`
1684+ the element object type, so its field accesses unbox. Every write of the
1685+ local must be such an index read of one class (nil writes excepted). */
1686+ for (int s = 0; s < c->nscopes; s++) {
1687+ Scope *sc = &c->scopes[s];
1688+ for (int li = 0; li < sc->nlocals; li++) {
1689+ LocalVar *lv = &sc->locals[li];
1690+ if (lv->type != TY_POLY || lv->is_param || lv->is_block_param || lv->rbs_seeded) continue;
1691+ int cls = -1, ok = 1, saw = 0;
1692+ for (int id = 0; id < nt->count && ok; id++) {
1693+ const char *ty = nt_type(nt, id);
1694+ if (!ty || strcmp(ty, "LocalVariableWriteNode") || c->nscope[id] != s) continue;
1695+ const char *nm = nt_str(nt, id, "name");
1696+ if (!nm || strcmp(nm, lv->name)) continue;
1697+ int v = nt_ref(nt, id, "value");
1698+ const char *vty = v >= 0 ? nt_type(nt, v) : NULL;
1699+ if (vty && !strcmp(vty, "NilNode")) continue;
1700+ if (!vty || strcmp(vty, "CallNode")) { ok = 0; break; }
1701+ const char *cn = nt_str(nt, v, "name");
1702+ int crecv = nt_ref(nt, v, "receiver");
1703+ int can = 0; { int ca = nt_ref(nt, v, "arguments"); if (ca >= 0) nt_arr(nt, ca, "arguments", &can); }
1704+ int idx_op = cn && (!strcmp(cn, "[]") || !strcmp(cn, "at")) && can == 1;
1705+ int end_op = cn && (!strcmp(cn, "first") || !strcmp(cn, "last")) && can == 0;
1706+ if ((!idx_op && !end_op) || crecv < 0) { ok = 0; break; }
1707+ TyKind rt = infer_type(c, crecv);
1708+ if (!ty_is_obj_array(rt)) { ok = 0; break; }
1709+ int ec = ty_obj_array_class(rt);
1710+ if (cls < 0) cls = ec; else if (cls != ec) { ok = 0; break; }
1711+ saw = 1;
1712+ }
1713+ if (ok && saw && cls >= 0) lv->type = ty_object(cls);
1714+ }
1715+ }
1716+
1717+ free(sl); free(read_slot); free(claimed);
1718+}
1719+
15051720 void analyze_program(Compiler *c) {
15061721 /* scope 0 = top level */
15071722 Scope *top = comp_scope_new(c, NULL, -1);
@@ -2422,6 +2637,11 @@
24222637 }
24232638 }
24242639
2640+ /* narrow monomorphic object arrays (POLY_ARRAY -> obj-pointer array) before
2641+ the node cache is finalized so the rebuild below propagates the new element
2642+ types to every `arr[i]` / `arr[i].field` site. */
2643+ narrow_object_arrays(c);
2644+
24252645 /* finalize: gc-root needs + full node type cache */
24262646 for (int s = 0; s < c->nscopes; s++)
24272647 for (int i = 0; i < c->scopes[s].nlocals; i++)