[IR] Refactored Psi2Ir for klibs
This commit is contained in:
+49
-50
@@ -16,14 +16,12 @@ import org.jetbrains.kotlin.backend.konan.serialization.KonanManglerDesc
|
||||
import org.jetbrains.kotlin.backend.konan.serialization.KonanManglerIr
|
||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
||||
import org.jetbrains.kotlin.config.languageVersionSettings
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.konan.DeserializedKlibModuleOrigin
|
||||
import org.jetbrains.kotlin.descriptors.konan.KlibModuleOrigin
|
||||
import org.jetbrains.kotlin.descriptors.konan.isNativeStdlib
|
||||
import org.jetbrains.kotlin.ir.builders.TranslationPluginContext
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
|
||||
@@ -31,7 +29,7 @@ import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
|
||||
internal fun Context.psiToIr(symbolTable: SymbolTable) {
|
||||
internal fun Context.psiToIr(symbolTable: SymbolTable, isProducingLibrary: Boolean) {
|
||||
// Translate AST to high level IR.
|
||||
val expectActualLinker = config.configuration.get(CommonConfigurationKeys.EXPECT_ACTUAL_LINKER)?:false
|
||||
|
||||
@@ -75,37 +73,22 @@ internal fun Context.psiToIr(symbolTable: SymbolTable) {
|
||||
get() = generatorContext.irBuiltIns
|
||||
}
|
||||
|
||||
val linker =
|
||||
KonanIrLinker(
|
||||
moduleDescriptor,
|
||||
functionIrClassFactory,
|
||||
translationContext,
|
||||
this as LoggingContext,
|
||||
generatorContext.irBuiltIns,
|
||||
symbolTable,
|
||||
forwardDeclarationsModuleDescriptor,
|
||||
stubGenerator,
|
||||
irProviderForCEnumsAndCStructs,
|
||||
exportedDependencies,
|
||||
deserializeFakeOverrides,
|
||||
config.cachedLibraries
|
||||
)
|
||||
|
||||
translator.addPostprocessingStep { module ->
|
||||
val pluginContext = IrPluginContextImpl(
|
||||
generatorContext.moduleDescriptor,
|
||||
generatorContext.bindingContext,
|
||||
generatorContext.languageVersionSettings,
|
||||
generatorContext.symbolTable,
|
||||
generatorContext.typeTranslator,
|
||||
generatorContext.irBuiltIns,
|
||||
linker = linker
|
||||
)
|
||||
pluginExtensions.forEach { extension ->
|
||||
extension.generate(module, pluginContext)
|
||||
}
|
||||
}
|
||||
val linker = KonanIrLinker(
|
||||
moduleDescriptor,
|
||||
functionIrClassFactory,
|
||||
translationContext,
|
||||
this as LoggingContext,
|
||||
generatorContext.irBuiltIns,
|
||||
symbolTable,
|
||||
forwardDeclarationsModuleDescriptor,
|
||||
stubGenerator,
|
||||
irProviderForCEnumsAndCStructs,
|
||||
exportedDependencies,
|
||||
deserializeFakeOverrides,
|
||||
config.cachedLibraries
|
||||
)
|
||||
|
||||
// context.config.librariesWithDependencies could change at each iteration.
|
||||
var dependenciesCount = 0
|
||||
while (true) {
|
||||
// context.config.librariesWithDependencies could change at each iteration.
|
||||
@@ -123,7 +106,10 @@ internal fun Context.psiToIr(symbolTable: SymbolTable) {
|
||||
val kotlinLibrary = dependency.getCapability(KlibModuleOrigin.CAPABILITY)?.let {
|
||||
(it as? DeserializedKlibModuleOrigin)?.library
|
||||
}
|
||||
linker.deserializeIrModuleHeader(dependency, kotlinLibrary)
|
||||
if (isProducingLibrary)
|
||||
linker.deserializeOnlyHeaderModule(dependency, kotlinLibrary)
|
||||
else
|
||||
linker.deserializeIrModuleHeader(dependency, kotlinLibrary)
|
||||
}
|
||||
if (dependencies.size == dependenciesCount) break
|
||||
dependenciesCount = dependencies.size
|
||||
@@ -135,48 +121,61 @@ internal fun Context.psiToIr(symbolTable: SymbolTable) {
|
||||
.filter(ModuleDescriptor::isFromInteropLibrary)
|
||||
.forEach(irProviderForCEnumsAndCStructs::referenceAllEnumsAndStructsFrom)
|
||||
|
||||
val irProviders = listOf(linker)
|
||||
translator.addPostprocessingStep { module ->
|
||||
val pluginContext = IrPluginContextImpl(
|
||||
generatorContext.moduleDescriptor,
|
||||
generatorContext.bindingContext,
|
||||
generatorContext.languageVersionSettings,
|
||||
generatorContext.symbolTable,
|
||||
generatorContext.typeTranslator,
|
||||
generatorContext.irBuiltIns,
|
||||
linker
|
||||
)
|
||||
pluginExtensions.forEach { extension ->
|
||||
extension.generate(module, pluginContext)
|
||||
}
|
||||
}
|
||||
|
||||
expectDescriptorToSymbol = mutableMapOf<DeclarationDescriptor, IrSymbol>()
|
||||
expectDescriptorToSymbol = mutableMapOf()
|
||||
val module = translator.generateModuleFragment(
|
||||
generatorContext,
|
||||
environment.getSourceFiles(),
|
||||
irProviders,
|
||||
pluginExtensions,
|
||||
irProviders = listOf(linker),
|
||||
linkerExtensions = pluginExtensions,
|
||||
// TODO: This is a hack to allow platform libs to build in reasonable time.
|
||||
// referenceExpectsForUsedActuals() appears to be quadratic in time because of
|
||||
// how ExpectedActualResolver is implemented.
|
||||
// Need to fix ExpectActualResolver to either cache expects or somehow reduce the member scope searches.
|
||||
if (expectActualLinker) expectDescriptorToSymbol else null
|
||||
expectDescriptorToSymbol = if (expectActualLinker) expectDescriptorToSymbol else null
|
||||
)
|
||||
|
||||
linker.postProcess()
|
||||
|
||||
if (this.stdlibModule in modulesWithoutDCE) {
|
||||
functionIrClassFactory.buildAllClasses()
|
||||
}
|
||||
|
||||
// Enable lazy IR genration for newly-created symbols inside BE
|
||||
stubGenerator.unboundSymbolGeneration = true
|
||||
|
||||
symbolTable.noUnboundLeft("Unbound symbols left after linker")
|
||||
|
||||
module.acceptVoid(ManglerChecker(KonanManglerIr, Ir2DescriptorManglerAdapter(KonanManglerDesc)))
|
||||
|
||||
val modules = if (isProducingLibrary) emptyMap() else linker.modules
|
||||
|
||||
if (!config.configuration.getBoolean(KonanConfigKeys.DISABLE_FAKE_OVERRIDE_VALIDATOR)) {
|
||||
val fakeOverrideChecker = FakeOverrideChecker(KonanManglerIr, KonanManglerDesc)
|
||||
linker.modules.values.forEach { fakeOverrideChecker.check(it) }
|
||||
modules.values.forEach { fakeOverrideChecker.check(it) }
|
||||
}
|
||||
|
||||
irModule = module
|
||||
|
||||
// Note: coupled with [shouldLower] below.
|
||||
irModules = linker.modules.filterValues { llvmModuleSpecification.containsModule(it) }
|
||||
|
||||
internalAbi.init(irModules.values + irModule!!)
|
||||
irModules = modules.filterValues { llvmModuleSpecification.containsModule(it) }
|
||||
|
||||
ir.symbols = symbols
|
||||
|
||||
functionIrClassFactory.module =
|
||||
(listOf(irModule!!) + linker.modules.values)
|
||||
.single { it.descriptor.isNativeStdlib() }
|
||||
if (!isProducingLibrary) {
|
||||
if (this.stdlibModule in modulesWithoutDCE)
|
||||
functionIrClassFactory.buildAllClasses()
|
||||
internalAbi.init(irModules.values + irModule!!)
|
||||
functionIrClassFactory.module = (modules.values + irModule!!).single { it.descriptor.isNativeStdlib() }
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -131,7 +131,7 @@ internal val buildCExportsPhase = konanUnitPhase(
|
||||
)
|
||||
|
||||
internal val psiToIrPhase = konanUnitPhase(
|
||||
op = { this.psiToIr(symbolTable!!) },
|
||||
op = { this.psiToIr(symbolTable!!, config.produce == CompilerOutputKind.LIBRARY) },
|
||||
name = "Psi2Ir",
|
||||
description = "Psi to IR conversion and klib linkage",
|
||||
prerequisite = setOf(createSymbolTablePhase)
|
||||
|
||||
Reference in New Issue
Block a user