Skip to content

Commit 589b96d

Browse files
authored
Merge pull request #30223 from JuliaLang/vc/irshow
Fix toggling of debuginfo for show methods
2 parents 69ac379 + 41eaea7 commit 589b96d

File tree

8 files changed

+51
-20
lines changed

8 files changed

+51
-20
lines changed

base/reflection.jl

Lines changed: 12 additions & 5 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
@@ -940,7 +943,8 @@ end
940943
Returns an array of type-inferred lowered form (IR) for the methods matching the given
941944
generic function and type signature. The keyword argument `optimize` controls whether
942945
additional optimizations, such as inlining, are also applied.
943-
The keyword debuginfo controls the amount of code metadata present in the output.
946+
The keyword `debuginfo` controls the amount of code metadata present in the output,
947+
possible options are `:source` or `:none`.
944948
"""
945949
function code_typed(@nospecialize(f), @nospecialize(types=Tuple);
946950
optimize=true, debuginfo::Symbol=:default,
@@ -950,9 +954,12 @@ function code_typed(@nospecialize(f), @nospecialize(types=Tuple);
950954
if isa(f, Core.Builtin)
951955
throw(ArgumentError("argument is not a generic function"))
952956
end
953-
if debuginfo == :default
957+
if @isdefined(IRShow)
958+
debuginfo = IRShow.debuginfo(debuginfo)
959+
elseif debuginfo == :default
954960
debuginfo = :source
955-
elseif debuginfo != :source && debuginfo != :none
961+
end
962+
if debuginfo != :source && debuginfo != :none
956963
throw(ArgumentError("'debuginfo' must be either :source or :none"))
957964
end
958965
types = to_tuple_type(types)

base/show.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,16 +1578,17 @@ module IRShow
15781578
Base.last(r::Compiler.StmtRange) = Compiler.last(r)
15791579
include("compiler/ssair/show.jl")
15801580

1581-
const debuginfo = Dict{Symbol, Any}(
1581+
const __debuginfo = Dict{Symbol, Any}(
15821582
# :full => src -> Base.IRShow.DILineInfoPrinter(src.linetable), # and add variable slot information
15831583
:source => src -> Base.IRShow.DILineInfoPrinter(src.linetable),
15841584
# :oneliner => src -> Base.IRShow.PartialLineInfoPrinter(src.linetable),
15851585
:none => src -> Base.IRShow.lineinfo_disabled,
15861586
)
1587-
debuginfo[:default] = debuginfo[:none]
1587+
const default_debuginfo = Ref{Symbol}(:none)
1588+
debuginfo(sym) = sym == :default ? default_debuginfo[] : sym
15881589
end
15891590

1590-
function show(io::IO, src::CodeInfo; debuginfo::Symbol=:default)
1591+
function show(io::IO, src::CodeInfo; debuginfo::Symbol=:source)
15911592
# Fix slot names and types in function body
15921593
print(io, "CodeInfo(")
15931594
lambda_io::IOContext = io
@@ -1598,7 +1599,9 @@ function show(io::IO, src::CodeInfo; debuginfo::Symbol=:default)
15981599
if isempty(src.linetable) || src.linetable[1] isa LineInfoNode
15991600
println(io)
16001601
# TODO: static parameter values?
1601-
IRShow.show_ir(lambda_io, src, IRShow.debuginfo[debuginfo](src))
1602+
# only accepts :source or :none, we can't have a fallback for default since
1603+
# that would break code_typed(, debuginfo=:source) iff IRShow.default_debuginfo[] = :none
1604+
IRShow.show_ir(lambda_io, src, IRShow.__debuginfo[debuginfo](src))
16021605
else
16031606
# this is a CodeInfo that has not been used as a method yet, so its locations are still LineNumberNodes
16041607
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+

src/disasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ static void jl_dump_asm_internal(
859859
DILineInfoTable::iterator di_lineIter = di_lineinfo.begin();
860860
DILineInfoTable::iterator di_lineEnd = di_lineinfo.end();
861861
DILineInfoPrinter dbgctx{"; ", true};
862+
dbgctx.SetVerbosity(debuginfo);
862863
if (pass != 0) {
863864
if (di_ctx && di_lineIter != di_lineEnd) {
864865
// Set up the line info

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)