Blog Infos
Author
Published
Topics
Published
Topics

Mount K2 photo by Daniel Born on Unsplash

 

Kotlin is now 13 years old! Yeah, a teenager. And it’s starting to show its rebellious nature now! You know with Kotlin Multiplatform and all, Kotlin wants to be the one-stop solution. And like any good, caring parent, JetBrains supports Kotlin with its great ambition. All of this starts with the new Kotlin compiler, K2.

New compiler, sure! But why?

Before we try to understand the changes in the compiler, we need to understand the motivations behind the changes. Here’s a list of the grounds.

  • Kotlin as a language & its syntax has been constantly evolving. The compiler needs to evolve to cater for these changes
  • The compiler needs to be more efficient. The first compiler was never meant to be fast; it only enabled rapid software development.
  • Better multithreading & multiplatform support. With Kotlin taking charge of compiling code for different platforms and targets, it becomes important for the compiler to support different features of the platform while also being efficient at compiling the Kotlin code.
  • Better compiler structures. Compilers use different data structures to store semantic information about the code. The Abstract Syntax Tree implementation in Kotlin compiler is not very beneficial for future roadmap.

This video by JetBrains does a great job of explaining the reasons in depth.

The K2 Compiler

The Kotlin compiler can be thought of as a black box. This box inputs source files, dependencies, flags and settings, and throws out JVM Classes, JS Files or Native objects.

Kotlin compiler as a black box

If we look into the high-level structure of the K2 compiler, there are two parts to it, the Frontend and the Backend.

High-level compiler structure

K2 Frontend

Kotlin K2 brings a rewritten frontend. The new frontend uses a new data structure to store program information, Frontend Intermediate Representation (FIR). The FIR is a Syntax Tree however there’s a significant shift from the Syntax Tree used in the K1 compiler. The FIR stores semantic information about the program for faster compilation, whereas in the K1 compiler a map of maps, called BindingContext, is used to look up semantic information at each node.

K2 Frontend Stages

The Frontend performs its tasks in stages

  • Parser: The Parser takes in the source code and generates a Parse Tree. It starts by running the lexer which converts the source code into tokens. These tokens are then arranged as nodes of the Parse Tree. It’s a Concrete Syntax Tree (CST), which means no information is dropped. All whitespace, comments, parenthesis etc. are stored in the Parse Tree. This tree is also called the Program Structure Interface (PSI) Tree.
    More about Concrete Syntax Tree can be about here.
  • Raw FIR Builder: This stage generates a Raw FIR from the Parse Tree. Raw FIR is an Abstract Syntax Tree (AST), where tokens that have no further meaning to the program are dropped. Whitespaces, Comments, Parentheses etc are dropped here. This stage also performs some desugaring of the syntax to make it clear and less ambiguous for the following stages of the compiler.
    More information on Abstract Syntax Tree can be found here.

Examples of Desugarring in Raw FIR Builder

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

Jobs

  • Analyser: This is the most complex stage of the frontend. It does the most amount of work on the FIR to compile the code. At each step, the analyser keeps FIR updated with the most up-to-date semantic information for each node.
    The analyser uses a top-down approach to resolve candidates for function and property invocation, as well as to resolve overload information.
    Import resolution, type resolution & name resolution are some of the important tasks performed by the analyser.
    The updated FIR then becomes the output for the Frontend, as well as the input for the final diagnostic stage
  • Checker: FIR is analysed for warnings & errors, and the diagnostic information is generated as another output from the front end.
K2 Backend

There have been some changes in the Kotlin compiler’s backend as well, however, it is important to understand that performance improvement is NOT a goal for K2 Backend. Instead, the team at JetBrains wants to make it easy for different backends to share logic, support new language features and also open the floor for the community to develop backend compiler plugins.

Kotlin K2 streamlines its backends by sharing a lot of logic among the different backends for JVM, JS and Native targets. A backend for WASM is also under development and available as an experimental backend.

All K2 Backends take FIR as an input, which passes through a backend tool called FIR2IR. As the name suggests, the FIR is converted into Intermediate Representation (IR) by this tool. This IR is well known by the backend. Each of the backends has its own generators which use this IR to generate .class, .js or native .so files for the target.

Trying out the K2 compiler

Starting with Kotlin 1.9, an alpha build of the K2 compiler is available to try out. To enable compilation against the new compiler, add the following line to your gradle.properties, or use the gradle wrapper to pass the flag.

// gradle.properties
kotlin.experimental.tryK2=true

// OR

./gradlew build -Pkotlin.experimental.tryK2=true

The K2 development team at JetBrains highly recommends that we developers try out the new compiler and provide our feedback directly in Kotlin Slack via the #k2-early-adopters channel.

K2 in Android

Experimental support for the K2 compiler is available in Android Studio Giraffe 223 & Hedgehog 231 builds. KSP, Compose & Android Studio stable support is under active development and should be available in the next year or so.

To get started with Kotlin K2 in your current Android project, add the following snippet to your build.gradle file

kotlin {
  sourceSets.all {
    languageSettings {
      languageVersion = "2.0"
    }
  }
}
Further readings

This article aims at providing a brief overview of what’s new in the Kotlin compiler K2. If you want to understand each of the stages deeper, the following articles and videos contain good information.

Kotlin is now 13 years old! Yeah, a teenager. And it's starting to show its rebellious nature now! You know with Kotlin Multiplatform and all, Kotlin wants to be the one-stop solution. And like any good, caring parent, JetBrains supports Kotlin with its great ambition. All of this starts with the new Kotlin compiler, K2.
Kotlin is now 13 years old! Yeah, a teenager. And it's starting to show its rebellious nature now! You know with Kotlin Multiplatform and all, Kotlin wants to be the one-stop solution. And like any good, caring parent, JetBrains supports Kotlin with its great ambition. All of this starts with the new Kotlin compiler, K2.

https://blog.jetbrains.com/kotlin/2021/10/the-road-to-the-k2-compiler/

Kotlin is now 13 years old! Yeah, a teenager. And it's starting to show its rebellious nature now! You know with Kotlin Multiplatform and all, Kotlin wants to be the one-stop solution. And like any good, caring parent, JetBrains supports Kotlin with its great ambition. All of this starts with the new Kotlin compiler, K2.

https://medium.com/google-developer-experts/crash-course-on-the-kotlin-compiler-k1-k2-frontends-backends-fe2238790bd8

Thank you for making it to the end! You can find my other articles here. Massive thanks to Enrique for his support in getting this article out!
I also speak on various Android & Kotlin topics in meetups & conferences. Check out my previous talks here.

If you want to support me with the work I do, feel free to buy me a coffee!

 

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu