我在尝试运行使用 LLVM API 生成代码的程序时得到一个"堆栈转储":
Stack dump:
0. Program arguments: ./llvm-playground
1. Running pass 'Simplify the CFG' on function '@foo'
0 libLLVM.dylib 0x000000010397c1e5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1 libLLVM.dylib 0x000000010397c5e8 SignalHandler(int) + 180
2 libsystem_platform.dylib 0x00007fff7d689b5d _sigtramp + 29
3 libsystem_platform.dylib 000000000000000000 _sigtramp + 2190959808
...
25 llvm-playground 0x000000010380591d main + 893
26 libdyld.dylib 0x00007fff7d49e3d5 start + 1
27 libdyld.dylib 0x0000000000000001 start + 2192972845
[1] 14917 segmentation fault ./llvm-playground
生成的 IR 如下所示:
; ModuleID = 'MyModule'
source_filename = "MyModule"
define i32 @foo(i32 %arg1) #0 {
entry:
%malloccall = tail call i8* @malloc(i64 8)
%0 = bitcast i8* %malloccall to i32*
%1 = getelementptr inbounds i32, i32* %0, i32 1
store i32 5, i32* %1
}
declare noalias i8* @malloc(i32*)
attributes #0 = { "target-cpu"="skylake" }
当使用llc
编译相同的 IR 代码时,可以立即发现问题(缺少 return 语句(:
error: expected instruction opcode
}
是否可以在 LLVM 生成的堆栈转储中出现相同的错误消息?
这是添加所需错误消息的缺失代码段:
std::string ModuleBuilder::GetIR() const {
std::string module_str;
llvm::raw_string_ostream ostream{module_str};
module_->print(ostream, nullptr, false);
return module_str;
}
void ModuleBuilder::Verify() const {
std::string error_str;
llvm::raw_string_ostream ostream{error_str};
if (llvm::verifyModule(*module_, &ostream)) {
std::cout << "ERROR found in IR: " << GetIR() << "nERROR: " << error_str
<< "nn";
}
}
现在,只需要在将模块提交到优化层之前调用Verify()
。