Use CLI compiler arguments directly in PhaseConfig creation

This commit is contained in:
Georgy Bronnikov
2019-03-21 17:18:07 +03:00
parent 40079f7cae
commit fae003866b
16 changed files with 105 additions and 112 deletions
@@ -232,6 +232,30 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
)
var phasesToDump: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xexclude-from-dumping",
description = "Names of elements that should not be dumped"
)
var namesExcludedFromDumping: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xphases-to-validate-before",
description = "Validate backend state before these phases"
)
var phasesToValidateBefore: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xphases-to-validate-after",
description = "Validate backend state after these phases"
)
var phasesToValidateAfter: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xphases-to-validate",
description = "Validate backend state both before and after these phases"
)
var phasesToValidate: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xprofile-phases",
description = "Profile backend phases"
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.cli.common;
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig;
import org.jetbrains.kotlin.cli.common.config.ContentRoot;
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
import org.jetbrains.kotlin.config.CompilerConfigurationKey;
@@ -44,6 +45,9 @@ public class CLIConfigurationKeys {
public static final CompilerConfigurationKey<File> METADATA_DESTINATION_DIRECTORY =
CompilerConfigurationKey.create("metadata destination directory");
public static final CompilerConfigurationKey<PhaseConfig> PHASE_CONFIG =
CompilerConfigurationKey.create("phase configuration");
private CLIConfigurationKeys() {
}
}
@@ -39,22 +39,6 @@ fun <A : CommonCompilerArguments> CompilerConfiguration.setupCommonArguments(
}
setupLanguageVersionSettings(arguments)
put(CommonConfigurationKeys.LIST_PHASES, arguments.listPhases)
listOf(
CommonConfigurationKeys.DISABLED_PHASES to arguments.disablePhases,
CommonConfigurationKeys.VERBOSE_PHASES to arguments.verbosePhases,
CommonConfigurationKeys.PHASES_TO_DUMP_STATE_BEFORE to arguments.phasesToDumpBefore,
CommonConfigurationKeys.PHASES_TO_DUMP_STATE_AFTER to arguments.phasesToDumpAfter,
CommonConfigurationKeys.PHASES_TO_DUMP_STATE to arguments.phasesToDump
).forEach { (k, v) ->
if (v != null) put(k, setOf(*v))
}
put(CommonConfigurationKeys.PROFILE_PHASES, arguments.profilePhases)
put(CommonConfigurationKeys.CHECK_PHASE_CONDITIONS, arguments.checkPhaseConditions or arguments.checkStickyPhaseConditions)
put(CommonConfigurationKeys.CHECK_STICKY_CONDITIONS, arguments.checkStickyPhaseConditions)
}
fun <A : CommonCompilerArguments> CompilerConfiguration.setupLanguageVersionSettings(arguments: A) {
@@ -9,62 +9,69 @@ import org.jetbrains.kotlin.backend.common.phaser.AnyNamedPhase
import org.jetbrains.kotlin.backend.common.phaser.CompilerPhase
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
import org.jetbrains.kotlin.backend.common.phaser.toPhaseMap
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
fun createPhaseConfig(compoundPhase: CompilerPhase<*, *, *>, config: CompilerConfiguration): PhaseConfig {
fun createPhaseConfig(
compoundPhase: CompilerPhase<*, *, *>,
arguments: CommonCompilerArguments,
messageCollector: MessageCollector
): PhaseConfig {
fun warn(message: String) = messageCollector.report(CompilerMessageSeverity.WARNING, message)
val phases = compoundPhase.toPhaseMap()
val enabled = computeEnabled(phases, config).toMutableSet()
val verbose = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.VERBOSE_PHASES)
val enabled = computeEnabled(phases, arguments.disablePhases, ::warn).toMutableSet()
val verbose = phaseSetFromArguments(phases, arguments.verbosePhases, ::warn)
val beforeDumpSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_DUMP_STATE_BEFORE)
val afterDumpSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_DUMP_STATE_AFTER)
val bothDumpSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_DUMP_STATE)
val beforeDumpSet = phaseSetFromArguments(phases, arguments.phasesToDumpBefore, ::warn)
val afterDumpSet = phaseSetFromArguments(phases, arguments.phasesToDumpAfter, ::warn)
val bothDumpSet = phaseSetFromArguments(phases, arguments.phasesToDump, ::warn)
val toDumpStateBefore = beforeDumpSet + bothDumpSet
val toDumpStateAfter = afterDumpSet + bothDumpSet
val beforeValidateSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_VALIDATE_BEFORE)
val afterValidateSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_VALIDATE_AFTER)
val bothValidateSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_VALIDATE)
val beforeValidateSet = phaseSetFromArguments(phases, arguments.phasesToValidateBefore, ::warn)
val afterValidateSet = phaseSetFromArguments(phases, arguments.phasesToValidateAfter, ::warn)
val bothValidateSet = phaseSetFromArguments(phases, arguments.phasesToValidate, ::warn)
val toValidateStateBefore = beforeValidateSet + bothValidateSet
val toValidateStateAfter = afterValidateSet + bothValidateSet
val needProfiling = config.getBoolean(CommonConfigurationKeys.PROFILE_PHASES)
val checkConditions = config.getBoolean(CommonConfigurationKeys.CHECK_PHASE_CONDITIONS)
val checkStickyConditions = config.getBoolean(CommonConfigurationKeys.CHECK_STICKY_CONDITIONS)
val namesOfElementsExcludedFromDumping = arguments.namesExcludedFromDumping?.toSet() ?: emptySet()
val needProfiling = arguments.profilePhases
val checkConditions = arguments.checkPhaseConditions
val checkStickyConditions = arguments.checkStickyPhaseConditions
return PhaseConfig(
compoundPhase, phases, enabled, verbose, toDumpStateBefore, toDumpStateAfter, toValidateStateBefore, toValidateStateAfter,
namesOfElementsExcludedFromDumping,
needProfiling, checkConditions, checkStickyConditions
)
).also {
if (arguments.listPhases) {
it.list()
}
}
}
private fun computeEnabled(
phases: MutableMap<String, AnyNamedPhase>,
config: CompilerConfiguration
namesOfDisabled: Array<String>?,
warn: (String) -> Unit
): Set<AnyNamedPhase> {
val disabledPhases = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.DISABLED_PHASES)
val disabledPhases = phaseSetFromArguments(phases, namesOfDisabled, warn)
return phases.values.toSet() - disabledPhases
}
private fun phaseSetFromConfiguration(
private fun phaseSetFromArguments(
phases: MutableMap<String, AnyNamedPhase>,
config: CompilerConfiguration,
key: CompilerConfigurationKey<Set<String>>
names: Array<String>?,
warn: (String) -> Unit
): Set<AnyNamedPhase> {
val phaseNames = config.get(key) ?: emptySet()
if ("ALL" in phaseNames) return phases.values.toSet()
return phaseNames.mapNotNull {
if (names == null) return emptySet()
if ("ALL" in names) return phases.values.toSet()
return names.mapNotNull {
phases[it] ?: run {
warn(config, "no phase named $it, ignoring")
warn("no phase named $it, ignoring")
null
}
}.toSet()
}
private fun warn(config: CompilerConfiguration, message: String) {
val messageCollector = config.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) ?: MessageCollector.NONE
messageCollector.report(CompilerMessageSeverity.WARNING, message)
}
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.cli.jvm
import com.intellij.ide.highlighter.JavaFileType
import com.intellij.openapi.Disposable
import org.jetbrains.kotlin.backend.jvm.jvmPhases
import org.jetbrains.kotlin.cli.common.*
import org.jetbrains.kotlin.cli.common.ExitCode.*
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
@@ -62,6 +63,8 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
): ExitCode {
val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
configuration.put(CLIConfigurationKeys.PHASE_CONFIG, createPhaseConfig(jvmPhases, arguments, messageCollector))
if (!configuration.configureJdkHome(arguments)) return COMPILATION_ERROR
configuration.put(JVMConfigurationKeys.DISABLE_STANDARD_SCRIPT_DEFINITION, arguments.disableStandardScript)
@@ -28,13 +28,13 @@ import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.asJava.FilteredJvmDiagnostics
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection
import org.jetbrains.kotlin.backend.common.output.SimpleOutputFileCollection
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory
import org.jetbrains.kotlin.backend.jvm.jvmPhases
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.checkKotlinPackageUsage
import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoot
import org.jetbrains.kotlin.cli.common.createPhaseConfig
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.OUTPUT
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.WARNING
@@ -441,7 +441,11 @@ object KotlinToJVMBytecodeCompiler {
sourceFiles,
configuration
)
.codegenFactory(if (isIR) JvmIrCodegenFactory(createPhaseConfig(jvmPhases, configuration)) else DefaultCodegenFactory)
.codegenFactory(
if (isIR) JvmIrCodegenFactory(
configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases)
) else DefaultCodegenFactory
)
.withModule(module)
.onIndependentPartCompilationEnd(createOutputFilesFlushingCallbackIfPossible(configuration))
.build()