For what purpose is useIR used?

Viewed 4058

Something I noticed in build.gradle when I use android studio canary.

What exactly is its intended use?

kotlinOptions.useIR = true 
1 Answers

This is an option for the Kotlin compiler which is mostly useful if you plan on going multiplatform. Earlier this year the new Kotlin Compiler was announced to be stable and that was ready for use. See this post

Now, what's IR?

Compilers usually have mainly two components:

  • A frontend
  • A backend

A compiler frontend takes care to check your program is valid and makes sense by performing some syntactic and grammatic validation.

After the frontend is sure the program you've written is correct it proceeds to generate things like a derived syntax tree from your source files.

There's some discussion whether the task I'm about to describe is performed by a the frontend or a third module called a "middle-end".

Besides these data-structures, compiler frontends (or middleends), can also output something called IR which stands for (Intermediate Representation or Internal Representation) which basically is a simplified (using less complex instructions) version of your program.

This intermediate representation is later taken by the compiler backend to generate target code:

  • The Kotlin/JS backend outputs JavaScript code
  • The Kotlin/Native backend outputs llvm code
  • The Kotlin/JVM backend outputs Java byte code

Here's a diagram

enter image description here

Forgot to add it into the diagram but the last three boxes are all compiler backends

Now; with all this chatter:

What does the useIR option do? Essentially use the intermediate representation to generate the target code for your platform

Related