forked from aviatesk/JET.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_optanalyzer.jl
328 lines (289 loc) · 10.3 KB
/
test_optanalyzer.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
module test_optanalyzer
include("../setup.jl")
getsomething(x::Any) = x
getsomething(x::Array) = x[]
getsomething(::Nothing) = throw(ArgumentError("nothing is nothing"))
getsomething(::Missing) = throw(ArgumentError("too philosophical"))
# bad: will lead to excessive specializations via runtime dispatch
function isType1(x)
if isa(x, DataType)
return isa(x, DataType) && x.name === Type.body.name
elseif isa(x, Union)
return isType1(x.a) && isType1(x.b)
elseif isa(x, UnionAll)
return isType1(x.body)
else
return false
end
end
# good: will be statically dispatched
function isType2(@nospecialize x)
if isa(x, DataType)
return isa(x, DataType) && x.name === Type.body.name
elseif isa(x, Union)
return isType2(x.a) && isType2(x.b)
elseif isa(x, UnionAll)
return isType2(x.body)
else
return false
end
end
@testset "runtime dispatch" begin
test_opt((Int, Vector{Any}, String,)) do a, b, c
return (
getsomething(a),
getsomething(b),
getsomething(c),
getsomething(nothing),
getsomething(missing))
end
# NOTE the following test is line-sensitive !
# if the argument type isn't well typed, compiler can't determine which method to call,
# and it will lead to runtime dispatch
let result = report_opt((Vector{Any},)) do xs
getsomething(xs[1]) # runtime dispatch !
end
@test length(get_reports_with_test(result)) == 1
r = only(get_reports_with_test(result))
@test isa(r, RuntimeDispatchReport)
@test any(r.vst) do vf
vf.file === Symbol(@__FILE__) &&
vf.line == (@__LINE__) - 7
end
end
# union split might help
test_opt((Vector{Union{Int,String,Nothing}},)) do xs
getsomething(xs[1]) # no runtime dispatch!
end
# NOTE the following test is line-sensitive !
let result = report_opt((Vector{Any},)) do xs
isType1(xs[1])
end
@test length(get_reports_with_test(result)) == 1
r = only(get_reports_with_test(result))
@test isa(r, RuntimeDispatchReport)
@test any(r.vst) do vf
vf.file === Symbol(@__FILE__) &&
vf.line == (@__LINE__) - 7
end
end
test_opt((Vector{Any},)) do xs
isType2(xs[1])
end
# real-world targets
# the `unoptimize_throw_blocks` configuration disables optimizations on "throw blocks" by default,
# but `DispatchAnalyzer` ignores problems from them, so we don't get error reports here
@test_opt sin(10)
end
function captured_variable_f(a, n)
incr! = x -> a += x
for i = 1:n
if isodd(i)
incr!(i)
end
end
return a
end
# we report `Core.Box` whatever it's type-stable
# adapted https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured
function abmult(r::Int)
if r < 0
r = -r
end
f = x -> x * r
return f
end
function abmult2(r0::Int)
r::Int = r0
if r < 0
r = -r
end
f = x -> x * r
return f
end
function abmult3(r::Int)
if r < 0
r = -r
end
f = let r = r
x -> x * r
end
return f
end
@testset "captured variables" begin
let result = report_opt(captured_variable_f, (Int, Int))
@test any(get_reports_with_test(result)) do report
return isa(report, CapturedVariableReport) &&
report.name === :a
end
end
let result = @report_opt abmult(42)
@test any(get_reports_with_test(result)) do report
return isa(report, CapturedVariableReport) &&
report.name === :r
end
end
let result = @report_opt abmult2(42)
@test any(get_reports_with_test(result)) do report
return isa(report, CapturedVariableReport) &&
report.name === :r
end
end
@test_opt abmult3(42) # no captured variable for `abmult3` !
end
with_runtime_dispatch(::UInt8) = :UInt8
with_runtime_dispatch(::UInt16) = :UInt16
with_runtime_dispatch(::UInt32) = :UInt32
with_runtime_dispatch(::UInt64) = :UInt64
with_runtime_dispatch(::UInt128) = :UInt128
struct WithRuntimeDispatch
name::Symbol
end
WithRuntimeDispatch(::UInt8) = WithRuntimeDispatch(:UInt8)
WithRuntimeDispatch(::UInt16) = WithRuntimeDispatch(:UInt16)
WithRuntimeDispatch(::UInt32) = WithRuntimeDispatch(:UInt32)
WithRuntimeDispatch(::UInt64) = WithRuntimeDispatch(:UInt64)
WithRuntimeDispatch(::UInt128) = WithRuntimeDispatch(:UInt128)
const FNC_F1_DEF_LINE = (@__LINE__)+1
skip_noncompileable_calls1(f, a) = f(a)
const FNC_F2_DEF_LINE = (@__LINE__)+1
skip_noncompileable_calls_f2(f, @nospecialize a) = f(a)
# problem: when ∑1/n exceeds 30 ?
function target_modules_compute(x)
r = 1
s = 0.0
n = 1
@time while r < x
s += 1/n
if s ≥ r
println("round $r/$x has been finished") # we're not interested type-instabilities within this call
r += 1
end
n += 1
end
return n, s
end
@testset "OptAnalyzer configurations" begin
@testset "function_filter" begin
@assert JET.InferenceParams(JET.OptAnalyzer()).max_union_splitting < 5
let result = report_opt((Vector{Any},)) do xs
with_runtime_dispatch(xs[1])
end
@test !isempty(get_reports_with_test(result))
@test any(r->isa(r,RuntimeDispatchReport), get_reports_with_test(result))
end
let function_filter(@nospecialize f) = f !== with_runtime_dispatch
test_opt((Vector{Any},); function_filter) do xs
with_runtime_dispatch(xs[1])
end
end
# function_filter for types
let result = report_opt((Vector{Any},)) do xs
WithRuntimeDispatch(xs[1])
end
@test !isempty(get_reports_with_test(result))
@test any(r->isa(r,RuntimeDispatchReport), get_reports_with_test(result))
end
let function_filter(@nospecialize f) = f !== WithRuntimeDispatch
test_opt((Vector{Any},); function_filter) do xs
WithRuntimeDispatch(xs[1])
end
end
end
@testset "skip_noncompileable_calls" begin
let # by default, we only report the runtime dispatch within the lambda function,
# and ignore error reports from `callf` calls
result = report_opt((Vector{Any},)) do ary
skip_noncompileable_calls1(sin, ary[1]) # runtime dispatch !
end
@test length(get_reports_with_test(result)) == 1
@test any(get_reports_with_test(result)) do r
isa(r, RuntimeDispatchReport) &&
last(r.vst).file === Symbol(@__FILE__) && last(r.vst).line == (@__LINE__) - 5 # report for the lambda function
end
end
let # when the `skip_noncompileable_calls` configuration is turned off,
# we will get error reports from `callsin` as well
result = report_opt((Vector{Any},); skip_noncompileable_calls=false) do ary
skip_noncompileable_calls1(sin, ary[1]) # runtime dispatch !
end
@test length(get_reports_with_test(result)) == 2
@test any(get_reports_with_test(result)) do r
isa(r, RuntimeDispatchReport) &&
last(r.vst).file === Symbol(@__FILE__) && last(r.vst).line == (@__LINE__) - 5 # report for the lambda function
end
@test any(get_reports_with_test(result)) do r
isa(r, RuntimeDispatchReport) &&
last(r.vst).file === Symbol(@__FILE__) && last(r.vst).line == FNC_F1_DEF_LINE # report for `f(a::Any)`
end
end
let # `skip_noncompileable_calls` shouldn't ignore `@nospecialize` annotation
result = report_opt((Vector{Any},)) do ary
skip_noncompileable_calls_f2(sin, ary[1]) # no runtime dispatch here, but `g(a)` is runtime dispatch
end
@test length(get_reports_with_test(result)) == 1
@test any(get_reports_with_test(result)) do r
isa(r, RuntimeDispatchReport) &&
last(r.vst).file === Symbol(@__FILE__) && last(r.vst).line == FNC_F2_DEF_LINE # report for `g(a::Any)`
end
end
end
@testset "target_modules" begin
let # we will get bunch of reports from the `println` call
result = @report_opt target_modules_compute(30)
@test !isempty(get_reports_with_test(result))
end
# if we use different `target_modules`, the reports from `println` should get filtered out
@test_opt target_modules=(@__MODULE__,) target_modules_compute(30)
end
end
# https://github.com/aviatesk/JET.jl/issues/334
# integration with concrete evaluation added in 1.8
test_opt() do
Val(:ϵ)
end
@test_opt log(2.1)
# https://github.com/aviatesk/JET.jl/issues/335
# don't report duplicated problems from inlined callees
issue335_callf(f, args...) = f(args...)
@inline function issue335_problematic_callee(val)
return issue335_undefined_call(val)
end
let result = @report_opt issue335_callf(issue335_problematic_callee, 42)
report = only(get_reports_with_test(result))
@test any(report.vst) do vsf
vsf.line == (@__LINE__)-6 &&
vsf.linfo.def.name === :issue335_problematic_callee
end
end
test_opt() do
issue335_callf(42) do val
if val < 0
return issue335_problematic_callee(val)
end
return sin(val)
end
end
# report runtime dispatches within "noncompileable" but inlineable frames
@inline noncompileable_inlined1(a, b) = a + b
let result = report_opt((Any,Any)) do a, b
noncompileable_inlined1(a, b)
end
@test any(get_reports_with_test(result)) do @nospecialize report
report isa RuntimeDispatchReport
end
end
noncompileable_inlined2(a, b) = a + b
let result = report_opt((Any,Any)) do a, b
@inline noncompileable_inlined2(a, b)
end
@test_broken any(get_reports_with_test(result)) do @nospecialize report
report isa RuntimeDispatchReport
end
end
using StaticArrays
const Issue560Vec3 = SVector{3, Float64}
const issue560μ = zeros(Issue560Vec3, 2, 3, 4, 5)
issue560f(μ) = reinterpret(reshape, Float64, μ)
@test_opt issue560f(issue560μ)
end # module test_optanalyzer