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

Extend ifelse lifting to regular SROA #50403

Merged
merged 2 commits into from
Jul 4, 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
3 changes: 3 additions & 0 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,9 @@ function insert_node!(compact::IncrementalCompact, @nospecialize(before), newins
return os
end
elseif isa(before, NewSSAValue)
# As above, new_new_nodes must get counted. We don't visit them during our compact,
# so they're immediately considered reified.
count_added_node!(compact, newinst.stmt)
# TODO: This is incorrect and does not maintain ordering among the new nodes
before_entry = compact.new_new_nodes.info[-before.id]
newline = something(newinst.line, compact.new_new_nodes.stmts[-before.id][:line])
Expand Down
17 changes: 9 additions & 8 deletions base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ function lift_comparison!(::typeof(isdefined), compact::IncrementalCompact,
lift_comparison_leaves!(isdefined_tfunc, compact, val, cmp, lifting_cache, idx, 𝕃ₒ)
end

function phi_or_ifelse_predecessors(@nospecialize(def), compact::IncrementalCompact)
isa(def, PhiNode) && return def.values
is_known_call(def, Core.ifelse, compact) && return def.args[3:4]
return nothing
end

function lift_comparison_leaves!(@specialize(tfunc),
compact::IncrementalCompact, @nospecialize(val), @nospecialize(cmp),
lifting_cache::IdDict{Pair{AnySSAValue, Any}, AnySSAValue}, idx::Int,
Expand All @@ -573,12 +579,8 @@ function lift_comparison_leaves!(@specialize(tfunc),
end
isa(typeconstraint, Union) || return # bail out if there won't be a good chance for lifting

predecessors = function (@nospecialize(def), compact::IncrementalCompact)
isa(def, PhiNode) && return def.values
is_known_call(def, Core.ifelse, compact) && return def.args[3:4]
return nothing
end
leaves, visited_philikes = collect_leaves(compact, val, typeconstraint, 𝕃ₒ, predecessors)

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

# check if we can evaluate the comparison for each one of the leaves
Expand Down Expand Up @@ -1093,11 +1095,10 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
end

# perform SROA on immutable structs here on

field = try_compute_fieldidx_stmt(compact, stmt, struct_typ)
field === nothing && continue

leaves, visited_philikes = collect_leaves(compact, val, struct_typ, 𝕃ₒ)
leaves, visited_philikes = collect_leaves(compact, val, struct_typ, 𝕃ₒ, phi_or_ifelse_predecessors)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now all callsites of collect_leaves use phi_or_ifelse_predecessors, so I think we can remove this argument entirely.

isempty(leaves) && continue

lifted_result = lift_leaves(compact, field, leaves, 𝕃ₒ)
Expand Down