Any suggestion for "Unknown instruction type encountered" error?

Hello all,

I was playing with LLVM pass. I changed the lib/Transforms/Hello/Hello.cpp 's content to be my own pass. Then I make install the pass and use an example test1.c to see whether it works or not. When I run example using the following command:
clang -emit-llvm test1.c -c -o test1.bc
opt -load …/build_llvm/Debug+Asserts/lib/LLVMHello.so -hello < test1.bc > /dev/null

It shows the following error:

Unknown instruction type encountered!
UNREACHABLE executed at include/llvm/InstVisitor.h:120!
0 opt 0x00000000014190b6 llvm::sys::PrintStackTrace(_IO_FILE*) + 38
1 opt 0x0000000001419333
2 opt 0x0000000001418d8b
3 libpthread.so.0 0x0000003aa600f500
4 libc.so.6 0x0000003aa5c328a5 gsignal + 53
5 libc.so.6 0x0000003aa5c34085 abort + 373
6 opt 0x000000000140089b
7 LLVMHello.so 0x00007f889beb5833
8 LLVMHello.so 0x00007f889beb57bd
9 LLVMHello.so 0x00007f889beb575e
10 LLVMHello.so 0x00007f889beb56c5
11 LLVMHello.so 0x00007f889beb55f2
12 LLVMHello.so 0x00007f889beb5401
13 opt 0x00000000013a4e21 llvm::FPPassManager::runOnFunction(llvm::Function&) + 393
14 opt 0x00000000013a5021 llvm::FPPassManager::runOnModule(llvm::Module&) + 89
15 opt 0x00000000013a5399 llvm::MPPassManager::runOnModule(llvm::Module&) + 573
16 opt 0x00000000013a59a8 llvm::PassManagerImpl::run(llvm::Module&) + 254
17 opt 0x00000000013a5bbf llvm::PassManager::run(llvm::Module&) + 39
18 opt 0x000000000084b455 main + 5591
19 libc.so.6 0x0000003aa5c1ecdd __libc_start_main + 253
20 opt 0x000000000083d359
Stack dump:
0. Program arguments: opt -load …/build_llvm/Debug+Asserts/lib/LLVMHello.so -hello

  1. Running pass ‘Function Pass Manager’ on module ‘’.
  2. Running pass ‘Hello Pass’ on function ‘@main

I will illustrate the pass code, the test1 example, and the IR generated below, so that anyone could help me or give me some suggestion. Thanks.

The Hello.cpp pass is as the following:

#define DEBUG_TYPE "hello"
#include "llvm/Pass.h"
#include "llvm/IR/Module.h"
#include "llvm/InstVisitor.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/raw_ostream.h"
namespace {
 
  struct Hello : public llvm::FunctionPass, llvm::InstVisitor<Hello> {
  private:
    llvm::BasicBlock *FailBB;
  public:
    static char ID;
   Hello() : llvm::FunctionPass(ID) {FailBB = 0;}
 

    virtual bool runOnFunction(llvm::Function &F) {
      
      visit(F);
      return false;
    }

    llvm::BasicBlock *getTrapBB(llvm::Instruction &Inst) {
     
      if (FailBB) return FailBB; 
      llvm::Function *Fn = Inst.getParent()->getParent();
     
      llvm::LLVMContext& ctx = Fn->getContext();
      llvm::IRBuilder<> builder(ctx);
      
      FailBB = llvm::BasicBlock::Create(ctx, "FailBlock", Fn);
      llvm::ReturnInst::Create(Fn->getContext(), FailBB);
      return FailBB;

    }
    void visitLoadInst(llvm::LoadInst & LI) {
    }

   void visitStoreInst(llvm::StoreInst & SI) {
     llvm::Value * Addr = SI.getOperand(1);
     llvm::PointerType* PTy = llvm::cast<llvm::PointerType>(Addr->getType());
     llvm::Type * ElTy = PTy -> getElementType();
     if (!ElTy->isPointerTy()) {
       llvm::BasicBlock *OldBB = SI.getParent();
       llvm::errs() << "yes, got it \n";
       llvm::ICmpInst *Cmp = new llvm::ICmpInst(&SI, llvm::CmpInst::ICMP_EQ, Addr, llvm::Constant::getNullValue(Addr->getType()), "");
       
       llvm::Instruction *Iter = &SI;
       OldBB->getParent()->dump();
       llvm::BasicBlock *NewBB = OldBB->splitBasicBlock(Iter, "newBlock");
       OldBB->getParent()->dump();
      
     }
    
    }
  };

  char Hello::ID = 0;
  static llvm::RegisterPass<Hello> X("hello", "Hello Pass", false, false);
}

The test1.c example is as the following:

#include <stdio.h>
void main() {
  int x;
  x = 5;
}

The IR for the example after adding the pass is as the following:
define void @main() #0 {
entry:
  %x = alloca i32, align 4
  %0 = icmp eq i32* %x, null
  br label %newBlock

newBlock:                                         ; preds = %entry
  store i32 5, i32* %x, align 4
  ret void
}

any suggestion?

hacker cling wrote:

Hello all,
     I was playing with LLVM pass. I changed the
lib/Transforms/Hello/Hello.cpp 's content to be my own pass. Then I make
install the pass and use an example test1.c to see whether it works or
not. When I run example using the following command:
clang -emit-llvm test1.c -c -o test1.bc
opt -load ../build_llvm/Debug+Asserts/lib/LLVMHello.so -hello < test1.bc
> /dev/null

It shows the following error:

Unknown instruction type encountered!
UNREACHABLE executed at include/llvm/InstVisitor.h:120!

The error message here means that the type of instruction -- alloca, add, sub, load, store, etc. -- is one that did not exist in the compiler at the time that opt was built. This is a static list in the compiler, same as documented on llvm.org/docs/LangRef.html .

My guess is that your .bc file is from a very different version of llvm than your 'opt' binary. Perhaps you're using a clang installed on the system and an opt your built yourself? We sometimes change the instruction numbering for the encoding in the .bc files. Does 'opt -verify < test1.bc' work?

Another alternative is that you've managed to form invalid IR in some other way, or corrupted memory. Have you tried building 'opt' and LLVMHello.so with debug (so that llvm's assertions are enabled)? And what about running the 'opt' command under valgrind?

Nick

0 opt 0x00000000014190b6
llvm::sys::PrintStackTrace(_IO_FILE*) + 38
1 opt 0x0000000001419333
2 opt 0x0000000001418d8b
3 libpthread.so.0 0x0000003aa600f500
4 libc.so.6 0x0000003aa5c328a5 gsignal + 53
5 libc.so.6 0x0000003aa5c34085 abort + 373
6 opt 0x000000000140089b
7 LLVMHello.so 0x00007f889beb5833
8 LLVMHello.so 0x00007f889beb57bd
9 LLVMHello.so 0x00007f889beb575e
10 LLVMHello.so 0x00007f889beb56c5
11 LLVMHello.so 0x00007f889beb55f2
12 LLVMHello.so 0x00007f889beb5401
13 opt 0x00000000013a4e21
llvm::FPPassManager::runOnFunction(llvm::Function&) + 393
14 opt 0x00000000013a5021
llvm::FPPassManager::runOnModule(llvm::Module&) + 89
15 opt 0x00000000013a5399
llvm::MPPassManager::runOnModule(llvm::Module&) + 573
16 opt 0x00000000013a59a8
llvm::PassManagerImpl::run(llvm::Module&) + 254
17 opt 0x00000000013a5bbf
llvm::PassManager::run(llvm::Module&) + 39
18 opt 0x000000000084b455 main + 5591
19 libc.so.6 0x0000003aa5c1ecdd __libc_start_main + 253
20 opt 0x000000000083d359
Stack dump:
0. Program arguments: opt -load
../build_llvm/Debug+Asserts/lib/LLVMHello.so -hello
1. Running pass 'Function Pass Manager' on module '<stdin>'.
2. Running pass 'Hello Pass' on function '@main'

I will illustrate the pass code, the test1 example, and the IR generated
below, so that anyone could help me or give me some suggestion. Thanks.

The Hello.cpp pass is as the following:

#define DEBUG_TYPE"hello"
#include"llvm/Pass.h"
#include"llvm/IR/Module.h"
#include"llvm/InstVisitor.h"
#include"llvm/IR/Constants.h"
#include"llvm/IR/IRBuilder.h"
#include"llvm/Support/raw_ostream.h"
namespace {

   struct Hello : public llvm::FunctionPass, llvm::InstVisitor<Hello> {
   private:
     llvm::BasicBlock *FailBB;
   public:
     static char ID;
    Hello() : llvm::FunctionPass(ID) {FailBB = 0;}

     virtual bool runOnFunction(llvm::Function&F) {

       visit(F);
       return false;
     }

     llvm::BasicBlock *getTrapBB(llvm::Instruction&Inst) {

       if (FailBB) return FailBB;
       llvm::Function *Fn = Inst.getParent()->getParent();

       llvm::LLVMContext& ctx = Fn->getContext();
       llvm::IRBuilder<> builder(ctx);

You don't seem to ever use this builder?

Hi Nick,