Commit 6dcb31bf

6dcb31bf5cebMerged

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

Description

Re-narrow iteration-block params that transiently locked to poly

The monotonic fixpoint locks a block parameter to poly the first time it sees a transient poly -- e.g. arr.map{|x| x+0.5}.map{|x| x.floor}: the inner map's receiver is the outer map's result, which is only a typed FloatArray after the outer block's x narrows to int. Until then the inner x is the element of a not-yet-typed array, locks poly, and never narrows back (ty_unify only widens). The chain then produced a poly array and .reduce over it was unsupported, so the filed one-liner failed to compile even though the SIGSEGV it reported is fixed.

The post-fixpoint optimistic re-narrow already clears poly ivars and poly non-block params and re-runs the monotonic passes; block params were excluded. Extend it to iteration-block params whose receiver is now a typed (non-poly) array, resetting them to UNKNOWN so the re-run re-derives the element type. Only read-only params are reset: a param reassigned in the body has a widening contribution the element-type re-derivation cannot recover, so it stays locked (resetting it would re-narrow and silently miscompile). The reset is gated to element-binding iteration methods, so instance_exec (typed via build_ie_map, not in the re-run), rescue variables (TY_EXCEPTION), and proc/lambda standalone params are untouched.

Now [1,2,3].map{|x| x+0.5}.map{|x| x.floor}.reduce(0){|a,x| a+x} yields 6. Adds that one-liner and a genuinely-heterogeneous-receiver case (must stay poly) to the regression test.

Gates: optcarrot 59662, test 847-0-0, bench 57-0-0, self-host bootstrap (clang) IR+C fixpoint OK.

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

Details

Commit
6dcb31bf5ceb
Parent
8c52619d7597

src/analyze.cmodifiedView file

@@ -926,6 +926,76 @@
926926 fclose(f);
927927 }
928928
929+/* Iteration methods whose block binds a parameter to the receiver array's
930+ element type (the forms infer_block_params re-derives from the receiver).
931+ A param of such a block can lock to poly when the element type settles only
932+ late in the fixpoint (e.g. `arr.map{...}.map{...}`: the inner map's receiver
933+ becomes a typed array only after the outer map narrows). */
934+static int iter_elem_block_method(const char *n) {
935+ static const char *names[] = {
936+ "each","map","collect","select","reject","filter","find","detect",
937+ "find_all","flat_map","filter_map","reverse_each","each_entry",
938+ "take_while","drop_while","sort_by","min_by","max_by","group_by",
939+ "partition","count","sum","any?","all?","none?","one?","keep_if",
940+ "delete_if","uniq","find_index","each_with_index","reduce","inject", NULL };
941+ for (int i = 0; names[i]; i++) if (!strcmp(n, names[i])) return 1;
942+ return 0;
943+}
944+
945+/* Is local `nm` assigned anywhere in `node`'s subtree (a block body)? Stops at
946+ nested defs/classes and at nested blocks/lambdas that re-bind `nm` (its
947+ writes there are a different variable). Used to leave a reassigned block
948+ param locked: its widening contribution is not recoverable by re-derivation
949+ from the element type alone. */
950+static int blkp_name_written(Compiler *c, int node, const char *nm) {
951+ if (node < 0) return 0;
952+ const NodeTable *nt = c->nt;
953+ const char *ty = nt_type(nt, node);
954+ if (!ty) return 0;
955+ if (!strcmp(ty, "DefNode") || !strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode")) return 0;
956+ if ((!strcmp(ty, "BlockNode") || !strcmp(ty, "LambdaNode")) && blkp_binds_param(c, node, nm)) return 0;
957+ if (lv_node_is_write(ty)) {
958+ const char *n = nt_str(nt, node, "name");
959+ if (n && !strcmp(n, nm)) return 1;
960+ }
961+ int nr = nt_num_refs(nt, node);
962+ for (int i = 0; i < nr; i++) if (blkp_name_written(c, nt_ref_at(nt, node, i), nm)) return 1;
963+ int na = nt_num_arrs(nt, node);
964+ for (int i = 0; i < na; i++) { int m = 0; const int *ids = nt_arr_at(nt, node, i, &m); for (int k = 0; k < m; k++) if (blkp_name_written(c, ids[k], nm)) return 1; }
965+ return 0;
966+}
967+
968+/* Clear a transient poly lock on iteration-block params over a now-typed
969+ (non-poly) array, so the optimistic re-narrow can re-derive the element
970+ type. Only read-only params are reset: a param reassigned in the body has a
971+ widening contribution the element-type re-derivation cannot recover (the
972+ re-run would re-narrow it and silently miscompile), so it stays locked.
973+ Reset to UNKNOWN -- never the element type directly. Returns the count. */
974+static int reset_locked_iter_block_params(Compiler *c) {
975+ const NodeTable *nt = c->nt;
976+ int n = 0;
977+ for (int id = 0; id < nt->count; id++) {
978+ int block = nt_ref(nt, id, "block");
979+ if (block < 0) continue;
980+ const char *cn = nt_str(nt, id, "name");
981+ if (!cn || !iter_elem_block_method(cn)) continue;
982+ int recv = nt_ref(nt, id, "receiver");
983+ if (recv < 0) continue;
984+ TyKind rt = infer_type(c, recv);
985+ if (!ty_is_array(rt) || rt == TY_POLY_ARRAY) continue;
986+ Scope *bs = comp_scope_of(c, block);
987+ if (!bs) continue;
988+ int body = nt_ref(nt, block, "body");
989+ for (int k = 0; ; k++) {
990+ const char *pn = block_param_name(c, block, k);
991+ if (!pn) break;
992+ LocalVar *lp = scope_local(bs, pn);
993+ if (lp && lp->type == TY_POLY && !blkp_name_written(c, body, pn)) { lp->type = TY_UNKNOWN; n++; }
994+ }
995+ }
996+ return n;
997+}
998+
929999 void analyze_program(Compiler *c) {
9301000 /* scope 0 = top level */
9311001 Scope *top = comp_scope_new(c, NULL, -1);
@@ -1154,6 +1224,7 @@
11541224 if (p && p->type == TY_POLY && !p->is_block_param) { p->type = TY_UNKNOWN; any = 1; }
11551225 }
11561226 }
1227+ if (reset_locked_iter_block_params(c)) any = 1;
11571228 if (any) {
11581229 for (int iter = 0; iter < 128; iter++) {
11591230 int ch = 0;