Код: Выделить всё
from __future__ import print_function
from ctypes import CFUNCTYPE, c_double, c_float
import llvmlite.binding as llvm
# All these initializations are required for code generation!
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
from numba import njit
@njit('float32(float32,float32)')
def foo(x,y):
return x+y
llvm_str = foo.inspect_llvm(foo.signatures[0])
print(llvm_str)
print("------------------")
llvm_ir = llvm_str
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.
"""
# 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
def compile_ir(engine, llvm_ir):
"""
Compile the LLVM IR string with the given engine.
The compiled module object is returned.
"""
# Create a LLVM module object from the IR
mod = llvm.parse_assembly(llvm_ir)
mod.verify()
# Now add the module and make sure it is ready for execution
engine.add_module(mod)
engine.finalize_object()
engine.run_static_constructors()
return mod
engine = create_execution_engine()
mod = compile_ir(engine, llvm_ir)
# Look up the function pointer (a Python int)
# func_ptr = engine.get_function_address("fpadd")
func_ptr = engine.get_function_address("foo")
# Run the function via ctypes
# cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
cfunc = CFUNCTYPE(c_float, c_float, c_float)(func_ptr)
res = cfunc(1.0, 3.5)
# print("fpadd(...) =", res)
print("foo(...) =", res)
Traceback (последний последний вызов):
File "test_llvmwithNumbaGetLLVMIR5.py", строка 63, в
res = cfunc(1.0, 3.5)
OSError: исключение: запись нарушения прав доступа 0x0000000000000000
Я думаю, ошибка потому что экземпляр IR foo() не в порядке. Но я действительно понятия не имею, как с этим справиться.
Подробнее здесь: https://stackoverflow.com/questions/787 ... -numba-jit