File: exclusivity_static_diagnostics.sil

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (1631 lines) | stat: -rw-r--r-- 84,362 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
// RUN: %target-sil-opt -enable-sil-verify-all %s -enforce-exclusivity=unchecked -diagnose-static-exclusivity -verify | %FileCheck %s

sil_stage raw

import Builtin
import Swift


sil @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
sil @takesOneInout : $@convention(thin) (@inout Int) -> ()
sil @makesInt : $@convention(thin) () -> Int
sil @takesInoutAndNoEscapeClosure : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_owned () -> ()) -> ()
sil @takesInoutAndNoEscapeClosureTakingArgument : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_owned (Int) -> ()) -> ()
sil @takesInoutAndNoEscapeGuaranteedClosureTakingArgument : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_guaranteed (Int) -> ()) -> ()
sil @takesInoutAndNoEscapeClosureWithGenericReturn : $@convention(thin) <T> (@inout Int, @noescape @callee_owned (Int) -> @out T) -> ()
sil @takesInoutAndNoEscapeBlockClosure : $@convention(thin) (@inout Int, @owned @convention(block) @noescape () -> ()) -> ()
sil @takesInoutAndNoEscapeOptionalBlockClosure : $@convention(thin) (@inout Int, @owned Optional<@convention(block) @noescape () -> ()>) -> ()

// CHECK-LABEL: sil hidden [ossa] @twoLocalInoutsDisaliased
sil hidden [ossa] @twoLocalInoutsDisaliased : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %5 = alloc_box ${ var Int }
  %6 = project_box %5 : ${ var Int }, 0
  store %0 to [trivial] %6 : $*Int
  %8 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %9 = begin_access [modify] [unknown] %3 : $*Int
  %10 = begin_access [modify] [unknown] %6 : $*Int  // no-error
  %11 = apply %8(%9, %10) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %10 : $*Int
  end_access %9: $*Int
  destroy_value %5 : ${ var Int }
  destroy_value %2 : ${ var Int }
  %14 = tuple ()
  return %14 : $()
}

// CHECK-LABEL: sil hidden [ossa] @twoLocalInoutsSimpleAliasing
sil hidden [ossa] @twoLocalInoutsSimpleAliasing : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = begin_access [modify] [unknown] %3 : $*Int  // expected-note {{conflicting access is here}}
  %7 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %6 : $*Int
  end_access %5: $*Int
  destroy_value %2 : ${ var Int }
  %8 = tuple ()
  return %8 : $()
}

// CHECK-LABEL: sil hidden [ossa] @conflictingPriorAccess
sil hidden [ossa] @conflictingPriorAccess : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = begin_access [modify] [unknown] %5 : $*Int
  %7 = begin_access [modify] [unknown] %3 : $*Int  // expected-note {{conflicting access is here}}
  %8 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %7 : $*Int
  end_access %6 : $*Int
  end_access %5: $*Int
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %9 : $()
}

// CHECK-LABEL: sil hidden [ossa] @twoSequentialInouts
sil hidden [ossa] @twoSequentialInouts : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesOneInout : $@convention(thin) (@inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*Int
  %6 = apply %4(%5) : $@convention(thin) (@inout Int) -> ()
  end_access %5 : $*Int
  %7 = begin_access [modify] [unknown] %3 : $*Int // no-error
  %8 = apply %4(%7) : $@convention(thin) (@inout Int) -> ()
  end_access %7: $*Int
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %8 : $()
}


// CHECK-LABEL: sil hidden [ossa] @unconditionalBranch
sil hidden [ossa] @unconditionalBranch : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = begin_access [modify] [unknown] %3 : $*Int
  br finish
finish:
  end_access %4: $*Int
  destroy_value %2 : ${ var Int }
  %5 = tuple ()
  return %5 : $()
}

// CHECK-LABEL: sil hidden [ossa] @diamondMergeStacks
sil hidden [ossa] @diamondMergeStacks : $@convention(thin) (Int, Builtin.Int1) -> () {
bb0(%0 : $Int, %1 : $Builtin.Int1):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = begin_access [modify] [unknown] %3 : $*Int
  cond_br %1, then, else
then:
  br finish
else:
  br finish
finish:
  end_access %4: $*Int
  destroy_value %2 : ${ var Int }
  %5 = tuple ()
  return %5 : $()
}


// CHECK-LABEL: sil hidden [ossa] @loopMergeStacks
sil hidden [ossa] @loopMergeStacks : $@convention(thin) (Int, Builtin.Int1) -> () {
bb0(%0 : $Int, %1 : $Builtin.Int1):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = begin_access [modify] [unknown] %3 : $*Int
  br bb1
bb1:
  cond_br %1, bb2, bb3
bb2:
  br bb1
bb3:
  end_access %4: $*Int
  destroy_value %2 : ${ var Int }
  %5 = tuple ()
  return %5 : $()
}

// CHECK-LABEL: sil hidden [ossa] @loopWithError
sil hidden [ossa] @loopWithError : $@convention(thin) (Int, Builtin.Int1) -> () {
bb0(%0 : $Int, %1 : $Builtin.Int1):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  br bb1
bb1:
  // Make sure we don't diagnose twice.
  %4 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %5 = begin_access [modify] [unknown] %3 : $*Int // expected-note {{conflicting access is here}}
  end_access %5: $*Int
  end_access %4: $*Int
  cond_br %1, bb2, bb3
bb2:
  br bb1
bb3:
  destroy_value %2 : ${ var Int }
  %6 = tuple ()
  return %6 : $()
}

// CHECK-LABEL: sil hidden [ossa] @modifySubAccessesAreAllowed
sil hidden [ossa] @modifySubAccessesAreAllowed : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*Int
  %6 = begin_access [modify] [unknown] %5 : $*Int  // no-error
  %7 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %6 : $*Int
  end_access %5: $*Int
  destroy_value %2 : ${ var Int }
  %8 = tuple ()
  return %8 : $()
}

// Multiple access kinds

// CHECK-LABEL: sil hidden [ossa] @twoLocalReadsSimpleAliasing
sil hidden [ossa] @twoLocalReadsSimpleAliasing : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %1 = alloc_box ${ var Int }
  %2 = project_box %1 : ${ var Int }, 0
  store %0 to [trivial] %2 : $*Int
  %4 = begin_access [read] [unknown] %2 : $*Int
  %5 = begin_access [read] [unknown] %2 : $*Int  // no-error
  end_access %5 : $*Int
  end_access %4: $*Int
  destroy_value %1 : ${ var Int }
  %6 = tuple ()
  return %6 : $()
}

// CHECK-LABEL: sil hidden [ossa] @localReadFollowedByModify
sil hidden [ossa] @localReadFollowedByModify : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %1 = alloc_box ${ var Int }
  %2 = project_box %1 : ${ var Int }, 0
  store %0 to [trivial] %2 : $*Int
  %4 = begin_access [read] [unknown] %2 : $*Int // expected-note {{conflicting access is here}}
  %5 = begin_access [modify] [unknown] %2 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  end_access %5 : $*Int
  end_access %4: $*Int
  destroy_value %1 : ${ var Int }
  %6 = tuple ()
  return %6 : $()
}

// CHECK-LABEL: sil hidden [ossa] @localModifyFollowedByRead
sil hidden [ossa] @localModifyFollowedByRead : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %1 = alloc_box ${ var Int }
  %2 = project_box %1 : ${ var Int }, 0
  store %0 to [trivial] %2 : $*Int
  %4 = begin_access [modify] [unknown] %2 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %5 = begin_access [read] [unknown] %2 : $*Int // expected-note {{conflicting access is here}}
  end_access %5 : $*Int
  end_access %4: $*Int
  destroy_value %1 : ${ var Int }
  %6 = tuple ()
  return %6 : $()
}


class ClassWithStoredProperty {
  @_hasStorage var f: Int
  init()
}
// CHECK-LABEL: sil hidden [ossa] @classStoredProperty
sil hidden [ossa] @classStoredProperty : $@convention(thin) (@owned ClassWithStoredProperty) -> () {
bb0(%0 : @owned $ClassWithStoredProperty):
  %0a = begin_borrow %0 : $ClassWithStoredProperty
  %1 = ref_element_addr %0a : $ClassWithStoredProperty, #ClassWithStoredProperty.f

  // expected-error@+1{{overlapping accesses to 'f', but modification requires exclusive access; consider copying to a local variable}}
  %2 = begin_access [modify] [dynamic] %1 : $*Int
  %3 = ref_element_addr %0a : $ClassWithStoredProperty, #ClassWithStoredProperty.f

  // expected-note@+1{{conflicting access is here}}
  %4 = begin_access [modify] [dynamic] %3 : $*Int
  end_access %4 : $*Int
  end_access %2 : $*Int
  end_borrow %0a : $ClassWithStoredProperty
  destroy_value %0 : $ClassWithStoredProperty
  %5 = tuple ()
  return %5 : $()
}

// CHECK-LABEL: sil hidden [ossa] @lookThroughBeginBorrow
sil hidden [ossa] @lookThroughBeginBorrow : $@convention(thin) (@owned ClassWithStoredProperty) -> () {
bb0(%0 : @owned $ClassWithStoredProperty):
  %1 = begin_borrow %0 : $ClassWithStoredProperty
  %2 = begin_borrow %0 : $ClassWithStoredProperty
  %3 = ref_element_addr %1 : $ClassWithStoredProperty, #ClassWithStoredProperty.f

  // expected-error@+1{{overlapping accesses to 'f', but modification requires exclusive access; consider copying to a local variable}}
  %4 = begin_access [modify] [dynamic] %3 : $*Int
  %5 = ref_element_addr %2 : $ClassWithStoredProperty, #ClassWithStoredProperty.f

  // expected-note@+1{{conflicting access is here}}
  %6 = begin_access [modify] [dynamic] %5 : $*Int
  end_access %6 : $*Int
  end_access %4 : $*Int
  end_borrow %2 : $ClassWithStoredProperty
  end_borrow %1 : $ClassWithStoredProperty
  destroy_value %0 : $ClassWithStoredProperty
  %7 = tuple ()
  return %7 : $()
}

// Tests for address identity

// Treat 'alloc_box' as identity for project_box

// CHECK-LABEL: sil hidden [ossa] @twoAllocBoxProjections
sil hidden [ossa] @twoAllocBoxProjections : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = copy_value %2 : ${ var Int }
  %5 = project_box %4 : ${ var Int }, 0
  %6 = begin_access [modify] [unknown] %3 : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %7 = begin_access [modify] [unknown] %5 : $*Int  // expected-note {{conflicting access is here}}
  end_access %7 : $*Int
  end_access %6: $*Int
  destroy_value %2 : ${ var Int }
  destroy_value %4 : ${ var Int }
  %8 = tuple ()
  return %8 : $()
}

