Skip to content

Commit

Permalink
SROA: remove dead lifting cache mechanism (#51014)
Browse files Browse the repository at this point in the history
It hadn't been fixed for years.
Let's get rid of the unnecessary allocation cost.
  • Loading branch information
aviatesk authored Sep 11, 2023
1 parent 7b9fdf8 commit ea49abe
Showing 1 changed file with 13 additions and 36 deletions.
49 changes: 13 additions & 36 deletions base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,7 @@ end
function lift_comparison! end

function lift_comparison!(::typeof(===), compact::IncrementalCompact,
idx::Int, stmt::Expr, lifting_cache::IdDict{Pair{AnySSAValue, Any}, AnySSAValue},
𝕃ₒ::AbstractLattice)
idx::Int, stmt::Expr, 𝕃ₒ::AbstractLattice)
args = stmt.args
length(args) == 3 || return
lhs, rhs = args[2], args[3]
Expand All @@ -538,28 +537,26 @@ function lift_comparison!(::typeof(===), compact::IncrementalCompact,
else
return
end
lift_comparison_leaves!(egal_tfunc, compact, val, cmp, lifting_cache, idx, 𝕃ₒ)
lift_comparison_leaves!(egal_tfunc, compact, val, cmp, idx, 𝕃ₒ)
end

function lift_comparison!(::typeof(isa), compact::IncrementalCompact,
idx::Int, stmt::Expr, lifting_cache::IdDict{Pair{AnySSAValue, Any}, AnySSAValue},
𝕃ₒ::AbstractLattice)
idx::Int, stmt::Expr, 𝕃ₒ::AbstractLattice)
args = stmt.args
length(args) == 3 || return
cmp = argextype(args[3], compact)
val = args[2]
lift_comparison_leaves!(isa_tfunc, compact, val, cmp, lifting_cache, idx, 𝕃ₒ)
lift_comparison_leaves!(isa_tfunc, compact, val, cmp, idx, 𝕃ₒ)
end

function lift_comparison!(::typeof(isdefined), compact::IncrementalCompact,
idx::Int, stmt::Expr, lifting_cache::IdDict{Pair{AnySSAValue, Any}, AnySSAValue},
𝕃ₒ::AbstractLattice)
idx::Int, stmt::Expr, 𝕃ₒ::AbstractLattice)
args = stmt.args
length(args) == 3 || return
cmp = argextype(args[3], compact)
isa(cmp, Const) || return # `isdefined_tfunc` won't return Const
val = args[2]
lift_comparison_leaves!(isdefined_tfunc, compact, val, cmp, lifting_cache, idx, 𝕃ₒ)
lift_comparison_leaves!(isdefined_tfunc, compact, val, cmp, idx, 𝕃ₒ)
end

function phi_or_ifelse_predecessors(@nospecialize(def), compact::IncrementalCompact)
Expand All @@ -570,15 +567,13 @@ end

function lift_comparison_leaves!(@specialize(tfunc),
compact::IncrementalCompact, @nospecialize(val), @nospecialize(cmp),
lifting_cache::IdDict{Pair{AnySSAValue, Any}, AnySSAValue}, idx::Int,
𝕃ₒ::AbstractLattice)
idx::Int, 𝕃ₒ::AbstractLattice)
typeconstraint = widenconst(argextype(val, compact))
if isa(val, Union{OldSSAValue, SSAValue})
val, typeconstraint = simple_walk_constraint(compact, val, typeconstraint)
end
isa(typeconstraint, Union) || return # bail out if there won't be a good chance for lifting


leaves, visited_philikes = collect_leaves(compact, val, typeconstraint, 𝕃ₒ, phi_or_ifelse_predecessors)
length(leaves) ≀ 1 && return # bail out if we don't have multiple leaves

Expand All @@ -599,8 +594,7 @@ function lift_comparison_leaves!(@specialize(tfunc),

# perform lifting
lifted_val = perform_lifting!(compact,
visited_philikes, cmp, lifting_cache, Bool,
lifted_leaves::LiftedLeaves, val, nothing)::LiftedValue
visited_philikes, cmp, Bool, lifted_leaves::LiftedLeaves, val, nothing)::LiftedValue

compact[idx] = lifted_val.val
end
Expand Down Expand Up @@ -655,7 +649,6 @@ end

