Example #1
0
    def compile_function(self, func, argtypes, restype=None,
                         ctypes=False, **kwds):
        """
        Compile a python function given the argument types. Compile only
        if not compiled already, and only if an annotated numba function
        (in the future, use heuristics to determine whether a function
        should be compiled or not).

        Returns a triplet of (signature, llvm_func, python_callable)
        `python_callable` may be the original function, or a ctypes callable
        if the function was compiled.
        """
        if func is not None:
            result = self.get_function(func, argtypes)
            if result is not None:
                return result

            if is_numba_func(func):
                from numba import pipeline

                func = getattr(func, '_numba_func', func)
                compile_only = getattr(func, '_numba_compile_only', False)
                kwds['compile_only'] = kwds.get('compile_only', compile_only)
                # numba function, compile
                func_signature, translator, ctypes_func = pipeline.compile(
                                self.context, func, restype, argtypes,
                                ctypes=ctypes, **kwds)
                self.compiled_functions[func, tuple(func_signature.args)] = (
                                      func_signature, translator, ctypes_func)
                return func_signature, translator.lfunc, ctypes_func

        # print func, getattr(func, '_is_numba_func', False)
        # create a signature taking N objects and returning an object
        signature = ofunc(argtypes=ofunc.arg_types * len(argtypes)).signature
        return signature, None, func
Example #2
0
    def compile_function(self,
                         func,
                         argtypes,
                         restype=None,
                         ctypes=False,
                         **kwds):
        """
        Compile a python function given the argument types. Compile only
        if not compiled already, and only if an annotated numba function
        (in the future, use heuristics to determine whether a function
        should be compiled or not).

        Returns a triplet of (signature, llvm_func, python_callable)
        `python_callable` may be the original function, or a ctypes callable
        if the function was compiled.
        """
        if func is not None:
            result = self.get_function(func, argtypes)
            if result is not None:
                sig, trans, pycall = result
                return sig, trans.lfunc, pycall

            if is_numba_func(func):
                from numba import pipeline

                func = getattr(func, '_numba_func', func)
                compile_only = getattr(func, '_numba_compile_only', False)
                kwds['compile_only'] = kwds.get('compile_only', compile_only)
                # numba function, compile
                func_signature, translator, ctypes_func = pipeline.compile(
                    self.context,
                    func,
                    restype,
                    argtypes,
                    ctypes=ctypes,
                    **kwds)
                self.compiled_functions[func, tuple(func_signature.args)] = (
                    func_signature, translator, ctypes_func)
                return func_signature, translator.lfunc, ctypes_func

        # print func, getattr(func, '_is_numba_func', False)
        # create a signature taking N objects and returning an object
        signature = ofunc(argtypes=ofunc.arg_types * len(argtypes)).signature
        return signature, None, func
def _compile_methods(class_dict, context, ext_type, lmethods, method_pointers):
    parent_method_pointers = getattr(
                    ext_type.py_class, '__numba_method_pointers', None)
    for i, (method_name, func_signature) in enumerate(ext_type.methods):
        if method_name not in class_dict:
            # Inherited method
            assert parent_method_pointers is not None
            name, p = parent_method_pointers[i]
            assert name == method_name
            method_pointers.append((method_name, p))
            continue

        method = class_dict[method_name]
        # Don't use compile_after_type_inference, re-infer, since we may
        # have inferred some return types
        # TODO: delayed types and circular calls/variable assignments
        sig, translator, wrapper = pipeline.compile(context, method,
                                                    func_signature.return_type,
                                                    func_signature.args)
        lmethods.append(translator.lfunc)
        method_pointers.append((method_name, translator.lfunc_pointer))
        class_dict[method_name] = wrapper
Example #4
0
    def compile_function(self, func, argtypes, restype=None,
                         ctypes=False, **kwds):
        """
        Compile a python function given the argument types. Compile only
        if not compiled already, and only if it is registered to the function
        cache.

        Returns a triplet of (signature, llvm_func, python_callable)
        `python_callable` may be the original function, or a ctypes callable
        if the function was compiled.
        """
        # For NumbaFunction, we get the original python function.
        func = getattr(func, 'py_func', func)
        assert func in self.__compiled_funcs, func

        # get the compile flags
        flags = None # stub

        # Search in cache
        result = self.get_function(func, argtypes, flags)
        if result is not None:
            sig, trans, pycall = result
            return sig, trans.lfunc, pycall

        # Compile the function
        from numba import pipeline

        compile_only = getattr(func, '_numba_compile_only', False)
        kwds['compile_only'] = kwds.get('compile_only', compile_only)

        assert kwds.get('llvm_module') is None, kwds.get('llvm_module')

        compiled = pipeline.compile(self.context, func, restype, argtypes,
                                    ctypes=ctypes, **kwds)
        func_signature, translator, ctypes_func = compiled
    
        argtypes_flags = tuple(func_signature.args), flags
        self.__compiled_funcs[func][argtypes_flags] = compiled
        return func_signature, translator.lfunc, ctypes_func
Example #5
0
def _compile_methods(class_dict, context, ext_type, lmethods, method_pointers):
    parent_method_pointers = getattr(
                    ext_type.py_class, '__numba_method_pointers', None)
    for i, (method_name, func_signature) in enumerate(ext_type.methods):
        if method_name not in class_dict:
            # Inherited method
            assert parent_method_pointers is not None
            name, p = parent_method_pointers[i]
            assert name == method_name
            method_pointers.append((method_name, p))
            continue

        method = class_dict[method_name]
        # Don't use compile_after_type_inference, re-infer, since we may
        # have inferred some return types
        # TODO: delayed types and circular calls/variable assignments
        sig, translator, wrapper = pipeline.compile(context, method.py_func,
                                                    func_signature.return_type,
                                                    func_signature.args)
        lmethods.append(translator.lfunc)
        method_pointers.append((method_name, translator.lfunc_pointer))
        class_dict[method_name] = method.result(wrapper)