Commit 8645f0f3

8645f0f330b9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 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
8645f0f330b9
Parent
757ecfc377bc

spinel_analyze.rbmodifiedView file

@@ -430,6 +430,7 @@
430430 @needs_str_poly_hash = 0
431431 @needs_sym_poly_hash = 0
432432 @cls_cmeth_live = ""
433+ @cls_meth_live = ""
433434 @multi_const_inits = "".split(",")
434435
435436 # Lambda support
@@ -14435,6 +14436,7 @@
1443514436 collect_sym_names
1443614437 scan_toplevel_ivars(@root_id)
1443714438 compute_live_cls_methods
14439+ compute_live_instance_methods
1443814440 @analysis_frozen = 1
1443914441 precompute_all_scope_decls
1444014442 annotate_all_node_types
@@ -16157,7 +16159,178 @@
1615716159 iter = iter + 1
1615816160 end
1615916161 end
16162+ # Issue #393. Mark instance methods reachable from the program
16163+ # entry. Anything not marked gets a stub body at emit time
16164+ # (`(void)params; return default;`) so an uncalled `def f(x); @typed = x; end`
16165+ # whose param defaulted to int doesn't fail C-compile against a
16166+ # narrower ivar slot.
16167+ #
16168+ # Conservative liveness: a method `M` on class `C` is live iff:
16169+ # 1. `M` == "initialize" (constructor synth always reaches it).
16170+ # 2. Some CallNode anywhere in the program names `M`.
16171+ # 3. Some SymbolNode anywhere has value `M` (covers method(:M) /
16172+ # define_method(:M) / send(:M, ...) reflection sites).
16173+ # This over-approximates -- a method named "size" on user class `C`
16174+ # is marked live whenever ANY call site (even an unrelated
16175+ # `arr.size`) names "size". That's fine for the DCE goal; the
16176+ # downside is false-negatives (a genuinely-unused method we keep)
16177+ # rather than false-positives (a live method we strip).
16178+ def compute_live_instance_methods
16179+ @cls_meth_live = ""
16180+ # Step 1: methods whose name is always-implicitly-dispatched are
16181+ # marked live unconditionally. `<=>` is reached by Comparable's
16182+ # `<` / `>` etc. operators (compile_call_expr's cmp_owner arm
16183+ # synthesises the call, so `<=>` itself never appears as a
16184+ # CallNode name in the AST — and the conservative "name appears
16185+ # somewhere" rule below would miss it). Same for the bracket
16186+ # operators, common conversion methods, and `inspect` / `to_s`
16187+ # called by string interpolation, `puts`, etc.
16188+ always_live = ["initialize", "<=>", "==", "!=", "eql?", "hash",
16189+ "to_s", "inspect", "to_a", "to_i", "to_f", "to_str",
16190+ "[]", "[]=", "each", "<", ">", "<=", ">=",
16191+ "+", "-", "*", "/", "%", "<<", "coerce", "<<="]
16192+ ci = 0
16193+ while ci < @cls_names.length
16194+ mnames_init = @cls_meth_names[ci].split(";")
16195+ mj_init = 0
16196+ while mj_init < mnames_init.length
16197+ ai = 0
16198+ while ai < always_live.length
16199+ if mnames_init[mj_init] == always_live[ai]
16200+ cls_meth_mark_live(ci, always_live[ai])
16201+ end
16202+ ai = ai + 1
16203+ end
16204+ mj_init = mj_init + 1
16205+ end
16206+ ci = ci + 1
16207+ end
16208+ # Step 2/3: collect every CallNode name + SymbolNode value, mark
16209+ # methods on every class that has them.
16210+ # `"".split(",")` initializer types `used` as a str_array, since
16211+ # `acc` in collect_used_method_names is pushed strings only.
16212+ used = "".split(",")
16213+ collect_used_method_names(@root_id, used)
16214+ # Lifted instance_eval blocks: their bodies were detached from
16215+ # @root_id by ieval_rewrite_call and stashed in @ieval_body_ids,
16216+ # so a walk from @root_id misses any `get("/")` / similar call
16217+ # buried inside `app.instance_eval { get("/") }`. Walk them too.
16218+ ie = 0
16219+ while ie < @ieval_body_ids.length
16220+ collect_used_method_names(@ieval_body_ids[ie], used)
16221+ ie = ie + 1
16222+ end
16223+ # User-defined method bodies: top-level methods (@meth_body_ids)
16224+ # and instance / class methods (@cls_meth_bodies + @cls_cmeth_bodies).
16225+ # The @root_id walk hits class definitions but those routes only to
16226+ # ClassNode -> body, not to each method body's nested call sites.
16227+ mi_b = 0
16228+ while mi_b < @meth_body_ids.length
16229+ collect_used_method_names(@meth_body_ids[mi_b], used)
16230+ mi_b = mi_b + 1
16231+ end
16232+ ci_b = 0
16233+ while ci_b < @cls_names.length
16234+ mb_list = @cls_meth_bodies[ci_b].split(";")
16235+ mbk = 0
16236+ while mbk < mb_list.length
16237+ bid_mb = mb_list[mbk].to_i
16238+ if bid_mb > 0
16239+ collect_used_method_names(bid_mb, used)
16240+ end
16241+ mbk = mbk + 1
16242+ end
16243+ cmb_list = @cls_cmeth_bodies[ci_b].split(";")
16244+ cmbk = 0
16245+ while cmbk < cmb_list.length
16246+ bid_cmb = cmb_list[cmbk].to_i
16247+ if bid_cmb > 0
16248+ collect_used_method_names(bid_cmb, used)
16249+ end
16250+ cmbk = cmbk + 1
16251+ end
16252+ ci_b = ci_b + 1
16253+ end
16254+ ui = 0
16255+ while ui < used.length
16256+ uname = used[ui]
16257+ ci2 = 0
16258+ while ci2 < @cls_names.length
16259+ mns = @cls_meth_names[ci2].split(";")
16260+ mj2 = 0
16261+ while mj2 < mns.length
16262+ if mns[mj2] == uname
16263+ cls_meth_mark_live(ci2, uname)
16264+ end
16265+ mj2 = mj2 + 1
16266+ end
16267+ ci2 = ci2 + 1
16268+ end
16269+ ui = ui + 1
16270+ end
16271+ end
1616016272
16273+ # Walk the AST collecting CallNode names + SymbolNode values into
16274+ # `acc`. Used by compute_live_instance_methods. Iterative (explicit
16275+ # stack via `push_child_ids`) so we don't recurse into 600+-deep
16276+ # method bodies and blow the stack.
16277+ def collect_used_method_names(nid, acc)
16278+ if nid < 0
16279+ return
16280+ end
16281+ t = @nd_type[nid]
16282+ if t == "CallNode"
16283+ cname = @nd_name[nid]
16284+ if cname != "" && not_in(cname, acc) == 1
16285+ acc.push(cname)
16286+ end
16287+ end
16288+ if t == "SymbolNode"
16289+ sval = @nd_content[nid]
16290+ if sval != "" && not_in(sval, acc) == 1
16291+ acc.push(sval)
16292+ end
16293+ end
16294+ cs = []
16295+ push_child_ids(nid, cs)
16296+ ck = 0
16297+ while ck < cs.length
16298+ collect_used_method_names(cs[ck], acc)
16299+ ck = ck + 1
16300+ end
16301+ end
16302+
16303+ def cls_meth_is_live(ci, mname)
16304+ if ci < 0 || ci >= @cls_names.length
16305+ return 0
16306+ end
16307+ if @cls_meth_live == nil || @cls_meth_live == ""
16308+ return 0
16309+ end
16310+ needle = ";" + @cls_names[ci] + "::" + mname + ";"
16311+ haystack = ";" + @cls_meth_live + ";"
16312+ if haystack.include?(needle)
16313+ return 1
16314+ end
16315+ 0
16316+ end
16317+
16318+ def cls_meth_mark_live(ci, mname)
16319+ if ci < 0 || ci >= @cls_names.length
16320+ return
16321+ end
16322+ if cls_meth_is_live(ci, mname) == 1
16323+ return
16324+ end
16325+ entry = @cls_names[ci] + "::" + mname
16326+ if @cls_meth_live == ""
16327+ @cls_meth_live = entry
16328+ else
16329+ @cls_meth_live = @cls_meth_live + ";" + entry
16330+ end
16331+ end
16332+
16333+
1616116334 def cls_cmeth_is_live(ci, mname)
1616216335 if ci < 0 || ci >= @cls_names.length
1616316336 return 0
@@ -18696,6 +18869,7 @@
1869618869
1869718870 # Non-array string ivars (computed in analyze, consumed by emit)
1869818871 buf = buf + "STR @cls_cmeth_live " + ir_escape(@cls_cmeth_live) + "\n"
18872+ buf = buf + "STR @cls_meth_live " + ir_escape(@cls_meth_live) + "\n"
1869918873
1870018874 # Per-AST-node records (T / NM / NB / SN / ST) get accumulated
1870118875 # into a StrArray and joined once. Building them with `buf + ...`