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

allow constant propagation to break call cycles in inference #36058

Merged
merged 1 commit into from
May 29, 2020
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
8 changes: 5 additions & 3 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
end
# try constant propagation if only 1 method is inferred to non-Bottom
# this is in preparation for inlining, or improving the return result
if nonbot > 0 && seen == napplicable && !edgecycle && isa(rettype, Type) && InferenceParams(interp).ipo_constant_propagation
is_unused = call_result_unused(sv)
if nonbot > 0 && seen == napplicable && (!edgecycle || !is_unused) && isa(rettype, Type) && InferenceParams(interp).ipo_constant_propagation
# if there's a possibility we could constant-propagate a better result
# (hopefully without doing too much work), try to do that now
# TODO: it feels like this could be better integrated into abstract_call_method / typeinf_edge
Expand All @@ -139,7 +140,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
rettype = const_rettype
end
end
if call_result_unused(sv) && !(rettype === Bottom)
if is_unused && !(rettype === Bottom)
# We're mainly only here because the optimizer might want this code,
# but we ourselves locally don't typically care about it locally
# (beyond checking if it always throws).
Expand Down Expand Up @@ -268,7 +269,8 @@ function abstract_call_method_with_const_args(interp::AbstractInterpreter, @nosp
typeinf(interp, frame) || return Any
end
result = inf_result.result
isa(result, InferenceState) && return Any # TODO: unexpected, is this recursive constant inference?
# if constant inference hits a cycle, just bail out
isa(result, InferenceState) && return Any
add_backedge!(inf_result.linfo, sv)
return result
end
Expand Down
22 changes: 22 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2601,3 +2601,25 @@ _use_unstable_kw_2() = _unstable_kw(x = 2, y = rand())
end
_construct_structwithsplatnew() = StructWithSplatNew(("",))
@test Base.return_types(_construct_structwithsplatnew) == Any[StructWithSplatNew]

# case where a call cycle can be broken by constant propagation
struct NotQRSparse
x::Matrix{Float64}
n::Int
end
@inline function getprop(F::NotQRSparse, d::Symbol)
if d === :Q
return NotQRSparse(getprop(F, :B), _size_ish(F, 2))
elseif d === :A
return Dict()
elseif d === :B
return rand(2,2)
elseif d === :C
return ""
else
error()
end
end
_size_ish(F::NotQRSparse, i::Integer) = size(getprop(F, :B), 1)
_call_size_ish(x) = _size_ish(x,1)
@test Base.return_types(_call_size_ish, (NotQRSparse,)) == Any[Int]