Search code examples
clangllvmllvm-clangllvm-ir

Error when direct executing program from LLVM bitcode (expected instruction opcode)


I'm trying to run a program from the LLVM bitcode generated by my compiler, but when I run the lli command it returns an error

lli-3.6: test2.ll:9:1: error: expected instruction opcode

When I use the lli with the .ll generated by clang -S -emit-llvm it works. There are many optimizations in this code, though. I tried to insert some of them manually, but it didn't work.

My problem is that I don't know if the structure of my code is correct, or if it is just missing something particular for the interpreter to work properly. Originally, I tried to use the JIT in the code, but it was giving me more errors with the libraries and the documention was not helpful.

My llvm bitcode is the following:

%struct.test = type { i32, i32 }

define internal void @test_program() {
entry:
  %a = alloca i32
  store i32 5, i32* %a
  call void @printf(i32 3)
  %bar = alloca %struct.test
}

define internal void @f(i32 %x) {
entry:
  %b = alloca i32
  %mul = mul i32 6, 2
  %add = add i32 %mul, 3
  %add1 = add i32 10, %add
  store i32 %add1, i32* %b
  %tmp_eq = icmp eq i32* %b, i32 25
  br i1 %tmp_eq, label %cond_true, label %cond_false

cond_true:                                        ; preds = %entry
  store i32 40, i32* %b

cond_false:                                       ; preds = %entry
  store i32 50, i32* %b
}

declare void @printf() 

Solution

  • The LLVM IR file you provided is malformed - it's missing terminator instructions on its basic blocks (except on %entry in @f). It looks like you have a bug in your custom optimizations.