llvm JIT向模块添加库 [英] llvm JIT add library to module
问题描述
我正在研究使用LLVM的JIT.该语言有一个用C ++编写的小运行时,我使用clang编译为LLVM IR
I am working on a JIT that uses LLVM. The language has a small run-time written in C++ which I compile down to LLVM IR using clang
clang++ runtime.cu --cuda-gpu-arch=sm_50 -c -emit-llvm
,然后加载* .bc文件,生成其他IR,然后即时执行.之所以使用CUDA,是因为我想在运行时添加一些GPU加速.但是,这引入了特定于CUDA的外部函数,该函数会产生诸如以下错误:
and then load the *.bc files, generate additional IR, and execute on the fly. The reason for the CUDA stuff is that I want to add some GPU acceleration to the runtime. However, this introduces CUDA specific external functions which gives errors such as:
LLVM ERROR: Program used external function 'cudaSetupArgument' which could not be resolved!
如此处所述, ,通常可以通过在编译程序时包含适当的库来解决:
As discussed here, this is usually solved by including the appropriate libraries when compiling the program:
g++ main.c cmal.o -L/usr/local/cuda/lib64 -lcudart
但是,我不确定如何使用LLVM在JITed模块中包括库.我发现了这个问题,该问题过去曾经是可能的像这样将库添加到JIT中的模块:
However, I am not sure how to include libraries in JITed modules using LLVM. I found this question which suggested that is used to be possible to add libraries to modules in the JIT like this:
[your module]->addLibrary("m");
不幸的是,此方法已被弃用.谁能告诉我最好的方法吗?让我知道是否需要提供更多信息!
Unfortunately, this has been deprecated. Can anyone tell me the best way to do this now? Let me know if I need to provide more information!
此外,我不确定这是否是将GPU卸载整合到我的JIT中的最佳方法,因此,如果有人可以向我指出一种更好的方法,请执行:)
Furthermore, I am not really sure if this is the best way to be incorporating GPU offloading into my JIT, so if anyone can point me to a better method then please do :)
谢谢!
我正在使用LLVM 5.0,而我正在使用的JIT引擎来自llvm/ExecutionEngine/ExecutionEngine.h
,更具体地说,我是这样创建的:
I am using LLVM 5.0 and the JIT engine I am using is from llvm/ExecutionEngine/ExecutionEngine.h
, more specifically I create it like this:
EngineBuilder EB(std::move(module));
ExecutionEngine *EE = EB.create(targetMachine);
推荐答案
您需要明确地向JIT引擎学习其他符号.
You need to teach your JIT engine about other symbols explicitly.
如果它们在动态库(dylib
,so
,dll
)中,则可以调用
If they are in a dynamic library (dylib
, so
, dll
) then you can just call
sys::DynamicLibrary::LoadLibraryPermanently("path_to_some.dylib")
带有动态库的路径.
如果符号在目标文件或归档中,则需要做更多的工作:您需要将它们加载到内存中,并使用其API添加到ExecutionEngine
中.
If the symbols are in an object file or an archive, then it requires a bit more work: you would need to load them into memory and add to the ExecutionEngine
using its APIs.
以下是目标文件的示例:
Here is an example for an object file:
std::string objectFileName("some_object_file.o");
ErrorOr<std::unique_ptr<MemoryBuffer>> buffer =
MemoryBuffer::getFile(objectFileName.c_str());
if (!buffer) {
// handle error
}
Expected<std::unique_ptr<ObjectFile>> objectOrError =
ObjectFile::createObjectFile(buffer.get()->getMemBufferRef());
if (!objectOrError) {
// handle error
}
std::unique_ptr<ObjectFile> objectFile(std::move(objectOrError.get()));
auto owningObject = OwningBinary<ObjectFile>(std::move(objectFile),
std::move(buffer.get()));
executionEngine.addObjectFile(std::move(owningObject));
对于档案,将模板类型ObjectFile
替换为Archive
,然后调用
For archives replace template types ObjectFile
with Archive
, and call
executionEngine.addArchive(std::move(owningArchive));
最后.
这篇关于llvm JIT向模块添加库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!