Skip to content

Commit

Permalink
add construct_[post]domtree(::[IRCode|CFG]) interfaces (#53638)
Browse files Browse the repository at this point in the history
We currently have `construct_[post]domtree(::Vector{BasicBlock})` only.
The higher level interfaces are often convenient.
  • Loading branch information
aviatesk committed Mar 8, 2024
1 parent d45581c commit f6504e4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
6 changes: 3 additions & 3 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mutable struct LazyCFGReachability
end
function get!(x::LazyCFGReachability)
isdefined(x, :reachability) && return x.reachability
domtree = construct_domtree(x.ir.cfg.blocks)
domtree = construct_domtree(x.ir)
return x.reachability = CFGReachability(x.ir.cfg, domtree)
end

Expand All @@ -189,8 +189,8 @@ end
function get!(x::LazyGenericDomtree{IsPostDom}) where {IsPostDom}
isdefined(x, :domtree) && return x.domtree
return @timeit "domtree 2" x.domtree = IsPostDom ?
construct_postdomtree(x.ir.cfg.blocks) :
construct_domtree(x.ir.cfg.blocks)
construct_postdomtree(x.ir) :
construct_domtree(x.ir)
end

const LazyDomtree = LazyGenericDomtree{false}
Expand Down
4 changes: 2 additions & 2 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ function get!(lazyagdomtree::LazyAugmentedDomtree)
cfg_insert_edge!(cfg, bb, length(cfg.blocks))
end
end
domtree = construct_domtree(cfg.blocks)
domtree = construct_domtree(cfg)
return lazyagdomtree.agdomtree = AugmentedDomtree(cfg, domtree)
end

Expand Down Expand Up @@ -1180,7 +1180,7 @@ function slot2reg(ir::IRCode, ci::CodeInfo, sv::OptimizationState)
# need `ci` for the slot metadata, IR for the code
svdef = sv.linfo.def
nargs = isa(svdef, Method) ? Int(svdef.nargs) : 0
@timeit "domtree 1" domtree = construct_domtree(ir.cfg.blocks)
@timeit "domtree 1" domtree = construct_domtree(ir)
defuse_insts = scan_slot_def_use(nargs, ci, ir.stmts.stmt)
𝕃ₒ = optimizer_lattice(sv.inlining.interp)
@timeit "construct_ssa" ir = construct_ssa!(ci, ir, sv, domtree, defuse_insts, 𝕃ₒ) # consumes `ir`
Expand Down
6 changes: 6 additions & 0 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ function IRCode()
return ir
end

construct_domtree(ir::IRCode) = construct_domtree(ir.cfg)
construct_domtree(cfg::CFG) = construct_domtree(cfg.blocks)

construct_postdomtree(ir::IRCode) = construct_postdomtree(ir.cfg)
construct_postdomtree(cfg::CFG) = construct_postdomtree(cfg.blocks)

function block_for_inst(ir::IRCode, inst::Int)
if inst > length(ir.stmts)
inst = ir.new_nodes.info[inst - length(ir.stmts)].pos
Expand Down
4 changes: 2 additions & 2 deletions test/compiler/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let code = Any[
ReturnNode(Core.SSAValue(10)),
]
ir = make_ircode(code)
domtree = Core.Compiler.construct_domtree(ir.cfg.blocks)
domtree = Core.Compiler.construct_domtree(ir)
ir = Core.Compiler.domsort_ssa!(ir, domtree)
Core.Compiler.verify_ir(ir)
phi = ir.stmts.stmt[3]
Expand All @@ -47,7 +47,7 @@ let code = Any[]
push!(code, Expr(:call, :opaque))
push!(code, ReturnNode(nothing))
ir = make_ircode(code)
domtree = Core.Compiler.construct_domtree(ir.cfg.blocks)
domtree = Core.Compiler.construct_domtree(ir)
ir = Core.Compiler.domsort_ssa!(ir, domtree)
Core.Compiler.verify_ir(ir)
end
Expand Down
14 changes: 7 additions & 7 deletions test/compiler/ssair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ let cfg = CFG(BasicBlock[
@test dfs.from_pre[dfs.to_parent_pre[dfs.to_pre[5]]] == 4
let correct_idoms = Compiler.naive_idoms(cfg.blocks),
correct_pidoms = Compiler.naive_idoms(cfg.blocks, true)
@test Compiler.construct_domtree(cfg.blocks).idoms_bb == correct_idoms
@test Compiler.construct_postdomtree(cfg.blocks).idoms_bb == correct_pidoms
@test Compiler.construct_domtree(cfg).idoms_bb == correct_idoms
@test Compiler.construct_postdomtree(cfg).idoms_bb == correct_pidoms
# For completeness, reverse the order of pred/succ in the CFG and verify
# the answer doesn't change (it does change the which node is chosen
# as the semi-dominator, since it changes the DFS numbering).
Expand All @@ -85,8 +85,8 @@ let cfg = CFG(BasicBlock[
c && (blocks[4] = make_bb(reverse(blocks[4].preds), blocks[4].succs))
d && (blocks[5] = make_bb(reverse(blocks[5].preds), blocks[5].succs))
cfg′ = CFG(blocks, cfg.index)
@test Compiler.construct_domtree(cfg′.blocks).idoms_bb == correct_idoms
@test Compiler.construct_postdomtree(cfg′.blocks).idoms_bb == correct_pidoms
@test Compiler.construct_domtree(cfg′).idoms_bb == correct_idoms
@test Compiler.construct_postdomtree(cfg′).idoms_bb == correct_pidoms
end
end
end
Expand Down Expand Up @@ -292,7 +292,7 @@ let cfg = CFG(BasicBlock[
make_bb([2, 6], []),
make_bb([4], [5, 3]),
], Int[])
domtree = Compiler.construct_domtree(cfg.blocks)
domtree = Compiler.construct_domtree(cfg)
@test domtree.dfs_tree.to_pre == [1, 2, 4, 5, 3, 6]
@test domtree.idoms_bb == Compiler.naive_idoms(cfg.blocks) == [0, 1, 1, 3, 1, 4]

Expand Down Expand Up @@ -486,7 +486,7 @@ let ir = Base.code_ircode((Bool,Any)) do c, x
end
end
# domination analysis
domtree = Core.Compiler.construct_domtree(ir.cfg.blocks)
domtree = Core.Compiler.construct_domtree(ir)
@test Core.Compiler.dominates(domtree, 1, 2)
@test Core.Compiler.dominates(domtree, 1, 3)
@test Core.Compiler.dominates(domtree, 1, 4)
Expand All @@ -497,7 +497,7 @@ let ir = Base.code_ircode((Bool,Any)) do c, x
end
end
# post domination analysis
post_domtree = Core.Compiler.construct_postdomtree(ir.cfg.blocks)
post_domtree = Core.Compiler.construct_postdomtree(ir)
@test Core.Compiler.postdominates(post_domtree, 4, 1)
@test Core.Compiler.postdominates(post_domtree, 4, 2)
@test Core.Compiler.postdominates(post_domtree, 4, 3)
Expand Down
2 changes: 1 addition & 1 deletion test/compiler/tarjan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function test_reachability(V, E; deletions = 2E ÷ 3, all_checks=false)
end

cfg = rand_cfg(V, E)
domtree = Core.Compiler.construct_domtree(cfg.blocks)
domtree = Core.Compiler.construct_domtree(cfg)
reachability = CFGReachability(cfg, domtree)
check_reachability(reachability, cfg, domtree, all_checks)

Expand Down

0 comments on commit f6504e4

Please sign in to comment.