Disable backend codegen during frontend-only compilation

This commit is contained in:
Sergey Igushkin
2019-12-11 18:02:18 +03:00
parent 4f9a60efbc
commit 01aec32596
6 changed files with 53 additions and 17 deletions
@@ -140,6 +140,8 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
val outputKind = CompilerOutputKind.valueOf( val outputKind = CompilerOutputKind.valueOf(
(arguments.produce ?: "program").toUpperCase()) (arguments.produce ?: "program").toUpperCase())
put(PRODUCE, outputKind) put(PRODUCE, outputKind)
put(METADATA_KLIB, arguments.metadataKlib)
arguments.libraryVersion ?. let { put(LIBRARY_VERSION, it) } arguments.libraryVersion ?. let { put(LIBRARY_VERSION, it) }
arguments.mainPackage ?.let{ put(ENTRY, it) } arguments.mainPackage ?.let{ put(ENTRY, it) }
@@ -245,6 +245,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
@Argument(value="-Xallocator", valueDescription = "std | mimalloc", description = "Allocator used in runtime") @Argument(value="-Xallocator", valueDescription = "std | mimalloc", description = "Allocator used in runtime")
var allocator: String = "std" var allocator: String = "std"
@Argument(value = "-Xmetadata-klib", description = "Produce a klib that only contains the declarations metadata")
var metadataKlib: Boolean = false
override fun configureAnalysisFlags(collector: MessageCollector): MutableMap<AnalysisFlag<*>, Any> = override fun configureAnalysisFlags(collector: MessageCollector): MutableMap<AnalysisFlag<*>, Any> =
super.configureAnalysisFlags(collector).also { super.configureAnalysisFlags(collector).also {
val useExperimental = it[AnalysisFlags.useExperimental] as List<*> val useExperimental = it[AnalysisFlags.useExperimental] as List<*>
@@ -64,6 +64,8 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
internal val produce get() = configuration.get(KonanConfigKeys.PRODUCE)!! internal val produce get() = configuration.get(KonanConfigKeys.PRODUCE)!!
internal val metadataKlib get() = configuration.get(KonanConfigKeys.METADATA_KLIB)!!
internal val produceStaticFramework get() = configuration.getBoolean(KonanConfigKeys.STATIC_FRAMEWORK) internal val produceStaticFramework get() = configuration.getBoolean(KonanConfigKeys.STATIC_FRAMEWORK)
internal val purgeUserLibs: Boolean internal val purgeUserLibs: Boolean
@@ -66,6 +66,8 @@ class KonanConfigKeys {
= CompilerConfigurationKey.create("memory model") = CompilerConfigurationKey.create("memory model")
val META_INFO: CompilerConfigurationKey<List<String>> val META_INFO: CompilerConfigurationKey<List<String>>
= CompilerConfigurationKey.create("generate metadata") = CompilerConfigurationKey.create("generate metadata")
val METADATA_KLIB: CompilerConfigurationKey<Boolean>
= CompilerConfigurationKey.create("metadata klib")
val MODULE_KIND: CompilerConfigurationKey<ModuleKind> val MODULE_KIND: CompilerConfigurationKey<ModuleKind>
= CompilerConfigurationKey.create("module kind") = CompilerConfigurationKey.create("module kind")
val MODULE_NAME: CompilerConfigurationKey<String?> val MODULE_NAME: CompilerConfigurationKey<String?>
@@ -267,9 +267,11 @@ internal val serializerPhase = konanUnitPhase(
val mppKlibs = config.configuration.get(CommonConfigurationKeys.KLIB_MPP)?:false val mppKlibs = config.configuration.get(CommonConfigurationKeys.KLIB_MPP)?:false
val descriptorTable = DescriptorTable.createDefault() val descriptorTable = DescriptorTable.createDefault()
serializedIr = KonanIrModuleSerializer( serializedIr = irModule?.let { ir ->
this, irModule!!.irBuiltins, descriptorTable, expectDescriptorToSymbol, skipExpects = !mppKlibs KonanIrModuleSerializer(
).serializedIrModule(irModule!!) this, ir.irBuiltins, descriptorTable, expectDescriptorToSymbol, skipExpects = !mppKlibs
).serializedIrModule(ir)
}
val serializer = KlibMetadataMonolithicSerializer( val serializer = KlibMetadataMonolithicSerializer(
this.config.configuration.languageVersionSettings, this.config.configuration.languageVersionSettings,
@@ -421,6 +423,22 @@ internal val bitcodePhase = namedIrModulePhase(
cStubsPhase cStubsPhase
) )
private val backendCodegen = namedUnitPhase(
name = "Backend codegen",
description = "Backend code generation",
lower = takeFromContext<Context, Unit, IrModuleFragment> { it.irModule!! } then
allLoweringsPhase then // Lower current module first.
dependenciesLowerPhase then // Then lower all libraries in topological order.
// With that we guarantee that inline functions are unlowered while being inlined.
entryPointPhase then
bitcodePhase then
verifyBitcodePhase then
printBitcodePhase then
linkBitcodeDependenciesPhase then
bitcodeOptimizationPhase then
unitSink()
)
// Have to hide Context as type parameter in order to expose toplevelPhase outside of this module. // Have to hide Context as type parameter in order to expose toplevelPhase outside of this module.
val toplevelPhase: CompilerPhase<*, Unit, Unit> = namedUnitPhase( val toplevelPhase: CompilerPhase<*, Unit, Unit> = namedUnitPhase(
name = "Compiler", name = "Compiler",
@@ -436,16 +454,7 @@ val toplevelPhase: CompilerPhase<*, Unit, Unit> = namedUnitPhase(
namedUnitPhase( namedUnitPhase(
name = "Backend", name = "Backend",
description = "All backend", description = "All backend",
lower = takeFromContext<Context, Unit, IrModuleFragment> { it.irModule!! } then lower = backendCodegen then
allLoweringsPhase then // Lower current module first.
dependenciesLowerPhase then // Then lower all libraries in topological order.
// With that we guarantee that inline functions are unlowered while being inlined.
entryPointPhase then
bitcodePhase then
verifyBitcodePhase then
printBitcodePhase then
linkBitcodeDependenciesPhase then
bitcodeOptimizationPhase then
produceOutputPhase then produceOutputPhase then
disposeLLVMPhase then disposeLLVMPhase then
unitSink() unitSink()
@@ -485,5 +494,11 @@ internal fun PhaseConfig.konanPhasesConfig(config: KonanConfig) {
disableUnless(dcePhase, getBoolean(KonanConfigKeys.OPTIMIZATION)) disableUnless(dcePhase, getBoolean(KonanConfigKeys.OPTIMIZATION))
disableUnless(ghaPhase, getBoolean(KonanConfigKeys.OPTIMIZATION)) disableUnless(ghaPhase, getBoolean(KonanConfigKeys.OPTIMIZATION))
disableUnless(verifyBitcodePhase, config.needCompilerVerification || getBoolean(KonanConfigKeys.VERIFY_BITCODE)) disableUnless(verifyBitcodePhase, config.needCompilerVerification || getBoolean(KonanConfigKeys.VERIFY_BITCODE))
val isDescriptorsOnlyLibrary = config.metadataKlib == true
disableIf(psiToIrPhase, isDescriptorsOnlyLibrary)
disableIf(destroySymbolTablePhase, isDescriptorsOnlyLibrary)
disableIf(copyDefaultValuesToActualPhase, isDescriptorsOnlyLibrary)
disableIf(backendCodegen, isDescriptorsOnlyLibrary)
} }
} }
@@ -6,6 +6,10 @@
package org.jetbrains.kotlin.backend.konan.llvm package org.jetbrains.kotlin.backend.konan.llvm
import llvm.* import llvm.*
import org.jetbrains.kotlin.backend.common.phaser.CompilerPhase
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
import org.jetbrains.kotlin.backend.common.phaser.PhaserState
import org.jetbrains.kotlin.backend.common.phaser.namedUnitPhase
import org.jetbrains.kotlin.backend.konan.* import org.jetbrains.kotlin.backend.konan.*
import org.jetbrains.kotlin.backend.konan.descriptors.GlobalHierarchyAnalysis import org.jetbrains.kotlin.backend.konan.descriptors.GlobalHierarchyAnalysis
import org.jetbrains.kotlin.backend.konan.optimizations.* import org.jetbrains.kotlin.backend.konan.optimizations.*
@@ -44,10 +48,14 @@ internal val createLLVMDeclarationsPhase = makeKonanModuleOpPhase(
} }
) )
internal val disposeLLVMPhase = makeKonanModuleOpPhase( internal val disposeLLVMPhase = namedUnitPhase(
name = "DisposeLLVM", name = "DisposeLLVM",
description = "Dispose LLVM", description = "Dispose LLVM",
op = { context, _ -> context.disposeLlvm() } lower = object : CompilerPhase<Context, Unit, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
context.disposeLlvm()
}
}
) )
internal val RTTIPhase = makeKonanModuleOpPhase( internal val RTTIPhase = makeKonanModuleOpPhase(
@@ -265,10 +273,14 @@ internal val bitcodeOptimizationPhase = makeKonanModuleOpPhase(
op = { context, _ -> runLlvmOptimizationPipeline(context) } op = { context, _ -> runLlvmOptimizationPipeline(context) }
) )
internal val produceOutputPhase = makeKonanModuleOpPhase( internal val produceOutputPhase = namedUnitPhase(
name = "ProduceOutput", name = "ProduceOutput",
description = "Produce output", description = "Produce output",
op = { context, _ -> produceOutput(context) } lower = object : CompilerPhase<Context, Unit, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
produceOutput(context)
}
}
) )
internal val verifyBitcodePhase = makeKonanModuleOpPhase( internal val verifyBitcodePhase = makeKonanModuleOpPhase(