VMLI链路

2024-04-28 06:46:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python编写编译器,使用llvmlite生成中间llvmir。Lexer和parser已经完成,现在我正在生成代码。编译器是动态的和弱类型的,所以我需要在运行时做一些事情,比如分配。为此,我已经用C实现了一些函数,现在我想用llvmlite中的builder.call调用这些函数。在

我没有找到文档或例子来说明如何做到这一点。在

这个函数只是一个简单的例子,实际的要大得多。在

C:

int some_function(int a)
{
     return a + 4;
}

Python:

^{pr2}$

我可以直接使用llvmlite构建器来编写函数,但是用C编写会非常快速、干净和容易。欢迎任何帮助!在


Tags: 函数代码文档parser类型编译器builder动态
3条回答

您可以导入包含运行库的动态库。在

llvmlite.binding.load_library_permanently("runtime.so")

然后您可以简单地生成正常的函数调用。在

@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)

以及测试库.cpp $referencehttps://helloacm.com/calling-c-shared-library-from-python-code-linux-version/

关于这个问题,另一个参考文献

-Call C/C++ functions from the ExecutionEngine -Call Python code from LLVM JIT -http://eli.thegreenplace.net/2015/calling-back-into-python-from-llvmlite-jited-code/

在llvmir端,您只需声明具有正确签名的函数(无正文),并像任何其他函数一样插入对它们的调用。这就像在C中调用在另一个文件中定义的函数一样。在

从那以后,你就不得不以某种方式链接到你的C函数上。这里的细节取决于您打算如何使用生成的IR代码。例如,您可以使用clang将其转换为目标文件,然后像任何其他程序一样链接它。或者可以使用llvmjit,在这种情况下,@Coder3000的答案(llvmlite.binding.load_library_permanently)应该可以让llvm解析函数调用。在

相关问题 更多 >