Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active August 29, 2022 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/234aea416ae7feb95f4b0e63f8b13748 to your computer and use it in GitHub Desktop.
Save xuwei-k/234aea416ae7feb95f4b0e63f8b13748 to your computer and use it in GitHub Desktop.
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