Skip to content

Commit

Permalink
adjust the name and remarks of const prop heuristics (#55260)
Browse files Browse the repository at this point in the history
`const_prop_entry_heuristics` currently checks the return type only, so
I have given it a name that reflects this and adjusted the remarks
accordingly. There are no changes to the basic behavior of inference.
  • Loading branch information
aviatesk authored Jul 29, 2024
1 parent 5da9468 commit 4dfce5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
27 changes: 14 additions & 13 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1034,12 +1034,12 @@ function maybe_get_const_prop_profitable(interp::AbstractInterpreter,
match::MethodMatch, sv::AbsIntState)
method = match.method
force = force_const_prop(interp, f, method)
if !const_prop_entry_heuristic(interp, result, si, sv, force)
# N.B. remarks are emitted within `const_prop_entry_heuristic`
if !const_prop_rettype_heuristic(interp, result, si, sv, force)
# N.B. remarks are emitted within `const_prop_rettype_heuristic`
return nothing
end
if !const_prop_argument_heuristic(interp, arginfo, sv)
add_remark!(interp, sv, "[constprop] Disabled by argument and rettype heuristics")
add_remark!(interp, sv, "[constprop] Disabled by argument heuristics")
return nothing
end
all_overridden = is_all_overridden(interp, arginfo, sv)
Expand All @@ -1061,28 +1061,28 @@ function maybe_get_const_prop_profitable(interp::AbstractInterpreter,
return mi
end

function const_prop_entry_heuristic(interp::AbstractInterpreter, result::MethodCallResult,
si::StmtInfo, sv::AbsIntState, force::Bool)
if result.rt isa LimitedAccuracy
function const_prop_rettype_heuristic(interp::AbstractInterpreter, result::MethodCallResult,
si::StmtInfo, sv::AbsIntState, force::Bool)
rt = result.rt
if rt isa LimitedAccuracy
# optimizations like inlining are disabled for limited frames,
# thus there won't be much benefit in constant-prop' here
# N.B. don't allow forced constprop' for safety (xref #52763)
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (limited accuracy)")
add_remark!(interp, sv, "[constprop] Disabled by rettype heuristic (limited accuracy)")
return false
elseif force
return true
elseif call_result_unused(si) && result.edgecycle
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (edgecycle with unused result)")
add_remark!(interp, sv, "[constprop] Disabled by rettype heuristic (edgecycle with unused result)")
return false
end
# check if this return type is improvable (i.e. whether it's possible that with more
# information, we might get a more precise type)
rt = result.rt
if isa(rt, Type)
# could always be improved to `Const`, `PartialStruct` or just a more precise type,
# unless we're already at `Bottom`
if rt === Bottom
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (erroneous result)")
add_remark!(interp, sv, "[constprop] Disabled by rettype heuristic (erroneous result)")
return false
end
return true
Expand All @@ -1091,14 +1091,15 @@ function const_prop_entry_heuristic(interp::AbstractInterpreter, result::MethodC
return true
elseif isa(rt, Const)
if is_nothrow(result.effects)
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (nothrow const)")
add_remark!(interp, sv, "[constprop] Disabled by rettype heuristic (nothrow const)")
return false
end
# Could still be improved to Bottom (or at least could see the effects improved)
return true
else
add_remark!(interp, sv, "[constprop] Disabled by rettype heuristic (unimprovable result)")
return false
end
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (unimprovable result)")
return false
end

# determines heuristically whether if constant propagation can be worthwhile
Expand Down
8 changes: 4 additions & 4 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5052,13 +5052,13 @@ g() = empty_nt_values(Base.inferencebarrier(Tuple{}))
# to terminate the call.
@newinterp RecurseInterpreter
let CC = Core.Compiler
function CC.const_prop_entry_heuristic(interp::RecurseInterpreter, result::CC.MethodCallResult,
si::CC.StmtInfo, sv::CC.AbsIntState, force::Bool)
function CC.const_prop_rettype_heuristic(interp::RecurseInterpreter, result::CC.MethodCallResult,
si::CC.StmtInfo, sv::CC.AbsIntState, force::Bool)
if result.rt isa CC.LimitedAccuracy
return force # allow forced constprop to recurse into unresolved cycles
end
return @invoke CC.const_prop_entry_heuristic(interp::CC.AbstractInterpreter, result::CC.MethodCallResult,
si::CC.StmtInfo, sv::CC.AbsIntState, force::Bool)
return @invoke CC.const_prop_rettype_heuristic(interp::CC.AbstractInterpreter, result::CC.MethodCallResult,
si::CC.StmtInfo, sv::CC.AbsIntState, force::Bool)
end
end
Base.@constprop :aggressive type_level_recurse1(x...) = x[1] == 2 ? 1 : (length(x) > 100 ? x : type_level_recurse2(x[1] + 1, x..., x...))
Expand Down

0 comments on commit 4dfce5d

Please sign in to comment.