[Native][IR] Partial linkage support for cached inline functions

- Support partial linkage for declarations from inline functions that are lazily deserialized from a KLIB with static cache
- Linker API: Split IrModuleDeserializer.deserializeIrSymbol() into deserializeIrSymbolOrFail() and tryDeserializeIrSymbol()
- Linker API: Refactor KotlinIrLinker.handleSignatureIdNotFoundInModuleWithDependencies() to deserializeOrReturnUnboundIrSymbolIfPartialLinkageEnabled()

^KT-52478
This commit is contained in:
Dmitriy Dolovov
2022-06-23 01:20:52 +04:00
parent 66de825508
commit 532b9e2c5f
18 changed files with 378 additions and 202 deletions
@@ -376,7 +376,7 @@ fun getIrModuleInfoForKlib(
val irBuiltIns = IrBuiltInsOverDescriptors(moduleDescriptor.builtIns, typeTranslator, symbolTable)
val allowUnboundSymbols = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false
val unlinkedDeclarationsSupport = JsUnlinkedDeclarationsSupport(allowUnboundSymbols)
val unlinkedDeclarationsSupport = JsUnlinkedDeclarationsSupport(irBuiltIns, allowUnboundSymbols)
val irLinker = JsIrLinker(
currentModule = null,
@@ -435,7 +435,7 @@ fun getIrModuleInfoForSourceFiles(
}
val allowUnboundSymbols = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false
val unlinkedDeclarationsSupport = JsUnlinkedDeclarationsSupport(allowUnboundSymbols)
val unlinkedDeclarationsSupport = JsUnlinkedDeclarationsSupport(irBuiltIns, allowUnboundSymbols)
val irLinker = JsIrLinker(
currentModule = psi2IrContext.moduleDescriptor,
@@ -30,7 +30,10 @@ class JsIrLinker(
override val translationPluginContext: TranslationPluginContext?,
private val icData: ICData? = null,
friendModules: Map<String, Collection<String>> = emptyMap(),
override val unlinkedDeclarationsSupport: UnlinkedDeclarationsSupport = JsUnlinkedDeclarationsSupport(allowUnboundSymbols = false),
override val unlinkedDeclarationsSupport: UnlinkedDeclarationsSupport = JsUnlinkedDeclarationsSupport(
builtIns,
allowUnboundSymbols = false
),
private val stubGenerator: DeclarationStubGenerator? = null
) : KotlinIrLinker(
currentModule = currentModule,
@@ -35,12 +35,8 @@ class JsLazyIrModuleDeserializer(
private val descriptorFinder = DescriptorByIdSignatureFinderImpl(moduleDescriptor, JsManglerDesc)
private fun resolveDescriptor(idSig: IdSignature): DeclarationDescriptor {
return descriptorFinder.findDescriptorBySignature(idSig) ?: error("No descriptor found for $idSig")
}
override fun deserializeIrSymbol(idSig: IdSignature, symbolKind: BinarySymbolData.SymbolKind): IrSymbol {
val descriptor = resolveDescriptor(idSig)
override fun tryDeserializeIrSymbol(idSig: IdSignature, symbolKind: BinarySymbolData.SymbolKind): IrSymbol? {
val descriptor = descriptorFinder.findDescriptorBySignature(idSig) ?: return null
val declaration = stubGenerator.run {
when (symbolKind) {
@@ -57,6 +53,8 @@ class JsLazyIrModuleDeserializer(
return declaration.symbol
}
override fun deserializedSymbolNotFound(idSig: IdSignature): Nothing = error("No descriptor found for $idSig")
@OptIn(ObsoleteDescriptorBasedAPI::class)
override fun declareIrSymbol(symbol: IrSymbol) {
if (symbol is IrFieldSymbol) {
@@ -5,19 +5,20 @@
package org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir
import org.jetbrains.kotlin.backend.common.serialization.unlinked.UnlinkedDeclarationsSupport
import org.jetbrains.kotlin.backend.common.serialization.unlinked.BasicUnlinkedDeclarationsSupport
import org.jetbrains.kotlin.backend.common.serialization.unlinked.UnlinkedDeclarationsSupport.UnlinkedMarkerTypeHandler
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.types.IrErrorType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.impl.IrErrorTypeImpl
import org.jetbrains.kotlin.types.Variance
class JsUnlinkedDeclarationsSupport(override val allowUnboundSymbols: Boolean) : UnlinkedDeclarationsSupport {
private val handler = object : UnlinkedMarkerTypeHandler {
class JsUnlinkedDeclarationsSupport(
override val builtIns: IrBuiltIns,
override val allowUnboundSymbols: Boolean
) : BasicUnlinkedDeclarationsSupport() {
override val handler = object : UnlinkedMarkerTypeHandler {
override val unlinkedMarkerType = IrErrorTypeImpl(null, emptyList(), Variance.INVARIANT)
override fun IrType.isUnlinkedMarkerType() = this is IrErrorType
}
override fun <T : Any> whenUnboundSymbolsAllowed(action: (UnlinkedMarkerTypeHandler) -> T): T? =
if (allowUnboundSymbols) action(handler) else null
}