Beispiel #1
0
 def __init__(self):
     self.module = lc.Module()
     # Current IR builder.
     self.builder = None
     self.func = None
     self.func_symtab = {}
     self.bbs = {}
Beispiel #2
0
 def _create_empty_module(self, name):
     ir_module = lc.Module(name)
     ir_module.triple = CUDA_TRIPLE[utils.MACHINE_BITS]
     if self._data_layout:
         ir_module.data_layout = self._data_layout
     nvvm.add_ir_version(ir_module)
     return ir_module
    def _context_builder_sig_args(self):
        typing_context = typing.Context()
        context = cpu.CPUContext(typing_context)
        module = lc.Module("test_module")

        sig = typing.signature(types.int32, types.int32)
        llvm_fnty = context.call_conv.get_function_type(
            sig.return_type, sig.args)
        function = module.get_or_insert_function(llvm_fnty, name='test_fn')
        args = context.call_conv.get_arguments(function)
        assert function.is_declaration
        entry_block = function.append_basic_block('entry')
        builder = lc.Builder(entry_block)

        return context, builder, sig, args
Beispiel #4
0
    def _context_builder_sig_args(self):
        typing_context = typing.Context()
        context = cpu.CPUContext(typing_context)
        lib = context.codegen().create_library("testing")
        with context.push_code_library(lib):
            module = lc.Module("test_module")

            sig = typing.signature(types.int32, types.int32)
            llvm_fnty = context.call_conv.get_function_type(
                sig.return_type, sig.args)
            function = module.get_or_insert_function(llvm_fnty, name="test_fn")
            args = context.call_conv.get_arguments(function)
            assert function.is_declaration
            entry_block = function.append_basic_block("entry")
            builder = lc.Builder(entry_block)

            yield context, builder, sig, args
Beispiel #5
0
 def _create_empty_module(self, name):
     ir_module = lc.Module(cgutils.normalize_ir_text(name))
     ir_module.triple = ll.get_process_triple()
     if self._data_layout:
         ir_module.data_layout = self._data_layout
     return ir_module
Beispiel #6
0
 def create_module(self, name):
     """Create a LLVM module
     """
     return lc.Module(name)
Beispiel #7
0
import llvmlite.llvmpy.core as ll_core


def create_execution_engine():
    """
    Create an ExecutionEngine suitable for JIT code generation on
    the host CPU.  The engine is reusable for an arbitrary number of
    modules.
    """
    import llvmlite.binding as llvm

    llvm.initialize()
    llvm.initialize_native_target()
    llvm.initialize_native_asmprinter()  # yes, even this one

    # Create a target machine representing the host
    target = llvm.Target.from_default_triple()
    target_machine = target.create_target_machine()
    # And an execution engine with an empty backing module
    backing_mod = llvm.parse_assembly("")
    engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
    return engine, target_machine


module = ll_core.Module('numpile.module')
engine, target = create_execution_engine()
function_cache = {}
Beispiel #8
0
 def _create_empty_module(self, name):
     ir_module = lc.Module(name)
     ir_module.triple = TRIPLE
     return ir_module
Beispiel #9
0
 def _create_empty_module(self, name):
     ir_module = lc.Module(name)
     ir_module.triple = ll.get_process_triple()
     if self._data_layout:
         ir_module.data_layout = self._data_layout
     return ir_module
Beispiel #10
0
 def _create_empty_module(self, name):
     ir_module = lc.Module(name)
     ir_module.triple = SPIR_TRIPLE[utils.MACHINE_BITS]
     if self._data_layout:
         ir_module.data_layout = self._data_layout
     return ir_module
Beispiel #11
0
    def test_cache(self):
        def times2(i):
            return 2*i

        def times3(i):
            return i*3

        def make_closure(x, y):
            def f(z):
                return y + z
            return f

        typing_context = typing.Context()
        context = cpu.CPUContext(typing_context)
        module = lc.Module("test_module")

        sig = typing.signature(types.int32, types.int32)
        llvm_fnty = context.call_conv.get_function_type(sig.return_type,
                                                        sig.args)
        function = module.get_or_insert_function(llvm_fnty, name='test_fn')
        args = context.call_conv.get_arguments(function)
        assert function.is_declaration
        entry_block = function.append_basic_block('entry')
        builder = lc.Builder(entry_block)

        # Ensure the cache is empty to begin with
        self.assertEqual(0, len(context.cached_internal_func))
        
        # After one compile, it should contain one entry
        context.compile_internal(builder, times2, sig, args)
        self.assertEqual(1, len(context.cached_internal_func))

        # After a second compilation of the same thing, it should still contain
        # one entry
        context.compile_internal(builder, times2, sig, args)
        self.assertEqual(1, len(context.cached_internal_func))

        # After compilation of another function, the cache should have grown by
        # one more.
        context.compile_internal(builder, times3, sig, args)
        self.assertEqual(2, len(context.cached_internal_func))

        sig2 = typing.signature(types.float64, types.float64)
        llvm_fnty2 = context.call_conv.get_function_type(sig2.return_type,
                                                         sig2.args)
        function2 = module.get_or_insert_function(llvm_fnty2, name='test_fn_2')
        args2 = context.call_conv.get_arguments(function2)
        assert function2.is_declaration
        entry_block2 = function2.append_basic_block('entry')
        builder2 = lc.Builder(entry_block2)
        
        # Ensure that the same function with a different signature does not
        # reuse an entry from the cache in error
        context.compile_internal(builder2, times3, sig2, args2)
        self.assertEqual(3, len(context.cached_internal_func))

        # Closures with distinct cell contents must each be compiled.
        clo11 = make_closure(1, 1)
        clo12 = make_closure(1, 2)
        clo22 = make_closure(2, 2)
        res1 = context.compile_internal(builder, clo11, sig, args)
        self.assertEqual(4, len(context.cached_internal_func))
        res2 = context.compile_internal(builder, clo12, sig, args)
        self.assertEqual(5, len(context.cached_internal_func))
        # Same cell contents as above (first parameter isn't captured)
        res3 = context.compile_internal(builder, clo22, sig, args)
        self.assertEqual(5, len(context.cached_internal_func))