def mk_pipeline(
     cls,
     args,
     return_type=None,
     flags=None,
     locals={},
     library=None,
     typing_context=None,
     target_context=None,
 ):
     if not flags:
         flags = Flags()
     flags.nrt = True
     if typing_context is None:
         typing_context = registry.cpu_target.typing_context
     if target_context is None:
         target_context = registry.cpu_target.target_context
     return cls(
         typing_context,
         target_context,
         library,
         args,
         return_type,
         flags,
         locals,
     )
Exemple #2
0
 def autogenerate(cls):
     test_flags = ['fastmath', ]  # TODO: add 'auto_parallel' ?
     # generate all the combinations of the flags
     test_flags = sum([list(combinations(test_flags, x)) for x in range( \
                                                 len(test_flags)+1)], [])
     flag_list = []  # create Flag class instances
     for ft in test_flags:
         flags = Flags()
         flags.nrt = True
         flags.error_model =  'numpy'
         flags.__name__ = '_'.join(ft+('usecase',))
         for f in ft:
             setattr(flags, f, {
                 'fastmath': cpu.FastMathOptions(True)
             }.get(f, True))
         flag_list.append(flags)
     # main loop covering all the modes and use-cases
     for dtype in ('complex64', 'float64', 'float32', 'int32', ):
         for vlen in vlen2cpu:
             for flags in flag_list:
                 for mode in "scalar", "range", "prange", "numpy":
                     cls._inject_test(dtype, mode, vlen, flags)
     # mark important
     for n in ( "test_int32_range4_usecase",  # issue #3016
                 ):
         setattr(cls, n, tag("important")(getattr(cls, n)))
 def gen_ir(self, func, args_tuple, fastmath=False):
     with override_env_config("NUMBA_CPU_NAME",
                              "skylake-avx512"), override_env_config(
                                  "NUMBA_CPU_FEATURES", ""):
         _flags = Flags()
         _flags.fastmath = FastMathOptions(fastmath)
         _flags.nrt = True
         jitted = compile_isolated(func, args_tuple, flags=_flags)
         return jitted.library.get_llvm_str()
Exemple #4
0
 def test_mangled_flags_with_fastmath_parfors_inline(self):
     # at least for these control cases
     flags = Flags()
     flags.nrt = True
     flags.auto_parallel = True
     flags.fastmath = True
     flags.inline = "always"
     self.assertLess(len(flags.get_mangle_string()), len(flags.summary()))
     demangled = flags.demangle(flags.get_mangle_string())
     # There should be no pointer value in the demangled string.
     self.assertNotIn("0x", demangled)
 def mk_pipeline(cls, args, return_type=None, flags=None, locals={},
                 library=None, typing_context=None, target_context=None):
     if not flags:
         flags = Flags()
     flags.nrt = True
     if typing_context is None:
         typing_context = typing.Context()
     if target_context is None:
         target_context = cpu.CPUContext(typing_context)
     return cls(typing_context, target_context, library, args, return_type,
                flags, locals)
Exemple #6
0
    def test_usecase(self):
        n = 10
        obs_got = np.zeros(n)
        obs_expected = obs_got.copy()

        flags = Flags()
        flags.nrt = True
        cres = compile_isolated(usecase, (types.float64[:], types.intp),
                                flags=flags)
        cres.entry_point(obs_got, n)
        usecase(obs_expected, n)

        self.assertPreciseEqual(obs_got, obs_expected)
Exemple #7
0
    def test_demangle(self):
        def check(flags):
            mangled = flags.get_mangle_string()
            out = flags.demangle(mangled)
            # Demangle result MUST match summary()
            self.assertEqual(out, flags.summary())

        # test empty flags
        flags = Flags()
        check(flags)

        # test default
        check(DEFAULT_FLAGS)

        # test other
        flags = Flags()
        flags.no_cpython_wrapper = True
        flags.nrt = True
        flags.fastmath = True
        check(flags)
