-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #6375: Add miniphase for simple beta reductions #8452
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package dotty.tools | ||
package dotc | ||
package transform | ||
|
||
import core._ | ||
import MegaPhase._ | ||
import Symbols._, Contexts._, Types._, Decorators._ | ||
import StdNames.nme | ||
import ast.Trees._ | ||
import ast.TreeTypeMap | ||
|
||
/** Rewrite an application | ||
* | ||
* (((x1, ..., xn) => b): T)(y1, ..., yn) | ||
* | ||
* where | ||
* | ||
* - all yi are pure references without a prefix | ||
* - the closure can also be contextual or erased, but cannot be a SAM type | ||
* _ the type ascription ...: T is optional | ||
* | ||
* to | ||
* | ||
* [xi := yi]b | ||
* | ||
* This is more limited than beta reduction in inlining since it only works for simple variables `yi`. | ||
* It is more general since it also works for type-ascripted closures. | ||
* | ||
* A typical use case is eliminating redundant closures for blackbox macros that | ||
* return context functions. See i6375.scala. | ||
*/ | ||
class BetaReduce extends MiniPhase: | ||
import ast.tpd._ | ||
|
||
def phaseName: String = "betaReduce" | ||
|
||
override def transformApply(app: Apply)(using ctx: Context): Tree = app.fun match | ||
case Select(fn, nme.apply) if defn.isFunctionType(fn.tpe) => | ||
val app1 = betaReduce(app, fn, app.args) | ||
if app1 ne app then ctx.log(i"beta reduce $app -> $app1") | ||
app1 | ||
case _ => | ||
app | ||
|
||
private def betaReduce(tree: Apply, fn: Tree, args: List[Tree])(using ctx: Context): Tree = | ||
fn match | ||
case Typed(expr, _) => betaReduce(tree, expr, args) | ||
case Block(Nil, expr) => betaReduce(tree, expr, args) | ||
case Block((anonFun: DefDef) :: Nil, closure: Closure) => | ||
val argSyms = | ||
for arg <- args yield | ||
arg.tpe.dealias match | ||
case ref @ TermRef(NoPrefix, _) if isPurePath(arg) => ref.symbol | ||
case _ => NoSymbol | ||
val vparams = anonFun.vparamss.head | ||
if argSyms.forall(_.exists) && argSyms.hasSameLengthAs(vparams) then | ||
TreeTypeMap( | ||
oldOwners = anonFun.symbol :: Nil, | ||
newOwners = ctx.owner :: Nil, | ||
substFrom = vparams.map(_.symbol), | ||
substTo = argSyms).transform(anonFun.rhs) | ||
else tree | ||
case _ => tree |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* In the following we should never have two nested closures after phase betaReduce | ||
* The output of the program should instead look like this: | ||
|
||
package <empty> { | ||
@scala.annotation.internal.SourceFile("i6375.scala") class Test() extends | ||
Object | ||
() { | ||
final given def given_Int: Int = 0 | ||
@scala.annotation.internal.ContextResultCount(1) def f(): (Int) ?=> Boolean | ||
= | ||
{ | ||
def $anonfun(using evidence$1: Int): Boolean = true | ||
closure($anonfun) | ||
} | ||
@scala.annotation.internal.ContextResultCount(1) inline def g(): | ||
(Int) ?=> Boolean | ||
= | ||
{ | ||
def $anonfun(using evidence$3: Int): Boolean = true | ||
closure($anonfun) | ||
} | ||
{ | ||
{ | ||
def $anonfun(using evidence$3: Int): Boolean = true | ||
closure($anonfun) | ||
} | ||
}.apply(this.given_Int) | ||
} | ||
} | ||
*/ | ||
class Test: | ||
given Int = 0 | ||
|
||
def f(): Int ?=> Boolean = true : (Int ?=> Boolean) | ||
|
||
inline def g(): Int ?=> Boolean = true | ||
g() | ||
|
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.
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.
We need a regression test for this in https://github.com/lampepfl/dotty/blob/master/compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala
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.
Yes, but I won't be the one adding it. Who can take care of this?
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.
Done.
@odersky could you double-check the bytecode to see it looks like what you expected.