我正在玩LLVM 3.7
,并希望使用新的ORC内容。但是我现在已经在这几个小时了,仍然没有得到每一层的用途,何时使用它们,如何构成它们或者至少是最小的一组我需要到位。
通过了Kaleidoscope
教程,但是这些没有解释构成部分是什么,只是说这里和此处(加上解析等分散核心LLVM位)。虽然开始它很棒,但它留下了很多空白。在LLVM中有很多关于各种各样的东西的文档,但是它实际上接近于压倒一切。像http://llvm.org/releases/3.7.0/docs/ProgrammersManual.html这样的东西,但我无法找到解释所有部分如何组合在一起的东西。考虑到MCJIT
和更新的ORC
API,似乎有更多混淆似乎有多个API用于执行相同的操作。我看到Lang Hames post解释,自从他在该链接中发布的补丁以来,似乎已经发生了一些变化。
因此,对于一个特定问题,所有这些层如何组合在一起?
当我以前使用LLVM时,我可以相当容易地链接到C函数,使用" How to use JIT"示例作为基础,我尝试链接到外部函数extern "C" double doIt
,但最终得到LLVM ERROR: Tried to execute an unknown external function: doIt
。
看一下this ORC示例,我似乎需要配置搜索符号的位置。但TBH虽然我仍然在摇摆,但它在很大程度上是猜测工作。这就是我得到的:
#include "llvm/ADT/STLExtras.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "std.hpp"
using namespace llvm;
int main() {
InitializeNativeTarget();
LLVMContext Context;
// Create some module to put our function into it.
std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
Module *M = Owner.get();
// Create the add1 function entry and insert this entry into module M. The
// function will have a return type of "int" and take an argument of "int".
// The '0' terminates the list of argument types.
Function *Add1F = cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context), Type::getInt32Ty(Context), (Type *) 0));
// Add a basic block to the function. As before, it automatically inserts
// because of the last argument.
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
// Create a basic block builder with default parameters. The builder will
// automatically append instructions to the basic block `BB'.
IRBuilder<> builder(BB);
// Get pointers to the constant `1'.
Value *One = builder.getInt32(1);
// Get pointers to the integer argument of the add1 function...
assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
Argument *ArgX = Add1F->arg_begin(); // Get the arg
ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
// Create the add instruction, inserting it into the end of BB.
Value *Add = builder.CreateAdd(One, ArgX);
// Create the return instruction and add it to the basic block
builder.CreateRet(Add);
// Now, function add1 is ready.
// Now we're going to create function `foo', which returns an int and takes no
// arguments.
Function *FooF = cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context), (Type *) 0));
// Add a basic block to the FooF function.
BB = BasicBlock::Create(Context, "EntryBlock", FooF);
// Tell the basic block builder to attach itself to the new basic block
builder.SetInsertPoint(BB);
// Get pointer to the constant `10'.
Value *Ten = builder.getInt32(10);
// Pass Ten to the call to Add1F
CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
Add1CallRes->setTailCall(true);
// Create the return instruction and add it to the basic block.
builder.CreateRet(Add1CallRes);
std::vector<Type *> args;
args.push_back(Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), args, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, "doIt", Owner.get());
// Now we create the JIT.
ExecutionEngine *EE = EngineBuilder(std::move(Owner)).create();
outs() << "We just constructed this LLVM module:\n\n" << *M;
outs() << "\n\nRunning foo: ";
outs().flush();
// Call the `foo' function with no arguments:
std::vector<GenericValue> noargs;
GenericValue gv = EE->runFunction(FooF, noargs);
auto ax = EE->runFunction(F, noargs);
// Import result of execution:
outs() << "Result: " << gv.IntVal << "\n";
outs() << "Result 2: " << ax.IntVal << "\n";
delete EE;
llvm_shutdown();
return 0;
}
doIt
在std.hpp
中声明。
答案 0 :(得分:6)
你的问题很模糊,但也许我可以帮助一下。 This code sample是一个使用Orc构建的简单JIT - 它评论很好,因此应该很容易理解。
简单地说,Orc构建在MCJIT使用的相同构建块之上(MC用于将LLVM模块编译为目标文件,RuntimeDyld
用于运行时的动态链接),但是它的概念提供了更多的灵活性。层。因此它可以支持像MCJIT不支持的“懒惰”JIT编译之类的东西。这对LLVM社区很重要,因为很久以前删除的“旧JIT”支持这些东西。 Orc JIT让我们可以获得这些先进的JIT功能,同时仍然建立在MC之上,因此不会重复代码发射逻辑。
为了得到更好的答案,我建议你提出更具体的问题。