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_stackfield. 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'slname = []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
typedef struct sp_Object_s { uint8_t _pad; } sp_Object;static sp_Object *sp_Object_new(void){return(sp_Object*)sp_gc_alloc(sizeof(sp_Object),NULL,NULL);}typedef struct{mrb_int*data;mrb_int start;mrb_int len;mrb_int cap;}sp_IntArray;/* `is_stack` is 1 when the struct itself lives on the C stack (escape- * analysis-driven stack allocation in the codegen). Grow / replace * helpers consult this flag to skip the sp_gc_hdr / sp_gc_bytes * bookkeeping that's only valid for sp_gc_alloc'd structs. */typedef struct{mrb_int*data;mrb_int start;mrb_int len;mrb_int cap;mrb_int is_stack;}sp_IntArray;static void sp_IntArray_fin(void*p){free(((sp_IntArray*)p)->data);}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;}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;}/* Stack-allocated sp_IntArray. The struct lives on the caller's stack * frame, the backing data buffer is still heap-malloc'd (with the same * realloc-on-grow path as sp_IntArray_new). Used by codegen for locals * that escape analysis proves don't outlive the method body — skips * the sp_gc_alloc that would otherwise track the struct in the GC * heap. * * Wrapper struct: a callee that receives this pointer as a parameter * registers it as a GC root via SP_GC_ROOT(param). When a later * allocation triggers gc_collect, sp_gc_mark dereferences the root * and reads `obj[-1]` (the static-string skip marker). For a stack- * allocated struct that byte is arbitrary stack memory — undefined * behaviour. The wrapper places 8 bytes of padding before the arr * field with the last byte set to 0xff (sp_gc_mark's "immutable, * skip" marker), so the GC marks it as a no-op static object. */typedef struct { unsigned char _pad[8]; sp_IntArray arr; } sp_IntArray_stack_wrap;static sp_IntArray *sp_IntArray_stack_init(sp_IntArray_stack_wrap *w) { w->_pad[7] = 0xff; w->arr.cap = 16; w->arr.data = (mrb_int *)malloc(sizeof(mrb_int) * w->arr.cap); w->arr.start = 0; w->arr.len = 0; w->arr.is_stack = 1; return &w->arr;}static void sp_IntArray_stack_fin(void *p) { sp_IntArray_stack_wrap *w = (sp_IntArray_stack_wrap *)p; if (w->arr.data) { free(w->arr.data); w->arr.data = NULL; }}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;}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;}/* a[start, len] / a[start..end] for IntArray. Negative start counts from * collection has no nullable form. */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;}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;}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;}}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;}}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++;}static inline mrb_int sp_IntArray_pop(sp_IntArray*a){return a->data[a->start+--a->len];}static inline mrb_int sp_IntArray_shift(sp_IntArray*a){mrb_int v=a->data[a->start];a->start++;a->len--;return v;}spinel_codegen.rbmodifiedView file
Loading diff…
test/int_array_stack_alloc.rbaddedView file
Loading diff…
test/int_array_stack_alloc.rb.expectedaddedView file
Loading diff…