Skip to content

Commit 42b6571

Browse files
committed
code_warntype: restore printing of Variables
also expose `optimize` flag, and set default to false closes #29287
1 parent 2e37784 commit 42b6571

File tree

7 files changed

+42
-34
lines changed

7 files changed

+42
-34
lines changed

base/show.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ function sourceinfo_slotnames(src::CodeInfo)
635635
for i in eachindex(slotnames)
636636
name = string(slotnames[i])
637637
idx = get!(names, name, i)
638-
if idx != i
638+
if idx != i || isempty(name)
639639
printname = "$name@_$i"
640640
idx > 0 && (printnames[idx] = "$name@_$idx")
641641
names[name] = 0

doc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ default: html
66
SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
77
JULIAHOME := $(abspath $(SRCDIR)/..)
88
include $(JULIAHOME)/Make.inc
9-
JULIA_EXECUTABLE := $(call spawn,$(build_bindir)/julia)
9+
JULIA_EXECUTABLE := $(call spawn,$(build_bindir)/julia) --startup-file=no
1010

1111
.PHONY: help clean cleanall html pdf deps deploy
1212

doc/src/manual/performance-tips.md

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,29 +1362,21 @@ julia> @noinline pos(x) = x < 0 ? 0 : x;
13621362
13631363
julia> function f(x)
13641364
y = pos(x)
1365-
sin(y*x + 1)
1365+
return sin(y*x + 1)
13661366
end;
13671367
13681368
julia> @code_warntype f(3.2)
1369+
Variables
1370+
#self#::Core.Compiler.Const(f, false)
1371+
x::Float64
1372+
y::Union{Float64, Int64}
1373+
13691374
Body::Float64
1370-
2 1 ─ %1 = invoke Main.pos(%%x::Float64)::UNION{FLOAT64, INT64}
1371-
3 │ %2 = isa(%1, Float64)::Bool
1372-
└── goto 3 if not %2
1373-
2 ─ %4 = π (%1, Float64)
1374-
│ %5 = Base.mul_float(%4, %%x)::Float64
1375-
└── goto 6
1376-
3 ─ %7 = isa(%1, Int64)::Bool
1377-
└── goto 5 if not %7
1378-
4 ─ %9 = π (%1, Int64)
1379-
│ %10 = Base.sitofp(Float64, %9)::Float64
1380-
│ %11 = Base.mul_float(%10, %%x)::Float64
1381-
└── goto 6
1382-
5 ─ Base.error("fatal error in type inference (type bound)")
1383-
└── unreachable
1384-
6 ┄ %15 = φ (2 => %5, 4 => %11)::Float64
1385-
│ %16 = Base.add_float(%15, 1.0)::Float64
1386-
│ %17 = invoke Main.sin(%16::Float64)::Float64
1387-
└── return %17
1375+
1 ─ (y = Main.pos(x))
1376+
│ %2 = (y * x)::Float64
1377+
│ %3 = (%2 + 1)::Float64
1378+
│ %4 = Main.sin(%3)::Float64
1379+
└── return %4
13881380
```
13891381

13901382
Interpreting the output of [`@code_warntype`](@ref), like that of its cousins [`@code_lowered`](@ref),

src/julia.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ typedef struct _jl_method_t {
309309
} jl_method_t;
310310

311311
// This type caches the data for a specType signature specialization of a Method
312-
typedef struct _jl_method_instance_t {
312+
struct _jl_method_instance_t {
313313
JL_DATA_TYPE
314314
union {
315315
jl_value_t *value; // generic accessor
@@ -332,7 +332,7 @@ typedef struct _jl_method_instance_t {
332332
// names of declarations in the JIT,
333333
// suitable for referencing in LLVM IR
334334
jl_llvm_functions_t functionObjectsDecls;
335-
} jl_method_instance_t;
335+
};
336336

337337
// all values are callable as Functions
338338
typedef jl_value_t jl_function_t;

stdlib/InteractiveUtils/src/codeview.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,24 @@ Keyword argument `debuginfo` may be one of `:source` or `:none` (default), to sp
3131
3232
See [`@code_warntype`](@ref man-code-warntype) for more information.
3333
"""
34-
function code_warntype(io::IO, @nospecialize(f), @nospecialize(t); debuginfo::Symbol=:default)
34+
function code_warntype(io::IO, @nospecialize(f), @nospecialize(t); debuginfo::Symbol=:default, optimize::Bool=false)
3535
debuginfo = Base.IRShow.debuginfo(debuginfo)
3636
lineprinter = Base.IRShow.__debuginfo[debuginfo]
37-
for (src, rettype) in code_typed(f, t)
37+
for (src, rettype) in code_typed(f, t, optimize=optimize)
3838
lambda_io::IOContext = io
3939
if src.slotnames !== nothing
40-
lambda_io = IOContext(lambda_io, :SOURCE_SLOTNAMES => Base.sourceinfo_slotnames(src))
40+
slotnames = Base.sourceinfo_slotnames(src)
41+
lambda_io = IOContext(lambda_io, :SOURCE_SLOTNAMES => slotnames)
42+
println(io, "Variables")
43+
slottypes = src.slottypes
44+
for i = 1:length(slotnames)
45+
print(io, " ", slotnames[i])
46+
if isa(slottypes, Vector{Any})
47+
warntype_type_printer(io, slottypes[i], true)
48+
end
49+
println(io)
50+
end
51+
println(io)
4152
end
4253
print(io, "Body")
4354
warntype_type_printer(io, rettype, true)

stdlib/Test/src/Test.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,7 @@ end
12871287

12881288
_args_and_call(args...; kwargs...) = (args[1:end-1], kwargs, args[end](args[1:end-1]...; kwargs...))
12891289
_materialize_broadcasted(f, args...) = Broadcast.materialize(Broadcast.broadcasted(f, args...))
1290+
12901291
"""
12911292
@inferred [AllowedType] f(x)
12921293
@@ -1309,8 +1310,12 @@ julia> typeof(f(2))
13091310
Int64
13101311
13111312
julia> @code_warntype f(2)
1313+
Variables
1314+
#self#::Core.Compiler.Const(f, false)
1315+
a::Int64
1316+
13121317
Body::UNION{FLOAT64, INT64}
1313-
1 ─ %1 = Base.slt_int(1, a)::Bool
1318+
1 ─ %1 = (a > 1)::Bool
13141319
└── goto #3 if not %1
13151320
2 ─ return 1
13161321
3 ─ return 1.0

test/reflection.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,13 @@ function g15714(array_var15714)
332332
array_var15714[index_var15714] += 0
333333
end
334334
let index_var15714
335-
for index_var15714 in eachindex(array_var15714)
335+
for outer index_var15714 in eachindex(array_var15714)
336336
array_var15714[index_var15714] += 0
337337
end
338338
index_var15714
339339
end
340340
let index_var15714
341-
for index_var15714 in eachindex(array_var15714)
341+
for outer index_var15714 in eachindex(array_var15714)
342342
array_var15714[index_var15714] += 0
343343
end
344344
index_var15714
@@ -350,11 +350,11 @@ import InteractiveUtils.code_warntype
350350
used_dup_var_tested15714 = false
351351
used_unique_var_tested15714 = false
352352
function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types), must_used_vars)
353-
src, rettype = code_typed(f, types)[1]
353+
src, rettype = code_typed(f, types, optimize=false)[1]
354354
dupnames = Set()
355355
slotnames = Set()
356356
for name in src.slotnames
357-
if name in slotnames
357+
if name in slotnames || name === Symbol("")
358358
push!(dupnames, name)
359359
else
360360
push!(slotnames, name)
@@ -368,7 +368,7 @@ function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types
368368
for sym in must_used_vars
369369
must_used_checked[sym] = false
370370
end
371-
for str in (sprint(code_warntype, f, types),
371+
for str in (sprint(io -> code_warntype(io, f, types, optimize=false)),
372372
repr("text/plain", src))
373373
for var in must_used_vars
374374
@test occursin(string(var), str)
@@ -410,9 +410,9 @@ function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types
410410
end
411411
end
412412
test_typed_ast_printing(f15714, Tuple{Vector{Float32}},
413-
[:array_var15714])
413+
[:array_var15714, :index_var15714])
414414
test_typed_ast_printing(g15714, Tuple{Vector{Float32}},
415-
[:array_var15714])
415+
[:array_var15714, :index_var15714])
416416
#This test doesn't work with the new optimizer because we drop slotnames
417417
#We may want to test it against debug info eventually
418418
#@test used_dup_var_tested15715

0 commit comments

Comments
 (0)