From 4475325ffae917c81283efeefea8d47703bb5d97 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 1 Jul 2020 02:21:56 +0200 Subject: [PATCH] IR: refactor PhaseBuilders a little - use named classes to improve stacktraces - reorder parameters to make the code shorter - use explicit types to improve IDE resolve in usages --- .../backend/common/phaser/PhaseBuilders.kt | 190 ++++++++---------- 1 file changed, 81 insertions(+), 109 deletions(-) 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 365881896a3..b194103ce4d 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 @@ -58,17 +58,10 @@ fun namedIrModulePhase( stickyPostconditions: Set> = lower.stickyPostconditions, actions: Set> = setOf(defaultDumper, validationAction), nlevels: Int = 1 -) = NamedCompilerPhase( - name, - description, - prerequisite, - lower, - preconditions, - postconditions, - stickyPostconditions, - actions, - nlevels -) +): NamedCompilerPhase = + NamedCompilerPhase( + name, description, prerequisite, lower, preconditions, postconditions, stickyPostconditions, actions, nlevels + ) fun namedIrFilePhase( name: String, @@ -80,17 +73,10 @@ fun namedIrFilePhase( stickyPostconditions: Set> = lower.stickyPostconditions, actions: Set> = setOf(defaultDumper, validationAction), nlevels: Int = 1 -) = NamedCompilerPhase( - name, - description, - prerequisite, - lower, - preconditions, - postconditions, - stickyPostconditions, - actions, - nlevels -) +): NamedCompilerPhase = + NamedCompilerPhase( + name, description, prerequisite, lower, preconditions, postconditions, stickyPostconditions, actions, nlevels + ) fun makeCustomPhase( op: (Context, Element) -> Unit, @@ -102,27 +88,19 @@ fun makeCustomPhase( stickyPostconditions: Set> = emptySet(), actions: Set> = setOf(defaultDumper, validationAction), nlevels: Int = 1 -) = NamedCompilerPhase( - name, - description, - prerequisite, - preconditions = preconditions, - postconditions = postconditions, - stickyPostconditions = stickyPostconditions, - actions = actions, - nlevels = nlevels, - lower = object : SameTypeCompilerPhase { - override fun invoke( - phaseConfig: PhaseConfig, - phaserState: PhaserState, - context: Context, - input: Element - ): Element { - op(context, input) - return input - } +): NamedCompilerPhase = + NamedCompilerPhase( + name, description, prerequisite, CustomPhaseAdapter(op), preconditions, postconditions, stickyPostconditions, actions, nlevels, + ) + +private class CustomPhaseAdapter( + private val op: (Context, Element) -> Unit +) : SameTypeCompilerPhase { + override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Element): Element { + op(context, input) + return input } -) +} fun namedUnitPhase( name: String, @@ -130,11 +108,10 @@ fun namedUnitPhase( prerequisite: Set> = emptySet(), nlevels: Int = 1, lower: CompilerPhase -) = NamedCompilerPhase( - name, description, prerequisite, - lower = lower, - nlevels = nlevels -) +): NamedCompilerPhase = + NamedCompilerPhase( + name, description, prerequisite, lower, nlevels = nlevels + ) @Suppress("unused") // Used in kotlin-native fun namedOpUnitPhase( @@ -142,7 +119,7 @@ fun namedOpUnitPhase( description: String, prerequisite: Set>, op: Context.() -> Unit -) = namedUnitPhase( +): NamedCompilerPhase = namedUnitPhase( name, description, prerequisite, nlevels = 0, lower = object : SameTypeCompilerPhase { @@ -161,35 +138,32 @@ fun performByIrFile( stickyPostconditions: Set> = emptySet(), actions: Set> = setOf(defaultDumper), lower: CompilerPhase -) = namedIrModulePhase( - name, description, prerequisite, - preconditions = preconditions, - postconditions = postconditions, - stickyPostconditions = stickyPostconditions, - actions = actions, - nlevels = 1, - lower = object : SameTypeCompilerPhase { - override fun invoke( - phaseConfig: PhaseConfig, - phaserState: PhaserState, - context: Context, - input: IrModuleFragment - ): IrModuleFragment { - for (irFile in input.files) { - try { - lower.invoke(phaseConfig, phaserState.changeType(), context, irFile) - } catch (e: Throwable) { - CodegenUtil.reportBackendException(e, "IR lowering", irFile.fileEntry.name) - } - } +): NamedCompilerPhase = + namedIrModulePhase( + name, description, prerequisite, PerformByIrFilePhase(lower), preconditions, postconditions, stickyPostconditions, actions, + nlevels = 1, + ) - // TODO: no guarantee that module identity is preserved by `lower` - return input +private class PerformByIrFilePhase( + private val lower: CompilerPhase +) : SameTypeCompilerPhase { + override fun invoke( + phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrModuleFragment + ): IrModuleFragment { + for (irFile in input.files) { + try { + lower.invoke(phaseConfig, phaserState.changeType(), context, irFile) + } catch (e: Throwable) { + CodegenUtil.reportBackendException(e, "IR lowering", irFile.fileEntry.name) + } } - override fun getNamedSubphases(startDepth: Int) = lower.getNamedSubphases(startDepth) + // TODO: no guarantee that module identity is preserved by `lower` + return input } -) + + override fun getNamedSubphases(startDepth: Int) = lower.getNamedSubphases(startDepth) +} fun makeIrFilePhase( lowering: (Context) -> FileLoweringPass, @@ -200,20 +174,20 @@ fun makeIrFilePhase( postconditions: Set> = emptySet(), stickyPostconditions: Set> = emptySet(), actions: Set> = setOf(defaultDumper, validationAction) -) = namedIrFilePhase( - name, description, prerequisite, - preconditions = preconditions, - postconditions = postconditions, - stickyPostconditions = stickyPostconditions, - actions = actions, - nlevels = 0, - lower = object : SameTypeCompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrFile): IrFile { - lowering(context).lower(input) - return input - } +): NamedCompilerPhase = + namedIrFilePhase( + name, description, prerequisite, FileLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions, + nlevels = 0, + ) + +private class FileLoweringPhaseAdapter( + private val lowering: (Context) -> FileLoweringPass +) : SameTypeCompilerPhase { + override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrFile): IrFile { + lowering(context).lower(input) + return input } -) +} fun makeIrModulePhase( lowering: (Context) -> FileLoweringPass, @@ -224,39 +198,37 @@ fun makeIrModulePhase( postconditions: Set> = emptySet(), stickyPostconditions: Set> = emptySet(), actions: Set> = setOf(defaultDumper, validationAction) -) = namedIrModulePhase( - name, description, prerequisite, - preconditions = preconditions, - postconditions = postconditions, - stickyPostconditions = stickyPostconditions, - actions = actions, - nlevels = 0, - lower = object : SameTypeCompilerPhase { - override fun invoke( - phaseConfig: PhaseConfig, - phaserState: PhaserState, - context: Context, - input: IrModuleFragment - ): IrModuleFragment { - lowering(context).lower(input) - return input - } +): NamedCompilerPhase = + namedIrModulePhase( + name, description, prerequisite, ModuleLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions, + nlevels = 0, + ) + +private class ModuleLoweringPhaseAdapter( + private val lowering: (Context) -> FileLoweringPass +) : SameTypeCompilerPhase { + override fun invoke( + phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: IrModuleFragment + ): IrModuleFragment { + lowering(context).lower(input) + return input } -) +} @Suppress("unused") // Used in kotlin-native -fun unitSink() = object : CompilerPhase { - override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Input) {} -} +fun unitSink(): CompilerPhase = + object : CompilerPhase { + override fun invoke(phaseConfig: PhaseConfig, 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) = +fun takeFromContext(op: (Context) -> NewData): CompilerPhase = object : CompilerPhase { override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: OldData) = op(context) } -fun transform(op: (OldData) -> NewData) = +fun transform(op: (OldData) -> NewData): CompilerPhase = object : CompilerPhase { override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: OldData) = op(input) }