Skip to content

Commit

Permalink
Use Core.throw("unreachable") for unreachables
Browse files Browse the repository at this point in the history
`ReturnNode()` only appears later in the Julia compiler optimization pipeline.
  • Loading branch information
Pangoraw committed Oct 15, 2023
1 parent 6fcac25 commit 96318f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ir/wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function IRCode(ir::IR)
x = get(defs, br.args[1], br.args[1]) |> unvars
push!(stmts, ReturnNode(x))
elseif br == unreachable
push!(stmts, ReturnNode())
push!(stmts, Expr(:call, GlobalRef(Core, :throw), "unreachable"))
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
16 changes: 16 additions & 0 deletions 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
using InteractiveUtils: code_typed

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

Expand Down Expand Up @@ -194,3 +195,18 @@ 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)

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

0 comments on commit 96318f9

Please sign in to comment.