Skip to content

Commit

Permalink
work around ReturnNode() issues until JuliaLang/julia#51715 lands (#…
Browse files Browse the repository at this point in the history
…115)

* Use Core.throw("unreachable") for unreachables

`ReturnNode()` only appears later in the Julia compiler optimization pipeline.

* Use returnode() when possible

* Update src/ir/wrap.jl

Co-authored-by: Simeon Schaub <[email protected]>

---------

Co-authored-by: Simeon Schaub <[email protected]>
  • Loading branch information
Pangoraw and simeonschaub authored Oct 16, 2023
1 parent 6fcac25 commit 2cb1227
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/ir/wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ function IRCode(ir::IR)
x = get(defs, br.args[1], br.args[1]) |> unvars
push!(stmts, ReturnNode(x))
elseif br == unreachable
push!(stmts, ReturnNode())
@static if VERSION >= v"1.10"
push!(stmts, ReturnNode())
else
push!(stmts, Expr(:call, GlobalRef(Core, :throw), "unreachable"))
end
elseif br.condition == nothing
push!(stmts, GotoNode(br.block))
else
Expand Down
2 changes: 1 addition & 1 deletion src/reflection/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function slots!(ci::CodeInfo)
end
if VERSION >= v"1.6.0-DEV.272"
ci.code[i] = MacroTools.prewalk(ci.code[i]) do x
x isa Core.ReturnNode ? Core.ReturnNode(f(x.val)) :
x isa Core.ReturnNode ? (isdefined(x,:val) ? Core.ReturnNode(f(x.val)) : x) :
x isa Core.GotoIfNot ? Core.GotoIfNot(f(x.cond), x.dest) :
f(x)
end
Expand Down
19 changes: 18 additions & 1 deletion test/compiler.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using IRTools, MacroTools, InteractiveUtils, Test
using IRTools: @dynamo, IR, meta, isexpr, xcall, self, insertafter!, recurse!,
argument!, return!, func, var, functional
argument!, return!, block!, branch!, func, var, functional
using InteractiveUtils: code_typed

@dynamo roundtrip(a...) = IR(a...)

Expand Down Expand Up @@ -194,3 +195,19 @@ function f94(x)
end
@test f94(1) == 1
@test passthrough(f94, 1) == 1

@testset "unreachable" begin
# 1: (%1)
# return nothing
# 2:
# unreachable
ir = IR()
argument!(ir)
return!(ir, nothing)
block!(ir)
branch!(ir, 0)

func_ir = IRTools.func(ir)
@test (code_typed(func_ir, Tuple{typeof(func_ir)}) |> only
isa Pair{Core.CodeInfo,DataType})
end

0 comments on commit 2cb1227

Please sign in to comment.