// CHECK-LABEL: sil hidden [ossa] @lookThroughMarkUninitialized
sil hidden [ossa] @lookThroughMarkUninitialized : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %1 = alloc_box ${ var Int }
  %2 = mark_uninitialized [rootself] %1 : ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = project_box %2 : ${ var Int }, 0
  %5 = begin_access [modify] [unknown] %3 : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = begin_access [modify] [unknown] %4 : $*Int  // expected-note {{conflicting access is here}}
  end_access %6 : $*Int
  end_access %5: $*Int
  destroy_value %2 : ${ var Int }
  %7 = tuple ()
  return %7 : $()
}

// Treat global as identity for global_addr instruction-
sil_global hidden @global1 : $Int
sil_global hidden @global2 : $Int

// CHECK-LABEL: sil hidden [ossa] @modifySameGlobal
sil hidden [ossa] @modifySameGlobal : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %1 = global_addr @global1 :$*Int
  %2 = global_addr @global1 :$*Int
  %3 = begin_access [modify] [unknown] %1 : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %4 = begin_access [modify] [unknown] %2 : $*Int  // expected-note {{conflicting access is here}}
  end_access %4 : $*Int
  end_access %3: $*Int
  %5 = tuple ()
  return %5 : $()
}

// CHECK-LABEL: sil hidden [ossa] @modifyDifferentGlobal
sil hidden [ossa] @modifyDifferentGlobal : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %1 = global_addr @global1 :$*Int
  %2 = global_addr @global2 :$*Int
  %3 = begin_access [modify] [unknown] %1 : $*Int
  %4 = begin_access [modify] [unknown] %2 : $*Int  // no-error
  end_access %4 : $*Int
  end_access %3: $*Int
  %5 = tuple ()
  return %5 : $()
}

// Multiple errors accessing the same location

// If we have a sequence of begin read - begin write - begin read accesses make
// sure the second read doesn't report a confusing read-read conflict.
// CHECK-LABEL: sil hidden [ossa] @readWriteReadConflictingThirdAccess
sil hidden [ossa] @readWriteReadConflictingThirdAccess : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [read] [unknown] %3 : $*Int  // expected-note {{conflicting access is here}}
  %6 = begin_access [modify] [unknown] %3 : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %7 = begin_access [read] [unknown] %3 : $*Int // no-error
  %8 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %7 : $*Int
  end_access %6 : $*Int
  end_access %5: $*Int
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %9 : $()
}

// If we have a sequence of begin write - begin write - begin write accesses make sure the
// third write doesn't report a conflict.
// CHECK-LABEL: sil hidden [ossa] @writeWriteWriteConflictingThirdAccess
sil hidden [ossa] @writeWriteWriteConflictingThirdAccess : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = begin_access [modify] [unknown] %3 : $*Int  // expected-note {{conflicting access is here}}
  %7 = begin_access [modify] [unknown] %3 : $*Int  // no-error
  %8 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %7 : $*Int
  end_access %6 : $*Int
  end_access %5: $*Int
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %9 : $()
}

// If we have a sequence of begin write - end write - begin write - begin write
// accesses make sure the it is the second begin write that gets the note
// about the conflict and not the first
// CHECK-LABEL: sil hidden [ossa] @resetFirstAccessForNote
sil hidden [ossa] @resetFirstAccessForNote : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*Int  // no-note
  end_access %5 : $*Int
  %6 = begin_access [modify] [unknown] %3 : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %7 = begin_access [modify] [unknown] %3 : $*Int  // expected-note {{conflicting access is here}}
  %8 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %7 : $*Int
  end_access %6: $*Int
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %9 : $()
}

// Check for iterator invalidation issues when the hash from
// basic blocks to analysis state is re-hashed. The number of
// blocks in this test is determined by the initial size of the
// 'BlockOutAccesses' DenseMap in the implementation.
//
//  The unreachable block below must branch to bN where
//    N = 3/4 * INITIAL_SIZE - 2
sil [ossa] @blockMapRehash : $@convention(method) (Builtin.Int1) -> () {
bb0(%0 : $Builtin.Int1):
 br bb1
bb1:
  br bb2
bb2:
  br bb3
bb3:
  br bb4
bb4:
  br bb5
bb5:
  br bb6
bb6:
  br bb7
bb7:
  br bb8
bb8:
  br bb9
bb9:
  br bb10
bb10:
  br bb11
bb11:
  br bb12
bb12:
  br bb13
bb13:
  br bb14
bb14:
  br bb15
bb15:
  br bb16
bb16:
   br bb17
bb17:
  br bb18
bb18:
  br bb19
bb19:
  br bb20
bb20:
  br bb21
bb21:
  br bb22
bb22:
  br bb23 // no-crash
bb23:
  %1 = tuple ()
  return %1 : $()
bbUnreachable:
  br bb22
}

// Check that a pointer_to_address access passes diagnostics.
//
// CHECK-LABEL: sil hidden [ossa] @pointerToAddr
sil hidden [ossa] @pointerToAddr : $@convention(thin) (Builtin.RawPointer) -> Int {
bb0(%0: $Builtin.RawPointer):
  %adr = pointer_to_address %0 : $Builtin.RawPointer to [strict] $*Int
  %access = begin_access [read] [dynamic] %adr : $*Int
  %val = load [trivial] %access : $*Int
  end_access %access : $*Int
  return %val : $Int
}

// Helper.
struct S {
  var x: Int
}

// Check inlined struct element access.
// This only happens when mandatory passes are applied repeatedly.
// e.g. importing from a debug standard library.
//
// CHECK-LABEL: sil hidden [ossa] @inlinedStructElement
sil hidden [ossa] @inlinedStructElement : $@convention(thin) (@inout S) -> Int {
bb0(%0 : $*S):
  %2 = begin_access [modify] [static] %0 : $*S
  %3 = struct_element_addr %2 : $*S, #S.x
  %4 = begin_access [read] [static] %3 : $*Int
  %5 = load [trivial] %4 : $*Int
  end_access %4 : $*Int
  end_access %2 : $*S
  return %5 : $Int
}

// Check inlined tuple element access.
// This only happens when mandatory passes are applied repeatedly.
//
// CHECK-LABEL: sil hidden [ossa] @inlinedTupleElement
sil hidden [ossa] @inlinedTupleElement : $@convention(thin) (@inout (Int, Int)) -> Int {
bb0(%0 : $*(Int, Int)):
  %2 = begin_access [modify] [static] %0 : $*(Int, Int)
  %3 = tuple_element_addr %2 : $*(Int, Int), 0
  %4 = begin_access [read] [static] %3 : $*Int
  %5 = load [trivial] %4 : $*Int
  end_access %4 : $*Int
  end_access %2 : $*(Int, Int)
  return %5 : $Int
}

// Check inlined enum access.
// This only happens when mandatory passes are applied repeatedly.
//
// CHECK-LABEL: sil hidden [ossa] @inlinedEnumValue
sil hidden [ossa] @inlinedEnumValue : $@convention(thin) (@in Optional<Int>, Int) -> Int {
bb0(%0 : $*Optional<Int>, %1 : $Int):
  %6 = unchecked_take_enum_data_addr %0 : $*Optional<Int>, #Optional.some!enumelt
  %7 = begin_access [read] [static] %6 : $*Int
  %8 = load [trivial] %7 : $*Int
  end_access %7 : $*Int
  return %8 : $Int
}

// Helper.
class Storage {}

// Check inlined array access.
// This only happens when mandatory passes are applied repeatedly.
//
// CHECK-LABEL: sil hidden [ossa] @inlinedArrayProp
sil hidden [ossa] @inlinedArrayProp : $@convention(thin) (@guaranteed Storage, Builtin.Word) -> Int {
bb0(%0 : @guaranteed $Storage, %1 : $Builtin.Word):
  %2 = ref_tail_addr %0 : $Storage, $UInt
  %3 = begin_access [read] [static] %2 : $*UInt
  %4 = tail_addr %3 : $*UInt, %1 : $Builtin.Word, $Int
  %5 = begin_access [read] [static] %4 : $*Int
  %6 = index_addr %5 : $*Int, %1 : $Builtin.Word
  %7 = begin_access [read] [static] %6 : $*Int
  %8 = load [trivial] %7 : $*Int
  end_access %7 : $*Int
  end_access %5 : $*Int
  end_access %3 : $*UInt
  return %8 : $Int
}

// Conflicts involving noescape closures.

sil hidden [ossa] @closureThatModifiesCaptureAndTakesInout: $@convention(thin) (@inout Int, @inout_aliasable Int) -> () {
bb0(%0 : $*Int, %1 : $*Int):
  %2 = begin_access [modify] [unknown] %1 : $*Int // expected-note {{conflicting access is here}}
  end_access %2 : $*Int
  %3 = tuple ()
  return %3 : $()
}

// FIXME: We should prevent SILGen from emitting such a thing because the only
// way to distinguish a capture from a regular @inout argument here is by the
// @inout_aliasable convention. When we eliminate that convention we should make
// sure that whenever we promote a closure from a @callee_guaranteed @noescape
// function type to @conventio(thin), we insert access enforcement on the caller
// side for captured variables.
//
// CHECK-LABEL: sil hidden [ossa] @inProgressModifyModifyConflictWithCallToClosure
sil hidden [ossa] @inProgressModifyModifyConflictWithCallToClosure : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @closureThatModifiesCaptureAndTakesInout: $@convention(thin) (@inout Int, @inout_aliasable Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %8 = apply %4(%5, %3) : $@convention(thin) (@inout Int, @inout_aliasable Int) -> ()
  end_access %5: $*Int
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %9 : $()
}

sil hidden [ossa] @closureWithArgument_1 : $@convention(thin) (Int, @inout_aliasable Int) -> () {
bb0(%0 : $Int, %1 : $*Int):
  %2 = begin_access [modify] [unknown] %1 : $*Int // expected-note 2 {{conflicting access is here}}
  end_access %2 : $*Int
  %3 = tuple ()
  return %3 : $()
}

// CHECK-LABEL: sil hidden [ossa] @inProgressConflictWithNoEscapeClosureArgument
sil hidden [ossa] @inProgressConflictWithNoEscapeClosureArgument : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesInoutAndNoEscapeClosureTakingArgument : $@convention(thin) (@inout Int, @guaranteed @noescape @callee_owned (Int) -> ()) -> ()
  %5 = function_ref @closureWithArgument_1 : $@convention(thin) (Int, @inout_aliasable Int) -> ()
  %6 = partial_apply %5(%3) : $@convention(thin) (Int, @inout_aliasable Int) -> ()
  %conv = convert_escape_to_noescape %6 : $@callee_owned (Int) -> () to $@callee_owned @noescape (Int) -> ()
  %7 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %8 = apply %4(%7, %conv) : $@convention(thin) (@inout Int, @guaranteed @noescape @callee_owned (Int) -> ()) -> ()
  end_access %7: $*Int
  destroy_value %conv : $@callee_owned @noescape (Int) -> ()
  destroy_value %6 : $@callee_owned (Int) -> ()
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %9 : $()
}

