Introduce FlexiblePhaseConfig

For dynamic Kotlin/Native driver we need a PhaseConfigurationService
that does not force us to provide a list of phases upfront.
This commit is contained in:
Sergey Bogolepov
2022-10-14 15:56:42 +03:00
committed by Space Team
parent cd3dece726
commit c22cad07ed
5 changed files with 89 additions and 0 deletions
@@ -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<PhaseConfig> PHASE_CONFIG =
CompilerConfigurationKey.create("phase configuration");
public static final CompilerConfigurationKey<PhaseConfigurationService> FLEXIBLE_PHASE_CONFIG =
CompilerConfigurationKey.create("flexible phase configuration");
public static final CompilerConfigurationKey<Integer> REPEAT_COMPILE_MODULES =
CompilerConfigurationKey.create("debug key for profiling, repeats compileModules");
@@ -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<String>?.asNonNullSet(): Set<String> = 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,
)
}
@@ -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<String>,
private val verbose: Set<String>,
private val toDumpStateBefore: Set<String>,
private val toDumpStateAfter: Set<String>,
private val toValidateStateBefore: Set<String>,
private val toValidateStateAfter: Set<String>,
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
}
@@ -67,7 +67,11 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
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) {
@@ -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)