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

inference: prioritize force_constant_prop over const_prop_entry_heuristic #41882

Merged
merged 1 commit into from
Aug 24, 2021
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
19 changes: 13 additions & 6 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -567,16 +567,21 @@ end
function maybe_get_const_prop_profitable(interp::AbstractInterpreter, result::MethodCallResult,
@nospecialize(f), argtypes::Vector{Any}, match::MethodMatch,
sv::InferenceState)
const_prop_entry_heuristic(interp, result, sv) || return nothing
if !InferenceParams(interp).ipo_constant_propagation
add_remark!(interp, sv, "[constprop] Disabled by parameter")
return nothing
end
method = match.method
force = force_const_prop(interp, f, method)
force || const_prop_entry_heuristic(interp, result, sv) || return nothing
nargs::Int = method.nargs
method.isva && (nargs -= 1)
if length(argtypes) < nargs
length(argtypes) < nargs && return nothing
if !(const_prop_argument_heuristic(interp, argtypes) || const_prop_rettype_heuristic(interp, result.rt))
add_remark!(interp, sv, "[constprop] Disabled by argument and rettype heuristics")
return nothing
end
const_prop_argument_heuristic(interp, argtypes) || const_prop_rettype_heuristic(interp, result.rt) || return nothing
allconst = is_allconst(argtypes)
force = force_const_prop(interp, f, method)
if !force
if !const_prop_function_heuristic(interp, f, argtypes, nargs, allconst)
add_remark!(interp, sv, "[constprop] Disabled by function heuristic")
Expand All @@ -599,10 +604,12 @@ end

function const_prop_entry_heuristic(interp::AbstractInterpreter, result::MethodCallResult, sv::InferenceState)
if call_result_unused(sv) && result.edgecycle
add_remark!(interp, sv, "[constprop] Edgecycle with unused result")
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (edgecycle with unused result)")
return false
end
return is_improvable(result.rt) && InferenceParams(interp).ipo_constant_propagation
is_improvable(result.rt) && return true
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (unimprovable return type)")
return false
end

# see if propagating constants may be worthwhile
Expand Down
23 changes: 23 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,29 @@ end
end
end

# force constant-prop' for `setproperty!`
let m = Module()
ci = @eval m begin
# if we don't force constant-prop', `T = fieldtype(Foo, ::Symbol)` will be union-split to
# `Union{Type{Any},Type{Int}` and it will make `convert(T, nothing)` too costly
# and it leads to inlining failure
mutable struct Foo
val
_::Int
end

function setter(xs)
for x in xs
x.val = nothing
end
end

$code_typed1(setter, (Vector{Foo},))
end

@test !any(x->isinvoke(x, :setproperty!), ci.code)
end

# Issue #41299 - inlining deletes error check in :>
g41299(f::Tf, args::Vararg{Any,N}) where {Tf,N} = f(args...)
@test_throws TypeError g41299(>:, 1, 2)