Skip to content

Commit

Permalink
code_warntype: restore printing of Variables
Browse files Browse the repository at this point in the history
also expose `optimize` flag, and set default to false

closes JuliaLang#29287
  • Loading branch information
vtjnash committed Feb 13, 2019
1 parent 2e37784 commit 42b6571
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ function sourceinfo_slotnames(src::CodeInfo)
for i in eachindex(slotnames)
name = string(slotnames[i])
idx = get!(names, name, i)
if idx != i
if idx != i || isempty(name)
printname = "$name@_$i"
idx > 0 && (printnames[idx] = "$name@_$idx")
names[name] = 0
Expand Down
2 changes: 1 addition & 1 deletion doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ default: html
SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
JULIAHOME := $(abspath $(SRCDIR)/..)
include $(JULIAHOME)/Make.inc
JULIA_EXECUTABLE := $(call spawn,$(build_bindir)/julia)
JULIA_EXECUTABLE := $(call spawn,$(build_bindir)/julia) --startup-file=no

.PHONY: help clean cleanall html pdf deps deploy

Expand Down
30 changes: 11 additions & 19 deletions doc/src/manual/performance-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -1362,29 +1362,21 @@ julia> @noinline pos(x) = x < 0 ? 0 : x;
julia> function f(x)
y = pos(x)
sin(y*x + 1)
return sin(y*x + 1)
end;
julia> @code_warntype f(3.2)
Variables
#self#::Core.Compiler.Const(f, false)
x::Float64
y::Union{Float64, Int64}
Body::Float64
2 1 ─ %1 = invoke Main.pos(%%x::Float64)::UNION{FLOAT64, INT64}
3 │ %2 = isa(%1, Float64)::Bool
└── goto 3 if not %2
2 ─ %4 = π (%1, Float64)
│ %5 = Base.mul_float(%4, %%x)::Float64
└── goto 6
3 ─ %7 = isa(%1, Int64)::Bool
└── goto 5 if not %7
4 ─ %9 = π (%1, Int64)
│ %10 = Base.sitofp(Float64, %9)::Float64
│ %11 = Base.mul_float(%10, %%x)::Float64
└── goto 6
5 ─ Base.error("fatal error in type inference (type bound)")
└── unreachable
6 ┄ %15 = φ (2 => %5, 4 => %11)::Float64
│ %16 = Base.add_float(%15, 1.0)::Float64
│ %17 = invoke Main.sin(%16::Float64)::Float64
└── return %17
1 ─ (y = Main.pos(x))
│ %2 = (y * x)::Float64
│ %3 = (%2 + 1)::Float64
│ %4 = Main.sin(%3)::Float64
└── return %4
```

Interpreting the output of [`@code_warntype`](@ref), like that of its cousins [`@code_lowered`](@ref),
Expand Down
4 changes: 2 additions & 2 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ typedef struct _jl_method_t {
} jl_method_t;

// This type caches the data for a specType signature specialization of a Method
typedef struct _jl_method_instance_t {
struct _jl_method_instance_t {
JL_DATA_TYPE
union {
jl_value_t *value; // generic accessor
Expand All @@ -332,7 +332,7 @@ typedef struct _jl_method_instance_t {
// names of declarations in the JIT,
// suitable for referencing in LLVM IR
jl_llvm_functions_t functionObjectsDecls;
} jl_method_instance_t;
};

// all values are callable as Functions
typedef jl_value_t jl_function_t;
Expand Down
17 changes: 14 additions & 3 deletions stdlib/InteractiveUtils/src/codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,24 @@ Keyword argument `debuginfo` may be one of `:source` or `:none` (default), to sp
See [`@code_warntype`](@ref man-code-warntype) for more information.
"""
function code_warntype(io::IO, @nospecialize(f), @nospecialize(t); debuginfo::Symbol=:default)
function code_warntype(io::IO, @nospecialize(f), @nospecialize(t); debuginfo::Symbol=:default, optimize::Bool=false)
debuginfo = Base.IRShow.debuginfo(debuginfo)
lineprinter = Base.IRShow.__debuginfo[debuginfo]
for (src, rettype) in code_typed(f, t)
for (src, rettype) in code_typed(f, t, optimize=optimize)
lambda_io::IOContext = io
if src.slotnames !== nothing
lambda_io = IOContext(lambda_io, :SOURCE_SLOTNAMES => Base.sourceinfo_slotnames(src))
slotnames = Base.sourceinfo_slotnames(src)
lambda_io = IOContext(lambda_io, :SOURCE_SLOTNAMES => slotnames)
println(io, "Variables")
slottypes = src.slottypes
for i = 1:length(slotnames)
print(io, " ", slotnames[i])
if isa(slottypes, Vector{Any})
warntype_type_printer(io, slottypes[i], true)
end
println(io)
end
println(io)
end
print(io, "Body")
warntype_type_printer(io, rettype, true)
Expand Down
7 changes: 6 additions & 1 deletion stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ end

_args_and_call(args...; kwargs...) = (args[1:end-1], kwargs, args[end](args[1:end-1]...; kwargs...))
_materialize_broadcasted(f, args...) = Broadcast.materialize(Broadcast.broadcasted(f, args...))

"""
@inferred [AllowedType] f(x)
Expand All @@ -1309,8 +1310,12 @@ julia> typeof(f(2))
Int64
julia> @code_warntype f(2)
Variables
#self#::Core.Compiler.Const(f, false)
a::Int64
Body::UNION{FLOAT64, INT64}
1 ─ %1 = Base.slt_int(1, a)::Bool
1 ─ %1 = (a > 1)::Bool
└── goto #3 if not %1
2 ─ return 1
3 ─ return 1.0
Expand Down
14 changes: 7 additions & 7 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ function g15714(array_var15714)
array_var15714[index_var15714] += 0
end
let index_var15714
for index_var15714 in eachindex(array_var15714)
for outer index_var15714 in eachindex(array_var15714)
array_var15714[index_var15714] += 0
end
index_var15714
end
let index_var15714
for index_var15714 in eachindex(array_var15714)
for outer index_var15714 in eachindex(array_var15714)
array_var15714[index_var15714] += 0
end
index_var15714
Expand All @@ -350,11 +350,11 @@ import InteractiveUtils.code_warntype
used_dup_var_tested15714 = false
used_unique_var_tested15714 = false
function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types), must_used_vars)
src, rettype = code_typed(f, types)[1]
src, rettype = code_typed(f, types, optimize=false)[1]
dupnames = Set()
slotnames = Set()
for name in src.slotnames
if name in slotnames
if name in slotnames || name === Symbol("")
push!(dupnames, name)
else
push!(slotnames, name)
Expand All @@ -368,7 +368,7 @@ function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types
for sym in must_used_vars
must_used_checked[sym] = false
end
for str in (sprint(code_warntype, f, types),
for str in (sprint(io -> code_warntype(io, f, types, optimize=false)),
repr("text/plain", src))
for var in must_used_vars
@test occursin(string(var), str)
Expand Down Expand Up @@ -410,9 +410,9 @@ function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types
end
end
test_typed_ast_printing(f15714, Tuple{Vector{Float32}},
[:array_var15714])
[:array_var15714, :index_var15714])
test_typed_ast_printing(g15714, Tuple{Vector{Float32}},
[:array_var15714])
[:array_var15714, :index_var15714])
#This test doesn't work with the new optimizer because we drop slotnames
#We may want to test it against debug info eventually
#@test used_dup_var_tested15715
Expand Down

0 comments on commit 42b6571

Please sign in to comment.