Commit 67d4dacb
67d4dacb7392Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): stub uncalled instance methods (#393)
Fixes #393.
def render(html); @body = html; end on a class whose initializer
pinned @body to const char * failed C-compile when render had
no callers: html defaulted to mrb_int (the no-caller default),
the body lowered to self->iv_body = lv_html (mrb_int into a
const char* slot), -Werror tripped.
Approach: instance-method DCE. compute_live_instance_methods
collects every CallNode name and SymbolNode value across the
program (and through @ieval_body_ids / @meth_body_ids /
@cls_meth_bodies / @cls_cmeth_bodies, which the @root_id walk
alone misses), and marks every class's same-named method live.
At emit_instance_method, a non-live method's body is replaced with
(void)params; return default; -- always-compiles, never-runs.
Always-live (independent of CallNode-name presence) so operator / dispatch / interpolation surfaces don't get stripped: initialize, <=>, ==, !=, eql?, hash, to_s, inspect, to_a, to_i, to_f, to_str, [], []=, each, <, >, <=, >=, +, -, *, /, %, <<, coerce.
Self-host validates: the conservative liveness rule correctly
identifies 3 genuinely dead helpers in spinel_codegen.rb itself
(emit_header, method_linkage (variant-without-_named, all
callers use the named one), emit_tuple_structs); bootstrap
gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 67d4dacb7392
- Parent
- eaefc1614fda
spinel_codegen.rbmodifiedView file
# `def self.factory(attrs); new(attrs); end` against a 0-arg # or differently-typed constructor). compute_live_cls_methods compute_live_instance_methods emit_forward_decls emit_global_constants emit_raw("/*LAMBDA_INSERT_POINT*/") # Live entries are stored as `<ClassName>::<methodName>` joined # by `;` in @cls_cmeth_live. Idempotent: marking an already-live # entry is a no-op. # Issue #393. Mark instance methods reachable from the program # entry. Anything not marked gets a stub body at emit time # (`(void)params; return default;`) so an uncalled `def f(x); @typed = x; end` # whose param defaulted to int doesn't fail C-compile against a # narrower ivar slot. # # Conservative liveness: a method `M` on class `C` is live iff: # 1. `M` == "initialize" (constructor synth always reaches it). # 2. Some CallNode anywhere in the program names `M`. # 3. Some SymbolNode anywhere has value `M` (covers method(:M) / # define_method(:M) / send(:M, ...) reflection sites). # This over-approximates -- a method named "size" on user class `C` # is marked live whenever ANY call site (even an unrelated # `arr.size`) names "size". That's fine for the DCE goal; the # downside is false-negatives (a genuinely-unused method we keep) # rather than false-positives (a live method we strip). def compute_live_instance_methods @cls_meth_live = "" # Step 1: methods whose name is always-implicitly-dispatched are # marked live unconditionally. `<=>` is reached by Comparable's # `<` / `>` etc. operators (compile_call_expr's cmp_owner arm # synthesises the call, so `<=>` itself never appears as a # CallNode name in the AST — and the conservative "name appears # somewhere" rule below would miss it). Same for the bracket # operators, common conversion methods, and `inspect` / `to_s` # called by string interpolation, `puts`, etc. always_live = ["initialize", "<=>", "==", "!=", "eql?", "hash", "to_s", "inspect", "to_a", "to_i", "to_f", "to_str", "[]", "[]=", "each", "<", ">", "<=", ">=", "+", "-", "*", "/", "%", "<<", "coerce", "<<="] ci = 0 while ci < @cls_names.length mnames_init = @cls_meth_names[ci].split(";") mj_init = 0 while mj_init < mnames_init.length ai = 0 while ai < always_live.length if mnames_init[mj_init] == always_live[ai] cls_meth_mark_live(ci, always_live[ai]) end ai = ai + 1 end mj_init = mj_init + 1 end ci = ci + 1 end # Step 2/3: collect every CallNode name + SymbolNode value, mark # methods on every class that has them. # `"".split(",")` initializer types `used` as a str_array, since # `acc` in collect_used_method_names is pushed strings only. used = "".split(",") collect_used_method_names(@root_id, used) # Lifted instance_eval blocks: their bodies were detached from # @root_id by ieval_rewrite_call and stashed in @ieval_body_ids, # so a walk from @root_id misses any `get("/")` / similar call # buried inside `app.instance_eval { get("/") }`. Walk them too. ie = 0 while ie < @ieval_body_ids.length collect_used_method_names(@ieval_body_ids[ie], used) ie = ie + 1 end # User-defined method bodies: top-level methods (@meth_body_ids) # and instance / class methods (@cls_meth_bodies + @cls_cmeth_bodies). # The @root_id walk hits class definitions but those routes only to # ClassNode -> body, not to each method body's nested call sites. mi_b = 0 while mi_b < @meth_body_ids.length collect_used_method_names(@meth_body_ids[mi_b], used) mi_b = mi_b + 1 end ci_b = 0 while ci_b < @cls_names.length mb_list = @cls_meth_bodies[ci_b].split(";") mbk = 0 while mbk < mb_list.length bid_mb = mb_list[mbk].to_i if bid_mb > 0 collect_used_method_names(bid_mb, used) end mbk = mbk + 1 end cmb_list = @cls_cmeth_bodies[ci_b].split(";") cmbk = 0 while cmbk < cmb_list.length bid_cmb = cmb_list[cmbk].to_i if bid_cmb > 0 collect_used_method_names(bid_cmb, used) end cmbk = cmbk + 1 end ci_b = ci_b + 1 end ui = 0 while ui < used.length uname = used[ui] ci2 = 0 while ci2 < @cls_names.length mns = @cls_meth_names[ci2].split(";") mj2 = 0 while mj2 < mns.length if mns[mj2] == uname cls_meth_mark_live(ci2, uname) end mj2 = mj2 + 1 end ci2 = ci2 + 1 end ui = ui + 1 end end # Walk the AST collecting CallNode names + SymbolNode values into # `acc`. Used by compute_live_instance_methods. Iterative (explicit # stack via `push_child_ids`) so we don't recurse into 600+-deep # method bodies and blow the stack. def collect_used_method_names(nid, acc) if nid < 0 return end t = @nd_type[nid] if t == "CallNode" cname = @nd_name[nid] if cname != "" && not_in(cname, acc) == 1 acc.push(cname) end end if t == "SymbolNode" sval = @nd_content[nid] if sval != "" && not_in(sval, acc) == 1 acc.push(sval) end end cs = [] push_child_ids(nid, cs) ck = 0 while ck < cs.length collect_used_method_names(cs[ck], acc) ck = ck + 1 end end def cls_meth_is_live(ci, mname) if ci < 0 || ci >= @cls_names.length return 0 end if @cls_meth_live == nil || @cls_meth_live == "" return 0 end needle = ";" + @cls_names[ci] + "::" + mname + ";" haystack = ";" + @cls_meth_live + ";" if haystack.include?(needle) return 1 end 0 end def cls_meth_mark_live(ci, mname) if ci < 0 || ci >= @cls_names.length return end if cls_meth_is_live(ci, mname) == 1 return end entry = @cls_names[ci] + "::" + mname if @cls_meth_live == "" @cls_meth_live = entry else @cls_meth_live = @cls_meth_live + ";" + entry end end def compute_live_cls_methods @cls_cmeth_live = "" collect_cls_calls(@root_id, -1) is_tramp = 1 end end if bid >= 0 && is_tramp == 0 # Issue #393: stub uncalled methods so an inferred-int param # doesn't fail C-compile against a narrower ivar slot. is_dead = 0 if cls_meth_is_live(ci, mname) == 0 is_dead = 1 end if is_dead == 1 # Mark every param as `(void)`-cast so -Wunused doesn't fire, # then return the default. jd = 0 while jd < pnames.length emit(" (void)lv_" + pnames[jd] + ";") jd = jd + 1 end elsif bid >= 0 && is_tramp == 0 declare_method_locals(bid, pnames) if @in_gc_scope == 0 if @needs_gc == 1test/uncalled_method_dce.rbaddedView file
Loading diff…
test/uncalled_method_dce.rb.expectedaddedView file
Loading diff…