我可以将现有方法绑定到LLVM函数*并从JIT编译的代码中使用它吗?

时间:2010-06-23 18:23:04

标签: c++ jit llvm

我正在使用LLVM C ++ API。我想JIT编译代码并运行它。

但是,我需要从所述JIT编译的代码中调用C ++方法。通常,LLVM将方法调用视为函数调用,并将对象指针作为第一个参数传递,因此调用不应该是一个问题。真正的问题是将该功能转换为LLVM。

据我所知,可以使用外部链接来实现功能,并通过其名称获取它。问题是,因为它是一个C ++方法,它的名字会被破坏,所以我不认为这样做是个好主意。

制作FunctionType对象非常简单。但是从那里,我如何通知LLVM我的方法并获得一个Function对象呢?

3 个答案:

答案 0 :(得分:15)

来自LLVM邮件列表的人是helpful enough to provide a better solution。他们没有说如何从方法到函数获取指针,但我已经想出了这部分,所以没关系。

编辑干净的方法就是将方法包装到函数中:

int Foo_Bar(Foo* foo)
{
    return foo->bar();
}

然后使用Foo_Bar的地址,而不是尝试获取Foo::bar。使用llvm::ExecutionEngine::addGlobalMapping添加映射,如下所示。

像往常一样,最简单的解决方案有一些有趣的好处。例如,它可以在没有打嗝的情况下使用虚拟功能。 (但它的娱乐性却低得多。其余的答案都是为了历史目的而保留的,主要是因为我在C ++运行时的内部发挥了很多乐趣。还要注意它是不可移植的。)


你需要按照这些方法来计算方法的地址(警告,这是一个可能只与Itanium ABI兼容的脏黑客):

template<typename T>
const void* void_cast(const T& object)
{
    union Retyper
    {
        const T object;
        void* pointer;
        Retyper(T obj) : object(obj) { }
    };

    return Retyper(object).pointer;
}

template<typename T, typename M>
const void* getMethodPointer(const T* object, M method) // will work for virtual methods
{
    union MethodEntry
    {
        intptr_t offset;
        void* function;
    };

    const MethodEntry* entry = static_cast<const MethodEntry*>(void_cast(&method));

    if (entry->offset % sizeof(intptr_t) == 0) // looks like that's how the runtime guesses virtual from static
        return getMethodPointer(method);

    const void* const* const vtable = *reinterpret_cast<const void* const* const* const>(object);
    return vtable[(entry->offset - 1) / sizeof(void*)];
}

template<typename M>
const void* getMethodPointer(M method) // will only work with non-virtual methods
{
    union MethodEntry
    {
        intptr_t offset;
        void* function;
    };

    return static_cast<const MethodEntry*>(void_cast(&method))->function;
}

然后使用llvm::ExecutionEngine::addGlobalMapping将函数映射到您获得的地址。要调用它,请将您的对象作为第一个参数传递给其他参数,其余的像往常一样。这是一个简单的例子。

class Foo
{
    void Bar();
    virtual void Baz();
};

class FooFoo : public Foo
{
    virtual void Baz();
};

Foo* foo = new FooFoo;

const void* barMethodPointer = getMethodPointer(&Foo::Bar);
const void* bazMethodPointer = getMethodPointer(foo, &Foo::Baz); // will get FooFoo::Baz

llvm::ExecutionEngine* engine = llvm::EngineBuilder(module).Create();

llvm::Function* bar = llvm::Function::Create(/* function type */, Function::ExternalLinkage, "foo", module);
llvm::Function* baz = llvm::Function::Create(/* function type */, Function::ExternalLinkage, "baz", module);
engine->addGlobalMapping(bar, const_cast<void*>(barMethodPointer)); // LLVM always takes non-const pointers
engine->addGlobalMapping(baz, const_cast<void*>(bazMethodPointer));

答案 1 :(得分:8)

一种方法是围绕所需方法的C包装器,即

extern "C" {
  void wrapped_foo(bar *b, int arg1, int arg2) {
    b->foo(arg1, arg2);
  }
}

extern "C"位使函数使用C调用约定并防止任何名称重整。有关C / C ++互操作的详细信息,请参阅http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.6,包括extern "C"

您也应该能够在C ++代码中获取函数的地址,然后将该地址存储在LLVM已知的全局中。

答案 2 :(得分:4)

嗯,使用非标准的dladdr和一种荒谬的错综复杂的方法将方法指针强制转换为void指针,似乎有办法从指针中获取方法的名称。

这肯定比枪械更危险。不要在家里(或在工作中)这样做。

C ++禁止将方法指针强制转换为void *(这是dladdr需要工作的),即使使用全能的C版,也可以作弊。

#include <string>
#include <dlfcn.h>

template<typename T>
static void* voidify(T method)
{
    asm ("movq %rdi, %rax"); // should work on x86_64 ABI compliant platforms
}

template<typename T>
const char* getMethodName(T method)
{
    Dl_info info;
    if (dladdr(voidify(method), &info))
        return info.dli_sname;
    return "";
}

从那里:

int main()
{
    std::cout << getMethodName(&Foo::bar) << std::endl;
    // prints something like "_ZN3Foo3barEv"
}

... aaaand你应该能够将该符号名称与LLVM一起使用。但它不适用于虚拟方法(不使用它的另一个好理由)。

编辑黑客攻击虚拟方法指针的处理方式,我已经整理了一个更精细的功能,也适用于它们。只有最勇敢的人才应follow this link