[K/N] Remove unused default dependencies from klib
^KT-55603
This commit is contained in:
committed by
Space Team
parent
f662908440
commit
95541da9a1
@@ -1147,23 +1147,28 @@ open class SymbolTable(
|
||||
throw IllegalArgumentException("Unexpected value descriptor: $value")
|
||||
}
|
||||
|
||||
private inline fun <D : DeclarationDescriptor, IR : IrSymbolOwner, S : IrBindableSymbol<D, IR>> FlatSymbolTable<D, IR, S>.forEachPublicSymbolImpl(
|
||||
private inline fun <D : DeclarationDescriptor, IR : IrSymbolOwner, S : IrBindableSymbol<D, IR>> FlatSymbolTable<D, IR, S>.forEachSymbolImpl(
|
||||
block: (IrSymbol) -> Unit
|
||||
) {
|
||||
idSigToSymbol.forEach { (_, sym) ->
|
||||
assert(sym.isPublicApi)
|
||||
block(sym)
|
||||
}
|
||||
}
|
||||
|
||||
fun forEachPublicSymbol(block: (IrSymbol) -> Unit) {
|
||||
classSymbolTable.forEachPublicSymbolImpl { block(it) }
|
||||
constructorSymbolTable.forEachPublicSymbolImpl { block(it) }
|
||||
simpleFunctionSymbolTable.forEachPublicSymbolImpl { block(it) }
|
||||
propertySymbolTable.forEachPublicSymbolImpl { block(it) }
|
||||
enumEntrySymbolTable.forEachPublicSymbolImpl { block(it) }
|
||||
typeAliasSymbolTable.forEachPublicSymbolImpl { block(it) }
|
||||
fieldSymbolTable.forEachPublicSymbolImpl { block(it) }
|
||||
/**
|
||||
* This function is quite messy and doesn't have good contract of what exactly is traversed.
|
||||
* Basic idea is it traverse symbols which can be reasonable referered from other module
|
||||
*
|
||||
* Be careful when using it, and avoid it, except really need.
|
||||
*/
|
||||
fun forEachDeclarationSymbol(block: (IrSymbol) -> Unit) {
|
||||
classSymbolTable.forEachSymbolImpl { block(it) }
|
||||
constructorSymbolTable.forEachSymbolImpl { block(it) }
|
||||
simpleFunctionSymbolTable.forEachSymbolImpl { block(it) }
|
||||
propertySymbolTable.forEachSymbolImpl { block(it) }
|
||||
enumEntrySymbolTable.forEachSymbolImpl { block(it) }
|
||||
typeAliasSymbolTable.forEachSymbolImpl { block(it) }
|
||||
fieldSymbolTable.forEachSymbolImpl { block(it) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.declarations.IrSymbolOwner
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.isPublicApi
|
||||
import org.jetbrains.kotlin.ir.util.IdSignature
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.library.IrLibrary
|
||||
@@ -144,7 +145,8 @@ class CurrentModuleWithICDeserializer(
|
||||
|
||||
override fun init(delegate: IrModuleDeserializer) {
|
||||
val knownBuiltIns = irBuiltIns.knownBuiltins.map { (it as IrSymbolOwner).symbol }.toSet()
|
||||
symbolTable.forEachPublicSymbol {
|
||||
symbolTable.forEachDeclarationSymbol {
|
||||
assert(it.isPublicApi)
|
||||
if (it.descriptor.isDirtyDescriptor()) { // public && non-deserialized should be dirty symbol
|
||||
if (it !in knownBuiltIns) {
|
||||
dirtyDeclarations[it.signature!!] = it
|
||||
|
||||
+25
-6
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.fir.FirDiagnosticsCompilerResultsReporter
|
||||
import org.jetbrains.kotlin.config.languageVersionSettings
|
||||
import org.jetbrains.kotlin.descriptors.deserialization.PlatformDependentTypeTransformer
|
||||
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.isEmpty
|
||||
import org.jetbrains.kotlin.descriptors.konan.isNativeStdlib
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporterFactory
|
||||
import org.jetbrains.kotlin.fir.backend.*
|
||||
@@ -25,10 +25,14 @@ import org.jetbrains.kotlin.fir.descriptors.FirModuleDescriptor
|
||||
import org.jetbrains.kotlin.fir.pipeline.convertToIrAndActualize
|
||||
import org.jetbrains.kotlin.fir.signaturer.Ir2FirManglerAdapter
|
||||
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import org.jetbrains.kotlin.library.metadata.KlibMetadataFactories
|
||||
import org.jetbrains.kotlin.library.metadata.impl.ForwardDeclarationKind
|
||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
|
||||
internal val KlibFactories = KlibMetadataFactories(::KonanBuiltIns, DynamicTypeDeserializer, PlatformDependentTypeTransformer.None)
|
||||
@@ -39,7 +43,6 @@ internal fun PhaseContext.fir2Ir(
|
||||
val fir2IrExtensions = Fir2IrExtensions.Default
|
||||
|
||||
var builtInsModule: KotlinBuiltIns? = null
|
||||
val dependencies = mutableListOf<ModuleDescriptorImpl>()
|
||||
|
||||
val resolvedLibraries = config.resolvedLibraries.getFullResolvedList()
|
||||
val configuration = config.configuration
|
||||
@@ -54,7 +57,6 @@ internal fun PhaseContext.fir2Ir(
|
||||
packageAccessHandler = null,
|
||||
lookupTracker = LookupTracker.DO_NOTHING
|
||||
)
|
||||
dependencies += moduleDescriptor
|
||||
|
||||
val isBuiltIns = moduleDescriptor.isNativeStdlib()
|
||||
if (isBuiltIns) builtInsModule = moduleDescriptor.builtIns
|
||||
@@ -64,7 +66,7 @@ internal fun PhaseContext.fir2Ir(
|
||||
|
||||
librariesDescriptors.forEach { moduleDescriptor ->
|
||||
// Yes, just to all of them.
|
||||
moduleDescriptor.setDependencies(ArrayList(dependencies))
|
||||
moduleDescriptor.setDependencies(ArrayList(librariesDescriptors))
|
||||
}
|
||||
val diagnosticsReporter = DiagnosticReporterFactory.createPendingReporter()
|
||||
|
||||
@@ -92,6 +94,23 @@ internal fun PhaseContext.fir2Ir(
|
||||
"`${irModuleFragment.name}` must be Name.special, since it's required by KlibMetadataModuleDescriptorFactoryImpl.createDescriptorOptionalBuiltIns()"
|
||||
}
|
||||
|
||||
val usedPackages = buildSet {
|
||||
components.symbolTable.forEachDeclarationSymbol {
|
||||
val p = it.owner as? IrDeclaration ?: return@forEachDeclarationSymbol
|
||||
val fragment = (p.getPackageFragment() as? IrExternalPackageFragment) ?: return@forEachDeclarationSymbol
|
||||
add(fragment.fqName)
|
||||
}
|
||||
// This packages exists in all platform libraries, but can contain only synthetic declarations.
|
||||
// These declarations are not really located in klib, so we don't need to depend on klib to use them.
|
||||
removeAll(ForwardDeclarationKind.values().map { it.packageFqName })
|
||||
}.toList()
|
||||
|
||||
|
||||
val usedLibraries = librariesDescriptors.zip(resolvedLibraries).filter { (module, _) ->
|
||||
usedPackages.any { !module.packageFragmentProviderForModuleContentWithoutDependencies.isEmpty(it) }
|
||||
}.map { it.second }.toSet()
|
||||
|
||||
|
||||
val symbols = createKonanSymbols(irModuleFragment, components, pluginContext)
|
||||
|
||||
val renderDiagnosticNames = configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME)
|
||||
@@ -101,7 +120,7 @@ internal fun PhaseContext.fir2Ir(
|
||||
throw KonanCompilationException("Compilation failed: there were some diagnostics during fir2ir")
|
||||
}
|
||||
|
||||
return Fir2IrOutput(input.firResult, symbols, irModuleFragment, components, pluginContext, irActualizationResult)
|
||||
return Fir2IrOutput(input.firResult, symbols, irModuleFragment, components, pluginContext, irActualizationResult, usedLibraries)
|
||||
}
|
||||
|
||||
private fun PhaseContext.createKonanSymbols(
|
||||
|
||||
+5
-2
@@ -45,13 +45,16 @@ internal fun PhaseContext.firSerializer(
|
||||
configuration.get(CommonConfigurationKeys.METADATA_VERSION)
|
||||
?: configuration.languageVersionSettings.languageVersion.toMetadataVersion()
|
||||
|
||||
val resolvedLibraries = config.resolvedLibraries.getFullResolvedList(TopologicalLibraryOrder) // FIXME KT-55603
|
||||
val resolvedLibraries = config.resolvedLibraries.getFullResolvedList(TopologicalLibraryOrder)
|
||||
val usedResolvedLibraries = resolvedLibraries.filter {
|
||||
(!it.isDefault && !configuration.getBoolean(KonanConfigKeys.PURGE_USER_LIBS)) || it in input.usedLibraries
|
||||
}
|
||||
val actualizedFirDeclarations = input.irActualizationResult.extractFirDeclarations()
|
||||
return serializeNativeModule(
|
||||
configuration = configuration,
|
||||
messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None,
|
||||
sourceFiles,
|
||||
resolvedLibraries.map { it.library as KonanLibrary },
|
||||
usedResolvedLibraries.map { it.library as KonanLibrary },
|
||||
input.irModuleFragment,
|
||||
expectDescriptorToSymbol = mutableMapOf() // TODO: expect -> actual mapping
|
||||
) { file ->
|
||||
|
||||
+3
-1
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.fir.backend.Fir2IrComponents
|
||||
import org.jetbrains.kotlin.fir.backend.Fir2IrPluginContext
|
||||
import org.jetbrains.kotlin.fir.pipeline.FirResult
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.library.metadata.resolver.KotlinResolvedLibrary
|
||||
|
||||
internal data class Fir2IrOutput(
|
||||
val firResult: FirResult,
|
||||
@@ -21,7 +22,8 @@ internal data class Fir2IrOutput(
|
||||
val irModuleFragment: IrModuleFragment,
|
||||
val components: Fir2IrComponents,
|
||||
val pluginContext: Fir2IrPluginContext,
|
||||
val irActualizationResult: IrActualizationResult?
|
||||
val irActualizationResult: IrActualizationResult?,
|
||||
val usedLibraries: Set<KotlinResolvedLibrary>
|
||||
)
|
||||
|
||||
internal val Fir2IrPhase = createSimpleNamedCompilerPhase(
|
||||
|
||||
Reference in New Issue
Block a user