New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for out-of-line CFFI modules #1454
Conversation
- Update tests to test with an out-of-line module, and mixing calls to inline and out-of-line functions - Update documentation
# We have no record of the function's address in an OOL module, so we | ||
# resort to looking up the function by name in the interpreter's | ||
# symbol table using ctypes | ||
cfunc = getattr(ctypes.pythonapi, cffi_func.__name__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only worked the test is using sin
and cos
symbols, which are in the "C" namespace. This will not work for other user defined symbols; for example:
from cffi import FFI
ffi = FFI()
ffi.cdef("int foo(int a, int b, int c);")
ffi.set_source("_example",
"""
static int foo(int a, int b, int c)
{
return a + b * c;
}
""")
ffi.compile()
import _example
from numba import cffi_support, njit
cffi_support.register_module(_example)
foo = _example.lib.foo
print(foo(1, 2, 3))
@njit
def bar(x, y, z):
return foo(x, y, z)
print(bar(1, 2, 3))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you need is:
addr = mod.ffi.addressof(mod.lib, 'foo') # get function pointer
ptr = int(mod.ffi.cast("uintptr_t", addr)) # cast to uintptr_t and then to python int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like this patch: https://gist.github.com/sklam/7c89e0ec668bc326c92f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great suggestion, many thanks! Will update the PR and add your example as a test.
I've updated this to work with user-defined functions as per your patch, and added a test calling the |
Support for out-of-line CFFI modules
Out-of-line CFFI modules appear differently to inline ones - they are all of type
builtin_function_or_method
. This PR allows registering an out-of-line CFFI module with Numba so that it can build a mapping of types for all functions in the module, and therefore compile calls to them in jitted functions.