Commit 2ff1c607

2ff1c6074851Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

perf(codegen): gate sp_Method emit on actual usage

spinel_analyze's register_builtin_classes pushes a Method entry into @cls_names unconditionally, and emit_class_structs / emit_class_methods / emit_gc_scan_functions then emit the struct + sp_Method_new + sp_Method_initialize + sp_Method_gc_scan in every output .c. -O3 DCE removes the dead code from the final binary, but the C compiler still parses ~25 lines per program. For tiny programs the dead Method baggage is half the file.

Gate the emit on a new @needs_method flag. The flag is flipped by a pre-scan (scan_for_method_usage) that detects the AST patterns that trigger sp_Method usage:

  • method(:sym) / <recv>.method(:sym) -- bare or bound bind that emits sp_Method_new.
  • Method.new(...) / Method const reference -- explicit use of the Method class.
  • Any HashNode literal -- a Hash can become poly_poly_hash after inference settles, and emit_method_eql_dispatch references sp_Method to recover bound-receiver / fn_ptr identity for Method-typed keys. Conservative: scanning after inference would catch only the actual poly_poly_hash cases but the pre-scan has to run before emit_class_structs decides what to emit.

A new helper cls_emit_skipped(ci) collapses the gating logic into one place. Every loop over @cls_names (struct typedef + body, gc_scan, method forward decls, constructor

  • method bodies) consults it. The poly-recv dispatch arm in compile_poly_method_call that emits (sp_Method *)recv.v.p also picks up the gate -- pre-fix it would emit the cast unconditionally and break the link when Method's struct was skipped.

