Commit b67cc757

b67cc75789c2Merged

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

Description

feat(codegen): stack-allocate escape-safe int_array locals

Adds a small escape analysis pass to compile_codegen.rb: for each local typed int_array whose write pattern (single assignment of [], plus reads on the local via built-in IntArray operations or the whitelisted push_child_ids accumulator pattern) can't possibly let the pointer outlive the method body, the codegen emits the local as a stack-allocated sp_IntArray instead of going through sp_IntArray_new + sp_gc_alloc.

Three pieces:

  • sp_runtime.h: sp_IntArray gains an is_stack field. sp_IntArray_ push_grow's GC-bookkeeping arm (h->size / sp_gc_bytes updates, which assume the struct is preceded by an sp_gc_hdr) is skipped when is_stack is set. A new helper sp_IntArray_stack_init takes a wrapper struct (8 bytes of padding + the arr field), seeds the padding's last byte with 0xff so sp_gc_mark treats it as an immutable static object should a callee SP_GC_ROOT(param) land on it, and initializes the arr. sp_IntArray_stack_fin frees the data buffer on scope exit via the cleanup attribute.

  • spinel_codegen.rb: new helpers detect_stack_alloc_int_arrays / int_array_local_stackable_in / local_int_array_escapes walk the method body, classify each LocalVariableReadNode of the candidate local by its parent context (CallNode receiver, arg position, ReturnNode, ivar/cvar/gvar write, BlockNode, etc.), and reject anything outside the conservative safe set. The declare_method_locals prologue emit branches on the resulting @stack_local_set: safe locals get the sp_IntArray_stack_wrap _stk_<name> + stack_init pattern, no SP_GC_ROOT registration. The body's lname = [] write is suppressed for safe locals so the prologue's init isn't clobbered.

  • Block bodies, return positions, ivar/cvar/gvar stores, and calls to user methods other than push_child_ids fall outside the safe set. Multi-write locals are also rejected (no detection of re-init patterns yet). These restrictions can be relaxed in follow-up commits.

Self-host triggers 45 stack-alloc sites (18 in codegen.rb, 27 in analyze.rb), notably scan_for_argv / scan_for_class_hierarchy_usage / collect_param_method_calls's accumulator arrays. Bootstrap fixpoint still converges, all 400 tests pass, bench 52/0/0, optcarrot checksum 59662 unchanged.

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

Details

Commit
b67cc75789c2
Parent
0a53f1980069

lib/sp_runtime.hmodifiedView file

