Skip to content

Commit

Permalink
Update docs and print more friendly signature
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Aug 17, 2024
1 parent 838267f commit 95e20bd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
31 changes: 28 additions & 3 deletions docs/src/optanalysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,37 @@ julia> using Cthulhu
julia> ascend(rpts[1])
Choose a call for analysis (q to quit):
> sumup(::typeof(sin))
runtime dispatch to make_vals(%1::Any)::Any
> sumup(::typeof(sin))
Open an editor at a possible caller of
Tuple{typeof(make_vals), Any}
or browse typed code:
> "REPL[7]", sumup: lines [4]
Browse typed code
```

`ascend` will show the full call-chain to reach a particlar runtime dispatch; in this case, it was our entry point, but in other cases it may be deeper in the call graph.
`ascend` will show the full call-chain to reach a particular runtime dispatch; in this case, it was our entry point, but in other cases it may be deeper in the call graph. In this case, we've interactively moved the selector `>` down to the `sumup` call (you cannot descend into the `"runtime dispatch to..."` as there is no known code associated with it) and hit `<Enter>`, at which point Cthulhu showed us that the call to `make_vals(::Any)` occured only on line 4 of the definition of `sumup` (which we entered at the REPL). Cthulhu is now prompting us to either open the code in an editor (which will fail in this case, since there is no associated file!) or view the type-annoted code. If we select the "Browse typed code" option we see

Because Cthulhu is an interactive terminal program, we can't demonstrate it in this page, but you're encouraged to see Cthulhu's documentation which includes a video tutorial.
```
sumup(f) @ Main REPL[7]:1
1 function sumup(f::Core.Const(sin))::Any
2 # this function uses the non-constant global variable `n` here
3 # and it makes every succeeding operations type-unstable
4 vals::Any = make_vals(n::Any)::Any
5 s::Any = zero(eltype(vals::Any)::Any)::Any
6 for v::Any in vals::Any::Any
7 (s::Any += f::Core.Const(sin)(v::Any)::Any)::Any
8 end
9 return s::Any
10 end
Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark.
```

with red highlighting to indicate the non-inferable arguments.

For more information, you're encouraged to read Cthulhu's documentation, which includes a video tutorial better-suited to this interactive tool.

## [Entry Points](@id optanalysis-entry)

Expand Down
7 changes: 4 additions & 3 deletions ext/JETCthulhuExt.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module JETCthulhuExt

using JET: JET, InferenceErrorReport, VirtualFrame
using JET: JET, InferenceErrorReport, VirtualFrame, PrintConfig, print_signature
using Cthulhu: Cthulhu, Node, Data, callstring
using Core: MethodInstance

Expand All @@ -13,8 +13,9 @@ end
function Cthulhu.treelist(r::InferenceErrorReport)
io = IOBuffer()
cf = CallFrames(r.vst)
printstyled(IOContext(io, :color=>true), r.sig.tt, color=:red)
Cthulhu.treelist!(Node(Data{Union{MethodInstance,Type}}("runtime call to " * String(take!(io)), r.sig.tt)), io, cf, "", Base.IdSet{Union{MethodInstance,Nothing}}([nothing]))
print_signature(IOContext(io, :color=>true), r.sig, PrintConfig())
# printstyled(IOContext(io, :color=>true), r.sig.tt, color=:red)
Cthulhu.treelist!(Node(Data{Union{MethodInstance,Type}}("runtime dispatch to " * String(take!(io)), r.sig.tt)), io, cf, "", Base.IdSet{Union{MethodInstance,Nothing}}([nothing]))
end

Cthulhu.instance(cf::CallFrames) = isempty(cf.frames) ? nothing : cf.frames[end].linfo
Expand Down

0 comments on commit 95e20bd

Please sign in to comment.