Skip to content

Commit 41eaea7

Browse files
committed
map :default for IRShow to :source or :none + docs
1 parent 6ed9413 commit 41eaea7

File tree

7 files changed

+48
-20
lines changed

7 files changed

+48
-20
lines changed

base/reflection.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,12 @@ Note that an error will be thrown if `types` are not leaf types when `generated`
745745
`true` and any of the corresponding methods are an `@generated` method.
746746
"""
747747
function code_lowered(@nospecialize(f), @nospecialize(t=Tuple); generated::Bool=true, debuginfo::Symbol=:default)
748-
if debuginfo == :default
748+
if @isdefined(IRShow)
749+
debuginfo = IRShow.debuginfo(debuginfo)
750+
elseif debuginfo == :default
749751
debuginfo = :source
750-
elseif debuginfo != :source && debuginfo != :none
752+
end
753+
if debuginfo != :source && debuginfo != :none
751754
throw(ArgumentError("'debuginfo' must be either :source or :none"))
752755
end
753756
return map(method_instances(f, t)) do m
@@ -951,9 +954,12 @@ function code_typed(@nospecialize(f), @nospecialize(types=Tuple);
951954
if isa(f, Core.Builtin)
952955
throw(ArgumentError("argument is not a generic function"))
953956
end
954-
if debuginfo == :default
957+
if @isdefined(IRShow)
958+
debuginfo = IRShow.debuginfo(debuginfo)
959+
elseif debuginfo == :default
955960
debuginfo = :source
956-
elseif debuginfo != :source && debuginfo != :none
961+
end
962+
if debuginfo != :source && debuginfo != :none
957963
throw(ArgumentError("'debuginfo' must be either :source or :none"))
958964
end
959965
types = to_tuple_type(types)

base/show.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,17 +1566,17 @@ module IRShow
15661566
Base.last(r::Compiler.StmtRange) = Compiler.last(r)
15671567
include("compiler/ssair/show.jl")
15681568

1569-
const debuginfo = Dict{Symbol, Any}(
1569+
const __debuginfo = Dict{Symbol, Any}(
15701570
# :full => src -> Base.IRShow.DILineInfoPrinter(src.linetable), # and add variable slot information
15711571
:source => src -> Base.IRShow.DILineInfoPrinter(src.linetable),
15721572
# :oneliner => src -> Base.IRShow.PartialLineInfoPrinter(src.linetable),
15731573
:none => src -> Base.IRShow.lineinfo_disabled,
15741574
)
1575-
# setting debuginfo[:default] = debuginfo[:none] will disable debuginfo printing globally
1576-
debuginfo[:default] = debuginfo[:source]
1575+
const default_debuginfo = Ref{Symbol}(:none)
1576+
debuginfo(sym) = sym == :default ? default_debuginfo[] : sym
15771577
end
15781578

1579-
function show(io::IO, src::CodeInfo; debuginfo::Symbol=:default)
1579+
function show(io::IO, src::CodeInfo; debuginfo::Symbol=:source)
15801580
# Fix slot names and types in function body
15811581
print(io, "CodeInfo(")
15821582
lambda_io::IOContext = io
@@ -1587,7 +1587,9 @@ function show(io::IO, src::CodeInfo; debuginfo::Symbol=:default)
15871587
if isempty(src.linetable) || src.linetable[1] isa LineInfoNode
15881588
println(io)
15891589
# TODO: static parameter values?
1590-
IRShow.show_ir(lambda_io, src, IRShow.debuginfo[debuginfo](src))
1590+
# only accepts :source or :none, we can't have a fallback for default since
1591+
# that would break code_typed(, debuginfo=:source) iff IRShow.default_debuginfo[] = :none
1592+
IRShow.show_ir(lambda_io, src, IRShow.__debuginfo[debuginfo](src))
15911593
else
15921594
# this is a CodeInfo that has not been used as a method yet, so its locations are still LineNumberNodes
15931595
body = Expr(:block)

doc/src/devdocs/reflection.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ as assignments, branches, and calls:
9696
```jldoctest
9797
julia> Meta.lower(@__MODULE__, :( [1+2, sin(0.5)] ))
9898
:($(Expr(:thunk, CodeInfo(
99+
@ none within `top-level scope'
99100
1 ─ %1 = 1 + 2
100101
│ %2 = sin(0.5)
101102
│ %3 = (Base.vect)(%1, %2)
@@ -122,13 +123,31 @@ calls and expand argument types automatically:
122123
```julia-repl
123124
julia> @code_llvm +(1,1)
124125
125-
; @ int.jl:53 within `+'
126126
define i64 @"julia_+_130862"(i64, i64) {
127127
top:
128128
%2 = add i64 %1, %0
129129
ret i64 %2
130130
}
131131
```
132132

133-
See [`@code_lowered`](@ref), [`@code_typed`](@ref), [`@code_warntype`](@ref),
133+
For more informations see [`@code_lowered`](@ref), [`@code_typed`](@ref), [`@code_warntype`](@ref),
134134
[`@code_llvm`](@ref), and [`@code_native`](@ref).
135+
136+
### Printing of debug information
137+
138+
The aforementioned functions and macros take the keyword argument `debuginfo` that controls the level
139+
debug information printed.
140+
141+
```
142+
julia> @code_typed debuginfo=:source +(1,1)
143+
CodeInfo(
144+
@ int.jl:53 within `+'
145+
1 ─ %1 = (Base.add_int)(x, y)::Int64
146+
└── return %1
147+
) => Int64
148+
```
149+
150+
Possible values for `debuginfo` are: `:none`, `:source`, and`:default`.
151+
Per default debug information is not printed, but that can be changed
152+
by setting `Base.IRShow.default_debuginfo[] = :source`.
153+

stdlib/InteractiveUtils/src/codeview.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ problematic for performance, so the results need to be used judiciously.
2727
In particular, unions containing either [`missing`](@ref) or [`nothing`](@ref) are displayed in yellow, since
2828
these are often intentional.
2929
30-
Keyword argument `debuginfo` may be one of source or none (default), to specify the verbosity of code comments.
30+
Keyword argument `debuginfo` may be one of `:source` or `:none` (default), to specify the verbosity of code comments.
3131
3232
See [`@code_warntype`](@ref man-code-warntype) for more information.
3333
"""
3434
function code_warntype(io::IO, @nospecialize(f), @nospecialize(t); debuginfo::Symbol=:default)
35-
lineprinter = Base.IRShow.debuginfo[debuginfo]
35+
debuginfo = Base.IRShow.debuginfo(debuginfo)
36+
lineprinter = Base.IRShow.__debuginfo[debuginfo]
3637
for (src, rettype) in code_typed(f, t)
3738
lambda_io::IOContext = io
3839
if src.slotnames !== nothing