Exemple #8
0
 def test_demangling_from_mangled_symbols(self):
     """Test demangling of flags from mangled symbol"""
     # Use default mangler to mangle the string
     fname = 'foo'
     argtypes = types.int32,
     flags = Flags()
     flags.nrt = True
     flags.target_backend = "myhardware"
     name = default_mangler(
         fname,
         argtypes,
         abi_tags=[flags.get_mangle_string()],
     )
     # Find the ABI-tag. Starts with "B"
     prefix = "_Z3fooB"
     # Find the length of the ABI-tag
     m = re.match("[0-9]+", name[len(prefix):])
     size = m.group(0)
     # Extract the ABI tag
     base = len(prefix) + len(size)
     abi_mangled = name[base:base + int(size)]
     # Demangle and check
     demangled = Flags.demangle(abi_mangled)
     self.assertEqual(demangled, flags.summary())
Exemple #9
0
import heapq as hq
import itertools

import numpy as np

from numba import jit, typed
from numba.core.compiler import Flags
from numba.core.config import IS_WIN32
from numba.tests.support import TestCase, CompilationCache, MemoryLeakMixin

no_pyobj_flags = Flags()
no_pyobj_flags.nrt = True


def heapify(x):
    return hq.heapify(x)


def heappop(heap):
    return hq.heappop(heap)


def heappush(heap, item):
    return hq.heappush(heap, item)


def heappushpop(heap, item):
    return hq.heappushpop(heap, item)


def heapreplace(heap, item):
Exemple #10
0
try:
    import scipy
except ImportError:
    scipy = None

enable_pyobj_flags = Flags()
enable_pyobj_flags.enable_pyobject = True

force_pyobj_flags = Flags()
force_pyobj_flags.force_pyobject = True

no_pyobj_flags = Flags()

nrt_flags = Flags()
nrt_flags.nrt = True

tag = testing.make_tag_decorator(['important', 'long_running'])

_32bit = sys.maxsize <= 2**32
is_parfors_unsupported = _32bit
skip_parfors_unsupported = unittest.skipIf(
    is_parfors_unsupported,
    'parfors not supported',
)
skip_py38_or_later = unittest.skipIf(utils.PYVERSION >= (3, 8),
                                     "unsupported on py3.8 or later")

skip_unless_py10_or_later = unittest.skipUnless(utils.PYVERSION >= (3, 10),
                                                "needs Python 3.10 or later")
Exemple #11
0
 def test_mangled_flags_is_shorter(self):
     # at least for these control cases
     flags = Flags()
     flags.nrt = True
     flags.auto_parallel = True
     self.assertLess(len(flags.get_mangle_string()), len(flags.summary()))
Exemple #12
0
    def _cull_exports(self):
        """Read all the exported functions/modules in the translator
        environment, and join them into a single LLVM module.
        """
        self.exported_function_types = {}
        self.function_environments = {}
        self.environment_gvs = {}

        codegen = self.context.codegen()
        library = codegen.create_library(self.module_name)

        # Generate IR for all exported functions
        flags = Flags()
        flags.no_compile = True
        if not self.export_python_wrap:
            flags.no_cpython_wrapper = True
            flags.no_cfunc_wrapper = True
        if self.use_nrt:
            flags.nrt = True
            # Compile NRT helpers
            nrt_module, _ = nrtdynmod.create_nrt_module(self.context)
            library.add_ir_module(nrt_module)

        for entry in self.export_entries:
            cres = compile_extra(self.typing_context,
                                 self.context,
                                 entry.function,
                                 entry.signature.args,
                                 entry.signature.return_type,
                                 flags,
                                 locals={},
                                 library=library)

            func_name = cres.fndesc.llvm_func_name
            llvm_func = cres.library.get_function(func_name)

            if self.export_python_wrap:
                llvm_func.linkage = 'internal'
                wrappername = cres.fndesc.llvm_cpython_wrapper_name
                wrapper = cres.library.get_function(wrappername)
                wrapper.name = self._mangle_method_symbol(entry.symbol)
                wrapper.linkage = 'external'
                fnty = cres.target_context.call_conv.get_function_type(
                    cres.fndesc.restype, cres.fndesc.argtypes)
                self.exported_function_types[entry] = fnty
                self.function_environments[entry] = cres.environment
                self.environment_gvs[entry] = cres.fndesc.env_name
            else:
                llvm_func.name = entry.symbol
                self.dll_exports.append(entry.symbol)

        if self.export_python_wrap:
            wrapper_module = library.create_ir_module("wrapper")
            self._emit_python_wrapper(wrapper_module)
            library.add_ir_module(wrapper_module)

        # Hide all functions in the DLL except those explicitly exported
        library.finalize()
        for fn in library.get_defined_functions():
            if fn.name not in self.dll_exports:
                if fn.linkage in {Linkage.private, Linkage.internal}:
                    # Private/Internal linkage must have "default" visibility
                    fn.visibility = "default"
                else:
                    fn.visibility = 'hidden'
        return library
