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

WIP: Add a flag to trace inference's heuristic limiting decisions #31000

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ function abstract_call_method_with_const_args(@nospecialize(rettype), @nospecial
return result
end

struct EdgeCycleLimited
newsig
sig
end

function abstract_call_method(method::Method, @nospecialize(sig), sparams::SimpleVector, sv::InferenceState)
if method.name === :depwarn && isdefined(Main, :Base) && method.module === Main.Base
return Any, false, nothing
Expand Down Expand Up @@ -301,6 +306,10 @@ function abstract_call_method(method::Method, @nospecialize(sig), sparams::Simpl
newsig = limit_type_size(sig, comparison, sv.linfo.specTypes, sv.params.TUPLE_COMPLEXITY_LIMIT_DEPTH, spec_len)

if newsig !== sig
if edgecycle && sv.params.trace_inference_limits
push!(sv.params.trace_buffer, EdgeCycleLimited(newsig, sig))
end

# continue inference, but note that we've limited parameter complexity
# on this call (to ensure convergence), so that we don't cache this result
if call_result_unused(sv)
Expand Down
11 changes: 8 additions & 3 deletions base/compiler/params.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ struct Params
# contains more than this many elements
MAX_TUPLE_SPLAT::Int

# Whether to trace when inference decides to truncate inference
trace_inference_limits::Bool
trace_buffer::Vector{Any}

# reasonable defaults
global function CustomParams(world::UInt,
;
Expand All @@ -47,13 +51,14 @@ struct Params
tupletype_depth::Int = DEFAULT_PARAMS.TUPLE_COMPLEXITY_LIMIT_DEPTH,
tuple_splat::Int = DEFAULT_PARAMS.MAX_TUPLE_SPLAT,
union_splitting::Int = DEFAULT_PARAMS.MAX_UNION_SPLITTING,
apply_union_enum::Int = DEFAULT_PARAMS.MAX_APPLY_UNION_ENUM)
apply_union_enum::Int = DEFAULT_PARAMS.MAX_APPLY_UNION_ENUM,
trace_inference_limits::Bool = false)
return new(Vector{InferenceResult}(),
world, false,
inlining, ipo_constant_propagation, aggressive_constant_propagation,
inline_cost_threshold, inline_nonleaf_penalty, inline_tupleret_bonus,
max_methods, union_splitting, apply_union_enum, tupletype_depth,
tuple_splat)
tuple_splat, trace_inference_limits, Any[])
end
function Params(world::UInt)
inlining = inlining_enabled()
Expand All @@ -64,7 +69,7 @@ struct Params
#=inline_tupleret_bonus, max_methods, union_splitting, apply_union_enum=#
400, 4, 4, 8,
#=tupletype_depth, tuple_splat=#
3, 32)
3, 32, false, Any[])
end
end
const DEFAULT_PARAMS = Params(UInt(0))
8 changes: 7 additions & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,8 @@ possible options are `:source` or `:none`.
function code_typed(@nospecialize(f), @nospecialize(types=Tuple);
optimize=true, debuginfo::Symbol=:default,
world = ccall(:jl_get_world_counter, UInt, ()),
params = Core.Compiler.Params(world))
trace = false,
params = Core.Compiler.CustomParams(world; trace_inference_limits=trace))
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
if isa(f, Core.Builtin)
throw(ArgumentError("argument is not a generic function"))
Expand All @@ -984,6 +985,11 @@ function code_typed(@nospecialize(f), @nospecialize(types=Tuple);
debuginfo == :none && remove_linenums!(code)
push!(asts, code => ty)
end
if trace && !isempty(params.trace_buffer)
for entry in params.trace_buffer
@info entry
Copy link
Member

Choose a reason for hiding this comment

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

code_typed shouldn't print—and this seems more like a code_warntype feature anyways?

Copy link
Member

Choose a reason for hiding this comment

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

We could separate this functionality out and expect users to pass in the right CustomParams or alternatively a separate macro @explain/ @WHY?!! that does this.

end
end
return asts
end

Expand Down
9 changes: 9 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,15 @@ function show(io::IO, src::CodeInfo; debuginfo::Symbol=:source)
print(io, ")")
end

# Show for inference limit trace objects
function show(io::IO, ecl::Core.Compiler.EdgeCycleLimited)
Copy link
Member

Choose a reason for hiding this comment

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

We don't typically like Base to have any references to Core.Compiler. Perhaps this should go into the IRShow module?

print(io, "Signature ")
show_tuple_as_call(io, Symbol("<unknown>"), ecl.sig)
print(io, " was narrowed to ")
show_tuple_as_call(io, Symbol("<unknown>"), ecl.newsig)
println(io, " due to recursion [EdgeCycleLimited]")
end


function dump(io::IOContext, x::SimpleVector, n::Int, indent)
if isempty(x)
Expand Down