# Where can I learn how to read the output of \`@code\_llvm\`?

**URL:** <https://discourse.julialang.org/t/where-can-i-learn-how-to-read-the-output-of-code-llvm/64075>\
**Category:** Tooling\
**Tags:** llvm\
**Created:** [July 5, 2021, 12:09pm UTC](https://discourse.julialang.org/t/where-can-i-learn-how-to-read-the-output-of-code-llvm/64075 "2021-07-05T12:09:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [July 5, 2021, 12:09pm UTC](https://discourse.julialang.org/t/where-can-i-learn-how-to-read-the-output-of-code-llvm/64075/1 "2021-07-05T12:09:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [July 5, 2021, 12:17pm UTC](https://discourse.julialang.org/t/where-can-i-learn-how-to-read-the-output-of-code-llvm/64075/2 "2021-07-05T12:17:02Z")

</div>

Together with the [LLVM LangRef](https://llvm.org/docs/LangRef.html), 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](https://github.com/maleadt/LLVM.jl/tree/master/examples) 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.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [July 5, 2021, 12:47pm UTC](https://discourse.julialang.org/t/where-can-i-learn-how-to-read-the-output-of-code-llvm/64075/3 "2021-07-05T12:47:36Z")

</div>

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](https://llvm.org/docs/Statepoints.html)

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [July 5, 2021, 12:55pm UTC](https://discourse.julialang.org/t/where-can-i-learn-how-to-read-the-output-of-code-llvm/64075/4 "2021-07-05T12:55:05Z")

</div>

> [@SteffenPL](#):
>
> I was wondering if there is some kind of reference for each command?

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

> [@SteffenPL](#):
>
> For example `pushq` seems to increment a counter for a GC system.

`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.

> [@SteffenPL](#):
>
> [Garbage Collection Safepoints in LLVM — LLVM 18.0.0git documentation](https://llvm.org/docs/Statepoints.html)

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

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [July 5, 2021, 1:01pm UTC](https://discourse.julialang.org/t/where-can-i-learn-how-to-read-the-output-of-code-llvm/64075/5 "2021-07-05T13:01:18Z")

</div>

Thanks for the help!

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