Commit b556c5a9

b556c5a9d0abMerged

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

Description

Skip implicit holder specialization for self-referential classes

Holder allocation-site specialization cloned a class that stores its own type in an ivar (a cons cell whose tail is another cell, a tree node holding child nodes, an env chained to a parent env) into a synthetic class. The clone left the constructor's self-typed parameter pointing at the original class while the rewritten call site passed the specialized type, so the generated C no longer type-checked, and the recursive method dispatch resolved to an undeclared base-class function.

bm_micro_lisp (cons cells) failed to compile and bm_binary_trees (tree nodes) segfaulted at runtime. This was a make bench regression not covered by make test, bootstrap, or optcarrot.

Skip holder specialization for any class constructed anywhere with its own type as an object argument. The non-self-referential holders the optimization targets are unaffected, and it gives no measurable optcarrot gain on these shapes.

Refs #1299

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
b556c5a9d0ab
Parent
b56168af7522

spinel_analyze.rbmodifiedView file

@@ -10104,6 +10104,36 @@
1010410104 0
1010510105 end
1010610106
10107+ # A self-referential holder stores its own type (a cons cell whose tail is
10108+ # another cell, a tree node holding child nodes, an env chained to a parent
10109+ # env). Cloning such a class to a synthetic specialization leaves the
10110+ # constructor's self-typed parameter pointing at the original class while the
10111+ # rewritten call site passes the specialized type, so the generated C does not
10112+ # type-check (and the recursive method dispatch resolves to an undeclared
10113+ # base-class function). Specialization buys nothing measurable here, so skip
10114+ # the whole class when any of its construction sites takes its own type as an
10115+ # object argument.
10116+ def implicit_class_has_self_obj_holder_site?(cname, site_ids, site_classes)
10117+ k = 0
10118+ while k < site_ids.length
10119+ if site_classes[k] == cname
10120+ arg_types = implicit_new_site_arg_types(site_ids[k])
10121+ j = 0
10122+ while j < arg_types.length
10123+ bt = base_type(arg_types[j])
10124+ if is_obj_type(bt) == 1 && is_array_type(bt) == 0
10125+ if bt[4, bt.length - 4] == cname
10126+ return 1
10127+ end
10128+ end
10129+ j = j + 1
10130+ end
10131+ end
10132+ k = k + 1
10133+ end
10134+ 0
10135+ end
10136+
1010710137 def implicit_new_site_obj_signature_key(cname, nid)
1010810138 arg_types = implicit_new_site_arg_types(nid)
1010910139 key = cname + "|" + arg_types.length.to_s
@@ -10365,6 +10395,7 @@
1036510395 while ci < original_class_count
1036610396 cname = @cls_names[ci]
1036710397 if count_implicit_new_sites_for_class(site_classes, cname) >= 2
10398+ holder_self_ref = implicit_class_has_self_obj_holder_site?(cname, site_ids, site_classes)
1036810399 k = 0
1036910400 while k < site_ids.length
1037010401 if site_classes[k] == cname
@@ -10372,7 +10403,7 @@
1037210403 holder_site = 0
1037310404 if implicit_specializable_class?(ci) == 1 && implicit_new_site_arg_count(site_ids[k]) == 0 && implicit_candidate_class_escaped?(escaping_classes, cname) == 0
1037410405 specialize_site = 1
10375- elsif implicit_holder_specializable_class?(ci) == 1 && implicit_new_site_arg_count(site_ids[k]) > 0 && implicit_new_site_has_obj_arg?(site_ids[k]) == 1
10406+ elsif implicit_holder_specializable_class?(ci) == 1 && holder_self_ref == 0 && implicit_new_site_arg_count(site_ids[k]) > 0 && implicit_new_site_has_obj_arg?(site_ids[k]) == 1
1037610407 specialize_site = 1
1037710408 holder_site = 1
1037810409 end