-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Redefine quoted.Expr.betaReduce #9469
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
+21 −21 | src/main/scala/cps/CpsExpr.scala | |
+8 −9 | src/main/scala/cps/forest/InlinedTransform.scala |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,22 +37,26 @@ class BetaReduce extends MiniPhase: | |
|
||
override def transformApply(app: Apply)(using Context): Tree = app.fun match | ||
case Select(fn, nme.apply) if defn.isFunctionType(fn.tpe) => | ||
val app1 = betaReduce(app, fn, app.args) | ||
val app1 = BetaReduce(app, fn, app.args) | ||
if app1 ne app then report.log(i"beta reduce $app -> $app1") | ||
app1 | ||
case _ => | ||
app | ||
|
||
private def betaReduce(tree: Apply, fn: Tree, args: List[Tree])(using 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) => BetaReduce(anonFun, args) | ||
case _ => tree | ||
|
||
object BetaReduce: | ||
import ast.tpd._ | ||
|
||
/** Beta-reduces a call to `fn` with arguments `argSyms` or returns `tree` */ | ||
def apply(tree: Apply, fn: Tree, args: List[Tree])(using Context): Tree = | ||
fn match | ||
case Typed(expr, _) => BetaReduce(tree, expr, args) | ||
case Block(Nil, expr) => BetaReduce(tree, expr, args) | ||
case Inlined(_, Nil, expr) => BetaReduce(tree, expr, args) | ||
case Block((anonFun: DefDef) :: Nil, closure: Closure) => BetaReduce(anonFun, args) | ||
case _ => tree | ||
end apply | ||
|
||
/** Beta-reduces a call to `ddef` with arguments `argSyms` */ | ||
def apply(ddef: DefDef, args: List[Tree])(using Context) = | ||
val bindings = List.newBuilder[ValDef] | ||
|
@@ -65,7 +69,8 @@ object BetaReduce: | |
ref.symbol | ||
case _ => | ||
val flags = Synthetic | (param.symbol.flags & Erased) | ||
val binding = ValDef(newSymbol(ctx.owner, param.name, flags, arg.tpe.widen, coord = arg.span), arg) | ||
val tpe = if arg.tpe.dealias.isInstanceOf[ConstantType] then arg.tpe.dealias else arg.tpe.widen | ||
val binding = ValDef(newSymbol(ctx.owner, param.name, flags, tpe, coord = arg.span), arg).withSpan(arg.span) | ||
bindings += binding | ||
binding.symbol | ||
|
||
|
@@ -76,5 +81,13 @@ object BetaReduce: | |
substTo = argSyms | ||
).transform(ddef.rhs) | ||
|
||
seq(bindings.result(), expansion) | ||
val expansion1 = new TreeMap { | ||
override def transform(tree: Tree)(using Context) = tree.tpe.widenTermRefExpr match | ||
case ConstantType(const) if isPureExpr(tree) => cpy.Literal(tree)(const) | ||
case _ => super.transform(tree) | ||
}.transform(expansion) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is constant folding -- can we reuse the logic in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not exactly constant folding. It is just the propagation of constants. After this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It fails. It also looks like overkill. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. Maybe as an optimization for later, beta-reduce & inlining may create new const-folding opportunities. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed |
||
val bindings1 = | ||
bindings.result().filterNot(vdef => vdef.tpt.tpe.isInstanceOf[ConstantType] && isPureExpr(vdef.rhs)) | ||
|
||
seq(bindings1, expansion1) | ||
end apply |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,4 +74,3 @@ object Test { | |
println(s"run-time: ${Macros.betaReduce(dummy7)(8)}") | ||
} | ||
} | ||
|
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.
It's unclear to me why we cannot just use
arg.tpe.widen
as the type of the binding?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.
It is to allow TermRef to definitions with constant types to be constant folded after this transformation.