如何调用未知类型的JITed LLVM函数? [英] How to call a JITed LLVM function with unknown type?

查看:96
本文介绍了如何调用未知类型的JITed LLVM函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LLVM为JIT编译器实现前端.我首先从LLVM教程中的万花筒示例开始.我知道如何使用LLVM C ++ API生成和JIT LLVM IR.我也知道如何使用llvm :: ExecutionEngine的"getPointerToFunction"方法来调用JITed函数.

I am implementing a front-end for a JIT compiler using LLVM. I started by following the Kaleidoscope example in the LLVM tutorial. I know how to generate and JIT LLVM IR using the LLVM C++ API. I also know how to call the JITed function, using the "getPointerToFunction" method of llvm::ExecutionEngine.

getPointerToFunction返回一个void *,然后必须将其强制转换为正确的函数类型.例如,在我的编译器中,我具有如下所示的单元测试:

getPointerToFunction returns a void* which I must then cast to the correct function type. For example, in my compiler I have unit test that looks like the following:

void* compiled_func = compiler.get_function("f");   
auto f = reinterpret_cast<int32_t(*)(int32_t)>(compiled_func);
int32_t result = f(10);

问题是我必须事先了解函数签名.在上面的示例中,我有一个函数"f",该函数需要一个32位整数并返回一个32位整数.由于我自己创建了"f",因​​此我知道函数的类型,因此可以调用JIT的函数.但是,总的来说,我不知道用户输入的函数签名是什么(或结构类型是什么).用户可以创建具有任意参数和返回类型的任意函数,因此我不知道哪种函数指针类型可以从LLVM的getPointerToFunction强制转换void *.我的运行时需要能够调用这些函数(例如,对于Read-Evaluate-Print循环).如何在JIT运行时中处理这些任意函数?

The problem is that I have to know the function signature beforehand. In the example above, I have a function "f" which takes takes a 32-bit integer and returns a 32-bit integer. Since I create "f" myself, I know what the function type is, so I'm able to call the JIT'ed function. However, in general, I do not know what the function signature is (or what the struct types are) that are entered by the user. The user can create arbitrary functions, with arbitrary arguments and return types, so I don't know what function pointer type to cast the void* from LLVM's getPointerToFunction. My runtime needs to be able to call those functions (for a Read-Evaluate-Print loop, for example). How can I handle such arbitrary functions from my JIT runtime?

谢谢

推荐答案

compiled_func获得的信息不多-如您所写,它只是一个void*.但是,当您编写一般来说,我不知道函数签名是什么"时,这并不准确-您刚刚编译了该函数,因此您应该可以访问LLVM Function对象,可以查询该对象的功能.类型.确实,这是LLVM IR类型,而不是C ++类型,但是您通常可以知道将哪个转换为哪个.

There's not much information you get can from compiled_func - as you wrote, it's just a void*. But when you write "in general, I do not know what the function signature is", that's not accurate - you've just compiled that function, so you should have access to the LLVM Function object, which can be queried about its type. It's true that it's an LLVM IR type and not a C++ type, but you can often know which translates to which.

例如,如果我们从教程的准时万花筒:

if (Function *LF = F->Codegen()) {
  LF->dump();  // Dump the function for exposition purposes.

  // JIT the function, returning a function pointer.
  void *FPtr = TheExecutionEngine->getPointerToFunction(LF);

  // Cast it to the right type (takes no arguments, returns a double) so we
  // can call it as a native function.
  double (*FP)() = (double (*)())(intptr_t)FPtr;
  fprintf(stderr, "Evaluated to %f\n", FP());
}

然后是的,FPtr被假定"为类型double (),但是这里也有类型Function*LF,所以您可以做类似的事情:

Then yes, FPtr was "assumed" to be of type double (), but there's also LF of type Function* here, so you could have done something like:

Type* RetTy = LF->getReturnType();
if (RetTy->isDoubleTy()) {
  double (*FP)() = (double (*)())(intptr_t)FPtr;
  fprintf(stderr, "Evaluated to %f\n", FP());
} else if (RetTy->isIntegerTy(32)) {
  int (*FP)() = (int (*)())(intptr_t)FPtr;
  fprintf(stderr, "Evaluated to %d\n", FP());
} else ...

以几乎相同的方式,您可以查询函数的参数类型.

And in much the same way, you can query a function about its parameter types.

有点麻烦?您可以使用执行引擎通过便捷的 runFunction 方法,该方法接收 GenericValue s的向量,并返回GenericValue.您仍然应该查询Function类型,以查找每个GenericValue下的基础类型.

A bit cumbersome? You can use your execution engine to invoke the function, via its handy runFunction method, which receives a vector of GenericValues and returns a GenericValue. You should still query the Function type to find what the underlying type under each GenericValue should be.

这篇关于如何调用未知类型的JITed LLVM函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