Retrieving Generated LLVM from Numba

After compiling the Python function with Numba, for example:

from numba import jit

@jit
def sum(x, y):
    return x + y

How can I get the generated LLVM code (as a string) of a compiled function?

It looks like this was available in a previous version of Numba via the lfunc property of the compiled function, but this does not work.

Similar functionality also appeared in the form of resetting the generated LLVM assembly (at compile time). However, it doesn't seem to work anymore if I'm not mistaken. In any case, executing a terminal command will not be ideal, as I would really like the code in Python, although I know that I can do this with a subprocess.

This is an attempt to create a portable version of Python code at runtime that will be translated; I welcome any suggestions regarding this.

+4
2

, , -, :

from numba.compiler import compile_isolated

# second argument specifies the argument types to the sum function
cfunc = compile_isolated(sum, (types.int64, types.int64))

# get llvm IR as string
llvm_code_str = str(cfunc.llvm_module)

, , ( , ).

IRL llvm , NUMBA_DUMP_LLVM = 1 python script ( numba, bin Anaconda bin Numba: numba --dump-llvm test.py).

+2

, numba version 0.18.0 ( ), LLVM IR inspect_llvm inspect_asm jitted,

@jit(nopython=True,nogil=True)
def mysum(a,b):
     return a+b

# First run the function with arguments for the code to get generated
a, b = np.random.rand(10), np.random.rand(10)

# Get the llvm IR
mysum.inspect_llvm()
# Get the assembly code
mysum.inspect_asm()

, . , ( ),

for v, k in mysum.inspect_llvm().items():
    print(v, k)

define i32 @__main__9mysum_...
    ...
    ...
    ...

, , for LLVM

vmovsd  (%r10), %xmm0
vaddsd  (%rcx), %xmm0, %xmm0
vmovsd  %xmm0, -32(%rbx,%rbp)
vmovsd  (%r10), %xmm0
vaddsd  (%rcx), %xmm0, %xmm0
vmovsd  %xmm0, -24(%rbx,%rbp)
vmovsd  (%r10), %xmm0
vaddsd  (%rcx), %xmm0, %xmm0
vmovsd  %xmm0, -16(%rbx,%rbp)
vmovsd  (%r10), %xmm0
vaddsd  (%rcx), %xmm0, %xmm0
vmovsd  %xmm0, -8(%rbx,%rbp)
vmovsd  (%r10), %xmm0
vaddsd  (%rcx), %xmm0, %xmm0
vmovsd  %xmm0, (%rbx,%rbp)
vmovsd  (%r10), %xmm0
vaddsd  (%rcx), %xmm0, %xmm0
vmovsd  %xmm0, 8(%rbx,%rbp)
vmovsd  (%r10), %xmm0
vaddsd  (%rcx), %xmm0, %xmm0
vmovsd  %xmm0, 16(%rbx,%rbp)
vmovsd  (%r10), %xmm0
vaddsd  (%rcx), %xmm0, %xmm0
vmovsd  %xmm0, 24(%rbx,%rbp)
addq    $8, %rdx
addq    $64, %rbx
cmpq    %rdx, %rdi
jne LBB0_48    
+3

All Articles