1
- 提取物起LLVM IR
- 他们每个人保存到不同的文件(或只是一个字符串对象)
- 从文件中读取保存的功能(或只是一个字符串对象)
- 重新使用它的框架
不过,我目前的提取方法仅复制部分信息,我不能事后再建功能。我主要有红外读者抱怨的问题:
error: use of undefined type named 'class.std::allocator'
...
error: use of undefined comdat '$_ZNSt10_Iter_baseIPiLb0EE7_S_baseES0_'
它可以通过添加适当的声明(COMDAT和类型),以提取IR的顶部来解决。
BEFORE:
; 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
}
AFTER:
#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;
}
...
我怎样才能让:
%"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这个过程自动?