Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 950bb26

Browse files
committed
Merge pull request scala#4552 from lrytz/opt/closureInlining
Closure elimination for new backend
2 parents 1b09e12 + 3e7776e commit 950bb26

17 files changed

+1268
-126
lines changed

src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ abstract class BCodeSkelBuilder extends BCodeHelpers {
140140
if (AsmUtils.traceClassEnabled && cnode.name.contains(AsmUtils.traceClassPattern))
141141
AsmUtils.traceClass(cnode)
142142

143-
if (settings.YoptInlinerEnabled) {
143+
if (settings.YoptAddToBytecodeRepository) {
144144
// The inliner needs to find all classes in the code repo, also those being compiled
145145
byteCodeRepository.add(cnode, ByteCodeRepository.CompilationUnit)
146146
}

src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ abstract class BTypes {
4444

4545
val inliner: Inliner[this.type]
4646

47+
val closureOptimizer: ClosureOptimizer[this.type]
48+
4749
val callGraph: CallGraph[this.type]
4850

4951
val backendReporting: BackendReporting

src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package scala.tools.nsc
77
package backend.jvm
88

99
import scala.tools.asm
10-
import scala.tools.nsc.backend.jvm.opt.{LocalOpt, CallGraph, Inliner, ByteCodeRepository}
10+
import scala.tools.nsc.backend.jvm.opt._
1111
import scala.tools.nsc.backend.jvm.BTypes.{InlineInfo, MethodInlineInfo, InternalName}
1212
import BackendReporting._
1313
import scala.tools.nsc.settings.ScalaSettings
@@ -42,6 +42,8 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes {
4242

4343
val inliner: Inliner[this.type] = new Inliner(this)
4444

45+
val closureOptimizer: ClosureOptimizer[this.type] = new ClosureOptimizer(this)
46+
4547
val callGraph: CallGraph[this.type] = new CallGraph(this)
4648

4749
val backendReporting: BackendReporting = new BackendReportingImpl(global)

src/compiler/scala/tools/nsc/backend/jvm/BackendReporting.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,28 @@ object BackendReporting {
246246
case class ResultingMethodTooLarge(calleeDeclarationClass: InternalName, name: String, descriptor: String,
247247
callsiteClass: InternalName, callsiteName: String, callsiteDesc: String) extends CannotInlineWarning
248248

249+
/**
250+
* Used in `rewriteClosureApplyInvocations` when a closure apply callsite cannot be rewritten
251+
* to the closure body method.
252+
*/
253+
trait RewriteClosureApplyToClosureBodyFailed extends OptimizerWarning {
254+
def pos: Position
255+
256+
override def emitWarning(settings: ScalaSettings): Boolean = this match {
257+
case RewriteClosureAccessCheckFailed(_, cause) => cause.emitWarning(settings)
258+
case RewriteClosureIllegalAccess(_, _) => settings.YoptWarningEmitAtInlineFailed
259+
}
260+
261+
override def toString: String = this match {
262+
case RewriteClosureAccessCheckFailed(_, cause) =>
263+
s"Failed to rewrite the closure invocation to its implementation method:\n" + cause
264+
case RewriteClosureIllegalAccess(_, callsiteClass) =>
265+
s"The closure body invocation cannot be rewritten because the target method is not accessible in class $callsiteClass."
266+
}
267+
}
268+
case class RewriteClosureAccessCheckFailed(pos: Position, cause: OptimizerWarning) extends RewriteClosureApplyToClosureBodyFailed
269+
case class RewriteClosureIllegalAccess(pos: Position, callsiteClass: InternalName) extends RewriteClosureApplyToClosureBodyFailed
270+
249271
/**
250272
* Used in the InlineInfo of a ClassBType, when some issue occurred obtaining the inline information.
251273
*/

src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,25 @@ abstract class GenBCode extends BCodeSyncAndTry {
216216
class Worker2 {
217217
def runGlobalOptimizations(): Unit = {
218218
import scala.collection.convert.decorateAsScala._
219-
q2.asScala foreach {
220-
case Item2(_, _, plain, _, _) =>
221-
// skip mirror / bean: wd don't inline into tem, and they are not used in the plain class
222-
if (plain != null) callGraph.addClass(plain)
219+
if (settings.YoptBuildCallGraph) {
220+
q2.asScala foreach {
221+
case Item2(_, _, plain, _, _) =>
222+
// skip mirror / bean: wd don't inline into tem, and they are not used in the plain class
223+
if (plain != null) callGraph.addClass(plain)
224+
}
223225
}
224-
bTypes.inliner.runInliner()
226+
if (settings.YoptInlinerEnabled)
227+
bTypes.inliner.runInliner()
228+
if (settings.YoptClosureElimination)
229+
closureOptimizer.rewriteClosureApplyInvocations()
225230
}
226231

227232
def localOptimizations(classNode: ClassNode): Unit = {
228233
BackendStats.timed(BackendStats.methodOptTimer)(localOpt.methodOptimizations(classNode))
229234
}
230235

231236
def run() {
232-
if (settings.YoptInlinerEnabled) runGlobalOptimizations()
237+
runGlobalOptimizations()
233238

234239
while (true) {
235240
val item = q2.poll

src/compiler/scala/tools/nsc/backend/jvm/analysis/InstructionStackEffect.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ object InstructionStackEffect {
9494
val isSize2 = peekStack(0).getSize == 2
9595
if (isSize2) t(1, 0) else t(2, 0)
9696

97-
case DUP => t(0, 1)
97+
case DUP => t(1, 2)
9898

9999
case DUP_X1 => t(2, 3)
100100

@@ -104,7 +104,7 @@ object InstructionStackEffect {
104104

105105
case DUP2 =>
106106
val isSize2 = peekStack(0).getSize == 2
107-
if (isSize2) t(0, 1) else t(0, 2)
107+
if (isSize2) t(1, 2) else t(2, 4)
108108

109109
case DUP2_X1 =>
110110
val isSize2 = peekStack(0).getSize == 2

0 commit comments

Comments
 (0)