Beispiel #1
0
    def _handle_jit_decorator(self, func_def, decorator):
        from numba import ast_type_inference

        jit_args = ast_type_inference._parse_args(
            decorator,
            ['restype', 'argtypes', 'backend', 'target', 'nopython'])

        if decorator.args or decorator.keywords:
            restype = self._parse_restype(decorator, jit_args)
            if restype is not None and restype.is_function:
                signature = restype
            else:
                argtypes = self._parse_argtypes(decorator, func_def, jit_args)
                signature = minitypes.FunctionType(restype,
                                                   argtypes,
                                                   name=func_def.name)
        else:  #elif func_def.args:
            raise error.NumbaError(
                decorator, "The argument types and return type "
                "need to be specified")
        #else:
        #    signature = minitypes.FunctionType(None, [])

        # TODO: Analyse closure at call or outer function return time to
        # TODO:     infer return type
        # TODO: parse out nopython argument
        return signature
Beispiel #2
0
def infer(func, arg_types):
    sig = minitypes.FunctionType(return_type=None, args=arg_types)
    ast = functions._get_ast(func)

    sig, symtab, ast = ast_type_inference._infer_types(decorators.context,
                                                       func, ast, sig)
    return sig, symtab
Beispiel #3
0
 def run_pipeline(self, func):
     func_sig = minitypes.FunctionType(minitypes.void, [])
     source = inspect.getsource(func)
     astree = ast.parse(source)
     pipeline = decorators.context.numba_pipeline(decorators.context,
                                                  func, astree, func_sig)
     return pipeline.const_folding(astree)
Beispiel #4
0
def test_math_funcs():
    functions = get_functions()
    exceptions = 0
    for func_name in functions:
        # func_name = 'sqrt'
        func = functions[func_name]
        for dest_type in dest_types:
            signature = minitypes.FunctionType(None, [dest_type, dest_type])
            print(("executing...", func_name, signature))

            try:
                numba_func = jit(signature)(func)
            except error.NumbaError as e:
                exceptions += 1
                print((func_name, dest_type, e))
                continue

            x, y = 5.2, 6.9
            if dest_type.is_int:
                x, y = 5, 6

            r1 = numba_func(x, y)
            r2 = func(x, y)
            assert np.allclose(r1, r2), (r1, r2, signature, func_name)

    if exceptions:
        raise Exception
Beispiel #5
0
    def __init__(self,
                 signature,
                 func,
                 args,
                 keywords=None,
                 py_func=None,
                 **kw):
        if py_func and not kw.get('name', None):
            kw['name'] = py_func.__name__
        if signature is None:
            signature = minitypes.FunctionType(return_type=object_,
                                               args=[object_] * len(args))
            if keywords:
                signature.args.extend([object_] * len(keywords))

        super(ObjectCallNode, self).__init__(signature, args)
        assert func is not None
        self.function = func
        self.py_func = py_func

        self.args_tuple = ast.Tuple(elts=args, ctx=ast.Load())
        self.args_tuple.variable = Variable(
            numba_types.TupleType(size=len(args)))

        if keywords:
            keywords = [(ConstNode(k.arg), k.value) for k in keywords]
            keys, values = zip(*keywords)
            self.kwargs_dict = ast.Dict(list(keys), list(values))
            self.kwargs_dict.variable = Variable(minitypes.object_)
        else:
            self.kwargs_dict = NULL_obj

        self.type = signature.return_type
Beispiel #6
0
 def test():
     for argtype in [object_, float_, double]:
         # f_ = autojit(f)
         f_ = jit(minitypes.FunctionType(None, [argtype]))(f)
         for v in range(-10, 10):
             assert f_(v) == f(v)
             assert f_(float(v)) == f(float(v))
Beispiel #7
0
def compile2(env, func, restype=None, argtypes=None, ctypes=False,
             compile_only=False, **kwds):
    """
    Compile a numba annotated function.

        - decompile function into a Python ast
        - run type inference using the given input types
        - compile the function to LLVM
    """
    # Let the pipeline create a module for the function it is compiling
    # and the user will link that in.
    assert 'llvm_module' not in kwds
    kwds['llvm_module'] = lc.Module.new(module_name(func))
    logger.debug(kwds)
    func_ast = functions._get_ast(func)
    func_signature = minitypes.FunctionType(return_type=restype,
                                            args=argtypes)
    #pipeline, (func_signature, symtab, ast) = _infer_types2(
    #            env, func, restype, argtypes, codegen=True, **kwds)
    with env.TranslationContext(env, func, func_ast, func_signature,
                                need_lfunc_wrapper=not compile_only,
                                **kwds) as func_env:
        pipeline = env.get_pipeline(kwds.get('pipeline_name', None))
        func_ast.pipeline = pipeline
        post_ast = pipeline(func_ast, env)
        func_signature = func_env.func_signature
        symtab = func_env.symtab
        t = func_env.translator
    return func_env
Beispiel #8
0
def _process_signature(ext_type, method, default_signature,
                       is_static=False, is_class=False):
    if isinstance(method, minitypes.Function):
        # @double(...)
        # def func(self, ...): ...
        return _process_signature(ext_type, method.py_func,
                                  method.signature, is_static, is_class)
    elif isinstance(method, types.FunctionType):
        if default_signature is None:
            # TODO: construct dependency graph, toposort, type infer
            # TODO: delayed types
            raise error.NumbaError(
                "Method '%s' does not have signature" % (method.__name__,))
        validate_method(method, default_signature or object_(), is_static)
        if default_signature is None:
            default_signature = minitypes.FunctionType(return_type=None,
                                                       args=[])
        sig = get_signature(ext_type, is_class, is_static, default_signature)
        return Method(method, is_class, is_static), sig.return_type, sig.args
    else:
        if isinstance(method, staticmethod):
            is_static = True
        elif isinstance(method, classmethod):
            is_class = True
        else:
            return None, None, None

        method, restype, argtypes = _process_signature(
                        ext_type, method.__func__, default_signature,
                        is_static=is_static, is_class=is_class)

    return method, restype, argtypes
