我在尝试运行使用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产生的堆栈转储中是否可能有相同的错误消息?
答案 0 :(得分:0)
这是缺少的代码,其中添加了所需的错误消息:
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
<< "\n\n";
}
}
现在,所需的只是在将模块提交到优化层之前调用Verify()
。