-
Notifications
You must be signed in to change notification settings - Fork 30
/
abstractinterpretation.jl
1289 lines (1190 loc) · 54.3 KB
/
abstractinterpretation.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 horrible because we don't have a stable `AbstractInterpreter` API yet
# TODO once https://github.com/JuliaLang/julia/pull/39305 gets merged, we can remove the
# overloadings of `abstract_call_gf_by_type` and `abstract_call_method_with_const_args`
# and keep their legacy definitions in a separate file for compatbility with older Julia versions
const IS_LATEST = isdefined(Core, :InterConditional)
"""
mutable struct AbstractGlobal
# analyzed type
t::Any
# `id` of `JETInterpreter` that defined this
id::Symbol
# `Symbol` of a dummy generic function that generates dummy backedge (i.e. `li`)
edge_sym::Symbol
# dummy backedge, which will be invalidated on update of `t`
li::MethodInstance
# whether this abstract global variable is declarared as constant or not
iscd::Bool
end
Wraps a global variable whose type is analyzed by abtract interpretation.
`AbstractGlobal` object will be actually evaluated into the context module, and a later
analysis may refer to its type or alter it on another assignment.
On the refinement of the abstract global variable, the dummy backedge associated with it
will be invalidated, and inference depending on that will be re-run on the next analysis.
"""
mutable struct AbstractGlobal
# analyzed type
t::Any
# `id` of `JETInterpreter` that lastly assigned this global variable
id::Symbol
# the name of a dummy generic function that generates dummy backedge `li`
edge_sym::Symbol
# dummy backedge associated to this global variable, which will be invalidated on update of `t`
li::MethodInstance
# whether this abstract global variable is declarared as constant or not
iscd::Bool
function AbstractGlobal(@nospecialize(t),
id::Symbol,
edge_sym::Symbol,
li::MethodInstance,
iscd::Bool,
)
return new(t, id, edge_sym, li, iscd)
end
end
"""
function overload_abstract_call_gf_by_type!()
...
end
push_inithook!(overload_abstract_call_gf_by_type!)
the aims of this overload are:
1. report `NoMethodErrorReport` on empty method signature matching
2. keep inference on non-concrete call sites in toplevel frame created by [`virtual_process!`](@ref)
3. don't bail out even after the current return type grows up to `Any` and collect as much
error points as possible; of course it slows down inference performance, but hopefully it
stays to be "practical" speed (because the number of matching methods is limited beforehand)
4. force constant prop' even if the inference result can't be improved anymore when `rettype`
is already `Const`; this is because constant prop' can still produce more "correct"
analysis by throwing away the error reports in the callee frames
5. always add backedges (even if a new method can't refine the return type grew up to`Any`),
because a new method always may change the JET analysis result
"""
function overload_abstract_call_gf_by_type!()
# %% for easier interactive update of abstract_call_gf_by_type
ex = @static if IS_LATEST; quote
# TODO:
# - report "too many method matched"
# - maybe "cound not identify method table for call" won't happen since we eagerly propagate bottom for e.g. undef var case, etc.
function abstract_call_gf_by_type(interp::$JETInterpreter, @nospecialize(f),
fargs::Union{Nothing,Vector{Any}}, argtypes::Vector{Any}, @nospecialize(atype),
sv::InferenceState, max_methods::Int = InferenceParams(interp).MAX_METHODS)
if sv.params.unoptimize_throw_blocks && sv.currpc in sv.throw_blocks
return CallMeta(Any, false)
end
valid_worlds = WorldRange()
atype_params = unwrap_unionall(atype).parameters
splitunions = 1 < unionsplitcost(atype_params) <= InferenceParams(interp).MAX_UNION_SPLITTING
mts = Core.MethodTable[]
fullmatch = Bool[]
if splitunions
splitsigs = switchtupleunion(atype)
applicable = Any[]
infos = MethodMatchInfo[]
for sig_n in splitsigs
mt = ccall(:jl_method_table_for, Any, (Any,), sig_n)
if mt === nothing
add_remark!(interp, sv, "Could not identify method table for call")
return CallMeta(Any, false)
end
mt = mt::Core.MethodTable
matches = findall(sig_n, method_table(interp); limit=max_methods)
if matches === missing
add_remark!(interp, sv, "For one of the union split cases, too many methods matched")
return CallMeta(Any, false)
end
#=== abstract_call_gf_by_type patch point 1-1 start ===#
info = MethodMatchInfo(matches)
if $is_empty_match(info)
# report `NoMethodErrorReport` for union-split signatures
$report!(interp, $NoMethodErrorReport(interp, sv, true, atype))
end
push!(infos, info)
#=== abstract_call_gf_by_type patch point 1-1 end ===#
append!(applicable, matches)
valid_worlds = intersect(valid_worlds, matches.valid_worlds)
thisfullmatch = _any(match->(match::MethodMatch).fully_covers, matches)
found = false
for (i, mt′) in enumerate(mts)
if mt′ === mt
fullmatch[i] &= thisfullmatch
found = true
break
end
end
if !found
push!(mts, mt)
push!(fullmatch, thisfullmatch)
end
end
info = UnionSplitInfo(infos)
else
mt = ccall(:jl_method_table_for, Any, (Any,), atype)
if mt === nothing
add_remark!(interp, sv, "Could not identify method table for call")
return CallMeta(Any, false)
end
mt = mt::Core.MethodTable
matches = findall(atype, method_table(interp, sv); limit=max_methods)
if matches === missing
# this means too many methods matched
# (assume this will always be true, so we don't compute / update valid age in this case)
add_remark!(interp, sv, "Too many methods matched")
return CallMeta(Any, false)
end
push!(mts, mt)
push!(fullmatch, _any(match->(match::MethodMatch).fully_covers, matches))
info = MethodMatchInfo(matches)
#=== abstract_call_gf_by_type patch point 1-2 start ===#
if $is_empty_match(info)
# report `NoMethodErrorReport` for this call signature
$report!(interp, $NoMethodErrorReport(interp, sv, false, atype))
end
#=== abstract_call_gf_by_type patch point 1-2 end ===#
applicable = matches.matches
valid_worlds = matches.valid_worlds
end
update_valid_age!(sv, valid_worlds)
applicable = applicable::Array{Any,1}
napplicable = length(applicable)
rettype = Bottom
edgecycle = false
edges = MethodInstance[]
conditionals = nothing # keeps refinement information of call argument types when the return type is boolean
nonbot = 0 # the index of the only non-Bottom inference result if > 0
seen = 0 # number of signatures actually inferred
multiple_matches = napplicable > 1
if f !== nothing && napplicable == 1 && is_method_pure(applicable[1]::MethodMatch)
val = pure_eval_call(f, argtypes)
if val !== false
# TODO: add some sort of edge(s)
return CallMeta(val, MethodResultPure())
end
end
#=== abstract_call_gf_by_type patch point 4-1 start ===#
nreports = length(interp.reports)
#=== abstract_call_gf_by_type patch point 4-1 end ===#
for i in 1:napplicable
match = applicable[i]::MethodMatch
method = match.method
sig = match.spec_types
if bail_out_toplevel_call(interp, sig, sv)
# only infer concrete call sites in top-level expressions
add_remark!(interp, sv, "Refusing to infer non-concrete call site in top-level expression")
rettype = Any
break
end
sigtuple = unwrap_unionall(sig)::DataType
this_rt = Bottom
splitunions = false
# TODO: splitunions = 1 < unionsplitcost(sigtuple.parameters) * napplicable <= InferenceParams(interp).MAX_UNION_SPLITTING
# this used to trigger a bug in inference recursion detection, and is unmaintained now
if splitunions
splitsigs = switchtupleunion(sig)
for sig_n in splitsigs
rt, edgecycle1, edge = abstract_call_method(interp, method, sig_n, svec(), multiple_matches, sv)
edgecycle |= edgecycle1::Bool
if edge !== nothing
push!(edges, edge)
end
this_rt = tmerge(this_rt, rt)
if bail_out_call(interp, this_rt, sv)
break
end
end
else
this_rt, edgecycle1, edge = abstract_call_method(interp, method, sig, match.sparams, multiple_matches, sv)
edgecycle |= edgecycle1::Bool
if edge !== nothing
push!(edges, edge)
end
end
this_conditional = ignorelimited(this_rt)
this_rt = widenwrappedconditional(this_rt)
@assert !(this_conditional isa Conditional) "invalid lattice element returned from inter-procedural context"
if this_rt !== Bottom
if nonbot === 0
nonbot = i
else
nonbot = -1
end
end
seen += 1
rettype = tmerge(rettype, this_rt)
if bail_out_call(interp, rettype, sv)
break
end
if this_conditional !== Bottom && is_lattice_bool(rettype) && fargs !== nothing
if conditionals === nothing
conditionals = Any[Bottom for _ in 1:length(argtypes)],
Any[Bottom for _ in 1:length(argtypes)]
end
condval = maybe_extract_const_bool(this_conditional)
for i = 1:length(argtypes)
fargs[i] isa Slot || continue
if this_conditional isa InterConditional && this_conditional.slot == i
vtype = this_conditional.vtype
elsetype = this_conditional.elsetype
else
elsetype = vtype = tmeet(argtypes[i], fieldtype(sig, i))
condval === true && (elsetype = Union{})
condval === false && (vtype = Union{})
end
conditionals[1][i] = tmerge(conditionals[1][i], vtype)
conditionals[2][i] = tmerge(conditionals[2][i], elsetype)
end
end
end
#=== abstract_call_gf_by_type patch point 4-2 start ===#
# check if constant propagation can improve analysis by throwing away possibly false positive reports
has_been_reported = (length(interp.reports) - nreports) > 0
#=== abstract_call_gf_by_type patch point 4-2 end ===#
# try constant propagation if only 1 method is inferred to non-Bottom
# this is in preparation for inlining, or improving the return result
is_unused = call_result_unused(sv)
#=== abstract_call_gf_by_type patch point 4-3 start ===#
if nonbot > 0 && seen == napplicable && (!edgecycle || !is_unused) &&
(is_improvable(rettype) || has_been_reported) && InferenceParams(interp).ipo_constant_propagation
#=== abstract_call_gf_by_type patch point 4-3 end ===#
# if there's a possibility we could constant-propagate a better result
# (hopefully without doing too much work), try to do that now
# TODO: refactor this, enable constant propagation for each (union-split) signature
match = applicable[nonbot]::MethodMatch
const_rettype, result = abstract_call_method_with_const_args(interp, rettype, f, argtypes, applicable[nonbot]::MethodMatch, sv, edgecycle)
const_conditional = ignorelimited(const_rettype)
@assert !(const_conditional isa Conditional) "invalid lattice element returned from inter-procedural context"
const_rettype = widenwrappedconditional(const_rettype)
if ignorelimited(const_rettype) ⊑ rettype
# use the better result, if it is a refinement of rettype
rettype = const_rettype
if const_conditional isa InterConditional && conditionals === nothing && fargs !== nothing
arg = fargs[const_conditional.slot]
if arg isa Slot
rettype = Conditional(arg, const_conditional.vtype, const_conditional.elsetype)
if const_rettype isa LimitedAccuracy
rettype = LimitedAccuracy(rettype, const_rettype.causes)
end
end
end
end
if result !== nothing
info = ConstCallInfo(info, result)
end
# and update refinements with the InterConditional info too
# (here we ignorelimited, since there isn't much below this in the
# lattice, particularly when we're already using tmeet)
if const_conditional isa InterConditional && conditionals !== nothing
let i = const_conditional.slot,
vtype = const_conditional.vtype,
elsetype = const_conditional.elsetype
if !(vtype ⊑ conditionals[1][i])
vtype = tmeet(conditionals[1][i], widenconst(vtype))
end
if !(elsetype ⊑ conditionals[2][i])
elsetype = tmeet(conditionals[2][i], widenconst(elsetype))
end
conditionals[1][i] = vtype
conditionals[2][i] = elsetype
end
end
end
if rettype isa LimitedAccuracy
union!(sv.pclimitations, rettype.causes)
rettype = rettype.typ
end
# if we have argument refinement information, apply that now to get the result
if is_lattice_bool(rettype) && conditionals !== nothing && fargs !== nothing
slot = 0
vtype = elsetype = Any
condval = maybe_extract_const_bool(rettype)
for i in 1:length(fargs)
# find the first argument which supports refinment,
# and intersect all equvalent arguments with it
arg = fargs[i]
arg isa Slot || continue # can't refine
old = argtypes[i]
old isa Type || continue # unlikely to refine
id = slot_id(arg)
if slot == 0 || id == slot
new_vtype = conditionals[1][i]
if condval === false
vtype = Union{}
elseif new_vtype ⊑ vtype
vtype = new_vtype
else
vtype = tmeet(vtype, widenconst(new_vtype))
end
new_elsetype = conditionals[2][i]
if condval === true
elsetype = Union{}
elseif new_elsetype ⊑ elsetype
elsetype = new_elsetype
else
elsetype = tmeet(elsetype, widenconst(new_elsetype))
end
if (slot > 0 || condval !== false) && !(old ⊑ vtype) # essentially vtype ⋤ old
slot = id
elseif (slot > 0 || condval !== true) && !(old ⊑ elsetype) # essentially elsetype ⋤ old
slot = id
else # reset: no new useful information for this slot
vtype = elsetype = Any
if slot > 0
slot = 0
end
end
end
end
if vtype === Bottom && elsetype === Bottom
rettype = Bottom # accidentally proved this call to be dead / throw !
elseif slot > 0
rettype = Conditional(SlotNumber(slot), vtype, elsetype) # record a Conditional improvement to this slot
end
end
@assert !(rettype isa InterConditional) "invalid lattice element returned from inter-procedural context"
if is_unused && !(rettype === Bottom)
add_remark!(interp, sv, "Call result type was widened because the return value is unused")
# We're mainly only here because the optimizer might want this code,
# but we ourselves locally don't typically care about it locally
# (beyond checking if it always throws).
# So avoid adding an edge, since we don't want to bother attempting
# to improve our result even if it does change (to always throw),
# and avoid keeping track of a more complex result type.
rettype = Any
end
add_call_backedges!(interp, rettype, edges, fullmatch, mts, atype, sv)
if !isempty(sv.pclimitations) # remove self, if present
delete!(sv.pclimitations, sv)
for caller in sv.callers_in_cycle
delete!(sv.pclimitations, caller)
end
end
$analyze_task_parallel_code!(interp, f, argtypes, sv)
#print("=> ", rettype, "\n")
return CallMeta(rettype, info)
end
end; else; quote # @static if IS_LATEST; quote
function abstract_call_gf_by_type(interp::$JETInterpreter, @nospecialize(f), argtypes::Vector{Any}, @nospecialize(atype), sv::InferenceState,
max_methods::Int = InferenceParams(interp).MAX_METHODS)
if sv.params.unoptimize_throw_blocks && sv.currpc in sv.throw_blocks
return CallMeta(Any, false)
end
valid_worlds = WorldRange()
atype_params = unwrap_unionall(atype).parameters
splitunions = 1 < unionsplitcost(atype_params) <= InferenceParams(interp).MAX_UNION_SPLITTING
mts = Core.MethodTable[]
fullmatch = Bool[]
if splitunions
splitsigs = switchtupleunion(atype)
applicable = Any[]
infos = MethodMatchInfo[]
for sig_n in splitsigs
mt = ccall(:jl_method_table_for, Any, (Any,), sig_n)
if mt === nothing
add_remark!(interp, sv, "Could not identify method table for call")
return CallMeta(Any, false)
end
mt = mt::Core.MethodTable
matches = findall(sig_n, method_table(interp); limit=max_methods)
if matches === missing
add_remark!(interp, sv, "For one of the union split cases, too many methods matched")
return CallMeta(Any, false)
end
#=== abstract_call_gf_by_type patch point 1-1 start ===#
info = MethodMatchInfo(matches)
if $is_empty_match(info)
# report `NoMethodErrorReport` for union-split signatures
$report!(interp, $NoMethodErrorReport(interp, sv, true, atype))
end
push!(infos, info)
#=== abstract_call_gf_by_type patch point 1-1 end ===#
append!(applicable, matches)
valid_worlds = intersect(valid_worlds, matches.valid_worlds)
thisfullmatch = _any(match->(match::MethodMatch).fully_covers, matches)
found = false
for (i, mt′) in enumerate(mts)
if mt′ === mt
fullmatch[i] &= thisfullmatch
found = true
break
end
end
if !found
push!(mts, mt)
push!(fullmatch, thisfullmatch)
end
end
info = UnionSplitInfo(infos)
else
mt = ccall(:jl_method_table_for, Any, (Any,), atype)
if mt === nothing
add_remark!(interp, sv, "Could not identify method table for call")
return CallMeta(Any, false)
end
mt = mt::Core.MethodTable
matches = findall(atype, method_table(interp, sv); limit=max_methods)
if matches === missing
# this means too many methods matched
# (assume this will always be true, so we don't compute / update valid age in this case)
add_remark!(interp, sv, "Too many methods matched")
return CallMeta(Any, false)
end
push!(mts, mt)
push!(fullmatch, _any(match->(match::MethodMatch).fully_covers, matches))
info = MethodMatchInfo(matches)
#=== abstract_call_gf_by_type patch point 1-2 start ===#
if $is_empty_match(info)
# report `NoMethodErrorReport` for this call signature
$report!(interp, $NoMethodErrorReport(interp, sv, false, atype))
end
#=== abstract_call_gf_by_type patch point 1-2 end ===#
applicable = matches.matches
valid_worlds = matches.valid_worlds
end
update_valid_age!(sv, valid_worlds)
applicable = applicable::Array{Any,1}
napplicable = length(applicable)
rettype = Bottom
edgecycle = false
edges = MethodInstance[]
nonbot = 0 # the index of the only non-Bottom inference result if > 0
seen = 0 # number of signatures actually inferred
istoplevel = sv.linfo.def isa Module
multiple_matches = napplicable > 1
if f !== nothing && napplicable == 1 && is_method_pure(applicable[1]::MethodMatch)
val = pure_eval_call(f, argtypes)
if val !== false
# TODO: add some sort of edge(s)
return CallMeta(val, MethodResultPure())
end
end
#=== abstract_call_gf_by_type patch point 4-1 start ===#
nreports = length(interp.reports)
#=== abstract_call_gf_by_type patch point 4-1 end ===#
for i in 1:napplicable
match = applicable[i]::MethodMatch
method = match.method
sig = match.spec_types
#=== abstract_call_gf_by_type patch point 2 start ===#
if istoplevel && !isdispatchtuple(sig) && !$istoplevel(sv) # keep going for "our" toplevel frame
#=== abstract_call_gf_by_type patch point 2 end ===#
# only infer concrete call sites in top-level expressions
add_remark!(interp, sv, "Refusing to infer non-concrete call site in top-level expression")
rettype = Any
break
end
sigtuple = unwrap_unionall(sig)::DataType
splitunions = false
this_rt = Bottom
# TODO: splitunions = 1 < unionsplitcost(sigtuple.parameters) * napplicable <= InferenceParams(interp).MAX_UNION_SPLITTING
# currently this triggers a bug in inference recursion detection
if splitunions
splitsigs = switchtupleunion(sig)
for sig_n in splitsigs
rt, edgecycle1, edge = abstract_call_method(interp, method, sig_n, svec(), multiple_matches, sv)
if edge !== nothing
push!(edges, edge)
end
edgecycle |= edgecycle1::Bool
this_rt = tmerge(this_rt, rt)
#=== abstract_call_gf_by_type patch point 3-1 start ===#
# this_rt === Any && break # keep going and collect as much error reports as possible
#=== abstract_call_gf_by_type patch point 3-1 end ===#
end
else
this_rt, edgecycle1, edge = abstract_call_method(interp, method, sig, match.sparams, multiple_matches, sv)
edgecycle |= edgecycle1::Bool
if edge !== nothing
push!(edges, edge)
end
end
if this_rt !== Bottom
if nonbot === 0
nonbot = i
else
nonbot = -1
end
end
seen += 1
rettype = tmerge(rettype, this_rt)
#=== abstract_call_gf_by_type patch point 3-2 start ===#
# rettype === Any && break # keep going and collect as much error reports as possible
#=== abstract_call_gf_by_type patch point 3-2 end ===#
end
#=== abstract_call_gf_by_type patch point 4-2 start ===#
# check if constant propagation can improve analysis by throwing away possibly false positive reports
has_been_reported = (length(interp.reports) - nreports) > 0
#=== abstract_call_gf_by_type patch point 4-2 end ===#
# try constant propagation if only 1 method is inferred to non-Bottom
# this is in preparation for inlining, or improving the return result
is_unused = call_result_unused(sv)
#=== abstract_call_gf_by_type patch point 4-3 start ===#
if nonbot > 0 && seen == napplicable && (!edgecycle || !is_unused) &&
(is_improvable(rettype) || has_been_reported) && InferenceParams(interp).ipo_constant_propagation
#=== abstract_call_gf_by_type patch point 4-3 end ===#
# if there's a possibility we could constant-propagate a better result
# (hopefully without doing too much work), try to do that now
# TODO: it feels like this could be better integrated into abstract_call_method / typeinf_edge
const_rettype = abstract_call_method_with_const_args(interp, rettype, f, argtypes, applicable[nonbot]::MethodMatch, sv, edgecycle)
if const_rettype ⊑ rettype
# use the better result, if it's a refinement of rettype
rettype = const_rettype
end
end
if is_unused && !(rettype === Bottom)
add_remark!(interp, sv, "Call result type was widened because the return value is unused")
# We're mainly only here because the optimizer might want this code,
# but we ourselves locally don't typically care about it locally
# (beyond checking if it always throws).
# So avoid adding an edge, since we don't want to bother attempting
# to improve our result even if it does change (to always throw),
# and avoid keeping track of a more complex result type.
rettype = Any
end
#=== abstract_call_gf_by_type patch point 5 start ===#
# a new method may refine analysis, so we always add backedges
if true # !(rettype === Any) # adding a new method couldn't refine (widen) this type
#=== abstract_call_gf_by_type patch point 5 end ===#
for edge in edges
add_backedge!(edge::MethodInstance, sv)
end
for (thisfullmatch, mt) in zip(fullmatch, mts)
if !thisfullmatch
# also need an edge to the method table in case something gets
# added that did not intersect with any existing method
add_mt_backedge!(mt, atype, sv)
end
end
end
#print("=> ", rettype, "\n")
$(isdefined(CC, :LimitedAccuracy) && quote
if rettype isa LimitedAccuracy
union!(sv.pclimitations, rettype.causes)
rettype = rettype.typ
end
if !isempty(sv.pclimitations) # remove self, if present
delete!(sv.pclimitations, sv)
for caller in sv.callers_in_cycle
delete!(sv.pclimitations, caller)
end
end
end)
$analyze_task_parallel_code!(interp, f, argtypes, sv)
return CallMeta(rettype, info)
end
end; end # @static if IS_LATEST; quote
Core.eval(CC, ex)
# %% for easier interactive update of abstract_call_gf_by_type
end # function overload_abstract_call_gf_by_type!()
push_inithook!(overload_abstract_call_gf_by_type!)
function is_empty_match(info)
res = info.results
isa(res, MethodLookupResult) || return false # when does this happen ?
return isempty(res.matches)
end
@static if IS_LATEST
import .CC:
bail_out_call,
bail_out_toplevel_call,
add_call_backedges!
# keep going and collect as much error reports as possible
bail_out_call(interp::JETInterpreter, @nospecialize(t), sv) = false
function bail_out_toplevel_call(interp::JETInterpreter, @nospecialize(sig), sv)
return isa(sv.linfo.def, Module) && !isdispatchtuple(sig) && !istoplevel(sv)
end
function add_call_backedges!(interp::JETInterpreter,
@nospecialize(rettype),
edges::Vector{MethodInstance},
fullmatch::Vector{Bool}, mts::Vector{Core.MethodTable}, @nospecialize(atype),
sv::InferenceState)
# a new method may refine analysis, so we always add backedges
# if rettype === Any
# # for `NativeInterpreter`, we don't add backedges when a new method couldn't refine
# # (widen) this type
# return
# end
for edge in edges
add_backedge!(edge, sv)
end
for (thisfullmatch, mt) in zip(fullmatch, mts)
if !thisfullmatch
# also need an edge to the method table in case something gets
# added that did not intersect with any existing method
add_mt_backedge!(mt, atype, sv)
end
end
end
end # @static if IS_LATEST
# add special cased analysis pass for task parallelism (xref: https://github.com/aviatesk/JET.jl/issues/114)
# in Julia's task parallelism implementation, parallel code is represented as closure
# and it's wrapped in `Task` object
# `NativeInterpreter` doesn't run type inference nor optimization on the body of those closures
# when compiling code that creates parallel tasks, but JET will try to run additional
# analysis pass by recurring into the closures
# NOTE JET won't do anything other than doing JET analysis, e.g. won't annotate return type
# of wrapped code block in order to not confuse the original `AbstractInterpreter` routine
# track https://github.com/JuliaLang/julia/pull/39773 for the changes in native abstract interpretation routine
function analyze_task_parallel_code!(interp::JETInterpreter, @nospecialize(f), argtypes::Vector{Any}, sv::InferenceState)
# TODO ideally JET should analyze a closure wrapped in a `Task` only when encontering `schedule` call on it
# but the `Task` construction may not happen in the same frame where `schedule` is called,
# and so we may not be able to access to the closure at the point
# as a compromise, JET now invokes the additional analysis on `Task` construction,
# regardless of whether it's really `schedule`d or not
if f === Task &&
length(argtypes) ≥ 2 &&
(v = argtypes[2]; v ⊑ Function)
# if we encounter `Task(::Function)`, try to get its inner function and run analysis on it
# the closure can be a nullary lambda that really doesn't depend on
# the captured environment, and in that case we can retrieve it as
# a function object, otherwise we will try to retrieve the type of the closure
ft = (isa(v, Const) ? Core.Typeof(v.val) :
isa(v, Core.PartialStruct) ? v.typ :
isa(v, DataType) ? v :
return)::Type
return profile_additional_pass_by_type!(interp, Tuple{ft}, sv)
end
return
end
# run additional interpretation with a new interpreter,
# and then append the reports to the original interpreter
function profile_additional_pass_by_type!(interp::JETInterpreter, @nospecialize(tt::Type{<:Tuple}), sv::InferenceState)
newinterp = JETInterpreter(interp)
# in order to preserve the inference termination, we keep to use the current frame
# and borrow the `AbstractInterpreter`'s cycle detection logic
# XXX the additional analysis pass by `abstract_call_method` may involve various site-effects,
# but what we're doing here is essentially equivalent to modifying the user code and inlining
# the threaded code block as a usual code block, and thus the side-effects won't (hopefully)
# confuse the abstract interpretation, which is supposed to terminate on any kind of code
# while we just run an additional analysis pass and don't record a result of the call
# for later, there still may be a risk to produce an invalid code after type inference
mm = get_single_method_match(tt, InferenceParams(newinterp).MAX_METHODS, get_world_counter(newinterp))
abstract_call_method(newinterp, mm.method, mm.spec_types, mm.sparams, false, sv)
append!(interp.reports, newinterp.reports)
end
"""
function overload_abstract_call_method_with_const_args!()
...
end
push_inithook!(overload_abstract_call_method_with_const_args!)
the aim of this overloads is:
1. force constant prop' even if the inference result can't be improved anymore when `rettype`
is already `Const`; this is because constant prop' can still produce more "correct"
analysis by throwing away the error reports in the callee frames
"""
function overload_abstract_call_method_with_const_args!()
# %% for easier interactive update of abstract_call_method_with_const_args
ex = @static if IS_LATEST; quote
function abstract_call_method_with_const_args(interp::$JETInterpreter, @nospecialize(rettype), @nospecialize(f), argtypes::Vector{Any}, match::MethodMatch, sv::InferenceState, edgecycle::Bool)
method = match.method
nargs::Int = method.nargs
method.isva && (nargs -= 1)
length(argtypes) >= nargs || return Any, nothing
haveconst = false
allconst = true
# see if any or all of the arguments are constant and propagating constants may be worthwhile
for a in argtypes
a = widenconditional(a)
if allconst && !isa(a, Const) && !isconstType(a) && !isa(a, PartialStruct) && !isa(a, PartialOpaque)
allconst = false
end
if !haveconst && has_nontrivial_const_info(a) && const_prop_profitable(a)
haveconst = true
end
if haveconst && !allconst
break
end
end
#=== abstract_call_method_with_const_args patch point 1 start ===#
# force constant propagation even if it doesn't improve return type;
# constant prop' may improve report accuracy
haveconst || #= improvable_via_constant_propagation(rettype) || =# return Any, nothing
#=== abstract_call_method_with_const_args patch point 1 end ===#
force_inference = method.aggressive_constprop || InferenceParams(interp).aggressive_constant_propagation
if !force_inference && nargs > 1
if istopfunction(f, :getindex) || istopfunction(f, :setindex!)
arrty = argtypes[2]
# don't propagate constant index into indexing of non-constant array
if arrty isa Type && arrty <: AbstractArray && !issingletontype(arrty)
return Any, nothing
elseif arrty ⊑ Array
return Any, nothing
end
elseif istopfunction(f, :iterate)
itrty = argtypes[2]
if itrty ⊑ Array
return Any, nothing
end
end
end
if !force_inference && !allconst &&
(istopfunction(f, :+) || istopfunction(f, :-) || istopfunction(f, :*) ||
istopfunction(f, :(==)) || istopfunction(f, :!=) ||
istopfunction(f, :<=) || istopfunction(f, :>=) || istopfunction(f, :<) || istopfunction(f, :>) ||
istopfunction(f, :<<) || istopfunction(f, :>>))
# it is almost useless to inline the op of when all the same type,
# but highly worthwhile to inline promote of a constant
length(argtypes) > 2 || return Any, nothing
t1 = widenconst(argtypes[2])
all_same = true
for i in 3:length(argtypes)
if widenconst(argtypes[i]) !== t1
all_same = false
break
end
end
all_same && return Any, nothing
end
if istopfunction(f, :getproperty) || istopfunction(f, :setproperty!)
force_inference = true
end
force_inference |= allconst
mi = specialize_method(match, !force_inference)
mi === nothing && return Any, nothing
mi = mi::MethodInstance
# decide if it's likely to be worthwhile
if !force_inference && !const_prop_heuristic(interp, method, mi)
return Any, nothing
end
inf_cache = get_inference_cache(interp)
inf_result = cache_lookup(mi, argtypes, inf_cache)
if inf_result === nothing
if edgecycle
# if there might be a cycle, check to make sure we don't end up
# calling ourselves here.
infstate = sv
cyclei = 0
while !(infstate === nothing)
if method === infstate.linfo.def && any(infstate.result.overridden_by_const)
return Any, nothing
end
if cyclei < length(infstate.callers_in_cycle)
cyclei += 1
infstate = infstate.callers_in_cycle[cyclei]
else
cyclei = 0
infstate = infstate.parent
end
end
end
inf_result = InferenceResult(mi, argtypes)
frame = InferenceState(inf_result, #=cache=#false, interp)
frame === nothing && return Any, nothing # this is probably a bad generated function (unsound), but just ignore it
frame.parent = sv
push!(inf_cache, inf_result)
typeinf(interp, frame) || return Any, nothing
end
result = inf_result.result
# if constant inference hits a cycle, just bail out
isa(result, InferenceState) && return Any, nothing
#=== abstract_call_method_with_const_args patch point 3 start ===#
add_backedge!(mi, sv)
$update_reports!(interp, sv)
#=== abstract_call_method_with_const_args patch point 3 end ===#
return result, inf_result
end
end; else; quote # @static if IS_LATEST; quote
function abstract_call_method_with_const_args(interp::$JETInterpreter, @nospecialize(rettype), @nospecialize(f), argtypes::Vector{Any}, match::MethodMatch, sv::InferenceState, edgecycle::Bool)
method = match.method
nargs::Int = method.nargs
method.isva && (nargs -= 1)
length(argtypes) >= nargs || return Any
haveconst = false
allconst = true
# see if any or all of the arguments are constant and propagating constants may be worthwhile
for a in argtypes
a = widenconditional(a)
if allconst && !isa(a, Const) && !isconstType(a) && !isa(a, PartialStruct)
allconst = false
end
if !haveconst && has_nontrivial_const_info(a) && const_prop_profitable(a)
haveconst = true
end
if haveconst && !allconst
break
end
end
#=== abstract_call_method_with_const_args patch point 1 start ===#
# force constant propagation even if it doesn't improve return type;
# constant prop' may improve report accuracy
haveconst || #= improvable_via_constant_propagation(rettype) || =# return Any
#=== abstract_call_method_with_const_args patch point 1 end ===#
force_inference = $(hasfield(Method, :aggressive_constprop) ? :(method.aggressive_constprop) : false) || InferenceParams(interp).aggressive_constant_propagation
if !force_inference && nargs > 1
if istopfunction(f, :getindex) || istopfunction(f, :setindex!)
arrty = argtypes[2]
# don't propagate constant index into indexing of non-constant array
if arrty isa Type && arrty <: AbstractArray && !issingletontype(arrty)
return Any
elseif arrty ⊑ Array
return Any
end
elseif istopfunction(f, :iterate)
itrty = argtypes[2]
if itrty ⊑ Array
return Any
end
end
end
if !force_inference && !allconst &&
(istopfunction(f, :+) || istopfunction(f, :-) || istopfunction(f, :*) ||
istopfunction(f, :(==)) || istopfunction(f, :!=) ||
istopfunction(f, :<=) || istopfunction(f, :>=) || istopfunction(f, :<) || istopfunction(f, :>) ||
istopfunction(f, :<<) || istopfunction(f, :>>))
# it is almost useless to inline the op of when all the same type,
# but highly worthwhile to inline promote of a constant
length(argtypes) > 2 || return Any
t1 = widenconst(argtypes[2])
all_same = true
for i in 3:length(argtypes)
if widenconst(argtypes[i]) !== t1
all_same = false
break
end
end
all_same && return Any
end
if istopfunction(f, :getproperty) || istopfunction(f, :setproperty!)
force_inference = true
end
force_inference |= allconst
mi = specialize_method(match, !force_inference)
mi === nothing && return Any
mi = mi::MethodInstance
# decide if it's likely to be worthwhile
if !force_inference && !const_prop_heuristic(interp, method, mi)
return Any
end
inf_cache = get_inference_cache(interp)
inf_result = cache_lookup(mi, argtypes, inf_cache)
if inf_result === nothing
if edgecycle
# if there might be a cycle, check to make sure we don't end up
# calling ourselves here.
infstate = sv
cyclei = 0
while !(infstate === nothing)
if method === infstate.linfo.def && any(infstate.result.overridden_by_const)
return Any
end
if cyclei < length(infstate.callers_in_cycle)
cyclei += 1
infstate = infstate.callers_in_cycle[cyclei]
else
cyclei = 0
infstate = infstate.parent
end
end
end
inf_result = InferenceResult(mi, argtypes)
frame = InferenceState(inf_result, #=cache=#false, interp)
frame === nothing && return Any # this is probably a bad generated function (unsound), but just ignore it
$(isdefined(CC, :LimitedAccuracy) || :(frame.limited = true))
frame.parent = sv
push!(inf_cache, inf_result)
typeinf(interp, frame) || return Any
end
result = inf_result.result
# if constant inference hits a cycle, just bail out
isa(result, InferenceState) && return Any
#=== abstract_call_method_with_const_args patch point 3 start ===#
add_backedge!(mi, sv)
$update_reports!(interp, sv)
#=== abstract_call_method_with_const_args patch point 3 end ===#
return result
end
end; end # @static if IS_LATEST; quote
Core.eval(CC, ex)
# %% for easier interactive update of abstract_call_method_with_const_args
end # function overload_abstract_call_method_with_const_args!()
push_inithook!(overload_abstract_call_method_with_const_args!)
# works within inter-procedural context
function CC.abstract_call_method(interp::JETInterpreter, method::Method, @nospecialize(sig), sparams::SimpleVector, hardlimit::Bool, sv::InferenceState)
ret = @invoke abstract_call_method(interp::AbstractInterpreter, method::Method, sig, sparams::SimpleVector, hardlimit::Bool, sv::InferenceState)
update_reports!(interp, sv)
return ret
end
function update_reports!(interp::JETInterpreter, sv::InferenceState)
rs = interp.to_be_updated
if !isempty(rs)
vf = get_virtual_frame(interp, sv)
for r in rs
pushfirst!(r.st, vf)
end
empty!(rs)
end
end
@static if IS_LATEST
import .CC:
abstract_invoke,
InvokeCallInfo,
instanceof_tfunc
function CC.abstract_invoke(interp::JETInterpreter, argtypes::Vector{Any}, sv::InferenceState)
ret = @invoke abstract_invoke(interp::AbstractInterpreter, argtypes::Vector{Any}, sv::InferenceState)
if ret.rt === Bottom
# here we report error that happens at the call of `invoke` itself.
# if the error type (`Bottom`) is propagated from the `invoke`d call, the error has
# already been reported within `typeinf_edge`, so ignore that case
if !isa(ret.info, InvokeCallInfo)
report!(interp, InvalidInvokeErrorReport(interp, sv, argtypes))
end
end
return ret
end
@reportdef InvalidInvokeErrorReport(interp, sv, argtypes::Vector{Any})
function get_msg(::Type{InvalidInvokeErrorReport}, interp, sv, argtypes::Vector{Any})
fallback_msg = "invalid invoke" # mostly because of runtime unreachable
ft = widenconst(argtype_by_index(argtypes, 2))
ft === Bottom && return fallback_msg
t = argtype_by_index(argtypes, 3)
(types, isexact, isconcrete, istype) = instanceof_tfunc(t)