[JS IR] tie together IC code in tests and K2JsIrCompiler

This commit is contained in:
Anton Bannykh
2021-11-12 13:59:41 +03:00
committed by TeamCityServer
parent ff02ea5840
commit 75368a2c06
7 changed files with 121 additions and 23 deletions
@@ -77,7 +77,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
}
@Suppress("UNUSED_PARAMETER")
private fun usePerFileInvalidator(configuration: CompilerConfiguration): Boolean = false
private fun usePerFileInvalidator(configuration: CompilerConfiguration): Boolean = true
private fun IcCacheInfo.toICCacheMap(): Map<String, ICCache> {
return data.map { it.key to ICCache(PersistentCacheProvider.EMPTY, PersistentCacheConsumer.EMPTY, it.value) }.toMap()
@@ -212,12 +212,12 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
MainModule.Klib(includes)
}
val perFileCache = usePerFileInvalidator(configuration)
val perFileCache = usePerFileInvalidator(configurationJs)
val start = System.currentTimeMillis()
val updated = if (perFileCache) {
actualizeCacheForModule(includes, outputFilePath, configuration, libraries, icCaches, IrFactoryImpl)
actualizeCacheForModule(includes, outputFilePath, configurationJs, libraries, icCaches, IrFactoryImpl)
} else {
buildCache(
cachePath = outputFilePath,
@@ -282,6 +282,27 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
messageCollector.report(INFO,"Produce executable: $outputFilePath")
messageCollector.report(INFO, arguments.cacheDirectories ?: "")
if (icCaches.isNotEmpty()) {
val beforeIc2Js = System.currentTimeMillis()
val caches = loadModuleCaches(icCaches)
val moduleKind = configurationJs[JSConfigurationKeys.MODULE_KIND]!!
val compiledModule = generateJsFromAst(moduleName, moduleKind, caches)
val outputs = compiledModule.outputs!!
outputFile.write(outputs)
outputs.dependencies.forEach { (name, content) ->
outputFile.resolveSibling("$name.js").write(content)
}
messageCollector.report(INFO, "Executable production duration (IC): ${System.currentTimeMillis() - beforeIc2Js}ms")
return OK
}
val phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)
val module = if (includes != null) {
@@ -378,7 +399,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
arguments.irSafeExternalBooleanDiagnostic,
messageCollector
),
lowerPerModule = icCaches.isNotEmpty(),
lowerPerModule = false,//icCaches.isNotEmpty(),
granularity = granularity,
icCompatibleIr2Js = arguments.irNewIr2Js,
)
@@ -126,15 +126,16 @@ fun lowerPreservingTags(modules: Iterable<IrModuleFragment>, context: JsIrBacken
@Suppress("UNUSED_PARAMETER")
fun generateJsFromAst(
mainModule: String,
caches: Map<String, ModuleCache>
mainModuleName: String,
moduleKind: ModuleKind,
caches: Map<String, ModuleCache>,
): CompilerResult {
val deserializer = JsIrAstDeserializer()
val fragments = JsIrProgram(caches.values.map { JsIrModule(it.name, it.name, it.asts.values.mapNotNull { it.ast?.let { deserializer.deserialize(ByteArrayInputStream(it))} }) })
val fragments = JsIrProgram(caches.values.map { JsIrModule(it.name, it.name, it.asts.values.sortedBy { it.name }.mapNotNull { it.ast?.let { deserializer.deserialize(ByteArrayInputStream(it))} }) })
return CompilerResult(
generateSingleWrappedModuleBody(
"main",
ModuleKind.PLAIN,
mainModuleName,
moduleKind,
fragments.modules.flatMap { it.fragments },
sourceMapsInfo = null,
generateScriptModule = false,
@@ -191,7 +191,7 @@ private fun buildCacheForModule(
}
// TODO: actual way of building a cache could change in future
buildCacheForModuleFiles(irModule, dependencies, deserializer, configuration, dirtyFiles, cacheConsumer)
buildCacheForModuleFiles(irModule, dependencies, deserializer, configuration, dirtyFiles, cacheConsumer, emptySet(), null) // TODO: main arguments?
cacheConsumer.commitLibraryPath(libraryPath)
}
@@ -434,6 +434,8 @@ fun rebuildCacheForDirtyFiles(
dirtyFiles: Collection<String>?,
cacheConsumer: PersistentCacheConsumer,
irFactory: IrFactory,
exportedDeclarations: Set<FqName>,
mainArguments: List<String>?,
) {
val loadedModules = loadModules(configuration.languageVersionSettings, dependencyGraph)
@@ -462,7 +464,16 @@ fun rebuildCacheForDirtyFiles(
val currentIrModule = irModules.find { it.second == library }?.first!!
buildCacheForModuleFiles(currentIrModule, irModules.map { it.first }, jsIrLinker, configuration, dirtyFiles, cacheConsumer)
buildCacheForModuleFiles(
currentIrModule,
irModules.map { it.first },
jsIrLinker,
configuration,
dirtyFiles,
cacheConsumer,
exportedDeclarations,
mainArguments
)
}
@Suppress("UNUSED_PARAMETER")
@@ -472,14 +483,17 @@ private fun buildCacheForModuleFiles(
deserializer: JsIrLinker,
configuration: CompilerConfiguration,
dirtyFiles: Collection<String>?, // if null consider the whole module dirty
cacheConsumer: PersistentCacheConsumer
cacheConsumer: PersistentCacheConsumer,
exportedDeclarations: Set<FqName>,
mainArguments: List<String>?,
) {
compileWithIC(
currentModule,
configuration = configuration,
deserializer = deserializer,
dependencies = dependencies,
exportedDeclarations = setOf(FqName("box")),
mainArguments = mainArguments,
exportedDeclarations = exportedDeclarations,
filesToLower = dirtyFiles?.toSet(),
cacheConsumer = cacheConsumer,
)
@@ -489,3 +503,16 @@ private fun buildCacheForModuleFiles(
// val dirtyS = if (dirtyFiles == null) "[ALL]" else dirtyFiles.joinToString(",", "[", "]") { it }
// println("Dirty files -> $dirtyS")
}
fun loadModuleCaches(icCachePaths: Collection<String>): Map<String, ModuleCache> {
val icCacheMap: Map<ModulePath, String> = loadCacheInfo(icCachePaths)
return icCacheMap.entries.associate { (lib, cache) ->
val provider = createCacheProvider(cache)
val files = provider.filePaths()
lib to ModuleCache(lib, files.associate { f ->
f to FileCache(f, provider.binaryAst(f), provider.dts(f), provider.sourceMap(f))
})
}
}
@@ -50,6 +50,8 @@ interface PersistentCacheProvider {
fun sourceMap(path: String): ByteArray?
fun filePaths(): Iterable<String>
companion object {
val EMPTY = object : PersistentCacheProvider {
override fun fileFingerPrint(path: String): Hash {
@@ -83,6 +85,8 @@ interface PersistentCacheProvider {
override fun sourceMap(path: String): ByteArray? {
return null
}
override fun filePaths(): Iterable<String> = emptyList()
}
}
}
@@ -178,6 +182,15 @@ class PersistentCacheProviderImpl(private val cachePath: String) : PersistentCac
override fun sourceMap(path: String): ByteArray? {
return readBytesFromCacheFile(path, fileSourceMap)
}
override fun filePaths(): Iterable<String> {
return File(cachePath).listFiles()!!.filter { it.isDirectory }.mapNotNull { f ->
val fileInfo = File(f, fileInfoFile)
if (fileInfo.exists()) {
fileInfo.readLines()[0]
} else null
}
}
}
interface PersistentCacheConsumer {