Commit 094eb021

094eb02112d5Merged

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

Description

Narrow whole-array-literal dispatch tables too

The method-table narrow only inspected @t[i] = v element writes, so a table built from a literal (@nmt_mem = [[0xff]*0x400, [0xff]*0x400], the PPU nametable banks) was not recognized. Classify the elements of a whole-ivar array-literal write (@t = [..] and @t = [x] * n) the same way; a non-literal whole write bails (can't verify).

PPU @nmt_mem[bank][idx] now narrows to an int dispatch. make test 845, make bench 57, optcarrot checksum 59662 (fps unchanged pending the separate PPU register de-poly).

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

Details

Commit
094eb02112d5
Parent
c631cf13a71f

src/analyze_infer.cmodifiedView file

@@ -4,35 +4,71 @@
44 `cid` is an int-returning kind: a bound method (a dispatch-table entry, called
55 with an int arg returns int), an int array, an int, or nil filler. Mirrors
66 legacy's cls_ivar_observed_types check in poly_index_narrow_int. */
7+/* Classify a value type stored as an element of a dispatch/data table:
8+ 1 = int-returning when indexed/called (int bit, int array get, bound
9+ method/proc call, or a poly assumed to be such a callable); 2 = neutral
10+ filler (nil/unknown); 0 = a clearly non-int kind. */
11+static int table_elem_int_returning(TyKind vt) {
12+ if (vt == TY_INT || vt == TY_INT_ARRAY || vt == TY_METHOD || vt == TY_PROC ||
13+ vt == TY_POLY)
14+ return 1;
15+ if (vt == TY_NIL || vt == TY_UNKNOWN) return 2;
16+ return 0;
17+}
18+
719 static int ivar_array_elems_int_returning(Compiler *c, int cid, const char *ivname) {
820 const NodeTable *nt = c->nt;
921 int saw = 0;
1022 for (int id = 0; id < nt->count; id++) {
1123 const char *ty = nt_type(nt, id);
12- if (!ty || strcmp(ty, "CallNode")) continue;
13- const char *nm = nt_str(nt, id, "name");
14- if (!nm || strcmp(nm, "[]=")) continue;
15- int recv = nt_ref(nt, id, "receiver");
16- if (recv < 0) continue;
17- const char *rty = nt_type(nt, recv);
18- if (!rty || strcmp(rty, "InstanceVariableReadNode")) continue;
19- const char *rn = nt_str(nt, recv, "name");
20- if (!rn || strcmp(rn, ivname)) continue;
21- Scope *s = comp_scope_of(c, id);
22- if (!s || s->class_id != cid) continue;
23- int args = nt_ref(nt, id, "arguments");
24- int an = 0;
25- const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
26- if (an < 2) continue;
27- TyKind vt = comp_ntype(c, av[1]);
28- /* int-returning element kinds: int (bit extraction), int array (get), a
29- bound method / proc (called with the int arg, returns int). A poly
30- element is assumed to be a dispatch-table callable -- optcarrot's
31- @fetch/@store hold Method|IntArray unified to poly through a hash. */
32- if (vt == TY_INT || vt == TY_INT_ARRAY || vt == TY_METHOD || vt == TY_PROC ||
33- vt == TY_POLY) { saw = 1; continue; }
34- if (vt == TY_NIL || vt == TY_UNKNOWN) continue; /* filler / unresolved */
35- return 0; /* a clearly non-int element kind */
24+ if (!ty) continue;
25+ /* element write `@ivar[i] = v` */
26+ if (!strcmp(ty, "CallNode")) {
27+ const char *nm = nt_str(nt, id, "name");
28+ if (!nm || strcmp(nm, "[]=")) continue;
29+ int recv = nt_ref(nt, id, "receiver");
30+ if (recv < 0) continue;
31+ const char *rty = nt_type(nt, recv);
32+ if (!rty || strcmp(rty, "InstanceVariableReadNode")) continue;
33+ const char *rn = nt_str(nt, recv, "name");
34+ if (!rn || strcmp(rn, ivname)) continue;
35+ Scope *s = comp_scope_of(c, id);
36+ if (!s || s->class_id != cid) continue;
37+ int args = nt_ref(nt, id, "arguments");
38+ int an = 0;
39+ const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
40+ if (an < 2) continue;
41+ int k = table_elem_int_returning(comp_ntype(c, av[1]));
42+ if (k == 0) return 0;
43+ if (k == 1) saw = 1;
44+ continue;
45+ }
46+ /* whole-ivar write `@ivar = [..]` / `@ivar = [x] * n` */
47+ if (!strcmp(ty, "InstanceVariableWriteNode")) {
48+ const char *nm = nt_str(nt, id, "name");
49+ if (!nm || strcmp(nm, ivname)) continue;
50+ Scope *s = comp_scope_of(c, id);
51+ if (!s || s->class_id != cid) continue;
52+ int v = nt_ref(nt, id, "value");
53+ if (v < 0) continue;
54+ const char *vty = nt_type(nt, v);
55+ int arr = -1; /* the ArrayNode whose elements to inspect */
56+ if (vty && !strcmp(vty, "ArrayNode")) arr = v;
57+ else if (vty && !strcmp(vty, "CallNode") && nt_str(nt, v, "name") &&
58+ !strcmp(nt_str(nt, v, "name"), "*")) {
59+ int ar = nt_ref(nt, v, "receiver"); /* `[..] * n` */
60+ if (ar >= 0 && nt_type(nt, ar) && !strcmp(nt_type(nt, ar), "ArrayNode")) arr = ar;
61+ }
62+ if (arr < 0) return 0; /* non-literal whole write: can't verify */
63+ int en = 0;
64+ const int *els = nt_arr(nt, arr, "elements", &en);
65+ for (int e = 0; e < en; e++) {
66+ int k = table_elem_int_returning(comp_ntype(c, els[e]));
67+ if (k == 0) return 0;
68+ if (k == 1) saw = 1;
69+ }
70+ continue;
71+ }
3672 }
3773 return saw;
3874 }