[IR] Sort dependencies a bit early
This commit is contained in:
@@ -12,22 +12,11 @@ import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.ir.backend.js.lower.moveBodilessDeclarationsToSeparatePlace
|
||||
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.IrModuleToJsTransformer
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.JsMainFunctionDetector
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
|
||||
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
|
||||
import org.jetbrains.kotlin.library.KotlinLibrary
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
|
||||
fun sortDependencies(dependencies: Collection<IrModuleFragment>): Collection<IrModuleFragment> {
|
||||
val mapping = dependencies.map { it.descriptor to it }.toMap()
|
||||
|
||||
return DFS.topologicalOrder(dependencies) { m ->
|
||||
val descriptor = m.descriptor
|
||||
descriptor.allDependencyModules.filter { it != descriptor }.map { mapping[it] }
|
||||
}.reversed()
|
||||
}
|
||||
|
||||
class CompilerResult(
|
||||
val jsCode: String,
|
||||
@@ -63,8 +52,7 @@ fun compile(
|
||||
).generateUnboundSymbolsAsDependencies()
|
||||
}
|
||||
|
||||
// Since modules should be initialized in the correct topological order we sort them
|
||||
val irFiles = sortDependencies(dependencyModules).flatMap { it.files } + moduleFragment.files
|
||||
val irFiles = dependencyModules.flatMap { it.files } + moduleFragment.files
|
||||
|
||||
moduleFragment.files.clear()
|
||||
moduleFragment.files += irFiles
|
||||
|
||||
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.backend.common.phaser.invokeToplevel
|
||||
import org.jetbrains.kotlin.backend.wasm.codegen.IrModuleToWasm
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.ir.backend.js.loadIr
|
||||
import org.jetbrains.kotlin.ir.backend.js.sortDependencies
|
||||
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
|
||||
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
|
||||
import org.jetbrains.kotlin.library.KotlinLibrary
|
||||
@@ -45,8 +44,7 @@ fun compileWasm(
|
||||
).generateUnboundSymbolsAsDependencies()
|
||||
}
|
||||
|
||||
// Since modules should be initialized in the correct topological order we sort them
|
||||
val irFiles = sortDependencies(dependencyModules).flatMap { it.files } + moduleFragment.files
|
||||
val irFiles = dependencyModules.flatMap { it.files } + moduleFragment.files
|
||||
|
||||
moduleFragment.files.clear()
|
||||
moduleFragment.files += irFiles
|
||||
|
||||
@@ -42,6 +42,7 @@ import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.CompilerDeserializationConfiguration
|
||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
import org.jetbrains.kotlin.konan.file.File as KFile
|
||||
|
||||
|
||||
@@ -110,6 +111,15 @@ data class IrModuleInfo(
|
||||
val deserializer: JsIrLinker
|
||||
)
|
||||
|
||||
private fun sortDependencies(dependencies: List<KotlinLibrary>, mapping: Map<KotlinLibrary, ModuleDescriptor>): Collection<KotlinLibrary> {
|
||||
val m2l = mapping.map { it.value to it.key }.toMap()
|
||||
|
||||
return DFS.topologicalOrder(dependencies) { m ->
|
||||
val descriptor = mapping[m] ?: error("No descriptor found for library ${m.libraryName}")
|
||||
descriptor.allDependencyModules.filter { it != descriptor }.map { m2l[it] }
|
||||
}.reversed()
|
||||
}
|
||||
|
||||
fun loadIr(
|
||||
project: Project,
|
||||
files: List<KtFile>,
|
||||
@@ -127,7 +137,7 @@ fun loadIr(
|
||||
|
||||
val deserializer = JsIrLinker(moduleDescriptor, JsMangler, emptyLoggingContext, irBuiltIns, symbolTable)
|
||||
|
||||
val deserializedModuleFragments = allDependencies.map {
|
||||
val deserializedModuleFragments = sortDependencies(allDependencies, depsDescriptors.descriptors).map {
|
||||
deserializer.deserializeIrModuleHeader(depsDescriptors.getModuleDescriptor(it))!!
|
||||
}
|
||||
|
||||
@@ -244,7 +254,7 @@ private class ModulesStructure(
|
||||
private val storageManager: LockBasedStorageManager = LockBasedStorageManager("ModulesStructure")
|
||||
private var runtimeModule: ModuleDescriptorImpl? = null
|
||||
|
||||
private val descriptors = mutableMapOf<KotlinLibrary, ModuleDescriptorImpl>()
|
||||
val descriptors = mutableMapOf<KotlinLibrary, ModuleDescriptorImpl>()
|
||||
|
||||
fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl = descriptors.getOrPut(current) {
|
||||
val parts = loadKlibMetadataParts(current)
|
||||
|
||||
Reference in New Issue
Block a user