@ Coder3000と@ Ismail Badawiの答えは完璧ですが、コードはありません。私のコードがあります
#!/usr/bin/env python
# coding=utf-8
from __future__ import print_function
from ctypes import CFUNCTYPE, c_double,cdll,c_int
import llvmlite.binding as llvm
import llvmlite.ir as ir
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
#load shared library
llvm.load_library_permanently('./TestLib.so')
# Create some useful types
double = ir.DoubleType()
fnty = ir.FunctionType(double, (double, double))
# Create an empty module...
module = ir.Module("fdadd")
# and declare a function named "fpadd" inside it
func = ir.Function(module, fnty, name="fpadd")
# Now implement the function
block = func.append_basic_block(name="entry")
builder = ir.IRBuilder(block)
a, b = func.args
result = builder.fadd(a, b, name="res")
builder.ret(result)
#function call in llvm ir internal
func2=ir.Function(module,fnty,name="fdadd")
block2=func2.append_basic_block(name="entry")
builder=ir.IRBuilder(block2)
a,b=func2.args
result2=builder.call(func,(a,b))
builder.ret(result2)
# llvm IR call external C++ function
outfunc=ir.Function(module,fnty,name="SampleAddDouble")
#just declare shared library function in module
outaddfunc=ir.Function(module,fnty,name="outadd")
builder=ir.IRBuilder(outaddfunc.append_basic_block(name="entry"))
a,b=outaddfunc.args
outresult=builder.call(outfunc,(a,b))
builder.ret(outresult)
strmod=str(module)
# Print the module IR
print(strmod)
print("-----------------------------------------")
#assembly llvm ir
assmod = llvm.parse_assembly(strmod)
assmod.verify()
print("--parse assembly")
target = llvm.Target.from_default_triple()
target_machine = target.create_target_machine()
engine = llvm.create_mcjit_compiler(assmod, target_machine)
engine.finalize_object()
print(engine)
# Look up the function pointer (a Python int)
#llvm execution engine call llvm IR function(or dsl function)
func_ptr = engine.get_function_address("fpadd")
print('func_ptr is:',func_ptr)
# Run the function via ctypes
cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
res = cfunc(1.0, 3.5)
print("fpadd(...) =", res)
#llvm binding layer call shared library function
add_int_addr = llvm.address_of_symbol("SampleAddInt")
print(add_int_addr)
add_int_func=CFUNCTYPE(c_int,c_int,c_int)(add_int_addr)
res2=add_int_func(23,34)
print(res2)
#llvm execution engine call shared library function ,by llvm IR
add_double_addr=engine.get_function_address("outadd")
print(add_double_addr)
add_double_func=CFUNCTYPE(c_double,c_double,c_double)(add_double_addr)
res3=add_double_func(1.21,1.12)
print(res3)
そして、Testlib.cpp $reference https://helloacm.com/calling-c-shared-library-from-python-code-linux-version/
この問題については、他の参照があります
- ExecutionEngine から C/C++ 関数を呼び出す
- LLVM JIT から Python コードを呼び出す
- http://eli.thegreenplace.net/2015/calling-back-into-python-from-llvmlite-jited-code/