[Native] Simplify filtering of deserialized KLIBs on 2nd stage The KLIB resolver may load a huge number of libraries on the 2nd Kotlin/Native compilation stage. This not only includes the libraries explicitly specified in compiler's CLI, which is expected, but also the ones implicitly loaded from the distribution. Sometimes, there might be 100+ platform libraries among them (example: any Apple target). Not all of them should necessarily be included in the expensive IR linkage process and the pipeline of lowerings. That's the reason there is a special filtering of "roots" in `NativeSecondStageCompilationConfig.librariesWithDependencies()`, which helps to narrow down the set of processed libraries to only the required ones. It turns out that one part of the filtering condition is useless and can be safely removed along with the corresponding Klib flag: - The `Klib.hasDeclarationsAccessedDuringFrontendResolve` flag is only raised when a `KlibMetadataDeserializedPackageFragment` is touched. - `KlibMetadataDeserializedPackageFragment` is touched only when few K1-based checkers are executed. This happens just because the K1 frontend is still used in the Kotlin/Native driver to initiate the backend compilation pipeline. - The checkers explore only the "included" library (-Xinclude) and the standard library. So, the flag can be raised only for a few libraries and in practice can't be reliably used to distinguish the required libraries. ^KT-83940
diff --git a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/KlibAttributes.kt b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/KlibAttributes.kt deleted file mode 100644 index 3a6e6da..0000000 --- a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/KlibAttributes.kt +++ /dev/null
@@ -1,19 +0,0 @@ -/* - * Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.library.metadata - -import org.jetbrains.kotlin.library.Klib -import org.jetbrains.kotlin.library.klibFlag - -/** - * Indicates whether there are declarations in the [Klib] that have been accessed during the frontend resolve phase. - * - * This flag is used as an optimization in Kotlin/Native: There might be 100+ platform libraries in the Kotlin/Native distribution, - * all are added to the "classpath" implicitly. We don't want all these libraries to participate in the expensive IR-linkage - * process on the 2nd compilation stage. And using this flag, we can skip those that are not needed. - */ -var Klib.hasDeclarationsAccessedDuringFrontendResolve: Boolean by klibFlag() - internal set
diff --git a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/KlibMetadataPackageFragment.kt b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/KlibMetadataPackageFragment.kt index e01a987..e463de1 100644 --- a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/KlibMetadataPackageFragment.kt +++ b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/KlibMetadataPackageFragment.kt
@@ -52,7 +52,6 @@ override val proto: ProtoBuf.PackageFragment get() { - library.hasDeclarationsAccessedDuringFrontendResolve = true return protoForNames } }
diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeSecondStageCompilationConfig.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeSecondStageCompilationConfig.kt index 4ebb059..7956f0d 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeSecondStageCompilationConfig.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeSecondStageCompilationConfig.kt
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.konan.properties.loadProperties import org.jetbrains.kotlin.konan.target.* import org.jetbrains.kotlin.library.KotlinLibrary -import org.jetbrains.kotlin.library.metadata.hasDeclarationsAccessedDuringFrontendResolve import org.jetbrains.kotlin.native.resolve.KonanLibrariesResolveSupport import org.jetbrains.kotlin.utils.KotlinNativePaths import java.nio.file.Files @@ -400,7 +399,18 @@ * Returns the list of libraries in reverse topological order. */ fun librariesWithDependencies(): List<KotlinLibrary> { - return resolvedLibraries.filterRoots { (it.library.isExplicitlySpecifiedByUserInCLIArgument && !purgeUserLibs) || it.library.hasDeclarationsAccessedDuringFrontendResolve }.getFullList() + return resolvedLibraries.filterRoots { + // Let's leave only those dependencies (roots) that have been explicitly specified by the used in compiler's CLI. + // + // The implicit dependencies (those that are loaded from the Kotlin/Native distribution: stdlib & platform libraries) + // should be skipped. There might be 100+ platform libraries per a target, and we don't want ALL of them to participate + // in the expensive IR-linkage process. + // + // Later upon the subsequent `getFullList()` call, some of the implicit dependencies will be added. But only if they + // are mentioned in `depends=` manifest property in root libraries. Which means only a small really required subset + // of them will be added. + it.library.isExplicitlySpecifiedByUserInCLIArgument && !purgeUserLibs + }.getFullList() } internal val externalDependenciesFile = configuration.externalDependencies?.let(::File)