From 6a4722188f88bfb69450470bec1e56dd84478db2 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Mon, 3 Oct 2022 14:07:50 +0300 Subject: [PATCH] Extract SameTypeNamedCompilerPhase from NamedCompilerPhase Currently, compiler pipelines are heavily couples with NamedCompilerPhase. Unfortunately, NamedCompilerPhase uses the same type for Input and Output, thus it is not applicable to phases that try to transform some data purely. Thus, we separate this class into two, allowing to have a new inheritor of NamedCompilerPhase with different Input and Output types. --- .../backend/common/phaser/CompilerPhase.kt | 103 ++++++++++++------ .../backend/common/phaser/PhaseBuilders.kt | 32 +++--- .../backend/common/phaser/performByIrFile.kt | 10 +- .../kotlin/ir/backend/js/JsLoweringPhases.kt | 20 ++-- .../jetbrains/kotlin/backend/jvm/JvmPhases.kt | 8 +- .../jetbrains/kotlin/backend/jvm/JvmLower.kt | 12 +- .../kotlin/backend/wasm/WasmLoweringPhases.kt | 14 +-- .../backend/konan/KonanLoweringPhases.kt | 12 +- .../kotlin/backend/konan/ToplevelPhases.kt | 32 +++--- 9 files changed, 142 insertions(+), 101 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 b957979b9d1..425fc586261 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 @@ -18,7 +18,7 @@ class PhaserState( } // Copy state, forgetting the sticky postconditions (which will not be applicable to the new type) -fun PhaserState.changeType() = PhaserState(alreadyDone, depth, phaseCount, mutableSetOf()) +fun PhaserState.changePhaserStateType() = PhaserState(alreadyDone, depth, phaseCount, mutableSetOf()) inline fun PhaserState.downlevel(nlevels: Int, block: () -> R): R { depth += nlevels @@ -30,7 +30,7 @@ inline fun PhaserState.downlevel(nlevels: Int, block: () -> R): R { interface CompilerPhase { fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output - fun getNamedSubphases(startDepth: Int = 0): List>> = emptyList() + fun getNamedSubphases(startDepth: Int = 0): List>> = emptyList() // In phase trees, `stickyPostconditions` is inherited along the right edge to be used in `then`. val stickyPostconditions: Set> get() = emptySet() @@ -47,7 +47,7 @@ interface SameTypeCompilerPhase : CompilerPha // A failing checker should just throw an exception. typealias Checker = (Data) -> Unit -typealias AnyNamedPhase = NamedCompilerPhase<*, *> +typealias AnyNamedPhase = AbstractNamedCompilerPhase<*, *, *> enum class BeforeOrAfter { BEFORE, AFTER } @@ -66,20 +66,18 @@ infix operator fun Action.plus(other: Action( +// TODO: A better name would be just `NamedCompilerPhase`, but it is already used (see below). +abstract class AbstractNamedCompilerPhase( val name: String, val description: String, - val prerequisite: Set> = emptySet(), - private val lower: CompilerPhase, - val preconditions: Set> = emptySet(), - val postconditions: Set> = emptySet(), - override val stickyPostconditions: Set> = emptySet(), - private val actions: Set> = emptySet(), - private val nlevels: Int = 0 -) : SameTypeCompilerPhase { - override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Data): Data { + val prerequisite: Set> = emptySet(), + val preconditions: Set> = emptySet(), + val postconditions: Set> = emptySet(), + protected val nlevels: Int = 0 +) : CompilerPhase { + override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output { if (!phaseConfig.isEnabled(this)) { - return input + return outputIfNotEnabled(phaseConfig, phaserState, context, input) } assert(phaserState.alreadyDone.containsAll(prerequisite)) { @@ -93,10 +91,10 @@ class NamedCompilerPhase( runAndProfile(phaseConfig, phaserState, context, input) } else { phaserState.downlevel(nlevels) { - lower.invoke(phaseConfig, phaserState, context, input) + phaseBody(phaseConfig, phaserState, context, input) } } - runAfter(phaseConfig, phaserState, context, output) + runAfter(phaseConfig, changePhaserStateType(phaserState), context, output) phaserState.alreadyDone.add(this) phaserState.phaseCount++ @@ -104,7 +102,57 @@ class NamedCompilerPhase( return output } - private fun runBefore(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Data) { + abstract fun phaseBody(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output + + abstract fun outputIfNotEnabled(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output + + abstract fun changePhaserStateType(phaserState: PhaserState): PhaserState + + abstract fun runBefore(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input) + + abstract fun runAfter(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, output: Output) + + private fun runAndProfile(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, source: Input): Output { + var result: Output? = null + val msec = measureTimeMillis { + result = phaserState.downlevel(nlevels) { + phaseBody(phaseConfig, phaserState, context, source) + } + } + // TODO: use a proper logger + println("${"\t".repeat(phaserState.depth)}$description: $msec msec") + return result!! + } + + override fun toString() = "Compiler Phase @$name" +} + +// TODO: This class should be named `SameTypeNamedCompilerPhase`, +// but it would be a breaking change (e.g. there are usages in IntelliJ repo), +// so we introduce a typealias instead as a temporary solution. +class NamedCompilerPhase( + name: String, + description: String, + prerequisite: Set> = emptySet(), + private val lower: CompilerPhase, + preconditions: Set> = emptySet(), + postconditions: Set> = emptySet(), + override val stickyPostconditions: Set> = emptySet(), + private val actions: Set> = emptySet(), + nlevels: Int = 0 +) : AbstractNamedCompilerPhase( + name, description, prerequisite, preconditions, postconditions, nlevels +) { + override fun phaseBody(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Data): Data = + lower.invoke(phaseConfig, phaserState, context, input) + + override fun outputIfNotEnabled(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Data): Data = + input + + override fun changePhaserStateType(phaserState: PhaserState): PhaserState = + phaserState + + override 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 +161,7 @@ class NamedCompilerPhase( } } - private fun runAfter(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, output: Data) { + override 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,20 +174,9 @@ class NamedCompilerPhase( } } - private fun runAndProfile(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, source: Data): Data { - var result: Data? = null - val msec = measureTimeMillis { - result = phaserState.downlevel(nlevels) { - lower.invoke(phaseConfig, phaserState, context, source) - } - } - // TODO: use a proper logger - println("${"\t".repeat(phaserState.depth)}$description: $msec msec") - return result!! - } - - override fun getNamedSubphases(startDepth: Int): List>> = + override fun getNamedSubphases(startDepth: Int): List>> = listOf(startDepth to this) + lower.getNamedSubphases(startDepth + nlevels) - - override fun toString() = "Compiler Phase @$name" } + + +typealias SameTypeNamedCompilerPhase = NamedCompilerPhase \ No newline at end of file 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 81fd6c57aa5..1f143980818 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 @@ -23,7 +23,7 @@ private class CompositePhase( for ((previous, next) in phases.zip(phases.drop(1))) { if (next !is SameTypeCompilerPhase<*, *>) { // Discard `stickyPostconditions`, they are useless since data type is changing. - currentState = currentState.changeType() + currentState = currentState.changePhaserStateType() } currentState.stickyPostconditions.addAll(previous.stickyPostconditions) result = next.invoke(phaseConfig, currentState, context, result) @@ -32,7 +32,7 @@ private class CompositePhase( return result as Output } - override fun getNamedSubphases(startDepth: Int): List>> = + override fun getNamedSubphases(startDepth: Int): List>> = phases.flatMap { it.getNamedSubphases(startDepth) } override val stickyPostconditions get() = phases.last().stickyPostconditions @@ -51,14 +51,14 @@ fun makeCustomPhase( op: (Context, Element) -> Unit, name: String, description: String, - prerequisite: Set> = emptySet(), + prerequisite: Set> = emptySet(), preconditions: Set> = emptySet(), postconditions: Set> = emptySet(), stickyPostconditions: Set> = emptySet(), actions: Set> = setOf(defaultDumper, validationAction), nlevels: Int = 1 -): NamedCompilerPhase = - NamedCompilerPhase( +): SameTypeNamedCompilerPhase = + SameTypeNamedCompilerPhase( name, description, prerequisite, CustomPhaseAdapter(op), preconditions, postconditions, stickyPostconditions, actions, nlevels, ) @@ -74,11 +74,11 @@ private class CustomPhaseAdapter( fun namedUnitPhase( name: String, description: String, - prerequisite: Set> = emptySet(), + prerequisite: Set> = emptySet(), nlevels: Int = 1, lower: CompilerPhase -): NamedCompilerPhase = - NamedCompilerPhase( +): SameTypeNamedCompilerPhase = + SameTypeNamedCompilerPhase( name, description, prerequisite, lower, nlevels = nlevels ) @@ -86,9 +86,9 @@ fun namedUnitPhase( fun namedOpUnitPhase( name: String, description: String, - prerequisite: Set>, + prerequisite: Set>, op: Context.() -> Unit -): NamedCompilerPhase = namedUnitPhase( +): SameTypeNamedCompilerPhase = namedUnitPhase( name, description, prerequisite, nlevels = 0, lower = object : SameTypeCompilerPhase { @@ -102,13 +102,13 @@ fun makeIrFilePhase( lowering: (Context) -> FileLoweringPass, name: String, description: String, - prerequisite: Set> = emptySet(), + prerequisite: Set> = emptySet(), preconditions: Set> = emptySet(), postconditions: Set> = emptySet(), stickyPostconditions: Set> = emptySet(), actions: Set> = setOf(defaultDumper, validationAction) -): NamedCompilerPhase = - NamedCompilerPhase( +): SameTypeNamedCompilerPhase = + SameTypeNamedCompilerPhase( name, description, prerequisite, FileLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions, nlevels = 0, ) @@ -126,13 +126,13 @@ fun makeIrModulePhase( lowering: (Context) -> FileLoweringPass, name: String, description: String, - prerequisite: Set> = emptySet(), + prerequisite: Set> = emptySet(), preconditions: Set> = emptySet(), postconditions: Set> = emptySet(), stickyPostconditions: Set> = emptySet(), actions: Set> = setOf(defaultDumper, validationAction) -): NamedCompilerPhase = - NamedCompilerPhase( +): SameTypeNamedCompilerPhase = + SameTypeNamedCompilerPhase( name, description, prerequisite, ModuleLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions, nlevels = 0, ) 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 2e95481fe80..9b8902b23a9 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 @@ -32,8 +32,8 @@ fun performByIrFile( description: String = "Perform phases by IrFile", copyBeforeLowering: Boolean = true, lower: List>, -): NamedCompilerPhase = - NamedCompilerPhase( +): SameTypeNamedCompilerPhase = + SameTypeNamedCompilerPhase( name, description, emptySet(), PerformByIrFilePhase(lower, copyBeforeLowering), emptySet(), emptySet(), emptySet(), setOf(defaultDumper), nlevels = 1, ) @@ -60,7 +60,7 @@ private class PerformByIrFilePhase( ): IrModuleFragment { for (irFile in input.files) { try { - val filePhaserState = phaserState.changeType() + val filePhaserState = phaserState.changePhaserStateType() for (phase in lower) { phase.invoke(phaseConfig, filePhaserState, context, irFile) } @@ -97,7 +97,7 @@ private class PerformByIrFilePhase( for ((irFile, state) in filesAndStates) { executor.execute { try { - val filePhaserState = state.changeType() + val filePhaserState = state.changePhaserStateType() for (phase in lower) { phase.invoke(phaseConfig, filePhaserState, context, irFile) } @@ -134,7 +134,7 @@ private class PerformByIrFilePhase( return input } - override fun getNamedSubphases(startDepth: Int): List>> = + override fun getNamedSubphases(startDepth: Int): List>> = lower.flatMap { it.getNamedSubphases(startDepth) } } 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 a828c2e6532..080db9f0936 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 @@ -33,8 +33,8 @@ private fun makeJsModulePhase( lowering: (JsIrBackendContext) -> FileLoweringPass, name: String, description: String, - prerequisite: Set> = emptySet() -): NamedCompilerPhase> = makeCustomJsModulePhase( + prerequisite: Set> = emptySet() +): SameTypeNamedCompilerPhase> = makeCustomJsModulePhase( op = { context, modules -> lowering(context).lower(modules) }, name = name, description = description, @@ -45,8 +45,8 @@ private fun makeCustomJsModulePhase( op: (JsIrBackendContext, IrModuleFragment) -> Unit, description: String, name: String, - prerequisite: Set> = emptySet() -): NamedCompilerPhase> = NamedCompilerPhase( + prerequisite: Set> = emptySet() +): SameTypeNamedCompilerPhase> = SameTypeNamedCompilerPhase( name = name, description = description, prerequisite = prerequisite, @@ -67,13 +67,13 @@ private fun makeCustomJsModulePhase( ) sealed class Lowering(val name: String) { - abstract val modulePhase: NamedCompilerPhase> + abstract val modulePhase: SameTypeNamedCompilerPhase> } class DeclarationLowering( name: String, description: String, - prerequisite: Set> = emptySet(), + prerequisite: Set> = emptySet(), private val factory: (JsIrBackendContext) -> DeclarationTransformer ) : Lowering(name) { fun declarationTransformer(context: JsIrBackendContext): DeclarationTransformer { @@ -86,7 +86,7 @@ class DeclarationLowering( class BodyLowering( name: String, description: String, - prerequisite: Set> = emptySet(), + prerequisite: Set> = emptySet(), private val factory: (JsIrBackendContext) -> BodyLoweringPass ) : Lowering(name) { fun bodyLowering(context: JsIrBackendContext): BodyLoweringPass { @@ -98,7 +98,7 @@ class BodyLowering( class ModuleLowering( name: String, - override val modulePhase: NamedCompilerPhase> + override val modulePhase: SameTypeNamedCompilerPhase> ) : Lowering(name) private fun makeDeclarationTransformerPhase( @@ -115,7 +115,7 @@ private fun makeBodyLoweringPhase( prerequisite: Set = emptySet() ) = BodyLowering(name, description, prerequisite.map { it.modulePhase }.toSet(), lowering) -fun NamedCompilerPhase>.toModuleLowering() = ModuleLowering(this.name, this) +fun SameTypeNamedCompilerPhase>.toModuleLowering() = ModuleLowering(this.name, this) private val validateIrBeforeLowering = makeCustomJsModulePhase( { context, module -> validationCallback(context, module) }, @@ -927,7 +927,7 @@ val loweringList = listOf( // TODO comment? Eliminate ModuleLowering's? Don't filter them here? val pirLowerings = loweringList.filter { it is DeclarationLowering || it is BodyLowering } + staticMembersLoweringPhase -val jsPhases = NamedCompilerPhase( +val jsPhases = SameTypeNamedCompilerPhase( name = "IrModuleLowering", description = "IR module lowering", lower = loweringList.map { diff --git a/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmPhases.kt b/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmPhases.kt index 46c1cdff7e9..1e6dd8d6aec 100644 --- a/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmPhases.kt +++ b/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmPhases.kt @@ -6,7 +6,7 @@ package org.jetbrains.kotlin.backend.jvm import org.jetbrains.kotlin.backend.common.FileLoweringPass -import org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase +import org.jetbrains.kotlin.backend.common.phaser.SameTypeNamedCompilerPhase import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase import org.jetbrains.kotlin.backend.common.phaser.performByIrFile import org.jetbrains.kotlin.backend.common.phaser.then @@ -16,7 +16,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.IrModuleFragment import org.jetbrains.kotlin.ir.util.render -private fun codegenPhase(generateMultifileFacade: Boolean): NamedCompilerPhase { +private fun codegenPhase(generateMultifileFacade: Boolean): SameTypeNamedCompilerPhase { val suffix = if (generateMultifileFacade) "MultifileFacades" else "Regular" val descriptionSuffix = if (generateMultifileFacade) ", multifile facades" else ", regular files" return performByIrFile( @@ -50,7 +50,7 @@ private class FileCodegen(private val context: JvmBackendContext, private val ge // Generate multifile facades first, to compute and store JVM signatures of const properties which are later used // when serializing metadata in the multifile parts. // TODO: consider dividing codegen itself into separate phases (bytecode generation, metadata serialization) to avoid this -internal val jvmCodegenPhases = NamedCompilerPhase( +internal val jvmCodegenPhases = SameTypeNamedCompilerPhase( name = "Codegen", description = "Code generation", nlevels = 1, @@ -60,5 +60,5 @@ internal val jvmCodegenPhases = NamedCompilerPhase( // This property is needed to avoid dependencies from "leaf" modules (cli, tests-common-new) on backend.jvm:lower. // It's used to create PhaseConfig and is the only thing needed from lowerings in the leaf modules. -val jvmPhases: NamedCompilerPhase +val jvmPhases: SameTypeNamedCompilerPhase get() = jvmLoweringPhases diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt index ca0fbf7d257..2813461c43f 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt @@ -31,7 +31,7 @@ import org.jetbrains.kotlin.name.NameUtils private var patchParentPhases = 0 @Suppress("unused") -private fun makePatchParentsPhase(): NamedCompilerPhase { +private fun makePatchParentsPhase(): SameTypeNamedCompilerPhase { val number = patchParentPhases++ return makeIrFilePhase( { PatchDeclarationParents() }, @@ -43,7 +43,7 @@ private fun makePatchParentsPhase(): NamedCompilerPhase { +private fun makeCheckParentsPhase(): SameTypeNamedCompilerPhase { val number = checkParentPhases++ return makeIrFilePhase( { CheckDeclarationParents() }, @@ -387,9 +387,9 @@ val jvmLoweringPhases = buildJvmLoweringPhases("IrLowering", listOf("PerformByIr private fun buildJvmLoweringPhases( name: String, - phases: List>>> -): NamedCompilerPhase { - return NamedCompilerPhase( + phases: List>>> +): SameTypeNamedCompilerPhase { + return SameTypeNamedCompilerPhase( name = name, description = "IR lowering", nlevels = 1, @@ -417,7 +417,7 @@ private fun buildJvmLoweringPhases( // Build a compiler phase from a list of lowering sequences: each subsequence is run // in parallel per file, and each parallel composition is run in sequence. private fun buildLoweringsPhase( - perModuleLowerings: List>>>, + perModuleLowerings: List>>>, ): CompilerPhase = perModuleLowerings.map { (name, lowerings) -> performByIrFile(name, lower = lowerings) } .reduce< 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 fdd128bdb29..7292915a2d7 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 @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.backend.wasm import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.lower import org.jetbrains.kotlin.backend.common.lower.* +import org.jetbrains.kotlin.backend.common.lower.coroutines.AddContinuationToNonLocalSuspendFunctionsLowering import org.jetbrains.kotlin.backend.common.lower.inline.FunctionInlining import org.jetbrains.kotlin.backend.common.lower.loops.ForLoopsLowering import org.jetbrains.kotlin.backend.common.lower.optimizations.PropertyAccessorInlineLowering @@ -16,7 +17,6 @@ import org.jetbrains.kotlin.backend.common.toMultiModuleAction import org.jetbrains.kotlin.backend.wasm.lower.* import org.jetbrains.kotlin.ir.backend.js.lower.* import org.jetbrains.kotlin.ir.backend.js.lower.coroutines.AddContinuationToFunctionCallsLowering -import org.jetbrains.kotlin.backend.common.lower.coroutines.AddContinuationToNonLocalSuspendFunctionsLowering import org.jetbrains.kotlin.ir.backend.js.lower.coroutines.JsSuspendFunctionsLowering import org.jetbrains.kotlin.ir.backend.js.lower.inline.RemoveInlineDeclarationsWithReifiedTypeParametersLowering import org.jetbrains.kotlin.ir.backend.wasm.lower.generateMainFunctionCalls @@ -27,8 +27,8 @@ private fun makeWasmModulePhase( lowering: (WasmBackendContext) -> FileLoweringPass, name: String, description: String, - prerequisite: Set> = emptySet() -): NamedCompilerPhase> = + prerequisite: Set> = emptySet() +): SameTypeNamedCompilerPhase> = makeCustomWasmModulePhase( op = { context, modules -> lowering(context).lower(modules) }, name = name, @@ -40,9 +40,9 @@ private fun makeCustomWasmModulePhase( op: (WasmBackendContext, IrModuleFragment) -> Unit, description: String, name: String, - prerequisite: Set> = emptySet() -): NamedCompilerPhase> = - NamedCompilerPhase( + prerequisite: Set> = emptySet() +): SameTypeNamedCompilerPhase> = + SameTypeNamedCompilerPhase( name = name, description = description, prerequisite = prerequisite, @@ -549,7 +549,7 @@ private val unitToVoidLowering = makeWasmModulePhase( description = "Replace some Unit's with Void's" ) -val wasmPhases = NamedCompilerPhase( +val wasmPhases = SameTypeNamedCompilerPhase( name = "IrModuleLowering", description = "IR module lowering", lower = validateIrBeforeLowering then 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 5979ed695e8..5acf23fdec8 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 @@ -40,22 +40,22 @@ private fun makeKonanFileLoweringPhase( lowering: (Context) -> FileLoweringPass, name: String, description: String, - prerequisite: Set> = emptySet() + prerequisite: Set> = emptySet() ) = makeIrFilePhase(lowering, name, description, prerequisite, actions = filePhaseActions) private fun makeKonanModuleLoweringPhase( lowering: (Context) -> FileLoweringPass, name: String, description: String, - prerequisite: Set> = emptySet() + prerequisite: Set> = emptySet() ) = makeIrModulePhase(lowering, name, description, prerequisite, actions = modulePhaseActions) internal fun makeKonanFileOpPhase( op: (Context, IrFile) -> Unit, name: String, description: String, - prerequisite: Set> = emptySet() -) = NamedCompilerPhase( + prerequisite: Set> = emptySet() +) = SameTypeNamedCompilerPhase( name, description, prerequisite, nlevels = 0, lower = object : SameTypeCompilerPhase { override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrFile): IrFile { @@ -70,8 +70,8 @@ internal fun makeKonanModuleOpPhase( op: (Context, IrModuleFragment) -> Unit, name: String, description: String, - prerequisite: Set> = emptySet() -) = NamedCompilerPhase( + prerequisite: Set> = emptySet() +) = SameTypeNamedCompilerPhase( name, description, prerequisite, nlevels = 0, lower = object : SameTypeCompilerPhase { override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrModuleFragment): IrModuleFragment { 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 bdab4cc0cda..54f8555f420 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 @@ -5,9 +5,9 @@ package org.jetbrains.kotlin.backend.konan -import org.jetbrains.kotlin.backend.common.checkDeclarationParents import org.jetbrains.kotlin.backend.common.IrValidator import org.jetbrains.kotlin.backend.common.IrValidatorConfig +import org.jetbrains.kotlin.backend.common.checkDeclarationParents import org.jetbrains.kotlin.backend.common.phaser.* import org.jetbrains.kotlin.backend.common.serialization.CompatibilityMode import org.jetbrains.kotlin.backend.common.serialization.metadata.KlibMetadataMonolithicSerializer @@ -19,11 +19,15 @@ import org.jetbrains.kotlin.backend.konan.lower.SamSuperTypesChecker import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExport import org.jetbrains.kotlin.backend.konan.objcexport.createCodeSpec import org.jetbrains.kotlin.backend.konan.objcexport.produceObjCExportInterface -import org.jetbrains.kotlin.backend.konan.serialization.* +import org.jetbrains.kotlin.backend.konan.serialization.KonanIdSignaturer +import org.jetbrains.kotlin.backend.konan.serialization.KonanIrModuleSerializer +import org.jetbrains.kotlin.backend.konan.serialization.KonanManglerDesc import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.config.languageVersionSettings -import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.declarations.IrModuleFragment import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl +import org.jetbrains.kotlin.ir.declarations.path import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.konan.target.CompilerOutputKind import org.jetbrains.kotlin.name.FqName @@ -69,7 +73,7 @@ internal fun fileValidationCallback(state: ActionState, irFile: IrFile, context: internal fun konanUnitPhase( name: String, description: String, - prerequisite: Set> = emptySet(), + prerequisite: Set> = emptySet(), op: Context.() -> Unit ) = namedOpUnitPhase(name, description, prerequisite, op) @@ -235,7 +239,7 @@ internal val finalizeCachePhase = konanUnitPhase( description = "Finalize cache (rename temp to the final dist)" ) -internal val allLoweringsPhase = NamedCompilerPhase( +internal val allLoweringsPhase = SameTypeNamedCompilerPhase( name = "IrLowering", description = "IR Lowering", // TODO: The lowerings before inlinePhase should be aligned with [NativeInlineFunctionResolver.kt] @@ -298,7 +302,7 @@ internal val allLoweringsPhase = NamedCompilerPhase( actions = setOf(defaultDumper, ::moduleValidationCallback) ) -internal val dependenciesLowerPhase = NamedCompilerPhase( +internal val dependenciesLowerPhase = SameTypeNamedCompilerPhase( name = "LowerLibIR", description = "Lower library's IR", prerequisite = emptySet(), @@ -336,7 +340,7 @@ internal val dependenciesLowerPhase = NamedCompilerPhase( } }) -internal val umbrellaCompilation = NamedCompilerPhase( +internal val umbrellaCompilation = SameTypeNamedCompilerPhase( name = "UmbrellaCompilation", description = "A batched compilation with shared FE and ME phases", prerequisite = emptySet(), @@ -389,7 +393,7 @@ internal val entryPointPhase = makeCustomPhase( } ) -internal val bitcodePhase = NamedCompilerPhase( +internal val bitcodePhase = SameTypeNamedCompilerPhase( name = "Bitcode", description = "LLVM Bitcode generation", lower = returnsInsertionPhase then @@ -411,7 +415,7 @@ internal val bitcodePhase = NamedCompilerPhase( cStubsPhase ) -private val bitcodePostprocessingPhase = NamedCompilerPhase( +private val bitcodePostprocessingPhase = SameTypeNamedCompilerPhase( name = "BitcodePostprocessing", description = "Optimize and rewrite bitcode", lower = checkExternalCallsPhase then @@ -422,7 +426,7 @@ private val bitcodePostprocessingPhase = NamedCompilerPhase( rewriteExternalCallsCheckerGlobals ) -private val backendCodegen = NamedCompilerPhase( +private val backendCodegen = SameTypeNamedCompilerPhase( name = "Backend codegen", description = "Backend code generation", lower = entryPointPhase then @@ -456,7 +460,7 @@ internal val disposeGenerationStatePhase = namedUnitPhase( } ) -private val phasesOverMainModule = NamedCompilerPhase( +private val phasesOverMainModule = SameTypeNamedCompilerPhase( name = "PhasesOverMainModule", description = "Phases over main module", lower = takeFromContext { it.irModule!! } then @@ -466,7 +470,7 @@ private val phasesOverMainModule = NamedCompilerPhase( prerequisite = setOf(psiToIrPhase) ) -private val entireBackend = NamedCompilerPhase( +private val entireBackend = SameTypeNamedCompilerPhase( name = "EntireBackend", description = "Entire backend", lower = createGenerationStatePhase then @@ -480,7 +484,7 @@ private val entireBackend = NamedCompilerPhase( disposeGenerationStatePhase ) -private val middleEnd = NamedCompilerPhase( +private val middleEnd = SameTypeNamedCompilerPhase( name = "MiddleEnd", description = "Build and prepare IR for back end", lower = createSymbolTablePhase then @@ -494,7 +498,7 @@ private val middleEnd = NamedCompilerPhase( functionsWithoutBoundCheck ) -private val singleCompilation = NamedCompilerPhase( +private val singleCompilation = SameTypeNamedCompilerPhase( name = "SingleCompilation", description = "Single compilation", lower = entireBackend