Skip to content

Commit adff8e7

Browse files
committed
perf: Only run final commit if adce_pass! made any intersting changes
adce_pass! runs its own compaction, so we only need to run another run of compaction if it made any post-compaction changes.
1 parent 1704053 commit adff8e7

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

base/compiler/inferencestate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ const CACHE_MODE_VOLATILE = 0x01 << 2 # not cached, optimization allowed
205205

206206
mutable struct TryCatchFrame
207207
exct
208-
const enter_idx
208+
const enter_idx::Int
209209
TryCatchFrame(@nospecialize(exct), enter_idx::Int) = new(exct, enter_idx)
210210
end
211211

base/compiler/optimize.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,8 +925,10 @@ function run_passes_ipo_safe(
925925
# @timeit "verify 2" verify_ir(ir)
926926
@pass "compact 2" ir = compact!(ir)
927927
@pass "SROA" ir = sroa_pass!(ir, sv.inlining)
928-
@pass "ADCE" ir = adce_pass!(ir, sv.inlining)
929-
@pass "compact 3" ir = compact!(ir, true)
928+
@pass "ADCE" (ir, made_changes) = adce_pass!(ir, sv.inlining)
929+
if made_changes
930+
@pass "compact 3" ir = compact!(ir, true)
931+
end
930932
if JLOptions().debug_level == 2
931933
@timeit "verify 3" (verify_ir(ir, true, false, optimizer_lattice(sv.inlining.interp)); verify_linetable(ir.linetable))
932934
end

base/compiler/ssair/ir.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,14 @@ function insert_node_here!(compact::IncrementalCompact, newinst::NewInstruction,
946946
return inst
947947
end
948948

949+
function delete_inst_here!(compact)
950+
# Delete the statement, update refcounts etc
951+
compact[compact.idx-1] = nothing
952+
# Pretend that we never compacted this statement in the first place
953+
compact.idx -= 1
954+
return nothing
955+
end
956+
949957
function getindex(view::TypesView, v::OldSSAValue)
950958
id = v.id
951959
ir = view.ir.ir

base/compiler/ssair/passes.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,7 @@ function adce_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
17381738
all_phis = Int[]
17391739
unionphis = Pair{Int,Any}[] # sorted
17401740
compact = IncrementalCompact(ir, true)
1741+
made_changes = false
17411742
for ((_, idx), stmt) in compact
17421743
if isa(stmt, PhiNode)
17431744
push!(all_phis, idx)
@@ -1759,7 +1760,7 @@ function adce_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
17591760
# nullify safe `typeassert` calls
17601761
ty, isexact = instanceof_tfunc(argextype(stmt.args[3], compact), true)
17611762
if isexact && (𝕃ₒ, argextype(stmt.args[2], compact), ty)
1762-
compact[idx] = nothing
1763+
delete_inst_here!(compact)
17631764
continue
17641765
end
17651766
end
@@ -1801,6 +1802,7 @@ function adce_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
18011802
if t === Union{}
18021803
stmt = compact[SSAValue(phi)][:stmt]::PhiNode
18031804
kill_phi!(compact, phi_uses, 1:length(stmt.values), SSAValue(phi), stmt, true)
1805+
made_changes = true
18041806
continue
18051807
elseif t === Any
18061808
continue
@@ -1822,16 +1824,17 @@ function adce_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
18221824
end
18231825
compact.result[phi][:type] = t
18241826
kill_phi!(compact, phi_uses, to_drop, SSAValue(phi), stmt, false)
1827+
made_changes = true
18251828
end
18261829
# Perform simple DCE for unused values
18271830
extra_worklist = Int[]
18281831
for (idx, nused) in Iterators.enumerate(compact.used_ssas)
18291832
idx >= compact.result_idx && break
18301833
nused == 0 || continue
1831-
adce_erase!(phi_uses, extra_worklist, compact, idx, false)
1834+
made_changes |= adce_erase!(phi_uses, extra_worklist, compact, idx, false)
18321835
end
18331836
while !isempty(extra_worklist)
1834-
adce_erase!(phi_uses, extra_worklist, compact, pop!(extra_worklist), true)
1837+
made_changes |= adce_erase!(phi_uses, extra_worklist, compact, pop!(extra_worklist), true)
18351838
end
18361839
# Go back and erase any phi cycles
18371840
changed = true
@@ -1852,10 +1855,11 @@ function adce_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
18521855
while !isempty(extra_worklist)
18531856
if adce_erase!(phi_uses, extra_worklist, compact, pop!(extra_worklist), true)
18541857
changed = true
1858+
made_changes = true
18551859
end
18561860
end
18571861
end
1858-
return complete(compact)
1862+
return Pair{IRCode, Bool}(complete(compact), made_changes)
18591863
end
18601864

18611865
function is_bb_empty(ir::IRCode, bb::BasicBlock)

0 commit comments

Comments
 (0)