From c22cad07edd33550d66e34055d18ff6ba68cac0c Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Fri, 14 Oct 2022 15:56:42 +0300 Subject: [PATCH] Introduce FlexiblePhaseConfig For dynamic Kotlin/Native driver we need a PhaseConfigurationService that does not force us to provide a list of phases upfront. --- .../cli/common/CLIConfigurationKeys.java | 4 ++ .../cli/common/createFlexiblePhaseConfig.kt | 32 +++++++++++++ .../common/phaser/FlexiblePhaseConfig.kt | 48 +++++++++++++++++++ .../org/jetbrains/kotlin/cli/bc/K2Native.kt | 4 ++ .../kotlin/backend/konan/KonanConfig.kt | 1 + 5 files changed, 89 insertions(+) create mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/common/createFlexiblePhaseConfig.kt create mode 100644 compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/FlexiblePhaseConfig.kt diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLIConfigurationKeys.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLIConfigurationKeys.java index ec4200ee20b..0a524366515 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLIConfigurationKeys.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLIConfigurationKeys.java @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.cli.common; import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig; +import org.jetbrains.kotlin.backend.common.phaser.PhaseConfigurationService; import org.jetbrains.kotlin.cli.common.config.ContentRoot; import org.jetbrains.kotlin.cli.common.messages.MessageCollector; import org.jetbrains.kotlin.config.CompilerConfigurationKey; @@ -56,6 +57,9 @@ public class CLIConfigurationKeys { public static final CompilerConfigurationKey PHASE_CONFIG = CompilerConfigurationKey.create("phase configuration"); + public static final CompilerConfigurationKey FLEXIBLE_PHASE_CONFIG = + CompilerConfigurationKey.create("flexible phase configuration"); + public static final CompilerConfigurationKey REPEAT_COMPILE_MODULES = CompilerConfigurationKey.create("debug key for profiling, repeats compileModules"); diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/createFlexiblePhaseConfig.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/createFlexiblePhaseConfig.kt new file mode 100644 index 00000000000..b5171abc397 --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/createFlexiblePhaseConfig.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.cli.common + +import org.jetbrains.kotlin.backend.common.phaser.FlexiblePhaseConfig +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments + +fun createFlexiblePhaseConfig( + arguments: CommonCompilerArguments, +): FlexiblePhaseConfig { + fun Array?.asNonNullSet(): Set = this?.toSet() ?: emptySet() + + val toDumpBoth = arguments.phasesToDump.asNonNullSet() + val toValidateBoth = arguments.phasesToValidate.asNonNullSet() + + return FlexiblePhaseConfig( + disabled = arguments.disablePhases.asNonNullSet(), + verbose = arguments.verbosePhases.asNonNullSet(), + toDumpStateBefore = arguments.phasesToDumpBefore.asNonNullSet() + toDumpBoth, + toDumpStateAfter = arguments.phasesToDumpAfter.asNonNullSet() + toDumpBoth, + toValidateStateBefore = arguments.phasesToValidateBefore.asNonNullSet() + toValidateBoth, + toValidateStateAfter = arguments.phasesToValidateAfter.asNonNullSet() + toValidateBoth, + dumpOnlyFqName = arguments.dumpOnlyFqName, + dumpToDirectory = arguments.dumpDirectory, + needProfiling = arguments.profilePhases, + checkConditions = arguments.checkPhaseConditions, + checkStickyConditions = arguments.checkStickyPhaseConditions, + ) +} \ No newline at end of file diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/FlexiblePhaseConfig.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/FlexiblePhaseConfig.kt new file mode 100644 index 00000000000..74d8da7be45 --- /dev/null +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/FlexiblePhaseConfig.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.backend.common.phaser + +/** + * Phase configuration that does not know anything + * about actual compiler pipeline upfront. + */ +class FlexiblePhaseConfig( + disabled: Set, + private val verbose: Set, + private val toDumpStateBefore: Set, + private val toDumpStateAfter: Set, + private val toValidateStateBefore: Set, + private val toValidateStateAfter: Set, + override val dumpToDirectory: String? = null, + override val dumpOnlyFqName: String? = null, + override val needProfiling: Boolean = false, + override val checkConditions: Boolean = false, + override val checkStickyConditions: Boolean = false +) : PhaseConfigurationService { + private val disabledMut = disabled.toMutableSet() + + override fun isEnabled(phase: AnyNamedPhase): Boolean = + phase.name !in disabledMut + + override fun isVerbose(phase: AnyNamedPhase): Boolean = + phase.name in verbose + + override fun disable(phase: AnyNamedPhase) { + disabledMut += phase.name + } + + override fun shouldDumpStateBefore(phase: AnyNamedPhase): Boolean = + phase.name in toDumpStateBefore + + override fun shouldDumpStateAfter(phase: AnyNamedPhase): Boolean = + phase.name in toDumpStateAfter + + override fun shouldValidateStateBefore(phase: AnyNamedPhase): Boolean = + phase.name in toValidateStateBefore + + override fun shouldValidateStateAfter(phase: AnyNamedPhase): Boolean = + phase.name in toValidateStateAfter +} diff --git a/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt b/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt index ad8d240c796..bb17872765f 100644 --- a/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt +++ b/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt @@ -67,7 +67,11 @@ class K2Native : CLICompiler() { messageCollector.report(ERROR, "K2 does not support Native target right now") return ExitCode.COMPILATION_ERROR } + // TODO: Should be either of two, or PHASE_CONFIG will go away with the old driver. + // TODO: -Xlist-phases does not work correctly for now. configuration.put(CLIConfigurationKeys.PHASE_CONFIG, createPhaseConfig(toplevelPhaseErased, arguments, messageCollector)) + configuration.put(CLIConfigurationKeys.FLEXIBLE_PHASE_CONFIG, createFlexiblePhaseConfig(arguments)) + val enoughArguments = arguments.freeArgs.isNotEmpty() || arguments.isUsefulWithoutFreeArgs if (!enoughArguments) { diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt index f00b2cbae9e..0410795527e 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt @@ -49,6 +49,7 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration internal val target = targetManager.target val targetHasAddressDependency get() = target.hasAddressDependencyInMemoryModel() internal val phaseConfig = configuration.get(CLIConfigurationKeys.PHASE_CONFIG)!! + internal val flexiblePhaseConfig = configuration.get(CLIConfigurationKeys.FLEXIBLE_PHASE_CONFIG)!! // TODO: debug info generation mode and debug/release variant selection probably requires some refactoring. val debug: Boolean get() = configuration.getBoolean(KonanConfigKeys.DEBUG)