// CHECK-LABEL: sil hidden [ossa] @inProgressConflictWithNoEscapeGuaranteedClosureArgument : $@convention(thin) (Int) -> () {
sil hidden [ossa] @inProgressConflictWithNoEscapeGuaranteedClosureArgument : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesInoutAndNoEscapeGuaranteedClosureTakingArgument : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_guaranteed (Int) -> ()) -> ()
  %5 = function_ref @closureWithArgument_1 : $@convention(thin) (Int, @inout_aliasable Int) -> ()
  %6 = partial_apply [callee_guaranteed] %5(%3) : $@convention(thin) (Int, @inout_aliasable Int) -> ()
  %conv = convert_escape_to_noescape %6 : $@callee_guaranteed (Int) -> () to $@callee_guaranteed @noescape (Int) -> ()
  %bconv = begin_borrow %conv : $@callee_guaranteed @noescape (Int) -> ()
  %7 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %8 = apply %4(%7, %bconv) : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_guaranteed (Int) -> ()) -> ()
  end_access %7: $*Int
  end_borrow %bconv : $@callee_guaranteed @noescape (Int) -> ()
  destroy_value %conv : $@callee_guaranteed @noescape (Int) -> ()
  destroy_value %6 : $@callee_guaranteed (Int) -> ()
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %9 : $()
}

sil hidden [ossa] @closureThatModifiesCapture_2 : $@convention(thin) (@inout_aliasable Int) -> () {
bb0(%0 : $*Int):
  %1 = begin_access [modify] [unknown] %0 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  end_access %1 : $*Int
  %2 = tuple ()
  return %2 : $()
}

// CHECK-LABEL: sil hidden [ossa] @inProgressReadModifyConflictWithNoEscapeClosureArgument
sil hidden [ossa] @inProgressReadModifyConflictWithNoEscapeClosureArgument : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesInoutAndNoEscapeClosure : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_owned () -> ()) -> ()
  %5 = function_ref @closureThatModifiesCapture_2 : $@convention(thin) (@inout_aliasable Int) -> ()
  %6 = partial_apply %5(%3) : $@convention(thin) (@inout_aliasable Int) -> ()
  %conv = convert_escape_to_noescape %6 : $@callee_owned () -> () to $@callee_owned @noescape () -> ()
  %7 = begin_access [read] [unknown] %3 : $*Int // expected-note {{conflicting access is here}}
  %8 = apply %4(%3, %conv) : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_owned () -> ()) -> ()
  end_access %7: $*Int
  destroy_value %conv : $@callee_owned @noescape () -> ()
  destroy_value %6 : $@callee_owned () -> ()
  destroy_value %2 : ${ var Int }
  %9 = tuple ()
  return %9 : $()
}

sil hidden [ossa] @closureWithConcreteReturn : $@convention(thin) (Int, @inout_aliasable Int) -> (Int) {
bb0(%0 : $Int, %1 : $*Int):
  %2 = begin_access [modify] [unknown] %1 : $*Int // expected-note {{conflicting access is here}}
  end_access %2 : $*Int
  return %0 : $Int
}

sil [reabstraction_thunk] @thunkForClosureWithConcreteReturn : $@convention(thin) (Int, @noescape @callee_owned (Int) -> Int) -> @out Int

// CHECK-LABEL: sil hidden [ossa] @inProgressConflictWithNoEscapeClosureWithReabstractionThunk
sil hidden [ossa] @inProgressConflictWithNoEscapeClosureWithReabstractionThunk : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesInoutAndNoEscapeClosureWithGenericReturn : $@convention(thin) <T_0> (@inout Int, @noescape @callee_owned (Int) -> @out T_0) -> ()
  %5 = function_ref @closureWithConcreteReturn : $@convention(thin) (Int, @inout_aliasable Int) -> (Int)
  %6 = partial_apply %5(%3) : $@convention(thin) (Int, @inout_aliasable Int) -> (Int)
  %7 = convert_escape_to_noescape %6 : $@callee_owned (Int) -> Int to $@noescape @callee_owned (Int) -> Int
  %8 = function_ref @thunkForClosureWithConcreteReturn : $@convention(thin) (Int, @noescape @callee_owned (Int) -> Int) -> @out Int
  %9 = partial_apply %8(%7) : $@convention(thin) (Int, @noescape @callee_owned (Int) -> Int) -> @out Int
  %10 = convert_escape_to_noescape %9 : $@callee_owned (Int) -> @out Int to $@noescape @callee_owned (Int) -> @out Int
  %11 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %12 = apply %4<Int>(%11, %10) : $@convention(thin) <T_0> (@inout Int, @noescape @callee_owned (Int) -> @out T_0) -> ()
  end_access %11: $*Int
  destroy_value %10 : $@noescape @callee_owned (Int) -> @out Int
  destroy_value %9 : $@callee_owned (Int) -> @out Int
  destroy_value %6 : $@callee_owned (Int) -> Int
  destroy_value %2 : ${ var Int }
  %13 = tuple ()
  return %13 : $()
}

sil [reabstraction_thunk] @thunkForCalleeGuaranteed : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> ()
sil [reabstraction_thunk] @withoutActuallyEscapingThunk : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()

sil hidden [ossa] @closureThatModifiesCapture_1: $@convention(thin) (@inout_aliasable Int) -> () {
bb0(%0 : $*Int):
  %1 = begin_access [modify] [unknown] %0 : $*Int // expected-note 3{{conflicting access is here}}
  end_access %1 : $*Int
  %2 = tuple ()
  return %2 : $()
}

// CHECK-LABEL: sil hidden [ossa] @inProgressConflictWithNoEscapeClosureWithBlockStorage
sil hidden [ossa] @inProgressConflictWithNoEscapeClosureWithBlockStorage : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesInoutAndNoEscapeBlockClosure : $@convention(thin) (@inout Int, @owned @convention(block) @noescape () -> ()) -> ()
  %5 = function_ref @closureThatModifiesCapture_1 : $@convention(thin) (@inout_aliasable Int) -> ()
  %6 = partial_apply [callee_guaranteed] %5(%3) : $@convention(thin) (@inout_aliasable Int) -> ()
  %conv = convert_escape_to_noescape %6 : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
  %convb = copy_value %conv : $@noescape @callee_guaranteed () -> ()
  %thunk = function_ref @withoutActuallyEscapingThunk : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()
  %sentinel = partial_apply [callee_guaranteed] %thunk(%convb) : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()
  %sentinel2 = mark_dependence %sentinel : $@callee_guaranteed () -> () on %conv : $@noescape @callee_guaranteed () -> ()
  %sentinel3 = copy_value %sentinel2 : $@callee_guaranteed () -> ()
  %7 = alloc_stack $@block_storage @callee_guaranteed () -> ()
  %8 = project_block_storage %7 : $*@block_storage @callee_guaranteed () -> ()
  store %sentinel3 to [init] %8 : $*@callee_guaranteed () -> ()
  %10 = function_ref @thunkForCalleeGuaranteed : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> ()
  %11 = init_block_storage_header %7 : $*@block_storage @callee_guaranteed () -> (), invoke %10 : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> (), type $@convention(block) @noescape () -> ()
  %12 = copy_block %11 : $@convention(block) @noescape () -> ()
  %13 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %14 = apply %4(%13, %12) : $@convention(thin) (@inout Int, @owned @convention(block) @noescape () -> ()) -> ()
  end_access %13 : $*Int
  destroy_addr %8 : $*@callee_guaranteed() -> ()
  dealloc_stack %7 : $*@block_storage @callee_guaranteed () -> ()
  destroy_value %sentinel2 : $@callee_guaranteed () -> ()
  destroy_value %conv : $@noescape @callee_guaranteed () -> ()
  destroy_value %6 : $@callee_guaranteed () -> ()
  destroy_value %2 : ${ var Int }
  %ret = tuple ()
  return %ret : $()
}

// CHECK-LABEL: sil hidden [ossa] @inProgressConflictWithNoEscapeClosureWithOptionalBlockStorage
sil hidden [ossa] @inProgressConflictWithNoEscapeClosureWithOptionalBlockStorage : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesInoutAndNoEscapeOptionalBlockClosure : $@convention(thin) (@inout Int, @owned Optional<@convention(block) @noescape () -> ()>) -> ()
  %5 = function_ref @closureThatModifiesCapture_1 : $@convention(thin) (@inout_aliasable Int) -> ()
  %6 = partial_apply [callee_guaranteed] %5(%3) : $@convention(thin) (@inout_aliasable Int) -> ()
  %conv = convert_escape_to_noescape %6 : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
  %convb = copy_value %conv : $@noescape @callee_guaranteed () -> ()
  %thunk = function_ref @withoutActuallyEscapingThunk : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()
  %sentinel = partial_apply [callee_guaranteed] %thunk(%convb) : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()
  %sentinel2 = mark_dependence %sentinel : $@callee_guaranteed () -> () on %conv : $@noescape @callee_guaranteed () -> ()
  %sentinel3 = copy_value %sentinel2 : $@callee_guaranteed () -> ()
  %7 = alloc_stack $@block_storage @callee_guaranteed () -> ()
  %8 = project_block_storage %7 : $*@block_storage @callee_guaranteed () -> ()
  store %sentinel3 to [init] %8 : $*@callee_guaranteed () -> ()
  %10 = function_ref @thunkForCalleeGuaranteed : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> ()
  %11 = init_block_storage_header %7 : $*@block_storage @callee_guaranteed () -> (), invoke %10 : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> (), type $@convention(block) @noescape () -> ()
  %12 = copy_block %11 : $@convention(block) @noescape () -> ()
  %13 = enum $Optional<@convention(block) @noescape () -> ()>, #Optional.some!enumelt, %12 : $@convention(block) @noescape () -> ()
  %14 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %15 = apply %4(%14, %13) : $@convention(thin) (@inout Int, @owned Optional<@convention(block) @noescape () -> ()>) -> ()
  end_access %14 : $*Int
  destroy_addr %8 : $*@callee_guaranteed () -> ()
  dealloc_stack %7 : $*@block_storage @callee_guaranteed () -> ()
  destroy_value %sentinel2 : $@callee_guaranteed () -> ()
  destroy_value %conv : $@noescape @callee_guaranteed () -> ()
  destroy_value %6 : $@callee_guaranteed () -> ()
  destroy_value %2 : ${ var Int }
  %ret = tuple ()
  return %ret : $()
}


// Stored property relaxation.

struct NestedStructWithStoredProps {
  var a: Int
  var b: Int
}

