From e7e24eeb5f9beb2bcbe066f766c717673b8ee717 Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 15 Oct 2023 16:35:53 +0200 Subject: [PATCH] Handle `unreachable` inst in `find_ssavalue_uses`. --- base/compiler/utilities.jl | 1 + test/staged.jl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/base/compiler/utilities.jl b/base/compiler/utilities.jl index bdc0156efd824e..78079e6174f297 100644 --- a/base/compiler/utilities.jl +++ b/base/compiler/utilities.jl @@ -390,6 +390,7 @@ function find_ssavalue_uses(body::Vector{Any}, nvals::Int) for line in 1:length(body) e = body[line] if isa(e, ReturnNode) + isdefined(e, :val) || continue e = e.val elseif isa(e, GotoIfNot) e = e.cond diff --git a/test/staged.jl b/test/staged.jl index 5204f5f6ca777a..4e3f2eef40e8a3 100644 --- a/test/staged.jl +++ b/test/staged.jl @@ -346,3 +346,33 @@ let world = Base.get_world_counter() @test all(lin->lin.method === :sin, src.linetable) @test sin_generated(42) == sin(42) end + +# Allow passing unreachable insts in generated codeinfo +let + dummy() = return + dummy_m = which(dummy, Tuple{}) + + src = Base.uncompressed_ir(dummy_m) + src.code = Any[ + # block 1 + Core.ReturnNode(nothing), + # block 2 + Core.ReturnNode(), + ] + nstmts = length(src.code) + nslots = 1 + src.ssavaluetypes = nstmts + src.codelocs = fill(Int32(1), nstmts) + src.ssaflags = fill(Int32(0), nstmts) + src.slotflags = fill(0, nslots) + src.slottypes = Any[Any] + + @eval function f_unreachable() + $(Expr(:meta, :generated, Returns(src))) + $(Expr(:meta, :generated_only)) + end + + ir, _ = Base.code_ircode(f_unreachable, ()) |> only + @test length(ir.cfg.blocks) == 1 +end +