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
fclose(f);}/* Iteration methods whose block binds a parameter to the receiver array's element type (the forms infer_block_params re-derives from the receiver). A param of such a block can lock to poly when the element type settles only late in the fixpoint (e.g. `arr.map{...}.map{...}`: the inner map's receiver becomes a typed array only after the outer map narrows). */static int iter_elem_block_method(const char *n) { static const char *names[] = { "each","map","collect","select","reject","filter","find","detect", "find_all","flat_map","filter_map","reverse_each","each_entry", "take_while","drop_while","sort_by","min_by","max_by","group_by", "partition","count","sum","any?","all?","none?","one?","keep_if", "delete_if","uniq","find_index","each_with_index","reduce","inject", NULL }; for (int i = 0; names[i]; i++) if (!strcmp(n, names[i])) return 1; return 0;}/* Is local `nm` assigned anywhere in `node`'s subtree (a block body)? Stops at nested defs/classes and at nested blocks/lambdas that re-bind `nm` (its writes there are a different variable). Used to leave a reassigned block param locked: its widening contribution is not recoverable by re-derivation from the element type alone. */static int blkp_name_written(Compiler *c, int node, const char *nm) { if (node < 0) return 0; const NodeTable *nt = c->nt; const char *ty = nt_type(nt, node); if (!ty) return 0; if (!strcmp(ty, "DefNode") || !strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode")) return 0; if ((!strcmp(ty, "BlockNode") || !strcmp(ty, "LambdaNode")) && blkp_binds_param(c, node, nm)) return 0; if (lv_node_is_write(ty)) { const char *n = nt_str(nt, node, "name"); if (n && !strcmp(n, nm)) return 1; } int nr = nt_num_refs(nt, node); for (int i = 0; i < nr; i++) if (blkp_name_written(c, nt_ref_at(nt, node, i), nm)) return 1; int na = nt_num_arrs(nt, node); 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; } return 0;}/* Clear a transient poly lock on iteration-block params over a now-typed (non-poly) array, so the optimistic re-narrow can re-derive 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 (the re-run would re-narrow it and silently miscompile), so it stays locked. Reset to UNKNOWN -- never the element type directly. Returns the count. */static int reset_locked_iter_block_params(Compiler *c) { const NodeTable *nt = c->nt; int n = 0; for (int id = 0; id < nt->count; id++) { int block = nt_ref(nt, id, "block"); if (block < 0) continue; const char *cn = nt_str(nt, id, "name"); if (!cn || !iter_elem_block_method(cn)) continue; int recv = nt_ref(nt, id, "receiver"); if (recv < 0) continue; TyKind rt = infer_type(c, recv); if (!ty_is_array(rt) || rt == TY_POLY_ARRAY) continue; Scope *bs = comp_scope_of(c, block); if (!bs) continue; int body = nt_ref(nt, block, "body"); for (int k = 0; ; k++) { const char *pn = block_param_name(c, block, k); if (!pn) break; LocalVar *lp = scope_local(bs, pn); if (lp && lp->type == TY_POLY && !blkp_name_written(c, body, pn)) { lp->type = TY_UNKNOWN; n++; } } } return n;}void analyze_program(Compiler *c) { /* scope 0 = top level */ Scope *top = comp_scope_new(c, NULL, -1); if (p && p->type == TY_POLY && !p->is_block_param) { p->type = TY_UNKNOWN; any = 1; } } } if (reset_locked_iter_block_params(c)) any = 1; if (any) { for (int iter = 0; iter < 128; iter++) { int ch = 0;test/block_param_sibling_collision.rbmodifiedView file
Loading diff…