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(...)/Methodconst 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.palso 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
# by the ARGV ConstantReadNode arm and the ARGV.length / # ARGV[i] dispatch sites. @needs_argv = 0 # The Method class (registered by spinel_analyze's # register_builtin_classes) backs the `method(:sym)` / # `obj.method(:sym)` -> Method instance shape, plus the # poly_poly_hash eql? hook. When user code uses none of # those, the per-program emit (typedef sp_Method, struct, # sp_Method_gc_scan, sp_Method_new, sp_Method_initialize) # is DCE'd by -O3 but still costs compile time on every # build. Flip this when an actual usage site fires; emit # the Method struct + helpers only when it's set. @needs_method = 0 @regexp_patterns = "".split(",") @regexp_flags = "".split(",") # Dynamic-regex (InterpolatedRegularExpressionNode) call-site cache. emit_raw("/* Generated by Spinel AOT compiler */") emit_raw("#include \"sp_runtime.h\"") # Pre-scan for Method usage so emit_class_structs / # emit_class_methods can skip the sp_Method definitions # in programs that never touch `method(:sym)` / # `Method.new`. poly_poly_hash usage implies Method # (sp_method_obj_hash_dispatch reaches into sp_Method # to recover the bound receiver + fn_ptr). scan_for_method_usage(@root_id) if @needs_poly_poly_hash == 1 @needs_method = 1 end emit_sym_runtime emit_ffi_externs # Emit program-specific regexp patterns 0 end # Should the emit pipeline skip class ci's struct + constructor + # gc_scan + initialize? Currently only Method (the built-in # registered by spinel_analyze) qualifies, and only when no usage # site triggered @needs_method. Programs that never reach for # `method(:sym)` / `Method.new` / hash literals get ~25 lines of # dead C trimmed from their .c output. def cls_emit_skipped(ci) if ci < 0 || ci >= @cls_names.length return 0 end if @cls_names[ci] == "Method" && @needs_method == 0 return 1 end 0 end def emit_class_structs # Forward declare typedefs i = 0 while i < @cls_names.length emit_raw("typedef struct sp_" + @cls_names[i] + "_s sp_" + @cls_names[i] + ";") if cls_emit_skipped(i) == 0 emit_raw("typedef struct sp_" + @cls_names[i] + "_s sp_" + @cls_names[i] + ";") end i = i + 1 end if @cls_names.length > 0 end i = 0 while i < @cls_names.length emit_class_struct_with_deps(i) if cls_emit_skipped(i) == 0 emit_class_struct_with_deps(i) end i = i + 1 end end if @struct_emitted[ci] == 1 return end if cls_emit_skipped(ci) == 1 return end @struct_emitted[ci] = 1 # Walk this class and all its ancestors (whose ivars are flattened # into this struct by emit_parent_fields) and emit any value-type def emit_gc_scan_functions i = 0 while i < @cls_names.length if cls_emit_skipped(i) == 1 i = i + 1 next end if class_has_ptr_ivars(i) == 1 cname = @cls_names[i] emit_raw("static void sp_" + cname + "_gc_scan(void *p) {") # Class methods i = 0 while i < @cls_names.length if cls_emit_skipped(i) == 1 i = i + 1 next end cname = @cls_names[i] # Pin @current_class_idx during this forward-decl emit pass # so block_params_csig_for_body's infer_type calls (on def emit_class_methods i = 0 while i < @cls_names.length if cls_emit_skipped(i) == 1 i = i + 1 next end emit_constructor(i) mnames = @cls_meth_names[i].split(";") returns = @cls_meth_returns[i].split(";") end end # Walk the AST looking for usage sites that would emit # sp_Method-typed code. Flips @needs_method before # emit_class_structs / emit_class_methods run, so the Method # struct + helpers stay out of programs that never use it. # Pre-scan covers the patterns the codegen actually emits: # - `method(:sym)` (bare bind) # - `<recv>.method(:sym)` (bound bind) # - `Method.new(...)` (explicit construction) # - poly_poly_hash usage (sp_method_obj_hash_dispatch # / _eql_dispatch call into # sp_Method) # The codegen-side arms also flip the flag, but most of them # run after emit_class_methods -- the pre-scan is what makes # the gate effective. def scan_for_method_usage(nid) if nid < 0 return end if @needs_method == 1 return end if @nd_type[nid] == "CallNode" mname_sm = @nd_name[nid] if mname_sm == "method" @needs_method = 1 return end recv_sm = @nd_receiver[nid] if recv_sm >= 0 && @nd_type[recv_sm] == "ConstantReadNode" && @nd_name[recv_sm] == "Method" @needs_method = 1 return end end if @nd_type[nid] == "ConstantReadNode" && @nd_name[nid] == "Method" @needs_method = 1 return end # Hash literals are a conservative trigger: emit_method_eql_dispatch # gets armed when any access infers as poly_poly_hash, and the # dispatch shim references sp_Method to recover bound-receiver / # fn_ptr identity for Method-typed keys. The HashNode literal may # eventually narrow to a monomorphic variant (str_int_hash, etc.) # and not actually trigger poly_poly_hash, but pre-scan can't see # that decision -- it runs before inference settles. Over-emitting # Method here costs ~25 lines of dead C that -O3 DCE's away; the # alternative (missing the trigger) breaks the link. if @nd_type[nid] == "HashNode" @needs_method = 1 return end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length scan_for_method_usage(cs[k]) k = k + 1 end end def emit_main stmts = get_body_stmts(@root_id) # Pre-scan the entire AST for ARGV ConstantReadNode references # to N args by reusing the same arg_compiled list and unboxing # any that came in as poly. method_idx = find_class_idx("Method") if method_idx >= 0 if method_idx >= 0 && @needs_method == 1 mp = "((sp_Method *)" + recv_tmp + ".v.p)" sig_args = "void *" call_args = "(void *)" + mp + "->iv_self_obj"