Module crash when adding CallInst

Hi,

I am learning to write LLVM modules. I am using IRBuilder to insert calls to an existing function. The call is to be inserted at a random location in every function in the code. The call does get inserted, however, the module crashes later (after doFinalization()). The crash happens only sometimes, not always.

These are the kind of instructions that I see in the generated code.
%callins = call void @myfunc(), !dbg !285
%callins = call void @myfunc()

The stack trace shows the crash has something to do with debug information. Do I need to add metadata or debug info to the newly added call instruction? Any pointers on how I can do that?

Here is the code snippet and the stack trace:

IRBuilder<> Builder(ins); // to be inserted before ins
CallInst *call = Builder.CreateCall(func, “callins”); // call to func

0 clang 0x0000000001492ec2 llvm::sys::PrintStackTrace(_IO_FILE*) + 34
1 clang 0x0000000001490f61
2 libpthread.so.0 0x00002b33bd0ee340
3 clang 0x00000000016a43a6 llvm::DwarfUnit::addString(llvm::DIE&, llvm::dwarf::Attribute, llvm::StringRef) + 22
4 clang 0x00000000016c2003 llvm::DwarfCompileUnit::applyVariableAttributes(llvm::DbgVariable const&, llvm::DIE&) + 915
5 clang 0x000000000168bdfe llvm::DwarfDebug::finishVariableDefinitions() + 270
6 clang 0x0000000001690424 llvm::DwarfDebug::finalizeModuleInfo() + 52
7 clang 0x000000000169c4a8 llvm::DwarfDebug::endModule() + 40
8 clang 0x00000000016850f7 llvm::AsmPrinter::doFinalization(llvm::Module&) + 551
9 clang 0x00000000011befe5 llvm::FPPassManager::doFinalization(llvm::Module&) + 69
10 clang 0x00000000011ca40d llvm::legacy::PassManagerImpl::run(llvm::Module&) + 813
11 clang 0x0000000001837fad clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::raw_ostream*) + 1629
12 clang 0x000000000182a47f
13 clang 0x0000000001bdf043 clang::ParseAST(clang::Sema&, bool, bool) + 483
14 clang 0x0000000001603ba6 clang::FrontendAction::Execute() + 118
15 clang 0x00000000015e47b8 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 280
16 clang 0x0000000001673b41 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 1921
17 clang 0x0000000000820358 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 1320
18 clang 0x00000000007fceaa main + 8298
19 libc.so.6 0x00002b33bdd56ec5 __libc_start_main + 245
20 clang 0x000000000081e5d9

Any help is appreciated.

Thanks,
Pankaj

Have you tried an asserts build of clang/llvm? If you’re doing LLVM development you should probably be using an asserts build.

I would guess you’re hitting the issue where, if you have debug info for a function, any call instructions inside that function must themselves have a debug location (that !dbg thing). Otherwise bad debug info (and assertions to ensure that badness doesn’t happen) happens.

  • David

Thanks David. I will try using an asserts build.
I injected the call instruction before an instruction which has metadata. This fixes the issue.

  • Pankaj