Beispiel #9
0
 def run_pipeline(self, func):
     func_sig = minitypes.FunctionType(minitypes.void, [])
     source = inspect.getsource(func)
     astree = ast.parse(source)
     with environment.TranslationContext(self.env, func, astree, func_sig):
         pipeline_callable = self.env.get_or_add_pipeline(
             'const_folding', pipeline.ConstFolding)
         astree = pipeline.AST3to2()(astree, self.env)
         ret_val = pipeline_callable(astree, self.env)
     return ret_val
Beispiel #10
0
def get_signature(ext_type, is_class, is_static, sig):
    if is_static:
        leading_arg_types = ()
    elif is_class:
        leading_arg_types = (object_,)
    else:
        leading_arg_types = (ext_type,)

    argtypes = leading_arg_types + sig.args
    restype = sig.return_type
    return minitypes.FunctionType(return_type=restype, args=argtypes)
Beispiel #11
0
    def __init__(self, object, attr, **kwargs):
        super(ExtensionMethod, self).__init__(**kwargs)
        ext_type = object.variable.type
        assert ext_type.is_extension
        self.value = object
        self.attr = attr

        method_type, self.vtab_index = ext_type.methoddict[attr]
        self.type = minitypes.FunctionType(return_type=method_type.return_type,
                                           args=method_type.args,
                                           is_bound_method=True)
Beispiel #12
0
def resolve_templates(ast, env):
    # TODO: Unify with decorators module
    crnt = env.translation.crnt
    if crnt.template_signature is not None:
        from numba import typesystem

        argnames = [name.id for name in ast.args.args]
        argtypes = list(crnt.func_signature.args)

        typesystem.resolve_templates(crnt.locals, crnt.template_signature,
                                     argnames, argtypes)
        crnt.func_signature = minitypes.FunctionType(
            return_type=crnt.func_signature.return_type,
            args=tuple(argtypes))

    return ast
Beispiel #13
0
def map_type(cffi_type):
    "Map CFFI type to numba type"
    if cffi_type.kind in ('struct', 'union'):
        if cffi_type.kind == 'union':
            result = None
        else:
            result = struct([(name, map_type(field_type))
                             for name, field_type in cffi_type.fields])
    elif cffi_type.kind == 'function':
        restype = map_type(cffi_type.result)
        argtypes = [map_type(arg) for arg in cffi_type.args]
        result = minitypes.FunctionType(
            restype, argtypes, is_vararg=cffi_type.ellipsis).pointer()
    else:
        result = type_map.get(cffi_type)

    if result is None:
        raise minierror.UnmappableTypeError(cffi_type)

    return result
Beispiel #14
0
def from_ctypes_value(value):
    """
    Convert a ctypes value to a numba type
    """
    from numba import typesystem

    if is_ctypes_type(value):
        # Value is a ctypes type, e.g. c_int
        return typesystem.CastType(from_ctypes_type(value))

    elif is_ctypes_function(value):
        # TODO: move this to from_ctypes_type
        if value.argtypes is None:
            warnings.warn("ctypes function %s has no argument types set" %
                          (value, ))
            return object_

        restype = from_ctypes_type(value.restype)
        argtypes = [from_ctypes_type(at) for at in value.argtypes]
        signature = minitypes.FunctionType(return_type=restype, args=argtypes)
        return signature

    elif is_ctypes_type(type(value)) or hasattr(value, '_type_'):
        # Value is a ctypes value, e.g. c_int(10)
        result_type = from_ctypes_type(type(value))

        if result_type.is_pointer:
            # Handle ctypes pointers
            try:
                ctypes.cast(value, ctypes.c_void_p)
            except ctypes.ArgumentError:
                pass
            else:
                addr_int = ctypes.cast(value, ctypes.c_void_p).value
                result_type = typesystem.KnownPointerType(
                    result_type.base_type, addr_int)

        return result_type

    else:
        raise NotImplementedError(value)
Beispiel #15
0
def function(return_type, argtypes):
    return minitypes.FunctionType(return_type, argtypes)
Beispiel #16
0
def sep201_signature_string(functype, name):
    functype = minitypes.FunctionType(functype.return_type, functype.args, name)
    return str(functype)
Beispiel #17
0
 def signature(self):
     return minitypes.FunctionType(return_type=self.return_type,
                                   args=self.arg_types,
                                   is_vararg=self.is_vararg)
Beispiel #18
0
 def __init__(self, ctypes_func, restype, argtypes, **kwds):
     super(CTypesFunctionType, self).__init__(**kwds)
     self.ctypes_func = ctypes_func
     self.signature = minitypes.FunctionType(return_type=restype,
                                             args=argtypes)
Beispiel #19
0
def _infer_types(context, func, restype=None, argtypes=None, **kwargs):
    ast = functions._get_ast(func)
    func_signature = minitypes.FunctionType(return_type=restype, args=argtypes)
    return run_pipeline(context, func, ast, func_signature, **kwargs)