struct StructWithStoredProps {
  var x: Int
  var y: Int

  var n: NestedStructWithStoredProps
}

sil [ossa] @takesInoutIntAndStructWithStoredProps : $@convention(thin) (@inout Int, @inout StructWithStoredProps) -> ()

// CHECK-LABEL: sil hidden [ossa] @twoSeparateStoredProperties
sil hidden [ossa] @twoSeparateStoredProperties : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var StructWithStoredProps }
  %3 = project_box %2 : ${ var StructWithStoredProps }, 0
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps
  %6 = struct_element_addr %5 : $*StructWithStoredProps, #StructWithStoredProps.x
  %7 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps // no-error
  %8 = struct_element_addr %7 : $*StructWithStoredProps, #StructWithStoredProps.y
  %9 = apply %4(%6, %8) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %7 : $*StructWithStoredProps
  end_access %5: $*StructWithStoredProps
  destroy_value %2 : ${ var StructWithStoredProps }
  %10 = tuple ()
  return %10 : $()
}

// CHECK-LABEL: sil hidden [ossa] @twoSeparateNestedStoredProperties
sil hidden [ossa] @twoSeparateNestedStoredProperties : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var StructWithStoredProps }
  %3 = project_box %2 : ${ var StructWithStoredProps }, 0
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps
  %6 = struct_element_addr %5 : $*StructWithStoredProps, #StructWithStoredProps.n
  %7 = struct_element_addr %6 : $*NestedStructWithStoredProps, #NestedStructWithStoredProps.a
  %8 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps // no-error
  %9 = struct_element_addr %8 : $*StructWithStoredProps, #StructWithStoredProps.n
  %10 = struct_element_addr %9 : $*NestedStructWithStoredProps, #NestedStructWithStoredProps.b
  %11 = apply %4(%7, %10) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %8 : $*StructWithStoredProps
  end_access %5: $*StructWithStoredProps
  destroy_value %2 : ${ var StructWithStoredProps }
  %12 = tuple ()
  return %12 : $()
}

// CHECK-LABEL: sil hidden [ossa] @theSameStoredProperty
sil hidden [ossa] @theSameStoredProperty : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var StructWithStoredProps }
  %3 = project_box %2 : ${ var StructWithStoredProps }, 0
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = struct_element_addr %5 : $*StructWithStoredProps, #StructWithStoredProps.x
  %7 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps // expected-note {{conflicting access is here}}
  %8 = struct_element_addr %7 : $*StructWithStoredProps, #StructWithStoredProps.x
  %9 = apply %4(%6, %8) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %7 : $*StructWithStoredProps
  end_access %5: $*StructWithStoredProps
  destroy_value %2 : ${ var StructWithStoredProps }
  %10 = tuple ()
  return %10 : $()
}

// CHECK-LABEL: sil hidden [ossa] @storedPropertyAndAggregate
sil hidden [ossa] @storedPropertyAndAggregate : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var StructWithStoredProps }
  %3 = project_box %2 : ${ var StructWithStoredProps }, 0
  %4 = function_ref @takesInoutIntAndStructWithStoredProps : $@convention(thin) (@inout Int, @inout StructWithStoredProps) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = struct_element_addr %5 : $*StructWithStoredProps, #StructWithStoredProps.x
  %7 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps // expected-note {{conflicting access is here}}
  %8 = apply %4(%6, %7) : $@convention(thin) (@inout Int, @inout StructWithStoredProps) -> ()
  end_access %7 : $*StructWithStoredProps
  end_access %5: $*StructWithStoredProps
  destroy_value %2 : ${ var StructWithStoredProps }
  %9 = tuple ()
  return %9 : $()
}

// CHECK-LABEL: sil hidden [ossa] @nestedStoredPropertyAndAggregate
sil hidden [ossa] @nestedStoredPropertyAndAggregate : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var StructWithStoredProps }
  %3 = project_box %2 : ${ var StructWithStoredProps }, 0
  %4 = function_ref @takesInoutIntAndStructWithStoredProps : $@convention(thin) (@inout Int, @inout StructWithStoredProps) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = struct_element_addr %5 : $*StructWithStoredProps, #StructWithStoredProps.n
  %7 = struct_element_addr %6 : $*NestedStructWithStoredProps, #NestedStructWithStoredProps.a
  %8 = begin_access [modify] [unknown] %3 : $*StructWithStoredProps // expected-note {{conflicting access is here}}
  %9 = struct_element_addr %8 : $*StructWithStoredProps, #StructWithStoredProps.n
  end_access %8 : $*StructWithStoredProps
  end_access %5: $*StructWithStoredProps
  destroy_value %2 : ${ var StructWithStoredProps }
  %10 = tuple ()
  return %10 : $()
}

// CHECK-LABEL: sil hidden [ossa] @twoSeparateTupleElements
sil hidden [ossa] @twoSeparateTupleElements : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var (Int, Int) }
  %3 = project_box %2 : ${ var (Int, Int) }, 0
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*(Int, Int)
  %6 = tuple_element_addr %5 : $*(Int, Int), 0
  %7 = begin_access [modify] [unknown] %3 : $*(Int, Int) // no-error
  %8 = tuple_element_addr %7 : $*(Int, Int), 1
  %9 = apply %4(%6, %8) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %7 : $*(Int, Int)
  end_access %5: $*(Int, Int)
  destroy_value %2 : ${ var (Int, Int) }
  %10 = tuple ()
  return %10 : $()
}

// CHECK-LABEL: sil hidden [ossa] @sameTupleElement
sil hidden [ossa] @sameTupleElement : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var (Int, Int) }
  %3 = project_box %2 : ${ var (Int, Int) }, 0
  %4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
  %5 = begin_access [modify] [unknown] %3 : $*(Int, Int) // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = tuple_element_addr %5 : $*(Int, Int), 0
  %7 = begin_access [modify] [unknown] %3 : $*(Int, Int) // expected-note {{conflicting access is here}}
  %8 = tuple_element_addr %7 : $*(Int, Int), 0
  %9 = apply %4(%6, %8) : $@convention(thin) (@inout Int, @inout Int) -> ()
  end_access %7 : $*(Int, Int)
  end_access %5: $*(Int, Int)
  destroy_value %2 : ${ var (Int, Int) }
  %10 = tuple ()
  return %10 : $()
}

sil hidden [ossa] @passNilToNoEscape : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  %4 = enum $Optional<@convention(block) @noescape () -> ()>, #Optional.none!enumelt
  %5 = function_ref @takesInoutAndNoEscapeOptionalBlockClosure : $@convention(thin) (@inout Int, @owned Optional<@convention(block) @noescape () -> ()>) -> ()
  %6 = apply %5(%3, %4) : $@convention(thin) (@inout Int, @owned Optional<@convention(block) @noescape () -> ()>) -> ()
  destroy_value %2 : ${ var Int }
  %7 = tuple ()
  return %7 : $()
}

sil private [ossa] @closureForDirectPartialApplyTakingInout : $@convention(thin) (@inout Int, @inout_aliasable Int) -> () {
bb0(%0 : $*Int, %1 : $*Int):
  %access = begin_access [read] [unknown] %1 : $*Int // expected-note 3 {{conflicting access is here}}
  %val = load [trivial] %access : $*Int
  end_access %access : $*Int
  %v = tuple ()
  return %v : $()
}

sil hidden [ossa] @directPartialApplyTakingInout : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %box = alloc_box ${ var Int }, var, name "i"
  %boxadr = project_box %box : ${ var Int }, 0
  store %0 to [trivial] %boxadr : $*Int
  %f = function_ref @closureForDirectPartialApplyTakingInout : $@convention(thin) (@inout Int, @inout_aliasable Int) -> ()
  %pa = partial_apply [callee_guaranteed] %f(%boxadr) : $@convention(thin) (@inout Int, @inout_aliasable Int) -> ()
  %nepa = convert_escape_to_noescape %pa : $@callee_guaranteed (@inout Int) -> () to $@noescape @callee_guaranteed (@inout Int) -> ()
  %access = begin_access [modify] [unknown] %boxadr : $*Int  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %call = apply %nepa(%access) : $@noescape @callee_guaranteed (@inout Int) -> ()
  end_access %access : $*Int
  destroy_value %nepa : $@noescape @callee_guaranteed (@inout Int) -> ()
  destroy_value %pa : $@callee_guaranteed (@inout Int) -> ()
  destroy_value %box : ${ var Int }
  %v = tuple ()
  return %v : $()
}

sil private [ossa] @closureForDirectPartialApplyChain : $@convention(thin) (@inout Int, Int, @inout_aliasable Int) -> () {
bb0(%0 : $*Int, %1 : $Int, %2 : $*Int):
  %access = begin_access [read] [unknown] %2 : $*Int // expected-note {{conflicting access is here}}
  %val = load [trivial] %access : $*Int
  end_access %access : $*Int
  %v = tuple ()
  return %v : $()
}

sil hidden [ossa] @directPartialApplyChain : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %box = alloc_box ${ var Int }, var, name "i"
  %boxadr = project_box %box : ${ var Int }, 0
  store %0 to [trivial] %boxadr : $*Int
  %f = function_ref @closureForDirectPartialApplyChain : $@convention(thin) (@inout Int, Int, @inout_aliasable Int) -> ()
  %pa1 = partial_apply [callee_guaranteed] %f(%boxadr) : $@convention(thin) (@inout Int, Int, @inout_aliasable Int) -> ()
  %pa2 = partial_apply [callee_guaranteed] %pa1(%0) : $@callee_guaranteed (@inout Int, Int) -> ()
  %nepa = convert_escape_to_noescape %pa2 : $@callee_guaranteed (@inout Int) -> () to $@noescape @callee_guaranteed (@inout Int) -> ()
  %access = begin_access [modify] [unknown] %boxadr : $*Int   // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %call = apply %nepa(%access) : $@noescape @callee_guaranteed (@inout Int) -> ()
  end_access %access : $*Int
  destroy_value %nepa : $@noescape @callee_guaranteed (@inout Int) -> ()
  destroy_value %pa2 : $@callee_guaranteed (@inout Int) -> ()
  destroy_value %box : ${ var Int }
  %v = tuple ()
  return %v : $()
}

sil private [ossa] @closureForPartialApplyArgChain : $@convention(thin) (Int, @inout_aliasable Int) -> () {
bb0(%0 : $Int, %1 : $*Int):
  %access = begin_access [read] [unknown] %1 : $*Int // expected-note {{conflicting access is here}}
  %val = load [trivial] %access : $*Int
  end_access %access : $*Int
  %v = tuple ()
  return %v : $()
}