function perform_lifting!(compact::IncrementalCompact,
visited_philikes::Vector{AnySSAValue}, @nospecialize(cache_key),
lifting_cache::IdDict{Pair{AnySSAValue, Any}, AnySSAValue},
@nospecialize(result_t), lifted_leaves::Union{LiftedLeaves, LiftedDefs}, @nospecialize(stmt_val),
lazydomtree::Union{LazyDomtree,Nothing})
reverse_mapping = IdDict{AnySSAValue, Int}()
Expand Down Expand Up @@ -704,19 +697,6 @@ function perform_lifting!(compact::IncrementalCompact,
old_ssa = visited_philikes[i]
old_inst = compact[old_ssa]
old_node = old_inst[:stmt]::Union{PhiNode,Expr}
# FIXME this cache is broken somehow
# ckey = Pair{AnySSAValue, Any}(old_ssa, cache_key)
# cached = ckey in keys(lifting_cache)
cached = false
if cached
ssa = lifting_cache[ckey]
if isa(old_node, PhiNode)
lifted_philikes[i] = LiftedPhilike(ssa, old_node, false)
else
lifted_philikes[i] = LiftedPhilike(ssa, IfElseCall(old_node), false)
end
continue
end
if isa(old_node, PhiNode)
new_node = PhiNode()
ssa = insert_node!(compact, old_ssa, effect_free_and_nothrow(NewInstruction(new_node, result_t)))
Expand All @@ -734,7 +714,6 @@ function perform_lifting!(compact::IncrementalCompact,
ssa = insert_node!(compact, old_ssa, new_inst, #= attach_after =# true)
lifted_philikes[i] = LiftedPhilike(ssa, IfElseCall(new_node), true)
end
# lifting_cache[ckey] = ssa
end

# Fix up arguments
Expand Down Expand Up @@ -1018,8 +997,6 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
𝕃ₒ = inlining === nothing ? SimpleInferenceLattice.instance : optimizer_lattice(inlining.interp)
compact = IncrementalCompact(ir)
defuses = nothing # will be initialized once we encounter mutability in order to reduce dynamic allocations
lifting_cache = IdDict{Pair{AnySSAValue, Any}, AnySSAValue}()
def_lifting_cache = IdDict{Pair{AnySSAValue, Any}, AnySSAValue}()
# initialization of domtree is delayed to avoid the expensive computation in many cases
lazydomtree = LazyDomtree(ir)
for ((_, idx), stmt) in compact
Expand Down Expand Up @@ -1110,9 +1087,9 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
elseif is_known_call(stmt, Core._svec_ref, compact)
lift_svec_ref!(compact, idx, stmt)
elseif is_known_call(stmt, (===), compact)
lift_comparison!(===, compact, idx, stmt, lifting_cache, 𝕃ₒ)
lift_comparison!(===, compact, idx, stmt, 𝕃ₒ)
elseif is_known_call(stmt, isa, compact)
lift_comparison!(isa, compact, idx, stmt, lifting_cache, 𝕃ₒ)
lift_comparison!(isa, compact, idx, stmt, 𝕃ₒ)
elseif is_known_call(stmt, Core.ifelse, compact)
fold_ifelse!(compact, idx, stmt)
elseif isexpr(stmt, :new)
Expand All @@ -1131,7 +1108,7 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
struct_argtyp = argument_datatype(struct_typ)
if struct_argtyp === nothing
if isa(struct_typ, Union) && is_isdefined
lift_comparison!(isdefined, compact, idx, stmt, lifting_cache, 𝕃ₒ)
lift_comparison!(isdefined, compact, idx, stmt, 𝕃ₒ)
end
continue
end
Expand Down Expand Up @@ -1191,7 +1168,7 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
end

lifted_val = perform_lifting!(compact,
visited_philikes, field, lifting_cache, result_t, lifted_leaves, val, lazydomtree)
visited_philikes, field, result_t, lifted_leaves, val, lazydomtree)

# Insert the undef check if necessary
if any_undef
Expand All @@ -1203,7 +1180,7 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
lifted_leaves_def[k] = v === nothing ? false : true
end
def_val = perform_lifting!(compact,
visited_philikes, field, def_lifting_cache, Bool, lifted_leaves_def, val, lazydomtree).val
visited_philikes, field, Bool, lifted_leaves_def, val, lazydomtree).val
end
insert_node!(compact, SSAValue(idx), NewInstruction(
Expr(:throw_undef_if_not, Symbol("##getfield##"), def_val), Nothing))
Expand Down

0 comments on commit ea49abe

Please sign in to comment.