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

@@ -15937,6 +15937,7 @@
1593715937 # `def self.factory(attrs); new(attrs); end` against a 0-arg
1593815938 # or differently-typed constructor).
1593915939 compute_live_cls_methods
15940+ compute_live_instance_methods
1594015941 emit_forward_decls
1594115942 emit_global_constants
1594215943 emit_raw("/*LAMBDA_INSERT_POINT*/")
@@ -18708,6 +18709,177 @@
1870818709 # Live entries are stored as `<ClassName>::<methodName>` joined
1870918710 # by `;` in @cls_cmeth_live. Idempotent: marking an already-live
1871018711 # entry is a no-op.
18712+ # Issue #393. Mark instance methods reachable from the program
18713+ # entry. Anything not marked gets a stub body at emit time
18714+ # (`(void)params; return default;`) so an uncalled `def f(x); @typed = x; end`
18715+ # whose param defaulted to int doesn't fail C-compile against a
18716+ # narrower ivar slot.
18717+ #
18718+ # Conservative liveness: a method `M` on class `C` is live iff:
18719+ # 1. `M` == "initialize" (constructor synth always reaches it).
18720+ # 2. Some CallNode anywhere in the program names `M`.
18721+ # 3. Some SymbolNode anywhere has value `M` (covers method(:M) /
18722+ # define_method(:M) / send(:M, ...) reflection sites).
18723+ # This over-approximates -- a method named "size" on user class `C`
18724+ # is marked live whenever ANY call site (even an unrelated
18725+ # `arr.size`) names "size". That's fine for the DCE goal; the
18726+ # downside is false-negatives (a genuinely-unused method we keep)
18727+ # rather than false-positives (a live method we strip).
18728+ def compute_live_instance_methods
18729+ @cls_meth_live = ""
18730+ # Step 1: methods whose name is always-implicitly-dispatched are
18731+ # marked live unconditionally. `<=>` is reached by Comparable's
18732+ # `<` / `>` etc. operators (compile_call_expr's cmp_owner arm
18733+ # synthesises the call, so `<=>` itself never appears as a
18734+ # CallNode name in the AST — and the conservative "name appears
18735+ # somewhere" rule below would miss it). Same for the bracket
18736+ # operators, common conversion methods, and `inspect` / `to_s`
18737+ # called by string interpolation, `puts`, etc.
18738+ always_live = ["initialize", "<=>", "==", "!=", "eql?", "hash",
18739+ "to_s", "inspect", "to_a", "to_i", "to_f", "to_str",
18740+ "[]", "[]=", "each", "<", ">", "<=", ">=",
18741+ "+", "-", "*", "/", "%", "<<", "coerce", "<<="]
18742+ ci = 0
18743+ while ci < @cls_names.length
18744+ mnames_init = @cls_meth_names[ci].split(";")
18745+ mj_init = 0
18746+ while mj_init < mnames_init.length
18747+ ai = 0
18748+ while ai < always_live.length
18749+ if mnames_init[mj_init] == always_live[ai]
18750+ cls_meth_mark_live(ci, always_live[ai])
18751+ end
18752+ ai = ai + 1
18753+ end
18754+ mj_init = mj_init + 1
18755+ end
18756+ ci = ci + 1
18757+ end
18758+ # Step 2/3: collect every CallNode name + SymbolNode value, mark
18759+ # methods on every class that has them.
18760+ # `"".split(",")` initializer types `used` as a str_array, since
18761+ # `acc` in collect_used_method_names is pushed strings only.
18762+ used = "".split(",")
18763+ collect_used_method_names(@root_id, used)
18764+ # Lifted instance_eval blocks: their bodies were detached from
18765+ # @root_id by ieval_rewrite_call and stashed in @ieval_body_ids,
18766+ # so a walk from @root_id misses any `get("/")` / similar call
18767+ # buried inside `app.instance_eval { get("/") }`. Walk them too.
18768+ ie = 0
18769+ while ie < @ieval_body_ids.length
18770+ collect_used_method_names(@ieval_body_ids[ie], used)
18771+ ie = ie + 1
18772+ end
18773+ # User-defined method bodies: top-level methods (@meth_body_ids)
18774+ # and instance / class methods (@cls_meth_bodies + @cls_cmeth_bodies).
18775+ # The @root_id walk hits class definitions but those routes only to
18776+ # ClassNode -> body, not to each method body's nested call sites.
18777+ mi_b = 0
18778+ while mi_b < @meth_body_ids.length
18779+ collect_used_method_names(@meth_body_ids[mi_b], used)
18780+ mi_b = mi_b + 1
18781+ end
18782+ ci_b = 0
18783+ while ci_b < @cls_names.length
18784+ mb_list = @cls_meth_bodies[ci_b].split(";")
18785+ mbk = 0
18786+ while mbk < mb_list.length
18787+ bid_mb = mb_list[mbk].to_i
18788+ if bid_mb > 0
18789+ collect_used_method_names(bid_mb, used)
18790+ end
18791+ mbk = mbk + 1
18792+ end
18793+ cmb_list = @cls_cmeth_bodies[ci_b].split(";")
18794+ cmbk = 0
18795+ while cmbk < cmb_list.length
18796+ bid_cmb = cmb_list[cmbk].to_i
18797+ if bid_cmb > 0
18798+ collect_used_method_names(bid_cmb, used)
18799+ end
18800+ cmbk = cmbk + 1
18801+ end
18802+ ci_b = ci_b + 1
18803+ end
18804+ ui = 0
18805+ while ui < used.length
18806+ uname = used[ui]
18807+ ci2 = 0
18808+ while ci2 < @cls_names.length
18809+ mns = @cls_meth_names[ci2].split(";")
18810+ mj2 = 0
18811+ while mj2 < mns.length
18812+ if mns[mj2] == uname
18813+ cls_meth_mark_live(ci2, uname)
18814+ end
18815+ mj2 = mj2 + 1
18816+ end
18817+ ci2 = ci2 + 1
18818+ end
18819+ ui = ui + 1
18820+ end
18821+ end
18822+
18823+ # Walk the AST collecting CallNode names + SymbolNode values into
18824+ # `acc`. Used by compute_live_instance_methods. Iterative (explicit
18825+ # stack via `push_child_ids`) so we don't recurse into 600+-deep
18826+ # method bodies and blow the stack.
18827+ def collect_used_method_names(nid, acc)
18828+ if nid < 0
18829+ return
18830+ end
18831+ t = @nd_type[nid]
18832+ if t == "CallNode"
18833+ cname = @nd_name[nid]
18834+ if cname != "" && not_in(cname, acc) == 1
18835+ acc.push(cname)
18836+ end
18837+ end
18838+ if t == "SymbolNode"
18839+ sval = @nd_content[nid]
18840+ if sval != "" && not_in(sval, acc) == 1
18841+ acc.push(sval)
18842+ end
18843+ end
18844+ cs = []
18845+ push_child_ids(nid, cs)
18846+ ck = 0
18847+ while ck < cs.length
18848+ collect_used_method_names(cs[ck], acc)
18849+ ck = ck + 1
18850+ end
18851+ end
18852+
18853+ def cls_meth_is_live(ci, mname)
18854+ if ci < 0 || ci >= @cls_names.length
18855+ return 0
18856+ end
18857+ if @cls_meth_live == nil || @cls_meth_live == ""
18858+ return 0
18859+ end
18860+ needle = ";" + @cls_names[ci] + "::" + mname + ";"
18861+ haystack = ";" + @cls_meth_live + ";"
18862+ if haystack.include?(needle)
18863+ return 1
18864+ end
18865+ 0
18866+ end
18867+
18868+ def cls_meth_mark_live(ci, mname)
18869+ if ci < 0 || ci >= @cls_names.length
18870+ return
18871+ end
18872+ if cls_meth_is_live(ci, mname) == 1
18873+ return
18874+ end
18875+ entry = @cls_names[ci] + "::" + mname
18876+ if @cls_meth_live == ""
18877+ @cls_meth_live = entry
18878+ else
18879+ @cls_meth_live = @cls_meth_live + ";" + entry
18880+ end
18881+ end
18882+
1871118883 def compute_live_cls_methods
1871218884 @cls_cmeth_live = ""
1871318885 collect_cls_calls(@root_id, -1)
@@ -19470,7 +19642,21 @@
1947019642 is_tramp = 1
1947119643 end
1947219644 end
19473- if bid >= 0 && is_tramp == 0
19645+ # Issue #393: stub uncalled methods so an inferred-int param
19646+ # doesn't fail C-compile against a narrower ivar slot.
19647+ is_dead = 0
19648+ if cls_meth_is_live(ci, mname) == 0
19649+ is_dead = 1
19650+ end
19651+ if is_dead == 1
19652+ # Mark every param as `(void)`-cast so -Wunused doesn't fire,
19653+ # then return the default.
19654+ jd = 0
19655+ while jd < pnames.length
19656+ emit(" (void)lv_" + pnames[jd] + ";")
19657+ jd = jd + 1
19658+ end
19659+ elsif bid >= 0 && is_tramp == 0
1947419660 declare_method_locals(bid, pnames)
1947519661 if @in_gc_scope == 0
1947619662 if @needs_gc == 1