Why is LLVM segfaulting when I try to emit object code?

扶醉桌前 提交于 2019-12-03 16:58:43

All LLVM basic blocks must be terminated (see e.g. http://llvm.org/docs/doxygen/html/classllvm_1_1BasicBlock.html#details). In your case, the generated IR should look like this:

define void @func() {
entry:
    ret void
}

In your C++ code, you need to add builder.CreateRetVoid() before you call llvm::verifyFunction.

Also, llvm::verifyFunction is not visibly outputting an error because you haven't passed the second parameter which indicates the stream to which LLVM should output errors. Try this instead to output to stderr:

llvm::verifyFunction(*func, &llvm::errs())

You also should check the return value of llvm::verifyFunction. A true return value indicates an error.

See: http://llvm.org/docs/doxygen/html/namespacellvm.html#a26389c546573f058ad8ecbdc5c1933cf and http://llvm.org/docs/doxygen/html/raw__ostream_8h.html

You should also consider verifying the entire module before generating object files by calling llvm::verifyModule(theModule, theOsStream) (see http://llvm.org/docs/doxygen/html/Verifier_8h.html).

Finally, I'd recommend inspecting the IR generated by Clang when compiling C code so that you can inspect what correctly generated IR looks like. For example, you can create a simple C file as follows:

// test.c
void func(void) {}

And then compile and view as follows:

clang -S -emit-llvm test.c
cat test.ll
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!