Exemple #13
0
import numpy as np

import unittest
from numba.core.compiler import compile_isolated, Flags
from numba import njit, typeof
from numba.core import utils, types, errors
from numba.tests.support import TestCase, tag
from numba.core.typing import arraydecl
from numba.core.types import intp, ellipsis, slice2_type, slice3_type

enable_pyobj_flags = Flags()
enable_pyobj_flags.enable_pyobject = True

Noflags = Flags()
Noflags.nrt = True


def slicing_1d_usecase(a, start, stop, step):
    return a[start:stop:step]


def slicing_1d_usecase2(a, start, stop, step):
    b = a[start:stop:step]
    total = 0
    for i in range(b.shape[0]):
        total += b[i] * (i + 1)
    return total


def slicing_1d_usecase3(a, start, stop):
Exemple #14
0
 def compile_parallel(self, func, arg_types):
     fast_pflags = Flags()
     fast_pflags.auto_parallel = cpu.ParallelOptions(True)
     fast_pflags.nrt = True
     fast_pflags.fastmath = cpu.FastMathOptions(True)
     return compile_isolated(func, arg_types, flags=fast_pflags).entry_point
Exemple #15
0

def unsupported_parfor(a, b):
    return np.dot(a, b)  # dot as gemm unsupported


def supported_parfor(n):
    a = np.ones(n)
    for i in prange(n):
        a[i] = a[i] + np.sin(i)
    return a


force_parallel_flags = Flags()
force_parallel_flags.auto_parallel = ParallelOptions(True)
force_parallel_flags.nrt = True


class DebugTestBase(TestCase):

    all_dumps = set([
        'bytecode', 'cfg', 'ir', 'typeinfer', 'llvm', 'func_opt_llvm',
        'optimized_llvm', 'assembly'
    ])

    def assert_fails(self, *args, **kwargs):
        self.assertRaises(AssertionError, *args, **kwargs)

    def check_debug_output(self, out, dump_names):
        enabled_dumps = dict.fromkeys(self.all_dumps, False)
        for name in dump_names:
Exemple #16
0
)
from numba.core.extending import intrinsic, include_path
from numba.core.typing import signature
from numba.core.imputils import impl_ret_untracked
from llvmlite import ir
import llvmlite.binding as llvm
import numba.core.typing.cffi_utils as cffi_support
from numba.core.unsafe.nrt import NRT_get_api

from numba.tests.support import (MemoryLeakMixin, TestCase, temp_directory,
                                 import_dynamic)
from numba.core.registry import cpu_target
import unittest

enable_nrt_flags = Flags()
enable_nrt_flags.nrt = True

linux_only = unittest.skipIf(not sys.platform.startswith('linux'),
                             'linux only test')
x86_only = unittest.skipIf(platform.machine() not in ('i386', 'x86_64'),
                           'x86 only test')


class Dummy(object):
    alive = 0

    def __init__(self):
        type(self).alive += 1

    def __del__(self):
        type(self).alive -= 1