Commit b541bf4f

b541bf4fa3deMerged

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

Description

perf(codegen): gate basic class table on actual Class-value usage

sp_class_names + sp_class_to_s + sp_class_eq were always emitted when @cls_names.length > 0. The names table alone is ~1KB at spinel_codegen scale (cache-line for typical small programs), and the helpers add ~3 more lines of C.

Gate the whole emit on @needs_class_table, set by the pre-scan (scan_for_class_hierarchy_usage) when any of these AST patterns appears:

  • ConstantReadNode whose name resolves to a class / module / built-in class -- the const lowers to a sp_Class literal.
  • .class CallNode -- the lowering emits a sp_Class value.
  • .to_s / .name / .inspect with a ConstantReadNode / ConstantPathNode / .class CallNode recv -- chained <class>.to_s shape.
  • .== / .!= / .eql? with a ConstantReadNode operand -- sp_class_eq dispatch.
  • Any of the hierarchy-flag triggers (parents / ancestors / for_poly) -- those emit references sp_class_names too, so they imply @needs_class_table.

sp_runtime.h's sp_poly_to_s forward-declares sp_class_to_s for its SP_TAG_CLASS arm and isn't itself static inline -- -O3 doesn't always DCE it before the linker complains. Cheapest fix: emit a 1-line sp_class_to_s stub (return "") when the program has no Class-value usage. SP_TAG_CLASS values can't exist at runtime when sp_box_class is uncalled, so the stub is statically dead but satisfies the link.

Concrete effect:

  • puts "hi" C output: 20 lines -> 13 lines (-35%; -79% from the unoptimized 62-line shape).
  • Programs with any class-const usage keep the full table
    • helpers emission.
  • Optcarrot checksum 59662 unchanged; bootstrap fixpoint OK; 382 tests pass.

Details

Commit
b541bf4fa3de
Parent
ce1d1b795e25

spinel_codegen.rbmodifiedView file

