Skip to content

Commit

Permalink
Extend ifelse lifting to regular SROA (JuliaLang#50403)
Browse files Browse the repository at this point in the history
* Extend ifelse lifting to regular SROA

* Fix oracle violation

This is a pre-existing bug, but was exposed by my improvements to
SROA.
  • Loading branch information
Keno authored Jul 4, 2023
1 parent 02272f0 commit 7fc8646
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
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)
isempty(leaves) && continue

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

0 comments on commit 7fc8646

Please sign in to comment.