Skip to content

Commit

Permalink
add option to hide type annotations (#234)
Browse files Browse the repository at this point in the history
* add option to hide type annotations

The motivating case here is that Diffractor often generates complex and
deeply nested lattice elements, so hiding them can make the code easier
to read.

* fix toggle

* improve coverage
  • Loading branch information
simeonschaub authored Oct 6, 2021
1 parent ba4a116 commit cecf599
Show file tree
Hide file tree
Showing 70 changed files with 929 additions and 36 deletions.
16 changes: 9 additions & 7 deletions src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
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,
remarks::Bool=false, inline_cost::Bool=false)
remarks::Bool=false, inline_cost::Bool=false, type_annotations::Bool=true)
if isnothing(hide_type_stable)
hide_type_stable = something(verbose, false)
end
Expand Down Expand Up @@ -287,11 +287,11 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
if debuginfo == DInfo.compact
str = let debuginfo=debuginfo, codeinf=codeinf, rt=rt, mi=mi,
iswarn=iswarn, hide_type_stable=hide_type_stable,
remarks=_remarks, inline_cost=inline_cost
remarks=_remarks, inline_cost=inline_cost, type_annotations=type_annotations
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,
remarks, inline_cost)
remarks, inline_cost, type_annotations)
end
end
rmatch = findfirst(r"\u001B\[90m\u001B\[(\d+)G( *)\u001B\[1G\u001B\[39m\u001B\[90m( *)\u001B\[39m$", str)
Expand All @@ -302,15 +302,15 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
else
cthulhu_typed(term.out_stream::IO, debuginfo, codeinf, rt, mi;
iswarn, hide_type_stable,
remarks=_remarks, inline_cost)
remarks=_remarks, inline_cost, type_annotations)
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...)
usg = usage(view_cmd, optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, CONFIG.enable_highlighter)
usg = usage(view_cmd, optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, type_annotations, CONFIG.enable_highlighter)
cid = request(term, usg, menu)
toggle = menu.toggle

Expand Down Expand Up @@ -354,7 +354,7 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
debuginfo,
params, optimize, interruptexc,
iswarn, hide_type_stable,
remarks, inline_cost)
remarks, inline_cost, type_annotations)
continue
end

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

elseif toggle === :warn
iswarn ⊻= true
Expand All @@ -396,6 +396,8 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
if inline_cost && !optimize
@warn "enable optimization to see the inlining costs"
end
elseif toggle === :type_annotations
type_annotations ⊻= true
elseif toggle === :highlighter
CONFIG.enable_highlighter ⊻= true
if CONFIG.enable_highlighter
Expand Down
10 changes: 8 additions & 2 deletions src/codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ cthulhu_typed(io::IO, debuginfo::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,
remarks::Union{Nothing,Remarks}=nothing, inline_cost::Bool=false)
remarks::Union{Nothing,Remarks}=nothing, inline_cost::Bool=false,
type_annotations::Bool=true)

debuginfo = IRShow.debuginfo(debuginfo)
lineprinter = __debuginfo[debuginfo]
rettype = ignorelimited(rt)
Expand Down Expand Up @@ -159,7 +161,11 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
preprinter = lineprinter(src)
end
# postprinter configuration
_postprinter = iswarn ? InteractiveUtils.warntype_type_printer : IRShow.default_expr_type_printer
_postprinter = if type_annotations
iswarn ? InteractiveUtils.warntype_type_printer : IRShow.default_expr_type_printer
else
Returns(nothing)
end
if !isnothing(remarks)
function postprinter(io::IO, @nospecialize(typ), used::Bool)
_postprinter(io, typ, used)
Expand Down
6 changes: 5 additions & 1 deletion src/ui.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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, remarks, inline_cost, highlight)
function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, type_annotations, 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 @@ -64,6 +64,7 @@ function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debu
end, "]ebuginfo, [",
colorize(iotmp, remarks, 'r'), "]emarks, [",
colorize(iotmp, inline_cost, 'i'), "]nlining costs, [",
colorize(iotmp, type_annotations, 't'), "]ype annotations, [",
colorize(iotmp, highlight, 's'), "]yntax highlight for Source/LLVM/Native.")
println(ioctx, "Show: [",
colorize(iotmp, view_cmd === cthulhu_source, 'S'), "]ource code, [",
Expand Down Expand Up @@ -98,6 +99,9 @@ function TerminalMenus.keypress(m::CthulhuMenu, key::UInt32)
elseif key == UInt32('i')
m.toggle = :inline_cost
return true
elseif key == UInt32('t')
m.toggle = :type_annotations
return true
elseif key == UInt32('s')
m.toggle = :highlighter
return true
Expand Down
12 changes: 7 additions & 5 deletions test/codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ Revise.track(CthulhuTestSandbox, normpath(@__DIR__, "sandbox.jl"))
@testset "iswarn: $iswarn" for iswarn in tf
@testset "hide_type_stable: $hide_type_stable" for hide_type_stable in tf
@testset "inline_cost: $inline_cost" for inline_cost in tf
io = IOBuffer()
Cthulhu.cthulhu_typed(io, debuginfo,
src, rt, mi;
iswarn, hide_type_stable, inline_cost)
@test !isempty(String(take!(io))) # just check it works
@testset "type_annotations: $type_annotations" for type_annotations in tf
io = IOBuffer()
Cthulhu.cthulhu_typed(io, debuginfo,
src, rt, mi;
iswarn, hide_type_stable, inline_cost, type_annotations)
@test !isempty(String(take!(io))) # just check it works
end
end
end
end
Expand Down
28 changes: 15 additions & 13 deletions test/irshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@ end
@testset "iswarn: $iswarn" for iswarn in tf
@testset "hide_type_stable: $hide_type_stable" for hide_type_stable in tf
@testset "inline_cost: $inline_cost" for inline_cost in tf
!optimize && debuginfo === Cthulhu.DInfo.compact && continue
!optimize && inline_cost && continue
@testset "type_annotations: $type_annotations" for type_annotations in tf
!optimize && debuginfo === Cthulhu.DInfo.compact && continue
!optimize && inline_cost && continue

s = sprint(; context=:color=>true) do io
Cthulhu.cthulhu_typed(io, debuginfo,
src, rt, mi;
iswarn, hide_type_stable, inline_cost)
end
s = strip_base_linenums(s)
s = sprint(; context=:color=>true) do io
Cthulhu.cthulhu_typed(io, debuginfo,
src, rt, mi;
iswarn, hide_type_stable, inline_cost, type_annotations)
end
s = strip_base_linenums(s)

ground_truth = read(irshow_filename(optimize, debuginfo, iswarn, hide_type_stable, inline_cost), String)
if Sys.iswindows()
ground_truth = replace(ground_truth, "\r\n" => "\n")
ground_truth = read(irshow_filename(optimize, debuginfo, iswarn, hide_type_stable, inline_cost, type_annotations), String)
if Sys.iswindows()
ground_truth = replace(ground_truth, "\r\n" => "\n")
end
@test s == ground_truth
s != ground_truth && println(deepdiff(s, ground_truth))
end
@test s == ground_truth
s != ground_truth && println(deepdiff(s, ground_truth))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
│ ─ %-1 = invoke foo(::Int64,::Int64)::Any
││ │  0 %2 = %new(%1)
│  4 ┄─ 0 %9 = Core.getfield(%2, :contents)
│  │  1000 %10 = (%9 < 4)
│  8 ┄─ 0 %16 = Core.getfield(%2, :contents)
│  │  1000 %17 = (%16 + 1)
│  │  20 %20 = invoke %19(_2::Int64)
│  14 ┄ 0 %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
│ 8 │  1000 %31 = (%20 + %30)
  
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
│ ─ %-1 = invoke foo(::Int64,::Int64)::Any
││ │ %2 = %new(%1)
│  4 ┄─ %9 = Core.getfield(%2, :contents)
│  │ %10 = (%9 < 4)
│  8 ┄─ %16 = Core.getfield(%2, :contents)
│  │ %17 = (%16 + 1)
│  │ %20 = invoke %19(_2::Int64)
│  14 ┄ %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
│ 8 │ %31 = (%20 + %30)
  
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
│ ─ %-1 = invoke foo(::Int64,::Int64)::Any
│╻ Box2 1 ── 0 %1 = Core.Box
││ │  0 %2 = %new(%1)
│╻ + │  1 %3 = Base.add_int(_2, _3)
│  │  3 Core.setfield!(%2, :contents, %3)
│ 3 │  1 %5 = Core.isdefined(%2, :contents)
│  └─── 0 goto #3 if not %5
│  2 ── 0 goto #4
│  3 ── 0 $(Expr(:throw_undef_if_not, :z, false))
│  4 ┄─ 0 %9 = Core.getfield(%2, :contents)
│  │  1000 %10 = (%9 < 4)
│  └─── 0 goto #9 if not %10
│ 4 5 ── 1 %12 = Core.isdefined(%2, :contents)
│  └─── 0 goto #7 if not %12
│  6 ── 0 goto #8
│  7 ── 0 $(Expr(:throw_undef_if_not, :z, false))
│  8 ┄─ 0 %16 = Core.getfield(%2, :contents)
│  │  1000 %17 = (%16 + 1)
│  └─── 3 Core.setfield!(%2, :contents, %17)
│ 6 9 ┄─ 0 %19 = %new(Main.anonymous.:(var"#1#2"), %2)
│  │  20 %20 = invoke %19(_2::Int64)
│ 7 │  1 %21 = (isa)(_2, Missing)
│  └─── 0 goto #11 if not %21
│  10 ─ 0 goto #14
│  11 ─ 1 %24 = (isa)(_2, Int64)
│  └─── 0 goto #13 if not %24
│╻ + 12 ─ 1 %26 = Base.add_int(_2, _3)
│  └─── 0 goto #14
│  13 ─ 0 Core.throw(ErrorException("fatal error in type inference (type bound)"))
│  └─── 0 unreachable
│  14 ┄ 0 %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
│ 8 │  1000 %31 = (%20 + %30)
│  └─── 0 return %31
  
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
│ ─ %-1 = invoke foo(::Int64,::Int64)::Any
│╻ Box2 1 ── %1 = Core.Box
││ │ %2 = %new(%1)
│╻ + │ %3 = Base.add_int(_2, _3)
│  │ Core.setfield!(%2, :contents, %3)
│ 3 │ %5 = Core.isdefined(%2, :contents)
│  └─── goto #3 if not %5
│  2 ── goto #4
│  3 ── $(Expr(:throw_undef_if_not, :z, false))
│  4 ┄─ %9 = Core.getfield(%2, :contents)
│  │ %10 = (%9 < 4)
│  └─── goto #9 if not %10
│ 4 5 ── %12 = Core.isdefined(%2, :contents)
│  └─── goto #7 if not %12
│  6 ── goto #8
│  7 ── $(Expr(:throw_undef_if_not, :z, false))
│  8 ┄─ %16 = Core.getfield(%2, :contents)
│  │ %17 = (%16 + 1)
│  └─── Core.setfield!(%2, :contents, %17)
│ 6 9 ┄─ %19 = %new(Main.anonymous.:(var"#1#2"), %2)
│  │ %20 = invoke %19(_2::Int64)
│ 7 │ %21 = (isa)(_2, Missing)
│  └─── goto #11 if not %21
│  10 ─ goto #14
│  11 ─ %24 = (isa)(_2, Int64)
│  └─── goto #13 if not %24
│╻ + 12 ─ %26 = Base.add_int(_2, _3)
│  └─── goto #14
│  13 ─ Core.throw(ErrorException("fatal error in type inference (type bound)"))
│  └─── unreachable
│  14 ┄ %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
│ 8 │ %31 = (%20 + %30)
│  └─── return %31
  
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Body::Any
││ │  0 %2 = %new(%1)
│  4 ┄─ 0 %9 = Core.getfield(%2, :contents)
│  │  1000 %10 = (%9 < 4)
│  8 ┄─ 0 %16 = Core.getfield(%2, :contents)
│  │  1000 %17 = (%16 + 1)
│  │  20 %20 = invoke %19(_2::Int64)
│  14 ┄ 0 %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
│ 8 │  1000 %31 = (%20 + %30)
  
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Body::Any
││ │ %2 = %new(%1)
│  4 ┄─ %9 = Core.getfield(%2, :contents)
│  │ %10 = (%9 < 4)
│  8 ┄─ %16 = Core.getfield(%2, :contents)
│  │ %17 = (%16 + 1)
│  │ %20 = invoke %19(_2::Int64)
│  14 ┄ %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
│ 8 │ %31 = (%20 + %30)
  
Loading

0 comments on commit cecf599

Please sign in to comment.