从 LLVM 添加到 C++ lambda 的映射

add mapping to C++ lambda from LLVM

我有一组包装 C++ 方法调用的 lambda,我想用 LLVM 调用它们。我的尝试似乎遗漏了一些东西,即使在声明了类型并将全局映射添加到 lambda 之后,我仍然收到 LLVM 错误。重现我尝试的最小代码是:

#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/TargetSelect.h"

using namespace llvm;

int main() {

  InitializeNativeTarget();

  LLVMContext Context;

  std::unique_ptr<Module> Owner = make_unique<Module>("SomeModule", Context);
  Module *M = Owner.get();

  FunctionType *lambdaFT = FunctionType::get(Type::getInt32Ty(Context), false);
  Function *lambdaFN = Function::Create(lambdaFT, Function::ExternalLinkage, "lambda", Owner.get());
  //this is what the original question had
  //auto lambdaBody = []() { return 100; };
  //this is an edit after Johannes Schaub's answer
  int32_t ( *lambdaBody)() = +[]() { return 100; };

  Function *mainF = cast<Function>(M->getOrInsertFunction("main", Type::getInt32Ty(Context), (Type *) 0));

  BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", mainF);
  IRBuilder<> builder(BB);

  CallInst *lambdaRes = builder.CreateCall(lambdaFN, std::vector<Value *>(), "lambdaRetVar");
  builder.CreateRet(lambdaRes);

  ExecutionEngine *EE = EngineBuilder(std::move(Owner)).create();
  EE->addGlobalMapping(lambdaFN, &lambdaBody);

  outs() << "We just constructed this LLVM module:\n\n" << *M;
  outs() << "\n\nRunning main: ";

  std::vector<GenericValue> noargs;
  GenericValue gv = EE->runFunction(mainF, noargs);

  outs() << "Result: " << gv.IntVal << "\n";
  llvm_shutdown();
  delete EE;
  return 0;
}

这导致输出:

We just constructed this LLVM module:

; ModuleID = 'SomeModule'

declare i32 @lambda()

define i32 @main() {
EntryBlock:
  %lambdaRetVar = call i32 @lambda()
  ret i32 %lambdaRetVar
}

Running main: 
LLVM ERROR: Tried to execute an unknown external function: lambda

我做错了什么? 使用 LLVM 3.7.0(标签不存在)

你的 lambda 体是 class。您必须传递其函数调用运算符的地址,您可以将其转换为函数指针:auto lambdaBody = +[]() { return 100; }; 并将其作为 void*EE->addGlobalMapping(lambdaFN, reinterpret_cast<void*>(lambdaBody)); 传递。

Johannes Schaub 关于 lambda 的回答很有帮助,但事实证明 addGlobalMapping 并没有按照我的想法行事。还缺少一些东西。 最重要的是:

EE->InstallLazyFunctionCreator([&](const std::string &fnName) -> void * {
    if (fnName == "lambda") { return reinterpret_cast<void *>(lambdaBody); }
    return nullptr;
  });

InstallLazyFunctionCreator用于函数无法解析的时候。它以函数名称调用,作为响应,如果未找到此函数 returns nullptr 或要执行的函数。

这是完整的代码清单,其中添加了一些内容(并非全部都是必需的):

#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
//must include to avoid "JIT has not been linked in" when creating the ExecutionEngine - https://llvm.org/bugs/show_bug.cgi?id=22910
#include "llvm/ExecutionEngine/MCJIT.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/TargetSelect.h"
#include <iostream>

using namespace llvm;

int proxy() { return 1000; }

int main() {

  InitializeNativeTarget();
  llvm::InitializeNativeTargetAsmPrinter();
  llvm::InitializeNativeTargetAsmParser();

  LLVMContext Context;

  std::unique_ptr<Module> Owner = make_unique<Module>("SomeModule", Context);
  Module *M = Owner.get();

  FunctionType *lambdaFT = FunctionType::get(Type::getInt32Ty(Context), false);
  Function *lambdaFN = Function::Create(lambdaFT, Function::ExternalLinkage, "lambda", Owner.get());
  int32_t ( *lambdaBody)() = +[]() { return 9099899; };

  assert(9099899 == lambdaBody()); //make sure we can call it
  Function *mainF = cast<Function>(M->getOrInsertFunction("main", Type::getInt32Ty(Context), (Type *) 0));

  BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", mainF);
  IRBuilder<> builder(BB);

  CallInst *lambdaRes = builder.CreateCall(lambdaFN, std::vector<Value *>(), "lambdaRetVar");
  builder.CreateRet(lambdaRes);
  EngineBuilder eb(std::move(Owner));
  std::unique_ptr<RTDyldMemoryManager> MM;
  std::string Error;
  ExecutionEngine *EE = eb.setEngineKind(EngineKind::JIT)
                          .setMCJITMemoryManager(std::move(MM))
                          .setErrorStr(&Error)
                          .setOptLevel(CodeGenOpt::None)
                          .setCodeModel(CodeModel::JITDefault)
                          .setRelocationModel(Reloc::Default)
                              //.setMArch(MArch)
                          .setMCPU(sys::getHostCPUName())
                              //.setMAttrs(MAttrs)
                          .create();
  std::cout << Error << std::endl;
  EE->InstallLazyFunctionCreator([&](const std::string &fnName) -> void * {
    if (fnName == "lambda") { return reinterpret_cast<void *>(lambdaBody); }
    return nullptr;
  });
  EE->finalizeObject();
  outs() << "We just constructed this LLVM module:\n\n" << *M;
  outs() << "\n\nRunning main: ";

  std::vector<GenericValue> noargs;
  GenericValue gv = EE->runFunction(mainF, noargs);

  outs() << "Result: " << gv.IntVal << "\n";
  assert(gv.IntVal == 9099899);
  llvm_shutdown();
  delete EE;
  return 0;
}