Support for out-of-line CFFI modules #1454

Merged
merged 2 commits into from Oct 5, 2015

Projects

None yet

3 participants

@gmarkall
Contributor
gmarkall commented Oct 2, 2015

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.

@gmarkall gmarkall Support for out-of-line cffi modules
- Update tests to test with an out-of-line module, and mixing
  calls to inline and out-of-line functions
- Update documentation
452ca8a
@sklam sklam and 1 other commented on an outdated diff Oct 2, 2015
numba/typing/cffi_utils.py
def get_pointer(cffi_func):
"""
Get a pointer to the underlying function for a CFFI function as an
integer.
"""
+ if cffi_func in _ool_func_types:
+ # 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__)
@sklam
sklam Oct 2, 2015 Contributor

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))
@sklam
sklam Oct 2, 2015 Contributor

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 
@gmarkall
gmarkall Oct 5, 2015 Contributor

That's a great suggestion, many thanks! Will update the PR and add your example as a test.

@gmarkall
Contributor
gmarkall commented Oct 5, 2015

I've updated this to work with user-defined functions as per your patch, and added a test calling the foo function.

@seibert seibert merged commit f788235 into numba:master Oct 5, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment