Skip to content

Commit

Permalink
add option to show inference remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Sep 6, 2021
1 parent 62911a6 commit 3a05262
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
32 changes: 22 additions & 10 deletions src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ end
function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::MethodInstance;
override::Union{Nothing,InferenceResult}=nothing, debuginfo::Union{Symbol,DebugInfo}=DInfo.compact, # default is compact debuginfo
params=current_params(), optimize::Bool=true, interruptexc::Bool=true,
iswarn::Bool=false, hide_type_stable::Union{Nothing,Bool}=nothing, verbose::Union{Nothing,Bool}=nothing, inline_cost::Bool=false)
iswarn::Bool=false, hide_type_stable::Union{Nothing,Bool}=nothing, verbose::Union{Nothing,Bool}=nothing,
remarks::Bool=false, inline_cost::Bool=false)
if isnothing(hide_type_stable)
hide_type_stable = something(verbose, false)
end
Expand Down Expand Up @@ -273,13 +274,16 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
callsites = find_callsites(interp, codeinf, infos, mi, slottypes, optimize; params)

if display_CI
_remarks = remarks ? get(interp.remarks, mi, nothing) : nothing
printstyled(IOContext(term.out_stream::IO, :limit=>true), mi.def, '\n'; bold=true)
if debuginfo == DInfo.compact
# Eliminate trailing indentation (see first item in bullet list in PR #189)
str = let iswarn=iswarn, hide_type_stable=hide_type_stable, inline_cost=inline_cost, mi=mi, debuginfo=debuginfo, codeinf=codeinf, rt=rt
stringify() do io
str = let debuginfo=debuginfo, codeinf=codeinf, rt=rt, mi=mi,
iswarn=iswarn, hide_type_stable=hide_type_stable,
remarks=_remarks, inline_cost=inline_cost
stringify() do io # eliminate trailing indentation (see first item in bullet list in PR #189)
cthulhu_typed(io, debuginfo, codeinf, rt, mi;
iswarn, hide_type_stable, inline_cost)
iswarn, hide_type_stable,
remarks, inline_cost)
end
end
rmatch = findfirst(r"\u001B\[90m\u001B\[(\d+)G( *)\u001B\[1G\u001B\[39m\u001B\[90m( *)\u001B\[39m$", str)
Expand All @@ -289,16 +293,17 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
print(term.out_stream::IO, str)
else
cthulhu_typed(term.out_stream::IO, debuginfo, codeinf, rt, mi;
iswarn, hide_type_stable, inline_cost)
iswarn, hide_type_stable,
remarks=_remarks, inline_cost)
end
view_cmd = cthulhu_typed
end
display_CI = true
end

menu = CthulhuMenu(callsites, optimize, iswarn&get(term.out_stream::IO, :color, false)::Bool; menu_options...)
msg = usage(view_cmd, optimize, iswarn, hide_type_stable, debuginfo, inline_cost, CONFIG.enable_highlighter)
cid = request(term, msg, menu)
usg = usage(view_cmd, optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, CONFIG.enable_highlighter)
cid = request(term, usg, menu)
toggle = menu.toggle

if toggle === nothing
Expand Down Expand Up @@ -340,7 +345,8 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
_descend(term, next_interp, next_mi;
debuginfo,
params, optimize, interruptexc,
iswarn, hide_type_stable, inline_cost)
iswarn, hide_type_stable,
remarks, inline_cost)
continue
end

Expand All @@ -357,7 +363,8 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
_descend(term, interp, next_mi;
override = isa(info, ConstPropCallInfo) ? info.result : nothing, debuginfo,
params,optimize, interruptexc,
iswarn, hide_type_stable, inline_cost)
iswarn, hide_type_stable,
remarks, inline_cost)

elseif toggle === :warn
iswarn ⊻= true
Expand All @@ -371,6 +378,11 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
end
elseif toggle === :debuginfo
debuginfo = DebugInfo((Int(debuginfo) + 1) % 3)
elseif toggle === :remarks
remarks ⊻= true
if remarks && optimize
@warn "disable optimization to see the inference remarks"
end
elseif toggle === :inline_cost
inline_cost ⊻= true
if inline_cost && !optimize
Expand Down
21 changes: 19 additions & 2 deletions src/codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ cthulhu_typed(io::IO, debuginfo::DebugInfo, args...; kwargs...) =
cthulhu_typed(io, Symbol(debuginfo), args...; kwargs...)
function cthulhu_typed(io::IO, debuginfo::Symbol,
src::Union{CodeInfo,IRCode}, @nospecialize(rt), mi::Union{Nothing,MethodInstance};
iswarn::Bool=false, hide_type_stable::Bool=false, inline_cost::Bool=false)
iswarn::Bool=false, hide_type_stable::Bool=false,
remarks::Union{Nothing,Remarks}=nothing, inline_cost::Bool=false)
debuginfo = IRShow.debuginfo(debuginfo)
lineprinter = __debuginfo[debuginfo]
rettype = ignorelimited(rt)
Expand Down Expand Up @@ -136,6 +137,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
println(iolim, Callsite(-1, MICallInfo(mi, rettype), :invoke))
end

