Commit 74498298

74498298f707Merged

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

Description

codegen: scope volatile to locals that actually cross a begin/rescue boundary

A scope containing any begin/rescue marked every local volatile. That is sound but pessimistic: a hot loop accumulator that merely sits in the same method as an unrelated begin was forced volatile, blocking register allocation. Per C99 7.13.2.1 only a local modified between the begin's setjmp and a longjmp (a raise, or a retry) and read afterward is indeterminate -- i.e. a local written inside the begin construct.

emit_scope_decls now volatile-qualifies a body local only when its name is written inside a BeginNode subtree of the scope. Soundness is preserved at both ends: a bare (method-level) RescueNode -- one not nested in any BeginNode, whose setjmp protects the whole body -- falls back to qualifying every local; and a local written inside the begin still gets volatile. Verified: an inline begin; g = ...; rescue; g = ...; end keeps g volatile while sibling loop locals s/i drop it; a method-level def ... rescue ... end keeps all locals volatile.

This is SETJMP.md priority 1 (narrow the volatile range) -- setjmp/longjmp is retained; only its happy-path tax shrinks.

optcarrot: 711.7 -> 750.4 fps (+5.4%, best of 5), checksum 59662. Gates: 950 tests pass, 57 benchmarks pass.

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

Details

Commit
74498298f707
Parent
4058359a8053

src/codegen.cmodifiedView file

@@ -197,10 +197,77 @@
197197 return 0;
198198 }
199199
200+/* Mark every node id in the subtree rooted at `id` (ref fields + array-field
201+ elements are a node's children). Used to find the lexical extent of a
202+ begin/rescue construct. */
203+static void mark_subtree(const NodeTable *nt, int id, char *inb) {
204+ if (id < 0 || id >= nt->count || inb[id]) return;
205+ inb[id] = 1;
206+ int nr = nt_num_refs(nt, id);
207+ for (int i = 0; i < nr; i++) mark_subtree(nt, nt_ref_at(nt, id, i), inb);
208+ int na = nt_num_arrs(nt, id);
209+ for (int i = 0; i < na; i++) {
210+ int n; const int *a = nt_arr_at(nt, id, i, &n);
211+ for (int j = 0; j < n; j++) mark_subtree(nt, a[j], inb);
212+ }
213+}
214+
215+static int lv_is_write_or_target(NodeKind k) {
216+ return k == NK_LocalVariableWriteNode || k == NK_LocalVariableOrWriteNode ||
217+ k == NK_LocalVariableAndWriteNode || k == NK_LocalVariableOperatorWriteNode ||
218+ k == NK_LocalVariableTargetNode;
219+}
220+
221+/* Which locals in scope `si` need `volatile`? A `begin` emits a setjmp at its
222+ entry; per C99 7.13.2.1 only a local modified between that setjmp and a
223+ longjmp (a raise, or a retry) and read afterward is indeterminate -- i.e. a
224+ local *written inside the begin construct*. Locals written only outside it
225+ keep their setjmp-time value and need no volatile (the broad whole-scope
226+ qualifier this replaces was sound but pessimized hot loops that merely sit in
227+ the same method as an unrelated begin).
228+
229+ Returns the list of such names via *out/*nout (names borrow the node table's
230+ storage; the array is the caller's to free). Sets *all = 1 when a bare
231+ (method-level) RescueNode -- one not nested in any BeginNode -- protects the
232+ whole body, in which case every local needs volatile and *out stays NULL. */
233+static void begin_volatile_names(Compiler *c, int si, char ***out, int *nout, int *all) {
234+ const NodeTable *nt = c->nt;
235+ *out = NULL; *nout = 0; *all = 0;
236+ char *inb = (char *)calloc((size_t)(nt->count > 0 ? nt->count : 1), 1);
237+ if (!inb) { *all = 1; return; } /* OOM: fall back to the conservative whole-scope rule */
238+ for (int id = 0; id < nt->count; id++)
239+ if (nt_kind(nt, id) == NK_BeginNode && c->nscope[id] == si) mark_subtree(nt, id, inb);
240+ for (int id = 0; id < nt->count; id++)
241+ if (nt_kind(nt, id) == NK_RescueNode && c->nscope[id] == si && !inb[id]) { *all = 1; break; }
242+ if (*all) { free(inb); return; }
243+ char **names = NULL; int n = 0, cap = 0;
244+ for (int id = 0; id < nt->count; id++) {
245+ if (!inb[id] || c->nscope[id] != si || !lv_is_write_or_target(nt_kind(nt, id))) continue;
246+ const char *nm = nt_str(nt, id, "name");
247+ if (!nm) continue;
248+ int dup = 0;
249+ for (int j = 0; j < n; j++) if (!strcmp(names[j], nm)) { dup = 1; break; }
250+ if (dup) continue;
251+ if (n == cap) { cap = cap ? cap * 2 : 8; names = (char **)realloc(names, sizeof(char *) * cap); }
252+ names[n++] = (char *)nm;
253+ }
254+ *out = names; *nout = n;
255+ free(inb);
256+}
257+
258+static int name_in(char **names, int n, const char *nm) {
259+ if (!nm) return 0;
260+ for (int i = 0; i < n; i++) if (!strcmp(names[i], nm)) return 1;
261+ return 0;
262+}
263+
200264 /* Declare a scope's locals. Params are already C function parameters, so
201265 they only need a GC root; body locals get a full declaration. */
202266 void emit_scope_decls(Compiler *c, Scope *s, Buf *b) {
203- int vol = scope_has_begin(c, (int)(s - c->scopes));
267+ int si = (int)(s - c->scopes);
268+ int has_begin = scope_has_begin(c, si);
269+ char **volnames = NULL; int nvol = 0, all_vol = 0;
270+ if (has_begin) begin_volatile_names(c, si, &volnames, &nvol, &all_vol);
204271 for (int i = 0; i < s->nlocals; i++) {
205272 LocalVar *lv = &s->locals[i];
206273 /* define_method subst var: replaced inline by the literal, never a C
@@ -244,9 +311,11 @@
244311 else if (needs_root(lv->type) && !comp_ty_value_obj(c, lv->type)) buf_printf(b, " SP_GC_ROOT(lv_%s);\n", lv->name);
245312 }
246313 else {
314+ int vol = has_begin && (all_vol || name_in(volnames, nvol, lv->name));
247315 declare_local(c, b, lv, vol);
248316 }
249317 }
318+ free(volnames);
250319 }
251320
252321 /* ---- methods ---- */