-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathinline.jl
499 lines (449 loc) · 15.8 KB
/
inline.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Test
using Base.Meta
using Core: ReturnNode
"""
Helper to walk the AST and call a function on every node.
"""
function walk(func, expr)
func(expr)
if isa(expr, Expr)
func(expr.head)
for o in expr.args
walk(func, o)
end
end
end
"""
Helper to test that every slot is in range after inlining.
"""
function test_inlined_symbols(func, argtypes)
src, rettype = code_typed(func, argtypes)[1]
nl = length(src.slotnames)
ast = Expr(:block)
ast.args = src.code
walk(ast) do e
if isa(e, Core.Slot)
@test 1 <= e.id <= nl
end
if isa(e, Core.NewvarNode)
@test 1 <= e.slot.id <= nl
end
end
end
# Test case 1:
# Make sure that all symbols are properly escaped after inlining
# https://github.com/JuliaLang/julia/issues/12620
@inline function test_inner(count)
x = 1
i = 0
while i <= count
y = x
x = x + y
i += 1
end
end
function test_outer(a)
test_inner(a)
end
test_inlined_symbols(test_outer, Tuple{Int64})
# Test case 2:
# Make sure that an error is thrown for the undeclared
# y in the else branch.
# https://github.com/JuliaLang/julia/issues/12620
@inline function foo_inl(x)
if x
y = 2
else
return y
end
end
function bar12620()
for i = 1:3
foo_inl(i==1)
end
end
@test_throws UndefVarError(:y) bar12620()
# issue #16165
@inline f16165(x) = (x = UInt(x) + 1)
g16165(x) = f16165(x)
@test g16165(1) === (UInt(1) + 1)
# issue #18948
f18948() = (local bar::Int64; bar=1.5)
g18948() = (local bar::Int32; bar=0x80000000)
@test_throws InexactError f18948()
@test_throws InexactError g18948()
# issue #21074
struct s21074
x::Tuple{Int, Int}
end
@inline Base.getindex(v::s21074, i::Integer) = v.x[i]
@eval f21074() = $(s21074((1,2))).x[1]
let (src, _) = code_typed(f21074, ())[1]
@test src.code[end] == ReturnNode(1)
end
@eval g21074() = $(s21074((1,2)))[1]
let (src, _) = code_typed(g21074, ())[1]
@test src.code[end] == ReturnNode(1)
end
# issue #21311
counter21311 = Ref(0)
@noinline function update21311!(x)
counter21311[] += 1
x[] = counter21311[]
return x
end
@noinline map21311(t::Tuple{Any}) = (update21311!(t[1]),)
@inline map21311(t::Tuple) = (update21311!(t[1]), map21311(Base.tail(t))...)
function read21311()
xs = Ref(1), Ref(1)
map21311(xs)
return xs[1]
end
let a = read21311()
@test a[] == 1
end
# issue #29083
f29083(;μ,σ) = μ + σ*randn()
g29083() = f29083(μ=2.0,σ=0.1)
let c = code_typed(g29083, ())[1][1].code
# make sure no call to kwfunc remains
@test !any(e->(isa(e,Expr) && ((e.head === :invoke && e.args[1].def.name === :kwfunc) ||
(e.head === :foreigncall && e.args[1] === QuoteNode(:jl_get_keyword_sorter)))),
c)
end
@testset "issue #19122: [no]inline of short func. def. with return type annotation" begin
exf19122 = @macroexpand(@inline f19122()::Bool = true)
exg19122 = @macroexpand(@noinline g19122()::Bool = true)
@test exf19122.args[2].args[1].args[1] == :inline
@test exg19122.args[2].args[1].args[1] == :noinline
@inline f19122()::Bool = true
@noinline g19122()::Bool = true
@test f19122()
@test g19122()
end
@testset "issue #27403: getindex is inlined with Union{Int,Missing}" begin
function sum27403(X::AbstractArray)
s = zero(eltype(X)) + zero(eltype(X))
for x in X
if !ismissing(x)
s += x
end
end
s
end
(src, _) = code_typed(sum27403, Tuple{Vector{Int}})[1]
@test !any(x -> x isa Expr && x.head === :invoke, src.code)
end
function fully_eliminated(f, args)
let code = code_typed(f, args)[1][1].code
return length(code) == 1 && isa(code[1], ReturnNode)
end
end
function fully_eliminated(f, args, retval)
let code = code_typed(f, args)[1][1].code
return length(code) == 1 && isa(code[1], ReturnNode) && code[1].val == retval
end
end
# check that ismutabletype(type) can be fully eliminated
f_mutable_nothrow(s::String) = Val{typeof(s).name.flags}
@test fully_eliminated(f_mutable_nothrow, (String,))
# check that ifelse can be fully eliminated
function f_ifelse(x)
a = ifelse(true, false, true)
b = ifelse(a, true, false)
return b ? x + 1 : x
end
# 2 for now because the compiler leaves a GotoNode around
@test_broken length(code_typed(f_ifelse, (String,))[1][1].code) <= 2
# Test that inlining of _apply_iterate properly hits the inference cache
@noinline cprop_inline_foo1() = (1, 1)
@noinline cprop_inline_foo2() = (2, 2)
function cprop_inline_bar(x...)
if x === (1, 1, 1, 1)
return x
else
# What you put here doesn't really matter,
# the point is to prevent inlining when
# x is not known to be (1, 1, 1, 1)
println(stdout, "Hello")
println(stdout, "World")
println(stdout, "Hello")
println(stdout, "World")
println(stdout, "Hello")
println(stdout, "World")
println(stdout, "Hello")
println(stdout, "World")
println(stdout, "Hello")
println(stdout, "World")
println(stdout, "Hello")
println(stdout, "World")
return x
end
x
end
function cprop_inline_baz1()
return cprop_inline_bar(cprop_inline_foo1()..., cprop_inline_foo1()...)
end
@test fully_eliminated(cprop_inline_baz1, ())
function cprop_inline_baz2()
return cprop_inline_bar(cprop_inline_foo2()..., cprop_inline_foo2()...)
end
@test length(code_typed(cprop_inline_baz2, ())[1][1].code) == 2
# Check that apply_type/TypeVar can be fully eliminated
function f_apply_typevar(T)
NTuple{N, T} where N
return T
end
@test fully_eliminated(f_apply_typevar, (Type{Any},))
# check that div can be fully eliminated
function f_div(x)
div(x, 1)
return x
end
@test fully_eliminated(f_div, (Int,)) == 1
# ...unless we div by an unknown amount
function f_div(x, y)
div(x, y)
return x
end
@test length(code_typed(f_div, (Int, Int))[1][1].code) > 1
f_identity_splat(t) = (t...,)
@test fully_eliminated(f_identity_splat, (Tuple{Int,Int},))
# splatting one tuple into (,) plus zero or more empties should reduce
# this pattern appears for example in `fill_to_length`
f_splat_with_empties(t) = (()..., t..., ()..., ()...)
@test fully_eliminated(f_splat_with_empties, (NTuple{200,UInt8},))
# check that <: can be fully eliminated
struct SomeArbitraryStruct; end
function f_subtype()
T = SomeArbitraryStruct
T <: Bool
end
@test fully_eliminated(f_subtype, Tuple{}, false)
# check that pointerref gets deleted if unused
f_pointerref(T::Type{S}) where S = Val(length(T.parameters))
let code = code_typed(f_pointerref, Tuple{Type{Int}})[1][1].code
any_ptrref = false
for i = 1:length(code)
stmt = code[i]
isa(stmt, Expr) || continue
stmt.head === :call || continue
arg1 = stmt.args[1]
if arg1 === Base.pointerref || (isa(arg1, GlobalRef) && arg1.name === :pointerref)
any_ptrref = true
end
end
@test !any_ptrref
end
# Test that inlining can inline _applys of builtins/_applys on SimpleVectors
function foo_apply_apply_type_svec()
A = (Tuple, Float32)
B = Tuple{Float32, Float32}
Core.apply_type(A..., B.types...)
end
@test fully_eliminated(foo_apply_apply_type_svec, Tuple{}, NTuple{3, Float32})
# The that inlining doesn't drop ambiguity errors (#30118)
c30118(::Tuple{Ref{<:Type}, Vararg}) = nothing
c30118(::Tuple{Ref, Ref}) = nothing
b30118(x...) = c30118(x)
@test_throws MethodError c30118((Base.RefValue{Type{Int64}}(), Ref(Int64)))
@test_throws MethodError b30118(Base.RefValue{Type{Int64}}(), Ref(Int64))
# Issue #34900
f34900(x::Int, y) = x
f34900(x, y::Int) = y
f34900(x::Int, y::Int) = invoke(f34900, Tuple{Int, Any}, x, y)
@test fully_eliminated(f34900, Tuple{Int, Int}, Core.Argument(2))
@testset "check jl_ir_flag_inlineable for inline macro" begin
@test ccall(:jl_ir_flag_inlineable, Bool, (Any,), first(methods(@inline x -> x)).source)
@test !ccall(:jl_ir_flag_inlineable, Bool, (Any,), first(methods( x -> x)).source)
@test ccall(:jl_ir_flag_inlineable, Bool, (Any,), first(methods(@inline function f(x) x end)).source)
@test !ccall(:jl_ir_flag_inlineable, Bool, (Any,), first(methods(function f(x) x end)).source)
end
const _a_global_array = [1]
f_inline_global_getindex() = _a_global_array[1]
let ci = code_typed(f_inline_global_getindex, Tuple{})[1].first
@test any(x->(isexpr(x, :call) && x.args[1] === GlobalRef(Base, :arrayref)), ci.code)
end
# Issue #29114 & #36087 - Inlining of non-tuple splats
f_29115(x) = (x...,)
@test @allocated(f_29115(1)) == 0
@test @allocated(f_29115(1=>2)) == 0
let ci = code_typed(f_29115, Tuple{Int64})[1].first
@test length(ci.code) == 2 && isexpr(ci.code[1], :call) &&
ci.code[1].args[1] === GlobalRef(Core, :tuple)
end
let ci = code_typed(f_29115, Tuple{Pair{Int64, Int64}})[1].first
@test length(ci.code) == 4 && isexpr(ci.code[1], :call) &&
ci.code[end-1].args[1] === GlobalRef(Core, :tuple)
end
# Issue #37182 & #37555 - Inlining of pending nodes
function f37555(x::Int; kwargs...)
@assert x < 10
+(x, kwargs...)
end
@test f37555(1) == 1
# Test that we can inline small constants even if they are not isbits
struct NonIsBitsDims
dims::NTuple{N, Int} where N
end
NonIsBitsDims() = NonIsBitsDims(())
let ci = code_typed(NonIsBitsDims, Tuple{})[1].first
@test length(ci.code) == 1 && isa(ci.code[1], ReturnNode) &&
ci.code[1].val.value == NonIsBitsDims()
end
struct NonIsBitsDimsUndef
dims::NTuple{N, Int} where N
NonIsBitsDimsUndef() = new()
end
@test Core.Compiler.is_inlineable_constant(NonIsBitsDimsUndef())
@test !Core.Compiler.is_inlineable_constant((("a"^1000, "b"^1000), nothing))
# More nothrow modeling for apply_type
f_apply_type_typeof(x) = (Ref{typeof(x)}; nothing)
@test fully_eliminated(f_apply_type_typeof, Tuple{Any})
@test fully_eliminated(f_apply_type_typeof, Tuple{Vector})
@test fully_eliminated(x->(Val{x}; nothing), Tuple{Int})
@test fully_eliminated(x->(Val{x}; nothing), Tuple{Symbol})
@test fully_eliminated(x->(Val{x}; nothing), Tuple{Tuple{Int, Int}})
@test !fully_eliminated(x->(Val{x}; nothing), Tuple{String})
@test !fully_eliminated(x->(Val{x}; nothing), Tuple{Any})
@test !fully_eliminated(x->(Val{x}; nothing), Tuple{Tuple{Int, String}})
struct RealConstrained{T <: Real}; end
@test !fully_eliminated(x->(RealConstrained{x}; nothing), Tuple{Int})
@test !fully_eliminated(x->(RealConstrained{x}; nothing), Tuple{Type{Vector{T}} where T})
# Check that pure functions with non-inlineable results still get deleted
struct Big
x::NTuple{1024, Int}
end
@Base.pure Big() = Big(ntuple(identity, 1024))
function pure_elim_full()
Big()
nothing
end
@test fully_eliminated(pure_elim_full, Tuple{})
# Union splitting of convert
f_convert_missing(x) = convert(Int64, x)
let ci = code_typed(f_convert_missing, Tuple{Union{Int64, Missing}})[1][1],
ci_unopt = code_typed(f_convert_missing, Tuple{Union{Int64, Missing}}; optimize=false)[1][1]
# We want to check that inlining was able to union split this, but we don't
# want to make the test too specific to the exact structure that inlining
# generates, so instead, we just check that the compiler made it bigger.
# There are performance tests that are also sensitive to union splitting
# here, so a non-obvious regression
@test length(ci.code) >
length(ci_unopt.code)
end
# OC getfield elim
using Base.Experimental: @opaque
f_oc_getfield(x) = (@opaque ()->x)()
@test fully_eliminated(f_oc_getfield, Tuple{Int})
# check if `x` is a statically-resolved call of a function whose name is `sym`
isinvoke(@nospecialize(x), sym::Symbol) = isinvoke(x, mi->mi.def.name===sym)
function isinvoke(@nospecialize(x), pred)
if Meta.isexpr(x, :invoke)
return pred(x.args[1]::Core.MethodInstance)
end
return false
end
code_typed1(args...; kwargs...) = (first∘first)(code_typed(args...; kwargs...))::Core.CodeInfo
@testset "@inline/@noinline annotation before definition" begin
m = Module()
@eval m begin
@inline function _def_inline(x)
# this call won't be resolved and thus will prevent inlining to happen if we don't
# annotate `@inline` at the top of this function body
return unresolved_call(x)
end
def_inline(x) = _def_inline(x)
@noinline _def_noinline(x) = x # obviously will be inlined otherwise
def_noinline(x) = _def_noinline(x)
# test that they don't conflict with other "before-definition" macros
@inline Base.@aggressive_constprop function _def_inline_noconflict(x)
# this call won't be resolved and thus will prevent inlining to happen if we don't
# annotate `@inline` at the top of this function body
return unresolved_call(x)
end
def_inline_noconflict(x) = _def_inline_noconflict(x)
@noinline Base.@aggressive_constprop _def_noinline_noconflict(x) = x # obviously will be inlined otherwise
def_noinline_noconflict(x) = _def_noinline_noconflict(x)
end
let ci = code_typed1(m.def_inline, (Int,))
@test all(ci.code) do x
!isinvoke(x, :_def_inline)
end
end
let ci = code_typed1(m.def_noinline, (Int,))
@test any(ci.code) do x
isinvoke(x, :_def_noinline)
end
end
# test that they don't conflict with other "before-definition" macros
let ci = code_typed1(m.def_inline_noconflict, (Int,))
@test all(ci.code) do x
!isinvoke(x, :_def_inline_noconflict)
end
end
let ci = code_typed1(m.def_noinline_noconflict, (Int,))
@test any(ci.code) do x
isinvoke(x, :_def_noinline_noconflict)
end
end
end
@testset "@inline/@noinline annotation within a function body" begin
m = Module()
@eval m begin
function _body_inline(x)
@inline
# this call won't be resolved and thus will prevent inlining to happen if we don't
# annotate `@inline` at the top of this function body
return unresolved_call(x)
end
body_inline(x) = _body_inline(x)
function _body_noinline(x)
@noinline
return x # obviously will be inlined otherwise
end
body_noinline(x) = _body_noinline(x)
# test annotations for `do` blocks
@inline simple_caller(a) = a()
function do_inline(x)
simple_caller() do
@inline
# this call won't be resolved and thus will prevent inlining to happen if we don't
# annotate `@inline` at the top of this anonymous function body
return unresolved_call(x)
end
end
function do_noinline(x)
simple_caller() do
@noinline
return x # obviously will be inlined otherwise
end
end
end
let ci = code_typed1(m.body_inline, (Int,))
@test all(ci.code) do x
!isinvoke(x, :_body_inline)
end
end
let ci = code_typed1(m.body_noinline, (Int,))
@test any(ci.code) do x
isinvoke(x, :_body_noinline)
end
end
# test annotations for `do` blocks
let ci = code_typed1(m.do_inline, (Int,))
# what we test here is that both `simple_caller` and the anonymous function that the
# `do` block creates should inlined away, and as a result there is only the unresolved call
@test all(ci.code) do x
!isinvoke(x, :simple_caller) &&
!isinvoke(x, mi->startswith(string(mi.def.name), '#'))
end
end
let ci = code_typed1(m.do_noinline, (Int,))
# the anonymous function that the `do` block created shouldn't be inlined here
@test any(ci.code) do x
isinvoke(x, mi->startswith(string(mi.def.name), '#'))
end
end
end