forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffects.jl
1429 lines (1309 loc) · 50.4 KB
/
effects.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Test
include("irutils.jl")
# Test that the Core._apply_iterate bail path taints effects
function f_apply_bail(f)
f(()...)
return nothing
end
@test !Compiler.is_removable_if_unused(Base.infer_effects(f_apply_bail))
@test !fully_eliminated((Function,)) do f
f_apply_bail(f)
nothing
end
# Test that effect modeling for return_type doesn't incorrectly pick
# up the effects of the function being analyzed
f_throws() = error()
@noinline function return_type_unused(x)
Compiler.return_type(f_throws, Tuple{})
return x+1
end
@test Compiler.is_removable_if_unused(Base.infer_effects(return_type_unused, (Int,)))
@test fully_eliminated((Int,)) do x
return_type_unused(x)
return nothing
end
# Test that ambiguous calls don't accidentally get nothrow effect
ambig_effects_test(a::Int, b) = 1
ambig_effects_test(a, b::Int) = 1
ambig_effects_test(a, b) = 1
@test !Compiler.is_nothrow(Base.infer_effects(ambig_effects_test, (Int, Any)))
global ambig_unknown_type_global::Any = 1
@noinline function conditionally_call_ambig(b::Bool, a)
if b
ambig_effects_test(a, ambig_unknown_type_global)
end
return 0
end
@test !fully_eliminated((Bool,)) do b
conditionally_call_ambig(b, 1)
return nothing
end
# Test that a missing methtable identification gets tainted
# appropriately
struct FCallback; f::Union{Nothing, Function}; end
f_invoke_callback(fc) = let f=fc.f; (f !== nothing && f(); nothing); end
@test !Compiler.is_removable_if_unused(Base.infer_effects(f_invoke_callback, (FCallback,)))
@test !fully_eliminated((FCallback,)) do fc
f_invoke_callback(fc)
return nothing
end
# @assume_effects override
const ___CONST_DICT___ = Dict{Any,Any}(Symbol(c) => i for (i, c) in enumerate('a':'z'))
Base.@assume_effects :foldable concrete_eval(
f, args...; kwargs...) = f(args...; kwargs...)
@test fully_eliminated() do
concrete_eval(getindex, ___CONST_DICT___, :a)
end
# :removable override
Base.@assume_effects :removable removable_call(
f, args...; kwargs...) = f(args...; kwargs...)
@test fully_eliminated() do
@noinline removable_call(getindex, ___CONST_DICT___, :a)
nothing
end
# terminates_globally override
# https://github.com/JuliaLang/julia/issues/41694
Base.@assume_effects :terminates_globally function issue41694(x)
res = 1
0 ≤ x < 20 || error("bad fact")
while x > 1
res *= x
x -= 1
end
return res
end
@test Compiler.is_foldable(Base.infer_effects(issue41694, (Int,)))
@test fully_eliminated() do
issue41694(2)
end
Base.@assume_effects :terminates_globally function recur_termination1(x)
x == 0 && return 1
0 ≤ x < 20 || error("bad fact")
return x * recur_termination1(x-1)
end
@test Compiler.is_foldable(Base.infer_effects(recur_termination1, (Int,)))
@test Compiler.is_terminates(Base.infer_effects(recur_termination1, (Int,)))
function recur_termination2()
Base.@assume_effects :total !:terminates_globally
recur_termination1(12)
end
@test fully_eliminated(recur_termination2)
@test fully_eliminated() do; recur_termination2(); end
Base.@assume_effects :terminates_globally function recur_termination21(x)
x == 0 && return 1
0 ≤ x < 20 || error("bad fact")
return recur_termination22(x)
end
recur_termination22(x) = x * recur_termination21(x-1)
@test Compiler.is_foldable(Base.infer_effects(recur_termination21, (Int,)))
@test Compiler.is_foldable(Base.infer_effects(recur_termination22, (Int,)))
@test Compiler.is_terminates(Base.infer_effects(recur_termination21, (Int,)))
@test Compiler.is_terminates(Base.infer_effects(recur_termination22, (Int,)))
function recur_termination2x()
Base.@assume_effects :total !:terminates_globally
recur_termination21(12) + recur_termination22(12)
end
@test fully_eliminated(recur_termination2x)
@test fully_eliminated() do; recur_termination2x(); end
# anonymous function support for `@assume_effects`
@test fully_eliminated() do
map((2,3,4)) do x
# this :terminates_locally allows this anonymous function to be constant-folded
Base.@assume_effects :terminates_locally
res = 1
0 ≤ x < 20 || error("bad fact")
while x > 1
res *= x
x -= 1
end
return res
end
end
# control flow backedge should taint `terminates`
@test Base.infer_effects((Int,)) do n
for i = 1:n; end
end |> !Compiler.is_terminates
# interprocedural-recursion should taint `terminates` **appropriately**
function sumrecur(a, x)
isempty(a) && return x
return sumrecur(Base.tail(a), x + first(a))
end
@test Base.infer_effects(sumrecur, (Tuple{Int,Int,Int},Int)) |> Compiler.is_terminates
@test Base.infer_effects(sumrecur, (Tuple{Int,Int,Int,Vararg{Int}},Int)) |> !Compiler.is_terminates
# https://github.com/JuliaLang/julia/issues/45781
@test Base.infer_effects((Float32,)) do a
out1 = promote_type(Irrational{:π}, Bool)
out2 = sin(a)
out1, out2
end |> Compiler.is_terminates
# refine :consistent-cy effect inference using the return type information
@test Base.infer_effects((Any,)) do x
taint = Ref{Any}(x) # taints :consistent-cy, but will be adjusted
throw(taint)
end |> Compiler.is_consistent
@test Base.infer_effects((Int,)) do x
if x < 0
taint = Ref(x) # taints :consistent-cy, but will be adjusted
throw(DomainError(x, taint))
end
return nothing
end |> Compiler.is_consistent
@test Base.infer_effects((Int,)) do x
if x < 0
taint = Ref(x) # taints :consistent-cy, but will be adjusted
throw(DomainError(x, taint))
end
return x == 0 ? nothing : x # should `Union` of isbitstype objects nicely
end |> Compiler.is_consistent
@test Base.infer_effects((Symbol,Any)) do s, x
if s === :throw
taint = Ref{Any}(":throw option given") # taints :consistent-cy, but will be adjusted
throw(taint)
end
return s # should handle `Symbol` nicely
end |> Compiler.is_consistent
@test Base.infer_effects((Int,)) do x
return Ref(x)
end |> !Compiler.is_consistent
@test Base.infer_effects((Int,)) do x
return x < 0 ? Ref(x) : nothing
end |> !Compiler.is_consistent
@test Base.infer_effects((Int,)) do x
if x < 0
throw(DomainError(x, lazy"$x is negative"))
end
return nothing
end |> Compiler.is_foldable
# :the_exception expression should taint :consistent-cy
global inconsistent_var::Int = 42
function throw_inconsistent() # this is still :consistent
throw(inconsistent_var)
end
function catch_inconsistent()
try
throw_inconsistent()
catch err
err
end
end
@test !Compiler.is_consistent(Base.infer_effects(catch_inconsistent))
cache_inconsistent() = catch_inconsistent()
function compare_inconsistent()
a = cache_inconsistent()
global inconsistent_var = 0
b = cache_inconsistent()
global inconsistent_var = 42
return a === b
end
@test !compare_inconsistent()
# return type information shouldn't be able to refine it also
function catch_inconsistent(x::T) where T
v = x
try
throw_inconsistent()
catch err
v = err::T
end
return v
end
@test !Compiler.is_consistent(Base.infer_effects(catch_inconsistent, (Int,)))
cache_inconsistent(x) = catch_inconsistent(x)
function compare_inconsistent(x::T) where T
x = one(T)
a = cache_inconsistent(x)
global inconsistent_var = 0
b = cache_inconsistent(x)
global inconsistent_var = 42
return a === b
end
@test !compare_inconsistent(3)
# Effect modeling for Core.compilerbarrier
@test Base.infer_effects(Base.inferencebarrier, Tuple{Any}) |> Compiler.is_removable_if_unused
# effects modeling for allocation/access of uninitialized fields
struct Maybe{T}
x::T
Maybe{T}() where T = new{T}()
Maybe{T}(x) where T = new{T}(x)
Maybe(x::T) where T = new{T}(x)
end
Base.getindex(x::Maybe) = x.x
struct SyntacticallyDefined{T}
x::T
end
@test Base.infer_effects() do
Maybe{Int}()
end |> !Compiler.is_consistent
@test Base.infer_effects() do
Maybe{Int}()[]
end |> !Compiler.is_consistent
@test !fully_eliminated() do
Maybe{Int}()[]
end
@test Base.infer_effects() do
Maybe{String}()
end |> Compiler.is_consistent
@test Base.infer_effects() do
Maybe{String}()[]
end |> Compiler.is_consistent
let f() = Maybe{String}()[]
@test Base.return_types() do
f() # this call should be concrete evaluated
end |> only === Union{}
end
@test Base.infer_effects() do
Ref{Int}()
end |> !Compiler.is_consistent
@test Base.infer_effects() do
Ref{Int}()[]
end |> !Compiler.is_consistent
@test !fully_eliminated() do
Ref{Int}()[]
end
@test Base.infer_effects() do
Ref{String}()[]
end |> Compiler.is_consistent
let f() = Ref{String}()[]
@test Base.return_types() do
f() # this call should be concrete evaluated
end |> only === Union{}
end
@test Base.infer_effects((SyntacticallyDefined{Float64}, Symbol)) do w, s
getfield(w, s)
end |> Compiler.is_foldable
# effects propagation for `Core.invoke` calls
# https://github.com/JuliaLang/julia/issues/44763
global x44763::Int = 0
increase_x44763!(n) = (global x44763; x44763 += n)
invoke44763(x) = @invoke increase_x44763!(x)
@test Base.return_types() do
invoke44763(42)
end |> only === Int
@test x44763 == 0
# `@inbounds`/`@boundscheck` expression should taint :consistent correctly
# https://github.com/JuliaLang/julia/issues/48099
function A1_inbounds()
r = 0
@inbounds begin
@boundscheck r += 1
end
return r
end
@test !Compiler.is_consistent(Base.infer_effects(A1_inbounds))
# Test that purity doesn't try to accidentally run unreachable code due to
# boundscheck elimination
function f_boundscheck_elim(n)
# Inbounds here assumes that this is only ever called with `n==0`, but of
# course the compiler has no way of knowing that, so it must not attempt
# to run the `@inbounds getfield(sin, 1)` that `ntuple` generates.
ntuple(x->(@inbounds ()[x]), n)
end
@test !Compiler.is_noub(Base.infer_effects(f_boundscheck_elim, (Int,)))
@test Tuple{} <: only(Base.return_types(f_boundscheck_elim, (Int,)))
# Test that purity modeling doesn't accidentally introduce new world age issues
f_redefine_me(x) = x+1
f_call_redefine() = f_redefine_me(0)
f_mk_opaque() = Base.Experimental.@opaque ()->Base.inferencebarrier(f_call_redefine)()
const op_capture_world = f_mk_opaque()
f_redefine_me(x) = x+2
@test op_capture_world() == 1
@test f_mk_opaque()() == 2
# backedge insertion for Any-typed, effect-free frame
const CONST_DICT = let d = Dict()
for c in 'A':'z'
push!(d, c => Int(c))
end
d
end
Base.@assume_effects :foldable getcharid(c) = CONST_DICT[c]
@noinline callf(f, args...) = f(args...)
function entry_to_be_invalidated(c)
return callf(getcharid, c)
end
@test Base.infer_effects((Char,)) do x
entry_to_be_invalidated(x)
end |> Compiler.is_foldable
@test fully_eliminated(; retval=97) do
entry_to_be_invalidated('a')
end
getcharid(c) = CONST_DICT[c] # now this is not eligible for concrete evaluation
@test Base.infer_effects((Char,)) do x
entry_to_be_invalidated(x)
end |> !Compiler.is_foldable
@test !fully_eliminated() do
entry_to_be_invalidated('a')
end
@test !Compiler.builtin_nothrow(Compiler.fallback_lattice, Core.get_binding_type, Any[Rational{Int}, Core.Const(:foo)], Any)
# effects modeling for assignment to globals
global glob_assign_int::Int = 0
f_glob_assign_int() = global glob_assign_int = 1
let effects = Base.infer_effects(f_glob_assign_int, (); optimize=false)
@test Compiler.is_consistent(effects)
@test !Compiler.is_effect_free(effects)
@test Compiler.is_nothrow(effects)
end
# effects modeling for for setglobal!
global SETGLOBAL!_NOTHROW::Int = 0
let effects = Base.infer_effects(; optimize=false) do
setglobal!(@__MODULE__, :SETGLOBAL!_NOTHROW, 42)
end
@test Compiler.is_consistent(effects)
@test !Compiler.is_effect_free(effects)
@test Compiler.is_nothrow(effects)
end
# we should taint `nothrow` if the binding doesn't exist and isn't fixed yet,
setglobal!_nothrow_undefinedyet() = setglobal!(@__MODULE__, :UNDEFINEDYET, 42)
let effects = Base.infer_effects(setglobal!_nothrow_undefinedyet)
@test !Compiler.is_nothrow(effects)
end
@test_throws ErrorException setglobal!_nothrow_undefinedyet()
# This declares the binding as ::Any
@eval global_assignment_undefinedyet() = $(GlobalRef(@__MODULE__, :UNDEFINEDYET)) = 42
let effects = Base.infer_effects(global_assignment_undefinedyet)
@test Compiler.is_nothrow(effects)
end
# Again with type mismatch
global UNDEFINEDYET2::String = "0"
setglobal!_nothrow_undefinedyet2() = setglobal!(@__MODULE__, :UNDEFINEDYET2, 42)
@eval global_assignment_undefinedyet2() = $(GlobalRef(@__MODULE__, :UNDEFINEDYET2)) = 42
let effects = Base.infer_effects(global_assignment_undefinedyet2)
@test !Compiler.is_nothrow(effects)
end
let effects = Base.infer_effects(setglobal!_nothrow_undefinedyet2)
@test !Compiler.is_nothrow(effects)
end
@test_throws TypeError setglobal!_nothrow_undefinedyet2()
module ExportMutableGlobal
global mutable_global_for_setglobal_test::Int = 0
export mutable_global_for_setglobal_test
end
using .ExportMutableGlobal: mutable_global_for_setglobal_test
f_assign_imported() = global mutable_global_for_setglobal_test = 42
let effects = Base.infer_effects(f_assign_imported)
@test !Compiler.is_nothrow(effects)
end
@test_throws ErrorException f_assign_imported()
# Nothrow for setfield!
mutable struct SetfieldNothrow
x::Int
end
f_setfield_nothrow() = SetfieldNothrow(0).x = 1
let effects = Base.infer_effects(f_setfield_nothrow, ())
@test Compiler.is_nothrow(effects)
@test Compiler.is_effect_free(effects) # see EFFECT_FREE_IF_INACCESSIBLEMEMONLY
end
# even if 2-arg `getfield` may throw, it should be still `:consistent`
@test Compiler.is_consistent(Base.infer_effects(getfield, (NTuple{5, Float64}, Int)))
# SimpleVector allocation is consistent
@test Compiler.is_consistent(Base.infer_effects(Core.svec))
@test Base.infer_effects() do
Core.svec(nothing, 1, "foo")
end |> Compiler.is_consistent
# fastmath operations are in-`:consistent`
@test !Compiler.is_consistent(Base.infer_effects((a,b)->@fastmath(a+b), (Float64,Float64)))
# issue 46122: @assume_effects for @ccall
@test Base.infer_effects((Vector{Int},)) do a
Base.@assume_effects :effect_free @ccall this_call_does_not_really_exist(a::Any)::Ptr{Int}
end |> Compiler.is_effect_free
# `getfield_effects` handles access to union object nicely
let 𝕃 = Compiler.fallback_lattice
getfield_effects = Compiler.getfield_effects
@test Compiler.is_consistent(getfield_effects(𝕃, Any[Some{String}, Core.Const(:value)], String))
@test Compiler.is_consistent(getfield_effects(𝕃, Any[Some{Symbol}, Core.Const(:value)], Symbol))
@test Compiler.is_consistent(getfield_effects(𝕃, Any[Union{Some{Symbol},Some{String}}, Core.Const(:value)], Union{Symbol,String}))
end
@test Base.infer_effects((Bool,)) do c
obj = c ? Some{String}("foo") : Some{Symbol}(:bar)
return getfield(obj, :value)
end |> Compiler.is_consistent
# getfield is nothrow when bounds checking is turned off
@test Base.infer_effects((Tuple{Int,Int},Int)) do t, i
getfield(t, i, false)
end |> Compiler.is_nothrow
@test Base.infer_effects((Tuple{Int,Int},Symbol)) do t, i
getfield(t, i, false)
end |> Compiler.is_nothrow
@test Base.infer_effects((Tuple{Int,Int},String)) do t, i
getfield(t, i, false) # invalid name type
end |> !Compiler.is_nothrow
@test Base.infer_effects((Some{Any},)) do some
getfield(some, 1, :not_atomic)
end |> Compiler.is_nothrow
@test Base.infer_effects((Some{Any},)) do some
getfield(some, 1, :invalid_atomic_spec)
end |> !Compiler.is_nothrow
@test Base.infer_effects((Some{Any},Bool)) do some, boundscheck
getfield(some, 1, boundscheck)
end |> Compiler.is_nothrow
@test Base.infer_effects((Some{Any},Bool)) do some, boundscheck
getfield(some, 1, :not_atomic, boundscheck)
end |> Compiler.is_nothrow
@test Base.infer_effects((Some{Any},Bool)) do some, boundscheck
getfield(some, 1, :invalid_atomic_spec, boundscheck)
end |> !Compiler.is_nothrow
@test Base.infer_effects((Some{Any},Any)) do some, boundscheck
getfield(some, 1, :not_atomic, boundscheck)
end |> !Compiler.is_nothrow
@test Compiler.is_consistent(Base.infer_effects(setindex!, (Base.RefValue{Int}, Int)))
# :inaccessiblememonly effect
const global constant_global::Int = 42
const global ConstantType = Ref
global nonconstant_global::Int = 42
const global constant_mutable_global = Ref(0)
const global constant_global_nonisbits = Some(:foo)
@test Base.infer_effects() do
constant_global
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
ConstantType
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
ConstantType{Any}()
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
constant_global_nonisbits
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
getglobal(@__MODULE__, :constant_global)
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
nonconstant_global
end |> !Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
getglobal(@__MODULE__, :nonconstant_global)
end |> !Compiler.is_inaccessiblememonly
@test Base.infer_effects((Symbol,)) do name
getglobal(@__MODULE__, name)
end |> !Compiler.is_inaccessiblememonly
@test Base.infer_effects((Int,)) do v
global nonconstant_global = v
end |> !Compiler.is_inaccessiblememonly
@test Base.infer_effects((Int,)) do v
setglobal!(@__MODULE__, :nonconstant_global, v)
end |> !Compiler.is_inaccessiblememonly
@test Base.infer_effects((Int,)) do v
constant_mutable_global[] = v
end |> !Compiler.is_inaccessiblememonly
module ConsistentModule
const global constant_global::Int = 42
const global ConstantType = Ref
end # module
@test Base.infer_effects() do
ConsistentModule.constant_global
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
ConsistentModule.ConstantType
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
ConsistentModule.ConstantType{Any}()
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
getglobal(@__MODULE__, :ConsistentModule).constant_global
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
getglobal(@__MODULE__, :ConsistentModule).ConstantType
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects() do
getglobal(@__MODULE__, :ConsistentModule).ConstantType{Any}()
end |> Compiler.is_inaccessiblememonly
@test Base.infer_effects((Module,)) do M
M.constant_global
end |> !Compiler.is_inaccessiblememonly
@test Base.infer_effects((Module,)) do M
M.ConstantType
end |> !Compiler.is_inaccessiblememonly
@test Base.infer_effects() do M
M.ConstantType{Any}()
end |> !Compiler.is_inaccessiblememonly
# the `:inaccessiblememonly` helper effect allows us to prove `:consistent`-cy of frames
# including `getfield` / `isdefined` accessing to local mutable object
mutable struct SafeRef{T}
x::T
end
Base.getindex(x::SafeRef) = x.x;
Base.setindex!(x::SafeRef, v) = x.x = v;
Base.isassigned(x::SafeRef) = true;
function mutable_consistent(s)
SafeRef(s)[]
end
@test Compiler.is_inaccessiblememonly(Base.infer_effects(mutable_consistent, (Symbol,)))
@test fully_eliminated(; retval=:foo) do
mutable_consistent(:foo)
end
function nested_mutable_consistent(s)
SafeRef(SafeRef(SafeRef(SafeRef(SafeRef(s)))))[][][][][]
end
@test Compiler.is_inaccessiblememonly(Base.infer_effects(nested_mutable_consistent, (Symbol,)))
@test fully_eliminated(; retval=:foo) do
nested_mutable_consistent(:foo)
end
const consistent_global = Some(:foo)
@test Base.infer_effects() do
consistent_global.value
end |> Compiler.is_consistent
const inconsistent_global = SafeRef(:foo)
@test Base.infer_effects() do
inconsistent_global[]
end |> !Compiler.is_consistent
const inconsistent_condition_ref = Ref{Bool}(false)
@test Base.infer_effects() do
if inconsistent_condition_ref[]
return 0
else
return 1
end
end |> !Compiler.is_consistent
# should handle va-method properly
callgetfield1(xs...) = getfield(getfield(xs, 1), 1)
@test !Compiler.is_inaccessiblememonly(Base.infer_effects(callgetfield1, (Base.RefValue{Symbol},)))
const GLOBAL_XS = Ref(:julia)
global_getfield() = callgetfield1(GLOBAL_XS)
@test let
Base.Experimental.@force_compile
global_getfield()
end === :julia
GLOBAL_XS[] = :julia2
@test let
Base.Experimental.@force_compile
global_getfield()
end === :julia2
# the `:inaccessiblememonly` helper effect allows us to prove `:effect_free`-ness of frames
# including `setfield!` modifying local mutable object
const global_ref = Ref{Any}()
global const global_bit::Int = 42
makeref() = Ref{Any}()
setref!(ref, @nospecialize v) = ref[] = v
@noinline function removable_if_unused1()
x = makeref()
setref!(x, 42)
x
end
@noinline function removable_if_unused2()
x = makeref()
setref!(x, global_bit)
x
end
for f = Any[removable_if_unused1, removable_if_unused2]
effects = Base.infer_effects(f)
@test Compiler.is_inaccessiblememonly(effects)
@test Compiler.is_effect_free(effects)
@test Compiler.is_removable_if_unused(effects)
@test @eval fully_eliminated() do
$f()
nothing
end
end
@noinline function removable_if_unused3(v)
x = makeref()
setref!(x, v)
x
end
let effects = Base.infer_effects(removable_if_unused3, (Int,))
@test Compiler.is_inaccessiblememonly(effects)
@test Compiler.is_effect_free(effects)
@test Compiler.is_removable_if_unused(effects)
end
@test fully_eliminated((Int,)) do v
removable_if_unused3(v)
nothing
end
@noinline function unremovable_if_unused1!(x)
setref!(x, 42)
end
@test !Compiler.is_removable_if_unused(Base.infer_effects(unremovable_if_unused1!, (typeof(global_ref),)))
@test !Compiler.is_removable_if_unused(Base.infer_effects(unremovable_if_unused1!, (Any,)))
@noinline function unremovable_if_unused2!()
setref!(global_ref, 42)
end
@test !Compiler.is_removable_if_unused(Base.infer_effects(unremovable_if_unused2!))
@noinline function unremovable_if_unused3!()
getfield(@__MODULE__, :global_ref)[] = nothing
end
@test !Compiler.is_removable_if_unused(Base.infer_effects(unremovable_if_unused3!))
# array ops
# =========
# allocation
# ----------
# low-level constructor
@noinline construct_array(@nospecialize(T), args...) = Array{T}(undef, args...)
# should eliminate safe but dead allocations
let good_dims = [1, 2, 3, 4, 10]
Ns = [1, 2, 3, 4, 10]
for dim = good_dims, N = Ns
Int64(dim)^N > typemax(Int) && continue
dims = ntuple(i->dim, N)
@test @eval Base.infer_effects() do
construct_array(Int, $(dims...))
end |> Compiler.is_removable_if_unused
@test @eval fully_eliminated() do
construct_array(Int, $(dims...))
nothing
end
end
end
# should analyze throwness correctly
let bad_dims = [-1, typemax(Int)]
for dim in bad_dims, N in [1, 2, 3, 4, 10]
for T in Any[Int, Union{Missing,Nothing}, Missing, Any]
dims = ntuple(i->dim, N)
@test @eval Base.infer_effects() do
construct_array($T, $(dims...))
end |> !Compiler.is_removable_if_unused
@test @eval !fully_eliminated() do
construct_array($T, $(dims...))
nothing
end
@test_throws "invalid " @eval construct_array($T, $(dims...))
end
end
end
# high-level interfaces
# getindex
for safesig = Any[
(Type{Int},)
(Type{Int}, Int)
(Type{Int}, Int, Int)
(Type{Number},)
(Type{Number}, Number)
(Type{Number}, Int)
(Type{Any},)
(Type{Any}, Any,)
(Type{Any}, Any, Any)
]
let effects = Base.infer_effects(getindex, safesig)
@test Compiler.is_consistent_if_notreturned(effects)
@test Compiler.is_removable_if_unused(effects)
end
end
for unsafesig = Any[
(Type{Int}, String)
(Type{Int}, Any)
(Type{Number}, AbstractString)
(Type{Number}, Any)
]
let effects = Base.infer_effects(getindex, unsafesig)
@test !Compiler.is_nothrow(effects)
end
end
# vect
for safesig = Any[
()
(Int,)
(Int, Int)
]
let effects = Base.infer_effects(Base.vect, safesig)
@test Compiler.is_consistent_if_notreturned(effects)
@test Compiler.is_removable_if_unused(effects)
end
end
# array getindex
let tt = (MemoryRef{Any},Symbol,Bool)
@testset let effects = Base.infer_effects(Core.memoryrefget, tt)
@test Compiler.is_consistent_if_inaccessiblememonly(effects)
@test Compiler.is_effect_free(effects)
@test !Compiler.is_nothrow(effects)
@test Compiler.is_terminates(effects)
end
end
# array setindex!
let tt = (MemoryRef{Any},Any,Symbol,Bool)
@testset let effects = Base.infer_effects(Core.memoryrefset!, tt)
@test Compiler.is_consistent_if_inaccessiblememonly(effects)
@test Compiler.is_effect_free_if_inaccessiblememonly(effects)
@test !Compiler.is_nothrow(effects)
@test Compiler.is_terminates(effects)
end
end
# nothrow for arrayset
@test Base.infer_effects((MemoryRef{Int},Int)) do a, v
Core.memoryrefset!(a, v, :not_atomic, true)
end |> !Compiler.is_nothrow
@test Base.infer_effects((MemoryRef{Int},Int)) do a, v
a[] = v # may throw
end |> !Compiler.is_nothrow
# when bounds checking is turned off, it should be safe
@test Base.infer_effects((MemoryRef{Int},Int)) do a, v
Core.memoryrefset!(a, v, :not_atomic, false)
end |> Compiler.is_nothrow
@test Base.infer_effects((MemoryRef{Number},Number)) do a, v
Core.memoryrefset!(a, v, :not_atomic, false)
end |> Compiler.is_nothrow
# arraysize
# ---------
let effects = Base.infer_effects(size, (Array,Int))
@test Compiler.is_consistent_if_inaccessiblememonly(effects)
@test Compiler.is_effect_free(effects)
@test !Compiler.is_nothrow(effects)
@test Compiler.is_terminates(effects)
end
# Test that arraysize has proper effect modeling
@test fully_eliminated(M->(size(M, 2); nothing), (Matrix{Float64},))
# arraylen
# --------
let effects = Base.infer_effects(length, (Vector{Any},))
@test Compiler.is_consistent_if_inaccessiblememonly(effects)
@test Compiler.is_effect_free(effects)
@test Compiler.is_nothrow(effects)
@test Compiler.is_terminates(effects)
end
# resize
# ------
#for op = Any[
# Base._growbeg!,
# Base._growend!,
# Base._deletebeg!,
# Base._deleteend!,
# ]
# let effects = Base.infer_effects(op, (Vector, Int))
# @test Compiler.is_effect_free_if_inaccessiblememonly(effects)
# @test Compiler.is_terminates(effects)
# @test !Compiler.is_nothrow(effects)
# end
#end
@test Compiler.is_noub(Base.infer_effects(Base._growbeg!, (Vector{Int}, Int)))
@test Compiler.is_noub(Base.infer_effects(Base._growbeg!, (Vector{Any}, Int)))
@test Compiler.is_noub(Base.infer_effects(Base._growend!, (Vector{Int}, Int)))
@test Compiler.is_noub(Base.infer_effects(Base._growend!, (Vector{Any}, Int)))
# tuple indexing
# --------------
@test Compiler.is_foldable(Base.infer_effects(iterate, Tuple{Tuple{Int, Int}, Int}))
# end to end
# ----------
#function simple_vec_ops(T, op!, op, xs...)
# a = T[]
# op!(a, xs...)
# return op(a)
#end
#for T = Any[Int,Any], op! = Any[push!,pushfirst!], op = Any[length,size],
# xs = Any[(Int,), (Int,Int,)]
# let effects = Base.infer_effects(simple_vec_ops, (Type{T},typeof(op!),typeof(op),xs...))
# @test Compiler.is_foldable(effects)
# end
#end
# Test that builtin_effects handles vararg correctly
@test !Compiler.is_nothrow(Compiler.builtin_effects(Compiler.fallback_lattice, Core.isdefined,
Any[String, Vararg{Any}], Bool))
# Test that :new can be eliminated even if an sparam is unknown
struct SparamUnused{T}
x
SparamUnused(x::T) where {T} = new{T}(x)
end
mksparamunused(x) = (SparamUnused(x); nothing)
let src = code_typed1(mksparamunused, (Any,))
@test count(isnew, src.code) == 0
end
struct WrapperOneField{T}
x::T
end
# Effects for getfield of type instance
@test Base.infer_effects(Tuple{Nothing}) do x
WrapperOneField{typeof(x)}.instance
end |> Compiler.is_foldable_nothrow
@test Base.infer_effects(Tuple{WrapperOneField{Float64}, Symbol}) do w, s
getfield(w, s)
end |> Compiler.is_foldable
@test Base.infer_effects(Tuple{WrapperOneField{Symbol}, Symbol}) do w, s
getfield(w, s)
end |> Compiler.is_foldable
# Flow-sensitive consistent for _typevar
@test Base.infer_effects() do
return WrapperOneField == (WrapperOneField{T} where T)
end |> Compiler.is_foldable_nothrow
# Test that dead `@inbounds` does not taint consistency
# https://github.com/JuliaLang/julia/issues/48243
@test Base.infer_effects(Tuple{Int64}) do i
false && @inbounds (1,2,3)[i]
return 1
end |> Compiler.is_foldable_nothrow
@test Base.infer_effects(Tuple{Int64}) do i
@inbounds (1,2,3)[i]
end |> !Compiler.is_noub
@test Base.infer_effects(Tuple{Tuple{Int64}}) do x
@inbounds x[1]
end |> Compiler.is_foldable_nothrow
# Test that :new of non-concrete, but otherwise known type
# does not taint consistency.
@eval struct ImmutRef{T}
x::T
ImmutRef(x) = $(Expr(:new, :(ImmutRef{typeof(x)}), :x))
end
@test Compiler.is_foldable(Base.infer_effects(ImmutRef, Tuple{Any}))
@test Compiler.is_foldable_nothrow(Base.infer_effects(typejoin, ()))
# nothrow-ness of subtyping operations
# https://github.com/JuliaLang/julia/pull/48566
@test !Compiler.is_nothrow(Base.infer_effects((A,B)->A<:B, (Any,Any)))
@test !Compiler.is_nothrow(Base.infer_effects((A,B)->A>:B, (Any,Any)))
# GotoIfNot should properly mark itself as throwing when given a non-Bool
# https://github.com/JuliaLang/julia/pull/48583
gotoifnot_throw_check_48583(x) = x ? x : 0
@test !Compiler.is_nothrow(Base.infer_effects(gotoifnot_throw_check_48583, (Missing,)))
@test !Compiler.is_nothrow(Base.infer_effects(gotoifnot_throw_check_48583, (Any,)))
@test Compiler.is_nothrow(Base.infer_effects(gotoifnot_throw_check_48583, (Bool,)))
# unknown :static_parameter should taint :nothrow
# https://github.com/JuliaLang/julia/issues/46771
unknown_sparam_throw(::Union{Nothing, Type{T}}) where T = (T; nothing)
unknown_sparam_nothrow1(x::Ref{T}) where T = (T; nothing)
unknown_sparam_nothrow2(x::Ref{Ref{T}}) where T = (T; nothing)
@test Compiler.is_nothrow(Base.infer_effects(unknown_sparam_throw, (Type{Int},)))
@test Compiler.is_nothrow(Base.infer_effects(unknown_sparam_throw, (Type{<:Integer},)))
@test !Compiler.is_nothrow(Base.infer_effects(unknown_sparam_throw, (Type,)))
@test !Compiler.is_nothrow(Base.infer_effects(unknown_sparam_throw, (Nothing,)))
@test !Compiler.is_nothrow(Base.infer_effects(unknown_sparam_throw, (Union{Type{Int},Nothing},)))
@test !Compiler.is_nothrow(Base.infer_effects(unknown_sparam_throw, (Any,)))
@test Compiler.is_nothrow(Base.infer_effects(unknown_sparam_nothrow1, (Ref,)))
@test Compiler.is_nothrow(Base.infer_effects(unknown_sparam_nothrow2, (Ref{Ref{T}} where T,)))
# purely abstract recursion should not taint :terminates
# https://github.com/JuliaLang/julia/issues/48983
abstractly_recursive1() = abstractly_recursive2()
abstractly_recursive2() = (Base._return_type(abstractly_recursive1, Tuple{}); 1)
abstractly_recursive3() = abstractly_recursive2()
@test_broken Compiler.is_terminates(Base.infer_effects(abstractly_recursive3, ()))
actually_recursive1(x) = actually_recursive2(x)
actually_recursive2(x) = (x <= 0) ? 1 : actually_recursive1(x - 1)
actually_recursive3(x) = actually_recursive2(x)
@test !Compiler.is_terminates(Base.infer_effects(actually_recursive3, (Int,)))
# `isdefined` effects
struct MaybeSome{T}
value::T
MaybeSome(x::T) where T = new{T}(x)
MaybeSome{T}(x::T) where T = new{T}(x)
MaybeSome{T}() where T = new{T}()
end
const undefined_ref = Ref{String}()
const defined_ref = Ref{String}("julia")
const undefined_some = MaybeSome{String}()
const defined_some = MaybeSome{String}("julia")
let effects = Base.infer_effects() do
isdefined(undefined_ref, :x)
end
@test !Compiler.is_consistent(effects)
@test Compiler.is_nothrow(effects)
end
let effects = Base.infer_effects() do
isdefined(defined_ref, :x)
end
@test !Compiler.is_consistent(effects)
@test Compiler.is_nothrow(effects)
end
let effects = Base.infer_effects() do
isdefined(undefined_some, :value)
end
@test Compiler.is_consistent(effects)
@test Compiler.is_nothrow(effects)
end
let effects = Base.infer_effects() do
isdefined(defined_some, :value)
end
@test Compiler.is_consistent(effects)
@test Compiler.is_nothrow(effects)
end
# high-level interface test
isassigned_effects(s) = isassigned(Ref(s))
@test Compiler.is_consistent(Base.infer_effects(isassigned_effects, (Symbol,)))
@test fully_eliminated(; retval=true) do
isassigned_effects(:foo)
end
# inference on throw block should be disabled only when the effects are already known to be
# concrete-eval ineligible:
function optimize_throw_block_for_effects(x)
a = [x]
if x < 0
throw(ArgumentError(lazy"negative number given: $x"))
end
return a
end
let effects = Base.infer_effects(optimize_throw_block_for_effects, (Int,))
@test Compiler.is_consistent_if_notreturned(effects)
@test Compiler.is_effect_free(effects)
@test !Compiler.is_nothrow(effects)