Last active
August 29, 2022 14:25
-
-
Save xuwei-k/234aea416ae7feb95f4b0e63f8b13748 to your computer and use it in GitHub Desktop.
Scala 3 inlining phase profile compiler plugin
This file contains 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
// NIGHTLY require for ResearchPlugin | |
// https://github.com/lampepfl/dotty/blob/ed9267e8e1af6472db2b9164d92668de5b61376f/compiler/src/dotty/tools/dotc/plugins/Plugins.scala#L129-L133 | |
scalaVersion := "3.1.3-RC1-bin-20220328-ed9267e-NIGHTLY" | |
organization := "com.github.xuwei-k" | |
name := "inlining-profile" | |
libraryDependencies += "org.scala-lang" %% "scala3-compiler" % scalaVersion.value | |
version := "0.1.0-SNAPSHOT" |
This file contains 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
package example | |
import dotty.tools.dotc.CompilationUnit | |
import dotty.tools.dotc.core.Contexts.Context | |
import dotty.tools.dotc.core.Phases.Phase | |
import dotty.tools.dotc.plugins.ResearchPlugin | |
import dotty.tools.dotc.transform.Inlining | |
class InliningProfile extends ResearchPlugin { | |
override val name: String = "my-inlining" | |
override val description: String = "my inlining plugin" | |
def init(options: List[String], phasesList: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] = { | |
phasesList.map { | |
_.map { | |
case phase if phase.phaseName == "inlining" => | |
println("replace inlining phase") | |
new MyInliningPhase() | |
case other => | |
other | |
} | |
} | |
} | |
} | |
class MyInliningPhase extends Inlining { | |
private[this] val compileTimeResults = scala.collection.concurrent.TrieMap.empty[String, Long] | |
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] = { | |
println("start inlining") | |
val result = super.runOn(units) | |
println("end inlining") | |
compileTimeResults.toList.sortBy(-_._2).take(50).foreach(println) | |
result | |
} | |
override def run(using c: Context): Unit = { | |
val start = System.currentTimeMillis() | |
super.run | |
val end = System.currentTimeMillis() | |
compileTimeResults += (c.compilationUnit.source.path -> (end - start)) | |
} | |
} |
This file contains 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
pluginClass=example.InliningProfile |
This file contains 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
scalaVersion := "3.1.3-RC1-bin-20220328-ed9267e-NIGHTLY" | |
addCompilerPlugin("com.github.xuwei-k" %% "inlining-profile" % "0.1.0-SNAPSHOT") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment