-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathirpasses.jl
992 lines (934 loc) · 29 KB
/
irpasses.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
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Test
using Base.Meta
using Core: PhiNode, SSAValue, GotoNode, PiNode, QuoteNode, ReturnNode, GotoIfNot
# Tests for domsort
## Test that domsort doesn't mangle single-argument phis (#29262)
let m = Meta.@lower 1 + 1
@assert Meta.isexpr(m, :thunk)
src = m.args[1]::Core.CodeInfo
src.code = Any[
# block 1
Expr(:call, :opaque),
GotoIfNot(Core.SSAValue(1), 10),
# block 2
Core.PhiNode(Int32[8], Any[Core.SSAValue(7)]), # <- This phi must not get replaced by %7
Core.PhiNode(Int32[2, 8], Any[true, false]),
GotoIfNot(Core.SSAValue(1), 7),
# block 3
Expr(:call, :+, Core.SSAValue(3), 1),
# block 4
Core.PhiNode(Int32[5, 6], Any[0, Core.SSAValue(6)]),
Expr(:call, >, Core.SSAValue(7), 10),
GotoIfNot(Core.SSAValue(8), 3),
# block 5
Core.PhiNode(Int32[2, 8], Any[0, Core.SSAValue(7)]),
ReturnNode(Core.SSAValue(10)),
]
nstmts = length(src.code)
src.ssavaluetypes = nstmts
src.codelocs = fill(Int32(1), nstmts)
src.ssaflags = fill(Int32(0), nstmts)
ir = Core.Compiler.inflate_ir(src)
Core.Compiler.verify_ir(ir)
domtree = Core.Compiler.construct_domtree(ir.cfg.blocks)
ir = Core.Compiler.domsort_ssa!(ir, domtree)
Core.Compiler.verify_ir(ir)
phi = ir.stmts.inst[3]
@test isa(phi, Core.PhiNode) && length(phi.edges) == 1
end
# test that we don't stack-overflow in SNCA with large functions.
let m = Meta.@lower 1 + 1
@assert Meta.isexpr(m, :thunk)
src = m.args[1]::Core.CodeInfo
code = Any[]
N = 2^15
for i in 1:2:N
push!(code, Expr(:call, :opaque))
push!(code, GotoIfNot(Core.SSAValue(i), N+2)) # skip one block
end
# all goto here
push!(code, Expr(:call, :opaque))
push!(code, ReturnNode(nothing))
src.code = code
nstmts = length(src.code)
src.ssavaluetypes = nstmts
src.codelocs = fill(Int32(1), nstmts)
src.ssaflags = fill(Int32(0), nstmts)
ir = Core.Compiler.inflate_ir(src)
Core.Compiler.verify_ir(ir)
domtree = Core.Compiler.construct_domtree(ir.cfg.blocks)
ir = Core.Compiler.domsort_ssa!(ir, domtree)
Core.Compiler.verify_ir(ir)
end
# Tests for SROA
import Core.Compiler: argextype, singleton_type
const EMPTY_SPTYPES = Any[]
code_typed1(args...; kwargs...) = first(only(code_typed(args...; kwargs...)))::Core.CodeInfo
get_code(args...; kwargs...) = code_typed1(args...; kwargs...).code
# check if `x` is a statement with a given `head`
isnew(@nospecialize x) = Meta.isexpr(x, :new)
# check if `x` is a dynamic call of a given function
iscall(y) = @nospecialize(x) -> iscall(y, x)
function iscall((src, f)::Tuple{Core.CodeInfo,Function}, @nospecialize(x))
return iscall(x) do @nospecialize x
singleton_type(argextype(x, src, EMPTY_SPTYPES)) === f
end
end
iscall(pred::Function, @nospecialize(x)) = Meta.isexpr(x, :call) && pred(x.args[1])
struct ImmutableXYZ; x; y; z; end
mutable struct MutableXYZ; x; y; z; end
struct ImmutableOuter{T}; x::T; y::T; z::T; end
mutable struct MutableOuter{T}; x::T; y::T; z::T; end
# should optimize away very basic cases
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = ImmutableXYZ(x, y, z)
xyz.x, xyz.y, xyz.z
end
@test !any(isnew, src.code)
end
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = MutableXYZ(x, y, z)
xyz.x, xyz.y, xyz.z
end
@test !any(isnew, src.code)
end
# should handle simple mutabilities
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = MutableXYZ(x, y, z)
xyz.y = 42
xyz.x, xyz.y, xyz.z
end
@test !any(isnew, src.code)
@test any(src.code) do @nospecialize x
iscall((src, tuple), x) &&
x.args[2:end] == Any[#=x=# Core.Argument(2), 42, #=x=# Core.Argument(4)]
end
end
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = MutableXYZ(x, y, z)
xyz.x, xyz.z = xyz.z, xyz.x
xyz.x, xyz.y, xyz.z
end
@test !any(isnew, src.code)
@test any(src.code) do @nospecialize x
iscall((src, tuple), x) &&
x.args[2:end] == Any[#=z=# Core.Argument(4), #=y=# Core.Argument(3), #=x=# Core.Argument(2)]
end
end
# circumvent uninitialized fields as far as there is a solid `setfield!` definition
let src = code_typed1() do
r = Ref{Any}()
r[] = 42
return r[]
end
@test !any(isnew, src.code)
end
let src = code_typed1((Bool,)) do cond
r = Ref{Any}()
if cond
r[] = 42
return r[]
else
r[] = 32
return r[]
end
end
@test !any(isnew, src.code)
end
let src = code_typed1((Bool,)) do cond
r = Ref{Any}()
if cond
r[] = 42
else
r[] = 32
end
return r[]
end
@test !any(isnew, src.code)
end
let src = code_typed1((Bool,Bool,Any,Any,Any)) do c1, c2, x, y, z
r = Ref{Any}()
if c1
if c2
r[] = x
else
r[] = y
end
else
r[] = z
end
return r[]
end
@test !any(isnew, src.code)
end
let src = code_typed1((Bool,)) do cond
r = Ref{Any}()
if cond
r[] = 42
end
return r[]
end
# N.B. `r` should be allocated since `cond` might be `false` and then it will be thrown
@test any(isnew, src.code)
end
let src = code_typed1((Bool,Bool,Any,Any)) do c1, c2, x, y
r = Ref{Any}()
if c1
if c2
r[] = x
end
else
r[] = y
end
return r[]
end
# N.B. `r` should be allocated since `c2` might be `false` and then it will be thrown
@test any(isnew, src.code)
end
# alias analysis
# --------------
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = ImmutableXYZ(x, y, z)
outer = ImmutableOuter(xyz, xyz, xyz)
outer.x.x, outer.y.y, outer.z.z
end
@test !any(src.code) do @nospecialize x
Meta.isexpr(x, :new)
end
@test any(src.code) do @nospecialize x
iscall((src, tuple), x) &&
x.args[2:end] == Any[#=x=# Core.Argument(2), #=y=# Core.Argument(3), #=y=# Core.Argument(4)]
end
end
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = ImmutableXYZ(x, y, z)
# #42831 forms ::PartialStruct(ImmutableOuter{Any}, Any[ImmutableXYZ, ImmutableXYZ, ImmutableXYZ])
# so the succeeding `getproperty`s are type stable and inlined
outer = ImmutableOuter{Any}(xyz, xyz, xyz)
outer.x.x, outer.y.y, outer.z.z
end
@test !any(isnew, src.code)
@test any(src.code) do @nospecialize x
iscall((src, tuple), x) &&
x.args[2:end] == Any[#=x=# Core.Argument(2), #=y=# Core.Argument(3), #=y=# Core.Argument(4)]
end
end
# FIXME? in order to handle nested mutable `getfield` calls, we run SROA iteratively until
# any nested mutable `getfield` calls become no longer eliminable:
# it's probably not the most efficient option and we may want to introduce some sort of
# alias analysis and eliminates all the loads at once.
# mutable(immutable(...)) case
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = MutableXYZ(x, y, z)
t = (xyz,)
v = t[1].x
v, v, v
end
@test !any(isnew, src.code)
end
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = MutableXYZ(x, y, z)
outer = ImmutableOuter(xyz, xyz, xyz)
outer.x.x, outer.y.y, outer.z.z
end
@test !any(isnew, src.code)
@test any(src.code) do @nospecialize x
iscall((src, tuple), x) &&
x.args[2:end] == Any[#=x=# Core.Argument(2), #=y=# Core.Argument(3), #=y=# Core.Argument(4)]
end
end
let # this is a simple end to end test case, which demonstrates allocation elimination
# by handling `mutable[RefValue{String}](immutable[Tuple](...))` case correctly
# NOTE this test case isn't so robust and might be subject to future changes of the broadcasting implementation,
# in that case you don't really need to stick to keeping this test case around
simple_sroa(s) = broadcast(identity, Ref(s))
s = Base.inferencebarrier("julia")::String
simple_sroa(s)
# NOTE don't hard-code `"julia"` in `@allocated` clause and make sure to execute the
# compiled code for `simple_sroa`, otherwise everything can be folded even without SROA
@test @allocated(simple_sroa(s)) == 0
end
# immutable(mutable(...)) case
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = ImmutableXYZ(x, y, z)
outer = MutableOuter(xyz, xyz, xyz)
outer.x.x, outer.y.y, outer.z.z
end
@test !any(isnew, src.code)
@test any(src.code) do @nospecialize x
iscall((src, tuple), x) &&
x.args[2:end] == Any[#=x=# Core.Argument(2), #=y=# Core.Argument(3), #=y=# Core.Argument(4)]
end
end
# mutable(mutable(...)) case
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = MutableXYZ(x, y, z)
outer = MutableOuter(xyz, xyz, xyz)
outer.x.x, outer.y.y, outer.z.z
end
@test !any(isnew, src.code)
@test any(src.code) do @nospecialize x
iscall((src, tuple), x) &&
x.args[2:end] == Any[#=x=# Core.Argument(2), #=y=# Core.Argument(3), #=y=# Core.Argument(4)]
end
end
let src = code_typed1((Any,Any,Any)) do x, y, z
xyz = MutableXYZ(x, y, z)
inner = MutableOuter(xyz, xyz, xyz)
outer = MutableOuter(inner, inner, inner)
outer.x.x.x, outer.y.y.y, outer.z.z.z
end
@test !any(isnew, src.code)
@test any(src.code) do @nospecialize x
iscall((src, tuple), x) &&
x.args[2:end] == Any[#=x=# Core.Argument(2), #=y=# Core.Argument(3), #=y=# Core.Argument(4)]
end
end
let # NOTE `sroa_mutables!` eliminate from innermost definitions, so that it should be able
# to fully eliminate this insanely nested example
src = code_typed1((Int,)) do x
(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref((x))))))))))))[][][][][][][][][][]
end
@test !any(isnew, src.code)
end
# ϕ-allocation elimination
# ------------------------
mutable struct MutableSome
x::Any
MutableSome(@nospecialize x) = new(x)
MutableSome() = new()
end
Base.getindex(s::MutableSome) = s.x
Base.setindex!(s::MutableSome, @nospecialize x) = s.x = x
@testset "mutable ϕ-allocation elimination" begin
# safe cases
let src = code_typed1((Bool,Any,Any)) do cond, x, y
if cond
ϕ = MutableSome(x)
else
ϕ = MutableSome(y)
end
ϕ[]
end
@test !any(isnew, src.code)
@test count(src.code) do @nospecialize x
isa(x, Core.PhiNode) &&
#=x=# Core.Argument(3) in x.values &&
#=y=# Core.Argument(4) in x.values
end == 1
end
let src = code_typed1((Bool,Bool,Any,Any,Any)) do cond1, cond2, x, y, z
if cond1
ϕ = MutableSome(x)
elseif cond2
ϕ = MutableSome(y)
else
ϕ = MutableSome(z)
end
ϕ[]
end
@test !any(isnew, src.code)
@test count(src.code) do @nospecialize x
isa(x, Core.PhiNode) &&
#=x=# Core.Argument(4) in x.values &&
#=y=# Core.Argument(5) in x.values &&
#=z=# Core.Argument(6) in x.values
end == 1
end
let src = code_typed1((Bool,Any,Any,Any)) do cond, x, y, z
if cond
ϕ = MutableSome(x)
else
ϕ = MutableSome(y)
ϕ[] = z
end
ϕ[]
end
@test !any(isnew, src.code)
@test !any(iscall((src, setfield!)), src.code)
@test count(src.code) do @nospecialize x
isa(x, Core.PhiNode) &&
#=x=# Core.Argument(3) in x.values &&
#=z=# Core.Argument(5) in x.values
end == 1
end
let src = code_typed1((Bool,Any,Any,Any)) do cond, x, y, z
if cond
ϕ = MutableSome(x)
else
ϕ = MutableSome(y)
end
ϕ[] = z
ϕ[]
end
@test !any(isnew, src.code)
@test !any(iscall((src, setfield!)), src.code)
@test count(src.code) do @nospecialize x
isa(x, Core.ReturnNode) &&
#=z=# Core.Argument(5) === x.val
end == 1
end
let src = code_typed1((Bool,Any,Any,)) do cond, x, y
if cond
ϕ = MutableSome(x)
out1 = ϕ[]
else
ϕ = MutableSome(y)
out1 = ϕ[]
end
out2 = ϕ[]
out1, out2
end
@test !any(isnew, src.code)
@test count(src.code) do @nospecialize x
isa(x, Core.PhiNode) &&
#=x=# Core.Argument(3) in x.values &&
#=y=# Core.Argument(4) in x.values
end == 2
end
let src = code_typed1((Bool,Any,Any,Any)) do cond, x, y, z
if cond
ϕ = MutableSome(x)
out1 = ϕ[]
else
ϕ = MutableSome(y)
out1 = ϕ[]
ϕ[] = z
end
out2 = ϕ[]
out1, out2
end
@test !any(isnew, src.code)
@test !any(iscall((src, setfield!)), src.code)
@test count(src.code) do @nospecialize x
isa(x, Core.PhiNode) &&
#=x=# Core.Argument(3) in x.values &&
#=y=# Core.Argument(4) in x.values
end == 1
@test count(src.code) do @nospecialize x
isa(x, Core.PhiNode) &&
#=x=# Core.Argument(3) in x.values &&
#=z=# Core.Argument(5) in x.values
end == 1
end
# unsafe cases
let src = code_typed1((Bool,Any,Any)) do cond, x, y
if cond
ϕ = MutableSome(x)
else
ϕ = MutableSome(y)
end
some_escape(ϕ)
ϕ[]
end
@test count(isnew, src.code) == 2
end
let src = code_typed1((Bool,Any,Any)) do cond, x, y
if cond
ϕ = MutableSome(x)
some_escape(ϕ)
else
ϕ = MutableSome(y)
end
ϕ[]
end
@test count(isnew, src.code) == 2
end
let src = code_typed1((Bool,Any,)) do cond, x
if cond
ϕ = MutableSome(x)
else
ϕ = MutableSome()
end
ϕ[]
end
@test count(isnew, src.code) == 2
end
let src = code_typed1((Bool,Any,Any)) do cond, x, y
if cond
ϕ = MutableSome(x)
else
ϕ = MutableSome()
ϕ[] = y
end
ϕ[]
end
@test !any(isnew, src.code)
@test !any(iscall((src, setfield!)), src.code)
@test count(src.code) do @nospecialize x
isa(x, Core.PhiNode) &&
#=x=# Core.Argument(3) in x.values &&
#=y=# Core.Argument(4) in x.values
end == 1
end
# FIXME allocation forming multiple ϕ
let src = code_typed1((Bool,Any,Any)) do cond, x, y
if cond
ϕ2 = ϕ1 = MutableSome(x)
else
ϕ2 = ϕ1 = MutableSome(y)
end
ϕ1[], ϕ2[]
end
@test_broken !any(isnew, src.code)
@test_broken count(src.code) do @nospecialize x
isa(x, Core.PhiNode) &&
#=x=# Core.Argument(3) in x.values &&
#=y=# Core.Argument(4) in x.values
end == 1
end
end
function mutable_ϕ_elim(x, xs)
r = Ref(x)
for x in xs
r = Ref(x)
end
return r[]
end
let xs = String[string(gensym()) for _ in 1:100]
mutable_ϕ_elim("init", xs)
@test @allocated(mutable_ϕ_elim("init", xs)) == 0
end
# should work nicely with inlining to optimize away a complicated case
# adapted from http://wiki.luajit.org/Allocation-Sinking-Optimization#implementation%5B
struct Point
x::Float64
y::Float64
end
#=@inline=# add(a::Point, b::Point) = Point(a.x + b.x, a.y + b.y)
function compute()
a = Point(1.5, 2.5)
b = Point(2.25, 4.75)
for i in 0:(100000000-1)
a = add(add(a, b), b)
end
a.x, a.y
end
let src = code_typed1(compute)
@test !any(isnew, src.code)
end
mutable struct Foo30594; x::Float64; end
Base.copy(x::Foo30594) = Foo30594(x.x)
function add!(p::Foo30594, off::Foo30594)
p.x += off.x
return p
end
Base.:(+)(a::Foo30594, b::Foo30594) = add!(copy(a), b)
let results = Float64[]
@noinline use30594(x) = push!(results, x.x); nothing
function foo30594(cnt::Int, dx::Int)
step = Foo30594(dx)
curr = step + Foo30594(1)
for i in 1:cnt
use30594(curr)
curr = curr + step
end
nothing
end
foo30594(4, -1)
@test results == [0.0, -1.0, -2.0, -3.0]
end
# Issue #29983
# This one is a bit hard to trigger, but the key is to create a case
# where SROA needs to introduce an intermediate type-unstable phi node
struct Foo29983{T}
x::Tuple{T}
end
struct Bar29983{S}
x::S
end
Base.:+(a::T, b::Bar29983{S}) where {T, S} = Bar29983(a + b.x)
Base.:+(a::Bar29983{S}, b::T) where {T, S} = b + a
Base.:+(a::Bar29983{S}, b::Bar29983{T}) where {T, S} = Bar29983(a.x + b.x)
Base.:+(a::Foo29983, b::Foo29983) = Foo29983((a.x[1] + b.x[1],))
function f(x::Vector{T}) where {T}
x1 = Foo29983((x[1],))
la1 = Foo29983((x[1],))
f1 = Foo29983((0,))
for _ in 1:2
f1 += la1
end
return f1
end
@test f([Bar29983(1.0)]).x[1].x == 2.0
# Issue #31139 - Checking for correct number of arguments in getfield elim
let nt = (a=1, b=2)
blah31139(x) = getfield(x)
# Shouldn't throw
@test isa(code_typed(blah31139, Tuple{typeof(nt)}), Array)
# Should throw
@test_throws ArgumentError blah31139(nt)
end
# Expr(:new) annotated as PartialStruct
struct FooPartial
x
y
global f_partial
f_partial(x) = new(x, 2).x
end
let ci = code_typed(f_partial, Tuple{Float64})[1].first
@test length(ci.code) == 1 && isa(ci.code[1], ReturnNode)
end
# A SSAValue after the compaction line
let m = Meta.@lower 1 + 1
@assert Meta.isexpr(m, :thunk)
src = m.args[1]::Core.CodeInfo
src.code = Any[
# block 1
nothing,
# block 2
PhiNode(Int32[1, 7], Any[Core.Argument(2), SSAValue(9)]),
Expr(:call, isa, SSAValue(2), UnionAll),
GotoIfNot(Core.SSAValue(3), 11),
# block 3
nothing,
nothing,
PiNode(SSAValue(2), UnionAll),
Expr(:call, getfield, SSAValue(7), QuoteNode(:body)),
SSAValue(8), # <-- This SSAValue is the problem.
# SROA needs to propagate the old taint when it follows
# the phinode here
GotoNode(2),
# block 5
ReturnNode(Core.SSAValue(2)),
]
src.ssavaluetypes = Any[
Nothing,
Any,
Bool,
Any,
Nothing,
Nothing,
UnionAll,
Any,
Any,
Any,
Any
]
nstmts = length(src.code)
src.codelocs = fill(Int32(1), nstmts)
src.ssaflags = fill(Int32(0), nstmts)
ir = Core.Compiler.inflate_ir(src, Any[], Any[Any, Any])
@test Core.Compiler.verify_ir(ir) === nothing
ir = @test_nowarn Core.Compiler.sroa_pass!(ir)
@test Core.Compiler.verify_ir(ir) === nothing
end
# Issue #31546 - missing widenconst in SROA
function f_31546(x)
(a, b) = x == "r" ? (false, false) :
x == "r+" ? (true, false) :
x == "w" ? (true, true) : error()
return a, b
end
@test f_31546("w") == (true, true)
# Tests for cfg simplification
let src = code_typed(gcd, Tuple{Int, Int})[1].first
# Test that cfg_simplify doesn't mangle IR on code with loops
ir = Core.Compiler.inflate_ir(src)
Core.Compiler.verify_ir(ir)
ir = Core.Compiler.cfg_simplify!(ir)
Core.Compiler.verify_ir(ir)
end
let m = Meta.@lower 1 + 1
# Test that CFG simplify combines redundant basic blocks
@assert Meta.isexpr(m, :thunk)
src = m.args[1]::Core.CodeInfo
src.code = Any[
Core.Compiler.GotoNode(2),
Core.Compiler.GotoNode(3),
Core.Compiler.GotoNode(4),
Core.Compiler.GotoNode(5),
Core.Compiler.GotoNode(6),
Core.Compiler.GotoNode(7),
ReturnNode(2)
]
nstmts = length(src.code)
src.ssavaluetypes = nstmts
src.codelocs = fill(Int32(1), nstmts)
src.ssaflags = fill(Int32(0), nstmts)
ir = Core.Compiler.inflate_ir(src)
Core.Compiler.verify_ir(ir)
ir = Core.Compiler.cfg_simplify!(ir)
Core.Compiler.verify_ir(ir)
ir = Core.Compiler.compact!(ir)
@test length(ir.cfg.blocks) == 1 && Core.Compiler.length(ir.stmts) == 1
end
let m = Meta.@lower 1 + 1
# Test that CFG simplify doesn't mess up when chaining past return blocks
@assert Meta.isexpr(m, :thunk)
src = m.args[1]::Core.CodeInfo
src.code = Any[
Core.Compiler.GotoIfNot(Core.Compiler.Argument(2), 3),
Core.Compiler.GotoNode(4),
ReturnNode(1),
Core.Compiler.GotoNode(5),
Core.Compiler.GotoIfNot(Core.Compiler.Argument(2), 7),
# This fall through block of the previous GotoIfNot
# must be moved up along with it, when we merge it
# into the goto 4 block.
ReturnNode(2),
ReturnNode(3)
]
nstmts = length(src.code)
src.ssavaluetypes = nstmts
src.codelocs = fill(Int32(1), nstmts)
src.ssaflags = fill(Int32(0), nstmts)
ir = Core.Compiler.inflate_ir(src)
Core.Compiler.verify_ir(ir)
ir = Core.Compiler.cfg_simplify!(ir)
Core.Compiler.verify_ir(ir)
@test length(ir.cfg.blocks) == 5
ret_2 = ir.stmts.inst[ir.cfg.blocks[3].stmts[end]]
@test isa(ret_2, Core.Compiler.ReturnNode) && ret_2.val == 2
end
let m = Meta.@lower 1 + 1
# Test that CFG simplify doesn't try to merge every block in a loop into
# its predecessor
@assert Meta.isexpr(m, :thunk)
src = m.args[1]::Core.CodeInfo
src.code = Any[
# Block 1
Core.Compiler.GotoNode(2),
# Block 2
Core.Compiler.GotoNode(3),
# Block 3
Core.Compiler.GotoNode(1)
]
nstmts = length(src.code)
src.ssavaluetypes = nstmts
src.codelocs = fill(Int32(1), nstmts)
src.ssaflags = fill(Int32(0), nstmts)
ir = Core.Compiler.inflate_ir(src)
Core.Compiler.verify_ir(ir)
ir = Core.Compiler.cfg_simplify!(ir)
Core.Compiler.verify_ir(ir)
@test length(ir.cfg.blocks) == 1
end
# Issue #29213
function f_29213()
while true
try
break
finally
end
end
while 1==1
try
ed = (_not_defined,)
finally
break
end
end
ed = string(ed)
end
@test_throws UndefVarError f_29213()
function test_29253(K)
if true
try
error()
catch e
end
end
size(K,1)
end
let K = rand(2,2)
@test test_29253(K) == 2
end
function no_op_refint(r)
r[]
return
end
let code = code_typed(no_op_refint,Tuple{Base.RefValue{Int}})[1].first.code
@test length(code) == 1
@test isa(code[1], Core.ReturnNode)
@test code[1].val === nothing
end
# check getfield elim handling of GlobalRef
const _some_coeffs = (1,[2],3,4)
splat_from_globalref(x) = (x, _some_coeffs...,)
@test splat_from_globalref(0) == (0, 1, [2], 3, 4)
function pi_on_argument(x)
if isa(x, Core.Argument)
return x.n
end
return -2
end
let code = code_typed(pi_on_argument, Tuple{Any})[1].first.code,
nisa = 0, found_pi = false
for stmt in code
if Meta.isexpr(stmt, :call)
callee = stmt.args[1]
if (callee === isa || callee === :isa || (isa(callee, GlobalRef) &&
callee.name === :isa))
nisa += 1
end
elseif stmt === Core.PiNode(Core.Argument(2), Core.Argument)
found_pi = true
end
end
@test nisa == 1
@test found_pi
end
# issue #38936
# check that getfield elim can handle unions of tuple types
mutable struct S38936{T} content::T end
struct PrintAll{T} <: Function
parts::T
end
function (f::PrintAll)(io::IO)
for x in f.parts
print(io, x)
end
end
let f = PrintAll((S38936("<span>"), "data", S38936("</span")))
@test !any(code_typed(f, (IOBuffer,))[1][1].code) do stmt
stmt isa Expr && stmt.head === :call && stmt.args[1] === GlobalRef(Core, :tuple)
end
end
exc39508 = ErrorException("expected")
@noinline function test39508()
local err
try
err = exc39508::Exception
throw(err)
false
catch ex
@test ex === err
end
return err
end
@test test39508() === exc39508
let # `sroa_pass!` should work with constant globals
# immutable pass
src = @eval Module() begin
const REF_FLD = :x
struct ImmutableRef{T}
x::T
end
code_typed((Int,)) do x
r = ImmutableRef{Int}(x) # should be eliminated
x = getfield(r, REF_FLD) # should be eliminated
return sin(x)
end |> only |> first
end
@test !any(src.code) do @nospecialize(stmt)
Meta.isexpr(stmt, :call) || return false
ft = Core.Compiler.argextype(stmt.args[1], src, EMPTY_SPTYPES)
return Core.Compiler.widenconst(ft) == typeof(getfield)
end
@test !any(src.code) do @nospecialize(stmt)
return Meta.isexpr(stmt, :new)
end
# mutable pass
src = @eval Module() begin
const REF_FLD = :x
code_typed() do
r = Ref{Int}(42) # should be eliminated
x = getfield(r, REF_FLD) # should be eliminated
return sin(x)
end |> only |> first
end
@test !any(src.code) do @nospecialize(stmt)
Meta.isexpr(stmt, :call) || return false
ft = Core.Compiler.argextype(stmt.args[1], src, EMPTY_SPTYPES)
return Core.Compiler.widenconst(ft) == typeof(getfield)
end
@test !any(src.code) do @nospecialize(stmt)
return Meta.isexpr(stmt, :new)
end
end
let
# `typeassert` elimination after SROA
# NOTE we can remove this optimization once inference is able to reason about memory-effects
src = @eval Module() begin
mutable struct Foo; x; end
code_typed((Int,)) do a
x1 = Foo(a)
x2 = Foo(x1)
return typeassert(x2.x, Foo).x
end |> only |> first
end
# eliminate `typeassert(x2.x, Foo)`
@test all(src.code) do @nospecialize stmt
Meta.isexpr(stmt, :call) || return true
ft = Core.Compiler.argextype(stmt.args[1], src, EMPTY_SPTYPES)
return Core.Compiler.widenconst(ft) !== typeof(typeassert)
end
end
let
# Test for https://github.com/JuliaLang/julia/issues/43402
# Ensure that structs required not used outside of the ccall,
# still get listed in the ccall_preserves
src = @eval Module() begin
@inline function effectful()
s1 = Ref{Csize_t}()
s2 = Ref{Csize_t}()
ccall(:some_ccall, Cvoid,
(Ref{Csize_t},Ref{Csize_t}),
s1, s2)
return s1[], s2[]
end
code_typed() do
s1, s2 = effectful()
return s1
end |> only |> first
end
refs = map(Core.SSAValue, findall(x->x isa Expr && x.head == :new, src.code))
some_ccall = findfirst(x -> x isa Expr && x.head == :foreigncall && x.args[1] == :(:some_ccall), src.code)
@assert some_ccall !== nothing
stmt = src.code[some_ccall]
nccallargs = length(stmt.args[3]::Core.SimpleVector)
preserves = stmt.args[6+nccallargs:end]
@test length(refs) == 2
@test length(preserves) == 2
@test all(alloc -> alloc in preserves, refs)
end
# test `stmt_effect_free` and DCE
# ===============================
function fully_eliminated(f, args)
@nospecialize 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)
@nospecialize f args
let code = code_typed(f, args)[1][1].code
return length(code) == 1 && isa(code[1], ReturnNode) && code[1].val == retval
end
end
let # effect-freeness computation for array allocation
# should eliminate dead allocations
good_dims = (0, 2)
for dim in good_dims, N in 0:10
dims = ntuple(i->dim, N)
@eval @test fully_eliminated(()) do
Array{Int,$N}(undef, $(dims...))
nothing
end
end
# shouldn't eliminate errorneous dead allocations
bad_dims = [-1, # should keep "invalid Array dimensions"
typemax(Int)] # should keep "invalid Array size"
for dim in bad_dims, N in 1:10
dims = ntuple(i->dim, N)
@eval @test !fully_eliminated(()) do
Array{Int,$N}(undef, $(dims...))
nothing
end
end
# some high-level examples
@test fully_eliminated(()) do
Int[]
nothing
end
@test fully_eliminated(()) do
Matrix{Tuple{String,String}}(undef, 4, 4)
nothing
end
@test fully_eliminated(()) do
IdDict{Any,Any}()
nothing
end
end