Skip to content

Commit

Permalink
sort out argument order of maybe_erase_unused! (#47701)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk authored Nov 25, 2022
1 parent 039d8fd commit 9d25932
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
38 changes: 16 additions & 22 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,6 @@ end
insert_node!(ir::IRCode, pos::Int, newinst::NewInstruction, attach_after::Bool=false) =
insert_node!(ir, SSAValue(pos), newinst, attach_after)


mutable struct IncrementalCompact
ir::IRCode
result::InstructionStream
Expand Down Expand Up @@ -1606,20 +1605,18 @@ function iterate_compact(compact::IncrementalCompact)
return Pair{Int,Int}(compact.idx-1, old_result_idx)
end

function maybe_erase_unused!(
extra_worklist::Vector{Int}, compact::IncrementalCompact, idx::Int, in_worklist::Bool,
callback = null_dce_callback)

inst = idx <= length(compact.result) ? compact.result[idx] :
compact.new_new_nodes.stmts[idx - length(compact.result)]
maybe_erase_unused!(compact::IncrementalCompact, idx::Int, in_worklist::Bool, extra_worklist::Vector{Int}) =
maybe_erase_unused!(null_dce_callback, compact, idx, in_worklist, extra_worklist)
function maybe_erase_unused!(callback::Function, compact::IncrementalCompact, idx::Int,
in_worklist::Bool, extra_worklist::Vector{Int})
nresult = length(compact.result)
inst = idx nresult ? compact.result[idx] : compact.new_new_nodes.stmts[idx-nresult]
stmt = inst[:inst]
stmt === nothing && return false
if inst[:type] === Bottom
effect_free = false
else
effect_free = inst[:flag] & IR_FLAG_EFFECT_FREE != 0
end
function kill_ssa_value(val::SSAValue)
inst[:type] === Bottom && return false
effect_free = (inst[:flag] & IR_FLAG_EFFECT_FREE) 0
effect_free || return false
foreachssa(stmt) do val::SSAValue
if compact.used_ssas[val.id] == 1
if val.id < idx || in_worklist
push!(extra_worklist, val.id)
Expand All @@ -1628,12 +1625,8 @@ function maybe_erase_unused!(
compact.used_ssas[val.id] -= 1
callback(val)
end
if effect_free
foreachssa(kill_ssa_value, stmt)
inst[:inst] = nothing
return true
end
return false
inst[:inst] = nothing
return true
end

struct FixedNode
Expand Down Expand Up @@ -1722,16 +1715,17 @@ function just_fixup!(compact::IncrementalCompact, new_new_nodes_offset::Union{In
end
end

function simple_dce!(compact::IncrementalCompact, callback = null_dce_callback)
simple_dce!(compact::IncrementalCompact) = simple_dce!(null_dce_callback, compact)
function simple_dce!(callback::Function, compact::IncrementalCompact)
# Perform simple DCE for unused values
@assert isempty(compact.new_new_used_ssas) # just_fixup! wasn't run?
extra_worklist = Int[]
for (idx, nused) in Iterators.enumerate(compact.used_ssas)
nused == 0 || continue
maybe_erase_unused!(extra_worklist, compact, idx, false, callback)
maybe_erase_unused!(callback, compact, idx, false, extra_worklist)
end
while !isempty(extra_worklist)
maybe_erase_unused!(extra_worklist, compact, pop!(extra_worklist), true, callback)
maybe_erase_unused!(callback, compact, pop!(extra_worklist), true, extra_worklist)
end
end

Expand Down
10 changes: 7 additions & 3 deletions base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,9 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing, InliningState} = nothin
# but before the DCE) for our predicate within `sroa_mutables!`, but we also
# try an extra effort using a callback so that reference counts are updated
used_ssas = copy(compact.used_ssas)
simple_dce!(compact, (x::SSAValue) -> used_ssas[x.id] -= 1)
simple_dce!(compact) do x::SSAValue
used_ssas[x.id] -= 1
end
ir = complete(compact)
sroa_mutables!(ir, defuses, used_ssas, lazydomtree, inlining)
return ir
Expand Down Expand Up @@ -1485,9 +1487,11 @@ end
function adce_erase!(phi_uses::Vector{Int}, extra_worklist::Vector{Int}, compact::IncrementalCompact, idx::Int, in_worklist::Bool)
# return whether this made a change
if isa(compact.result[idx][:inst], PhiNode)
return maybe_erase_unused!(extra_worklist, compact, idx, in_worklist, val::SSAValue -> phi_uses[val.id] -= 1)
return maybe_erase_unused!(compact, idx, in_worklist, extra_worklist) do val::SSAValue
phi_uses[val.id] -= 1
end
else
return maybe_erase_unused!(extra_worklist, compact, idx, in_worklist)
return maybe_erase_unused!(compact, idx, in_worklist, extra_worklist)
end
end

Expand Down

0 comments on commit 9d25932

Please sign in to comment.