From 01aec32596234aff0641080144c80a73eb9ff1c5 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Wed, 11 Dec 2019 18:02:18 +0300 Subject: [PATCH] Disable backend codegen during frontend-only compilation --- .../org/jetbrains/kotlin/cli/bc/K2Native.kt | 2 + .../cli/bc/K2NativeCompilerArguments.kt | 3 ++ .../kotlin/backend/konan/KonanConfig.kt | 2 + .../backend/konan/KonanConfigurationKeys.kt | 2 + .../kotlin/backend/konan/ToplevelPhases.kt | 41 +++++++++++++------ .../backend/konan/llvm/BitcodePhases.kt | 20 +++++++-- 6 files changed, 53 insertions(+), 17 deletions(-) diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt index 81a4e22c729..cbcd8851595 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt @@ -140,6 +140,8 @@ class K2Native : CLICompiler() { val outputKind = CompilerOutputKind.valueOf( (arguments.produce ?: "program").toUpperCase()) put(PRODUCE, outputKind) + put(METADATA_KLIB, arguments.metadataKlib) + arguments.libraryVersion ?. let { put(LIBRARY_VERSION, it) } arguments.mainPackage ?.let{ put(ENTRY, it) } diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt index ebcc4cd4595..52cfcade235 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt @@ -245,6 +245,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() { @Argument(value="-Xallocator", valueDescription = "std | mimalloc", description = "Allocator used in runtime") 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, Any> = super.configureAnalysisFlags(collector).also { val useExperimental = it[AnalysisFlags.useExperimental] as List<*> diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt index 6fb3a1de34d..b19e8f2e2a2 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt @@ -64,6 +64,8 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration 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 purgeUserLibs: Boolean diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt index 5e07cbecb82..fdf356920af 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt @@ -66,6 +66,8 @@ class KonanConfigKeys { = CompilerConfigurationKey.create("memory model") val META_INFO: CompilerConfigurationKey> = CompilerConfigurationKey.create("generate metadata") + val METADATA_KLIB: CompilerConfigurationKey + = CompilerConfigurationKey.create("metadata klib") val MODULE_KIND: CompilerConfigurationKey = CompilerConfigurationKey.create("module kind") val MODULE_NAME: CompilerConfigurationKey diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt index d3e307ab1a9..f4b38af573f 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt @@ -267,9 +267,11 @@ internal val serializerPhase = konanUnitPhase( val mppKlibs = config.configuration.get(CommonConfigurationKeys.KLIB_MPP)?:false val descriptorTable = DescriptorTable.createDefault() - serializedIr = KonanIrModuleSerializer( - this, irModule!!.irBuiltins, descriptorTable, expectDescriptorToSymbol, skipExpects = !mppKlibs - ).serializedIrModule(irModule!!) + serializedIr = irModule?.let { ir -> + KonanIrModuleSerializer( + this, ir.irBuiltins, descriptorTable, expectDescriptorToSymbol, skipExpects = !mppKlibs + ).serializedIrModule(ir) + } val serializer = KlibMetadataMonolithicSerializer( this.config.configuration.languageVersionSettings, @@ -421,6 +423,22 @@ internal val bitcodePhase = namedIrModulePhase( cStubsPhase ) +private val backendCodegen = namedUnitPhase( + name = "Backend codegen", + description = "Backend code generation", + lower = takeFromContext { 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. val toplevelPhase: CompilerPhase<*, Unit, Unit> = namedUnitPhase( name = "Compiler", @@ -436,16 +454,7 @@ val toplevelPhase: CompilerPhase<*, Unit, Unit> = namedUnitPhase( namedUnitPhase( name = "Backend", description = "All backend", - lower = takeFromContext { 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 + lower = backendCodegen then produceOutputPhase then disposeLLVMPhase then unitSink() @@ -485,5 +494,11 @@ internal fun PhaseConfig.konanPhasesConfig(config: KonanConfig) { disableUnless(dcePhase, getBoolean(KonanConfigKeys.OPTIMIZATION)) disableUnless(ghaPhase, getBoolean(KonanConfigKeys.OPTIMIZATION)) 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) } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt index a6f37985101..0ed9bf2510a 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt @@ -6,6 +6,10 @@ package org.jetbrains.kotlin.backend.konan.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.descriptors.GlobalHierarchyAnalysis 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", description = "Dispose LLVM", - op = { context, _ -> context.disposeLlvm() } + lower = object : CompilerPhase { + override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Unit) { + context.disposeLlvm() + } + } ) internal val RTTIPhase = makeKonanModuleOpPhase( @@ -265,10 +273,14 @@ internal val bitcodeOptimizationPhase = makeKonanModuleOpPhase( op = { context, _ -> runLlvmOptimizationPipeline(context) } ) -internal val produceOutputPhase = makeKonanModuleOpPhase( +internal val produceOutputPhase = namedUnitPhase( name = "ProduceOutput", description = "Produce output", - op = { context, _ -> produceOutput(context) } + lower = object : CompilerPhase { + override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState, context: Context, input: Unit) { + produceOutput(context) + } + } ) internal val verifyBitcodePhase = makeKonanModuleOpPhase(