From 54deba63a1190dc41d2049c3330e3693e8672b0f Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Mon, 3 Oct 2022 12:14:47 +0300 Subject: [PATCH] Use PhaseConfigurationService in CompilerPhase instead of PhaseConfig --- .../backend/common/phaser/CompilerPhase.kt | 16 ++++++++-------- .../backend/common/phaser/DumperVerifier.kt | 12 ++++++------ .../backend/common/phaser/PhaseBuilders.kt | 16 ++++++++-------- .../kotlin/backend/common/phaser/PhaseConfig.kt | 6 +++--- .../backend/common/phaser/performByIrFile.kt | 6 +++--- .../kotlin/ir/backend/js/JsLoweringPhases.kt | 2 +- .../kotlin/backend/wasm/WasmLoweringPhases.kt | 2 +- .../kotlin/backend/konan/KonanLoweringPhases.kt | 5 ++--- .../kotlin/backend/konan/ToplevelPhases.kt | 8 ++++---- .../kotlin/backend/konan/llvm/BitcodePhases.kt | 7 ++----- 10 files changed, 38 insertions(+), 42 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/CompilerPhase.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/CompilerPhase.kt index 35676cdedec..e44f85ac491 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/CompilerPhase.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/CompilerPhase.kt @@ -28,7 +28,7 @@ inline fun PhaserState.downlevel(nlevels: Int, block: () -> R): R { } interface CompilerPhase { - fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Input): Output + fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output fun getNamedSubphases(startDepth: Int = 0): List>> = 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( private val actions: Set> = emptySet(), private val nlevels: Int = 0 ) : SameTypeCompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Data): Data { - if (this !in phaseConfig.enabled) { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Data): Data { + if (!phaseConfig.isEnabled(this)) { return input } @@ -86,7 +86,7 @@ class NamedCompilerPhase( "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( return output } - private fun runBefore(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Data) { + private fun runBefore(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, 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( } } - private fun runAfter(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, output: Data) { + private fun runAfter(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, 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( } } - private fun runAndProfile(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, source: Data): Data { + private fun runAndProfile(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, source: Data): Data { var result: Data? = null val msec = measureTimeMillis { result = phaserState.downlevel(nlevels) { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/DumperVerifier.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/DumperVerifier.kt index 5c8c4fdfd5b..e92e73b9ea1 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/DumperVerifier.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/DumperVerifier.kt @@ -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 makeDumpAction(dumper: Action): Action = diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseBuilders.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseBuilders.kt index feee4277c2e..81fd6c57aa5 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseBuilders.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseBuilders.kt @@ -17,7 +17,7 @@ private class CompositePhase( val phases: List> ) : CompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Input): Output { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output { @Suppress("UNCHECKED_CAST") var currentState = phaserState as PhaserState var result = phases.first().invoke(phaseConfig, currentState, context, input) for ((previous, next) in phases.zip(phases.drop(1))) { @@ -65,7 +65,7 @@ fun makeCustomPhase( private class CustomPhaseAdapter( private val op: (Context, Element) -> Unit ) : SameTypeCompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Element): Element { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Element): Element { op(context, input) return input } @@ -92,7 +92,7 @@ fun namedOpUnitPhase( name, description, prerequisite, nlevels = 0, lower = object : SameTypeCompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Unit) { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Unit) { context.op() } } @@ -116,7 +116,7 @@ fun makeIrFilePhase( private class FileLoweringPhaseAdapter( private val lowering: (Context) -> FileLoweringPass ) : SameTypeCompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrFile): IrFile { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrFile): IrFile { lowering(context).lower(input) return input } @@ -141,7 +141,7 @@ private class ModuleLoweringPhaseAdapter( private val lowering: (Context) -> FileLoweringPass ) : SameTypeCompilerPhase { override fun invoke( - phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrModuleFragment + phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrModuleFragment ): IrModuleFragment { lowering(context).lower(input) return input @@ -151,17 +151,17 @@ private class ModuleLoweringPhaseAdapter( @Suppress("unused") // Used in kotlin-native fun unitSink(): CompilerPhase = object : CompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Input) {} + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input) {} } // Intermediate phases to change the object of transformations @Suppress("unused") // Used in kotlin-native fun takeFromContext(op: (Context) -> NewData): CompilerPhase = object : CompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: OldData) = op(context) + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: OldData) = op(context) } fun transform(op: (OldData) -> NewData): CompilerPhase = object : CompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: OldData) = op(input) + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: OldData) = op(input) } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseConfig.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseConfig.kt index f17373b6296..a4518384cc0 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseConfig.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseConfig.kt @@ -44,9 +44,9 @@ class PhaseConfig( val toDumpStateAfter: Set = emptySet(), override val dumpToDirectory: String? = null, override val dumpOnlyFqName: String? = null, - val toValidateStateBefore: Set = emptySet(), - val toValidateStateAfter: Set = emptySet(), - val namesOfElementsExcludedFromDumping: Set = emptySet(), + private val toValidateStateBefore: Set = emptySet(), + private val toValidateStateAfter: Set = emptySet(), + private val namesOfElementsExcludedFromDumping: Set = emptySet(), override val needProfiling: Boolean = false, override val checkConditions: Boolean = false, override val checkStickyConditions: Boolean = false diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/performByIrFile.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/performByIrFile.kt index 1d94cb8ccfc..2e95481fe80 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/performByIrFile.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/performByIrFile.kt @@ -43,7 +43,7 @@ private class PerformByIrFilePhase( private val copyBeforeLowering: Boolean, ) : SameTypeCompilerPhase { override fun invoke( - phaseConfig: PhaseConfig, + phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrModuleFragment @@ -56,7 +56,7 @@ private class PerformByIrFilePhase( } private fun invokeSequential( - phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrModuleFragment + phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrModuleFragment ): IrModuleFragment { for (irFile in input.files) { try { @@ -74,7 +74,7 @@ private class PerformByIrFilePhase( } private fun invokeParallel( - phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrModuleFragment, nThreads: Int + phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrModuleFragment, nThreads: Int ): IrModuleFragment { if (input.files.isEmpty()) return input diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt index 7d229987ba6..a828c2e6532 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt @@ -52,7 +52,7 @@ private fun makeCustomJsModulePhase( prerequisite = prerequisite, lower = object : SameTypeCompilerPhase> { override fun invoke( - phaseConfig: PhaseConfig, + phaseConfig: PhaseConfigurationService, phaserState: PhaserState>, context: JsIrBackendContext, input: Iterable diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmLoweringPhases.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmLoweringPhases.kt index b2848922b29..fdd128bdb29 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmLoweringPhases.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmLoweringPhases.kt @@ -48,7 +48,7 @@ private fun makeCustomWasmModulePhase( prerequisite = prerequisite, lower = object : SameTypeCompilerPhase> { override fun invoke( - phaseConfig: PhaseConfig, + phaseConfig: PhaseConfigurationService, phaserState: PhaserState>, context: WasmBackendContext, input: Iterable diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt index cf74192deae..5979ed695e8 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt @@ -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 { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrFile): IrFile { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, 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 { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrModuleFragment): IrModuleFragment { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrModuleFragment): IrModuleFragment { op(context, input) return input } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt index feccc43e73d..bdab4cc0cda 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt @@ -303,7 +303,7 @@ internal val dependenciesLowerPhase = NamedCompilerPhase( description = "Lower library's IR", prerequisite = emptySet(), lower = object : CompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrModuleFragment): IrModuleFragment { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrModuleFragment): IrModuleFragment { val files = mutableListOf() 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 { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Unit) { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, 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 { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Unit) { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, 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 { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Unit) { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Unit) { context.disposeGenerationState() } } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt index 03c21eb8dab..d54f34863b2 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt @@ -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 { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Unit) { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Unit) { produceOutput(context) } }