Esempio n. 1
0
    def test_multi_module_linking(self):
        # generate external library module
        m = Module.new('external-library-module')
        fnty = Type.function(Type.int(), [Type.int(), Type.int()])
        libfname = 'myadd'
        func = m.add_function(fnty, libfname)
        bb = func.append_basic_block('')
        bldr = Builder.new(bb)
        bldr.ret(bldr.add(*func.args))
        func.verify()

        # JIT the lib module and bind dynamic symbol
        libengine = EngineBuilder.new(m).mcjit(True).create()
        myadd_ptr = libengine.get_pointer_to_function(func)
        le.dylib_add_symbol(libfname, myadd_ptr)

        # reference external library
        m = Module.new('user')
        fnty = Type.function(Type.int(), [Type.int(), Type.int()])
        func = m.add_function(fnty, 'foo')
        bb = func.append_basic_block('')
        bldr = Builder.new(bb)
        extadd = m.get_or_insert_function(fnty, name=libfname)
        bldr.ret(bldr.call(extadd, func.args))
        func.verify()

        # JIT the user module
        engine = EngineBuilder.new(m).mcjit(True).create()
        ptr = engine.get_pointer_to_function(func)
        self.assertEqual(myadd_ptr,
                         engine.get_pointer_to_named_function(libfname))

        from ctypes import c_int, CFUNCTYPE
        callee = CFUNCTYPE(c_int, c_int, c_int)(ptr)
        self.assertEqual(321 + 123, callee(321, 123))
Esempio n. 2
0
 def test_enginebuilder_with_tm(self):
     tm = le.TargetMachine.new()
     module = self.make_test_module()
     self.assertTrue(llvmpy.capsule.has_ownership(module._ptr._ptr))
     ee = EngineBuilder.new(module).create(tm)
     self.assertFalse(llvmpy.capsule.has_ownership(module._ptr._ptr))
     self.run_foo(ee, module)
Esempio n. 3
0
    def test_enginebuilder_basic(self):
        module = self.make_test_module()
        ee = EngineBuilder.new(module).create()

        with self.assertRaises(llvm.LLVMException):
            # Ensure the module is owned.
            llvm._util.check_is_unowned(module)

        self.run_foo(ee, module)
Esempio n. 4
0
    def test_enginebuilder_with_tm(self):
        tm = le.TargetMachine.new()
        module = self.make_test_module()
        ee = EngineBuilder.new(module).create(tm)

        with self.assertRaises(llvm.LLVMException):
            # Ensure the targetmachine is owned.
            llvm._util.check_is_unowned(tm)
        

        with self.assertRaises(llvm.LLVMException):
            # Ensure the module is owned.
            llvm._util.check_is_unowned(module)

        self.run_foo(ee, module)
Esempio n. 5
0
    def test_mcjit(self):
        m = Module.new('oidfjs')
        fnty = Type.function(Type.int(), [Type.int(), Type.int()])
        func = m.add_function(fnty, 'foo')
        bb = func.append_basic_block('')
        bldr = Builder.new(bb)
        bldr.ret(bldr.add(*func.args))

        func.verify()

        engine = EngineBuilder.new(m).mcjit(True).create()
        ptr = engine.get_pointer_to_function(func)

        from ctypes import c_int, CFUNCTYPE
        callee = CFUNCTYPE(c_int, c_int, c_int)(ptr)
        self.assertEqual(321 + 123, callee(321, 123))
Esempio n. 6
0
    def get_callable(self, entry_point_name, entry_point_typesig):
        """
        Returns a python callable that dispatches to the requested C function.
        """

        # get llvm represetation of function
        ll_function = self.ll_module.get_function_named(entry_point_name)

        # run jit compiler
        from llvm.ee import EngineBuilder

        self.exec_engine = \
            EngineBuilder.new(self.ll_module).mcjit(True).opt(3).create()

        c_func_ptr = self.exec_engine.get_pointer_to_function(ll_function)

        # cast c_func_ptr to python callable using ctypes
        return entry_point_typesig(c_func_ptr)
Esempio n. 7
0
    def func_template(self, ty, op):
        m = Module.new('dofjaa')
        fnty = Type.function(ty, [ty, ty])
        fn = m.add_function(fnty, 'foo')
        bldr = Builder.new(fn.append_basic_block(''))
        bldr.ret(getattr(bldr, op)(*fn.args))

        engine = EngineBuilder.new(m).mcjit(True).create()
        ptr = engine.get_pointer_to_function(fn)

        from ctypes import c_uint32, c_uint64, c_float, c_double, CFUNCTYPE

        maptypes = {
            Type.int(32): c_uint32,
            Type.int(64): c_uint64,
            Type.float(): c_float,
            Type.double(): c_double,
        }
        cty = maptypes[ty]
        prototype = CFUNCTYPE(*[cty] * 3)
        callee = prototype(ptr)
        callee(12, 23)
Esempio n. 8
0
import sys
import platform
import llvm

from llvm.core import Module
from llvm.ee import EngineBuilder

m = Module.new("fjoidajfa")
eb = EngineBuilder.new(m)
target = eb.select_target()

print("target.triple=%r" % target.triple)
if sys.platform == "darwin":
    s = {"64bit": "x86_64", "32bit": "x86"}[platform.architecture()[0]]
    assert target.triple.startswith(s + "-apple-darwin")

assert llvm.test(verbosity=2) == 0

print("llvm.__version__: %s" % llvm.__version__)
# assert llvm.__version__ == '0.12.0'
Esempio n. 9
0
 def _build_engine(self, mod, mattrs):
     if mattrs:
         return EngineBuilder.new(mod).mattrs(mattrs).create()
     else:
         return EngineBuilder.new(mod).create()
Esempio n. 10
0
    def test_enginebuilder_opt(self):
        module = self.make_test_module()
        ee = EngineBuilder.new(module).opt(3).create()

        self.run_foo(ee, module)
Esempio n. 11
0
    def test_enginebuilder_force_jit(self):
        module = self.make_test_module()
        ee = EngineBuilder.new(module).force_jit().create()

        self.run_foo(ee, module)
Esempio n. 12
0
 def test_enginebuilder_basic(self):
     module = self.make_test_module()
     self.assertTrue(llvmpy.capsule.has_ownership(module._ptr._ptr))
     ee = EngineBuilder.new(module).create()
     self.assertFalse(llvmpy.capsule.has_ownership(module._ptr._ptr))
     self.run_foo(ee, module)
Esempio n. 13
0
File: jit.py Progetto: sklam/mlvm
 def __init__(self, opt=OPT_NORMAL):
     self.__opt = opt
     self.__fatmod = Module.new('mlvm.jit.%X' % id(self))
     self.__engine = EngineBuilder.new(self.__fatmod).opt(opt).create()
     self.__symlib = {} # stores (name, argtys) -> (wrapper, callable)