IR: inline namedIrModulePhase and namedIrFilePhase phase builders

Construct NamedCompilerPhase directly instead. Only use phase builders
where the code becomes easier to read.
This commit is contained in:
Alexander Udalov
2020-07-01 02:55:04 +02:00
parent 15a969b3ba
commit 181965f6e8
6 changed files with 43 additions and 107 deletions
@@ -67,7 +67,7 @@ infix operator fun <Data, Context> Action<Data, Context>.plus(other: Action<Data
class NamedCompilerPhase<in Context : CommonBackendContext, Data>(
val name: String,
val description: String,
val prerequisite: Set<NamedCompilerPhase<Context, *>>,
val prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet(),
private val lower: CompilerPhase<Context, Data, Data>,
val preconditions: Set<Checker<Data>> = emptySet(),
val postconditions: Set<Checker<Data>> = emptySet(),
@@ -48,40 +48,10 @@ infix fun <Context : CommonBackendContext, Input, Mid, Output> CompilerPhase<Con
return CompositePhase(if (this is CompositePhase<Context, *, *>) phases + unsafeOther else listOf(unsafeThis, unsafeOther))
}
fun <Context : CommonBackendContext> namedIrModulePhase(
name: String,
description: String,
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet(),
lower: CompilerPhase<Context, IrModuleFragment, IrModuleFragment>,
preconditions: Set<Checker<IrModuleFragment>> = emptySet(),
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
stickyPostconditions: Set<Checker<IrModuleFragment>> = lower.stickyPostconditions,
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction),
nlevels: Int = 1
): NamedCompilerPhase<Context, IrModuleFragment> =
NamedCompilerPhase(
name, description, prerequisite, lower, preconditions, postconditions, stickyPostconditions, actions, nlevels
)
fun <Context : CommonBackendContext> namedIrFilePhase(
name: String,
description: String,
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet(),
lower: CompilerPhase<Context, IrFile, IrFile>,
preconditions: Set<Checker<IrFile>> = emptySet(),
postconditions: Set<Checker<IrFile>> = emptySet(),
stickyPostconditions: Set<Checker<IrFile>> = lower.stickyPostconditions,
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction),
nlevels: Int = 1
): NamedCompilerPhase<Context, IrFile> =
NamedCompilerPhase(
name, description, prerequisite, lower, preconditions, postconditions, stickyPostconditions, actions, nlevels
)
fun <Context : CommonBackendContext, Element : IrElement> makeCustomPhase(
op: (Context, Element) -> Unit,
description: String,
name: String,
description: String,
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet(),
preconditions: Set<Checker<Element>> = emptySet(),
postconditions: Set<Checker<Element>> = emptySet(),
@@ -134,7 +104,7 @@ fun <Context : CommonBackendContext> performByIrFile(
description: String = "Perform phases by IrFile",
lower: List<CompilerPhase<Context, IrFile, IrFile>>
): NamedCompilerPhase<Context, IrModuleFragment> =
namedIrModulePhase(
NamedCompilerPhase(
name, description, emptySet(), PerformByIrFilePhase(lower), emptySet(), emptySet(), emptySet(),
setOf(defaultDumper), nlevels = 1,
)
@@ -173,7 +143,7 @@ fun <Context : CommonBackendContext> makeIrFilePhase(
stickyPostconditions: Set<Checker<IrFile>> = emptySet(),
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction)
): NamedCompilerPhase<Context, IrFile> =
namedIrFilePhase(
NamedCompilerPhase(
name, description, prerequisite, FileLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions,
nlevels = 0,
)
@@ -197,7 +167,7 @@ fun <Context : CommonBackendContext> makeIrModulePhase(
stickyPostconditions: Set<Checker<IrModuleFragment>> = emptySet(),
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction)
): NamedCompilerPhase<Context, IrModuleFragment> =
namedIrModulePhase(
NamedCompilerPhase(
name, description, prerequisite, ModuleLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions,
nlevels = 0,
)
@@ -34,7 +34,7 @@ private fun makeJsModulePhase(
name: String,
description: String,
prerequisite: Set<NamedCompilerPhase<JsIrBackendContext, *>> = emptySet()
) = makeCustomJsModulePhase(
): NamedCompilerPhase<JsIrBackendContext, Iterable<IrModuleFragment>> = makeCustomJsModulePhase(
op = { context, modules -> lowering(context).lower(modules) },
name = name,
description = description,
@@ -46,7 +46,7 @@ private fun makeCustomJsModulePhase(
description: String,
name: String,
prerequisite: Set<NamedCompilerPhase<JsIrBackendContext, *>> = emptySet()
) = NamedCompilerPhase(
): NamedCompilerPhase<JsIrBackendContext, Iterable<IrModuleFragment>> = NamedCompilerPhase(
name = name,
description = description,
prerequisite = prerequisite,
@@ -63,11 +63,7 @@ private fun makeCustomJsModulePhase(
return input
}
},
preconditions = emptySet(),
postconditions = emptySet(),
stickyPostconditions = emptySet(),
actions = setOf(defaultDumper.toMultiModuleAction(), validationAction.toMultiModuleAction()),
nlevels = 0
)
private fun <C> Action<IrElement, C>.toMultiModuleAction(): Action<Iterable<IrModuleFragment>, C> {
@@ -735,12 +731,10 @@ val pirLowerings = loweringList.filter { it is DeclarationLowering || it is Body
val jsPhases = NamedCompilerPhase(
name = "IrModuleLowering",
description = "IR module lowering",
prerequisite = emptySet(),
lower = loweringList.map { it.modulePhase as CompilerPhase<JsIrBackendContext, Iterable<IrModuleFragment>, Iterable<IrModuleFragment>> }
.reduce { acc, lowering -> acc.then(lowering) },
preconditions = emptySet(),
postconditions = emptySet(),
stickyPostconditions = emptySet(),
lower = loweringList.map {
@Suppress("USELESS_CAST")
it.modulePhase as CompilerPhase<JsIrBackendContext, Iterable<IrModuleFragment>, Iterable<IrModuleFragment>>
}.reduce { acc, lowering -> acc.then(lowering) },
actions = setOf(defaultDumper.toMultiModuleAction(), validationAction.toMultiModuleAction()),
nlevels = 1
)
@@ -28,23 +28,18 @@ import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.load.java.JavaVisibilities
import org.jetbrains.kotlin.name.NameUtils
private fun makePatchParentsPhase(number: Int) = namedIrFilePhase(
lower = object : SameTypeCompilerPhase<CommonBackendContext, IrFile> {
override fun invoke(
phaseConfig: PhaseConfig,
phaserState: PhaserState<IrFile>,
context: CommonBackendContext,
input: IrFile
): IrFile {
input.acceptVoid(PatchDeclarationParentsVisitor())
return input
}
},
private fun makePatchParentsPhase(number: Int): NamedCompilerPhase<CommonBackendContext, IrFile> = makeIrFilePhase(
{ PatchDeclarationParents() },
name = "PatchParents$number",
description = "Patch parent references in IrFile, pass $number",
nlevels = 0
)
private class PatchDeclarationParents : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.acceptVoid(PatchDeclarationParentsVisitor())
}
}
private val validateIrBeforeLowering = makeCustomPhase<JvmBackendContext, IrModuleFragment>(
{ context, module -> validationCallback(context, module, checkProperties = true) },
name = "ValidateIrBeforeLowering",
@@ -374,7 +369,7 @@ private val jvmFilePhases = listOf(
makePatchParentsPhase(3)
)
val jvmPhases = namedIrModulePhase(
val jvmPhases = NamedCompilerPhase(
name = "IrLowering",
description = "IR lowering",
lower = validateIrBeforeLowering then
@@ -12,10 +12,7 @@ import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclaration
import org.jetbrains.kotlin.backend.common.ir.passTypeArgumentsFrom
import org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
import org.jetbrains.kotlin.backend.common.phaser.PhaserState
import org.jetbrains.kotlin.backend.common.phaser.SameTypeCompilerPhase
import org.jetbrains.kotlin.backend.common.phaser.namedIrModulePhase
import org.jetbrains.kotlin.backend.common.phaser.makeCustomPhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.codegen.fileParent
@@ -45,35 +42,26 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.inline.INLINE_ONLY_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
internal val generateMultifileFacadesPhase = namedIrModulePhase(
internal val generateMultifileFacadesPhase = makeCustomPhase<JvmBackendContext, IrModuleFragment>(
name = "GenerateMultifileFacades",
description = "Generate JvmMultifileClass facades, based on the information provided by FileClassLowering",
prerequisite = setOf(fileClassPhase),
lower = object : SameTypeCompilerPhase<JvmBackendContext, IrModuleFragment> {
override fun invoke(
phaseConfig: PhaseConfig,
phaserState: PhaserState<IrModuleFragment>,
context: JvmBackendContext,
input: IrModuleFragment
): IrModuleFragment {
val functionDelegates = mutableMapOf<IrFunction, IrFunction>()
op = { context, input ->
val functionDelegates = mutableMapOf<IrFunction, IrFunction>()
// In -Xmultifile-parts-inherit mode, instead of generating "bridge" methods in the facade which call into parts,
// we construct an inheritance chain such that all part members are present as fake overrides in the facade.
val shouldGeneratePartHierarchy = context.state.languageVersionSettings.getFlag(JvmAnalysisFlags.inheritMultifileParts)
input.files.addAll(
generateMultifileFacades(input.descriptor, context, shouldGeneratePartHierarchy, functionDelegates)
)
// In -Xmultifile-parts-inherit mode, instead of generating "bridge" methods in the facade which call into parts,
// we construct an inheritance chain such that all part members are present as fake overrides in the facade.
val shouldGeneratePartHierarchy = context.state.languageVersionSettings.getFlag(JvmAnalysisFlags.inheritMultifileParts)
input.files.addAll(
generateMultifileFacades(input.descriptor, context, shouldGeneratePartHierarchy, functionDelegates)
)
UpdateFunctionCallSites(functionDelegates).lower(input)
UpdateConstantFacadePropertyReferences(context, shouldGeneratePartHierarchy).lower(input)
UpdateFunctionCallSites(functionDelegates).lower(input)
UpdateConstantFacadePropertyReferences(context, shouldGeneratePartHierarchy).lower(input)
context.multifileFacadesToAdd.clear()
context.multifileFacadesToAdd.clear()
functionDelegates.entries.associateTo(context.multifileFacadeMemberToPartMember) { (member, newMember) -> newMember to member }
return input
}
functionDelegates.entries.associateTo(context.multifileFacadeMemberToPartMember) { (member, newMember) -> newMember to member }
}
)
@@ -27,31 +27,20 @@ private fun makeWasmModulePhase(
name: String,
description: String,
prerequisite: Set<NamedCompilerPhase<WasmBackendContext, *>> = emptySet()
) = makeIrModulePhase(lowering, name, description, prerequisite, actions = setOf(validationAction, defaultDumper))
): NamedCompilerPhase<WasmBackendContext, IrModuleFragment> =
makeIrModulePhase(
lowering, name, description, prerequisite, actions = setOf(validationAction, defaultDumper)
)
private fun makeCustomWasmModulePhase(
op: (WasmBackendContext, IrModuleFragment) -> Unit,
description: String,
name: String,
prerequisite: Set<NamedCompilerPhase<WasmBackendContext, *>> = emptySet()
) = namedIrModulePhase(
name,
description,
prerequisite,
actions = setOf(defaultDumper, validationAction),
nlevels = 0,
lower = object : SameTypeCompilerPhase<WasmBackendContext, IrModuleFragment> {
override fun invoke(
phaseConfig: PhaseConfig,
phaserState: PhaserState<IrModuleFragment>,
context: WasmBackendContext,
input: IrModuleFragment
): IrModuleFragment {
op(context, input)
return input
}
}
)
): NamedCompilerPhase<WasmBackendContext, IrModuleFragment> =
makeCustomPhase(
op, name, description, prerequisite, actions = setOf(defaultDumper, validationAction), nlevels = 0,
)
private val validateIrBeforeLowering = makeCustomWasmModulePhase(
{ context, module -> validationCallback(context, module) },
@@ -351,7 +340,7 @@ private val objectUsageLoweringPhase = makeWasmModulePhase(
description = "Transform IrGetObjectValue into instance generator call"
)
val wasmPhases = namedIrModulePhase<WasmBackendContext>(
val wasmPhases = NamedCompilerPhase(
name = "IrModuleLowering",
description = "IR module lowering",
lower = validateIrBeforeLowering then