Skip to content

Commit 6c78ef0

Browse files
authored
optimizer: improve SROA performance (#43488)
Essentially this commit reduces the runtime count # of domtree construction. The key observation is that SROA of mutable allocations isn't often successful. More specifically, during the sysimg build, `sroa_mutables!` is called `5776` times, and actual SROA happened only for `126` cases out of those cases. (, which means we should be able to avoid domtree construction in `5650`/`5776` cases).
1 parent 5200211 commit 6c78ef0

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

base/compiler/ssair/passes.jl

+14-7
Original file line numberDiff line numberDiff line change
@@ -820,11 +820,8 @@ function sroa_pass!(ir::IRCode)
820820
end
821821

822822
function sroa_mutables!(ir::IRCode, defuses::IdDict{Int, Tuple{SPCSet, SSADefUse}}, used_ssas::Vector{Int})
823-
# Compute domtree, needed below, now that we have finished compacting the IR.
824-
# This needs to be after we iterate through the IR with `IncrementalCompact`
825-
# because removing dead blocks can invalidate the domtree.
826-
@timeit "domtree 2" domtree = construct_domtree(ir.cfg.blocks)
827-
823+
# initialization of domtree is delayed to avoid the expensive computation in many cases
824+
local domtree = nothing
828825
for (idx, (intermediaries, defuse)) in defuses
829826
intermediaries = collect(intermediaries)
830827
# Check if there are any uses we did not account for. If so, the variable
@@ -885,16 +882,26 @@ function sroa_mutables!(ir::IRCode, defuses::IdDict{Int, Tuple{SPCSet, SSADefUse
885882
isempty(du.uses) && continue
886883
push!(du.defs, newidx)
887884
ldu = compute_live_ins(ir.cfg, du)
888-
phiblocks = isempty(ldu.live_in_bbs) ? Int[] : iterated_dominance_frontier(ir.cfg, ldu, domtree)
885+
if isempty(ldu.live_in_bbs)
886+
phiblocks = Int[]
887+
else
888+
domtree === nothing && (@timeit "domtree 2" domtree = construct_domtree(ir.cfg.blocks))
889+
phiblocks = iterated_dominance_frontier(ir.cfg, ldu, domtree)
890+
end
889891
allblocks = sort(vcat(phiblocks, ldu.def_bbs))
890892
blocks[fidx] = phiblocks, allblocks
891893
if fidx + 1 > length(defexpr.args)
892894
for use in du.uses
895+
domtree === nothing && (@timeit "domtree 2" domtree = construct_domtree(ir.cfg.blocks))
893896
has_safe_def(ir, domtree, allblocks, du, newidx, use) || @goto skip
894897
end
895898
end
896899
end
897-
# Everything accounted for. Go field by field and perform idf
900+
# Everything accounted for. Go field by field and perform idf:
901+
# Compute domtree now, needed below, now that we have finished compacting the IR.
902+
# This needs to be after we iterate through the IR with `IncrementalCompact`
903+
# because removing dead blocks can invalidate the domtree.
904+
domtree === nothing && (@timeit "domtree 2" domtree = construct_domtree(ir.cfg.blocks))
898905
preserve_uses = isempty(defuse.ccall_preserve_uses) ? nothing :
899906
IdDict{Int, Vector{Any}}((idx=>Any[] for idx in SPCSet(defuse.ccall_preserve_uses)))
900907
for fidx in 1:ndefuse

0 commit comments

Comments
 (0)