sil hidden [ossa] @partialApplyArgChain : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = alloc_box ${ var Int }
  %3 = project_box %2 : ${ var Int }, 0
  store %0 to [trivial] %3 : $*Int
  %4 = function_ref @takesInoutAndNoEscapeClosure : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_owned () -> ()) -> ()
  %5 = function_ref @closureForPartialApplyArgChain : $@convention(thin) (Int, @inout_aliasable Int) -> ()
  %6 = partial_apply %5(%3) : $@convention(thin) (Int, @inout_aliasable Int) -> ()
  %7 = partial_apply %6(%0) : $@callee_owned (Int) -> ()
  %conv = convert_escape_to_noescape %7 : $@callee_owned () -> () to $@callee_owned @noescape () -> ()
  %8 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %9 = apply %4(%8, %conv) : $@convention(thin) (@inout Int, @noescape @guaranteed @callee_owned () -> ()) -> ()
  end_access %8: $*Int
  destroy_value %conv : $@callee_owned @noescape () -> ()
  destroy_value %7 : $@callee_owned () -> ()
  destroy_value %2 : ${ var Int }
  %v = tuple ()
  return %v : $()
}

class SomeClass {}

// LLDB uses mark_uninitialized [var] in an unsupported way via an address to
// pointer. LLDB shouldn't do this, but until it is removed and validated we
// need a test for this.
//
// Check that this doesn't trigger the DiagnoseStaticExclusivity
// assert that all accesses have a valid AccessStorage source.
sil [ossa] @lldb_unsupported_markuninitialized_testcase : $@convention(thin) (UnsafeMutablePointer<Any>) -> () {
bb0(%0 : $UnsafeMutablePointer<Any>):
  %2 = struct_extract %0 : $UnsafeMutablePointer<Any>, #UnsafeMutablePointer._rawValue
  %3 = integer_literal $Builtin.Int64, 8
  %4 = index_raw_pointer %2 : $Builtin.RawPointer, %3 : $Builtin.Int64
  %5 = pointer_to_address %4 : $Builtin.RawPointer to [strict] $*Builtin.RawPointer
  %6 = load [trivial] %5 : $*Builtin.RawPointer
  %7 = pointer_to_address %6 : $Builtin.RawPointer to [strict] $*Error
  %8 = mark_uninitialized [var] %7 : $*Error
  %9 = struct_extract %0 : $UnsafeMutablePointer<Any>, #UnsafeMutablePointer._rawValue
  %10 = integer_literal $Builtin.Int64, 0
  %11 = index_raw_pointer %9 : $Builtin.RawPointer, %10 : $Builtin.Int64
  %12 = pointer_to_address %11 : $Builtin.RawPointer to [strict] $*Builtin.RawPointer
  %13 = load [trivial] %12 : $*Builtin.RawPointer
  %14 = pointer_to_address %13 : $Builtin.RawPointer to [strict] $*@thick SomeClass.Type
  %15 = mark_uninitialized [var] %14 : $*@thick SomeClass.Type
  %16 = metatype $@thick SomeClass.Type
  %17 = begin_access [modify] [unsafe] %15 : $*@thick SomeClass.Type
  assign %16 to %17 : $*@thick SomeClass.Type
  end_access %17 : $*@thick SomeClass.Type
  %9999 = tuple()
  return %9999 : $()
}

sil [ossa] @addressor : $@convention(thin) () -> UnsafeMutablePointer<Int32>

// An addressor produces an unsafely accessed RawPointer. Pass the
// address to an inout can result in an enforced (unknown) access on
// the unsafe address. We need to handle this case without asserting.
sil [ossa] @addressorAccess : $@convention(thin) (@guaranteed ClassWithStoredProperty, Int32) -> () {
bb0(%0 : @guaranteed $ClassWithStoredProperty, %1 : $Int32):
  %f = function_ref @addressor : $@convention(thin) () -> UnsafeMutablePointer<Int32>
  %up = apply %f() : $@convention(thin) () -> UnsafeMutablePointer<Int32>
  %o = unchecked_ref_cast %0 : $ClassWithStoredProperty to $Builtin.NativeObject
  %ptr = struct_extract %up : $UnsafeMutablePointer<Int32>, #UnsafeMutablePointer._rawValue
  %adr = pointer_to_address %ptr : $Builtin.RawPointer to [strict] $*Int32
  %dep = mark_dependence %adr : $*Int32 on %o : $Builtin.NativeObject
  %a1 = begin_access [modify] [unsafe] %dep : $*Int32
  %a2 = begin_access [modify] [static] %a1 : $*Int32
  store %1 to [trivial] %a2 : $*Int32
  end_access %a2 : $*Int32
  end_access %a1 : $*Int32
  %v = tuple ()
  return %v : $()
}

// rdar://problem/41976355
// https://github.com/apple/swift/issues/50733
// Swift 4.2 crash in 'DiagnoseStaticExclusivity' (llvm_unreachable)
//
// Test that '@noescape' closure verification allows a closure with
// '@inout_aliasable' argument convention, which may only be used as a
// nonescaping closure, to be passed to a reabstraction thunk without the
// '@noescape' function-type argument convention ('mutatingNoescapeWithThunk'
// and 'mutatingNoescapeConflictWithThunk').

sil [ossa] @mutatingNoescapeHelper : $@convention(thin) (Optional<UnsafePointer<Int32>>, @inout_aliasable Int32) -> () {
bb0(%0 : $Optional<UnsafePointer<Int32>>, %1 : $*Int32):
  %up = unchecked_enum_data %0 : $Optional<UnsafePointer<Int32>>, #Optional.some!enumelt
  %p = struct_extract %up : $UnsafePointer<Int32>, #UnsafePointer._rawValue
  %adr = pointer_to_address %p : $Builtin.RawPointer to [strict] $*Int32
  %val = load [trivial] %adr : $*Int32
  %access = begin_access [modify] [unknown] %1 : $*Int32 // expected-note {{conflicting access is here}}
  store %val to [trivial] %access : $*Int32
  end_access %access : $*Int32
  %v = tuple ()
  return %v : $()
}

sil shared [transparent] [serialized] [reabstraction_thunk] [ossa] @mutatingNoescapeThunk : $@convention(thin) (UnsafePointer<Int32>, @guaranteed @callee_guaranteed (Optional<UnsafePointer<Int32>>) -> ()) -> () {
bb0(%0 : $UnsafePointer<Int32>, %1 : @guaranteed $@callee_guaranteed (Optional<UnsafePointer<Int32>>) -> ()):
  %e = enum $Optional<UnsafePointer<Int32>>, #Optional.some!enumelt, %0 : $UnsafePointer<Int32>
  %c = apply %1(%e) : $@callee_guaranteed (Optional<UnsafePointer<Int32>>) -> ()
  %v = tuple ()
  return %v : $()
}

sil [ossa] @takeMutatingNoescapeClosure : $@convention(thin) <τ_0_0> (UnsafePointer<τ_0_0>, @noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()) -> ()

// A (mutating) closure taking an @inout_aliasable argument may be
// passed to a reabstraction_thunk as an escaping function type. This
// is valid as long as the thunk is only used as a @nosecape type.
sil [ossa] @mutatingNoescapeWithThunk : $@convention(method) (UnsafePointer<Int32>, @inout Int32) -> () {
bb0(%0 : $UnsafePointer<Int32>, %1 : $*Int32):
  %f1 = function_ref @mutatingNoescapeHelper : $@convention(thin) (Optional<UnsafePointer<Int32>>, @inout_aliasable Int32) -> ()
  %pa1 = partial_apply [callee_guaranteed] %f1(%1) : $@convention(thin) (Optional<UnsafePointer<Int32>>, @inout_aliasable Int32) -> ()
  %thunk = function_ref @mutatingNoescapeThunk : $@convention(thin) (UnsafePointer<Int32>, @guaranteed @callee_guaranteed (Optional<UnsafePointer<Int32>>) -> ()) -> ()
  %pa2 = partial_apply [callee_guaranteed] %thunk(%pa1) : $@convention(thin) (UnsafePointer<Int32>, @guaranteed @callee_guaranteed (Optional<UnsafePointer<Int32>>) -> ()) -> ()
  %closure = convert_escape_to_noescape [not_guaranteed] %pa2 : $@callee_guaranteed (UnsafePointer<Int32>) -> () to $@noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()
  %f2 = function_ref @takeMutatingNoescapeClosure : $@convention(thin) <τ_0_0> (UnsafePointer<τ_0_0>, @noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()) -> ()
  %call = apply %f2<Int32>(%0, %closure) : $@convention(thin) <τ_0_0> (UnsafePointer<τ_0_0>, @noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()) -> ()
  destroy_value %closure : $@noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()
  destroy_value %pa2 : $@callee_guaranteed (UnsafePointer<Int32>) -> ()
  %v = tuple ()
  return %v : $()
}

sil [ossa] @takeInoutAndMutatingNoescapeClosure : $@convention(thin) <τ_0_0> (@inout Int32, UnsafePointer<τ_0_0>, @noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()) -> ()

sil [ossa] @mutatingNoescapeConflictWithThunk : $@convention(method) (UnsafePointer<Int32>, @inout Int32) -> () {
bb0(%0 : $UnsafePointer<Int32>, %1 : $*Int32):
  %f1 = function_ref @mutatingNoescapeHelper : $@convention(thin) (Optional<UnsafePointer<Int32>>, @inout_aliasable Int32) -> ()
  %pa1 = partial_apply [callee_guaranteed] %f1(%1) : $@convention(thin) (Optional<UnsafePointer<Int32>>, @inout_aliasable Int32) -> ()
  %thunk = function_ref @mutatingNoescapeThunk : $@convention(thin) (UnsafePointer<Int32>, @guaranteed @callee_guaranteed (Optional<UnsafePointer<Int32>>) -> ()) -> ()
  %pa2 = partial_apply [callee_guaranteed] %thunk(%pa1) : $@convention(thin) (UnsafePointer<Int32>, @guaranteed @callee_guaranteed (Optional<UnsafePointer<Int32>>) -> ()) -> ()
  %closure = convert_escape_to_noescape [not_guaranteed] %pa2 : $@callee_guaranteed (UnsafePointer<Int32>) -> () to $@noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()
  %f2 = function_ref @takeInoutAndMutatingNoescapeClosure : $@convention(thin) <τ_0_0> (@inout Int32, UnsafePointer<τ_0_0>, @noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()) -> ()
  %access = begin_access [modify] [unknown] %1 : $*Int32  // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %call = apply %f2<Int32>(%access, %0, %closure) : $@convention(thin) <τ_0_0> (@inout Int32, UnsafePointer<τ_0_0>, @noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()) -> ()
  destroy_value %closure : $@noescape @callee_guaranteed (UnsafePointer<Int32>) -> ()
  destroy_value %pa2 : $@callee_guaranteed (UnsafePointer<Int32>) -> ()
  end_access %access : $*Int32
  %v = tuple ()
  return %v : $()
}

