News

The New JVM IR Backend Is Stable

Read this post in other languages:

(Updated)

The new backend is Stable and is itching to be made the new default. We need each of you to start adopting it, testing it, and giving us feedback so we can give it a final polish, let’s look at how you can start using it.

What is IR backend?
After the compiler frontend is done analyzing the code, the backend generates the executables. We have three backends: Kotlin/JVM, Kotlin/JS, and Kotlin/Native. The first two were historically written independently and didn’t share much code. When we started Kotlin/Native, it was based on a new infrastructure built around an internal representation (IR) for Kotlin code which serves a function somewhat similar to bytecode in virtual machines. We are now migrating the other two backends to the same IR. As a result, we will share a lot of the backend logic and have a unified pipeline, to allow most of the features, optimizations, and bug fixes to be done only once for all targets.
Having a common backend infrastructure opens the door for more multiplatform compiler extensions. It makes it possible to plug into the pipeline and add some custom processing and/or transformations that will automatically work across all targets.

We have been working to implement a new JVM IR backend as part of our ongoing project to rewrite the whole compiler. This new compiler will boost performance both for Kotlin users and for the Kotlin team itself by providing a versatile infrastructure that makes it easy to add new language features.

Our work on the JVM IR backend is nearly complete, and we’ll be making it Stable soon. Before we can do that, though, we need you to use it. In Kotlin 1.4.30, we’re making the new backend produce stable binaries, which means you will be able to safely use it in your projects. Read on to learn about the changes this new backend brings, as well as how to contribute to the process of finalizing this part of the compiler.

What changes with the new backend:

  • We’ve fixed a number of bugs that were present in the old backend.
  • The development of new language features will be much faster.
  • We will add all future performance improvements to the new JVM backend.
  • The new Jetpack Compose will only work with the new backend.

Another point in favor of starting to use the new JVM IR backend now is that it will become the default in Kotlin 1.5.0. Before we make it the default, we want to make sure we fix as many bugs as we can, and by adopting the new backend early you will help ensure that the migration is as smooth as possible.

To start using the new JVM IR backend

  1. Update the Kotlin dependency to 1.4.30 in your project.

  2. In the build configuration file, add the following lines to the target platform block of your project/module to turn the new compiler on.
    For Gradle add the following:

    • In Groovy
    • In Kotlin

    And for Maven add the following:

    For Android go with:

    To enable it for MPP use next in general cases

    But if you want to avoid creation of JVM target in projects where it’s not needed use this

  3. Please make a clean build and run tests after enabling the new backend to verify that your project compiles successfully.

You shouldn’t notice any difference, but if you do, please report it in YouTrack or send us a message in this Slack channel (get an invite here). When you do, please attach a list of steps to reproduce the problem and a code sample if possible.

You can switch back to the old backend anytime simply by removing the line from step two and rebuilding the project.

image description