[JS IR] Module descriptors depends on each other

This commit is contained in:
Ilya Goncharov
2023-01-06 18:41:42 +01:00
committed by Space Team
parent b42492cd4d
commit 7ae85ed68e
9 changed files with 52 additions and 24 deletions
@@ -449,7 +449,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
moduleSourceFiles,
environmentForJS.configuration,
sourceModule.jsFrontEndResult.jsAnalysisResult,
sortDependencies(sourceModule.descriptors),
sortDependencies(sourceModule.moduleDependencies),
icData,
expectDescriptorToSymbol,
IrFactoryImpl,
@@ -68,7 +68,11 @@ internal class JsIrLinkerLoader(
var runtimeModule: ModuleDescriptorImpl? = null
// TODO: deduplicate this code using part from klib.kt
fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl = descriptors.getOrPut(current) {
fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl {
if (current in descriptors) {
return descriptors.getValue(current)
}
val isBuiltIns = current.unresolvedDependencies.isEmpty()
val lookupTracker = LookupTracker.DO_NOTHING
@@ -82,12 +86,15 @@ internal class JsIrLinkerLoader(
)
if (isBuiltIns) runtimeModule = md
val dependencies = dependencyGraph[current]!!.map { getModuleDescriptor(it) }
md.setDependencies(listOf(md) + dependencies)
md
descriptors[current] = md
return md
}
return dependencyGraph.keys.associateBy { klib -> getModuleDescriptor(klib) }
val moduleDescriptorToKotlinLibrary = dependencyGraph.keys.associateBy { klib -> getModuleDescriptor(klib) }
return moduleDescriptorToKotlinLibrary
.onEach { (key, _) -> key.setDependencies(moduleDescriptorToKotlinLibrary.keys.toList()) }
.map<ModuleDescriptorImpl, KotlinLibrary, Pair<ModuleDescriptor, KotlinLibrary>> { it.key to it.value }
.toMap()
}
data class LoadedJsIr(val linker: JsIrLinker, val loadedFragments: Map<KotlinLibraryFile, IrModuleFragment>)
@@ -159,12 +159,9 @@ data class IrModuleInfo(
val moduleFragmentToUniqueName: Map<IrModuleFragment, String>,
)
fun sortDependencies(mapping: Map<KotlinLibrary, ModuleDescriptor>): Collection<KotlinLibrary> {
val m2l = mapping.map { it.value to it.key }.toMap()
return DFS.topologicalOrder(mapping.keys) { m ->
val descriptor = mapping[m] ?: error("No descriptor found for library ${m.libraryName}")
descriptor.allDependencyModules.filter { it != descriptor }.map { m2l[it] }
fun sortDependencies(moduleDependencies: Map<KotlinLibrary, List<KotlinLibrary>>): Collection<KotlinLibrary> {
return DFS.topologicalOrder(moduleDependencies.keys) { m ->
moduleDependencies.getValue(m)
}.reversed()
}
@@ -225,7 +222,7 @@ fun loadIr(
project,
configuration,
mainModule.files,
sortDependencies(depsDescriptors.descriptors),
sortDependencies(depsDescriptors.moduleDependencies),
friendModules,
symbolTable,
messageLogger,
@@ -238,7 +235,7 @@ fun loadIr(
val mainModuleLib = allDependencies.find { it.libraryFile.canonicalPath == mainPath }
?: error("No module with ${mainModule.libPath} found")
val moduleDescriptor = depsDescriptors.getModuleDescriptor(mainModuleLib)
val sortedDependencies = sortDependencies(depsDescriptors.descriptors)
val sortedDependencies = sortDependencies(depsDescriptors.moduleDependencies)
val friendModules = mapOf(mainModuleLib.uniqueName to depsDescriptors.friendDependencies.map { it.library.uniqueName })
return getIrModuleInfoForKlib(
@@ -520,7 +517,7 @@ class ModulesStructure(
files,
project,
compilerConfiguration,
allDependencies.map { getModuleDescriptor(it.library) },
allModuleDescriptors,
friendDependencies.map { getModuleDescriptor(it.library) },
analyzer.targetEnvironment,
thisIsBuiltInsModule = builtInModuleDescriptor == null,
@@ -556,7 +553,21 @@ class ModulesStructure(
// TODO: these are roughly equivalent to KlibResolvedModuleDescriptorsFactoryImpl. Refactor me.
val descriptors = mutableMapOf<KotlinLibrary, ModuleDescriptorImpl>()
fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl = descriptors.getOrPut(current) {
val allModuleDescriptors = run {
val descriptors = allDependencies.map { getModuleDescriptor(it.library) }
descriptors.forEach { descriptor ->
descriptor.setDependencies(descriptors)
}
descriptors
}
fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl {
if (current in descriptors) {
return descriptors.getValue(current)
}
val isBuiltIns = current.unresolvedDependencies.isEmpty()
val lookupTracker = compilerConfiguration[CommonConfigurationKeys.LOOKUP_TRACKER] ?: LookupTracker.DO_NOTHING
@@ -570,10 +581,9 @@ class ModulesStructure(
)
if (isBuiltIns) runtimeModule = md
val dependencies = moduleDependencies.getValue(current).map { getModuleDescriptor(it) }
md.setDependencies(listOf(md) + dependencies)
descriptors[current] = md
md
return md
}
val builtInModuleDescriptor =
@@ -84,12 +84,13 @@ class ClassicFrontend2IrConverter(
val sourceFiles = psiFiles.values.toList()
val icData = configuration.incrementalDataProvider?.getSerializedData(sourceFiles) ?: emptyList()
val expectDescriptorToSymbol = mutableMapOf<DeclarationDescriptor, IrSymbol>()
val (moduleFragment, pluginContext) = generateIrForKlibSerialization(
project,
sourceFiles,
configuration,
analysisResult,
sortDependencies(JsEnvironmentConfigurator.getAllRecursiveLibrariesFor(module, testServices)),
sortDependencies(JsEnvironmentConfigurator.getAllDependenciesMappingFor(module, testServices)),
icData,
expectDescriptorToSymbol,
IrFactoryImpl,
@@ -213,6 +213,16 @@ class JsEnvironmentConfigurator(testServices: TestServices) : EnvironmentConfigu
return dependencies.associateBy { testServices.jsLibraryProvider.getCompiledLibraryByDescriptor(it) }
}
fun getAllDependenciesMappingFor(module: TestModule, testServices: TestServices): Map<KotlinLibrary, List<KotlinLibrary>> {
val allRecursiveLibraries: Map<KotlinLibrary, ModuleDescriptor> = getAllRecursiveLibrariesFor(module, testServices)
val m2l = allRecursiveLibraries.map { it.value to it.key }.toMap()
return allRecursiveLibraries.keys.associateWith { m ->
val descriptor = allRecursiveLibraries[m] ?: error("No descriptor found for library ${m.libraryName}")
descriptor.allDependencyModules.filter { it != descriptor }.map { m2l.getValue(it) }
}
}
fun TestModule.hasFilesToRecompile(): Boolean {
return files.any { JsEnvironmentConfigurationDirectives.RECOMPILE in it.directives }
}
@@ -67,7 +67,7 @@ class FilePathsInKlibTest : CodegenTestCase() {
sourceFiles,
module.compilerConfiguration,
module.jsFrontEndResult.jsAnalysisResult,
sortDependencies(module.descriptors),
sortDependencies(module.moduleDependencies),
icData,
expectDescriptorToSymbol,
IrFactoryImpl,
@@ -387,7 +387,7 @@ abstract class AbstractInvalidationTest(
moduleSourceFiles,
configuration,
sourceModule.jsFrontEndResult.jsAnalysisResult,
sortDependencies(sourceModule.descriptors),
sortDependencies(sourceModule.moduleDependencies),
icData,
expectDescriptorToSymbol,
IrFactoryImpl,
@@ -208,7 +208,7 @@ class JsIrBackendFacade(
return getIrModuleInfoForKlib(
moduleDescriptor,
sortDependencies(JsEnvironmentConfigurator.getAllRecursiveLibrariesFor(module, testServices)) + mainModuleLib,
sortDependencies(JsEnvironmentConfigurator.getAllDependenciesMappingFor(module, testServices)) + mainModuleLib,
friendModules,
filesToLoad,
configuration,
@@ -130,7 +130,7 @@ abstract class AbstractJsKLibABITestCase : KtUsefulTestCase() {
moduleSourceFiles,
config,
sourceModule.jsFrontEndResult.jsAnalysisResult,
sortDependencies(sourceModule.descriptors),
sortDependencies(sourceModule.moduleDependencies),
icData,
expectDescriptorToSymbol,
IrFactoryImpl,