Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sroa: Better walk for chained KeyValue #52542

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 56 additions & 31 deletions base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ function walk_to_defs(compact::IncrementalCompact, @nospecialize(defssa), @nospe
def = compact[defssa][:stmt]
values = predecessors(def, compact)
if values !== nothing
push!(visited_philikes, defssa)
if isa(def, PhiNode) || length(values) > 1
push!(visited_philikes, defssa)
end
possible_predecessors = Int[]

for n in 1:length(values)
Expand Down Expand Up @@ -857,38 +859,32 @@ function lift_leaves_keyvalue(compact::IncrementalCompact, @nospecialize(key),
for i = 1:length(leaves)
leaf = leaves[i]
cache_key = leaf
while true
if isa(leaf, AnySSAValue)
(def, leaf) = walk_to_def(compact, leaf)
if is_known_invoke_or_call(def, Core.OptimizedGenerics.KeyValue.set, compact)
@assert isexpr(def, :invoke)
if length(def.args) in (5, 6)
collection = def.args[end-2]
set_key = def.args[end-1]
set_val_idx = length(def.args)
elseif length(def.args) == 4
collection = def.args[end-1]
# Key is deleted
# TODO: Model this
return nothing
elseif length(def.args) == 3
collection = def.args[end]
# The whole collection is deleted
# TODO: Model this
return nothing
else
return nothing
end
if set_key === key || (egal_tfunc(𝕃ₒ, argextype(key, compact), argextype(set_key, compact)) == Const(true))
lift_arg!(compact, leaf, cache_key, def, set_val_idx, lifted_leaves)
break
end
leaf = collection
continue
if isa(leaf, AnySSAValue)
(def, leaf) = walk_to_def(compact, leaf)
if is_known_invoke_or_call(def, Core.OptimizedGenerics.KeyValue.set, compact)
@assert isexpr(def, :invoke)
if length(def.args) in (5, 6)
set_key = def.args[end-1]
set_val_idx = length(def.args)
elseif length(def.args) == 4
# Key is deleted
# TODO: Model this
return nothing
elseif length(def.args) == 3
# The whole collection is deleted
# TODO: Model this
return nothing
else
return nothing
end
if set_key === key || (egal_tfunc(𝕃ₒ, argextype(key, compact), argextype(set_key, compact)) == Const(true))
lift_arg!(compact, leaf, cache_key, def, set_val_idx, lifted_leaves)
break
end
continue
end
return nothing
end
return nothing
end
return lifted_leaves
end
Expand All @@ -897,7 +893,36 @@ function lift_keyvalue_get!(compact::IncrementalCompact, idx::Int, stmt::Expr,
collection = stmt.args[end-1]
key = stmt.args[end]

leaves, visited_philikes = collect_leaves(compact, collection, Any, 𝕃ₒ, phi_or_ifelse_predecessors)
function keyvalue_predecessors(@nospecialize(def), compact::IncrementalCompact)
if is_known_invoke_or_call(def, Core.OptimizedGenerics.KeyValue.set, compact)
@assert isexpr(def, :invoke)
if length(def.args) in (5, 6)
collection = def.args[end-2]
set_key = def.args[end-1]
set_val_idx = length(def.args)
elseif length(def.args) == 4
collection = def.args[end-1]
# Key is deleted
# TODO: Model this
return nothing
elseif length(def.args) == 3
collection = def.args[end]
# The whole collection is deleted
# TODO: Model this
return nothing
else
return nothing
end
if set_key === key || (egal_tfunc(𝕃ₒ, argextype(key, compact), argextype(set_key, compact)) == Const(true))
# This is an actual def
return nothing
end
return Any[collection]
end
return phi_or_ifelse_predecessors(def, compact)
end

leaves, visited_philikes = collect_leaves(compact, collection, Any, 𝕃ₒ, keyvalue_predecessors)
isempty(leaves) && return

lifted_leaves = lift_leaves_keyvalue(compact, key, leaves, 𝕃ₒ)
Expand Down
12 changes: 12 additions & 0 deletions test/compiler/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,18 @@ end
@test_broken fully_eliminated(persistent_dict_elim_multiple)
@test code_typed(persistent_dict_elim_multiple)[1][1].code[end] == Core.ReturnNode(1)

function persistent_dict_elim_multiple_phi(c::Bool)
if c
a = Base.PersistentDict(:a => 1)
else
a = Base.PersistentDict(:a => 1)
end
b = Base.PersistentDict(a, :b => 2)
return b[:a]
end
@test_broken fully_eliminated(persistent_dict_elim_multiple_phi)
@test code_typed(persistent_dict_elim_multiple_phi)[1][1].code[end] == Core.ReturnNode(1)

# Test CFG simplify with try/catch blocks
let code = Any[
# Block 1
Expand Down