C++ LLVM克隆函数传递到不同的模块

C++ LLVM克隆函数传递到不同的模块,c++,llvm,llvm-ir,C++,Llvm,Llvm Ir,我正在创建LLVM过程,加载到opt-as库中,它从硬编码模块获取函数,并将其函数克隆到输入模块 bool MyPass::runOnModule(llvm::Module &M) { SMDiagnostic error; LLVMContext context; StringRef ImplAssembly = R"( define void @foo() { ret void } )"; auto InjectedModule = p

我正在创建LLVM过程,加载到opt-as库中,它从硬编码模块获取函数,并将其函数克隆到输入模块

bool MyPass::runOnModule(llvm::Module &M)
{
  SMDiagnostic error;
  LLVMContext context;
  StringRef ImplAssembly = R"(
    define void @foo() {
      ret void
    }
  )";
  auto InjectedModule = parseAssemblyString(ImplAssembly, error, context);

  auto* ImplFunction = InjectedModule->getFunction("foo");
  auto DeclFunction = Function::Create(ImplFunction->getFunctionType(), ImplFunction->getLinkage(), "foo", M);

  VMap[ImplFunction] = DeclFunction;

  SmallVector<ReturnInst*, 8> Returns;
  CloneFunctionInto(DeclFunction, ImplFunction, VMap, false, Returns);
  return true;
}

我想知道克隆函数的方法是否正确。如果是,那么找出崩溃背后的原因。

经过一些实验,发现在通过现有的M上下文而不是空变量上下文后,pass成功地完成了它的工作,也就是说,我应该通过现有上下文而不是创建新上下文

bool MyPass::runOnModule(llvm::Module &M)
{
  SMDiagnostic error;
  StringRef ImplAssembly = R"(
    define void @foo() {
      ret void
    }
  )";
  auto InjectedModule = parseAssemblyString(ImplAssembly, error, M.getContext());

  auto* ImplFunction = InjectedModule->getFunction("foo");
  auto DeclFunction = Function::Create(ImplFunction->getFunctionType(), ImplFunction->getLinkage(), "foo", M);

  VMap[ImplFunction] = DeclFunction;

  SmallVector<ReturnInst*, 8> Returns;
  CloneFunctionInto(DeclFunction, ImplFunction, VMap, false, Returns);
  return true;
}
bool MyPass::runOnModule(llvm::Module&M)
{
诊断错误;
StringRef ImplAssembly=R“(
定义void@foo(){
ret void
}
)";
auto InjectedModule=parseAssemblyString(ImplAssembly,error,M.getContext());
auto*ImplFunction=InjectedModule->getFunction(“foo”);
auto DeclFunction=Function::Create(implffunction->getFunctionType(),implffunction->getLinkage(),“foo”,M);
VMap[ImplFunction]=DeclFunction;
小向量回归;
CloneFunctionInto(DeclFunction、ImplFunction、VMap、false、Returns);
返回true;
}

您是否面临某些类型和拆卸问题?
bool MyPass::runOnModule(llvm::Module &M)
{
  SMDiagnostic error;
  StringRef ImplAssembly = R"(
    define void @foo() {
      ret void
    }
  )";
  auto InjectedModule = parseAssemblyString(ImplAssembly, error, M.getContext());

  auto* ImplFunction = InjectedModule->getFunction("foo");
  auto DeclFunction = Function::Create(ImplFunction->getFunctionType(), ImplFunction->getLinkage(), "foo", M);

  VMap[ImplFunction] = DeclFunction;

  SmallVector<ReturnInst*, 8> Returns;
  CloneFunctionInto(DeclFunction, ImplFunction, VMap, false, Returns);
  return true;
}