Commit 5de531fb

5de531fb630cMerged

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

Description

Static-fold dead if recv.reader branches backed by a nil-pinned ivar

emit_if already drops branches guarded by a statically-nil ivar read (if @mode where every write to @mode is nil). Extend that to a getter call recv.reader (no args, no block, pure object receiver) by resolving through the backing ivar @reader and checking all its writes are nil, rather than trusting the inferred return type, which can mis-widen a nil-pinned ivar to an unrelated type.

This lets optcarrot's if @conf.stackprof_mode (with @stackprof_mode and @stackprof_output pinned to nil for AOT static-fold) drop the dead StackProf branch instead of emitting an unresolvable nil.sub call.

Factor the program-wide nil-write scan into ivar_all_writes_nil(), shared by static_nil_ivar_cond and the new static_nil_reader_cond.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
5de531fb630c
Parent
2ea6e2a948f4

src/codegen_stmt.cmodifiedView file

@@ -643,13 +643,11 @@
643643 return 0;
644644 }
645645
646-/* An ivar read whose every program-wide write is nil is statically falsy
647- (`@mode = nil` and never reassigned -> `if @mode` never fires). Returns 0
648- for always-false, -1 otherwise. */
649-int static_nil_ivar_cond(Compiler *c, int pred) {
646+/* Scan every program-wide write to ivar `nm` ("@foo"): returns 0 when at least
647+ one write exists and all of them assign nil (statically falsy), -1 otherwise
648+ (no writes seen, a non-nil write, or an opaque write form). */
649+static int ivar_all_writes_nil(Compiler *c, const char *nm) {
650650 const NodeTable *nt = c->nt;
651- if (pred < 0 || !nt_type(nt, pred) || strcmp(nt_type(nt, pred), "InstanceVariableReadNode")) return -1;
652- const char *nm = nt_str(nt, pred, "name");
653651 if (!nm) return -1;
654652 int saw_write = 0;
655653 for (int id = 0; id < nt->count; id++) {
@@ -674,6 +672,46 @@
674672 return saw_write ? 0 : -1;
675673 }
676674
675+/* An ivar read whose every program-wide write is nil is statically falsy
676+ (`@mode = nil` and never reassigned -> `if @mode` never fires). Returns 0
677+ for always-false, -1 otherwise. */
678+int static_nil_ivar_cond(Compiler *c, int pred) {
679+ const NodeTable *nt = c->nt;
680+ if (pred < 0 || !nt_type(nt, pred) || strcmp(nt_type(nt, pred), "InstanceVariableReadNode")) return -1;
681+ return ivar_all_writes_nil(c, nt_str(nt, pred, "name"));
682+}
683+
684+/* `recv.reader` (no args, no block) where `reader` names an attr_reader-backed
685+ ivar @reader whose every program-wide write is nil is a falsy constant, so
686+ `if @conf.stackprof_mode` is statically dead. The receiver is restricted to a
687+ pure, object-typed form so dropping the branch skips no side effects and the
688+ call really is a getter. This resolves through the backing ivar rather than
689+ the inferred return type, which can mis-widen a nil-pinned ivar. Returns 0
690+ for always-false, -1 otherwise. */
691+int static_nil_reader_cond(Compiler *c, int pred) {
692+ const NodeTable *nt = c->nt;
693+ if (pred < 0 || !nt_type(nt, pred) || strcmp(nt_type(nt, pred), "CallNode")) return -1;
694+ const char *cn = nt_str(nt, pred, "name");
695+ if (!cn) return -1;
696+ int args = nt_ref(nt, pred, "arguments");
697+ if (args >= 0) {
698+ int ac = 0; nt_arr(nt, args, "arguments", &ac);
699+ if (ac != 0) return -1;
700+ }
701+ if (nt_ref(nt, pred, "block") >= 0) return -1;
702+ int recv = nt_ref(nt, pred, "receiver");
703+ if (recv < 0) return -1;
704+ const char *rty = nt_type(nt, recv);
705+ if (!rty) return -1;
706+ if (strcmp(rty, "InstanceVariableReadNode") &&
707+ strcmp(rty, "LocalVariableReadNode") &&
708+ strcmp(rty, "SelfNode")) return -1;
709+ if (!ty_is_object(comp_ntype(c, recv))) return -1; /* a real getter receiver */
710+ char nm[256];
711+ snprintf(nm, sizeof nm, "@%s", cn);
712+ return ivar_all_writes_nil(c, nm);
713+}
714+
677715 void emit_if(Compiler *c, int id, Buf *b, int indent, int is_unless, int tail) {
678716 const NodeTable *nt = c->nt;
679717 int pred = nt_ref(nt, id, "predicate");
@@ -684,6 +722,7 @@
684722 {
685723 int sc = static_isa_cond(c, pred);
686724 if (sc < 0) sc = static_nil_ivar_cond(c, pred);
725+ if (sc < 0) sc = static_nil_reader_cond(c, pred);
687726 int eff = (sc < 0) ? -1 : (is_unless ? !sc : sc);
688727 if (eff == 1) {
689728 /* condition always true: emit only the then-branch */