-
Notifications
You must be signed in to change notification settings - Fork 25
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
Tail Recursion Optimization #218
base: mlscript
Are you sure you want to change the base?
Conversation
TODO:
|
…e variable name clash
compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala
Outdated
Show resolved
Hide resolved
@@ -66,37 +77,57 @@ class TailRecOpt(fnUid: FreshInt, classUid: FreshInt, tag: FreshInt): | |||
if names.contains(nme) then Some(nme) | |||
else None | |||
|
|||
// would prefer to have this inside discoverOptimizableCalls, but this makes scala think it's not tail recursive | |||
// would prefer to have this inside discoverOptCalls, but scala does not support partially tail recursive functions directly |
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.
I think you could just add private val discoverOptCallsIndirect = discoverOptCalls
and call that instead.
Instead of writing |
// Tail calls to another function in the component will be replaced with a tail call | ||
// to the merged function | ||
def transformDefn(defn: Defn): Defn = | ||
// TODO: Figure out how to substitute variables with dummy variables. |
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.
This should be addressed or explained better.
Steps before this can be an official PR:
|
Completed:
TODO:
After this it should be good to merge. |
compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala
Outdated
Show resolved
Hide resolved
resolved |
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.