-
Notifications
You must be signed in to change notification settings - Fork 32
Tail Recursion Optimization #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
TODO:
|
…e variable name clash
compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala
Outdated
Show resolved
Hide resolved
OK, should be good to merge. There's just one trivial merge conflict in |
Great!
So... do you intend to ever fix these conflicts? 🤔 |
compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala
Outdated
Show resolved
Hide resolved
compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala
Outdated
Show resolved
Hide resolved
compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala
Outdated
Show resolved
Hide resolved
I figured doing it now will make the diff a bit harder to review. Once everything else is sorted, I'll fix the conflicts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing change, thanks! Should we merge now? (I'll do it.)
Go ahead |
Adds a pass to the IR which optimizes mutually tail-recursive and mutually tail-recursive modulo cons functions.
The full pass first optimizes modulo cons functions to be tail recursive, then optimizes those functions to be tail recursive.
This PR consists of the following features
IR Field Assignment
Adds an assignment primitive to the IR. Given
x: CtorApp
andval: TrvialExpr
, we now have the primitiveAlso modified the interpreter to no longer assume references to constructors are referentially transparent.
Tail Recursion Modulo Cons
Extends the idea in this paper to optimize strongly connected components of tail recursive functions, of which some calls may be within a constructor (guarded recursion).
Supports both modulo-cons calls and regular tail calls within the same strongly connected component.
Rewrite mutually tail recursive functions
Suppose we have functions:
where
e1
ande2
tail-callf
org
. This function will be rewritten a single function:where
e1'
ande2'
aree1
ande2
with their tail calls tof
andg
replaced with jumps toj
.In general, strongly connected components of mutually tail recursive functions will be optimized in this way. All join points used in each strongly connected component will be rewritten and merged into the same join point.