【问题标题】:How to extract complete function information on LLVM?如何提取 LLVM 上的完整函数信息?
【发布时间】:2017-08-07 22:38:48
【问题描述】:

我正在尝试

  1. 提取函数的 LLVM IR
  2. 将它们分别保存到不同的文件(或只是一个字符串对象)
  3. 从文件中读取保存的函数(或者只是一个字符串对象)
  4. 在框架上重复使用它

但是,我目前的提取方法只复制部分信息,之后我无法重新构建函数。我主要有 IR Reader 的问题抱怨:

error: use of undefined type named 'class.std::allocator'
...
error: use of undefined comdat '$_ZNSt10_Iter_baseIPiLb0EE7_S_baseES0_'

可以通过在提取的 IR 顶部添加适当的声明(comdat 和类型)来解决此问题。

之前:

; Function Attrs: noinline nounwind uwtable
define linkonce_odr i64 @_ZNK9__gnu_cxx13new_allocatorIiE8max_sizeEv(%"class.__gnu_cxx::new_allocator"*) #4 comdat align 2 {
  ret i64 4611686018427387903
}

之后:

%"class.__gnu_cxx::new_allocator" = type { i8 }
$_ZNK9__gnu_cxx13new_allocatorIiE8max_sizeEv = comdat any

; Function Attrs: noinline nounwind uwtable
define linkonce_odr i64 @_ZNK9__gnu_cxx13new_allocatorIiE8max_sizeEv(%"class.__gnu_cxx::new_allocator"*) #4 comdat align 2 {
  ret i64 4611686018427387903
}

我目前正在使用以下方法执行步骤 1、2、3、4:

#include "llvm/IR/Module.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/SourceMgr.h" // SMDDiagnostic
#include "llvm/Support/MemoryBuffer.h"

using namespace llvm;

...

// A method receives a LLVM::Function& F as its argument
// Steps 1 and 2
std::string IRString;
raw_string_ostream os(IRString);
F.print(os, nullptr);
os.flush();

// IRString has the LLVM IR for the function (currently with the BEFORE version)
// Now it is necessary to read back this IR
// Steps 3 and 4
SMDiagnostic Err;
LLVMContext Context;
std::unique_ptr<llvm::MemoryBuffer> buff =
llvm::MemoryBuffer::getMemBuffer(SU.getIRInfo());

std::unique_ptr<Module> Mod(parseIR(buff->getMemBufferRef(), Err, Context));
if (!Mod) {
    Err.print("Reading Problems", errs());
    return 1;
}

...

我怎样才能使这个过程自动化?

【问题讨论】:

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


    【解决方案1】:

    看看 llvm-extract 或 GVExtraction 类(由 llvm-extract 使用)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2020-09-01
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      相关资源
      最近更新 更多