How to call the externel C function in IR

Hello everyone. I want to call the function in the Dynamic Shared lib, do you know what code in python can do this? Thank you very much!

Hi @benjixu,

The following snippet of code might give you some directions.

from numba import njit
from numba.core import types, typing
from llvmlite import bindings

bindings.load_library_permanently('/path/to/shared_library.so')

# calls hypot from libm
# https://cplusplus.com/reference/cmath/hypot/
c_func_name = 'hypot'
return_type = types.float64
argty = types.float64
c_sig = typing.signature(return_type, argty)
c_hypot = types.ExternalFunction(c_func_name, c_sig)

@njit
def example(arg):
    return c_hypot(arg)

print(example(3.3))
2 Likes

@guilherme Thank you, this is very useful! Would it be worth adding such a snippet to the official documentation?

Sure, I can send a PR including this.

1 Like

Yes, I got that. btw, do you know how to handle the sse instructions in llvmlite? say for example, the CMPEQPS xmm2, xmm1
addps xmm1, xmm0, etc…

I want to write a similar IR statement like “addps”

Hi @benjixu, I only saw your message today.

Can you clarify exactly what are you trying to do? You want to emit SSE instructions from LLVM IR?

Regards