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 a Tuple{typeof(f),...} as root of ascend tree #588

Merged
merged 3 commits into from
Aug 17, 2024
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Cthulhu"
uuid = "f68482b8-f384-11e8-15f7-abe071a5a75f"
authors = ["Valentin Churavy <[email protected]> and contributors"]
version = "2.13.0"
version = "2.14.0"

[deps]
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
Expand Down
3 changes: 3 additions & 0 deletions src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,9 @@ function ascend(term, mi; interp::AbstractInterpreter=NativeInterpreter(), kwarg
end
end
end
if !isa(mi, MethodInstance)
error("You can only descend into known calls. If you tried to descend into a runtime-dispatched signature, try its caller instead.")
end
# The main application of `ascend` is finding cases of non-inferrability, so the
# warn highlighting is useful.
interp′ = CthulhuInterpreter(interp)
Expand Down
4 changes: 3 additions & 1 deletion src/backedges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ specTypes(mi::MethodInstance) = mi.specTypes
instance(mi::MethodInstance) = mi
nextnode(mi, edge) = edge

instance(@nospecialize(tt::Type)) = tt

instance(sfs::Vector{StackTraces.StackFrame}) = isempty(sfs) ? CC.Timings.ROOTmi : sfs[end].linfo::MethodInstance # we checked this type condition within `buildframes`
method(sfs::Vector{StackTraces.StackFrame}) = method(instance(sfs))
backedges(sframes::Vector{StackTraces.StackFrame}) = (ret = sframes[2:end]; isempty(ret) ? () : (ret,))
Expand Down Expand Up @@ -188,7 +190,7 @@ function treelist!(parent::Node, io::IO, mi, indent::AbstractString, visited::Ba
indent *= " "
for edge in backedges(mi)
str = indent * callstring(io, edge)
child = Node(Data(str, instance(edge)), parent)
child = Node(typeof(parent.data)(str, instance(edge)), parent)
treelist!(child, io, nextnode(mi, edge), indent, visited)
end
return parent
Expand Down
16 changes: 16 additions & 0 deletions src/callsite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ end

# maybe_callsite for higher-level inputs
maybe_callsite(cs::Callsite, callee::MethodInstance) = maybe_callsite(cs.info, callee)
maybe_callsite(cs::Callsite, @nospecialize(tt::Type)) = maybe_callsite(cs.info, tt)
maybe_callsite(info::CallInfo, callee::MethodInstance) = maybe_callsite(get_mi(info), callee)
# Special CallInfo cases:
function maybe_callsite(info::MultiCallInfo, callee::MethodInstance)
Expand All @@ -517,4 +518,19 @@ end
maybe_callsite(info::PureCallInfo, mi::MethodInstance) = mi.specTypes <: Tuple{mapany(Core.Typeof ∘ unwrapconst, info.argtypes)...}
maybe_callsite(info::RTCallInfo, mi::MethodInstance) = false

function maybe_callsite(info::RTCallInfo, @nospecialize(tt::Type))
isa(tt, Union) && return maybe_callsite(info, tt.a) || maybe_callsite(info, tt.b)
isa(tt, DataType) || return false
typeof(info.f) === tt.parameters[1] || return false
for (a, b) in zip(info.argtyps, tt.parameters[2:end])
a === b || return false
Comment on lines +525 to +526
Copy link
Member

Choose a reason for hiding this comment

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

When iterating over a zip created from a Vector{Any}, objects of type Tuple{<:Any,<:Any} are created, which can sometimes lead to unnecessary specializations, especially when the vector contains Type objects.
Since we're only using ===, it shouldn't be a problem for this case, but it's something to be mindful of when working on Core.Compiler and other external abstract interpreters in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I figured this case would be OK. I was leaning on the "self check" tests too, I admit!

end
return true
end
function maybe_callsite(info::MICallInfo, @nospecialize(tt::Type))
return tt <: info.mi.specTypes
end
timholy marked this conversation as resolved.
Show resolved Hide resolved

maybe_callsite(info::CallInfo, @nospecialize(tt::Type)) = false

unwrapconst(@nospecialize(arg)) = arg isa Core.Const ? arg.val : arg
2 changes: 1 addition & 1 deletion src/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function callinfo(sig, rt, max_methods=-1; world=get_world_counter())
return MultiCallInfo(sig, rt, callinfos)
end

function find_caller_of(interp::AbstractInterpreter, callee::MethodInstance, caller::MethodInstance; allow_unspecialized::Bool=false)
function find_caller_of(interp::AbstractInterpreter, callee::Union{MethodInstance,Type}, caller::MethodInstance; allow_unspecialized::Bool=false)
interp′ = CthulhuInterpreter(interp)
do_typeinf!(interp′, caller)
locs = Tuple{Core.LineInfoNode,Int}[]
Expand Down
Loading