Getting Segmentation Fault when using back( ) or getEntryBlock()

While writing a pass, I am getting segmentation fault when I try to use the value returned by back( ) or getEntryBlock( ) functions. I have attached the code for reference:

PreservedAnalyses PassPrac::run(Module &M, ModuleAnalysisManager &MAM){

    
    for(auto &F : M){
     
        auto &BB = F.back();
        IRBuilder <> builder ( (&BB) );          // This line results in SegFault
       // errs() << F.getEntryBlock() << "\n";   // This line results in SegFault too

    }
    return PreservedAnalyses::all();
}

Kindly help. Thanks in advance :slight_smile:

You’re probably hitting a declaration without a body (e.g. declare void @foo()). They still count as a Function in the module.

1 Like

Yup…I removed that declaration and everything worked out fine. Can you please further elaborate why a function declaration was causing segfault?
Thanku.

A declaration is just a Function that doesn’t have any basic blocks. So F.back() is returning an invalid reference to goodness knows what part of memory.

I expect a build of LLVM with assertions enabled would have crashed right there, but certainly all bets are off after you start pretending something is a BasicBlock when it’s really not.

1 Like

Thanks again.!