Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scala 3 inlining phase profile compiler plugin
// 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"
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))
}
}
pluginClass=example.InliningProfile
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