# How to get LLVM IR of a function

**URL:** <https://numba.discourse.group/t/how-to-get-llvm-ir-of-a-function/1808>\
**Category:** Development\
**Created:** [March 3, 2023, 12:02am UTC](https://numba.discourse.group/t/how-to-get-llvm-ir-of-a-function/1808 "2023-03-03T00:02:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krafczyk](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/krafczyk/32/790_2.png) [@krafczyk](https://numba.discourse.group/u/krafczyk)\
**Post date:** [March 3, 2023, 12:02am UTC](https://numba.discourse.group/t/how-to-get-llvm-ir-of-a-function/1808/1 "2023-03-03T00:02:46Z")

</div>

I’d like to get the LLVM IR representation of a function just before Numba compiles that function to machine code. I want to play around with this representation, possibly editing it before finishing the compilation and executing the result.

I found a way of getting an llvm representation on stackoverflow was using the `inspect_llvm` method. I tried this, but `inspect_llvm` returns a string version of this representation which I then have to use to create an `numba.llvmlite.Module` type, however that `Module` lists no functions inside, so clearly I’ve done something wrong, or something has gone wrong here. Here’s what I did:

```auto
# Define function and jit it
@nb.jit
def func_1(x):
    return x**2

# Call with argument to trigger compile
func_1(1.)

# Get llvm string
llvm_str = func_1.inspect_llvm()[(nb.types.float64,)]

print(llvm_str)

# Build llvmlite Module
func_1_llvm_mod = nb.llvmlite.ir.Module(llvm_str)

print(func_1_llvm_mod.functions)

```

The second print statement just yields `[]`.

What is the right way to get this representation right now? Or have I misunderstood something? Currently I’m using numba 0.56.4

---

<div class="post-metadata">

**Author:** ![diptorupd](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/diptorupd/32/92_2.png) [@diptorupd](https://numba.discourse.group/u/diptorupd)\
**Post date:** [March 3, 2023, 3:42am UTC](https://numba.discourse.group/t/how-to-get-llvm-ir-of-a-function/1808/2 "2023-03-03T03:42:20Z")

</div>

It is a bit more involved than that. What you have done here:  
`func_1_llvm_mod = nb.llvmlite.ir.Module(llvm_str)` is to create an empty module with the string you passed in as the argument as the name of the module.

To convert the string to an LLVM module use the `llvlite.binding.module.parse_assembly` function.

For reference look at what Numba’s `CPUCodegen` does: [numba/codegen.py at 0830ee85cc8f7b5932afcd5d90591e9de4800e49 · numba/numba · GitHub](https://github.com/numba/numba/blob/0830ee85cc8f7b5932afcd5d90591e9de4800e49/numba/core/codegen.py#L1161)

---

<div class="post-metadata">

**Author:** ![krafczyk](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/krafczyk/32/790_2.png) [@krafczyk](https://numba.discourse.group/u/krafczyk)\
**Post date:** [March 3, 2023, 2:47pm UTC](https://numba.discourse.group/t/how-to-get-llvm-ir-of-a-function/1808/3 "2023-03-03T14:47:11Z")

</div>

That worked! Thanks a lot!
