diff options
| author | Joel Klinghed <the_jk@opera.com> | 2026-11-24 00:55:37 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@opera.com> | 2026-11-24 00:55:37 +0100 |
| commit | 8c126e9a0370ace22b9293ea7616a59a25e5e89d (patch) | |
| tree | ee465d78a123aa320c3befc06f133dd190ee78ca /native-image/README.md | |
| parent | 2871d7a506a12656adb36998be7684f664727503 (diff) | |
native-image/kotlin-2.4.21-native-image.patch is the change to the
Kotlin repository at v2.4.21. It bundles parcelize and plugin jars from
outside the repository, adds the constructor metadata that the plugin
loader needs, and adds G1, PGO and GraalVM home options. It also has the
reachability metadata collected from OFA modules.
native-image/build.sh runs the plain, instrumented and PGO builds, and
native-image/README.md describes the full procedure and the results.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'native-image/README.md')
| -rw-r--r-- | native-image/README.md | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/native-image/README.md b/native-image/README.md new file mode 100644 index 0000000..31ec4dd --- /dev/null +++ b/native-image/README.md @@ -0,0 +1,246 @@ +# Building the Kotlin compiler native image for OFA + +The stub jar runs a GraalVM native image of the Kotlin compiler +(`kotlin.proxy.nativeImage`). The JetBrains release of that image only bundles +some compiler plugins, and loads no others. OFA uses parcelize and +kotlin-empty-to-string, so it needs its own image. This document records how the +`kotlin-2.4.21-native-image-x86_64-ofa-1` image was built. + +Files in this directory: + +| file | what it is | +|---|---| +| `kotlin-2.4.21-native-image.patch` | changes to `prepare/compiler-native-image` in the Kotlin repository at tag `v2.4.21` | +| `build.sh` | runs the native image build with the options below | + +Tools used from `../tools`: + +| tool | what it is | +|---|---| +| `collect_metadata.py` | replays a compile from a Kotlin daemon log with the GraalVM tracing agent, to collect reachability metadata | +| `ic_check.sh` | checks that incremental compilation gives correct output | + +## Requirements + +- Oracle GraalVM for JDK 25, in `/usr/lib/jvm/java-25-graalvm` (or set + `GRAALVM_HOME`). GraalVM Community Edition can build the image, but it has + no G1 GC and no PGO, which give most of the speed. +- The Kotlin repository: `git clone https://github.com/JetBrains/kotlin`, + then `git checkout v2.4.21`. +- An OFA checkout with the `kotlinProxyStubJar` / `kotlinProxyNativeImage` + support in `KotlinConventionPlugin`, and a Gradle cache that holds + `kotlin-empty-to-string-compiler` (any OFA Kotlin build downloads it). + +The first build of the Kotlin repository takes a long time. + +## Why the image needs changes + +In Kotlin 2.4.21, `PluginCliParser` reads `META-INF/services` from each +`-Xplugin` jar, then creates the registrar and the command line processor with +`Class.forName(name).newInstance()`. In the native image, `Class.forName` +finds only classes that are compiled into the image. So each plugin needs two +things: + +1. Its classes on the native image classpath. +2. Reflection metadata for the no-arg constructors of its registrar and + command line processor. + +The file `META-INF/org/jetbrains/kotlin/bundled-compiler-plugins.txt` in the +image is not read at runtime in 2.4.21. Plugin jars are matched by their +service files, not by name. + +## The patch + +`kotlin-2.4.21-native-image.patch` changes +`prepare/compiler-native-image/build.gradle.kts`: + +- Bundles parcelize (`:kotlin-parcelize-compiler`). +- `kotlin.build.native-image.extra-plugins` bundles plugin jars from outside + the Kotlin repository, for kotlin-empty-to-string. Format: + `pluginId;registrarFqName;commandLineProcessorFqName;jarPrefixes;jarPath`, + entries separated by `|`. The jar must be compiled against + kotlin-compiler-embeddable. +- `generateBundledPluginsMetadata` writes the constructor metadata for all + bundled plugins onto the image classpath. +- `kotlin.build.native-image.use-g1`, `pgo-instrument`, `pgo-profile` and + `extra-args` add `--gc=G1`, `--pgo-instrument`, `--pgo=<file>` and other + native-image options. The names match the Kotlin master branch. +- `kotlin.build.native-image.graalvm-home` selects GraalVM directly. Oracle + GraalVM reports `Oracle Corporation` as vendor, so the `GRAAL_VM` toolchain + request in 2.4.21 does not find it. + +The patch also changes the reachability metadata in +`resources/META-INF/native-image/org/jetbrains/kotlin/kotlin-compiler-embeddable/reachability-metadata.json`. +The JetBrains metadata was collected from Kotlin-only tests. OFA compiles Java +and Kotlin sources together, which uses IntelliJ PSI code that those tests do +not reach. The added metadata came from these steps: + +1. `collect_metadata.py` runs on modules that use different features: + + | module | why | + |---|---| + | `coreutils` | Java and Kotlin sources, parcelize | + | `live_score` | sources generated by KSP, parcelize, serialization | + | `ndc` | serialization, protobuf sources | + | `rewards` | compose, parcelize, serialization | + | `app` | the largest module | + +2. Six IntelliJ resource bundles are added by hand. The tracing agent did not + record them, and `:app` failed with `MissingResourceException: Can't find + bundle for base name messages.JavaPsiBundle`. The bundles are + `messages.CoreBundle`, `messages.CoreDeprecatedMessagesBundle`, + `messages.JavaErrorBundle`, `messages.JavaOptionBundle`, + `messages.JavaPsiBundle` and `messages.UtilBundle`. They are all the + `messages/*.properties` files in `kotlin-compiler-embeddable.jar`. + +## Procedure + +### 1. Apply the patch + +```bash +cd ~/source/kotlin +git checkout v2.4.21 +git apply ~/source/kotlin_proxy/native-image/kotlin-2.4.21-native-image.patch +``` + +The build with `-PdeployVersion` also changes +`libraries/stdlib/src/kotlin/util/KotlinVersion.kt`. That change is +generated, it is not part of the patch. + +### 2. Build a plain image + +```bash +~/source/kotlin_proxy/native-image/build.sh plain +``` + +The image is in `prepare/compiler-native-image/build/dist`. `build.sh` uses +`-march=x86-64-v3`, so the image runs on the IQ builders. Do not use +`-march=native` for an image that leaves the build machine. + +### 3. Build OFA with the image, and collect missing metadata + +Build the stub jars (`make` in this repository), then set in OFA +`mobile/mobile/gradle-local.properties`: + +```properties +kotlinProxyStubJar=/home/<user>/source/kotlin_proxy/dist/kotlin-proxy-compiler-stubs.jar +kotlinProxyNativeImage=/home/<user>/source/kotlin/prepare/compiler-native-image/build/dist +``` + +Stop old Kotlin daemons, then build all Kotlin modules: + +```bash +pkill -f 'KotlinCompile[D]aemon' +./gradlew compileDebugKotlin --continue +``` + +A module that fails with one of these errors needs more reachability metadata: + +- `PicoInitializationException: Either do the specified parameters not match + any of the following constructors:[]` +- `ClassNotFoundException` or `NoSuchMethodException` from compiler code. + +The failed compile is in `/tmp/kotlin-daemon.*.log`, with its arguments. Run: + +```bash +~/source/kotlin_proxy/tools/collect_metadata.py <module> [<module> ...] +``` + +`<module>` is the `-module-name` of the compile, for example `coreutils`. +The script merges the metadata into the Kotlin checkout. Then repeat steps 2 +and 3 until all modules build. + +A `MissingResourceException` for a resource bundle needs a hand-made entry in +the `resources` list of `reachability-metadata.json`: +`{"bundle": "messages.<Name>"}`. + +### 4. Check incremental compilation + +```bash +~/source/kotlin_proxy/tools/ic_check.sh jvm # must pass, else the test is wrong +~/source/kotlin_proxy/tools/ic_check.sh proxy +``` + +The native image compiler runs in a subprocess. The Kotlin daemon gets no +output or lookup data from it, so incremental builds leave stale class files. +`KotlinConventionPlugin` disables incremental compilation when the proxy is +used. All four checks must pass in proxy mode. + +### 5. Collect PGO profiles + +```bash +~/source/kotlin_proxy/native-image/build.sh instrument +``` + +Add a profile directory to `gradle-local.properties`. Each compile writes one +`.iprof` file there. + +```properties +kotlinProxyPgoProfileDir=/home/<user>/kotlin-pgo +``` + +Stop old Kotlin daemons, then compile all modules: + +```bash +pkill -f 'KotlinCompile[D]aemon' +./gradlew compileDebugKotlin --rerun-tasks +``` + +Merge the profiles: + +```bash +/usr/lib/jvm/java-25-graalvm/lib/svm/bin/native-image-utils merge-pgo-profiles \ + --input-dir=/home/<user>/kotlin-pgo --output-file=/home/<user>/kotlin.iprof +``` + +Then remove `kotlinProxyPgoProfileDir` from `gradle-local.properties`. + +### 6. Build the final image + +```bash +~/source/kotlin_proxy/native-image/build.sh pgo /home/<user>/kotlin.iprof +``` + +### 7. Package and upload + +`bin/` must be at the root of the archive. OFA passes the extracted directory +as `kotlinProxyNativeImage`. + +```bash +cd ~/source/kotlin/prepare/compiler-native-image/build/dist +tar -cJf ~/kotlin-2.4.21-native-image-x86_64-ofa-1.tar.xz . +sha256sum ~/kotlin-2.4.21-native-image-x86_64-ofa-1.tar.xz +``` + +Upload the archive and `dist/kotlin-proxy-compiler-stubs-nolog.jar` (as +`kotlin-proxy-compiler-stubs-<version>.jar`) to +`https://build.android.service.osa/storage/`. Update the file names and the +SHA-256 checksums in OFA `mobile/mobile/build/iq/kotlin_proxy.py`. Use a new +`ofa-N` suffix for a new image of the same Kotlin version. + +## Results + +`./gradlew compileDebugKotlin --rerun-tasks`, OFA, 2026-11-23: + +| compiler | build time | sum of Kotlin compile tasks | `:app` compile | +|---|---|---|---| +| JVM daemon | 2m00s | 151.4s | 50.8s | +| native image, G1, no PGO | about 2m00s | | | +| native image, G1, PGO | 1m48s | 131.6s | 39.2s | + +Each compile starts a new process with cold caches. Modules that compile in +less than 3s are not faster than with the JVM daemon. The large modules give +the gain. Without incremental compilation, the image is not useful for +developer builds, only for clean builds on IQ. + +## When Kotlin is updated + +The patch, the metadata and the checksums are for Kotlin 2.4.21. For a new +Kotlin version: + +1. Check out the new tag, and apply the patch. Fix conflicts in + `build.gradle.kts`. +2. Set `KOTLIN_VERSION` in `build.sh`, and the kotlin-empty-to-string version + for the new Kotlin version. +3. Do steps 2 to 7. Keep the hand-made bundle entries in the metadata. +4. Regenerate the stub jars for the new Kotlin version (see `../README.md`). |
