# Got \`Unreachable reached\` error while using IRTools

**URL:** <https://discourse.julialang.org/t/got-unreachable-reached-error-while-using-irtools/44860>\
**Category:** General Usage\
**Created:** [August 13, 2020, 7:24am UTC](https://discourse.julialang.org/t/got-unreachable-reached-error-while-using-irtools/44860 "2020-08-13T07:24:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![KDr2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdr2/32/2542_2.png) [@KDr2](https://discourse.julialang.org/u/KDr2)\
**Post date:** [August 13, 2020, 7:24am UTC](https://discourse.julialang.org/t/got-unreachable-reached-error-while-using-irtools/44860/1 "2020-08-13T07:24:47Z")

</div>

I am trying to run an [example from the document of IRTools.jl](https://fluxml.ai/IRTools.jl/latest/reference/#Base.insert!), but when I run the modified IR, I got an `Unreachable reached at 0x7ffad63e616d` error:

```julia
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
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’:

```julia
; 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.

---

<div class="post-metadata">

**Author:** ![KDr2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdr2/32/2542_2.png) [@KDr2](https://discourse.julialang.org/u/KDr2)\
**Post date:** [August 14, 2020, 9:33am UTC](https://discourse.julialang.org/t/got-unreachable-reached-error-while-using-irtools/44860/2 "2020-08-14T09:33:33Z")

</div>

`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
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)

```
