Search code examples
c++11llvmllvm-clangllvm-gcc

Issue with LLVM FunctionPass iterator?


I have a very basic pass that need to print how many BasicBlocks are there in a Function.

#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/Support/raw_ostream.h"
#include "iostream"
using namespace llvm;
namespace {
struct Mypass : public FunctionPass {
public:
static char ID; // Pass identification
Mypass() : FunctionPass(ID){}

bool runOnFunction(Function &F) override {
    errs()<<"Function Name:";
    errs()<<F.getName()<<"\n";
    Function::iterator bb_i = F.begin();
    int bb_count = 0;
    for (Function::iterator I = F.begin(); I != F.end(); I++){
        std::cout<< "Basic Block: BB"<<bb_count++<<std::endl;
    }

    return false;
}
};
}
char Mypass::ID = 0;
static RegisterPass<Mypass> X("Epass","Pass",true,false);

I run the pass using opt. And get this error:

> Function Name:testfnc1
>     Basic Block: BB0 0  libLLVM-3.4.so  0x000000388b1935d2 llvm::sys::PrintStackTrace(_IO_FILE*) + 34 1  libLLVM-3.4.so 
> 0x000000388b193dfa 2  libpthread.so.0 0x0000003feaa0f790 3 
> EstimatePass.so 0x00007f04eb789737 4  libLLVM-3.4.so 
> 0x000000388aad12b4 llvm::FPPassManager::runOnFunction(llvm::Function&)
> + 468 5  libLLVM-3.4.so  0x000000388aad139b llvm::FPPassManager::runOnModule(llvm::Module&) + 43 6  libLLVM-3.4.so
> 0x000000388aad0f1e llvm::legacy::PassManagerImpl::run(llvm::Module&) +
> 686 7  opt             0x000000000041f8d9 main + 5977 8  libc.so.6    
> 0x0000003fea21ed5d __libc_start_main + 253 9  opt            
> 0x000000000040ebf9 Stack dump:
> 0.    Program arguments: opt -load /net/home/shaheen/llvm/mypass/EstimatePass.so -Epass -time-passes
> test.bc 
> 1.    Running pass 'Function Pass Manager' on module 'test.bc'.
> 2.    Running pass 'Pass' on function '@testfnc1' Segmentation fault

I narrowed down to the issue and it happens when I increment the iterator. But I dont know why.

Here is the test file I am running my pass on:

#include "stdio.h"
void testfnc1(){
    int i=1;int j=0;
    for(i=1;i<10;i++)
    for(j=1;j<10;j++)
    printf("test");
};
int main()
{
    int i;  
    printf("hello\n");
    for(i=0;i<3;i++){
    printf("hello:");

    for(i=1;i<4;i++)
    printf("asd");}
    testfnc1();
}

Solution

  • The problem was that I was using llvm 3.6 while compiling the pass and the bitcode file I was generating was from clang version 3.4.

    I just changed the Clang version to 3.6 and its now clompletely working fine.

    Thank you