// <rdar://problem/41660554> Swift CI (macOS release main, OSS): SIL verification failed: Unknown formal access pattern: storage
// Test access marker verification of a KeyPath projection with nested @inout access after inlining.
sil @takeInoutInt : $@convention(thin) (@inout Int) -> ()

// The compiler intrinsic _projectXXXKeyPath returns a tuple of
// UnsafePointer and Optional AnyObject. After inlining, the unsafe
// pointer may appear to originate from anywhere, including a phi.
// There's currently no way to tell that the UnsafePointer originated
// from a KeyPath projection. This pattern could occur with any
// addressor. In either case, the nested access is valid but
// unidentified. Addressors that require enforcement must start a
// dynamic access within the addressor itself, before returning an
// UnsafePointer.
sil [ossa] @testNestedKeypathAccess : $@convention(thin) (@guaranteed (UnsafeMutablePointer<Int>, Optional<AnyObject>)) -> () {
bb0(%0 : @guaranteed $(UnsafeMutablePointer<Int>, Optional<AnyObject>)):
  %up = tuple_extract %0 : $(UnsafeMutablePointer<Int>, Optional<AnyObject>), 0
  %o = tuple_extract %0 : $(UnsafeMutablePointer<Int>, Optional<AnyObject>), 1
  %p = struct_extract %up : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
  %adr = pointer_to_address %p : $Builtin.RawPointer to [strict] $*Int
  %dep = mark_dependence %adr : $*Int on %o : $Optional<AnyObject>
  %access = begin_access [modify] [static] %dep : $*Int
  %f = function_ref @takeInoutInt : $@convention(thin) (@inout Int) -> ()
  %call = apply %f(%access) : $@convention(thin) (@inout Int) -> ()
  end_access %access : $*Int
  %v = tuple ()
  return %v : $()
}

// Test a conflict on a noescape closure where multiple closures may be conditionally stored in an ObjC block.
sil hidden [ossa] @noEscapeClosureWithConditionalBlockStorage : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %box = alloc_box ${ var Int }
  %boxadr = project_box %box : ${ var Int }, 0
  store %0 to [trivial] %boxadr : $*Int
  %closure = function_ref @closureThatModifiesCapture_1 : $@convention(thin) (@inout_aliasable Int) -> ()
  %pa = partial_apply [callee_guaranteed] %closure(%boxadr) : $@convention(thin) (@inout_aliasable Int) -> ()
  %conv = convert_escape_to_noescape %pa : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
  %convb = copy_value %conv : $@noescape @callee_guaranteed () -> ()
  %thunk = function_ref @withoutActuallyEscapingThunk : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()
  %sentinel = partial_apply [callee_guaranteed] %thunk(%convb) : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()
  %sentinel2 = mark_dependence %sentinel : $@callee_guaranteed () -> () on %conv : $@noescape @callee_guaranteed () -> ()
  %sentinel3 = copy_value %sentinel2 : $@callee_guaranteed () -> ()
  %calleethunk = function_ref @thunkForCalleeGuaranteed : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> ()
  cond_br undef, bb1, bb2

bb1:
  %bs1 = alloc_stack $@block_storage @callee_guaranteed () -> ()
  %pbs1 = project_block_storage %bs1 : $*@block_storage @callee_guaranteed () -> ()
  store %sentinel3 to [init] %pbs1 : $*@callee_guaranteed () -> ()
  %initblock1 = init_block_storage_header %bs1 : $*@block_storage @callee_guaranteed () -> (), invoke %calleethunk : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> (), type $@convention(block) @noescape () -> ()
  %copyblock1 = copy_block %initblock1 : $@convention(block) @noescape () -> ()
  destroy_addr %pbs1 : $*@callee_guaranteed() -> ()
  dealloc_stack %bs1 : $*@block_storage @callee_guaranteed () -> ()
  destroy_value %sentinel2 : $@callee_guaranteed () -> ()
  destroy_value %conv : $@noescape @callee_guaranteed () -> ()
  br bb3(%copyblock1 : $@convention(block) @noescape () -> ())

bb2:
  %bs2 = alloc_stack $@block_storage @callee_guaranteed () -> ()
  %pbs2 = project_block_storage %bs2 : $*@block_storage @callee_guaranteed () -> ()
  store %sentinel3 to [init] %pbs2 : $*@callee_guaranteed () -> ()
  %initblock2 = init_block_storage_header %bs2 : $*@block_storage @callee_guaranteed () -> (), invoke %calleethunk : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> (), type $@convention(block) @noescape () -> ()
  %copyblock2 = copy_block %initblock2 : $@convention(block) @noescape () -> ()
  destroy_addr %pbs2 : $*@callee_guaranteed() -> ()
  dealloc_stack %bs2 : $*@block_storage @callee_guaranteed () -> ()
  destroy_value %sentinel2 : $@callee_guaranteed () -> ()
  destroy_value %conv : $@noescape @callee_guaranteed () -> ()
  br bb3(%copyblock2 : $@convention(block) @noescape () -> ())

bb3(%block : @owned $@convention(block) @noescape () -> ()):
  %access = begin_access [modify] [unknown] %boxadr : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %f = function_ref @takesInoutAndNoEscapeBlockClosure : $@convention(thin) (@inout Int, @owned @convention(block) @noescape () -> ()) -> ()
  %call = apply %f(%access, %block) : $@convention(thin) (@inout Int, @owned @convention(block) @noescape () -> ()) -> ()
  end_access %access : $*Int
  destroy_value %pa : $@callee_guaranteed () -> ()
  destroy_value %box : ${ var Int }
  %ret = tuple ()
  return %ret : $()
}

// -----------------------------------------------------------------------------
// rdar://problem/42242406
// https://github.com/apple/swift/issues/50797
// Compiler crash when checking exclusivity of 'inout' alias
//
// Test that 'checkNoEscapePartialApply' does not assert on a '@noescape'
// closure passed through a block argument:
//
// closureWithNoCapture
// closureWithConflict
// partialApplyPhiThunk
// testPartialApplyPhi
// testDirectPartialApplyPhi

sil hidden [ossa] @closureWithNoCapture : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %2 = tuple ()
  return %2 : $()
}

sil hidden [ossa] @closureWithConflict : $@convention(thin) (Int, @inout_aliasable Int) -> () {
bb0(%0 : $Int, %1 : $*Int):
  %2 = begin_access [modify] [unknown] %1 : $*Int // expected-note {{conflicting access is here}}
  end_access %2 : $*Int
  %v = tuple ()
  return %v : $()
}

sil shared [transparent] [serialized] [reabstraction_thunk] [ossa] @partialApplyPhiThunk : $@convention(thin) (@in_guaranteed Int, @guaranteed @noescape @callee_guaranteed (Int) -> (@error Error)) -> (@error Error) {
bb0(%0 : $*Int, %1 : @guaranteed $@noescape @callee_guaranteed (Int) -> (@error Error)):
  %val = load [trivial] %0 : $*Int
  try_apply %1(%val) : $@noescape @callee_guaranteed (Int) -> (@error Error), normal bb1, error bb2

bb1(%v : $()):
  return %v : $()

bb2(%5 : @owned $Error):
  throw %5 : $Error
}

sil [ossa] @takeGenericNoEscapeFunction : $@convention(method) <τ_0_0> (@inout τ_0_0, @noescape @callee_guaranteed (@in_guaranteed τ_0_0) -> (@error Error)) -> (@error Error)

// CHECK-LABEL: sil [ossa] @testPartialApplyPhi
sil [ossa] @testPartialApplyPhi : $@convention(thin) (Int, @inout Int) -> (@error Error) {
bb0(%0 : $Int, %1 : $*Int):
  cond_br undef, bb1, bb2

bb1:
  %f1 = function_ref @closureWithNoCapture : $@convention(thin) (Int) -> ()
  %pa1 = partial_apply [callee_guaranteed] %f1() : $@convention(thin) (Int) -> ()
  br bb3(%pa1 : $@callee_guaranteed (Int) -> ())

bb2:
  %f2 = function_ref @closureWithConflict : $@convention(thin) (Int, @inout_aliasable Int) -> ()
  %pa2 = partial_apply [callee_guaranteed] %f2(%1) : $@convention(thin) (Int, @inout_aliasable Int) -> ()
  br bb3(%pa2 : $@callee_guaranteed (Int) -> ())

bb3(%pa3 : @owned $@callee_guaranteed (Int) -> ()):
  %cvt3 = convert_function %pa3 : $@callee_guaranteed (Int) -> () to $@callee_guaranteed (Int) -> (@error Error)
  %esc3 = convert_escape_to_noescape [not_guaranteed] %cvt3 : $@callee_guaranteed (Int) -> (@error Error) to $@noescape @callee_guaranteed (Int) -> (@error Error)

  %f3 = function_ref @partialApplyPhiThunk : $@convention(thin) (@in_guaranteed Int, @guaranteed @noescape @callee_guaranteed (Int) -> (@error Error)) -> (@error Error)
  %pa4 = partial_apply [callee_guaranteed] %f3(%esc3) : $@convention(thin) (@in_guaranteed Int, @guaranteed @noescape @callee_guaranteed (Int) -> (@error Error)) -> (@error Error)
  %esc4 = convert_escape_to_noescape [not_guaranteed] %pa4 : $@callee_guaranteed (@in_guaranteed Int) -> (@error Error) to $@noescape @callee_guaranteed (@in_guaranteed Int) -> (@error Error)

  // ClosureLifetimeFixup has not run yet, so these destroys are "incorrect".
  destroy_value %pa4 : $@callee_guaranteed (@in_guaranteed Int) -> (@error Error)
  destroy_value %cvt3 : $@callee_guaranteed (Int) -> (@error Error)

  %access = begin_access [modify] [static] %1 : $*Int   // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %f4 = function_ref @takeGenericNoEscapeFunction : $@convention(method) <τ_0_0> (@inout τ_0_0, @noescape @callee_guaranteed (@in_guaranteed τ_0_0) -> (@error Error)) -> (@error Error)
  try_apply %f4<Int>(%access, %esc4) : $@convention(method) <τ_0_0> (@inout τ_0_0, @noescape @callee_guaranteed (@in_guaranteed τ_0_0) -> (@error Error)) -> (@error Error), normal bb4, error bb5

bb4(%v : $()):
  end_access %access : $*Int
  destroy_value %esc4 : $@noescape @callee_guaranteed (@in_guaranteed Int) -> (@error Error)
  return %v : $()

bb5(%e : @owned $Error):
  end_access %access : $*Int
  destroy_value %esc4 : $@noescape @callee_guaranteed (@in_guaranteed Int) -> (@error Error)
  throw %e : $Error
}