Optcarrot uses method(:peek_X) heavily so @needs_method = 1 fires; no checksum / fps regression. Bootstrap fixpoint OK (spinel_codegen's own AST has Method usage and HashNode literals). 382 tests pass.

Concretely:

  • puts "hi" C output: 62 lines -> 33 lines (-47%).
  • Programs that touch hashes or method(:sym) keep the full Method emit -- no behavior change there.

Details

Commit
2ff1c6074851
Parent
f7aed7c8ffeb

spinel_codegen.rbmodifiedView file

@@ -423,6 +423,16 @@
423423 # by the ARGV ConstantReadNode arm and the ARGV.length /
424424 # ARGV[i] dispatch sites.
425425 @needs_argv = 0
426+ # The Method class (registered by spinel_analyze's
427+ # register_builtin_classes) backs the `method(:sym)` /
428+ # `obj.method(:sym)` -> Method instance shape, plus the
429+ # poly_poly_hash eql? hook. When user code uses none of
430+ # those, the per-program emit (typedef sp_Method, struct,
431+ # sp_Method_gc_scan, sp_Method_new, sp_Method_initialize)
432+ # is DCE'd by -O3 but still costs compile time on every
433+ # build. Flip this when an actual usage site fires; emit
434+ # the Method struct + helpers only when it's set.
435+ @needs_method = 0
426436 @regexp_patterns = "".split(",")
427437 @regexp_flags = "".split(",")
428438 # Dynamic-regex (InterpolatedRegularExpressionNode) call-site cache.
@@ -6081,6 +6091,16 @@
60816091
60826092 emit_raw("/* Generated by Spinel AOT compiler */")
60836093 emit_raw("#include \"sp_runtime.h\"")
6094+ # Pre-scan for Method usage so emit_class_structs /
6095+ # emit_class_methods can skip the sp_Method definitions
6096+ # in programs that never touch `method(:sym)` /
6097+ # `Method.new`. poly_poly_hash usage implies Method
6098+ # (sp_method_obj_hash_dispatch reaches into sp_Method
6099+ # to recover the bound receiver + fn_ptr).
6100+ scan_for_method_usage(@root_id)
6101+ if @needs_poly_poly_hash == 1
6102+ @needs_method = 1
6103+ end
60846104 emit_sym_runtime
60856105 emit_ffi_externs
60866106 # Emit program-specific regexp patterns
@@ -7093,11 +7113,29 @@
70937113 0
70947114 end
70957115
7116+ # Should the emit pipeline skip class ci's struct + constructor +
7117+ # gc_scan + initialize? Currently only Method (the built-in
7118+ # registered by spinel_analyze) qualifies, and only when no usage
7119+ # site triggered @needs_method. Programs that never reach for
7120+ # `method(:sym)` / `Method.new` / hash literals get ~25 lines of
7121+ # dead C trimmed from their .c output.
7122+ def cls_emit_skipped(ci)
7123+ if ci < 0 || ci >= @cls_names.length
7124+ return 0
7125+ end
7126+ if @cls_names[ci] == "Method" && @needs_method == 0
7127+ return 1
7128+ end
7129+ 0
7130+ end
7131+
70967132 def emit_class_structs
70977133 # Forward declare typedefs
70987134 i = 0
70997135 while i < @cls_names.length
7100- emit_raw("typedef struct sp_" + @cls_names[i] + "_s sp_" + @cls_names[i] + ";")
7136+ if cls_emit_skipped(i) == 0
7137+ emit_raw("typedef struct sp_" + @cls_names[i] + "_s sp_" + @cls_names[i] + ";")
7138+ end
71017139 i = i + 1
71027140 end
71037141 if @cls_names.length > 0
@@ -7115,7 +7153,9 @@
71157153 end
71167154 i = 0
71177155 while i < @cls_names.length
7118- emit_class_struct_with_deps(i)
7156+ if cls_emit_skipped(i) == 0
7157+ emit_class_struct_with_deps(i)
7158+ end
71197159 i = i + 1
71207160 end
71217161 end
@@ -7124,6 +7164,9 @@
71247164 if @struct_emitted[ci] == 1
71257165 return
71267166 end
7167+ if cls_emit_skipped(ci) == 1
7168+ return
7169+ end
71277170 @struct_emitted[ci] = 1
71287171 # Walk this class and all its ancestors (whose ivars are flattened
71297172 # into this struct by emit_parent_fields) and emit any value-type
@@ -7327,6 +7370,10 @@
73277370 def emit_gc_scan_functions
73287371 i = 0
73297372 while i < @cls_names.length
7373+ if cls_emit_skipped(i) == 1
7374+ i = i + 1
7375+ next
7376+ end
73307377 if class_has_ptr_ivars(i) == 1
73317378 cname = @cls_names[i]
73327379 emit_raw("static void sp_" + cname + "_gc_scan(void *p) {")
@@ -7405,6 +7452,10 @@
74057452 # Class methods
74067453 i = 0
74077454 while i < @cls_names.length
7455+ if cls_emit_skipped(i) == 1
7456+ i = i + 1
7457+ next
7458+ end
74087459 cname = @cls_names[i]
74097460 # Pin @current_class_idx during this forward-decl emit pass
74107461 # so block_params_csig_for_body's infer_type calls (on
@@ -8055,6 +8106,10 @@
80558106 def emit_class_methods
80568107 i = 0
80578108 while i < @cls_names.length
8109+ if cls_emit_skipped(i) == 1
8110+ i = i + 1
8111+ next
8112+ end
80588113 emit_constructor(i)
80598114 mnames = @cls_meth_names[i].split(";")
80608115 returns = @cls_meth_returns[i].split(";")
@@ -9170,6 +9225,65 @@
91709225 end
91719226 end
91729227
9228+ # Walk the AST looking for usage sites that would emit
9229+ # sp_Method-typed code. Flips @needs_method before
9230+ # emit_class_structs / emit_class_methods run, so the Method
9231+ # struct + helpers stay out of programs that never use it.
9232+ # Pre-scan covers the patterns the codegen actually emits:
9233+ # - `method(:sym)` (bare bind)
9234+ # - `<recv>.method(:sym)` (bound bind)
9235+ # - `Method.new(...)` (explicit construction)
9236+ # - poly_poly_hash usage (sp_method_obj_hash_dispatch
9237+ # / _eql_dispatch call into
9238+ # sp_Method)
9239+ # The codegen-side arms also flip the flag, but most of them
9240+ # run after emit_class_methods -- the pre-scan is what makes
9241+ # the gate effective.
9242+ def scan_for_method_usage(nid)
9243+ if nid < 0
9244+ return
9245+ end
9246+ if @needs_method == 1
9247+ return
9248+ end
9249+ if @nd_type[nid] == "CallNode"
9250+ mname_sm = @nd_name[nid]
9251+ if mname_sm == "method"
9252+ @needs_method = 1
9253+ return
9254+ end
9255+ recv_sm = @nd_receiver[nid]
9256+ if recv_sm >= 0 && @nd_type[recv_sm] == "ConstantReadNode" && @nd_name[recv_sm] == "Method"
9257+ @needs_method = 1
9258+ return
9259+ end
9260+ end
9261+ if @nd_type[nid] == "ConstantReadNode" && @nd_name[nid] == "Method"
9262+ @needs_method = 1
9263+ return
9264+ end
9265+ # Hash literals are a conservative trigger: emit_method_eql_dispatch
9266+ # gets armed when any access infers as poly_poly_hash, and the
9267+ # dispatch shim references sp_Method to recover bound-receiver /
9268+ # fn_ptr identity for Method-typed keys. The HashNode literal may
9269+ # eventually narrow to a monomorphic variant (str_int_hash, etc.)
9270+ # and not actually trigger poly_poly_hash, but pre-scan can't see
9271+ # that decision -- it runs before inference settles. Over-emitting
9272+ # Method here costs ~25 lines of dead C that -O3 DCE's away; the
9273+ # alternative (missing the trigger) breaks the link.
9274+ if @nd_type[nid] == "HashNode"
9275+ @needs_method = 1
9276+ return
9277+ end
9278+ cs = []
9279+ push_child_ids(nid, cs)
9280+ k = 0
9281+ while k < cs.length
9282+ scan_for_method_usage(cs[k])
9283+ k = k + 1
9284+ end
9285+ end
9286+
91739287 def emit_main
91749288 stmts = get_body_stmts(@root_id)
91759289 # Pre-scan the entire AST for ARGV ConstantReadNode references
@@ -17703,7 +17817,7 @@
1770317817 # to N args by reusing the same arg_compiled list and unboxing
1770417818 # any that came in as poly.
1770517819 method_idx = find_class_idx("Method")
17706- if method_idx >= 0
17820+ if method_idx >= 0 && @needs_method == 1
1770717821 mp = "((sp_Method *)" + recv_tmp + ".v.p)"
1770817822 sig_args = "void *"
1770917823 call_args = "(void *)" + mp + "->iv_self_obj"