[JS IR] Support a module friendship in IC infrastructure
^KT-55097 Fixed
This commit is contained in:
committed by
Space Team
parent
76e629340f
commit
c31705240a
@@ -275,7 +275,15 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
|
||||
// TODO: Handle non-empty main call arguments
|
||||
val mainCallArguments = if (K2JsArgumentConstants.NO_CALL == arguments.main) null else emptyList<String>()
|
||||
|
||||
val icCaches = prepareIcCaches(arguments, messageCollector, outputDir, libraries, configurationJs, mainCallArguments)
|
||||
val icCaches = prepareIcCaches(
|
||||
arguments = arguments,
|
||||
messageCollector = messageCollector,
|
||||
outputDir = outputDir,
|
||||
libraries = libraries,
|
||||
friendLibraries = friendLibraries,
|
||||
configurationJs = configurationJs,
|
||||
mainCallArguments = mainCallArguments
|
||||
)
|
||||
|
||||
// Run analysis if main module is sources
|
||||
var sourceModule: ModulesStructure? = null
|
||||
@@ -638,6 +646,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
|
||||
messageCollector: MessageCollector,
|
||||
outputDir: File,
|
||||
libraries: List<String>,
|
||||
friendLibraries: List<String>,
|
||||
configurationJs: CompilerConfiguration,
|
||||
mainCallArguments: List<String>?
|
||||
): List<ModuleArtifact> {
|
||||
@@ -656,6 +665,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
|
||||
val cacheUpdater = CacheUpdater(
|
||||
mainModule = arguments.includes!!,
|
||||
allModules = libraries,
|
||||
mainModuleFriends = friendLibraries,
|
||||
cacheDir = cacheDirectory,
|
||||
compilerConfiguration = configurationJs,
|
||||
irFactory = { IrFactoryImplForJsIC(WholeWorldStageController()) },
|
||||
|
||||
@@ -50,6 +50,7 @@ enum class DirtyFileState(val str: String) {
|
||||
class CacheUpdater(
|
||||
mainModule: String,
|
||||
private val allModules: Collection<String>,
|
||||
private val mainModuleFriends: Collection<String>,
|
||||
cacheDir: String,
|
||||
private val compilerConfiguration: CompilerConfiguration,
|
||||
private val irFactory: () -> IrFactory,
|
||||
@@ -105,6 +106,11 @@ class CacheUpdater(
|
||||
}
|
||||
}
|
||||
|
||||
val mainModuleFriendLibraries = libraryDependencies.keys.let { libs ->
|
||||
val friendPaths = mainModuleFriends.mapTo(HashSet(mainModuleFriends.size)) { File(it).canonicalPath }
|
||||
libs.filter { it.libraryFile.canonicalPath in friendPaths }
|
||||
}
|
||||
|
||||
private val incrementalCaches = libraryDependencies.keys.associate { lib ->
|
||||
val libFile = KotlinLibraryFile(lib)
|
||||
val file = File(libFile.path)
|
||||
@@ -591,7 +597,12 @@ class CacheUpdater(
|
||||
val dirtyFileExports = updater.collectExportedSymbolsForDirtyFiles(modifiedFiles)
|
||||
|
||||
stopwatch.startNext("Modified files - loading and linking IR")
|
||||
val jsIrLinkerLoader = JsIrLinkerLoader(compilerConfiguration, updater.libraryDependencies, irFactory())
|
||||
val jsIrLinkerLoader = JsIrLinkerLoader(
|
||||
compilerConfiguration = compilerConfiguration,
|
||||
dependencyGraph = updater.libraryDependencies,
|
||||
mainModuleFriends = updater.mainModuleFriendLibraries,
|
||||
irFactory = irFactory()
|
||||
)
|
||||
var loadedIr = jsIrLinkerLoader.loadIr(dirtyFileExports)
|
||||
|
||||
var iterations = 0
|
||||
@@ -703,7 +714,7 @@ fun rebuildCacheForDirtyFiles(
|
||||
val dirtySrcFiles = dirtyFiles?.map { KotlinSourceFile(it) } ?: KotlinLoadedLibraryHeader(library).sourceFileFingerprints.keys
|
||||
val modifiedFiles = mapOf(libFile to dirtySrcFiles.associateWith { emptyMetadata })
|
||||
|
||||
val jsIrLoader = JsIrLinkerLoader(configuration, dependencyGraph, irFactory)
|
||||
val jsIrLoader = JsIrLinkerLoader(configuration, dependencyGraph, emptyList(), irFactory)
|
||||
val (jsIrLinker, irModules) = jsIrLoader.loadIr(KotlinSourceFileMap<KotlinSourceFileExports>(modifiedFiles), true)
|
||||
|
||||
val currentIrModule = irModules[libFile] ?: notFoundIcError("loaded fragment", libFile)
|
||||
|
||||
+13
-2
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.ir.util.irMessageLogger
|
||||
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
|
||||
import org.jetbrains.kotlin.library.KotlinLibrary
|
||||
import org.jetbrains.kotlin.library.uniqueName
|
||||
import org.jetbrains.kotlin.library.unresolvedDependencies
|
||||
import org.jetbrains.kotlin.psi2ir.descriptors.IrBuiltInsOverDescriptors
|
||||
import org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl
|
||||
@@ -40,8 +41,11 @@ internal fun JsIrLinker.loadUnboundSymbols(checkNoUnbound: Boolean) {
|
||||
internal class JsIrLinkerLoader(
|
||||
private val compilerConfiguration: CompilerConfiguration,
|
||||
private val dependencyGraph: Map<KotlinLibrary, List<KotlinLibrary>>,
|
||||
private val mainModuleFriends: Collection<KotlinLibrary>,
|
||||
private val irFactory: IrFactory
|
||||
) {
|
||||
private val mainLibrary = dependencyGraph.keys.lastOrNull() ?: notFoundIcError("main library")
|
||||
|
||||
@OptIn(ObsoleteDescriptorBasedAPI::class)
|
||||
private fun createLinker(loadedModules: Map<ModuleDescriptor, KotlinLibrary>): JsIrLinker {
|
||||
val signaturer = IdSignatureDescriptor(JsManglerDesc)
|
||||
@@ -50,7 +54,15 @@ internal class JsIrLinkerLoader(
|
||||
val typeTranslator = TypeTranslatorImpl(symbolTable, compilerConfiguration.languageVersionSettings, moduleDescriptor)
|
||||
val irBuiltIns = IrBuiltInsOverDescriptors(moduleDescriptor.builtIns, typeTranslator, symbolTable)
|
||||
val partialLinkageEnabled = compilerConfiguration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false
|
||||
return JsIrLinker(null, compilerConfiguration.irMessageLogger, irBuiltIns, symbolTable, partialLinkageEnabled, null)
|
||||
return JsIrLinker(
|
||||
currentModule = null,
|
||||
messageLogger = compilerConfiguration.irMessageLogger,
|
||||
builtIns = irBuiltIns,
|
||||
symbolTable = symbolTable,
|
||||
partialLinkageEnabled = partialLinkageEnabled,
|
||||
translationPluginContext = null,
|
||||
friendModules = mapOf(mainLibrary.uniqueName to mainModuleFriends.map { it.uniqueName })
|
||||
)
|
||||
}
|
||||
|
||||
private fun loadModules(): Map<ModuleDescriptor, KotlinLibrary> {
|
||||
@@ -86,7 +98,6 @@ internal class JsIrLinkerLoader(
|
||||
val loadedModules = loadModules()
|
||||
val jsIrLinker = createLinker(loadedModules)
|
||||
|
||||
val mainLibrary = dependencyGraph.keys.lastOrNull() ?: notFoundIcError("main library")
|
||||
val irModules = loadedModules.entries.associate { (descriptor, module) ->
|
||||
val libraryFile = KotlinLibraryFile(module)
|
||||
val modifiedStrategy = when {
|
||||
|
||||
@@ -122,6 +122,7 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
|
||||
private inner class TestStepInfo(
|
||||
val moduleName: String,
|
||||
val modulePath: String,
|
||||
val friends: List<String>,
|
||||
val expectedFileStats: Map<String, Set<String>>
|
||||
)
|
||||
|
||||
@@ -144,16 +145,23 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
|
||||
|
||||
val outputKlibFile = resolveModuleArtifact(module, buildDir)
|
||||
|
||||
val friends = mutableListOf<File>()
|
||||
if (buildKlib) {
|
||||
val dependencies = moduleStep.dependencies.mapTo(mutableListOf(File(STDLIB_KLIB))) {
|
||||
resolveModuleArtifact(it.moduleName, buildDir)
|
||||
val dependencies = mutableListOf(File(STDLIB_KLIB))
|
||||
for (dep in moduleStep.dependencies) {
|
||||
val klibFile = resolveModuleArtifact(dep.moduleName, buildDir)
|
||||
dependencies += klibFile
|
||||
if (dep.isFriend) {
|
||||
friends += klibFile
|
||||
}
|
||||
}
|
||||
val configuration = createConfiguration(module, projStep.language)
|
||||
buildArtifact(configuration, module, moduleSourceDir, dependencies, outputKlibFile)
|
||||
buildArtifact(configuration, module, moduleSourceDir, dependencies, friends, outputKlibFile)
|
||||
}
|
||||
return TestStepInfo(
|
||||
module.safeModuleName,
|
||||
outputKlibFile.canonicalPath,
|
||||
friends.map { it.canonicalPath },
|
||||
expectedFileStats
|
||||
)
|
||||
}
|
||||
@@ -228,10 +236,16 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
|
||||
for (projStep in projectInfo.steps) {
|
||||
val testInfo = projStep.order.map { setupTestStep(projStep, it, true) }
|
||||
|
||||
val mainModuleInfo = testInfo.last()
|
||||
testInfo.find { it != mainModuleInfo && it.friends.isNotEmpty() }?.let {
|
||||
error("module ${it.moduleName} has friends, but only main module may have the friends")
|
||||
}
|
||||
|
||||
val configuration = createConfiguration(projStep.order.last(), projStep.language)
|
||||
val cacheUpdater = CacheUpdater(
|
||||
mainModule = testInfo.last().modulePath,
|
||||
mainModule = mainModuleInfo.modulePath,
|
||||
allModules = testInfo.mapTo(mutableListOf(STDLIB_KLIB)) { it.modulePath },
|
||||
mainModuleFriends = mainModuleInfo.friends,
|
||||
cacheDir = buildDir.resolve("incremental-cache").absolutePath,
|
||||
compilerConfiguration = configuration,
|
||||
irFactory = { IrFactoryImplForJsIC(WholeWorldStageController()) },
|
||||
@@ -281,7 +295,12 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
|
||||
}
|
||||
|
||||
private fun buildArtifact(
|
||||
configuration: CompilerConfiguration, moduleName: String, sourceDir: File, dependencies: Collection<File>, outputKlibFile: File
|
||||
configuration: CompilerConfiguration,
|
||||
moduleName: String,
|
||||
sourceDir: File,
|
||||
dependencies: Collection<File>,
|
||||
friends: Collection<File>,
|
||||
outputKlibFile: File
|
||||
) {
|
||||
if (outputKlibFile.exists()) outputKlibFile.delete()
|
||||
|
||||
@@ -290,8 +309,12 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
|
||||
val sourceFiles = sourceDir.filteredKtFiles().map { environment.createPsiFile(it) }
|
||||
|
||||
val sourceModule = prepareAnalyzedSourceModule(
|
||||
projectJs, sourceFiles, configuration, dependencies.map { it.canonicalPath }, emptyList(), // TODO
|
||||
AnalyzerWithCompilerReport(configuration)
|
||||
project = projectJs,
|
||||
files = sourceFiles,
|
||||
configuration = configuration,
|
||||
dependencies = dependencies.map { it.canonicalPath },
|
||||
friendDependencies = friends.map { it.canonicalPath },
|
||||
analyzer = AnalyzerWithCompilerReport(configuration)
|
||||
)
|
||||
|
||||
val moduleSourceFiles = (sourceModule.mainModule as MainModule.SourceFiles).files
|
||||
|
||||
@@ -193,6 +193,7 @@ abstract class AbstractJsKLibABITestCase : KtUsefulTestCase() {
|
||||
val cacheUpdater = CacheUpdater(
|
||||
mainModule = mainModuleKlibFile.absolutePath,
|
||||
allModules = allDependencies.regularDependencies.map { it.path },
|
||||
mainModuleFriends = emptyList(),
|
||||
cacheDir = buildDir.resolve("libs-cache").absolutePath,
|
||||
compilerConfiguration = configuration,
|
||||
irFactory = { IrFactoryImplForJsIC(WholeWorldStageController()) },
|
||||
|
||||
+5
@@ -155,6 +155,11 @@ public class InvalidationTestGenerated extends AbstractInvalidationTest {
|
||||
runTest("js/js.translator/testData/incremental/invalidation/fastPath2/");
|
||||
}
|
||||
|
||||
@TestMetadata("friendDependency")
|
||||
public void testFriendDependency() throws Exception {
|
||||
runTest("js/js.translator/testData/incremental/invalidation/friendDependency/");
|
||||
}
|
||||
|
||||
@TestMetadata("functionDefaultParams")
|
||||
public void testFunctionDefaultParams() throws Exception {
|
||||
runTest("js/js.translator/testData/incremental/invalidation/functionDefaultParams/");
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
open class PublicClass {
|
||||
internal fun foo(): Int = 0
|
||||
internal val bar: Int = 1
|
||||
open internal fun baz(): Int = 2
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
open class PublicClass {
|
||||
internal fun foo(): Int = 1
|
||||
internal val bar: Int = 1
|
||||
open internal fun baz(): Int = 3
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
open class PublicClass {
|
||||
internal fun foo(): Int = 2
|
||||
internal val bar: Int = 1
|
||||
open internal fun baz(): Int = 3
|
||||
|
||||
inline internal fun foo_inline(): Int = 1
|
||||
inline internal val bar_inline: Int get() = 1
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
open class PublicClass {
|
||||
internal fun foo(): Int = 2
|
||||
internal val bar: Int = 1
|
||||
open internal fun baz(): Int = 3
|
||||
|
||||
inline internal fun foo_inline(): Int = 1
|
||||
inline internal val bar_inline: Int get() = 2
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
open class PublicClass {
|
||||
internal fun foo(): Int = 2
|
||||
internal val bar: Int = 1
|
||||
open internal fun baz(): Int = 3
|
||||
|
||||
inline internal fun foo_inline(): Int = 2
|
||||
inline internal val bar_inline: Int get() = 2
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
open class PublicClass {
|
||||
internal fun foo(): Int = 2
|
||||
internal val bar: Int = 2
|
||||
open internal fun baz(): Int = 3
|
||||
|
||||
inline internal fun foo_inline(): Int = 3
|
||||
inline internal val bar_inline: Int get() = 2
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
open class PublicClass {
|
||||
internal fun foo(): Int = 3
|
||||
internal val bar: Int = 2
|
||||
open internal fun baz(): Int = 3
|
||||
|
||||
inline internal fun foo_inline(): Int = 3
|
||||
inline internal val bar_inline: Int get() = 3
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
STEP 0:
|
||||
modifications:
|
||||
U : l1.0.kt -> l1.kt
|
||||
added file: l1.kt
|
||||
STEP 1:
|
||||
modifications:
|
||||
U : l1.1.kt -> l1.kt
|
||||
modified ir: l1.kt
|
||||
STEP 2:
|
||||
modifications:
|
||||
U : l1.2.kt -> l1.kt
|
||||
modified ir: l1.kt
|
||||
STEP 3:
|
||||
updated exports: l1.kt
|
||||
STEP 4:
|
||||
STEP 5:
|
||||
modifications:
|
||||
U : l1.5.kt -> l1.kt
|
||||
modified ir: l1.kt
|
||||
STEP 6:
|
||||
modifications:
|
||||
U : l1.6.kt -> l1.kt
|
||||
modified ir: l1.kt
|
||||
STEP 7:
|
||||
STEP 8:
|
||||
modifications:
|
||||
U : l1.8.kt -> l1.kt
|
||||
modified ir: l1.kt
|
||||
STEP 9:
|
||||
modifications:
|
||||
U : l1.9.kt -> l1.kt
|
||||
modified ir: l1.kt
|
||||
@@ -0,0 +1,7 @@
|
||||
fun box(stepId: Int): String {
|
||||
val x = test()
|
||||
if (x != stepId) {
|
||||
return "Fail: $x != $stepId"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
STEP 0:
|
||||
dependencies: lib1
|
||||
friends: lib1
|
||||
modifications:
|
||||
U : publicClass.0.kt -> publicClass.kt
|
||||
U : test.0.kt -> test.kt
|
||||
added file: m.kt, test.kt, publicClass.kt
|
||||
STEP 1:
|
||||
dependencies: lib1
|
||||
friends: lib1
|
||||
STEP 2:
|
||||
dependencies: lib1
|
||||
friends: lib1
|
||||
STEP 3:
|
||||
dependencies: lib1
|
||||
friends: lib1
|
||||
modifications:
|
||||
U : test.3.kt -> test.kt
|
||||
modified ir: test.kt
|
||||
updated exports: publicClass.kt
|
||||
STEP 4:
|
||||
dependencies: lib1
|
||||
friends: lib1
|
||||
modifications:
|
||||
U : test.4.kt -> test.kt
|
||||
modified ir: test.kt
|
||||
updated exports: publicClass.kt
|
||||
STEP 5..6:
|
||||
dependencies: lib1
|
||||
friends: lib1
|
||||
updated imports: test.kt, publicClass.kt
|
||||
STEP 7:
|
||||
dependencies: lib1
|
||||
friends: lib1
|
||||
modifications:
|
||||
U : test.7.kt -> test.kt
|
||||
modified ir: test.kt
|
||||
updated exports: publicClass.kt
|
||||
STEP 8..9:
|
||||
dependencies: lib1
|
||||
friends: lib1
|
||||
updated imports: publicClass.kt
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
class PublicClassHeir : PublicClass() {
|
||||
override internal fun baz(): Int = 4
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun test(): Int {
|
||||
val v = PublicClassHeir()
|
||||
return v.foo() + v.bar + v.baz() - 5
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun test(): Int {
|
||||
val v = PublicClassHeir()
|
||||
return v.foo() + v.bar + v.baz() + v.foo_inline() - 5
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun test(): Int {
|
||||
val v = PublicClassHeir()
|
||||
return v.foo() + v.bar + v.baz() + v.foo_inline() + v.bar_inline - 5
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun test(): Int {
|
||||
val v = PublicClassHeir()
|
||||
return v.foo() + v.bar + v.baz()
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
MODULES: lib1, main
|
||||
|
||||
STEP 0:
|
||||
libs: lib1, main
|
||||
dirty js: lib1, main
|
||||
STEP 1..2:
|
||||
libs: lib1, main
|
||||
dirty js: lib1
|
||||
STEP 3:
|
||||
libs: lib1, main
|
||||
dirty js: lib1, main
|
||||
STEP 4:
|
||||
libs: lib1, main
|
||||
dirty js: main
|
||||
STEP 5..9:
|
||||
libs: lib1, main
|
||||
dirty js: lib1, main
|
||||
Reference in New Issue
Block a user