Skip to content

Fix ascend, split out find_caller_of and test it #136

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

Merged
merged 1 commit into from
Mar 12, 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
41 changes: 15 additions & 26 deletions src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ function _descend(interp::CthulhuInterpreter, mi::MethodInstance; override::Unio
end
elseif toggle === :edit
edit(whereis(mi.def)...)
display_CI = false
else
#Handle Standard alternative view, e.g. :native, :llvm
view_cmd = get(codeviews, toggle, nothing)
Expand All @@ -349,11 +350,16 @@ function do_typeinf!(interp, mi)
return nothing
end

function mkinterp(@nospecialize(F), @nospecialize(TT))
interp = CthulhuInterpreter()
function get_specialization(@nospecialize(F), @nospecialize(TT))
sigt = Base.signature_type(F, TT)
match = Base._which(sigt)
mi = Core.Compiler.specialize_method(match)
return mi
end

function mkinterp(@nospecialize(F), @nospecialize(TT))
interp = CthulhuInterpreter()
mi = get_specialization(F, TT)
do_typeinf!(interp, mi)
(interp, mi)
end
Expand Down Expand Up @@ -385,36 +391,17 @@ function ascend(mi; kwargs...)
if !isroot(node)
# Help user find the sites calling the parent
miparent = instance(node.parent.data.nd)
params = current_params()
locs = []
for optimize in (true, false)
(CI, rt, slottypes) = do_typeinf_slottypes(mi, optimize, params)
preprocess_ci!(CI, mi, optimize, CONFIG)
callsites = find_callsites(CI, mi, slottypes; params=params)
callsites = filter(cs->is_callsite(cs, miparent), callsites)
append!(locs, CI.linetable[CI.codelocs[(cs->cs.id).(callsites)]])
end
if !isempty(locs)
ulocs = Dict{Tuple{Symbol,Symbol},Vector{Int}}()
for loc in locs
lines = get!(Vector{Int}, ulocs, (loc.method, loc.file))
line = loc.line
if line ∉ lines
push!(lines, line)
end
end
vlocs = collect(ulocs)
strlocs = [string('"', k[2], "\", ", k[1], ": lines ", v) for (k, v) in ulocs]
perm = sortperm(strlocs)
strlocs, vlocs = strlocs[perm], vlocs[perm]
ulocs = find_caller_of(miparent, mi)
if !isempty(ulocs)
strlocs = [string(" "^k[3] * '"', k[2], "\", ", k[1], ": lines ", v) for (k, v) in ulocs]
push!(strlocs, "Browse typed code")
linemenu = TerminalMenus.RadioMenu(strlocs)
browsecodetyped = false
choice2 = 1
while choice2 != -1
choice2 = TerminalMenus.request("\nChoose caller of $miparent or proceed to typed code:", linemenu; cursor=choice2)
if 0 < choice2 < length(strlocs)
loc = vlocs[choice2]
loc = ulocs[choice2]
edit(String(loc[1][2]), first(loc[2]))
elseif choice2 == length(strlocs)
browsecodetyped = true
Expand All @@ -425,7 +412,9 @@ function ascend(mi; kwargs...)
end
# The main application of `ascend` is finding cases of non-inferrability, so the
# warn highlighting is useful.
browsecodetyped && _descend(mi; iswarn=true, optimize=false, interruptexc=false, kwargs...)
interp = CthulhuInterpreter()
do_typeinf!(interp, mi)
browsecodetyped && _descend(interp, mi; iswarn=true, optimize=false, interruptexc=false, kwargs...)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/backedges.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A lightly-modified version of the same function in Base
# Highlights argument types with color specified by highlighter(typ)
function show_tuple_as_call(@nospecialize(highlighter), io::IO, name::Symbol, sig::Type, demangle=false, kwargs=nothing)
function show_tuple_as_call(@nospecialize(highlighter), io::IO, name::Symbol, @nospecialize(sig::Type), demangle=false, kwargs=nothing)
if sig === Tuple
print(io, demangle ? Base.demangle_function_name(name) : name, "(...)")
return
Expand Down
1 change: 1 addition & 0 deletions src/callsite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ end

is_callsite(cs::Callsite, mi::MethodInstance) = is_callsite(cs.info, mi)
is_callsite(info::MICallInfo, mi::MethodInstance) = info.mi == mi
is_callsite(info::ConstPropCallInfo, mi::MethodInstance) = is_callsite(info.mi, mi)
is_callsite(info::DeoptimizedCallInfo, mi::MethodInstance) = is_callsite(info.accurate, mi)
is_callsite(info::TaskCallInfo, mi::MethodInstance) = is_callsite(info.ci, mi)
is_callsite(info::ReturnTypeCallInfo, mi::MethodInstance) = is_callsite(info.called_mi, mi)
Expand Down
46 changes: 46 additions & 0 deletions src/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,49 @@ function callinfo(sig, rt, max=-1; params=current_params())
length(callinfos) == 1 && return first(callinfos)
return MultiCallInfo(sig, rt, callinfos)
end

function find_caller_of(callee::MethodInstance, caller::MethodInstance)
interp = CthulhuInterpreter()
do_typeinf!(interp, caller)
params = current_params()
locs = Tuple{Core.LineInfoNode,Int}[]
for optimize in (true, false)
(CI, rt, infos, slottypes) = lookup(interp, caller, optimize)
preprocess_ci!(CI, caller, optimize, CONFIG)
callsites = find_callsites(CI, infos, caller, slottypes; params=params)
callsites = filter(cs->is_callsite(cs, callee), callsites)
foreach(cs -> add_sourceline!(locs, CI, cs.id), callsites)
end
# Consolidate by method, but preserve the order
prlookup = Dict{Tuple{Symbol,Symbol},Int}()
ulocs = Pair{Tuple{Symbol,Symbol,Int},Vector{Int}}[]
if !isempty(locs)
for (loc, depth) in locs
idx = get(prlookup, (loc.method, loc.file), nothing)
if idx === nothing
push!(ulocs, (loc.method, loc.file, depth) => Int[])
prlookup[(loc.method, loc.file)] = idx = length(ulocs)
end
lines = ulocs[idx][2]
line = loc.line
if line ∉ lines
push!(lines, line)
end
end
end
return ulocs
end

function add_sourceline!(locs, CI, stmtidx::Int)
if isa(CI, IRCode)
stack = Base.IRShow.compute_loc_stack(CI.linetable, CI.stmts.line[stmtidx])
for (i, idx) in enumerate(stack)
line = CI.linetable[idx]
line.line == 0 && continue
push!(locs, (CI.linetable[idx], i-1))
end
else
push!(locs, (CI.linetable[CI.codelocs[stmtidx]], 0))
end
return locs
end
40 changes: 40 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,46 @@ fst5(x) = fst4(x)
@test match(r" fst4 at .*:\d+ => fst5\(::Float64\) at .*:\d+", child.data.callstr) !== nothing
end

@testset "ascend" begin
# This tests only the non-interactive "look up the caller" portion
callee(x) = 2x
function caller(x)
val = 0.0
val += callee(3); line1 = @__LINE__
val += callee(3.0); line2 = @__LINE__
val += callee(x); line3 = @__LINE__
val = sum([val]) # FIXME: without this line, `lookup` fails because codeinst.inferred === nothing
return val, line1, line2, line3
end
_, line1, line2, line3 = caller(7)
micaller = Cthulhu.get_specialization(caller, Tuple{Int})
micallee_Int = Cthulhu.get_specialization(callee, Tuple{Int})
micallee_Float4 = Cthulhu.get_specialization(callee, Tuple{Float64})
info, lines = only(Cthulhu.find_caller_of(micallee_Int, micaller))
@test info == (:caller, Symbol(@__FILE__), 0) && lines == [line1, line3]
info, lines = only(Cthulhu.find_caller_of(micallee_Float4, micaller))
@test info == (:caller, Symbol(@__FILE__), 0) && lines == [line2]

# Detection in optimized (post-inlining) code
@noinline nicallee(x) = 2x
midcaller(x) = nicallee(x), @__LINE__
function outercaller(x)
val, line2 = midcaller(x); line1 = @__LINE__
val = sum([val]) # FIXME: without this line, `lookup` fails because codeinst.inferred === nothing
return val, line1, line2
end
_, line1, line2 = outercaller(7)
micaller = Cthulhu.get_specialization(outercaller, Tuple{Int})
micallee = Cthulhu.get_specialization(nicallee, Tuple{Int})
callerinfo = Cthulhu.find_caller_of(micallee, micaller)
@test length(callerinfo) == 2
info, lines = callerinfo[1]
@test info == (:outercaller, Symbol(@__FILE__), 0)
@test lines == [line1]
info, lines = callerinfo[2]
@test info == (:midcaller, Symbol(@__FILE__), 1)
@test lines == [line2]
end

##
# Cthulhu config test
Expand Down