Got `Unreachable reached` error while using IRTools

I am trying to run an example from the document of IRTools.jl, but when I run the modified IR, I got an Unreachable reached at 0x7ffad63e616d error:

julia> using IRTools

julia> f(x) = 3x + 2
f (generic function with 1 method)

julia> ir = @code_ir f(1)
1: (%1, %2)
  %3 = 3 * %2
  %4 = %3 + 2
  return %4

julia> pushfirst!(ir, :(println("hello, world")))
%5

julia> ir
1: (%1, %2)
  %5 = println("hello, world")
  %3 = 3 * %2
  %4 = %3 + 2
  return %4

julia> IRTools.evalir(ir, f, 3)
hello, world
Unreachable reached at 0x7ffad63e616d

signal (4): Illegal instruction
in expression starting at REPL[11]:1
##422 at /data/zhuoql/.julia/packages/IRTools/5JxlL/src/eval.jl:18
unknown function (ip: 0x7ffad63e6183)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2141 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1631 [inlined]
jl_f__apply at /buildworker/worker/package_linux64/build/src/builtins.c:627
jl_f__apply_latest at /buildworker/worker/package_linux64/build/src/builtins.c:665
#invokelatest#1 at ./essentials.jl:709
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2135 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1631 [inlined]
jl_f__apply at /buildworker/worker/package_linux64/build/src/builtins.c:627
invokelatest at ./essentials.jl:708
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2135 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1631 [inlined]
jl_f__apply at /buildworker/worker/package_linux64/build/src/builtins.c:627
evalir at /data/zhuoql/.julia/packages/IRTools/5JxlL/src/eval.jl:24
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2141 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305

If I keep the IR untouched, all goes well:

julia> f(x) = 3x + 2
f (generic function with 1 method)

julia> ir = @code_ir f(1)
1: (%1, %2)
  %3 = 3 * %2
  %4 = %3 + 2
  return %4

julia> IRTools.evalir(ir, f, 3)
11

I inspected the LLVM IR of the modified Julia IR (println(...) inserted), it shows the function has a ‘nonreturn attribute’:

; Function Attrs: noreturn
define nonnull %jl_value_t addrspace(10)* @julia__cow_18018(i64) #0 {
  ...
  call void @llvm.trap()
  unreachable
}

I am testing this with Julia 1.3, 1.4 and IRTools 0.3.3, 0.4.0.
Why and how to fix this? Thanks.

pushfirst!(ir, IRTools.xcall(Base, :println, "hello, world")) works.

Though IRTools.xcall(Base, :println, "hello, world") returns an Expr whose representation is :(Base.println("hello, world")), it is not identified with :(Base.println("hello, world")):

julia> a=IRTools.xcall(Base, :println, "Hi")
:(Base.println("Hi"))

julia> b=:(Base.println("Hi"))
:(Base.println("Hi"))

julia> a.args[1], a.args[1] |> typeof
(:(Base.println), GlobalRef)

julia> b.args[1], b.args[1] |> typeof
(:(Base.println), Expr)
3 Likes