Use PhaseConfigurationService in CompilerPhase instead of PhaseConfig

This commit is contained in:
Sergey Bogolepov
2022-10-03 12:14:47 +03:00
committed by Space Team
parent 06182fe547
commit 54deba63a1
10 changed files with 38 additions and 42 deletions
@@ -28,7 +28,7 @@ inline fun <R, D> PhaserState<D>.downlevel(nlevels: Int, block: () -> R): R {
}
interface CompilerPhase<in Context : CommonBackendContext, Input, Output> {
fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Input>, context: Context, input: Input): Output
fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Input>, context: Context, input: Input): Output
fun getNamedSubphases(startDepth: Int = 0): List<Pair<Int, NamedCompilerPhase<Context, *>>> = emptyList()
@@ -52,7 +52,7 @@ typealias AnyNamedPhase = NamedCompilerPhase<*, *>
enum class BeforeOrAfter { BEFORE, AFTER }
data class ActionState(
val config: PhaseConfig,
val config: PhaseConfigurationService,
val phase: AnyNamedPhase,
val phaseCount: Int,
val beforeOrAfter: BeforeOrAfter
@@ -77,8 +77,8 @@ class NamedCompilerPhase<in Context : CommonBackendContext, Data>(
private val actions: Set<Action<Data, Context>> = emptySet(),
private val nlevels: Int = 0
) : SameTypeCompilerPhase<Context, Data> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Data>, context: Context, input: Data): Data {
if (this !in phaseConfig.enabled) {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Data>, context: Context, input: Data): Data {
if (!phaseConfig.isEnabled(this)) {
return input
}
@@ -86,7 +86,7 @@ class NamedCompilerPhase<in Context : CommonBackendContext, Data>(
"Lowering $name: phases ${(prerequisite - phaserState.alreadyDone).map { it.name }} are required, but not satisfied"
}
context.inVerbosePhase = this in phaseConfig.verbose
context.inVerbosePhase = phaseConfig.isVerbose(this)
runBefore(phaseConfig, phaserState, context, input)
val output = if (phaseConfig.needProfiling) {
@@ -104,7 +104,7 @@ class NamedCompilerPhase<in Context : CommonBackendContext, Data>(
return output
}
private fun runBefore(phaseConfig: PhaseConfig, phaserState: PhaserState<Data>, context: Context, input: Data) {
private fun runBefore(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Data>, context: Context, input: Data) {
val state = ActionState(phaseConfig, this, phaserState.phaseCount, BeforeOrAfter.BEFORE)
for (action in actions) action(state, input, context)
@@ -113,7 +113,7 @@ class NamedCompilerPhase<in Context : CommonBackendContext, Data>(
}
}
private fun runAfter(phaseConfig: PhaseConfig, phaserState: PhaserState<Data>, context: Context, output: Data) {
private fun runAfter(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Data>, context: Context, output: Data) {
val state = ActionState(phaseConfig, this, phaserState.phaseCount, BeforeOrAfter.AFTER)
for (action in actions) action(state, output, context)
@@ -126,7 +126,7 @@ class NamedCompilerPhase<in Context : CommonBackendContext, Data>(
}
}
private fun runAndProfile(phaseConfig: PhaseConfig, phaserState: PhaserState<Data>, context: Context, source: Data): Data {
private fun runAndProfile(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Data>, context: Context, source: Data): Data {
var result: Data? = null
val msec = measureTimeMillis {
result = phaserState.downlevel(nlevels) {
@@ -31,15 +31,15 @@ private val IrElement.elementName: String
}
private fun ActionState.isDumpNeeded() =
phase in when (beforeOrAfter) {
BeforeOrAfter.BEFORE -> config.toDumpStateBefore
BeforeOrAfter.AFTER -> config.toDumpStateAfter
when (beforeOrAfter) {
BeforeOrAfter.BEFORE -> config.shouldDumpStateBefore(phase)
BeforeOrAfter.AFTER -> config.shouldDumpStateAfter(phase)
}
private fun ActionState.isValidationNeeded() =
phase in when (beforeOrAfter) {
BeforeOrAfter.BEFORE -> config.toValidateStateBefore
BeforeOrAfter.AFTER -> config.toValidateStateAfter
when (beforeOrAfter) {
BeforeOrAfter.BEFORE -> config.shouldValidateStateBefore(phase)
BeforeOrAfter.AFTER -> config.shouldValidateStateAfter(phase)
}
fun <Data, Context> makeDumpAction(dumper: Action<Data, Context>): Action<Data, Context> =
@@ -17,7 +17,7 @@ private class CompositePhase<Context : CommonBackendContext, Input, Output>(
val phases: List<CompilerPhase<Context, Any?, Any?>>
) : CompilerPhase<Context, Input, Output> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Input>, context: Context, input: Input): Output {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Input>, context: Context, input: Input): Output {
@Suppress("UNCHECKED_CAST") var currentState = phaserState as PhaserState<Any?>
var result = phases.first().invoke(phaseConfig, currentState, context, input)
for ((previous, next) in phases.zip(phases.drop(1))) {
@@ -65,7 +65,7 @@ fun <Context : CommonBackendContext, Element : IrElement> makeCustomPhase(
private class CustomPhaseAdapter<Context : CommonBackendContext, Element>(
private val op: (Context, Element) -> Unit
) : SameTypeCompilerPhase<Context, Element> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Element>, context: Context, input: Element): Element {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Element>, context: Context, input: Element): Element {
op(context, input)
return input
}
@@ -92,7 +92,7 @@ fun <Context : CommonBackendContext> namedOpUnitPhase(
name, description, prerequisite,
nlevels = 0,
lower = object : SameTypeCompilerPhase<Context, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
context.op()
}
}
@@ -116,7 +116,7 @@ fun <Context : CommonBackendContext> makeIrFilePhase(
private class FileLoweringPhaseAdapter<Context : CommonBackendContext>(
private val lowering: (Context) -> FileLoweringPass
) : SameTypeCompilerPhase<Context, IrFile> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrFile>, context: Context, input: IrFile): IrFile {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrFile>, context: Context, input: IrFile): IrFile {
lowering(context).lower(input)
return input
}
@@ -141,7 +141,7 @@ private class ModuleLoweringPhaseAdapter<Context : CommonBackendContext>(
private val lowering: (Context) -> FileLoweringPass
) : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(
phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
): IrModuleFragment {
lowering(context).lower(input)
return input
@@ -151,17 +151,17 @@ private class ModuleLoweringPhaseAdapter<Context : CommonBackendContext>(
@Suppress("unused") // Used in kotlin-native
fun <Context : CommonBackendContext, Input> unitSink(): CompilerPhase<Context, Input, Unit> =
object : CompilerPhase<Context, Input, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Input>, context: Context, input: Input) {}
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Input>, context: Context, input: Input) {}
}
// Intermediate phases to change the object of transformations
@Suppress("unused") // Used in kotlin-native
fun <Context : CommonBackendContext, OldData, NewData> takeFromContext(op: (Context) -> NewData): CompilerPhase<Context, OldData, NewData> =
object : CompilerPhase<Context, OldData, NewData> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<OldData>, context: Context, input: OldData) = op(context)
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<OldData>, context: Context, input: OldData) = op(context)
}
fun <Context : CommonBackendContext, OldData, NewData> transform(op: (OldData) -> NewData): CompilerPhase<Context, OldData, NewData> =
object : CompilerPhase<Context, OldData, NewData> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<OldData>, context: Context, input: OldData) = op(input)
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<OldData>, context: Context, input: OldData) = op(input)
}
@@ -44,9 +44,9 @@ class PhaseConfig(
val toDumpStateAfter: Set<AnyNamedPhase> = emptySet(),
override val dumpToDirectory: String? = null,
override val dumpOnlyFqName: String? = null,
val toValidateStateBefore: Set<AnyNamedPhase> = emptySet(),
val toValidateStateAfter: Set<AnyNamedPhase> = emptySet(),
val namesOfElementsExcludedFromDumping: Set<String> = emptySet(),
private val toValidateStateBefore: Set<AnyNamedPhase> = emptySet(),
private val toValidateStateAfter: Set<AnyNamedPhase> = emptySet(),
private val namesOfElementsExcludedFromDumping: Set<String> = emptySet(),
override val needProfiling: Boolean = false,
override val checkConditions: Boolean = false,
override val checkStickyConditions: Boolean = false
@@ -43,7 +43,7 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
private val copyBeforeLowering: Boolean,
) : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(
phaseConfig: PhaseConfig,
phaseConfig: PhaseConfigurationService,
phaserState: PhaserState<IrModuleFragment>,
context: Context,
input: IrModuleFragment
@@ -56,7 +56,7 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
}
private fun invokeSequential(
phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
): IrModuleFragment {
for (irFile in input.files) {
try {
@@ -74,7 +74,7 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
}
private fun invokeParallel(
phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment, nThreads: Int
phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment, nThreads: Int
): IrModuleFragment {
if (input.files.isEmpty()) return input
@@ -52,7 +52,7 @@ private fun makeCustomJsModulePhase(
prerequisite = prerequisite,
lower = object : SameTypeCompilerPhase<JsIrBackendContext, Iterable<IrModuleFragment>> {
override fun invoke(
phaseConfig: PhaseConfig,
phaseConfig: PhaseConfigurationService,
phaserState: PhaserState<Iterable<IrModuleFragment>>,
context: JsIrBackendContext,
input: Iterable<IrModuleFragment>
@@ -48,7 +48,7 @@ private fun makeCustomWasmModulePhase(
prerequisite = prerequisite,
lower = object : SameTypeCompilerPhase<WasmBackendContext, Iterable<IrModuleFragment>> {
override fun invoke(
phaseConfig: PhaseConfig,
phaseConfig: PhaseConfigurationService,
phaserState: PhaserState<Iterable<IrModuleFragment>>,
context: WasmBackendContext,
input: Iterable<IrModuleFragment>
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
private val validateAll = false
@@ -59,7 +58,7 @@ internal fun makeKonanFileOpPhase(
) = NamedCompilerPhase(
name, description, prerequisite, nlevels = 0,
lower = object : SameTypeCompilerPhase<Context, IrFile> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrFile>, context: Context, input: IrFile): IrFile {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrFile>, context: Context, input: IrFile): IrFile {
op(context, input)
return input
}
@@ -75,7 +74,7 @@ internal fun makeKonanModuleOpPhase(
) = NamedCompilerPhase(
name, description, prerequisite, nlevels = 0,
lower = object : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment): IrModuleFragment {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment): IrModuleFragment {
op(context, input)
return input
}
@@ -303,7 +303,7 @@ internal val dependenciesLowerPhase = NamedCompilerPhase(
description = "Lower library's IR",
prerequisite = emptySet(),
lower = object : CompilerPhase<Context, IrModuleFragment, IrModuleFragment> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment): IrModuleFragment {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment): IrModuleFragment {
val files = mutableListOf<IrFile>()
files += input.files
input.files.clear()
@@ -341,7 +341,7 @@ internal val umbrellaCompilation = NamedCompilerPhase(
description = "A batched compilation with shared FE and ME phases",
prerequisite = emptySet(),
lower = object : CompilerPhase<Context, Unit, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
val module = context.irModules.values.single()
val files = module.files.toList()
@@ -440,7 +440,7 @@ internal val createGenerationStatePhase = namedUnitPhase(
name = "CreateGenerationState",
description = "Create generation state",
lower = object : CompilerPhase<Context, Unit, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
context.generationState = NativeGenerationState(context)
}
}
@@ -450,7 +450,7 @@ internal val disposeGenerationStatePhase = namedUnitPhase(
name = "DisposeGenerationState",
description = "Dispose generation state",
lower = object : CompilerPhase<Context, Unit, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
context.disposeGenerationState()
}
}
@@ -6,10 +6,7 @@
package org.jetbrains.kotlin.backend.konan.llvm
import llvm.*
import org.jetbrains.kotlin.backend.common.phaser.CompilerPhase
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
import org.jetbrains.kotlin.backend.common.phaser.PhaserState
import org.jetbrains.kotlin.backend.common.phaser.namedUnitPhase
import org.jetbrains.kotlin.backend.common.phaser.*
import org.jetbrains.kotlin.backend.konan.*
import org.jetbrains.kotlin.backend.konan.descriptors.GlobalHierarchyAnalysis
import org.jetbrains.kotlin.backend.konan.llvm.coverage.runCoveragePass
@@ -308,7 +305,7 @@ internal val produceOutputPhase = namedUnitPhase(
name = "ProduceOutput",
description = "Produce output",
lower = object : CompilerPhase<Context, Unit, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
produceOutput(context)
}
}