From 2cb1227a0076d47cc6d4a07ae4f6a1c9893710fa Mon Sep 17 00:00:00 2001 From: Paul Berg Date: Tue, 17 Oct 2023 00:41:23 +0200 Subject: [PATCH] work around `ReturnNode()` issues until JuliaLang/julia#51715 lands (#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 --------- Co-authored-by: Simeon Schaub --- src/ir/wrap.jl | 6 +++++- src/reflection/utils.jl | 2 +- test/compiler.jl | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/ir/wrap.jl b/src/ir/wrap.jl index 8905671..6df715e 100644 --- a/src/ir/wrap.jl +++ b/src/ir/wrap.jl @@ -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 diff --git a/src/reflection/utils.jl b/src/reflection/utils.jl index 7fac33d..5fb4367 100644 --- a/src/reflection/utils.jl +++ b/src/reflection/utils.jl @@ -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 diff --git a/test/compiler.jl b/test/compiler.jl index fcdd567..634ccec 100644 --- a/test/compiler.jl +++ b/test/compiler.jl @@ -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...) @@ -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