is there any way to have the compiler dump IR from...
# compiler
j
is there any way to have the compiler dump IR from the CLI or API? i think a great addition to the Kotlin support on godbolt would be the IR output in addition to Java bytecode
s
There's a flag to dump IR between lowerings, probably you could use that. Cannot remember the exact incantation though, need to look it up.
Found the one I used in my presentation a few years ago, seems to still work:
Copy code
-Xdump-directory=${buildDir}/ir/
-Xphases-to-dump-before=ValidateIrBeforeLowering
for phases, you can use "all", it will just dump them numbered iirc
j
Does the before mean I'll missing the final lowering? Or is the final lowering the platform transition?
Or I guess the validate lowering is effectively a no op as far as the IR itself is concerned
Also thanks!
s
The names of lowerings are hardcoded in compiler, you can try "all" in that place, and it will print them in order with the names attached There's also variations of these APIs, e.g. -phases-to-dump-after, if you are interested in capturing complete state (maybe there's one which is like before and after) The example above was made to capture IR before anything happens to it for plugin development, so it is intentionally using the first thing in the list
j
Got a link to your presentation?
Also looks like its ALL (case-sensitive)
Cannot get this to work. I'm using -Xlist-phases which works, but I have your two flags and nothing shows up in the dir. Tried both ALL and a named phase.
s
Droidcon deleted the talk, which is whole another rant 😅 Weird, maybe something changed, I tried it a year ago last time, will look into it if I have time 🙂
A small catch here is that you actually need to make sure KotlinCompile was executed (e.g. caches are absent or dirty), otherwise it understandably doesn't dump anything
j
Maybe some quirk of native? I'll try JS or JVM.
s
Ah, it is JS and JVM only, from what I see
j
🤦
of course i'm trying to diagnose a native compiler error that results in LLVM IR validation failure
🥲 1
s
A bit late to the party, but in addition to compiler flags, there is also a system property to dump IR in Kotlin-like format:
org.jetbrains.kotlin.compiler.ir.dump.strategy=KotlinLike
. It is way easier to read compared to original tree dump, but it is harder to match it with in-memory IR nodes.
s
Yep, that Although it could be a bit harder to track compiler bugs in the Kotlin-like code, as they obscure descriptor/symbol definitions somehow