Blog

Home / Blog

Kotlin Upgrade advances K2 Compiler

Billy Yann
Data Scientist
Deep learning and machine learning specialist, well-versed with experience in Cloud infrastructure, Block-chain technologies, and Big Data solutions.
December 06, 2023


Introduction: K2 Compiler

Recently, Kotlin have been working on a new frontend for their Kotlin compiler or K2. Here, the frontend of K2 has the ability to parse the codes while performing semantic analysis, data flow analysis, call resolution, and type interference. Computer developers interact with the frontend of compilers as it continuously runs inside the IDE and reports all error and warning messages. It also helps out with auto-completion, semantic-aware inspections, and re-factorings. Currently, the new frontend is available for preview as per Kotlin and they are continually polishing and stabilizing it with a plan to make it the default compiler frontend in the future Kotlin release. The future release will be named Kotlin 2.0. 

Kotlin 2.0 Release Roadmap

Before the release of Kotlin 2.0, Kotlin is strategically planning to release a more traditional version named Kotlin 1.9. It will include updates from the ongoing development process of the K2 compiler frontend as it approaches the optimized stabilization. With no Kotlin 1.10 in the wings, the next major version after Kotlin 1.9 will be Kotlin 2.0. 

Why Kotlin 2.0?

The new frontend is a major part of the compiler. Here, the K2 compiler frontend is a complete re-write from scratch based on a novel architecture and nit a re-factoring of the old compiler. The differences of these two have been published by Kotlin in various videos, conference talks, and blogs. The new architecture is faster and has a more extensible design. It addresses the technical debt in the old compiler frontend by fixing bugs and corner cases in various places. Thus, it makes inroads for future extensions of the language as per Kotlin. 

For various programming languages, the major version of Kotlin gets scaled-up when major features and breaking changes get introduced. Usually, for software programs, the major version can be incremented whenever anything gets substantially written that signifies a major rework. Irrespective of Kotlin being a language, the fronted developers are following the practice they use for software programs. Kotlin is a software. The version of Kotlin language gets defined by the versions of its compiler. The Kotlin provides specifications as a language reference. 

Will Kotlin break any codes?

Kotlin, the software, changes with every language version update. Here, the new language features get introduced while the old bugs get fixed. The Kotlin developers always try to minimize the number and impact of breaking changes that follow the Kotlin language evolution process. Kotlin 2.0 will also be the same in this regard. The developers of Kotlin are consciously avoiding the introduction of any new features in Kotlin 2.0. Here, the re-write of the Kotlin compiler frontend is a considerable change that introduces a number of improvements by itself. 

The new data flow analysis algorithms that got developed is more precise as it leads to better smart casts than were possible before. These algorithms change the red codes that will not compile in Kotlin 1.9 on the old frontend into green code, which compiles in Kotlin 2.0. It is technically a defining aspect of the new Kotlin language feature. It is a side-effect of the new compiler architecture. 

With the new compiler, tons of long-standing bugs have been fixed. Inevitably, some of these fixes might break code that accidentally relied on incorrect compiler behavior. Kotlin is carefully studying the impact of all the changes in behavior between the old and new K2 frontends. It makes additional efforts to replicate the old behavior whenever possible. Thus, it might affect a sizable number of Kotlin users. Here, Kotlin is committed to minimize the impact of migrating the code from Kotlin 1.9 to Kotlin 2.0. 

Kotlin will keep the users updated on Kotlin 2.0 stabilization process that provide one with ample time to try out the upgrades of Kotlin 2.0 on various projects in advance. So, one can report any regressions that they might have missed in their testing. The current work of Kotlin is focused on a beta-quality release of the K2 compiler frontend later this year that can track in the roadmap issue. 

IDE Support for Kotlin 2.0

The IDE plugin for Kotlin is bundled into the IntelliJ IDEA and Android Studio updates. It includes a copy of the Kotlin Compiler frontend for the semantic analysis of the codes. This is not the version of the Kotlin compiler, which is used to actually compile the code. Here, the actual version of the compiler is determined by the settings to build files. When the code gets compiled with Kotlin language version 2.0, the developers use the new compiler frontend. It will get all the performance benefits of the new compiler architecture. IDE might use the old frontend to inspect the code. In order to get the Kotlin performance benefits of the new compiler frontend in the IDE, Kotlin is writing a completely new version of the Kotlin IDE plugin, which is built on top of the K2 compiler frontend. Also, Kotlin is currently working towards the public preview of the new IDE plugin. 

