Where can I learn how to read the output of `@code_llvm`?

I often ask myself which optimisations the Julia compiler does and find the answer in the output of @code_llvm.

The tutorials (I found so far) on LLVM IR are all about how to write a compiler. Is there a quicker way to learn how to read LLVM IR?

1 Like

Together with the LLVM LangRef, you can just run @code_llvm of various Julia functions and try to decode how it maps to the functions definition (or output of @code_typed). For each instruction you generate that you don’t understand, read the description for it in the LangRef (sounds obvious, but you learn a lot by doing this).

You could also try out the LLVM.jl examples in LLVM.jl/examples at master · maleadt/LLVM.jl · GitHub to get a feel for what kind of LLVM code you can generate and what it does.

I also like to read the LLVM codegen tests in LLVM’s repository (at test/CodeGen) when I’m working on LLVM-based compilers just to see what kind of code patterns one could see in the wild.

3 Likes

Thanks for the hints! Sounds logical.

I was wondering if there is some kind of reference for each command?
For example pushq seems to increment a counter for a GC system. But to figure out that I need to read a bit on the general description and understand the example code from context:
https://llvm.org/docs/Statepoints.html

That’s what the LangRef is for :slight_smile: Everything should be in there; just Ctrl-F for the one you’re looking at.

pushq is not an LLVM instruction, that’s an x86 (native) instruction for pushing a 64-bit register onto the stack. This isn’t something that really exists in LLVM IR, since stack details are usually handled by the LLVM target for you automatically.

It’s my understanding that we don’t use LLVM’s GC infrastructure, and instead manually implement our own.

Thanks for the help!

A lot to learn, but this will help me to get started :slight_smile:

1 Like