@@ -6102,13 +6102,20 @@
61026102 @needs_method = 1
61036103 end
61046104 # Same shape for the class-hierarchy tables / helpers:
6105- # @needs_class_parents / @needs_class_ancestors /
6106- # @needs_class_for_poly are set by compile-side arms too
6107- # late to land before emit_class_runtime emits the gated
6108- # sections. Pre-scan covers .superclass / .ancestors /
6109- # dynamic is_a? / class-const comparators / case-when on
6110- # Class subjects.
6105+ # @needs_class_table / @needs_class_parents /
6106+ # @needs_class_ancestors / @needs_class_for_poly are set
6107+ # by compile-side arms too late to land before
6108+ # emit_class_runtime emits the gated sections. Pre-scan
6109+ # covers .class / class-const value / .superclass /
6110+ # .ancestors / dynamic is_a? / class-const comparators /
6111+ # case-when on Class subjects.
61116112 scan_for_class_hierarchy_usage(@root_id)
6113+ # Hierarchy helpers (parents, ancestors, for_poly) all
6114+ # reference sp_class_names; if any of them is set, the
6115+ # basic table emit has to come along too.
6116+ if @needs_class_parents == 1 || @needs_class_ancestors == 1 || @needs_class_for_poly == 1
6117+ @needs_class_table = 1
6118+ end
61126119 emit_sym_runtime
61136120 emit_ffi_externs
61146121 # Emit program-specific regexp patterns
@@ -6345,12 +6352,21 @@
63456352 # array (~one cache line for typical small programs, ~1KB at
63466353 # spinel_codegen scale).
63476354 def emit_class_runtime
6348- # Issue #404 Phase 3: always emit the names table + sp_class_to_s,
6349- # even when no user class is declared. sp_runtime.h's sp_poly_to_s
6350- # has a SP_TAG_CLASS arm that forward-declares sp_class_to_s; an
6351- # empty TU would otherwise leave that reference unresolved. A
6352- # sentinel 1-slot array carries the empty case (ISO C forbids
6353- # zero-length array initializers).
6355+ # When the program has no Class-value usage (no ConstantReadNode
6356+ # for a class/module name, no `.class` call, no hierarchy
6357+ # methods), skip the heavy table + helpers. sp_runtime.h's
6358+ # sp_poly_to_s forward-declares sp_class_to_s and references
6359+ # it from its SP_TAG_CLASS arm. -O3 DCE doesn't always remove
6360+ # sp_poly_to_s in time (it's `static const char *`, not
6361+ # `static inline`), so the link fails on the unresolved
6362+ # sp_class_to_s. Cheapest fix: emit a 1-line stub that
6363+ # returns "" regardless. SP_TAG_CLASS values can't exist at
6364+ # runtime when no class const is boxed, so the stub is
6365+ # statically dead but satisfies the linker.
6366+ if @needs_class_table == 0
6367+ emit_raw("static const char *sp_class_to_s(sp_Class c){(void)c;return \"\";}")
6368+ return
6369+ end
63546370 emit_raw("/* sp_Class names table (issue #404) */")
63556371 # Phase 3 Tier 4: unified cls_id space prefixed with
63566372 # built-in primitives (cls_ids 0..BC-1), then user classes
@@ -9255,6 +9271,16 @@
92559271 return
92569272 end
92579273 t_ch = @nd_type[nid]
9274+ if t_ch == "ConstantReadNode"
9275+ cname_ch = @nd_name[nid]
9276+ # ConstantReadNode resolving to a class / module / built-in
9277+ # class name lowers to a sp_Class value (`((sp_Class){<id>})`),
9278+ # which needs sp_class_names + sp_class_to_s for .to_s and
9279+ # the rest of the basic helpers.
9280+ if find_class_idx(cname_ch) >= 0 || find_module_idx_local(cname_ch) >= 0 || builtin_class_id_for_name(cname_ch) >= 0
9281+ @needs_class_table = 1
9282+ end
9283+ end
92589284 if t_ch == "CallNode"
92599285 mname_ch = @nd_name[nid]
92609286 if mname_ch == "superclass"
@@ -9263,6 +9289,43 @@
92639289 if mname_ch == "ancestors"
92649290 @needs_class_ancestors = 1
92659291 end
9292+ if mname_ch == "class"
9293+ # `.class` lowers to a sp_Class literal -- the names
9294+ # table plus sp_class_to_s back the .to_s / .name / .inspect
9295+ # chain that typically follows.
9296+ @needs_class_table = 1
9297+ end
9298+ if mname_ch == "to_s" || mname_ch == "name" || mname_ch == "inspect"
9299+ # Static recv check is too brittle pre-inference; flag
9300+ # @needs_class_table conservatively when these mnames
9301+ # appear with a recv that could be a Class value
9302+ # (ConstantReadNode or a chained .class call).
9303+ recv_to_s = @nd_receiver[nid]
9304+ if recv_to_s >= 0
9305+ rty_to_s = @nd_type[recv_to_s]
9306+ if rty_to_s == "ConstantReadNode" || rty_to_s == "ConstantPathNode"
9307+ @needs_class_table = 1
9308+ elsif rty_to_s == "CallNode" && @nd_name[recv_to_s] == "class"
9309+ @needs_class_table = 1
9310+ end
9311+ end
9312+ end
9313+ if mname_ch == "==" || mname_ch == "!=" || mname_ch == "eql?"
9314+ # sp_class_eq fires when an operand is a Class value;
9315+ # pre-scan flags conservatively on ConstantReadNode
9316+ # operands.
9317+ recv_eq = @nd_receiver[nid]
9318+ if recv_eq >= 0 && (@nd_type[recv_eq] == "ConstantReadNode" || @nd_type[recv_eq] == "ConstantPathNode")
9319+ @needs_class_table = 1
9320+ end
9321+ args_id_eq = @nd_arguments[nid]
9322+ if args_id_eq >= 0
9323+ aa_eq = get_args(args_id_eq)
9324+ if aa_eq.length > 0 && (@nd_type[aa_eq[0]] == "ConstantReadNode" || @nd_type[aa_eq[0]] == "ConstantPathNode")
9325+ @needs_class_table = 1
9326+ end
9327+ end
9328+ end
92669329 if mname_ch == "is_a?" || mname_ch == "kind_of?" || mname_ch == "instance_of?"
92679330 args_id_ch = @nd_arguments[nid]
92689331 if args_id_ch >= 0