【问题标题】:Filling the LLVM CloneFunction VMAP填充 LLVM CloneFunction VMAP
【发布时间】:2016-02-16 19:45:33
【问题描述】:

我想写一些代码,给定一个 LLVM 函数 F,在同一个模块中创建一个精确的副本(以便以后可以在保留原始副本的同时对副本进行操作)。我想用 CloneFunctionInto 方法做到这一点。

我当前的尝试涉及尝试将每个(新 arg,旧 arg)对插入 VMap。以前我尝试过插入一个未初始化的 VMap 并将这对反过来。令人印象深刻的是,所有 3 个都导致了完全相同的错误消息:

断言 `VMap.count(&I) && "No mapping from source argument specified!"' 失败。

//F and S are defined higher up in the code
FunctionType *FType = F->getFunctionType();
Function *new_F = cast<Function>(M->getOrInsertFunction(S,FType));
std::vector<Type*> ArgTypes;
ValueToValueMapTy VMap;
Function::arg_iterator old_args = F->arg_begin();
for (Function::arg_iterator new_args = new_F->arg_begin(), new_args_end = new_F->arg_end();new_args != new_args_end; new_args++) {
  std::pair<Value*,Value*> pair(&*new_args,&*old_args);
  VMap.insert(pair);
  if (VMap.count(&*new_args)>0) {
   errs().write_escaped("Mapping added") << '\n';
  }
  old_args++;
 }
 SmallVector<ReturnInst*, 8> Returns;
 CloneFunctionInto(new_F, F, VMap, false, Returns, "_new", 0, 0);

在使用中,“添加映射”消息的打印次数正确(即每个参数打印一次),所以我真的不确定错误在哪里。

【问题讨论】:

    标签: llvm compiler-optimization llvm-c++-api


    【解决方案1】:

    当您只想克隆一个函数时,您可以使用CloneFunction 而不是CloneFunctionInto

    另外CloneFunction 向您展示了如何处理ValueToValueMap 进行克隆:

    来自CloneFunction.cpp

    00223 Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap,
    00224                               bool ModuleLevelChanges,
    00225                               ClonedCodeInfo *CodeInfo) {
    00226   std::vector<Type*> ArgTypes;
    00227 
    00228   // The user might be deleting arguments to the function by specifying them in
    00229   // the VMap.  If so, we need to not add the arguments to the arg ty vector
    00230   //
    00231   for (const Argument &I : F->args())
    00232     if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
    00233       ArgTypes.push_back(I.getType());
    00234 
    00235   // Create a new function type...
    00236   FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
    00237                                     ArgTypes, F->getFunctionType()->isVarArg());
    00238 
    00239   // Create the new function...
    00240   Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName());
    00241 
    00242   // Loop over the arguments, copying the names of the mapped arguments over...
    00243   Function::arg_iterator DestI = NewF->arg_begin();
    00244   for (const Argument & I : F->args())
    00245     if (VMap.count(&I) == 0) {     // Is this argument preserved?
    00246       DestI->setName(I.getName()); // Copy the name over...
    00247       VMap[&I] = &*DestI++;        // Add mapping to VMap
    00248     }
    00249 
    00250   if (ModuleLevelChanges)
    00251     CloneDebugInfoMetadata(NewF, F, VMap);
    00252 
    00253   SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
    00254   CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo);
    00255   return NewF;
    00256 }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 2015-02-27
      • 2021-06-07
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2012-01-22
      • 2015-07-13
      相关资源
      最近更新 更多