From 1b1c46b0f1966f08239da738a2d469e8aaa232e3 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sat, 24 Jul 2021 02:01:22 -0400 Subject: [PATCH] Fix cfg_simplify! for empty preds/succs --- base/compiler/compiler.jl | 3 +++ base/compiler/ssair/passes.jl | 4 ++-- test/compiler/irpasses.jl | 20 ++++++++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/base/compiler/compiler.jl b/base/compiler/compiler.jl index 5882c967dbf14..4dabbffcc2a0f 100644 --- a/base/compiler/compiler.jl +++ b/base/compiler/compiler.jl @@ -137,6 +137,9 @@ include("compiler/optimize.jl") # TODO: break this up further + extract utilitie include("compiler/bootstrap.jl") ccall(:jl_set_typeinf_func, Cvoid, (Any,), typeinf_ext_toplevel) +const _COMPILER_WORLD_AGE = ccall(:jl_get_tls_world_age, UInt, ()) +compiler_world_age() = _COMPILER_WORLD_AGE + include("compiler/parsing.jl") Core.eval(Core, :(_parse = Compiler.fl_parse)) diff --git a/base/compiler/ssair/passes.jl b/base/compiler/ssair/passes.jl index 37f5d38940e05..f2b4f95c3ccf9 100644 --- a/base/compiler/ssair/passes.jl +++ b/base/compiler/ssair/passes.jl @@ -1207,12 +1207,12 @@ function cfg_simplify!(ir::IRCode) # Compute (renamed) successors and predecessors given (renamed) block function compute_succs(i) orig_bb = follow_merged_succ(result_bbs[i]) - return map(i -> bb_rename_succ[i], bbs[orig_bb].succs) + return Int[bb_rename_succ[i] for i in bbs[orig_bb].succs] end function compute_preds(i) orig_bb = result_bbs[i] preds = bbs[orig_bb].preds - return map(pred -> bb_rename_pred[pred], preds) + return Int[bb_rename_pred[pred] for pred in preds] end BasicBlock[ diff --git a/test/compiler/irpasses.jl b/test/compiler/irpasses.jl index 3be15ef9cc317..950854fcd6d1f 100644 --- a/test/compiler/irpasses.jl +++ b/test/compiler/irpasses.jl @@ -4,6 +4,18 @@ using Test using Base.Meta using Core: PhiNode, SSAValue, GotoNode, PiNode, QuoteNode, ReturnNode, GotoIfNot +# `cfg_simplify!` is not exercised in the usual compiler pipeline. So, it is +# possible that the methods that are not defined in compiler's world age creep +# into the definition. Let's be extra careful here by testing this function in +# compiler's world age, to keep it usable in the compiler whenever we need it. +function cfg_simplify!(ir) + return Base.invoke_in_world( + Core.Compiler.compiler_world_age(), + Core.Compiler.cfg_simplify!, + ir, + ) +end + # Tests for domsort ## Test that domsort doesn't mangle single-argument phis (#29262) @@ -198,7 +210,7 @@ let src = code_typed(gcd, Tuple{Int, Int})[1].first # Test that cfg_simplify doesn't mangle IR on code with loops ir = Core.Compiler.inflate_ir(src) Core.Compiler.verify_ir(ir) - ir = Core.Compiler.cfg_simplify!(ir) + ir = cfg_simplify!(ir) Core.Compiler.verify_ir(ir) end @@ -221,7 +233,7 @@ let m = Meta.@lower 1 + 1 src.ssaflags = fill(Int32(0), nstmts) ir = Core.Compiler.inflate_ir(src) Core.Compiler.verify_ir(ir) - ir = Core.Compiler.cfg_simplify!(ir) + ir = cfg_simplify!(ir) Core.Compiler.verify_ir(ir) ir = Core.Compiler.compact!(ir) @test length(ir.cfg.blocks) == 1 && Core.Compiler.length(ir.stmts) == 1 @@ -249,7 +261,7 @@ let m = Meta.@lower 1 + 1 src.ssaflags = fill(Int32(0), nstmts) ir = Core.Compiler.inflate_ir(src) Core.Compiler.verify_ir(ir) - ir = Core.Compiler.cfg_simplify!(ir) + ir = cfg_simplify!(ir) Core.Compiler.verify_ir(ir) @test length(ir.cfg.blocks) == 5 ret_2 = ir.stmts.inst[ir.cfg.blocks[3].stmts[end]] @@ -275,7 +287,7 @@ let m = Meta.@lower 1 + 1 src.ssaflags = fill(Int32(0), nstmts) ir = Core.Compiler.inflate_ir(src) Core.Compiler.verify_ir(ir) - ir = Core.Compiler.cfg_simplify!(ir) + ir = cfg_simplify!(ir) Core.Compiler.verify_ir(ir) @test length(ir.cfg.blocks) == 1 end