stdlib/InteractiveUtils/src/macros.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ by putting them and their value before the function call, like this:
229229
230230
`optimize` controls whether additional optimizations, such as inlining, are also applied.
231231
`raw` makes all metadata and dbg.* calls visible.
232-
`debuginfo` may be one of full, source (default), none, to specify the verbosity of code comments.
232+
`debuginfo` may be one of `:source` (default) or `:none`, to specify the verbosity of code comments.
233233
`dump_module` prints the entire module that encapsulates the function.
234234
"""
235235
:@code_llvm
@@ -244,6 +244,6 @@ Set the optional keyword argument `debuginfo` by putting it before the function
244244
245245
@code_native debuginfo=:default f(x)
246246
247-
`debuginfo` may be one of source (default) or none, to specify the verbosity of code comments.
247+
`debuginfo` may be one of `:source` (default) or `:none`, to specify the verbosity of code comments.
248248
"""
249249
:@code_native

test/show.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ end
13091309

13101310
# Tests for code_typed linetable annotations
13111311
function compute_annotations(f, types)
1312-
src = code_typed(f, types)[1][1]
1312+
src = code_typed(f, types, debuginfo=:source)[1][1]
13131313
ir = Core.Compiler.inflate_ir(src)
13141314
la, lb, ll = Base.IRShow.compute_ir_line_annotations(ir)
13151315
max_loc_method = maximum(length(s) for s in la)
@@ -1364,7 +1364,7 @@ eval(Meta.parse("""function my_fun28173(x)
13641364
end
13651365
return y
13661366
end""")) # use parse to control the line numbers
1367-
let src = code_typed(my_fun28173, (Int,))[1][1]
1367+
let src = code_typed(my_fun28173, (Int,), debuginfo=:source)[1][1]
13681368
ir = Core.Compiler.inflate_ir(src)
13691369
fill!(src.codelocs, 0) # IRCode printing is only capable of printing partial line info
13701370
let source_slotnames = String["my_fun28173", "x"],
@@ -1402,7 +1402,7 @@ end
14021402
# Verify that extra instructions at the end of the IR
14031403
# don't throw errors in the printing, but instead print
14041404
# with as unnamed "!" BB.
1405-
let src = code_typed(gcd, (Int, Int))[1][1]
1405+
let src = code_typed(gcd, (Int, Int), debuginfo=:source)[1][1]
14061406
ir = Core.Compiler.inflate_ir(src)
14071407
push!(ir.stmts, Core.Compiler.ReturnNode())
14081408
lines = split(sprint(show, ir), '\n')

test/syntax.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@ end
736736
end
737737
end
738738

739-
f1_ci = code_typed(f1, (Int,))[1][1]
740-
f2_ci = code_typed(f2, (Int,))[1][1]
739+
f1_ci = code_typed(f1, (Int,), debuginfo=:source)[1][1]
740+
f2_ci = code_typed(f2, (Int,), debuginfo=:source)[1][1]
741741

742742
f1_exprs = get_expr_list(f1_ci)
743743
f2_exprs = get_expr_list(f2_ci)

0 commit comments

Comments
 (0)