IR: simplify phaser code and phase builder usages

(cherry picked from commit 5561f5508178f9cc3f0f81f7c2ce8bf8cf362f4a)
This commit is contained in:
Alexander Udalov
2020-07-01 12:36:18 +02:00
committed by Vasily Levchenko
parent 54917904ed
commit 9b300fcf50
2 changed files with 55 additions and 58 deletions
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.backend.common.lower.inline.LocalClassesInInlineLamb
import org.jetbrains.kotlin.backend.common.lower.loops.ForLoopsLowering
import org.jetbrains.kotlin.backend.common.phaser.*
import org.jetbrains.kotlin.backend.konan.lower.*
import org.jetbrains.kotlin.backend.konan.lower.ExpectDeclarationsRemoving
import org.jetbrains.kotlin.backend.konan.lower.FinallyBlocksLowering
import org.jetbrains.kotlin.backend.konan.lower.InitializersLowering
import org.jetbrains.kotlin.ir.declarations.IrFile
@@ -23,22 +22,22 @@ private fun makeKonanFileLoweringPhase(
lowering: (Context) -> FileLoweringPass,
name: String,
description: String,
prerequisite: Set<AnyNamedPhase> = emptySet()
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet()
) = makeIrFilePhase(lowering, name, description, prerequisite, actions = filePhaseActions)
private fun makeKonanModuleLoweringPhase(
lowering: (Context) -> FileLoweringPass,
name: String,
description: String,
prerequisite: Set<AnyNamedPhase> = emptySet()
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet()
) = makeIrModulePhase(lowering, name, description, prerequisite, actions = modulePhaseActions)
internal fun makeKonanFileOpPhase(
op: (Context, IrFile) -> Unit,
name: String,
description: String,
prerequisite: Set<AnyNamedPhase> = emptySet()
) = namedIrFilePhase(
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet()
) = NamedCompilerPhase(
name, description, prerequisite, nlevels = 0,
lower = object : SameTypeCompilerPhase<Context, IrFile> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrFile>, context: Context, input: IrFile): IrFile {
@@ -53,8 +52,8 @@ internal fun makeKonanModuleOpPhase(
op: (Context, IrModuleFragment) -> Unit,
name: String,
description: String,
prerequisite: Set<AnyNamedPhase> = emptySet()
) = namedIrModulePhase(
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet()
) = NamedCompilerPhase(
name, description, prerequisite, nlevels = 0,
lower = object : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment): IrModuleFragment {
@@ -108,7 +107,7 @@ internal val sharedVariablesPhase = makeKonanModuleLoweringPhase(
prerequisite = setOf(lateinitPhase)
)
internal val extractLocalClassesFromInlineBodies = namedIrModulePhase(
internal val extractLocalClassesFromInlineBodies = NamedCompilerPhase(
lower = object : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment): IrModuleFragment {
LocalClassesInInlineLambdasLowering(context).run {
@@ -130,7 +129,7 @@ internal val extractLocalClassesFromInlineBodies = namedIrModulePhase(
actions = modulePhaseActions
)
internal val inlinePhase = namedIrModulePhase(
internal val inlinePhase = NamedCompilerPhase(
lower = object : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment): IrModuleFragment {
FunctionInlining(context, NativeInlineFunctionResolver(context)).run {
@@ -336,7 +336,7 @@ internal val linkerPhase = konanUnitPhase(
description = "Linker"
)
internal val allLoweringsPhase = namedIrModulePhase(
internal val allLoweringsPhase = NamedCompilerPhase(
name = "IrLowering",
description = "IR Lowering",
// TODO: The lowerings before inlinePhase should be aligned with [NativeInlineFunctionResolver.kt]
@@ -353,37 +353,39 @@ internal val allLoweringsPhase = namedIrModulePhase(
performByIrFile(
name = "IrLowerByFile",
description = "IR Lowering by file",
lower = forLoopsPhase then
stringConcatenationPhase then
enumConstructorsPhase then
initializersPhase then
localFunctionsPhase then
tailrecPhase then
defaultParameterExtentPhase then
innerClassPhase then
dataClassesPhase then
singleAbstractMethodPhase then
ifNullExpressionsFusionPhase then
builtinOperatorPhase then
finallyBlocksPhase then
testProcessorPhase then
enumClassPhase then
delegationPhase then
callableReferencePhase then
interopPhase then
varargPhase then
compileTimeEvaluatePhase then
kotlinNothingValueExceptionPhase then
coroutinesPhase then
typeOperatorPhase then
bridgesPhase then
autoboxPhase then
returnsInsertionPhase
lower = listOf(
forLoopsPhase,
stringConcatenationPhase,
enumConstructorsPhase,
initializersPhase,
localFunctionsPhase,
tailrecPhase,
defaultParameterExtentPhase,
innerClassPhase,
dataClassesPhase,
singleAbstractMethodPhase,
ifNullExpressionsFusionPhase,
builtinOperatorPhase,
finallyBlocksPhase,
testProcessorPhase,
enumClassPhase,
delegationPhase,
callableReferencePhase,
interopPhase,
varargPhase,
compileTimeEvaluatePhase,
kotlinNothingValueExceptionPhase,
coroutinesPhase,
typeOperatorPhase,
bridgesPhase,
autoboxPhase,
returnsInsertionPhase,
)
),
actions = setOf(defaultDumper, ::moduleValidationCallback)
)
internal val dependenciesLowerPhase = SameTypeNamedPhaseWrapper(
internal val dependenciesLowerPhase = NamedCompilerPhase(
name = "LowerLibIR",
description = "Lower library's IR",
prerequisite = emptySet(),
@@ -421,35 +423,31 @@ internal val dependenciesLowerPhase = SameTypeNamedPhaseWrapper(
}
})
internal val entryPointPhase = SameTypeNamedPhaseWrapper(
internal val entryPointPhase = makeCustomPhase<Context, IrModuleFragment>(
name = "addEntryPoint",
description = "Add entry point for program",
prerequisite = emptySet(),
lower = object : CompilerPhase<Context, IrModuleFragment, IrModuleFragment> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>,
context: Context, input: IrModuleFragment): IrModuleFragment {
assert(context.config.produce == CompilerOutputKind.PROGRAM)
op = { context, _ ->
assert(context.config.produce == CompilerOutputKind.PROGRAM)
val originalFile = context.ir.symbols.entryPoint!!.owner.file
val originalModule = originalFile.packageFragmentDescriptor.containingDeclaration
val file = if (context.llvmModuleSpecification.containsModule(originalModule)) {
originalFile
} else {
// `main` function is compiled to other LLVM module.
// For example, test running support uses `main` defined in stdlib.
context.irModule!!.addFile(originalFile.fileEntry, originalFile.fqName)
}
require(context.llvmModuleSpecification.containsModule(
file.packageFragmentDescriptor.containingDeclaration))
file.addChild(makeEntryPoint(context))
return input
val originalFile = context.ir.symbols.entryPoint!!.owner.file
val originalModule = originalFile.packageFragmentDescriptor.containingDeclaration
val file = if (context.llvmModuleSpecification.containsModule(originalModule)) {
originalFile
} else {
// `main` function is compiled to other LLVM module.
// For example, test running support uses `main` defined in stdlib.
context.irModule!!.addFile(originalFile.fileEntry, originalFile.fqName)
}
require(context.llvmModuleSpecification.containsModule(
file.packageFragmentDescriptor.containingDeclaration))
file.addChild(makeEntryPoint(context))
}
)
internal val bitcodePhase = namedIrModulePhase(
internal val bitcodePhase = NamedCompilerPhase(
name = "Bitcode",
description = "LLVM Bitcode generation",
lower = contextLLVMSetupPhase then