[JVM_IR] Use createSimpleNamedCompilerPhase to create lowerings

This commit is contained in:
Ivan Kylchik
2023-09-21 11:47:41 +02:00
committed by Space Team
parent f5bb477459
commit 32057f6d10
5 changed files with 56 additions and 72 deletions
@@ -94,76 +94,67 @@ fun <Context : LoggingContext, Input> createSimpleNamedCompilerPhase(
op(context, input)
}
fun <Context : CommonBackendContext, Element : IrElement> makeCustomPhase(
op: (Context, Element) -> Unit,
private val defaultConditions = setOf(defaultDumper, validationAction)
fun <Context : CommonBackendContext> makeCustomPhase(
op: (Context, IrModuleFragment) -> Unit,
name: String,
description: String,
prerequisite: Set<AbstractNamedCompilerPhase<Context, *, *>> = emptySet(),
preconditions: Set<Checker<Element>> = emptySet(),
postconditions: Set<Checker<Element>> = emptySet(),
stickyPostconditions: Set<Checker<Element>> = emptySet(),
actions: Set<Action<Element, Context>> = setOf(defaultDumper, validationAction),
nlevels: Int = 1
): SameTypeNamedCompilerPhase<Context, Element> =
SameTypeNamedCompilerPhase(
name, description, prerequisite, CustomPhaseAdapter(op), preconditions, postconditions, stickyPostconditions, actions, nlevels,
preconditions: Set<Action<IrModuleFragment, Context>> = emptySet(),
postconditions: Set<Action<IrModuleFragment, Context>> = emptySet(),
): SimpleNamedCompilerPhase<Context, IrModuleFragment, IrModuleFragment> =
createSimpleNamedCompilerPhase(
name = name,
description = description,
preactions = defaultConditions + preconditions,
postactions = defaultConditions + postconditions,
prerequisite = prerequisite,
outputIfNotEnabled = { _, _, _, irModule -> irModule },
op = { context, irModule ->
op(context, irModule)
irModule
},
)
private class CustomPhaseAdapter<Context : CommonBackendContext, Element>(
private val op: (Context, Element) -> Unit
) : SameTypeCompilerPhase<Context, Element> {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<Element>, context: Context, input: Element): Element {
op(context, input)
return input
}
}
fun <Context : CommonBackendContext> makeIrFilePhase(
lowering: (Context) -> FileLoweringPass,
name: String,
description: String,
prerequisite: Set<AbstractNamedCompilerPhase<Context, *, *>> = emptySet(),
preconditions: Set<Checker<IrFile>> = emptySet(),
postconditions: Set<Checker<IrFile>> = emptySet(),
stickyPostconditions: Set<Checker<IrFile>> = emptySet(),
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction)
): SameTypeNamedCompilerPhase<Context, IrFile> =
SameTypeNamedCompilerPhase(
name, description, prerequisite, FileLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions,
nlevels = 0,
preconditions: Set<Action<IrFile, Context>> = emptySet(),
postconditions: Set<Action<IrFile, Context>> = emptySet(),
): SimpleNamedCompilerPhase<Context, IrFile, IrFile> =
createSimpleNamedCompilerPhase(
name = name,
description = description,
preactions = defaultConditions + preconditions,
postactions = defaultConditions + postconditions,
prerequisite = prerequisite,
outputIfNotEnabled = { _, _, _, irFile -> irFile },
op = { context, irFile ->
lowering(context).lower(irFile)
irFile
},
)
private class FileLoweringPhaseAdapter<Context : CommonBackendContext>(
private val lowering: (Context) -> FileLoweringPass
) : SameTypeCompilerPhase<Context, IrFile> {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrFile>, context: Context, input: IrFile): IrFile {
lowering(context).lower(input)
return input
}
}
fun <Context : CommonBackendContext> makeIrModulePhase(
lowering: (Context) -> FileLoweringPass,
name: String,
description: String,
prerequisite: Set<AbstractNamedCompilerPhase<Context, *, *>> = emptySet(),
preconditions: Set<Checker<IrModuleFragment>> = emptySet(),
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
stickyPostconditions: Set<Checker<IrModuleFragment>> = emptySet(),
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction)
): SameTypeNamedCompilerPhase<Context, IrModuleFragment> =
SameTypeNamedCompilerPhase(
name, description, prerequisite, ModuleLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions,
nlevels = 0,
preconditions: Set<Action<IrModuleFragment, Context>> = emptySet(),
postconditions: Set<Action<IrModuleFragment, Context>> = emptySet(),
): SimpleNamedCompilerPhase<Context, IrModuleFragment, IrModuleFragment> =
createSimpleNamedCompilerPhase(
name = name,
description = description,
preactions = defaultConditions + preconditions,
postactions = defaultConditions + postconditions,
prerequisite = prerequisite,
outputIfNotEnabled = { _, _, _, irModule -> irModule },
op = { context, irModule ->
lowering(context).lower(irModule)
irModule
},
)
private class ModuleLoweringPhaseAdapter<Context : CommonBackendContext>(
private val lowering: (Context) -> FileLoweringPass
) : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(
phaseConfig: PhaseConfigurationService, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
): IrModuleFragment {
lowering(context).lower(input)
return input
}
}
@@ -18,7 +18,6 @@ import org.jetbrains.kotlin.backend.jvm.ir.shouldContainSuspendMarkers
import org.jetbrains.kotlin.backend.jvm.lower.*
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
@@ -26,8 +25,6 @@ import org.jetbrains.kotlin.ir.util.PatchDeclarationParentsVisitor
import org.jetbrains.kotlin.ir.util.isAnonymousObject
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.util.resolveFakeOverride
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.load.java.JavaDescriptorVisibilities
import org.jetbrains.kotlin.name.NameUtils
@@ -35,7 +32,7 @@ import org.jetbrains.kotlin.name.NameUtils
private var patchParentPhases = 0
@Suppress("unused")
private fun makePatchParentsPhase(): SameTypeNamedCompilerPhase<CommonBackendContext, IrFile> {
private fun makePatchParentsPhase(): SimpleNamedCompilerPhase<CommonBackendContext, IrFile, IrFile> {
val number = patchParentPhases++
return makeIrFilePhase(
{ PatchDeclarationParents() },
@@ -47,7 +44,7 @@ private fun makePatchParentsPhase(): SameTypeNamedCompilerPhase<CommonBackendCon
private var checkParentPhases = 0
@Suppress("unused")
private fun makeCheckParentsPhase(): SameTypeNamedCompilerPhase<CommonBackendContext, IrFile> {
private fun makeCheckParentsPhase(): SimpleNamedCompilerPhase<CommonBackendContext, IrFile, IrFile> {
val number = checkParentPhases++
return makeIrFilePhase(
{ CheckDeclarationParents() },
@@ -452,7 +449,7 @@ val jvmLoweringPhases = buildJvmLoweringPhases("IrLowering", listOf("PerformByIr
private fun buildJvmLoweringPhases(
name: String,
phases: List<Pair<String, List<SameTypeNamedCompilerPhase<JvmBackendContext, IrFile>>>>,
phases: List<Pair<String, List<SimpleNamedCompilerPhase<JvmBackendContext, IrFile, IrFile>>>>,
): SameTypeNamedCompilerPhase<JvmBackendContext, IrModuleFragment> {
return SameTypeNamedCompilerPhase(
name = name,
@@ -489,7 +486,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<Pair<String, List<SameTypeNamedCompilerPhase<JvmBackendContext, IrFile>>>>,
perModuleLowerings: List<Pair<String, List<SimpleNamedCompilerPhase<JvmBackendContext, IrFile, IrFile>>>>,
): CompilerPhase<JvmBackendContext, IrModuleFragment, IrModuleFragment> =
perModuleLowerings.map { (name, lowerings) -> performByIrFile(name, lower = lowerings) }
.reduce<
@@ -42,7 +42,7 @@ import org.jetbrains.kotlin.name.JvmStandardClassIds.JVM_SYNTHETIC_ANNOTATION_FQ
import org.jetbrains.kotlin.resolve.inline.INLINE_ONLY_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmBackendErrors
internal val generateMultifileFacadesPhase = makeCustomPhase<JvmBackendContext, IrModuleFragment>(
internal val generateMultifileFacadesPhase = makeCustomPhase(
name = "GenerateMultifileFacades",
description = "Generate JvmMultifileClass facades, based on the information provided by FileClassLowering",
prerequisite = setOf(fileClassPhase),
@@ -47,12 +47,10 @@ import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.topologicalSort
internal val scriptsToClassesPhase = makeCustomPhase<JvmBackendContext, IrModuleFragment>(
internal val scriptsToClassesPhase = makeCustomPhase<JvmBackendContext>(
op = { context, irModule -> ScriptsToClassesLowering(context, context.innerClassesSupport).lower(irModule) },
name = "ScriptsToClasses",
description = "Put script declarations into classes",
op = { context, input ->
ScriptsToClassesLowering(context, context.innerClassesSupport).lower(input)
}
)
@@ -6,17 +6,15 @@
package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.phaser.makeCustomPhase
import org.jetbrains.kotlin.backend.common.phaser.makeIrModulePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.expectDeclarationsRemovingPhase
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.MetadataSource
internal val serializeIrPhase = makeCustomPhase<JvmBackendContext, IrModuleFragment>(
{ context, irModule -> SerializeIrPhase(context).lower(irModule) },
internal val serializeIrPhase = makeIrModulePhase(
lowering = ::SerializeIrPhase,
name = "SerializeIr",
description = "If specified by compiler options, save serialized IR in class annotations",
prerequisite = setOf(expectDeclarationsRemovingPhase),