// CHECK-LABEL: sil [ossa] @testDirectPartialApplyPhi
sil [ossa] @testDirectPartialApplyPhi : $@convention(thin) (@inout Int) -> (@error Error) {
bb0(%0 : $*Int):
  cond_br undef, bb1, bb2

bb1:
  %f1 = function_ref @closureForDirectPartialApplyTakingInout : $@convention(thin) (@inout Int, @inout_aliasable Int) -> ()
  %pa1 = partial_apply [callee_guaranteed] %f1(%0) : $@convention(thin) (@inout Int, @inout_aliasable Int) -> ()
  br bb3(%pa1 : $@callee_guaranteed (@inout Int) -> ())

bb2:
  %f2 = function_ref @closureForDirectPartialApplyTakingInout : $@convention(thin) (@inout Int, @inout_aliasable Int) -> ()
  %pa2 = partial_apply [callee_guaranteed] %f2(%0) : $@convention(thin) (@inout Int, @inout_aliasable Int) -> ()
  br bb3(%pa2 : $@callee_guaranteed (@inout Int) -> ())

bb3(%pa3 : @owned $@callee_guaranteed (@inout Int) -> ()):
  %esc3 = convert_escape_to_noescape [not_guaranteed] %pa3 : $@callee_guaranteed (@inout Int) -> () to $@noescape @callee_guaranteed (@inout Int) -> ()
  destroy_value %pa3 : $@callee_guaranteed (@inout Int) -> ()
  %access = begin_access [modify] [static] %0 : $*Int   // expected-error 2 {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %call = apply %esc3(%access) : $@noescape @callee_guaranteed (@inout Int) -> ()
  destroy_value %esc3 : $@noescape @callee_guaranteed (@inout Int) -> ()
  end_access %access : $*Int
  %v = tuple ()
  return %v : $()
}

// -----------------------------------------------------------------------------
// Test withoutActuallyEscaping thunk.
// <rdar://problem/43059088> Assertion in DiagnoseStaticExclusivity
// Noescape closure verification should not assert.

sil @takeEscapingClosure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()

sil private [ossa] @nestedInWithoutActuallyEscapingVerify : $@convention(thin) (@inout_aliasable Int) -> () {
bb0(%0 : $*Int):
  %a = begin_access [modify] [unknown] %0 : $*Int
  end_access %a : $*Int
  %v = tuple ()
  return %v : $()
}

// CHECK-LABEL: sil hidden [ossa] @testWithoutActuallyEscapingVerify : $@convention(thin) (@inout Int) -> () {
sil hidden [ossa] @testWithoutActuallyEscapingVerify : $@convention(thin) (@inout Int) -> () {
bb0(%0 : $*Int):
  %2 = function_ref @nestedInWithoutActuallyEscapingVerify : $@convention(thin) (@inout_aliasable Int) -> ()
  %3 = partial_apply [callee_guaranteed] %2(%0) : $@convention(thin) (@inout_aliasable Int) -> ()

  %4 = function_ref @thunkForWithoutActuallyEscapingVerify : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
  %3a = copy_value %3 : $@callee_guaranteed () -> ()
  %5 = partial_apply [callee_guaranteed] %4(%3a) : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
  %6 = mark_dependence %5 : $@callee_guaranteed () -> () on %3 : $@callee_guaranteed () -> ()

  %8 = function_ref @closureForWithoutActuallyEscapingVerify : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
  %9 = apply %8(%6) : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
  %10 = is_escaping_closure %6 : $@callee_guaranteed () -> ()
  cond_fail %10 : $Builtin.Int1
  destroy_value %3 : $@callee_guaranteed () -> ()
  destroy_value %6 : $@callee_guaranteed () -> ()
  %14 = tuple ()
  return %14 : $()
}

// CHECK-LABEL: sil shared [transparent] [serialized] [reabstraction_thunk] [without_actually_escaping] [ossa] @thunkForWithoutActuallyEscapingVerify : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> () {
sil shared [transparent] [serialized] [reabstraction_thunk] [without_actually_escaping] [ossa] @thunkForWithoutActuallyEscapingVerify : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> () {
bb0(%0 : @guaranteed $@callee_guaranteed () -> ()):
  %1 = apply %0() : $@callee_guaranteed () -> ()
  return %1 : $()
}

sil private [ossa] @closureForWithoutActuallyEscapingVerify : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> () {
bb0(%0 : @guaranteed $@callee_guaranteed () -> ()):
  %2 = function_ref @takeEscapingClosure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
  %3 = apply %2(%0) : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
  %4 = tuple ()
  return %4 : $()
}

// -----------------------------------------------------------------------------
// Test withoutActuallyEscaping convert_function.
// <rdar://problem/43059088> Assertion in DiagnoseStaticExclusivity
// Noescape closure verification should not assert.

// CHECK-LABEL: sil hidden [ossa] @testWithoutActuallyEscapingBlockVerify : $@convention(thin) (Int) -> () {
// CHECK: convert_function %{{.*}} : $@convention(block) () -> () to [without_actually_escaping] $@convention(block) () -> ()
sil hidden [ossa] @testWithoutActuallyEscapingBlockVerify : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
  %box = alloc_box ${ var Int }, var, name "localVal"
  %projbox = project_box %box : ${ var Int }, 0
  store %0 to [trivial] %projbox : $*Int
  %closureF = function_ref @testWithoutActuallyEscapingBlockVerifyClosure : $@convention(thin) (@inout_aliasable Int) -> ()
  %closure = partial_apply [callee_guaranteed] %closureF(%projbox) : $@convention(thin) (@inout_aliasable Int) -> ()
  %block = alloc_stack $@block_storage @callee_guaranteed () -> ()
  %blockproj = project_block_storage %block : $*@block_storage @callee_guaranteed () -> ()
  store %closure to [init] %blockproj : $*@callee_guaranteed () -> ()
  // function_ref thunk for @escaping @callee_guaranteed () -> ()
  %thunkF = function_ref @$sIeg_IeyB_TR : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> ()
  %initblock_unowned = init_block_storage_header %block : $*@block_storage @callee_guaranteed () -> (), invoke %thunkF : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> (), type $@convention(block) () -> ()
  %initblock = copy_block %initblock_unowned : $@convention(block) () -> ()
  destroy_addr %blockproj : $*@callee_guaranteed () -> ()
  dealloc_stack %block : $*@block_storage @callee_guaranteed () -> ()
  %borrow = begin_borrow %initblock : $@convention(block) () -> ()
  %copy = copy_value %borrow : $@convention(block) () -> ()
  %escapingF = convert_function %copy : $@convention(block) () -> () to [without_actually_escaping] $@convention(block) () -> ()
  %clauseF = function_ref @testWithoutActuallyEscapingBlockVerifyClause : $@convention(thin) (@guaranteed @convention(block) () -> ()) -> ()
  %call = apply %clauseF(%escapingF) : $@convention(thin) (@guaranteed @convention(block) () -> ()) -> ()
  destroy_value %escapingF : $@convention(block) () -> ()
  end_borrow %borrow : $@convention(block) () -> ()
  destroy_value %initblock : $@convention(block) () -> ()
  destroy_value %box : ${ var Int }
  %v = tuple ()
  return %v : $()
}

sil [ossa] @testWithoutActuallyEscapingBlockVerifyClosure : $@convention(thin) (@inout_aliasable Int) -> ()

// thunk for @escaping @callee_guaranteed () -> ()
sil shared [transparent] [serialized] [reabstraction_thunk] [ossa] @$sIeg_IeyB_TR : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> () {
// %0
bb0(%0 : $*@block_storage @callee_guaranteed () -> ()):
  %1 = project_block_storage %0 : $*@block_storage @callee_guaranteed () -> ()
  %2 = load [copy] %1 : $*@callee_guaranteed () -> ()
  %3 = begin_borrow %2 : $@callee_guaranteed () -> ()
  %4 = apply %3() : $@callee_guaranteed () -> ()
  end_borrow %3 : $@callee_guaranteed () -> ()
  %6 = tuple ()
  destroy_value %2 : $@callee_guaranteed () -> ()
  return %6 : $()
} // end sil function '$sIeg_IeyB_TR'

sil private [ossa] @testWithoutActuallyEscapingBlockVerifyClause : $@convention(thin) (@guaranteed @convention(block) () -> ()) -> () {
bb0(%0 : @guaranteed $@convention(block) () -> ()):
  %1 = copy_block %0 : $@convention(block) () -> ()
  %3 = begin_borrow %1 : $@convention(block) () -> ()
  %4 = copy_value %3 : $@convention(block) () -> ()
  %5 = apply %4() : $@convention(block) () -> ()
  destroy_value %4 : $@convention(block) () -> ()
  end_borrow %3 : $@convention(block) () -> ()
  destroy_value %1 : $@convention(block) () -> ()
  %v = tuple ()
  return %v : $()
}

//-----------------------------------------------------------------------------
// Test dynamically replaceable. Diagnostics should not be able to
// make assumptions about the accesses in the callee function.

struct TestDynamic {
  @_hasStorage @_hasInitialValue var x: Builtin.Int64 { get set }
  @_hasStorage @_hasInitialValue var y: Builtin.Int64 { get set }
  public mutating func foo()
  init(x: Builtin.Int64, y: Builtin.Int64)
  init()
}

@_hasStorage @_hasInitialValue var s: S { get set }

// CHECK-LABEL: sil hidden [ossa] @testCallDynamic : $@convention(method) (@inout TestDynamic) -> () {
sil hidden [ossa] @testCallDynamic : $@convention(method) (@inout TestDynamic) -> () {
bb0(%0 : $*TestDynamic):
  %access = begin_access [modify] [unknown] %0 : $*TestDynamic // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %sea = struct_element_addr %access : $*TestDynamic, #TestDynamic.x
  %f = dynamic_function_ref @testDynamicLocal : $@convention(thin) (@inout Builtin.Int64, @inout_aliasable TestDynamic) -> ()
  %call = apply %f(%sea, %0) : $@convention(thin) (@inout Builtin.Int64, @inout_aliasable TestDynamic) -> () // expected-note {{conflicting access is here}}
  end_access %access : $*TestDynamic
  %v = tuple ()
  return %v : $()
}

// This callee actually accesses different subpaths, but the caller's
// diagnostic should not be able to see that.
sil private [dynamically_replacable] [ossa] @testDynamicLocal : $@convention(thin) (@inout Builtin.Int64, @inout_aliasable TestDynamic) -> () {
bb0(%0 : $*Builtin.Int64, %1 : $*TestDynamic):
  %literal = integer_literal $Builtin.Int64, 1
  %access1 = begin_access [modify] [unknown] %0 : $*Builtin.Int64
  assign %literal to %access1 : $*Builtin.Int64
  end_access %access1 : $*Builtin.Int64
  %access2 = begin_access [modify] [unknown] %1 : $*TestDynamic
  %sea = struct_element_addr %access2 : $*TestDynamic, #TestDynamic.y
  assign %literal to %sea : $*Builtin.Int64
  end_access %access2 : $*TestDynamic
  %19 = tuple ()
  return %19 : $()
}

