From 55759afdd1874fa5d9d97d6042a864b88d60526f Mon Sep 17 00:00:00 2001 From: Leon Shen Date: Mon, 23 Sep 2019 18:21:55 -0400 Subject: [PATCH] fix bug related to block renaming for DCE --- base/compiler/ssair/ir.jl | 4 ++-- test/compiler/ssair.jl | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/base/compiler/ssair/ir.jl b/base/compiler/ssair/ir.jl index 0aec26d444476..665387c1143b6 100644 --- a/base/compiler/ssair/ir.jl +++ b/base/compiler/ssair/ir.jl @@ -1001,14 +1001,14 @@ end function finish_current_bb!(compact, active_bb, old_result_idx=compact.result_idx, unreachable=false) if compact.active_result_bb > length(compact.result_bbs) - #@assert compact.bb_rename[active_bb] == 0 + #@assert compact.bb_rename[active_bb] == -1 return true end bb = compact.result_bbs[compact.active_result_bb] # If this was the last statement in the BB and we decided to skip it, insert a # dummy `nothing` node, to prevent changing the structure of the CFG skipped = false - if !compact.cfg_transforms_enabled || active_bb == 0 || active_bb > length(compact.bb_rename_succ) || compact.bb_rename_succ[active_bb] != 0 + if !compact.cfg_transforms_enabled || active_bb == 0 || active_bb > length(compact.bb_rename_succ) || compact.bb_rename_succ[active_bb] != -1 if compact.result_idx == first(bb.stmts) length(compact.result) < old_result_idx && resize!(compact, old_result_idx) if unreachable diff --git a/test/compiler/ssair.jl b/test/compiler/ssair.jl index 30f53b124ed66..116e099398b7b 100644 --- a/test/compiler/ssair.jl +++ b/test/compiler/ssair.jl @@ -132,3 +132,34 @@ function f32579(x::Int, b::Bool) end @test f32579(0, true) === true @test f32579(0, false) === false + +# Test for bug caused by renaming blocks improperly, related to PR #32145 +using Base.Meta +let ci = (Meta.@lower 1 + 1).args[1] + ci.code = [ + # block 1 + Core.Compiler.GotoIfNot(Expr(:boundscheck), 6), + # block 2 + Expr(:call, GlobalRef(Base, :size), Core.Compiler.Argument(3)), + Core.Compiler.ReturnNode(), + # block 3 + Core.PhiNode(), + Core.Compiler.ReturnNode(), + # block 4 + Expr(:call, + GlobalRef(Main, :something), + GlobalRef(Main, :somethingelse)), + Core.Compiler.GotoIfNot(Core.SSAValue(6), 9), + # block 5 + Core.Compiler.ReturnNode(Core.SSAValue(6)), + # block 6 + Core.Compiler.ReturnNode(Core.SSAValue(6)) + ] + nstmts = length(ci.code) + ci.ssavaluetypes = nstmts + ci.codelocs = fill(Int32(1), nstmts) + ci.ssaflags = fill(Int32(0), nstmts) + ir = Core.Compiler.inflate_ir(ci) + ir = Core.Compiler.compact!(ir, true) + @test Core.Compiler.verify_ir(ir) == nothing +end