Commit 1862c705

1862c705e7fdMerged

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

Description

Narrow a poly array of int arrays to an int-array element read

optcarrot's PPU holds nested arrays of int arrays (@chr_banks built from buf.slice!, @nmt_mem from literals). spinelc could only type them poly_array (element poly), so @chr_banks[i] read a boxed poly; that cascaded poly through @chr_ref -> @chr_mem -> @io_pattern -> @io_addr and the whole PPU register cycle stayed boxed.

Add a narrow: @nested[i] on a poly-array ivar whose every element write (@x[i] = v, an array literal, or src.map { ... }) is an int array yields an int array; codegen unboxes the poly element to sp_IntArray*. Mirrors the method-table double-index narrow.

@chr_mem/@io_addr/@io_pattern/@bg_pattern de-poly. optcarrot ~488 -> ~527 fps, checksum 59662. make test 845, make bench 57.

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

Details

Commit
1862c705e7fd
Parent
f7c5a48082be

src/analyze_infer.cmodifiedView file

@@ -111,6 +111,99 @@
111111 return ivar_array_elems_int_returning(c, cid, ivname);
112112 }
113113
114+/* Whether every element stored into poly-array ivar `@<ivname>` is an int
115+ array (a nested array of int arrays, e.g. @chr_banks / @nmt_mem). Element
116+ reads then yield an int array rather than a boxed poly. */
117+static int ivar_array_elems_all_int_array(Compiler *c, int cid, const char *ivname) {
118+ const NodeTable *nt = c->nt;
119+ int saw = 0;
120+ for (int id = 0; id < nt->count; id++) {
121+ const char *ty = nt_type(nt, id);
122+ if (!ty) continue;
123+ if (!strcmp(ty, "CallNode")) {
124+ const char *nm = nt_str(nt, id, "name");
125+ if (!nm || strcmp(nm, "[]=")) continue;
126+ int recv = nt_ref(nt, id, "receiver");
127+ if (recv < 0 || strcmp(nt_type(nt, recv) ? nt_type(nt, recv) : "", "InstanceVariableReadNode")) continue;
128+ const char *rn = nt_str(nt, recv, "name");
129+ if (!rn || strcmp(rn, ivname)) continue;
130+ Scope *s = comp_scope_of(c, id);
131+ if (!s || s->class_id != cid) continue;
132+ int args = nt_ref(nt, id, "arguments");
133+ int an = 0;
134+ const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
135+ if (an < 2) continue;
136+ TyKind vt = comp_ntype(c, av[1]);
137+ if (vt == TY_INT_ARRAY) { saw = 1; continue; }
138+ if (vt == TY_NIL || vt == TY_UNKNOWN) continue;
139+ return 0;
140+ }
141+ if (!strcmp(ty, "InstanceVariableWriteNode")) {
142+ const char *nm = nt_str(nt, id, "name");
143+ if (!nm || strcmp(nm, ivname)) continue;
144+ Scope *s = comp_scope_of(c, id);
145+ if (!s || s->class_id != cid) continue;
146+ int v = nt_ref(nt, id, "value");
147+ if (v < 0) continue;
148+ const char *vty = nt_type(nt, v);
149+ int arr = -1;
150+ if (vty && !strcmp(vty, "ArrayNode")) arr = v;
151+ else if (vty && !strcmp(vty, "CallNode") && nt_str(nt, v, "name") &&
152+ !strcmp(nt_str(nt, v, "name"), "*")) {
153+ int ar = nt_ref(nt, v, "receiver");
154+ if (ar >= 0 && nt_type(nt, ar) && !strcmp(nt_type(nt, ar), "ArrayNode")) arr = ar;
155+ }
156+ else if (vty && !strcmp(vty, "CallNode") && nt_str(nt, v, "name") &&
157+ (!strcmp(nt_str(nt, v, "name"), "map") || !strcmp(nt_str(nt, v, "name"), "collect"))) {
158+ /* `@x = src.map { ... }`: elements are the block's result type. */
159+ int blk = nt_ref(nt, v, "block");
160+ int body = blk >= 0 ? nt_ref(nt, blk, "body") : -1;
161+ int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
162+ if (bn <= 0) return 0;
163+ TyKind et = comp_ntype(c, bb[bn - 1]);
164+ if (et == TY_INT_ARRAY) { saw = 1; continue; }
165+ if (et == TY_NIL || et == TY_UNKNOWN) continue;
166+ return 0;
167+ }
168+ if (arr < 0) return 0;
169+ int en = 0;
170+ const int *els = nt_arr(nt, arr, "elements", &en);
171+ for (int e = 0; e < en; e++) {
172+ TyKind et = comp_ntype(c, els[e]);
173+ if (et == TY_INT_ARRAY) { saw = 1; continue; }
174+ if (et == TY_NIL || et == TY_UNKNOWN) continue;
175+ return 0;
176+ }
177+ continue;
178+ }
179+ }
180+ return saw;
181+}
182+
183+/* `@nested[i]` (single index) where @nested is a poly array whose every
184+ element is an int array yields an int array (not a boxed poly). Returns the
185+ ivar name via *out_iv for codegen, or NULL. */
186+static int poly_index_int_array(Compiler *c, int id) {
187+ const NodeTable *nt = c->nt;
188+ const char *nm = nt_str(nt, id, "name");
189+ if (!nm || strcmp(nm, "[]")) return 0;
190+ int args = nt_ref(nt, id, "arguments");
191+ int an = 0;
192+ const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
193+ if (an != 1) return 0;
194+ const char *aty = nt_type(nt, av[0]);
195+ if (aty && !strcmp(aty, "RangeNode")) return 0;
196+ int recv = nt_ref(nt, id, "receiver");
197+ if (recv < 0 || strcmp(nt_type(nt, recv) ? nt_type(nt, recv) : "", "InstanceVariableReadNode")) return 0;
198+ const char *ivname = nt_str(nt, recv, "name");
199+ Scope *s = comp_scope_of(c, id);
200+ int cid = s ? s->class_id : -1;
201+ if (cid < 0 || cid >= c->nclasses || !ivname) return 0;
202+ int iv = comp_ivar_index(&c->classes[cid], ivname);
203+ if (iv < 0 || c->classes[cid].ivar_types[iv] != TY_POLY_ARRAY) return 0;
204+ return ivar_array_elems_all_int_array(c, cid, ivname);
205+}
206+
114207 TyKind infer_call(Compiler *c, int id) {
115208 const NodeTable *nt = c->nt;
116209 const char *name = nt_str(nt, id, "name");
@@ -129,6 +222,11 @@
129222 fetch/peek/store, which would otherwise run boxed-poly. */
130223 if (recv >= 0 && !strcmp(name, "[]") && argc == 1 && poly_double_index_int(c, id))
131224 return TY_INT;
225+ /* `@nested[i]` on a poly array of int arrays yields an int array (nested
226+ array of int arrays -- @chr_banks / @nmt_mem). Without this the element
227+ read is a boxed poly and cascades poly through the PPU. */
228+ if (recv >= 0 && !strcmp(name, "[]") && argc == 1 && poly_index_int_array(c, id))
229+ return TY_INT_ARRAY;
132230
133231 /* Complex / Rational value types. */
134232 if (recv < 0 && !strcmp(name, "Complex")) return TY_COMPLEX;