Oliver.O
11/20/2025, 11:34 AMkotlin("multiplatform"), but not kotlin("jvm"). Is this a known situation and will it change?tapchicoma
11/20/2025, 3:23 PMOliver.O
11/20/2025, 3:42 PMtapchicoma
11/20/2025, 3:43 PMtapchicoma
11/20/2025, 3:44 PMOliver.O
11/20/2025, 3:46 PMtapchicoma
11/20/2025, 3:46 PMdmitriy.novozhilov
11/21/2025, 7:52 AMFirLookupTrackerComponent.recordDirtyDeclaration).
Also you need to add irFile.fileForTopLevelPluginDeclarations = true for the IR file with the declaration you're creating, to mark it always dirty.dmitriy.novozhilov
11/21/2025, 7:53 AMdmitriy.novozhilov
11/21/2025, 7:58 AMLookupPredicate in the frontend and also generate the entrypoint on the frontend too (e.g. the logic will be "create entrypoint function if there was at least one function by this predicate"), then IC will work out of box.Oliver.O
11/21/2025, 12:25 PMSet<IrFile> of referenced files would help)?dmitriy.novozhilov
11/21/2025, 12:37 PMSo per your suggestion, would the predicate mechanism work if the plugin generates the annotations for top-level declarations of interest?No, this mechanism works only for annotations declared in the code.
For top-level symbol collectors, could you consider a more lightweight IC mechanism? Basically, a top-level symbol collector only dirties its entry point file. It does not necessarily change referenced files.The IC in its core works by the following algorithm • during the first compilation compiler records which fq names were referenced from which files • on the second compilation IC creates ABI dump difference, calculates what fqns could change and computes which files become dirty based on this information Note that "referenced" in this context is not equal to "resolved":
// 1.kt
package foo
fun foo() {}
// 2.kt
package bar
fun foo(x: Int) {}
// 3.kt
import foo.*
import bar.foo
fun test() {
foo()
}
In this example name foo.foo was referenced despite the fact that the call was actually resolved to bar.foo. So if IC detects that there was a change of a declaration with foo.foo fqname, 3.kt will become dirty.
So with compiler plugins things become much more complicated, as plugins usually are interested in declarations by some arbitrary predicate, which cannot be simplified to "fqname X was referenced from file Y" (even in your example you're interested in "any declaration with specific type"). We considered different approaches, but all of them fall into a problem that "arbitrary predicate" is too wide thing. So for now we started with an approach that
• any file, generated by the plugin is dirty, as it could depend on any new code which will be added on the second compilation
• any file referenced by the plugin is dirty, as we don't know exact logic what the plugin checked in that declartiondmitriy.novozhilov
11/21/2025, 12:43 PMirModuleFragment.files, so both frontend and backend plugins won't be able to reference/iterate over them, as they do in case of clean compilation.Oliver.O
11/21/2025, 1:46 PMKotlinCompilerPluginSupportPlugin.applyToCompilation()?tapchicoma
11/21/2025, 2:34 PMdmitriy.novozhilov
11/21/2025, 2:36 PMBut wouldn't the simple mechanism I suggested cover IC cases nicely, ...? Everything else you mentioned could just remain as is, including the IC model, right?Assume that we have files
a, b and c. a and b contain declarations you're interested in and you generate new d with something.
Then the user changes c by adding the declaration of type you're interested in. But with your suggestion IC considers a, b and d clean, so only new c is compiled, and your plugin generates new file f which contains generated code which processing declaration from c. But if you compile everything, then there would be only d generated with processing of declarations from a, b and c.Oliver.O
11/21/2025, 2:51 PMBut with your suggestion IC considersOK, I get the problem. I would want to dirty,aandbclean, so only newdis compiledc
d by seeing the new declaration in c, but at this time it's too late. In that case, we'd need an extra iteration to compile those files which were newly dirtied during an initial iteration (like KSP doing multi-iteration processing).
So am I right in assuming that a very simple solution could be to let a compiler plugin instruct the compiler to do a full compilation if files are added or removed?dmitriy.novozhilov
11/21/2025, 2:54 PMif files are added or removed?You mean input source files or output files produced by the plugin?
Oliver.O
11/21/2025, 2:55 PMdmitriy.novozhilov
11/21/2025, 2:57 PMOliver.O
11/21/2025, 2:58 PMc changed from being uninteresting to interesting.Oliver.O
11/21/2025, 2:58 PMdmitriy.novozhilov
11/21/2025, 2:59 PMa and b will be always recompiled, and your plugin will either produce the same result or more (if in the changed files there was something interesting for you).dmitriy.novozhilov
11/21/2025, 2:59 PMAnd that's why we're back to the predicates.You can achieve the same behavior manually in the backend, without predicates. I wrote how to do it above.
Oliver.O
11/21/2025, 3:01 PMOliver.O
11/25/2025, 11:45 PMentryPointFile.fileForTopLevelPluginDeclarations = true, the compiler fails with the error shown below. A reproducer is here (run ./gradlew integration-tests).
java.lang.IllegalStateException: Plugin generated file shouldn't have sources, but got .../testBalloon/integration-test/build/generated/testBalloon/src/commonTest/kotlin/EntryPointAnchor.kt
at org.jetbrains.kotlin.cli.common.output.OutputUtilsKt.writeAll$lambda$0(outputUtils.kt:64)
at org.jetbrains.kotlin.cli.common.output.OutputUtilsKt.writeAll(outputUtils.kt:32)
at org.jetbrains.kotlin.cli.common.output.OutputUtilsKt.writeAll(outputUtils.kt:59)
at org.jetbrains.kotlin.cli.jvm.compiler.CliCompilerUtilsKt.writeOutput(cliCompilerUtils.kt:104)
at org.jetbrains.kotlin.cli.jvm.compiler.CliCompilerUtilsKt.writeOutputsIfNeeded(cliCompilerUtils.kt:124)
at org.jetbrains.kotlin.cli.pipeline.jvm.JvmWriteOutputsPhase.executePhase(JvmWriteOutputsPhase.kt:17)
at org.jetbrains.kotlin.cli.pipeline.jvm.JvmWriteOutputsPhase.executePhase(JvmWriteOutputsPhase.kt:12)
at org.jetbrains.kotlin.cli.pipeline.PipelinePhase.phaseBody(PipelinePhase.kt:68)
at org.jetbrains.kotlin.cli.pipeline.PipelinePhase.phaseBody(PipelinePhase.kt:58)
at org.jetbrains.kotlin.config.phaser.NamedCompilerPhase.invoke(CompilerPhase.kt:102)
Could that be fixed in the compiler or would I have to use FIR to create the entry point file? If so, what would be the compiler APIs to use?dmitriy.novozhilov
11/26/2025, 10:44 AMIrFile or in some existing one? It should be new.
Could that be fixed in the compiler or would I have to use FIR to create the entry point file?Compiler works as expected here,
generatedForCompilerPlugin should be set only for generated files.Oliver.O
11/26/2025, 10:47 AMdmitriy.novozhilov
11/26/2025, 10:51 AMfun test(module: IrModuleFragment) {
val packageFqName = FqName.fromSegments("foo.bar".split("."))
val irFile = IrFileImpl(
NaiveSourceBasedFileEntryImpl("generatedFile.kt"),
IrFileSymbolImpl(),
packageFqName,
).also { it.generatedForCompilerPlugin = true }
module.files += irFile
}dmitriy.novozhilov
11/26/2025, 10:53 AMIrPluginContext.metadataDeclarationRegistrar.registerFunctionAsMetadataVisible.Oliver.O
11/26/2025, 10:55 AMmain function which does not (always) exist in tests.Oliver.O
11/26/2025, 10:56 AMgenerate() function and will report back how that works.Oliver.O
11/26/2025, 12:39 PMIrFileImpl has no symbol generatedForCompilerPlugin, but that does not seem to matter (the ISE from above no longer appears).
• Creating the IrFile in the backend produces an error on JS, Wasm/JS, Wasm/WASI and Native:
e: java.lang.AssertionError: The number of source files (1) does not match the number of IrFiles (2)
at org.jetbrains.kotlin.backend.common.serialization.SerializeModuleIntoKlibKt.serializeModuleIntoKlib(serializeModuleIntoKlib.kt:108)
at org.jetbrains.kotlin.ir.backend.js.KlibKt.serializeModuleIntoKlib(klib.kt:535)
at org.jetbrains.kotlin.cli.pipeline.web.WebKlibSerializationPipelinePhase.serializeFirKlib(WebKlibSerializationPipelinePhase.kt:69)
at org.jetbrains.kotlin.cli.pipeline.web.WebKlibSerializationPipelinePhase.executePhase(WebKlibSerializationPipelinePhase.kt:31)
at org.jetbrains.kotlin.cli.pipeline.web.WebKlibSerializationPipelinePhase.executePhase(WebKlibSerializationPipelinePhase.kt:24)
at org.jetbrains.kotlin.cli.pipeline.PipelinePhase.phaseBody(PipelinePhase.kt:68)
• on JVM, removing an "interesting" file from the sources, then compiling, then adding it back, then compiling, makes the plugin miss top-level symbols, so IC still does not work. In the compilation before (after the file was removed), the build report shows this error:
Incremental compilation was attempted but failed:
Failed to close caches: org.jetbrains.kotlin.incremental.CachesManagerCloseException: Failed to close cachesdmitriy.novozhilov
11/26/2025, 12:43 PMIn 2.3.0-RC, IrFileImpl has no symbol generatedForCompilerPlugin, but that does not seem to matter (the ISE from above no longer appears).My bad, the attribute is called fileForTopLevelPluginDeclarations.
Creating the IrFile in the backend produces an error on JS, Wasm/JS, Wasm/WASI and Native:I fixed it 2.3.20 (KT-58886).
dmitriy.novozhilov
11/26/2025, 12:44 PMon JVM, removing an "interesting" file from the sources, then compiling, then adding it back, then compiling, makes the plugin miss top-level symbols, so IC still does not work. In the compilation before (after the file was removed), the build report shows this error:That's interesting. Could you share a stable reproducer?
Oliver.O
11/26/2025, 12:56 PMthe attribute is called fileForTopLevelPluginDeclarations.I did set that, so everything good on that side.
I fixed it 2.3.20 (KT-58886).That's super cool as it would make the Gradle integration a lot easier.
That's interesting. Could you share a stable reproducer?Will do. AFK for an hour, but after that I'll put something together.
Oliver.O
11/26/2025, 3:22 PMdmitriy.novozhilov
11/27/2025, 9:15 AMExpected absolute path but found relative path: EntryPointAnchor.kt
So when I replaced NaiveSourceBasedFileEntryImpl(Constants.ENTRY_POINT_ANCHOR_FILE_NAME) with NaiveSourceBasedFileEntryImpl(File(Constants.ENTRY_POINT_ANCHOR_FILE_NAME).absolutePath) the test started to pass.Oliver.O
11/27/2025, 1:12 PMlookupTracker, fileMappingTracker and the synchronized lock)?
/**
* Registers a reference from [this] file to [referencedDeclaration], potentially residing in another file.
*
* This is required for incremental compilation.
*/
private fun IrFile.registerReference(referencedDeclaration: IrDeclarationWithName) {
val lookupTracker = configuration.lookupTracker ?: return
synchronized(lookupTracker) {
lookupTracker.record(
filePath = path,
position = Position.NO_POSITION,
scopeFqName = referencedDeclaration.fileParent.packageFqName.asString(),
scopeKind = ScopeKind.CLASSIFIER,
name = referencedDeclaration.name.asString()
)
configuration.fileMappingTracker?.recordSourceReferencedByCompilerPlugin(
File(referencedDeclaration.fileParent.path)
)
}
}dmitriy.novozhilov
11/27/2025, 1:15 PMOliver.O
11/27/2025, 1:36 PM> Task :compileTestKotlinLinuxX64 FAILED
e: Compilation failed: class org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl cannot be cast to class org.jetbrains.kotlin.ir.declarations.IrDeclaration (org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl and org.jetbrains.kotlin.ir.declarations.IrDeclaration are in unnamed module of loader java.net.URLClassLoader @42c72e21)
* Source files: File1.kt, File2.kt, log.kt
* Compiler version: 2.3.20-dev-4294
* Output kind: LIBRARY
e: java.lang.ClassCastException: class org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl cannot be cast to class org.jetbrains.kotlin.ir.declarations.IrDeclaration (org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl and org.jetbrains.kotlin.ir.declarations.IrDeclaration are in unnamed module of loader java.net.URLClassLoader @42c72e21)
at org.jetbrains.kotlin.ir.symbols.impl.IrSymbolBase.getDescriptor(IrSymbolImpl.kt:28)
at org.jetbrains.kotlin.ir.symbols.impl.IrFileSymbolImpl.getDescriptor(IrSymbolImpl.kt:20)
at org.jetbrains.kotlin.ir.declarations.IrPackageFragmentsKt.getPackageFragmentDescriptor(IrPackageFragments.kt:22)
at org.jetbrains.kotlin.backend.common.serialization.IrModuleSerializer.serializedIrModule(IrModuleSerializer.kt:45)
at org.jetbrains.kotlin.backend.common.serialization.SerializeModuleIntoKlibKt.serializeModuleIntoKlib(serializeModuleIntoKlib.kt:122)
at org.jetbrains.kotlin.backend.common.serialization.SerializeModuleIntoKlibKt.serializeModuleIntoKlib$default(serializeModuleIntoKlib.kt:92)
at org.jetbrains.kotlin.native.FirNativeSerializerKt.firSerializerBase-jlVyY7s(FirNativeSerializer.kt:53)
at org.jetbrains.kotlin.native.FirNativeSerializerKt.fir2IrSerializer(FirNativeSerializer.kt:34)
> Task :compileTestKotlinJs FAILED
e: java.lang.ClassCastException: class org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl cannot be cast to class org.jetbrains.kotlin.ir.declarations.IrDeclaration (org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl and org.jetbrains.kotlin.ir.declarations.IrDeclaration are in unnamed module of loader 'app')
at org.jetbrains.kotlin.ir.symbols.impl.IrSymbolBase.getDescriptor(IrSymbolImpl.kt:28)
at org.jetbrains.kotlin.ir.symbols.impl.IrFileSymbolImpl.getDescriptor(IrSymbolImpl.kt:20)
at org.jetbrains.kotlin.ir.declarations.IrPackageFragmentsKt.getPackageFragmentDescriptor(IrPackageFragments.kt:22)
at org.jetbrains.kotlin.backend.common.serialization.IrModuleSerializer.serializedIrModule(IrModuleSerializer.kt:45)
at org.jetbrains.kotlin.backend.common.serialization.SerializeModuleIntoKlibKt.serializeModuleIntoKlib(serializeModuleIntoKlib.kt:122)
at org.jetbrains.kotlin.ir.backend.js.KlibKt.serializeModuleIntoKlib(klib.kt:534)
at org.jetbrains.kotlin.cli.pipeline.web.WebKlibSerializationPipelinePhase.executePhase(WebKlibSerializationPipelinePhase.kt:40)
at org.jetbrains.kotlin.cli.pipeline.web.WebKlibSerializationPipelinePhase.executePhase(WebKlibSerializationPipelinePhase.kt:22)dmitriy.novozhilov
11/27/2025, 2:02 PMfun test(context: IrPluginContext) {
@OptIn(ObsoleteDescriptorBasedAPI::class)
val descriptor = EmptyPackageFragmentDescriptor(context.moduleDescriptor, FqName("my.file.package"))
val symbol = IrFileSymbolImpl(descriptor)
}
Unfortunately there are still some places which use descriptors.Oliver.O
11/27/2025, 2:04 PMdmitriy.novozhilov
11/27/2025, 2:06 PMOliver.O
11/27/2025, 4:44 PMsuspend fun main(), which previously got invoked automatically for test targets. On Native, I'm relying on a property annotated with kotlin.native.EagerInitialization. I'll check whether there were changes to these mechanisms. Otherwise, there might still be something missing in the way entry point file was created. (For the generated suspend fun main(), calling IrPluginContext.metadataDeclarationRegistrar.registerFunctionAsMetadataVisible had no effect.)Oliver.O
11/27/2025, 11:31 PMdmitriy.novozhilov
11/28/2025, 7:44 AMOliver.O
11/28/2025, 9:06 AMdmitriy.novozhilov
11/28/2025, 9:11 AMmain function and why it could not take it from the IrFile generated by the backend compiler plugin?
Does it maybe rely on some frontend information which is missing?Oliver.O
11/28/2025, 9:23 AMdmitriy.novozhilov
11/28/2025, 9:24 AMOliver.O
11/28/2025, 10:26 AMFirExtension which would allow me to directly generate an entry point FirFile. Seems like I would have to use FirDeclarationGenerationExtension.generateTopLevelClassLikeDeclaration(), and hope for FrontendFilesForPluginsGenerationPipelinePhase.createFilesWithGeneratedDeclarations() to create the synthetic entry point file for me, correct?dmitriy.novozhilov
11/28/2025, 10:31 AMdmitriy.novozhilov
11/28/2025, 10:32 AMhope forNot hope, but expect to.
svyatoslav.scherbina
11/28/2025, 1:51 PMDoes it maybe rely on some frontend information which is missing?It does. https://github.com/JetBrains/kotlin/blob/881790ddfa3bcfb3d32f57ac9d672bc34b2a63fe/[…]/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt
Oliver.O
11/28/2025, 2:32 PMIrFile generated by the backend compiler plugin. They are recognized if the entry point file is an external source file and the properties are generated for that source file by the backend compiler plugin. (The latter version cannot be used with IC, which is the reaseon for a change.)svyatoslav.scherbina
11/28/2025, 3:58 PM~/.konan/kotlin-native-prebuilt-$os-$arch-$version/bin/klib dump-ir $pathToKlib
show for the klib with such files? Are those properties present there?Oliver.O
11/28/2025, 4:16 PMval entryPointFile = IrFileImpl(
NaiveSourceBasedFileEntryImpl(Path(Constants.ENTRY_POINT_ANCHOR_FILE_NAME).absolutePathString()),
IrFileSymbolImpl(
@OptIn(ObsoleteDescriptorBasedAPI::class)
EmptyPackageFragmentDescriptor(pluginContext.moduleDescriptor, configuration.entryPointPackageName)
),
configuration.entryPointPackageName
).apply {
fileForTopLevelPluginDeclarations = true
}
moduleFragment.files += entryPointFilesvyatoslav.scherbina
11/28/2025, 4:26 PMFirFile.
For example:
https://github.com/JetBrains/kotlin/blob/bcebdacdc83504c9bf3a484871d9596303b46109/[…]/main/kotlin/org/jetbrains/kotlin/native/FirNativeSerializer.kt
->
https://github.com/JetBrains/kotlin/blob/a93c5a354c0397a19461fddaacc1e5b2345f6fc5/[…]/kotlin/backend/common/serialization/serializeModuleIntoKlib.ktOliver.O
11/28/2025, 4:31 PMmain() like) called at startup. I'm bringing my own test runner and do proper test reporting. I just need to have it called (the "standard" entry point can also get called, I don't mind). Ideally, there was one entry point mechanism in tests I could rely on for all targets.Oliver.O
11/29/2025, 12:15 AMsvyatoslav.scherbina
12/01/2025, 2:58 PMI'd also appreciate an easier solution: All I need is one entry point (Not sure how to achieve that with Kotlin/JVM. With Kotlin/Native, you can have this by passing the flagslike) called at startup. I'm bringing my own test runner and do proper test reporting. I just need to have it called (the "standard" entry point can also get called, I don't mind). Ideally, there was one entry point mechanism in tests I could rely on for all targets.main()
-entry your.package.main to the compiler in the linkDebugTest* task.
but seemingly never uses IC,There is no incremental compilation for K/N
compileKotlin* tasks.
For linkDebug* tasks, there is https://kotlinlang.org/docs/native-improving-compilation-time.html#try-incremental-compilation-of-klib-artifacts, but it is likely irrelevant here.Oliver.O
12/01/2025, 3:23 PM-entry your.package.main to the compiler in the linkDebugTest* task.
That would be an option if KGP lets me do that. I'll look into that.
> There is no incremental compilation for K/N compileKotlin* tasks.
Good to know. Then we're good on Native, as long as we use FIR. Thanks!Oliver.O
12/01/2025, 3:48 PMdmitriy.novozhilov
12/02/2025, 3:15 PMJVM fails with IC in different ways: If IR adds symbols to a FIR-generated entry point file, it fails with "Expected absolute path but found relative path" causing "Could not close incremental caches".I'm working on that in scope of KT-82809.
Oliver.O
12/02/2025, 3:17 PMOliver.O
12/08/2025, 10:03 PMincrementalJsKlib property of the Kotlin2JsCompile task class. I have created KT-82989 to address this.