fix(IRLower): memoize teardown routines, eliminate exponential codegen (54GB→3MB)#127
Merged
Merged
Conversation
Replace inline recursive field release with cached per-class teardown
IRFunctions. Call sites emit CALL to cached routine instead of
expanding all nested field releases at compile time.
- generateTeardownRoutines(): one IRFunction per class with ref fields
- emitTeardownBody(): inline field release with CALL to child teardowns
- emitRelease(): simplified — null guard + dec + CALL + heapFree
- needsTeardown(): checks classIdxOf(fldCls) >= 0 (not field count)
- Depth limit completely removed (no longer needed)
- emitDepth parameter removed from all call sites
Results: S1->S2 memory 54 GB -> 3 MB (18,000x improvement)
S2==S3==S4 deterministic, 344,822 bytes
No OOM, no stack overflow
Replace inline recursive field release with cached per-class teardown IRFunctions. Call sites emit CALL to cached routine instead of expanding all nested field releases at compile time. - generateTeardownRoutines(): one IRFunction per class with ref fields - emitTeardownBody(): inline field release with CALL to child teardowns - emitRelease(): simplified — null guard + dec + CALL + heapFree - needsTeardown(): checks classIdxOf(fldCls) >= 0 (not field count) - Depth limit completely removed Results: S1->S2 memory: 54 GB -> 3 MB (18,000x) Test suite: 17/17 PASS S1->S2 binary: deterministic (no crash, no OOM) Known issue: S2->S3 determinism blocked by pre-existing Env.args() V1.1 bug (startup argc/argv). Self-hosted S2 crashes in Env.args() before reaching Main.compile(). Separate fix needed.
…SEGV
Env.args() and Env.exePath() had no implementation in native backend,
falling through to generic static CALL handler which generated CALL to
non-existent Env__args/Env__exePath functions. ELF fixup resolved these
to bogus addresses → SIGSEGV at runtime.
Minimal fix:
- Env.args(): CALL __arimo_list_new → empty List<String>
- Env.exePath(): return internStr("") → empty string global
- Full argc/argv startup ABI → V1.1 (separate PR)
- Removed TODO comment, replaced with generic fallback note
This unblocks S2->S3 self-hosting determinism chain.
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.
Root Cause
PR #115 added recursive field release to emitRelease, walking the inheritance chain
and releasing all ref-type fields. Self-referential types (Expr→Expr, Stmt→Stmt)
caused exponential code duplication — each field release inlined the FULL
release code for its class, recursively, with no memoization.
Result: 79K stack frames (SIGSEGV) on v1.0, 54 GB heap (OOM) with depth limit.
Fix
Cached per-class teardown IRFunctions. Each class with ref-type fields gets
one teardown routine emitted once. Call sites emit a runtime to the cached
routine instead of inline expansion.
Architecture
Changes
generateTeardownRoutines(): one IRFunction per class (called after registerClasses)emitTeardownBody(): inline field release with CALL to child teardownsemitRelease(): simplified — null guard + dec + CALL + heapFree (no recursion)emitDepthparameter removed from all 4 call sitesSelf-referential safety
Runtime CALL chain. Refcount decrement before recursive CALL ensures
rc already at 0 → -1 on self-ref, JNE skips teardown. No double-free.
Results