Skip to content

Instantly share code, notes, and snippets.

@tshort
Created October 12, 2019 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tshort/f23bbc25d418e410e9b546fa75c01f9c to your computer and use it in GitHub Desktop.
Save tshort/f23bbc25d418e410e9b546fa75c01f9c to your computer and use it in GitHub Desktop.
Uses error trying to fix up part of an LLVM IR

Test script that gives an error:

using LLVM 

ctx = Context()
mod = parse(LLVM.Module,  """
    %jl_value_t = type opaque
    define i64 @fun() {
    top:
      %0 = load %jl_value_t*, %jl_value_t** inttoptr (i64 140070056653680 to %jl_value_t**)
      %1 = ptrtoint %jl_value_t* %0 to i64
      ret i64 %1
    }
    """, ctx)

verify(mod)
fun = functions(mod)["fun"]
inst = first(instructions(first(blocks(fun))))

o1(x) = operands(x)[1]

ctx = Context()
builder = Builder(ctx)
gptr = GlobalVariable(mod, LLVM.Int64Type(ctx), "globalint")
position!(builder, inst)
operands(o1(inst))[1] = load!(builder, gptr)

@show mod
verify(mod)

Here is the error message:

mod = 
%jl_value_t = type opaque

@globalint = external global i64

define i64 @fun() {
top:
  %0 = load i64, i64* @globalint
  %1 = load %jl_value_t*, %jl_value_t** inttoptr (i64 %0 to %jl_value_t**)
  %2 = ptrtoint %jl_value_t* %1 to i64
  ret i64 %2
}

ERROR: LoadError: LLVM error: Use of instruction is not an instruction!
  %0 = load i64, i64* @globalint

Stacktrace:
 [1] verify(::LLVM.Module) at /home/tshort/.julia/packages/LLVM/IoThY/src/analysis.jl:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment