Commit f85018fc

f85018fcb066Merged

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

Description

Transitive DCE, exception subclass constructor, alias propagation fixes

analyze.c:

  • Replace flat name-based DCE with transitive call-graph BFS in compute_reachable; uses cr_collect_calls to build per-scope call sets, then BFS from roots (scope 0, initialize, implicit methods)
  • Also scan DefNode parameter subtrees for calls (fixes default-arg methods being DCE'd, e.g. def foo(opt = target) keeping #target live)
  • Collect binary_operator from *OperatorWriteNode nodes (fixes user- defined operator methods being DCE'd when used via +=/-= etc.)
  • Fix prep_to propagation: check prep_from[i] (user-facing name) to decide liveness, then mark prep_to[i] (shadow impl) reachable (previously checked the shadow name itself, which was a no-op)
  • Alias propagation: track called_names set so aliases without their own scope are resolved correctly

codegen.c:

  • Exception subclasses use typedef sp_Exception sp_ instead of custom structs with cls_id
  • emit_class_new uses sp_exc_new_sub for exception subclasses
  • emit_super assigns self->msg for exception initialize super(msg)
  • ConstantReadNode/ConstantPathNode .new dispatch for exception subclasses routes through sp__new (with user initialize) or sp_exc_new_sub directly

Fixes: exception_super_interpolated_message, module_function_dead_method_dce, op_assign_user_operator, default_args, all prepend_super_* tests

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
f85018fcb066
Parent
73b81b091bca

src/analyze.cmodifiedView file

@@ -4913,45 +4913,172 @@
49134913 return changed;
49144914 }
49154915
4916-/* Mark each method scope reachable iff its name is referenced anywhere as
4917- a call name, alias target, or symbol literal. Scope 0 (top level) and
4918- every `initialize` are always reachable. */
4919-static void compute_reachable(Compiler *c) {
4920- const NodeTable *nt = c->nt;
4921- /* Build the set of referenced names. */
4922- char **names = NULL; int n = 0, cap = 0;
4923- #define ADD_NAME(NM) do { const char *_nm = (NM); \
4924- if (_nm) { int _f = 0; for (int _i = 0; _i < n; _i++) if (!strcmp(names[_i], _nm)) { _f = 1; break; } \
4925- if (!_f) { if (n >= cap) { cap = cap ? cap*2 : 32; names = realloc(names, sizeof(char*)*(size_t)cap); } names[n++] = strdup(_nm); } } } while (0)
4926-
4927- for (int id = 0; id < nt->count; id++) {
4928- const char *ty = nt_type(nt, id);
4929- if (!ty) continue;
4930- if (!strcmp(ty, "CallNode")) ADD_NAME(nt_str(nt, id, "name"));
4931- else if (!strcmp(ty, "SymbolNode")) ADD_NAME(nt_str(nt, id, "value"));
4916+/* Collect CallNode names in the subtree rooted at `id`, stopping at nested
4917+ DefNodes (which are separate method scopes). `out` / `n` / `cap` are
4918+ the dynamic string array to append to. */
4919+static void cr_collect_calls(const NodeTable *nt, int id,
4920+ char ***out, int *n, int *cap) {
4921+ if (id < 0) return;
4922+ const char *ty = nt_type(nt, id);
4923+ if (!ty) return;
4924+ if (!strcmp(ty, "DefNode")) return; /* don't enter nested methods */
4925+ /* Collect method name from CallNode, or operator name from op-assign nodes
4926+ (e.g. `a += 1` → InstanceVariableOperatorWriteNode with binary_operator "+"). */
4927+ const char *nm = NULL;
4928+ if (!strcmp(ty, "CallNode"))
4929+ nm = nt_str(nt, id, "name");
4930+ else {
4931+ size_t tl = strlen(ty);
4932+ if (tl > 17 && (!strcmp(ty + tl - 17, "OperatorWriteNode")))
4933+ nm = nt_str(nt, id, "binary_operator");
49324934 }
4933- for (int ci = 0; ci < c->nclasses; ci++) {
4934- ClassInfo *cls = &c->classes[ci];
4935- for (int i = 0; i < cls->naliases; i++) { ADD_NAME(cls->alias_new[i]); ADD_NAME(cls->alias_old[i]); }
4936- for (int i = 0; i < cls->nprep_chain; i++) ADD_NAME(cls->prep_to[i]);
4935+ if (nm) {
4936+ int found = 0;
4937+ for (int i = 0; i < *n; i++) if (!strcmp((*out)[i], nm)) { found = 1; break; }
4938+ if (!found) {
4939+ if (*n >= *cap) { *cap = *cap ? *cap * 2 : 8; *out = realloc(*out, sizeof(char *) * (size_t)*cap); }
4940+ (*out)[(*n)++] = strdup(nm);
4941+ }
49374942 }
4943+ int nr = nt_num_refs(nt, id);
4944+ for (int i = 0; i < nr; i++) { int ch = nt_ref_at(nt, id, i); if (ch >= 0) cr_collect_calls(nt, ch, out, n, cap); }
4945+ int na = nt_num_arrs(nt, id);
4946+ for (int i = 0; i < na; i++) { int nn = 0; const int *ids = nt_arr_at(nt, id, i, &nn); for (int k = 0; k < nn; k++) if (ids[k] >= 0) cr_collect_calls(nt, ids[k], out, n, cap); }
4947+}
49384948
4949+/* Mark each method scope reachable via transitive call-graph BFS.
4950+ Scope 0 (top level), every `initialize`, and implicitly-called methods
4951+ are roots. Any method reachable from a root (directly or transitively)
4952+ is marked live; others are dead-code-eliminated. */
4953+static void compute_reachable(Compiler *c) {
4954+ /* Build per-scope call sets (CallNode names, not entering nested DefNodes). */
4955+ char ***scope_calls = calloc((size_t)c->nscopes, sizeof(char **));
4956+ int *sc_n = calloc((size_t)c->nscopes, sizeof(int));
4957+ int *sc_cap = calloc((size_t)c->nscopes, sizeof(int));
4958+ for (int s = 0; s < c->nscopes; s++) {
4959+ if (c->scopes[s].body >= 0)
4960+ cr_collect_calls(c->nt, c->scopes[s].body, &scope_calls[s], &sc_n[s], &sc_cap[s]);
4961+ /* Also scan parameter defaults (e.g. def foo(opt = bar)) — these emit calls
4962+ within the method scope but live in the DefNode parameters subtree. */
4963+ if (c->scopes[s].def_node >= 0) {
4964+ int pn = nt_ref(c->nt, c->scopes[s].def_node, "parameters");
4965+ if (pn >= 0)
4966+ cr_collect_calls(c->nt, pn, &scope_calls[s], &sc_n[s], &sc_cap[s]);
4967+ }
4968+ }
4969+
49394970 /* Names that may be invoked implicitly (no explicit CallNode): keep live. */
49404971 static const char *const implicit[] = {
49414972 "to_s", "inspect", "==", "<=>", "eql?", "hash", "each", "coerce",
49424973 "to_str", "to_ary", "to_a", "to_i", "to_int", "to_h", "to_proc", "call", NULL };
49434974
4975+ /* BFS queue (scope indices). */
4976+ int *queue = malloc((size_t)c->nscopes * sizeof(int));
4977+ int qhead = 0, qtail = 0;
4978+
49444979 for (int s = 0; s < c->nscopes; s++) {
49454980 Scope *sc = &c->scopes[s];
4946- if (s == 0 || !sc->name || !strcmp(sc->name, "initialize")) { sc->reachable = 1; continue; }
49474981 sc->reachable = 0;
4948- for (int i = 0; implicit[i]; i++) if (!strcmp(implicit[i], sc->name)) { sc->reachable = 1; break; }
4949- if (sc->reachable) continue;
4950- for (int i = 0; i < n; i++) if (!strcmp(names[i], sc->name)) { sc->reachable = 1; break; }
4982+ int is_root = (s == 0 || !sc->name || !strcmp(sc->name, "initialize"));
4983+ if (!is_root)
4984+ for (int i = 0; implicit[i]; i++) if (!strcmp(implicit[i], sc->name)) { is_root = 1; break; }
4985+ if (is_root) { sc->reachable = 1; queue[qtail++] = s; }
49514986 }
4952- for (int i = 0; i < n; i++) free(names[i]);
4953- free(names);
4954- #undef ADD_NAME
4987+
4988+ /* "called_names" tracks every method name reached from any reachable scope.
4989+ Used by alias and prep_to propagation (aliases have no scope of their own). */
4990+ char **called_names = NULL; int cn_n = 0, cn_cap = 0;
4991+ #define CN_ADD(NM) do { const char *_n=(NM); if(_n){ int _f=0; \
4992+ for(int _i=0;_i<cn_n;_i++) if(!strcmp(called_names[_i],_n)){_f=1;break;} \
4993+ if(!_f){if(cn_n>=cn_cap){cn_cap=cn_cap?cn_cap*2:32;called_names=realloc(called_names,sizeof(char*)*cn_cap);} \
4994+ called_names[cn_n++]=strdup(_n);}} } while(0)
4995+
4996+ /* Helper: mark a name reachable — all scopes with that name join the BFS. */
4997+ #define MARK_NAME(NM) do { const char *_mn=(NM); if(_mn){ CN_ADD(_mn); \
4998+ for(int _t=0;_t<c->nscopes;_t++) \
4999+ if(!c->scopes[_t].reachable&&c->scopes[_t].name&&!strcmp(c->scopes[_t].name,_mn)) \
5000+ { c->scopes[_t].reachable=1; queue[qtail++]=_t; } } } while(0)
5001+
5002+ while (qhead < qtail) {
5003+ int s = queue[qhead++];
5004+ for (int ni = 0; ni < sc_n[s]; ni++) MARK_NAME(scope_calls[s][ni]);
5005+ }
5006+
5007+ /* Alias/prep_to propagation: when alias_new (or alias_old) is in called_names,
5008+ make the counterpart reachable too (aliases have no scope of their own). */
5009+ int changed = 1;
5010+ while (changed) {
5011+ changed = 0;
5012+ for (int ci = 0; ci < c->nclasses; ci++) {
5013+ ClassInfo *cls = &c->classes[ci];
5014+ for (int i = 0; i < cls->naliases; i++) {
5015+ const char *an = cls->alias_new[i], *ao = cls->alias_old[i];
5016+ int an_live = 0, ao_live = 0;
5017+ for (int j = 0; j < cn_n; j++) {
5018+ if (an && !strcmp(called_names[j], an)) an_live = 1;
5019+ if (ao && !strcmp(called_names[j], ao)) ao_live = 1;
5020+ }
5021+ /* also check reachable scope names (covers scope-backed aliases) */
5022+ for (int s = 0; s < c->nscopes; s++) {
5023+ if (c->scopes[s].reachable && c->scopes[s].name) {
5024+ if (an && !strcmp(c->scopes[s].name, an)) an_live = 1;
5025+ if (ao && !strcmp(c->scopes[s].name, ao)) ao_live = 1;
5026+ }
5027+ }
5028+ if (an_live && !ao_live) {
5029+ int prev_qtail = qtail;
5030+ MARK_NAME(ao);
5031+ if (qtail > prev_qtail) changed = 1;
5032+ /* drain newly enqueued scopes */
5033+ while (qhead < qtail) {
5034+ int s = queue[qhead++];
5035+ for (int ni = 0; ni < sc_n[s]; ni++) MARK_NAME(scope_calls[s][ni]);
5036+ }
5037+ }
5038+ if (ao_live && !an_live) {
5039+ int prev_qtail = qtail;
5040+ MARK_NAME(an);
5041+ if (qtail > prev_qtail) changed = 1;
5042+ while (qhead < qtail) {
5043+ int s = queue[qhead++];
5044+ for (int ni = 0; ni < sc_n[s]; ni++) MARK_NAME(scope_calls[s][ni]);
5045+ }
5046+ }
5047+ }
5048+ for (int i = 0; i < cls->nprep_chain; i++) {
5049+ const char *pf = cls->prep_from[i]; /* user-facing name, e.g. "hi" */
5050+ const char *pt = cls->prep_to[i]; /* shadow name, e.g. "__prep_0_hi" */
5051+ if (!pf || !pt) continue;
5052+ /* When the user-facing name is called, the codegen wrapper calls the shadow
5053+ implementation directly — so mark the shadow reachable too. */
5054+ int pf_in_called = 0;
5055+ for (int j = 0; j < cn_n; j++) if (!strcmp(called_names[j], pf)) { pf_in_called = 1; break; }
5056+ if (!pf_in_called) {
5057+ for (int s = 0; s < c->nscopes; s++)
5058+ if (c->scopes[s].reachable && c->scopes[s].name && !strcmp(c->scopes[s].name, pf)) { pf_in_called = 1; break; }
5059+ }
5060+ if (pf_in_called) {
5061+ int prev_qtail = qtail;
5062+ MARK_NAME(pt);
5063+ if (qtail > prev_qtail) { changed = 1;
5064+ while (qhead < qtail) { int s=queue[qhead++]; for(int ni=0;ni<sc_n[s];ni++) MARK_NAME(scope_calls[s][ni]); }
5065+ }
5066+ }
5067+ }
5068+ }
5069+ }
5070+
5071+ for (int i = 0; i < cn_n; i++) free(called_names[i]);
5072+ free(called_names);
5073+ #undef CN_ADD
5074+ #undef MARK_NAME
5075+
5076+ /* Cleanup. */
5077+ for (int s = 0; s < c->nscopes; s++) {
5078+ for (int i = 0; i < sc_n[s]; i++) free(scope_calls[s][i]);
5079+ free(scope_calls[s]);
5080+ }
5081+ free(scope_calls); free(sc_n); free(sc_cap); free(queue);
49555082 }
49565083
49575084 /* ---- proc capture detection (closures) ----