From 4c33cb80163555cd0f2afa6d410ad557b088a256 Mon Sep 17 00:00:00 2001 From: Anton Bannykh Date: Fri, 17 Dec 2021 16:35:58 +0300 Subject: [PATCH] [JS IR] IC: support per-module in K2JsIrCompiler --- .../jetbrains/kotlin/cli/js/K2JsIrCompiler.kt | 9 ++++++++- .../kotlin/ir/backend/js/compilerWithIC.kt | 5 ++--- .../kotlin/ir/backend/js/ic/invalidation.kt | 7 +++++-- .../kotlin/ir/backend/js/ic/CacheAccessors.kt | 18 ++++++------------ .../test/utils/JsIrIncrementalDataProvider.kt | 6 +----- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt index 583a67b1be5..3f53ce1ecb0 100644 --- a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt +++ b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt @@ -287,7 +287,14 @@ class K2JsIrCompiler : CLICompiler() { val translationMode = TranslationMode.fromFlags(false, arguments.irPerModule) - val compiledModule = generateJsFromAst(moduleName, moduleKind, SourceMapsInfo.from(configurationJs), setOf(translationMode), caches) + val compiledModule = generateJsFromAst( + moduleName, + moduleKind, + SourceMapsInfo.from(configurationJs), + setOf(translationMode), + caches, + relativeRequirePath = true + ) val outputs = compiledModule.outputs.values.single() diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compilerWithIC.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compilerWithIC.kt index 63071d0c121..9a91ec158b2 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compilerWithIC.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compilerWithIC.kt @@ -102,8 +102,6 @@ fun compileWithIC( val ast = transformer.generateBinaryAst(dirtyFiles) ast.entries.forEach { (path, bytes) -> cacheConsumer.commitBinaryAst(path, bytes) } - - cacheConsumer.commitModuleName(module.name.asString()) } fun lowerPreservingTags(modules: Iterable, context: JsIrBackendContext, phaseConfig: PhaseConfig, controller: WholeWorldStageController) { @@ -128,6 +126,7 @@ fun generateJsFromAst( sourceMapsInfo: SourceMapsInfo?, translationModes: Set, caches: Map, + relativeRequirePath: Boolean = false, ): CompilerResult { fun compilationOutput(multiModule: Boolean): CompilationOutputs { val deserializer = JsIrAstDeserializer() @@ -144,7 +143,7 @@ fun generateJsFromAst( moduleKind = moduleKind, jsIrProgram, sourceMapsInfo = sourceMapsInfo, - relativeRequirePath = false, + relativeRequirePath = relativeRequirePath, generateScriptModule = false, ) } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/invalidation.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/invalidation.kt index 1b1ccc8022f..37989cc1f04 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/invalidation.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/invalidation.kt @@ -205,7 +205,7 @@ private fun buildCacheForModule( cacheExecutor.execute(irModule, dependencies, deserializer, configuration, dirtyFiles, deletedFiles, cacheConsumer, emptySet(), mainArguments) - cacheConsumer.commitLibraryPath(libraryInfo.libPath.toCanonicalPath(), libraryInfo.flatHash, libraryInfo.transHash) + cacheConsumer.commitLibraryInfo(libraryInfo.libPath.toCanonicalPath(), libraryInfo.flatHash, libraryInfo.transHash, irModule.name.asString()) } private fun loadModules( @@ -548,6 +548,7 @@ private fun actualizeCacheForModule( return CacheUpdateStatus.DIRTY // invalidated and re-built } +// Used for tests only fun rebuildCacheForDirtyFiles( library: KotlinLibrary, configuration: CompilerConfiguration, @@ -585,6 +586,8 @@ fun rebuildCacheForDirtyFiles( val currentIrModule = irModules.find { it.second == library }?.first!! + cacheConsumer.commitLibraryInfo(library.libraryFile.path.toCanonicalPath(), 0UL, 0UL, currentIrModule.name.asString()) + buildCacheForModuleFiles( currentIrModule, irModules.map { it.first }, @@ -634,7 +637,7 @@ fun loadModuleCaches(icCachePaths: Collection): Map return icCacheMap.entries.associate { (lib, cache) -> val provider = createCacheProvider(cache.path) val files = provider.filePaths() - lib to ModuleCache(lib, files.associate { f -> + lib to ModuleCache(provider.moduleName(), files.associate { f -> f to FileCache(f, provider.binaryAst(f), provider.dts(f), provider.sourceMap(f)) }) } diff --git a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/ic/CacheAccessors.kt b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/ic/CacheAccessors.kt index 73e13798c1a..7bc53113848 100644 --- a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/ic/CacheAccessors.kt +++ b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/ic/CacheAccessors.kt @@ -200,7 +200,8 @@ class PersistentCacheProviderImpl(private val cachePath: String) : PersistentCac } override fun moduleName(): String { - TODO("Not yet implemented") + val infoFile = File(File(cachePath), "info") + return infoFile.readLines()[3] } } @@ -214,8 +215,7 @@ interface PersistentCacheConsumer { fun commitSourceMap(path: String, mapData: ByteArray) fun invalidateForFile(path: String) - fun commitLibraryPath(libraryPath: String, flatHash: ULong, transHash: ULong) - fun commitModuleName(moduleName: String) + fun commitLibraryInfo(libraryPath: String, flatHash: ULong, transHash: ULong, moduleName: String) companion object { val EMPTY = object : PersistentCacheConsumer { @@ -246,7 +246,7 @@ interface PersistentCacheConsumer { } - override fun commitLibraryPath(libraryPath: String, flatHash: ULong, transHash: ULong) { + override fun commitLibraryInfo(libraryPath: String, flatHash: ULong, transHash: ULong, moduleName: String) { } @@ -257,9 +257,6 @@ interface PersistentCacheConsumer { override fun commitSourceMap(path: String, mapData: ByteArray) { } - - override fun commitModuleName(moduleName: String) { - } } } } @@ -348,7 +345,7 @@ class PersistentCacheConsumerImpl(private val cachePath: String) : PersistentCac commitByteArrayToCacheFile(path, fileSourceMap, mapData) } - override fun commitLibraryPath(libraryPath: String, flatHash: ULong, transHash: ULong) { + override fun commitLibraryInfo(libraryPath: String, flatHash: ULong, transHash: ULong, moduleName: String) { val infoFile = File(File(cachePath), "info") if (infoFile.exists()) { infoFile.delete() @@ -359,10 +356,7 @@ class PersistentCacheConsumerImpl(private val cachePath: String) : PersistentCac it.println(libraryPath) it.println(flatHash.toString(16)) it.println(transHash.toString(16)) + it.println(moduleName) } } - - override fun commitModuleName(moduleName: String) { - TODO("Not yet implemented") - } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/JsIrIncrementalDataProvider.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/JsIrIncrementalDataProvider.kt index 49439280e51..b581b7c4dda 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/JsIrIncrementalDataProvider.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/JsIrIncrementalDataProvider.kt @@ -120,11 +120,7 @@ class TestModuleCache(val files: MutableMap) { files.remove(path) } - override fun commitLibraryPath(libraryPath: String, flatHash: ULong, transHash: ULong) { - - } - - override fun commitModuleName(moduleName: String) { + override fun commitLibraryInfo(libraryPath: String, flatHash: ULong, transHash: ULong, moduleName: String) { storedModuleName = moduleName } }