// Dictionary.subscript.modify
sil [serialized] [always_inline] @$sSD_7defaultq_x_q_yXKtciM : $@yield_once @convention(method) <τ_0_0, τ_0_1 where τ_0_0 : Hashable> (@in_guaranteed τ_0_0, @noescape @callee_guaranteed () -> @out τ_0_1, @inout Dictionary<τ_0_0, τ_0_1>) -> @yields @inout τ_0_1

// -----------------------------------------------------------------------------
// testBeginApplyOfClosure: DiagnoseStaticExclusivity must consider
// begin_apply as a user of accessed variables.
//
// Test a static exclusivity conflict between an outgoing coroutine
// argument and the closure passed to the coroutine.
//   %access = begin_access [modify] %0
//   begin_apply %coroutine(%access, %closure) -- where the closure captures %0

// CHECK-LABEL: sil private [ossa] @closureForBeginApplyOfClosure : $@convention(thin) (@inout_aliasable Int) -> @out Int {
sil private [ossa] @closureForBeginApplyOfClosure : $@convention(thin) (@inout_aliasable Int) -> @out Int {
bb0(%0 : $*Int, %1 : $*Int):
  %access = begin_access [read] [static] %1 : $*Int // expected-note {{conflicting access is here}}
  %val = load [trivial] %access : $*Int
  end_access %access : $*Int
  store %val to [trivial] %0 : $*Int
  %v = tuple ()
  return %v : $()
}

// CHECK-LABEL: sil [ossa] @testBeginApplyOfClosure : $@convention(thin) (@inout Int, @inout Dictionary<Int, Int>) -> () {
sil [ossa] @testBeginApplyOfClosure : $@convention(thin) (@inout Int, @inout Dictionary<Int, Int>) -> () {
bb0(%0 : $*Int, %1 : $*Dictionary<Int, Int>):
  %f = function_ref @closureForBeginApplyOfClosure : $@convention(thin) (@inout_aliasable Int) -> @out Int
  %pa = partial_apply [callee_guaranteed] %f(%0) : $@convention(thin) (@inout_aliasable Int) -> @out Int
  %cvt = convert_escape_to_noescape [not_guaranteed] %pa : $@callee_guaranteed () -> @out Int to $@noescape @callee_guaranteed () -> @out Int
  // function_ref Dictionary.subscript.modify
  %mod = function_ref @$sSD_7defaultq_x_q_yXKtciM : $@yield_once @convention(method) <τ_0_0, τ_0_1 where τ_0_0 : Hashable> (@in_guaranteed τ_0_0, @noescape @callee_guaranteed () -> @out τ_0_1, @inout Dictionary<τ_0_0, τ_0_1>) -> @yields @inout τ_0_1
  %access = begin_access [modify] [static] %0 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  (%yield, %token) = begin_apply %mod<Int, Int>(%access, %cvt, %1) : $@yield_once @convention(method) <τ_0_0, τ_0_1 where τ_0_0 : Hashable> (@in_guaranteed τ_0_0, @noescape @callee_guaranteed () -> @out τ_0_1, @inout Dictionary<τ_0_0, τ_0_1>) -> @yields @inout τ_0_1
  end_apply %token
  end_access %access : $*Int
  destroy_value %cvt : $@noescape @callee_guaranteed () -> @out Int
  destroy_value %pa : $@callee_guaranteed () -> @out Int
  %v = tuple ()
  return %v : $()
}

// -----------------------------------------------------------------------------
// testCoroutineWithClosureArg: AccessedSummaryAnalysis must consider
// begin_apply a valid user of partial_apply.
//
// Test that this does not assert in hasExpectedUsesOfNoEscapePartialApply.
//
// This test needs two closures, one to capture the variable, another
// to recapture the variable, so AccessSummary is forced to process
// the closure.
// CHECK-LABEL: sil hidden [ossa] @testCoroutineWithClosureArg : $@convention(thin) (Int, @inout Int, @inout Dictionary<Int, Int>) -> () {
sil hidden [ossa] @testCoroutineWithClosureArg : $@convention(thin) (Int, @inout Int, @inout Dictionary<Int, Int>) -> () {
bb0(%0 : $Int, %1 : $*Int, %2 : $*Dictionary<Int, Int>):
  %6 = function_ref @closureForTestCoroutineWithClosureArg : $@convention(thin) (@inout_aliasable Dictionary<Int, Int>, Int, @inout_aliasable Int) -> ()
  %7 = apply %6(%2, %0, %1) : $@convention(thin) (@inout_aliasable Dictionary<Int, Int>, Int, @inout_aliasable Int) -> ()
  %8 = tuple ()
  return %8 : $()
}

// thunk for @callee_guaranteed () -> (@unowned Int)
sil shared [transparent] [serialized] [reabstraction_thunk] [ossa] @$sSiIgd_SiIegr_TR : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> Int) -> @out Int {
bb0(%0 : $*Int, %1 : @guaranteed $@noescape @callee_guaranteed () -> Int):
  %2 = apply %1() : $@noescape @callee_guaranteed () -> Int
  store %2 to [trivial] %0 : $*Int
  %4 = tuple ()
  return %4 : $()
}

// CHECK-LABEL: sil private [ossa] @closureForTestCoroutineWithClosureArg : $@convention(thin) (@inout_aliasable Dictionary<Int, Int>, Int, @inout_aliasable Int) -> () {
sil private [ossa] @closureForTestCoroutineWithClosureArg : $@convention(thin) (@inout_aliasable Dictionary<Int, Int>, Int, @inout_aliasable Int) -> () {
bb0(%0 : $*Dictionary<Int, Int>, %1 : $Int, %2 : $*Int):
  %6 = function_ref @implicitClosureForTestCoroutineWithClosureArg : $@convention(thin) (@inout_aliasable Int) -> Int
  %7 = partial_apply [callee_guaranteed] %6(%2) : $@convention(thin) (@inout_aliasable Int) -> Int
  %8 = convert_escape_to_noescape [not_guaranteed] %7 : $@callee_guaranteed () -> Int to $@noescape @callee_guaranteed () -> Int
  %13 = begin_access [modify] [unknown] %0 : $*Dictionary<Int, Int>
  %14 = alloc_stack $Int
  store %1 to [trivial] %14 : $*Int
  // thunk for @callee_guaranteed () -> (@unowned Int)
  %16 = function_ref @$sSiIgd_SiIegr_TR : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> Int) -> @out Int
  %17 = partial_apply [callee_guaranteed] %16(%8) : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> Int) -> @out Int
  %18 = convert_escape_to_noescape [not_guaranteed] %17 : $@callee_guaranteed () -> @out Int to $@noescape @callee_guaranteed () -> @out Int
  destroy_value %17 : $@callee_guaranteed () -> @out Int
  // Dictionary.subscript.modify
  %20 = function_ref @$sSD_7defaultq_x_q_yXKtciM : $@yield_once @convention(method) <τ_0_0, τ_0_1 where τ_0_0 : Hashable> (@in_guaranteed τ_0_0, @noescape @callee_guaranteed () -> @out τ_0_1, @inout Dictionary<τ_0_0, τ_0_1>) -> @yields @inout τ_0_1
  (%21, %22) = begin_apply %20<Int, Int>(%14, %18, %13) : $@yield_once @convention(method) <τ_0_0, τ_0_1 where τ_0_0 : Hashable> (@in_guaranteed τ_0_0, @noescape @callee_guaranteed () -> @out τ_0_1, @inout Dictionary<τ_0_0, τ_0_1>) -> @yields @inout τ_0_1
  assign %1 to %21 : $*Int
  end_apply %22
  destroy_value %18 : $@noescape @callee_guaranteed () -> @out Int
  end_access %13 : $*Dictionary<Int, Int>
  dealloc_stack %14 : $*Int
  destroy_value %7 : $@callee_guaranteed () -> Int
  %28 = tuple ()
  return %28 : $()
}

// CHECK-LABEL: sil private [transparent] [ossa] @implicitClosureForTestCoroutineWithClosureArg : $@convention(thin) (@inout_aliasable Int) -> Int {
sil private [transparent] [ossa] @implicitClosureForTestCoroutineWithClosureArg : $@convention(thin) (@inout_aliasable Int) -> Int {
bb0(%0 : $*Int):
  %2 = begin_access [read] [unknown] %0 : $*Int
  %3 = load [trivial] %2 : $*Int
  end_access %2 : $*Int
  return %3 : $Int
}

// -----------------------------------------------------------------------------
// testExternalWithClosureArg: handle inout arguments to resilient functions
// conservatively.

sil [ossa] @externalWithInout : $@convention(thin) (@inout Int) -> ()

// CHECK-LABEL: sil private [ossa] @closureForTestExternalWithClosureArg : $@convention(thin) (@inout_aliasable Int) -> () {
sil private [ossa] @closureForTestExternalWithClosureArg : $@convention(thin) (@inout_aliasable Int) -> () {
bb0(%0 : $*Int):
  %f = function_ref @externalWithInout : $@convention(thin) (@inout Int) -> ()
  %call = apply %f(%0) : $@convention(thin) (@inout Int) -> () // expected-note {{conflicting access is here}}
  %v = tuple ()
  return %v : $()
}

sil [ossa] @calleeForTestExternalWithClosureArg : $@convention(thin) (@inout Int, @noescape @callee_guaranteed () -> ()) -> ()

// CHECK-LABEL: sil hidden [ossa] @testExternalWithClosureArg : $@convention(thin) (@inout Int) -> () {
sil hidden [ossa] @testExternalWithClosureArg : $@convention(thin) (@inout Int) -> () {
bb0(%0 : $*Int):
  %2 = function_ref @closureForTestExternalWithClosureArg : $@convention(thin) (@inout_aliasable Int) -> ()
  %3 = partial_apply [callee_guaranteed] %2(%0) : $@convention(thin) (@inout_aliasable Int) -> ()
  %4 = convert_escape_to_noescape [not_guaranteed] %3 : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
  %5 = begin_access [modify] [unknown] %0 : $*Int // expected-error {{overlapping accesses, but modification requires exclusive access; consider copying to a local variable}}
  %6 = function_ref @calleeForTestExternalWithClosureArg : $@convention(thin) (@inout Int, @noescape @callee_guaranteed () -> ()) -> ()
  %7 = apply %6(%5, %4) : $@convention(thin) (@inout Int, @noescape @callee_guaranteed () -> ()) -> ()
  end_access %5 : $*Int
  destroy_value %4 : $@noescape @callee_guaranteed () -> ()
  destroy_value %3 : $@callee_guaranteed () -> ()
  %10 = tuple ()
  return %10 : $()
}