escape analysis for automatic stack allocation of local objects#109
Merged
escape analysis for automatic stack allocation of local objects#109
Conversation
- new src/semantic/escape-analysis.ts: conservative escape analysis that identifies const object literal variables that don't escape their function scope - object.ts: allocateStruct() checks isStackEligibleKey to emit alloca instead of GC_malloc - variable-allocator.ts: sets currentVarDeclKey before generating rhs of variable declarations - llvm-generator.ts: calls analyzeEscapes(ast) in generateParts(), stores stack-eligible var keys - generator-context.ts: adds isStackEligibleKey/getCurrentVarDeclKey/setCurrentVarDeclKey to interface - fixed: all statement/expression type assertions use real ast types to avoid wrong gep indices in native compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: escape analysis for automatic stack allocation of local objects
Adds a conservative escape analysis semantic pass that automatically stack-allocates
constobject literals that don't escape their function scope. Zero GC pressure, zero syntax changes required — fully automatic.What it does
Any
const obj = { ... }declared inside a function that doesn't escape (not returned, not passed to external calls, not stored in outer scope or class fields) gets emitted asallocainstead ofGC_malloc. Stack allocation is:Conservative by design: uncertain → heap (safe). False "escape" wastes nothing. False "no-escape" would be a dangling pointer — never happens.
A value escapes if it is
Implementation
src/semantic/escape-analysis.ts(new)analyzeEscapes(ast)runs before codegen. For each function and class method body:collectCandidates— findsconst x = { ... }declarations (object literal RHS only)scanBlockForEscapes— walks all statements/expressions marking which candidates escapestring[]of"name:line:col"keys for stack-eligible declarationsFull AST coverage: if/while/for/for-of/try-catch/switch all traversed. Arrow function bodies treated conservatively (any reference inside an arrow marks the var escaped).
All type assertions use real AST types from
src/ast/types.ts— no invented partial interfaces that would produce wrong GEP indices in the native compiler.src/codegen/types/objects/object.tsNew
allocateStruct(structType, sizeBytes)helper used by all three object generators (generateInlineObject,generateInterfaceObject,generateInlineInterfaceObject). Checksctx.isStackEligibleKey(ctx.getCurrentVarDeclKey())— if eligible, emitsallocawith auto-hoisting to the entry block; otherwise falls back toGC_mallocas before.src/codegen/infrastructure/variable-allocator.tsSets
ctx.setCurrentVarDeclKey("name:line:col")before generating each variable declaration's RHS, clears it after. This letsallocateStructknow which declaration is currently being generated.src/codegen/llvm-generator.ts/generator-context.tsanalyzeEscapes(ast)called ingenerateParts(), result stored asstackEligibleVars: string[]IGeneratorContext:setStackEligibleVars,isStackEligibleKey,setCurrentVarDeclKey,getCurrentVarDeclKeyCorrectness
npm run verify)const obj = {}in the compiler source pass as args or are returned — correctly identified as escaped)isNumberglobal variable handling was dropped fromllvm-generator.tsduring rebase onto main; restoredStartup benchmark (hello world)
clang -O2)Gap is Boehm GC init cost (one-time per process). For long-running servers the escape analysis impact is on per-request allocation, not startup.