@@ -369,9 +369,42 @@
369369 typedef struct sp_Object_s { uint8_t _pad; } sp_Object;
370370 static sp_Object *sp_Object_new(void){return(sp_Object*)sp_gc_alloc(sizeof(sp_Object),NULL,NULL);}
371371
372-typedef struct{mrb_int*data;mrb_int start;mrb_int len;mrb_int cap;}sp_IntArray;
372+/* `is_stack` is 1 when the struct itself lives on the C stack (escape-
373+ * analysis-driven stack allocation in the codegen). Grow / replace
374+ * helpers consult this flag to skip the sp_gc_hdr / sp_gc_bytes
375+ * bookkeeping that's only valid for sp_gc_alloc'd structs. */
376+typedef struct{mrb_int*data;mrb_int start;mrb_int len;mrb_int cap;mrb_int is_stack;}sp_IntArray;
373377 static void sp_IntArray_fin(void*p){free(((sp_IntArray*)p)->data);}
374-static sp_IntArray*sp_IntArray_new(void){sp_IntArray*a=(sp_IntArray*)sp_gc_alloc(sizeof(sp_IntArray),sp_IntArray_fin,NULL);a->cap=16;a->data=(mrb_int*)malloc(sizeof(mrb_int)*a->cap);a->start=0;a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}return a;}
378+static sp_IntArray*sp_IntArray_new(void){sp_IntArray*a=(sp_IntArray*)sp_gc_alloc(sizeof(sp_IntArray),sp_IntArray_fin,NULL);a->cap=16;a->data=(mrb_int*)malloc(sizeof(mrb_int)*a->cap);a->start=0;a->len=0;a->is_stack=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}return a;}
379+/* Stack-allocated sp_IntArray. The struct lives on the caller's stack
380+ * frame, the backing data buffer is still heap-malloc'd (with the same
381+ * realloc-on-grow path as sp_IntArray_new). Used by codegen for locals
382+ * that escape analysis proves don't outlive the method body — skips
383+ * the sp_gc_alloc that would otherwise track the struct in the GC
384+ * heap.
385+ *
386+ * Wrapper struct: a callee that receives this pointer as a parameter
387+ * registers it as a GC root via SP_GC_ROOT(param). When a later
388+ * allocation triggers gc_collect, sp_gc_mark dereferences the root
389+ * and reads `obj[-1]` (the static-string skip marker). For a stack-
390+ * allocated struct that byte is arbitrary stack memory — undefined
391+ * behaviour. The wrapper places 8 bytes of padding before the arr
392+ * field with the last byte set to 0xff (sp_gc_mark's "immutable,
393+ * skip" marker), so the GC marks it as a no-op static object. */
394+typedef struct { unsigned char _pad[8]; sp_IntArray arr; } sp_IntArray_stack_wrap;
395+static sp_IntArray *sp_IntArray_stack_init(sp_IntArray_stack_wrap *w) {
396+ w->_pad[7] = 0xff;
397+ w->arr.cap = 16;
398+ w->arr.data = (mrb_int *)malloc(sizeof(mrb_int) * w->arr.cap);
399+ w->arr.start = 0;
400+ w->arr.len = 0;
401+ w->arr.is_stack = 1;
402+ return &w->arr;
403+}
404+static void sp_IntArray_stack_fin(void *p) {
405+ sp_IntArray_stack_wrap *w = (sp_IntArray_stack_wrap *)p;
406+ if (w->arr.data) { free(w->arr.data); w->arr.data = NULL; }
407+}
375408 static sp_IntArray*sp_IntArray_from_range(mrb_int s,mrb_int e){sp_IntArray*a=sp_IntArray_new();mrb_int n=e-s+1;if(n<0)n=0;if(n>a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*a->cap;h->size-=sizeof(mrb_int)*a->cap;a->cap=n;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}for(mrb_int i=0;i<n;i++)a->data[i]=s+i;a->len=n;return a;}
376409 static sp_IntArray*sp_IntArray_dup(sp_IntArray*a){sp_IntArray*b=sp_IntArray_new();if(a->len>b->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)b-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*b->cap;h->size-=sizeof(mrb_int)*b->cap;b->cap=a->len;b->data=(mrb_int*)realloc(b->data,sizeof(mrb_int)*b->cap);h->size+=sizeof(mrb_int)*b->cap;sp_gc_bytes+=sizeof(mrb_int)*b->cap;}memcpy(b->data,a->data+a->start,sizeof(mrb_int)*a->len);b->len=a->len;return b;}
377410 /* a[start, len] / a[start..end] for IntArray. Negative start counts from
@@ -381,7 +414,7 @@
381414 * collection has no nullable form. */
382415 static sp_IntArray*sp_IntArray_slice(sp_IntArray*a,mrb_int start,mrb_int len){if(start<0)start+=a->len;if(start<0)start=0;sp_IntArray*b=sp_IntArray_new();if(start>=a->len||len<=0)return b;if(start+len>a->len)len=a->len-start;if(len>b->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)b-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*b->cap;h->size-=sizeof(mrb_int)*b->cap;b->cap=len;b->data=(mrb_int*)realloc(b->data,sizeof(mrb_int)*b->cap);h->size+=sizeof(mrb_int)*b->cap;sp_gc_bytes+=sizeof(mrb_int)*b->cap;}memcpy(b->data,a->data+a->start+start,sizeof(mrb_int)*len);b->len=len;return b;}
383416 static void sp_IntArray_replace(sp_IntArray*dst,sp_IntArray*src){dst->len=0;dst->start=0;if(src->len>dst->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)dst-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*dst->cap;h->size-=sizeof(mrb_int)*dst->cap;void*nd=realloc(dst->data,sizeof(mrb_int)*src->len);if(!nd){perror("realloc");exit(1);}dst->data=(mrb_int*)nd;dst->cap=src->len;h->size+=sizeof(mrb_int)*dst->cap;sp_gc_bytes+=sizeof(mrb_int)*dst->cap;}memcpy(dst->data,src->data+src->start,sizeof(mrb_int)*src->len);dst->len=src->len;}
384-static void __attribute__((noinline)) sp_IntArray_push_grow(sp_IntArray*a){if(a->start>0){memmove(a->data,a->data+a->start,sizeof(mrb_int)*a->len);a->start=0;if(a->len<a->cap)return;}{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*a->cap;h->size-=sizeof(mrb_int)*a->cap;a->cap=a->cap*2+1;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}}
417+static void __attribute__((noinline)) sp_IntArray_push_grow(sp_IntArray*a){if(a->start>0){memmove(a->data,a->data+a->start,sizeof(mrb_int)*a->len);a->start=0;if(a->len<a->cap)return;}if(a->is_stack){a->cap=a->cap*2+1;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);return;}{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*a->cap;h->size-=sizeof(mrb_int)*a->cap;a->cap=a->cap*2+1;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}}
385418 static inline void sp_IntArray_push(sp_IntArray*a,mrb_int v){if(a->start+a->len>=a->cap)sp_IntArray_push_grow(a);a->data[a->start+a->len]=v;a->len++;}
386419 static inline mrb_int sp_IntArray_pop(sp_IntArray*a){return a->data[a->start+--a->len];}
387420 static inline mrb_int sp_IntArray_shift(sp_IntArray*a){mrb_int v=a->data[a->start];a->start++;a->len--;return v;}