# preprinter configuration
if src isa IRCode && inline_cost
isa(mi, MethodInstance) || throw("`mi::MethodInstance` is required")
code = src isa IRCode ? src.stmts.inst : src.code
Expand All @@ -156,7 +158,22 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
else
preprinter = lineprinter(src)
end
postprinter = iswarn ? InteractiveUtils.warntype_type_printer : IRShow.default_expr_type_printer
# postprinter configuration
_postprinter = iswarn ? InteractiveUtils.warntype_type_printer : IRShow.default_expr_type_printer
if !isnothing(remarks)
function postprinter(io::IO, @nospecialize(typ), used::Bool)
_postprinter(io, typ, used)
haskey(io, :idx) || return
idx = io[:idx]::Int
for (pc, remark) in remarks
if pc == idx
printstyled(io, ' ', remark; color=:light_black)
end
end
end
else
postprinter = _postprinter
end

should_print_stmt = hide_type_stable ? is_type_unstable : Returns(true)
bb_color = (src isa IRCode && debuginfo === :compact) ? :normal : :light_black
Expand Down
12 changes: 8 additions & 4 deletions src/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ struct OptimizedSource
isinlineable::Bool
end

const Remarks = Vector{Pair{Int, String}}

mutable struct CthulhuInterpreter <: AbstractInterpreter
native::NativeInterpreter

unopt::Dict{Union{MethodInstance, InferenceResult}, InferredSource}
opt::Dict{MethodInstance, CodeInstance}

msgs::Dict{MethodInstance, Vector{Pair{Int, String}}}
remarks::Dict{MethodInstance, Remarks}
end

CthulhuInterpreter() = CthulhuInterpreter(
NativeInterpreter(),
Dict{MethodInstance, InferredSource}(),
Dict{MethodInstance, CodeInstance}(),
Dict{MethodInstance, Vector{Pair{Int, String}}}()
Dict{MethodInstance, Remarks}()
)

import Core.Compiler: InferenceParams, OptimizationParams, get_world_counter,
Expand Down Expand Up @@ -58,8 +60,10 @@ Compiler.may_discard_trees(interp::CthulhuInterpreter) = false
Compiler.verbose_stmt_info(interp::CthulhuInterpreter) = true

function Compiler.add_remark!(interp::CthulhuInterpreter, sv::InferenceState, msg)
push!(get!(interp.msgs, sv.linfo, Tuple{Int, String}[]),
sv.currpc => msg)
if !haskey(interp.remarks, sv.linfo)
interp.remarks[sv.linfo] = Remarks()
end
push!(interp.remarks[sv.linfo], sv.currpc => msg)
end

function Compiler.finish(state::InferenceState, interp::CthulhuInterpreter)
Expand Down
14 changes: 9 additions & 5 deletions src/ui.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ mutable struct CthulhuMenu <: TerminalMenus.ConfiguredMenu{TerminalMenus.Config}
config::TerminalMenus.Config
end

function show_as_line(el, optimize::Bool, iswarn::Bool)
function show_as_line(callsite::Callsite, optimize::Bool, iswarn::Bool)
reduced_displaysize = displaysize(stdout)::Tuple{Int,Int} .- (0, 3)
sprint() do io
show(IOContext(io, :limit=>true, :displaysize=>reduced_displaysize, :optimize=>optimize, :iswarn=>iswarn, :color=>iswarn), el)
show(IOContext(io, :limit=>true, :displaysize=>reduced_displaysize, :optimize=>optimize, :iswarn=>iswarn, :color=>iswarn), callsite)
end
end

function CthulhuMenu(callsites, optimize::Bool, iswarn::Bool; pagesize::Int=10, sub_menu = false, kwargs...)
options = vcat(map(site->show_as_line(site, optimize, iswarn), callsites), [""])
options = vcat(map(callsite->show_as_line(callsite, optimize, iswarn), callsites), [""])
length(options) < 1 && error("CthulhuMenu must have at least one option")

# if pagesize is -1, use automatic paging
Expand All @@ -46,7 +46,7 @@ function stringify(@nospecialize(f), io::IO=IOBuffer())
end

const debugcolors = (:nothing, :light_black, :yellow)
function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debuginfo, inline_cost, highlight)
function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, highlight)
colorize(iotmp, use_color::Bool, c::Char) = stringify(iotmp) do io
use_color ? printstyled(io, c; color=:cyan) : print(io, c)
end
Expand All @@ -62,6 +62,7 @@ function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debu
stringify(iotmp) do io
printstyled(io, 'd'; color=debugcolors[Int(debuginfo)+1])
end, "]ebuginfo, [",
colorize(iotmp, remarks, 'r'), "]emarks, [",
colorize(iotmp, inline_cost, 'i'), "]nlining costs, [",
colorize(iotmp, highlight, 's'), "]yntax highlight for Source/LLVM/Native.")
println(ioctx, "Show: [",
Expand Down Expand Up @@ -91,6 +92,9 @@ function TerminalMenus.keypress(m::CthulhuMenu, key::UInt32)
elseif key == UInt32('d')
m.toggle = :debuginfo
return true
elseif key == UInt32('r')
m.toggle = :remarks
return true
elseif key == UInt32('i')
m.toggle = :inline_cost
return true
Expand Down Expand Up @@ -118,7 +122,7 @@ function TerminalMenus.keypress(m::CthulhuMenu, key::UInt32)
elseif key == UInt32('b')
m.toggle = :bookmark
return true
elseif key == UInt32('r') || key == UInt32('R')
elseif key == UInt32('R')
m.toggle = :revise
return true
elseif key == UInt32('e') || key == UInt32('E')
Expand Down

0 comments on commit 3a05262

Please sign in to comment.