Commit a512de6f

a512de6fd72bMerged

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

Description

perf(codegen): inline runtime sym/class-to_s callers + answer why stubs are needed

The user asked: why do the generated 1-line sp_sym_to_s / sp_class_to_s stubs still emit when no symbol / class is actually used?

Answer: lib/sp_runtime.h forward-declares both names so its sp_poly_to_s / sp_poly_puts / sp_poly_inspect / sp_SymArray_inspect chain can reference them. The codegen-emitted .c is built with -Werror, and an unused forward decl trips warning: 'sp_sym_to_s' used but never defined. A 1-line stub satisfies the warning -- statically dead (no SP_TAG_SYM / SP_TAG_CLASS values can exist at runtime when sp_box_sym / sp_box_class is uncalled) but visible to the compiler.

The other piece this commit takes care of: marking the sp_runtime.h callers (sp_poly_to_s, sp_poly_puts, sp_poly_inspect, sp_SymArray_inspect) as static inline. Without the inline, GCC at -O2 sometimes keeps the unused chain in the object file with its dangling reference to sp_sym_to_s / sp_class_to_s -- the link fails until the stub satisfies the resolution. Inline ensures DCE drops them when unused; the stub is then a safety net for the -Werror warning, not a structural requirement.

Net effect:

  • puts "hi" C output: 13 lines unchanged (the 2 stubs contribute 2 of those lines; removing them would need to suppress GCC's "used but never defined" warning, which isn't easily controllable).
  • Bootstrap fixpoint OK; 382 tests pass; Optcarrot checksum 59662 stays.

Details

Commit
a512de6fd72b
Parent
b541bf4fa3de

lib/sp_runtime.hmodifiedView file

@@ -717,7 +717,7 @@
717717 static const char*sp_StrArray_inspect(sp_StrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_str_inspect(a->data[i]));}sp_String_append(s,"]");return s->data;}
718718 /* Symbol arrays share the IntArray representation (sp_sym = mrb_int),
719719 but each element is rendered as ":name" via sp_sym_to_s. */
720-static const char*sp_SymArray_inspect(sp_IntArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,":");sp_String_append(s,sp_sym_to_s((sp_sym)a->data[a->start+i]));}sp_String_append(s,"]");return s->data;}
720+static inline const char*sp_SymArray_inspect(sp_IntArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,":");sp_String_append(s,sp_sym_to_s((sp_sym)a->data[a->start+i]));}sp_String_append(s,"]");return s->data;}
721721 /* PtrArray elements are object pointers without a per-element class
722722 tag, so we render them as `#<Object>` rather than recursing. */
723723 static const char*sp_PtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,"#<Object>");}sp_String_append(s,"]");return s->data;}
@@ -1006,7 +1006,7 @@
10061006 return buf;
10071007 }
10081008 static sp_RbVal sp_box_poly_array(void *p) { return sp_box_obj(p, SP_BUILTIN_POLY_ARRAY); }
1009-static void sp_poly_puts(sp_RbVal v) {
1009+static inline void sp_poly_puts(sp_RbVal v) {
10101010 switch (v.tag) {
10111011 case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;
10121012 case SP_TAG_STR: if (v.v.s) { fputs(v.v.s, stdout); if (!*v.v.s || v.v.s[strlen(v.v.s)-1] != '\n') putchar('\n'); } else putchar('\n'); break;
@@ -1037,11 +1037,13 @@
10371037 static mrb_bool sp_poly_nil_p(sp_RbVal v) { return v.tag == SP_TAG_NIL; }
10381038 static mrb_bool sp_poly_truthy(sp_RbVal v) { return !(v.tag == SP_TAG_NIL || (v.tag == SP_TAG_BOOL && !v.v.b)); }
10391039 /* Issue #404 Phase 3: forward-declare the program-emitted class
1040- name lookup so sp_poly_to_s's SP_TAG_CLASS arm resolves. The
1041- codegen emits sp_class_to_s unconditionally (even for programs
1042- with no user classes), so the link is always satisfied. */
1040+ name lookup so sp_poly_to_s's SP_TAG_CLASS arm resolves.
1041+ The codegen emits a 1-line stub when no class const is used,
1042+ or the real body when @needs_class_table fires. The forward
1043+ decl always needs a definition somewhere because -Werror
1044+ trips on "used but never defined" otherwise. */
10431045 static const char *sp_class_to_s(sp_Class c);
1044-static const char *sp_poly_to_s(sp_RbVal v) {
1046+static inline const char *sp_poly_to_s(sp_RbVal v) {
10451047 switch (v.tag) {
10461048 case SP_TAG_INT: return sp_int_to_s(v.v.i);
10471049 case SP_TAG_STR: return v.v.s ? v.v.s : sp_str_empty;
@@ -1150,7 +1152,7 @@
11501152 each branch reuses the matching primitive inspect helper. Falls back
11511153 to "#<Object>" for SP_TAG_OBJ because the runtime has no class-name
11521154 table yet (follow-up PR). Returns a GC-managed C string. */
1153-static const char *sp_poly_inspect(sp_RbVal v) {
1155+static inline const char *sp_poly_inspect(sp_RbVal v) {
11541156 switch (v.tag) {
11551157 case SP_TAG_INT: return sp_int_to_s(v.v.i);
11561158 case SP_TAG_STR: return sp_str_inspect(v.v.s);