Skip to content

Add trampoline pass for stack-safe (mutual) TRMC#900

Open
TimWhiting wants to merge 1 commit into
koka-lang:dev-uvfrom
TimWhiting:stack-usage
Open

Add trampoline pass for stack-safe (mutual) TRMC#900
TimWhiting wants to merge 1 commit into
koka-lang:dev-uvfrom
TimWhiting:stack-usage

Conversation

@TimWhiting

Copy link
Copy Markdown
Collaborator

Implements Core/Trampoline.hs: converts tail-recursive and mutually tail-recursive function SCCs -- including recursion modulo cons (e.g. zip-even/zip-odd) -- into a stack-safe form. SCC members are fused directly into a single self-recursive @run driver over a generated Bounce value type (one @Call-* constructor per member; tail and modulo-cons calls are redirected in place to @run(@Call-*(args)), preserving surrounding constructor structure), with thin per-member wrappers fi(args) = @run(@Call-*(args)). This collapses N-way mutual recursion into 1-way self-recursion and routes the result through CTail's existing TRMC machinery for free -- trampoline now runs before ctail in the optimization pipeline so the fused @run becomes a context/hole-passing loop with no stack growth for reference-typed results.

Adds the optTrampoline flag (enabled by default), wires the pass into coreOptimize, and adds tests.

…rsion

Implements Core/Trampoline.hs: converts tail-recursive and mutually
tail-recursive function SCCs -- including recursion modulo cons (e.g.
zip-even/zip-odd) -- into a stack-safe form. SCC members are fused
directly into a single self-recursive @run driver over a generated
Bounce value type (one @Call-* constructor per member; tail and
modulo-cons calls are redirected in place to @run(@Call-*(args)),
preserving surrounding constructor structure), with thin per-member
wrappers `fi(args) = @run(@Call-*(args))`. This collapses N-way mutual
recursion into 1-way self-recursion and routes the result through
CTail's existing TRMC machinery for free -- trampoline now runs before
ctail in the optimization pipeline so the fused @run becomes a
context/hole-passing loop with no stack growth for reference-typed
results.

Adds the optTrampoline flag (enabled by default), wires the pass into
coreOptimize, and adds tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant