1

I'm having the following problem when I run my program:

pseudo instructions should be removed before code emission
UNREACHABLE executed at /home/leonor/llvm/llvm/lib/Target/X86/X86CodeEmitter.cpp:1164!
Stack dump:
0.  Running pass 'X86 Machine Code Emitter' on function '@main'
./build/Release+Asserts/bin/llvm-dis: Bitcode stream must be at least 16 bytes in length

My program takes as input a .bc file and then loads the file and shows it. My doubt is: Why is this error happens only when the C program contains conditional statements (if, for ..). How to solve??

My code:

int main(int argc, char **argv) {

  InitializeNativeTarget();
  LLVMContext &Context = getGlobalContext();
  std::string Err;
  const std::string InputFile = "teste_f1.bc"; 
  OwningPtr<MemoryBuffer> result;
  error_code ec = MemoryBuffer::getFile(InputFile, result);
  MemoryBuffer *buffer = result.take();
  Module * Mod = ParseBitcodeFile(buffer, Context);

  ExecutionEngine* EE = 0;
  EngineBuilder builder(Mod);
  builder.setErrorStr(&Err);
  builder.setEngineKind(EngineKind::JIT);   
  EE = builder.create();

  Function * func = Mod->getFunction("main");
  std::vector <std::string> params;
  params.push_back(Mod->getModuleIdentifier());

  EE->runStaticConstructorsDestructors(false);
  int Result = EE->runFunctionAsMain(func, params, NULL);
  EE->runStaticConstructorsDestructors(true);

  WriteBitcodeToFile(Mod, outs());

  delete Mod;
  return 0;

}
user2084755
  • 169
  • 1
  • 9
  • "My program takes as input a .bc file and then loads the file and shows it" - well, that's not what the code does. The code loads a .bc file, *executes the `main` function inside*, then dumps back the contents of the .bc file (which is binary) to the output. And the problem occurs during the execution (or more accurately, the JIT compilation). By the way, if you follow this with `llvm-dis`, you can just instead call `print` or `dump` on the module to get the textual representation. – Oak Aug 18 '13 at 06:47

1 Answers1

0

It is because the code containing conditional statements (if,for etc), results in an IR which contains phi nodes. You can remove the phi nodes by using reg2mem pass. The command would be:

opt -reg2mem -o output.bc input.bc
shrm
  • 1,112
  • 2
  • 8
  • 20
  • The `reg2mem` pass **adds** phi nodes, it doesn't remove them (by replacing loads and stores with phis). Also I don't see why the presence of phi nodes should make the x86 code emitter not work... – Oak Aug 18 '13 at 06:51
  • @Oak `reg2mem` does **not** add phi nodes, take a look at the following links from the llvm mailing lists. If you are still not convinced, try it out on a piece of code yourself. How do you think a machine will execute phi node in assembly ? Why will code emitter emit phi nodes if the machine cannot execute phi nodes? Links: http://llvm.1065342.n5.nabble.com/PHI-nodes-td2116.html and https://groups.google.com/forum/#!topic/llvm-dev/UMaYSqdT014 – shrm Aug 18 '13 at 07:21
  • my mistake; I read `reg2mem` as `mem2reg` :) But I still don't get the part about phi nodes and emitting code. LLVM IR is indeed not x86 assembly, which is why there's instruction lowering and selection, which should take care of phi nodes. – Oak Aug 18 '13 at 07:31
  • 1
    @Oak OP is facing error at x86CodeEmitter, not LLVM IR emitter. There is a machine function pass called PHIElimination, which means phi nodes are lowered down to assembly, but nevertheless need to be removed before final code is emitted. As to why phi nodes are propagated till assembly, is a good question, maybe because some machine level passes depend on it. Consider correcting your mistake by upvoting. – shrm Aug 18 '13 at 07:43