如何从ifstream加载LLVM比特代码文件
How to load LLVM bitcode file from an ifstream?
我正在尝试加载运行时.bc
文件中定义的LLVM模块,但遇到了snag。
hello.cpp
生成了感兴趣的比特码:
// hello.cpp
// build with:
// clang-3.4 -c -emit-llvm hello.cpp -o hello.bc
#include <iostream>
void hello()
{
std::cout << "Hello, world!" << std::endl;
}
当下面的程序尝试在运行时加载它时,它会在llvm::BitstreamCursor::Read()
内崩溃:
// main.cpp
// build with:
// g++ main.cpp `llvm-config-3.4 --cppflags --ldflags --libs` -ldl -lpthread -lcurses
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>
#include <fstream>
#include <iostream>
llvm::Module *load_module(std::ifstream &stream)
{
if(!stream)
{
std::cerr << "error after open stream" << std::endl;
return 0;
}
// load bitcode
std::string ir((std::istreambuf_iterator<char>(stream)), (std::istreambuf_iterator<char>()));
// parse it
using namespace llvm;
LLVMContext context;
SMDiagnostic error;
Module *module = ParseIR(MemoryBuffer::getMemBuffer(StringRef(ir.c_str())), error, context);
if(!module)
{
std::string what;
llvm::raw_string_ostream os(what);
error.print("error after ParseIR()", os);
std::cerr << what;
} // end if
return module;
}
int main()
{
std::ifstream stream("hello.bc", std::ios_base::binary);
llvm::Module *m = load_module(stream);
if(m)
{
m->dump();
}
return 0;
}
我使用评论中提到的命令行反对LLVM v3.4。
知道我做错了什么?
有两个问题:
-
LLVMContext
的寿命需要超过Module
的寿命。否则Module
将参考不再存在的LLVMContext
。 -
StringRef
指的是IR,应从包含IR的std::string
构建,而不是零端的字符串。否则ParseIR
将无法正确找到IR的末端。
这是load_module
的更正版本:
llvm::Module *load_module(std::ifstream &stream, llvm::LLVMContext &context)
{
if(!stream)
{
std::cerr << "error after open stream" << std::endl;
return 0;
}
// load bitcode
std::string ir((std::istreambuf_iterator<char>(stream)), (std::istreambuf_iterator<char>()));
// parse it
using namespace llvm;
SMDiagnostic error;
Module *module = ParseIR(MemoryBuffer::getMemBuffer(StringRef(ir)), error, context);
if(!module)
{
std::string what;
llvm::raw_string_ostream os(what);
error.print("error after ParseIR()", os);
std::cerr << what;
} // end if
return module;
}
一种可能的解决方案是用
替换ParseIR()
调用 ParseIRFile("hello.bc", error, getGlobalContext())
这样做也使代码变得更加简单,因为您可以跳过手动加载比特码的部分。
以下内容足以获得模块的转储(您应该添加一些错误处理)
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
using namespace llvm;
int main()
{
LLVMContext context;
SMDiagnostic error;
Module *m = ParseIRFile("hello.bc", error, context);
if(m)
{
m->dump();
}
return 0;
}
相关文章:
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- 此代码编译良好,但文件未创建?请指出错误
- 找不到Linux Visual Studio代码C++文件
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- Visual Studio代码文件未输出
- 是否可以使用 GCC 编译具有特定编译器标志的代码文件的一部分?
- 如何从ifstream加载LLVM比特代码文件
- 在同一代码文件中包括 Directx 9 和 10
- 如果我只想要架构良好的工作区,但不一定是分开的编译,如何在C++中组织头/代码文件
- 使用 Eclipse 运行.exe C 代码文件
- 是否可以稍后在代码(C++ I/O 文件)中使用输出文件作为输入文件
- 匿名命名空间-在头文件和代码文件中都有意义
- 代码文件外的代码文档
- 在静态库中硬编码的代码文件和头路径
- 更快地解析代码文件
- 为什么有些代码文件使用 close() 而不包含 unistd.h
- 在代码(.c文件)我如何找到linux发行版名称版本
- 在一个代码文件中使用头文件,反之亦然
- SonarQube未分析带有警告的C++代码文件不在项目目录下
- Qt 创建器代码文件重构