Expectations after Kotlin 2.0

Here, the new architecture of the Kotlin 2.0 compiler frontend enables clean implementation of many novel features. Some of the features include KT-10648 context receivers, which is ready to preview in the old compiler frontend. However, they will not be turned on in Kotlin 2.0 by default. They remain in the preview stage for now. The stabilization of new features happen in future Kotlin 2.x releases. These also include the implementation of other new things in production pipeline of Kotlin. As usual, Kotlin share regular updates on their plans through the Kotlin roadmap. In future, there will be a Kotlin 3.0 with lots of big changes in the pipeline that include language-changes in the future. Kotlin will look into Kotlin Features Survey before implementing the new versions with changes. These changes will most certainly guarantee the next major Kotlin version. 

Kotlin new K2 Compiler – Performance + Extensibility

As aforementioned, Kotlin introduces the new K2 compiler. It offers a dual advantage that includes significantly faster compilation times with enhanced extensibility. Here, Kotlin has worked on a re-designed frontend and backend architectures. 

These employ new technologies such as the Frontend Intermediate Representation (FIR) and Intermediate Representation (IR). 

Frontend

For a compiler, the frontend is responsible for parsing the code. It helps to check syntax and semantics. This ensures that the code adheres to language rules and turns it into intermediate form, which the backend can understand. 

Backend

The backend undertakes the intermediate code and translate to machine-specific code, which makes it executable. 

Frontend Innovations

With frontend of the compiler, one can unpack the advancements. Kotlin, through K2 compiler, introduces the new concept of Frontend Intermediate Representation (FIR). FIR consolidates the elements of syntax trees and semantic information. The benefits of FIR include faster compilation, enhanced IDE performance, aligning with traditional compiler principles. Faster compilation results in a 2X speed improvement as per Google in call resolution, one of the most slowest and time intensive and consuming compiler processes. Error reporting and auto completion gets done by the enhanced IDE performances. 

Backend Innovations

The Intermediate Representation (IR) works well with backend innovation of compilers. The two primary implications include extensibility and multi backend support. The extensibility helps developers to create custom compiler extensions. With multi backend support, IR acts as a common language. Thus, it makes it easier to support different backend that include JVM and JS. Here, Kotlin 1.5 employs the new JVM IR backend and Kotlin 1.8 uses the JS IR backend. 

The Balance between Performance and Extensibility

With Kotlin K2 compilers, while the frontend is laser-focused on performance improvements, the backend focuses on extensibility. The advancement of frontend speed up operations that ensure a 2X performance gain whereas the new IR structure of backend allows a modular and plug-and-play approach. It makes it easier introduce new language features. 

In effect, the K2 compiler is a transformation of compilers, which positions Kotlin 2.0 as a faster and more extensible language. Thus, from the significant speed improvements in code compilation to the ability to extend the language capabilities, K2 shapes up to be a novel game-changer for Kotlin. 

Kotlin upgrade advances K2 compiler

Recently, the 1.9.20 version of Kotlin language by JetBrains moves the high performing K2 compiler to a beta stage. As per October 31, Kotlin 1.9.20 got announced with the code available on GitHub and update instructions. The newest version puts the K2 compiler in beta stage for all platforms. These include JVM, JavaScript, WebAssembly, and native languages. These get tried in any Kotlin projects. The new version also brings the K2 support in the kapt compiler plug-in. 

Conclusion

Here, K2 is intended to increase the compilation performance. So, it is slated to be production ready with the Kotlin 2.0 version. The compiler is positioned to speed up the language feature development. It unifies all platforms that support Kotlin, and offer a better architecture for multi-platform projects. The new features of K2 compiler include improved performance for the garbage collector in Kotlin and Native languages. It also includes the support for the WASI (WebAssembly System Interface) API in the standard library for Kotlin/Wasm.