Skip to content

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 2 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Compiler {
new ElimPackagePrefixes, // Eliminate references to package prefixes in Select nodes
new CookComments, // Cook the comments: expand variables, doc, etc.
new CheckStatic, // Check restrictions that apply to @static members
new BetaReduce, // Reduce closure applications
new init.Checker) :: // Check initialization of objects
List(new CompleteJavaEnums, // Fill in constructors for Java enums
new ElimRepeated, // Rewrite vararg parameters and arguments
Expand Down
63 changes: 63 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/BetaReduce.scala
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
36 changes: 36 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,40 @@ class InlineBytecodeTests extends DottyBytecodeTest {

}
}

@Test def i6375 = {
val source = """class Test:
| given Int = 0
| def f(): Int ?=> Boolean = true : (Int ?=> Boolean)
| inline def g(): Int ?=> Boolean = true
| def test = g()
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Test.class", directory = false).input
val clsNode = loadClassNode(clsIn)

val fun = getMethod(clsNode, "test")
val instructions = instructionsFromMethod(fun)
val expected =
List(
// Head tested separatly
VarOp(ALOAD, 0),
Invoke(INVOKEVIRTUAL, "Test", "given_Int", "()I", false),
Invoke(INVOKESTATIC, "scala/runtime/BoxesRunTime", "boxToInteger", "(I)Ljava/lang/Integer;", false),
Invoke(INVOKEINTERFACE, "scala/Function1", "apply", "(Ljava/lang/Object;)Ljava/lang/Object;", true),
Invoke(INVOKESTATIC, "scala/runtime/BoxesRunTime", "unboxToBoolean", "(Ljava/lang/Object;)Z", false),
Op(IRETURN)
)

instructions.head match {
case InvokeDynamic(INVOKEDYNAMIC, "apply$mcZI$sp", "()Ldotty/runtime/function/JFunction1$mcZI$sp;", _, _) =>
case _ => assert(false, "`g` was not properly inlined in `test`\n")
}

assert(instructions.tail == expected,
"`fg was not properly inlined in `test`\n" + diffInstructions(instructions, expected))

}
}
}
38 changes: 38 additions & 0 deletions tests/pos/i6375.scala
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

* 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()