File: SILGenProlog.cpp

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 (1612 lines) | stat: -rw-r--r-- 63,803 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
//===--- SILGenProlog.cpp - Function prologue emission --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "ArgumentSource.h"
#include "ExecutorBreadcrumb.h"
#include "FunctionInputGenerator.h"
#include "Initialization.h"
#include "ManagedValue.h"
#include "SILGenFunction.h"
#include "Scope.h"

#include "swift/AST/CanTypeVisitor.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PropertyWrappers.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Generators.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILArgumentConvention.h"
#include "swift/SIL/SILInstruction.h"

using namespace swift;
using namespace Lowering;

template <typename... T, typename... U>
static void diagnose(ASTContext &Context, SourceLoc loc, Diag<T...> diag,
                     U &&...args) {
  Context.Diags.diagnose(loc, diag, std::forward<U>(args)...);
}

SILValue SILGenFunction::emitSelfDeclForDestructor(VarDecl *selfDecl) {
  SILFunctionConventions conventions = F.getConventionsInContext();

  // Emit the implicit 'self' argument.
  SILType selfType = conventions.getSILArgumentType(
      conventions.getNumSILArguments() - 1, F.getTypeExpansionContext());
  selfType = F.mapTypeIntoContext(selfType);
  SILValue selfValue = F.begin()->createFunctionArgument(selfType, selfDecl);

  uint16_t ArgNo = 1; // Hardcoded for destructors.
  auto dv = SILDebugVariable(selfDecl->isLet(), ArgNo);

  // If we have a move only type, then mark it with
  // mark_unresolved_non_copyable_value so we can't escape it.
  //
  // For now, we do not handle move only class deinits. This is because we need
  // to do a bit more refactoring to handle the weird way that it deals with
  // ownership. But for simple move only deinits (like struct/enum), that are
  // owned, lets mark them as needing to be no implicit copy checked so they
  // cannot escape.
  if (selfType.isMoveOnly() && !selfType.isAnyClassReferenceType()) {
    SILValue addr = B.createAllocStack(selfDecl, selfValue->getType(), dv);
    addr = B.createMarkUnresolvedNonCopyableValueInst(
        selfDecl, addr,
        MarkUnresolvedNonCopyableValueInst::CheckKind::ConsumableAndAssignable);
    if (selfValue->getType().isObject()) {
      B.createStore(selfDecl, selfValue, addr, StoreOwnershipQualifier::Init);
    } else {
      B.createCopyAddr(selfDecl, selfValue, addr, IsTake, IsInitialization);
    }
    // drop_deinit invalidates any user-defined struct/enum deinit
    // before the individual members are destroyed.
    addr = B.createDropDeinit(selfDecl, addr);
    selfValue = addr;
  }

  VarLocs[selfDecl] = VarLoc::get(selfValue);
  SILLocation PrologueLoc(selfDecl);
  PrologueLoc.markAsPrologue();
  B.createDebugValue(PrologueLoc, selfValue, dv);
  return selfValue;
}

namespace {
struct LoweredParamGenerator {
  SILGenFunction &SGF;
  CanSILFunctionType fnTy;
  ArrayRefGenerator<ArrayRef<SILParameterInfo>> parameterTypes;

  LoweredParamGenerator(SILGenFunction &SGF,
                        unsigned numIgnoredTrailingParameters)
    : SGF(SGF), fnTy(SGF.F.getLoweredFunctionType()),
      parameterTypes(
          SGF.F.getLoweredFunctionTypeInContext(SGF.B.getTypeExpansionContext())
              ->getParameters().drop_back(numIgnoredTrailingParameters)) {}

  ParamDecl *paramDecl = nullptr;
  bool isNoImplicitCopy = false;
  LifetimeAnnotation lifetimeAnnotation = LifetimeAnnotation::None;

  void configureParamData(ParamDecl *paramDecl, bool isNoImplicitCopy,
                          LifetimeAnnotation lifetimeAnnotation) {
    this->paramDecl = paramDecl;
    this->isNoImplicitCopy = isNoImplicitCopy;
    this->lifetimeAnnotation = lifetimeAnnotation;
  }
  void resetParamData() {
    configureParamData(nullptr, false, LifetimeAnnotation::None);
  }

  ManagedValue claimNext() {
    auto parameterInfo = parameterTypes.claimNext();

    // We should only be called without a param decl when pulling
    // pack parameters out for multiple formal parameters (or a single
    // formal parameter pack).
    // TODO: preserve the parameters captured by the pack into the SIL
    // representation.
    bool isFormalParameterPack = (paramDecl == nullptr);
    assert(!isFormalParameterPack || parameterInfo.isPack());

    auto paramType =
        SGF.F.mapTypeIntoContext(SGF.getSILType(parameterInfo, fnTy));
    ManagedValue mv = SGF.B.createInputFunctionArgument(
        paramType, paramDecl, isNoImplicitCopy, lifetimeAnnotation,
        /*isClosureCapture*/ false, isFormalParameterPack);
    return mv;
  }

  bool isFinished() const {
    return parameterTypes.isFinished();
  }

  void advance() {
    (void) claimNext();
  }

  void finish() {
    parameterTypes.finish();
  }
};

struct WritebackReabstractedInoutCleanup final : Cleanup {
  SILValue OrigAddress, SubstAddress;
  AbstractionPattern OrigTy;
  CanType SubstTy;
  WritebackReabstractedInoutCleanup(SILValue origAddress, SILValue substAddress,
                                    AbstractionPattern origTy,
                                    CanType substTy)
      : OrigAddress(origAddress), SubstAddress(substAddress),
        OrigTy(origTy), SubstTy(substTy)
  {}
  
  void emit(SILGenFunction &SGF, CleanupLocation l, ForUnwind_t forUnwind)
  override {
    Scope s(SGF.Cleanups, l);
    // Load the final local value coming in.
    auto mv = SGF.emitLoad(l, SubstAddress,
                           SGF.getTypeLowering(SubstAddress->getType()),
                           SGFContext(), IsTake);
    // Reabstract the value back to the original representation.
    mv = SGF.emitSubstToOrigValue(l, mv.ensurePlusOne(SGF, l),
                                  OrigTy, SubstTy);
    // Write it back to the original inout parameter.
    SGF.B.createStore(l, mv.forward(SGF), OrigAddress,
                      StoreOwnershipQualifier::Init);
  }
  
  void dump(SILGenFunction&) const override {
    llvm::errs() << "WritebackReabstractedInoutCleanup\n";
    OrigAddress->print(llvm::errs());
    SubstAddress->print(llvm::errs());
  }
};

class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
                                              /*RetTy*/ ManagedValue,
                                              /*ArgTys...*/ AbstractionPattern,
                                              Initialization *>
{
public:
  SILGenFunction &SGF;
  SILLocation loc;
  LoweredParamGenerator &parameters;

  EmitBBArguments(SILLocation l, LoweredParamGenerator &parameters)
      : SGF(parameters.SGF), loc(l), parameters(parameters) {}

  ManagedValue claimNextParameter() {
    return parameters.claimNext();
  }

  ManagedValue handleParam(AbstractionPattern origType, CanType substType,
                           ParamDecl *pd) {
    // Note: inouts of tuples are not exploded, so we bypass visit().
    if (pd->isInOut())
      return handleInOut(origType, substType);
    return visit(substType, origType, /*emitInto*/ nullptr);
  }

  ManagedValue handlePackComponent(FunctionInputGenerator &formalParam) {
    auto origPatternType =
      formalParam.getOrigType().getPackExpansionPatternType();

    auto substParam = formalParam.getSubstParam();
    CanType substType = substParam.getParameterType();

    // Forward the pack cleanup and enter a new cleanup for the
    // remaining components.
    auto componentValue = formalParam.projectPackComponent(SGF, loc);

    // Handle scalar components.
    if (!isa<PackExpansionType>(substType)) {
      return handleScalar(componentValue, origPatternType, substType,
                          /*emit into*/ nullptr, substParam.isInOut());
    }

    auto componentPackTy = componentValue.getType().castTo<SILPackType>();

    // Handle pack expansion components.
    auto formalPackType = formalParam.getFormalPackType();
    auto componentIndex = formalParam.getPackComponentIndex();

    auto expectedExpansionTy = SGF.getLoweredRValueType(substType);
    auto expectedPackTy =
        SILPackType::get(SGF.getASTContext(), componentPackTy->getExtInfo(),
                         {expectedExpansionTy});

    // If we don't need a pack transformation, this is simple.
    // This is simultaneously testing that we don't need a transformation
    // and that we don't have other components in the pack.
    if (componentPackTy == expectedPackTy) {
      return componentValue;
    }

    // FIXME: perform this forwarding by just slicing the original pack.
    bool canForward =
      (expectedExpansionTy == componentPackTy->getElementType(componentIndex));

    auto rawOutputPackAddr =
      SGF.emitTemporaryPackAllocation(loc,
        SILType::getPrimitiveObjectType(expectedPackTy));
    auto outputFormalPackType =
      CanPackType::get(SGF.getASTContext(), {substType});
    return SGF.emitPackTransform(loc, componentValue,
                                 formalPackType, componentIndex,
                                 rawOutputPackAddr, outputFormalPackType, 0,
                                 canForward, /*plus one*/ !canForward,
                                 [&](ManagedValue input, SILType outputTy,
                                     SGFContext context) {
      if (canForward) return input;

      auto substEltType =
        cast<PackExpansionType>(substType).getPatternType();
      if (auto openedEnv = SGF.getInnermostPackExpansion()->OpenedElementEnv) {
        substEltType =
          openedEnv->mapContextualPackTypeIntoElementContext(substEltType);
      }

      return handleScalar(input, origPatternType, substEltType,
                          context.getEmitInto(), /*inout*/ false);
    });
  }

  ManagedValue visitType(CanType t, AbstractionPattern orig,
                         Initialization *emitInto) {
    auto mv = claimNextParameter();
    return handleScalar(mv, orig, t, emitInto, /*inout*/ false);
  }

  ManagedValue handleInOut(AbstractionPattern orig, CanType t) {
    auto mv = claimNextParameter();
    return handleScalar(mv, orig, t, /*emitInto*/ nullptr, /*inout*/ true);
  }

  ManagedValue handleScalar(ManagedValue mv,
                            AbstractionPattern orig, CanType t,
                            Initialization *emitInto, bool isInOut) {
    assert(!(isInOut && emitInto != nullptr));

    auto argType = SGF.getLoweredType(t, mv.getType().getCategory());

    // This is a hack to deal with the fact that Self.Type comes in as a static
    // metatype, but we have to downcast it to a dynamic Self metatype to get
    // the right semantics.
    if (argType != mv.getType()) {
      if (auto argMetaTy = argType.getAs<MetatypeType>()) {
        if (auto argSelfTy = dyn_cast<DynamicSelfType>(argMetaTy.getInstanceType())) {
          assert(argSelfTy.getSelfType()
                   == mv.getType().castTo<MetatypeType>().getInstanceType());
          mv = SGF.B.createUncheckedBitCast(loc, mv, argType);
        }
      }
    }
    if (isInOut) {
      // If we are inout and are move only, insert a note to the move checker to
      // check ownership.
      if (mv.getType().isMoveOnly() && !mv.getType().isMoveOnlyWrapped())
        mv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
            loc, mv,
            MarkUnresolvedNonCopyableValueInst::CheckKind::
                ConsumableAndAssignable);

      // If the value needs to be reabstracted, set up a shadow copy with
      // writeback here.
      if (argType.getASTType() != mv.getType().getASTType()) {
        // Load the value coming in.
        auto origBuf = mv.getValue();
        mv = SGF.emitLoad(loc, origBuf, SGF.getTypeLowering(mv.getType()), SGFContext(), IsTake);
        // Reabstract the value if necessary.
        mv = SGF.emitOrigToSubstValue(loc, mv.ensurePlusOne(SGF, loc), orig, t);
        // Store the value to a local buffer.
        auto substBuf = SGF.emitTemporaryAllocation(loc, argType);
        SGF.B.createStore(loc, mv.forward(SGF), substBuf, StoreOwnershipQualifier::Init);
        // Introduce a writeback to put the final value back in the inout.
        SGF.Cleanups.pushCleanup<WritebackReabstractedInoutCleanup>(origBuf, substBuf, orig, t);
        mv = ManagedValue::forLValue(substBuf);
      }

      return mv;
    }

    // This can happen if the value is resilient in the calling convention
    // but not resilient locally.
    bool argIsLoadable = argType.isLoadable(SGF.F);
    if (argIsLoadable) {
      if (argType.isAddress()) {
        mv = SGF.B.createLoadWithSameOwnership(loc, mv);
        argType = argType.getObjectType();
      }
    }

    assert(argType.getCategory() == mv.getType().getCategory());
    if (argType.getASTType() != mv.getType().getASTType()) {
      // Reabstract the value if necessary.
      mv = SGF.emitOrigToSubstValue(loc, mv.ensurePlusOne(SGF, loc), orig, t);
    }

    if (parameters.isNoImplicitCopy && !argIsLoadable) {
      // We do not support no implicit copy address only types. Emit an error.
      auto diag = diag::noimplicitcopy_used_on_generic_or_existential;
      diagnose(SGF.getASTContext(), mv.getValue().getLoc().getSourceLoc(),
               diag);
    }

    // If the value is a (possibly optional) ObjC block passed into the entry
    // point of the function, then copy it so we can treat the value reliably
    // as a heap object. Escape analysis can eliminate this copy if it's
    // unneeded during optimization.
    CanType objectType = t;
    if (auto theObjTy = t.getOptionalObjectType())
      objectType = theObjTy;
    if (isa<FunctionType>(objectType) &&
        cast<FunctionType>(objectType)->getRepresentation()
              == FunctionType::Representation::Block) {
      SILValue blockCopy = SGF.B.createCopyBlock(loc, mv.getValue());
      mv = SGF.emitManagedRValueWithCleanup(blockCopy);
    }

    if (emitInto) {
      if (mv.isPlusOneOrTrivial(SGF))
        mv.forwardInto(SGF, loc, emitInto);
      else
        mv.copyInto(SGF, loc, emitInto);
      return ManagedValue::forInContext();
    }

    return mv;
  }

  ManagedValue visitPackExpansionType(CanPackExpansionType t,
                                      AbstractionPattern orig,
                                      Initialization *emitInto) {
    // Pack expansions in the formal parameter list are made
    // concrete as packs.
    return visitType(PackType::get(SGF.getASTContext(), {t})
                       ->getCanonicalType(),
                     orig, emitInto);
  }

  ManagedValue visitTupleType(CanTupleType t, AbstractionPattern orig,
                              Initialization *emitInto) {
    // Only destructure if the abstraction pattern is also a tuple.
    if (!orig.isTuple())
      return visitType(t, orig, emitInto);

    auto &tl = SGF.SGM.Types.getTypeLowering(t, SGF.getTypeExpansionContext());

    // If the tuple contains pack expansions, and we're not emitting
    // into an initialization already, create a temporary so that we're
    // always emitting into an initialization.
    if (t.containsPackExpansionType() && !emitInto) {
      auto temporary = SGF.emitTemporary(loc, tl);

      auto result = expandTuple(orig, t, tl, temporary.get());
      assert(result.isInContext()); (void) result;

      return temporary->getManagedAddress();
    }

    return expandTuple(orig, t, tl, emitInto);
  }

  ManagedValue expandTuple(AbstractionPattern orig, CanTupleType t,
                           const TypeLowering &tl, Initialization *init) {
    assert((!t.containsPackExpansionType() || init) &&
           "should always have an emission context when expanding "
           "a tuple containing pack expansions");

    bool canBeGuaranteed = tl.isLoadable();

    // We only use specific initializations here that can always be split.
    SmallVector<InitializationPtr, 8> eltInitsBuffer;
    MutableArrayRef<InitializationPtr> eltInits;
    if (init) {
      assert(init->canSplitIntoTupleElements());
      eltInits = init->splitIntoTupleElements(SGF, loc, t, eltInitsBuffer);
    }

    // Collect the exploded elements.
    //
    // Reabstraction can give us original types that are pack
    // expansions without having pack expansions in the result.
    // In this case, we do not need to force emission into a pack
    // expansion.
    SmallVector<ManagedValue, 4> elements;
    orig.forEachTupleElement(t, [&](TupleElementGenerator &elt) {
      auto origEltType = elt.getOrigType();
      auto substEltTypes = elt.getSubstTypes();
      if (!elt.isOrigPackExpansion()) {
        auto eltValue =
          visit(substEltTypes[0], origEltType,
                init ? eltInits[elt.getSubstIndex()].get() : nullptr);
        assert((init != nullptr) == (eltValue.isInContext()));
        if (!eltValue.isInContext())
          elements.push_back(eltValue);

        if (eltValue.hasCleanup())
          canBeGuaranteed = false;
      } else {
        assert(init);
        expandPack(origEltType, substEltTypes, elt.getSubstIndex(),
                   eltInits.slice(elt.getSubstIndex(), substEltTypes.size()),
                   elements);
      }
    });

    // If we emitted into a context, we're done.
    if (init) {
      init->finishInitialization(SGF);
      return ManagedValue::forInContext();
    }

    if (tl.isLoadable() || !SGF.silConv.useLoweredAddresses()) {
      SmallVector<SILValue, 4> elementValues;
      if (canBeGuaranteed) {
        // If all of the elements were guaranteed, we can form a guaranteed tuple.
        for (auto element : elements)
          elementValues.push_back(element.getUnmanagedValue());
      } else {
        // Otherwise, we need to move or copy values into a +1 tuple.
        for (auto element : elements) {
          SILValue value = element.hasCleanup()
            ? element.forward(SGF)
            : element.copyUnmanaged(SGF, loc).forward(SGF);
          elementValues.push_back(value);
        }
      }
      auto tupleValue = SGF.B.createTuple(loc, tl.getLoweredType(),
                                          elementValues);
      if (tupleValue->getOwnershipKind() == OwnershipKind::None)
        return ManagedValue::forObjectRValueWithoutOwnership(tupleValue);
      return canBeGuaranteed ? ManagedValue::forBorrowedObjectRValue(tupleValue)
                             : SGF.emitManagedRValueWithCleanup(tupleValue);
    } else {
      // If the type is address-only, we need to move or copy the elements into
      // a tuple in memory.
      // TODO: It would be a bit more efficient to use a preallocated buffer
      // in this case.
      auto buffer = SGF.emitTemporaryAllocation(loc, tl.getLoweredType());
      for (auto i : indices(elements)) {
        auto element = elements[i];
        auto elementBuffer = SGF.B.createTupleElementAddr(loc, buffer,
                                        i, element.getType().getAddressType());
        if (element.hasCleanup())
          element.forwardInto(SGF, loc, elementBuffer);
        else
          element.copyInto(SGF, loc, elementBuffer);
      }
      return SGF.emitManagedRValueWithCleanup(buffer);
    }
  }

  void expandPack(AbstractionPattern origExpansionType,
                  CanTupleEltTypeArrayRef substEltTypes,
                  size_t firstSubstEltIndex,
                  MutableArrayRef<InitializationPtr> eltInits,
                  SmallVectorImpl<ManagedValue> &eltMVs) {
    assert(substEltTypes.size() == eltInits.size());

    // The next parameter is a pack which corresponds to some number of
    // components in the tuple.  Some of them may be pack expansions.
    // Either copy/move them into the tuple (necessary if there are any
    // pack expansions) or collect them in eltMVs.

    // Claim the next parameter, remember whether it was +1, and forward
    // the cleanup.  We can get away with just forwarding the cleanup
    // up front, not destructuring it, because we assume that the work
    // we're doing here won't ever unwind.
    ManagedValue packAddrMV = claimNextParameter();
    CleanupCloner cloner(SGF, packAddrMV);
    SILValue packAddr = packAddrMV.forward(SGF);
    auto packTy = packAddr->getType().castTo<SILPackType>();

    auto origPatternType = origExpansionType.getPackExpansionPatternType();

    auto inducedPackType =
      CanPackType::get(SGF.getASTContext(), substEltTypes);

    for (auto packComponentIndex : indices(substEltTypes)) {
      CanType substComponentType = substEltTypes[packComponentIndex];
      Initialization *componentInit =
        eltInits.empty() ? nullptr : eltInits[packComponentIndex].get();
      auto packComponentTy = packTy->getSILElementType(packComponentIndex);

      auto substExpansionType =
        dyn_cast<PackExpansionType>(substComponentType);

      // In the scalar case, project out the element address from the
      // pack and use the normal scalar path to trigger initialization.
      if (!substExpansionType) {
        auto packIndex =
          SGF.B.createScalarPackIndex(loc, packComponentIndex, inducedPackType);
        auto eltAddr =
          SGF.B.createPackElementGet(loc, packIndex, packAddr,
                                     packComponentTy);
        auto eltAddrMV = cloner.clone(eltAddr);
        auto result = handleScalar(eltAddrMV, origPatternType,
                                   substComponentType, componentInit,
                                   /*inout*/ false);
        assert(result.isInContext() == (componentInit != nullptr));
        if (!result.isInContext())
          eltMVs.push_back(result);
        continue;
      }

      // In the pack-expansion case, do the exact same thing,
      // but in a pack loop.
      assert(componentInit);
      assert(componentInit->canPerformPackExpansionInitialization());

      SILType eltTy;
      CanType substEltType;
      auto openedEnv =
        SGF.createOpenedElementValueEnvironment({packComponentTy},
                                                {&eltTy},
                                                {substExpansionType},
                                                {&substEltType});

      SGF.emitDynamicPackLoop(loc, inducedPackType, packComponentIndex,
                              openedEnv, [&](SILValue indexWithinComponent,
                                             SILValue expansionPackIndex,
                                             SILValue packIndex) {
        componentInit->performPackExpansionInitialization(SGF, loc,
                                            indexWithinComponent,
                                            [&](Initialization *eltInit) {
          // Project out the pack element and enter a managed value for it.
          auto eltAddr =
            SGF.B.createPackElementGet(loc, packIndex, packAddr, eltTy);
          auto eltAddrMV = cloner.clone(eltAddr);

          auto result = handleScalar(eltAddrMV, origPatternType, substEltType,
                                     eltInit, /*inout*/ false);
          assert(result.isInContext()); (void) result;
        });
      });
      componentInit->finishInitialization(SGF);
    }
  }
};

/// A helper for creating SILArguments and binding variables to the argument
/// names.
class ArgumentInitHelper {
  SILGenFunction &SGF;

  LoweredParamGenerator loweredParams;
  uint16_t ArgNo = 0;

  std::optional<FunctionInputGenerator> FormalParamTypes;

public:
  ArgumentInitHelper(SILGenFunction &SGF,
                     unsigned numIgnoredTrailingParameters)
      : SGF(SGF), loweredParams(SGF, numIgnoredTrailingParameters) {}

  /// Emit the given list of parameters.
  unsigned emitParams(std::optional<AbstractionPattern> origFnType,
                      ParameterList *paramList, ParamDecl *selfParam) {
    // If have an orig function type, initialize FormalParamTypes.
    SmallVector<AnyFunctionType::Param, 8> substFormalParams;
    if (origFnType) {
      // Start by constructing an array of subst params that we can use
      // for the generator.  This array needs to stay in scope across
      // the loop below, while we're potentially using FormalParamTypes.

      auto addParamDecl = [&](ParamDecl *pd) {
        if (pd->hasExternalPropertyWrapper())
          pd = cast<ParamDecl>(pd->getPropertyWrapperBackingProperty());
        substFormalParams.push_back(
          pd->toFunctionParam(pd->getTypeInContext()).getCanonical(nullptr));
      };
      for (auto paramDecl : *paramList) {
        addParamDecl(paramDecl);
      }
      if (selfParam) {
        addParamDecl(selfParam);
      }

      // Initialize the formal parameter generator.  Note that this can
      // immediately claim lowered parameters.
      // Some of the callers to emitBasicProlog do ask it to ignore the
      // formal self parameter, but they do not pass an origFnType down,
      // so we can ignore that possibility.
      FormalParamTypes.emplace(SGF.getASTContext(), loweredParams, *origFnType,
                               llvm::ArrayRef(substFormalParams),
                               /*ignore final*/ false);
    }

    // Emit each of the function's explicit parameters in order.
    if (paramList) {
      for (auto *param : *paramList)
        emitParam(param);
    }

    // The self parameter follows the formal parameters.
    if (selfParam) {
      emitParam(selfParam);
    }

    if (FormalParamTypes) FormalParamTypes->finish();
    loweredParams.finish();

    return ArgNo;
  }

private:
  ManagedValue makeArgument(SILLocation loc, ParamDecl *pd) {
    LifetimeAnnotation lifetimeAnnotation = LifetimeAnnotation::None;
    bool isNoImplicitCopy = false;
    if (pd->isSelfParameter()) {
      if (auto *afd = dyn_cast<AbstractFunctionDecl>(pd->getDeclContext())) {
        lifetimeAnnotation = afd->getLifetimeAnnotation();
        isNoImplicitCopy = afd->isNoImplicitCopy();
      }
    } else {
      lifetimeAnnotation = pd->getLifetimeAnnotation();
      isNoImplicitCopy = pd->isNoImplicitCopy();
    }

    // Configure the lowered parameter generator for this formal parameter.
    loweredParams.configureParamData(pd, isNoImplicitCopy, lifetimeAnnotation);

    ManagedValue paramValue;
    EmitBBArguments argEmitter(loc, loweredParams);
    if (FormalParamTypes && FormalParamTypes->isOrigPackExpansion()) {
      paramValue = argEmitter.handlePackComponent(*FormalParamTypes);
    } else {
      auto substType = pd->getTypeInContext()->getCanonicalType();
      assert(!FormalParamTypes ||
             FormalParamTypes->getSubstParam().getParameterType() == substType);
      auto origType = (FormalParamTypes ? FormalParamTypes->getOrigType()
                                        : AbstractionPattern(substType));

      paramValue = argEmitter.handleParam(origType, substType, pd);
    }

    // Reset the parameter data on the lowered parameter generator.
    loweredParams.resetParamData();

    // Advance the formal parameter types generator.  This must happen
    // after resetting parameter data because it can claim lowered
    // parameters.
    if (FormalParamTypes) {
      FormalParamTypes->advance();
    }

    return paramValue;
  }

  void updateArgumentValueForBinding(ManagedValue argrv, SILLocation loc,
                                     ParamDecl *pd,
                                     const SILDebugVariable &varinfo) {
    bool calledCompletedUpdate = false;
    SWIFT_DEFER {
      assert(calledCompletedUpdate && "Forgot to call completed update along "
                                      "all paths or manually turn it off");
    };
    auto completeUpdate = [&](ManagedValue value) -> void {
      SGF.B.createDebugValue(loc, value.getValue(), varinfo);
      SGF.VarLocs[pd] = SILGenFunction::VarLoc::get(value.getValue());
      calledCompletedUpdate = true;
    };

    // If we do not need to support lexical lifetimes, just return value as the
    // updated value.
    if (!SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule()))
      return completeUpdate(argrv);

    // Look for the following annotations on the function argument:
    // - @noImplicitCopy
    // - @_eagerMove
    // - @_noEagerMove
    bool isNoImplicitCopy = pd->isNoImplicitCopy();
    if (!argrv.getType().isMoveOnly(/*orWrapped=*/false)) {
      isNoImplicitCopy |= pd->getSpecifier() == ParamSpecifier::Borrowing;
      isNoImplicitCopy |= pd->getSpecifier() == ParamSpecifier::Consuming;
      if (pd->isSelfParameter()) {
        auto *dc = pd->getDeclContext();
        if (auto *fn = dyn_cast<FuncDecl>(dc)) {
          auto accessKind = fn->getSelfAccessKind();
          isNoImplicitCopy |= accessKind == SelfAccessKind::Borrowing;
          isNoImplicitCopy |= accessKind == SelfAccessKind::Consuming;
        }
      }
    }

    // If we have a no implicit copy argument and the argument is trivial,
    // we need to use copyable to move only to convert it to its move only
    // form.
    if (!isNoImplicitCopy) {
      if (!argrv.getType().isMoveOnly()) {
        // Follow the normal path.  The value's lifetime will be enforced based
        // on its ownership.
        return completeUpdate(argrv);
      }

      // At this point, we have a noncopyable type. If it is owned, create an
      // alloc_box for it.
      if (argrv.getOwnershipKind() == OwnershipKind::Owned) {
        // TODO: Once owned values are mutable, this needs to become mutable.
        auto boxType = SGF.SGM.Types.getContextBoxTypeForCapture(
            pd,
            SGF.SGM.Types.getLoweredRValueType(TypeExpansionContext::minimal(),
                                               pd->getTypeInContext()),
            SGF.F.getGenericEnvironment(),
            /*mutable*/ false);

        auto *box = SGF.B.createAllocBox(loc, boxType, varinfo);
        SILValue destAddr = SGF.B.createProjectBox(loc, box, 0);
        SGF.B.emitStoreValueOperation(loc, argrv.forward(SGF), destAddr,
                                      StoreOwnershipQualifier::Init);
        SGF.emitManagedRValueWithCleanup(box);

        // We manually set calledCompletedUpdate to true since we want to use
        // the debug info from the box rather than insert a custom debug_value.
        calledCompletedUpdate = true;
        SGF.VarLocs[pd] = SILGenFunction::VarLoc::get(destAddr, box);
        return;
      }

      // If we have a guaranteed noncopyable argument, we do something a little
      // different. Specifically, we emit it as normal and do a non-consume or
      // assign. The reason why we do this is that a guaranteed argument cannot
      // be used in an escaping closure. So today, we leave it with the
      // misleading consuming message. We still are able to pass it to
      // non-escaping closures though since the onstack partial_apply does not
      // consume the value.
      assert(argrv.getOwnershipKind() == OwnershipKind::Guaranteed);
      argrv = argrv.copy(SGF, loc);
      argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
          loc, argrv,
          MarkUnresolvedNonCopyableValueInst::CheckKind::NoConsumeOrAssign);
      return completeUpdate(argrv);
    }

    if (argrv.getType().isTrivial(SGF.F)) {
      SILValue value = SGF.B.createOwnedCopyableToMoveOnlyWrapperValue(
          loc, argrv.getValue());
      argrv = SGF.emitManagedRValueWithCleanup(value);
      argrv = SGF.B.createMoveValue(loc, argrv, IsLexical);

      // If our argument was owned, we use no implicit copy. Otherwise, we
      // use no copy.
      MarkUnresolvedNonCopyableValueInst::CheckKind kind;
      switch (pd->getValueOwnership()) {
      case ValueOwnership::Default:
      case ValueOwnership::Shared:
      case ValueOwnership::InOut:
        kind = MarkUnresolvedNonCopyableValueInst::CheckKind::NoConsumeOrAssign;
        break;

      case ValueOwnership::Owned:
        kind = MarkUnresolvedNonCopyableValueInst::CheckKind::
            ConsumableAndAssignable;
        break;
      }

      argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(loc, argrv, kind);
      return completeUpdate(argrv);
    }

    if (argrv.getOwnershipKind() == OwnershipKind::Guaranteed) {
      argrv = SGF.B.createGuaranteedCopyableToMoveOnlyWrapperValue(loc, argrv);
      argrv = argrv.copy(SGF, loc);
      argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
          loc, argrv,
          MarkUnresolvedNonCopyableValueInst::CheckKind::NoConsumeOrAssign);
      return completeUpdate(argrv);
    }

    if (argrv.getOwnershipKind() == OwnershipKind::Owned) {
      // If we have an owned value, forward it into the
      // mark_unresolved_non_copyable_value to avoid an extra destroy_value.
      argrv = SGF.B.createOwnedCopyableToMoveOnlyWrapperValue(loc, argrv);
      argrv = SGF.B.createMoveValue(loc, argrv, IsLexical);
      argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
          loc, argrv,
          MarkUnresolvedNonCopyableValueInst::CheckKind::
              ConsumableAndAssignable);
      return completeUpdate(argrv);
    }

    return completeUpdate(argrv);
  }

  /// Create a SILArgument and store its value into the given Initialization,
  /// if not null.
  void makeArgumentIntoBinding(SILLocation loc, ParamDecl *pd) {
    ManagedValue argrv = makeArgument(loc, pd);
    if (pd->isInOut()) {
      assert(argrv.getType().isAddress() && "expected inout to be address");
    } else if (!pd->isImmutableInFunctionBody()) {
      // If it's a locally mutable parameter, then we need to move the argument
      // value into a local box to hold the mutated value.
      // We don't need to mark_uninitialized since we immediately initialize.
      auto mutableBox =
          SGF.emitLocalVariableWithCleanup(pd,
                                           /*uninitialized kind*/ std::nullopt);
      argrv.ensurePlusOne(SGF, loc).forwardInto(SGF, loc, mutableBox.get());
      return;
    }
    // If the variable is immutable, we can bind the value as is.
    // Leave the cleanup on the argument, if any, in place to consume the
    // argument if we're responsible for it.
    SILDebugVariable varinfo(pd->isImmutableInFunctionBody(), ArgNo);
    if (!argrv.getType().isAddress()) {
      // NOTE: We setup SGF.VarLocs[pd] in updateArgumentValueForBinding.
      updateArgumentValueForBinding(argrv, loc, pd, varinfo);
      return;
    }

    if (auto *allocStack = dyn_cast<AllocStackInst>(argrv.getValue())) {
      allocStack->setArgNo(ArgNo);
      allocStack->setIsFromVarDecl();
      if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(
              SGF.getModule()) &&
          SGF.F.getLifetime(pd, allocStack->getType()).isLexical())
        allocStack->setIsLexical();
      SGF.VarLocs[pd] = SILGenFunction::VarLoc::get(allocStack);
      return;
    }

    if (auto *arg = dyn_cast<SILFunctionArgument>(argrv.getValue())) {
      if (arg->isNoImplicitCopy()) {
        switch (pd->getSpecifier()) {
        case swift::ParamSpecifier::Borrowing:
          // Shouldn't have any cleanups on this.
          assert(!argrv.hasCleanup());
          argrv = ManagedValue::forBorrowedAddressRValue(
              SGF.B.createCopyableToMoveOnlyWrapperAddr(pd, argrv.getValue()));
          break;
        case swift::ParamSpecifier::ImplicitlyCopyableConsuming:
        case swift::ParamSpecifier::Consuming:
        case swift::ParamSpecifier::Default:
        case swift::ParamSpecifier::InOut:
        case swift::ParamSpecifier::LegacyOwned:
        case swift::ParamSpecifier::LegacyShared:
          break;
        }
      }
    }

    SILValue debugOperand = argrv.getValue();

    if (argrv.getType().isMoveOnly()) {
      switch (pd->getValueOwnership()) {
      case ValueOwnership::Default:
        if (pd->isSelfParameter()) {
          assert(!isa<MarkUnresolvedNonCopyableValueInst>(argrv.getValue()) &&
                 "Should not have inserted mark must check inst in EmitBBArgs");
          if (!pd->isInOut()) {
            argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
                loc, argrv,
                MarkUnresolvedNonCopyableValueInst::CheckKind::
                    NoConsumeOrAssign);
          }
        } else {
          if (auto *fArg = dyn_cast<SILFunctionArgument>(argrv.getValue())) {
            switch (fArg->getArgumentConvention()) {
            case SILArgumentConvention::Direct_Guaranteed:
            case SILArgumentConvention::Direct_Owned:
            case SILArgumentConvention::Direct_Unowned:
            case SILArgumentConvention::Indirect_Inout:
            case SILArgumentConvention::Indirect_Out:
            case SILArgumentConvention::Indirect_InoutAliasable:
            case SILArgumentConvention::Pack_Inout:
            case SILArgumentConvention::Pack_Guaranteed:
            case SILArgumentConvention::Pack_Owned:
            case SILArgumentConvention::Pack_Out:
              llvm_unreachable("Should have been handled elsewhere");
            case SILArgumentConvention::Indirect_In:
              argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
                  loc, argrv,
                  MarkUnresolvedNonCopyableValueInst::CheckKind::
                      ConsumableAndAssignable);
              break;
            case SILArgumentConvention::Indirect_In_Guaranteed:
              argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
                  loc, argrv,
                  MarkUnresolvedNonCopyableValueInst::CheckKind::
                      NoConsumeOrAssign);
            }
          } else {
            assert(isa<MarkUnresolvedNonCopyableValueInst>(argrv.getValue()) &&
                   "Should have inserted mark must check inst in EmitBBArgs");
          }
        }
        break;
      case ValueOwnership::InOut: {
        assert(isa<MarkUnresolvedNonCopyableValueInst>(argrv.getValue()) &&
               "Expected mark must check inst with inout to be handled in "
               "emitBBArgs earlier");
        auto mark = cast<MarkUnresolvedNonCopyableValueInst>(argrv.getValue());
        debugOperand = mark->getOperand();
        break;
      }
      case ValueOwnership::Owned:
        argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
            loc, argrv,
            MarkUnresolvedNonCopyableValueInst::CheckKind::
                ConsumableAndAssignable);
        break;
      case ValueOwnership::Shared:
        argrv = SGF.B.createMarkUnresolvedNonCopyableValueInst(
            loc, argrv,
            MarkUnresolvedNonCopyableValueInst::CheckKind::NoConsumeOrAssign);
        break;
      }
    }

    DebugValueInst *debugInst
      = SGF.B.createDebugValueAddr(loc, debugOperand, varinfo);

    if (argrv.getValue() != debugOperand) {
      if (auto valueInst =
              dyn_cast<MarkUnresolvedNonCopyableValueInst>(argrv.getValue())) {
        // Move the debug instruction outside of any marker instruction that might
        // have been applied to the value, so that analysis doesn't move the
        // debug_value anywhere it shouldn't be.
        debugInst->moveBefore(valueInst);
      }
    }
    SGF.VarLocs[pd] = SILGenFunction::VarLoc::get(argrv.getValue());
  }

  void emitParam(ParamDecl *PD) {
    // Register any auxiliary declarations for the parameter to be
    // visited later.
    PD->visitAuxiliaryDecls([&](VarDecl *localVar) {
      SGF.LocalAuxiliaryDecls.push_back(localVar);
    });

    // If the parameter has an external property wrapper, then the
    // wrapper is the actual parameter.  Use that for everything
    // except the auxiliary decls collection above.
    if (PD->hasExternalPropertyWrapper()) {
      PD = cast<ParamDecl>(PD->getPropertyWrapperBackingProperty());
    }

    SILLocation loc(PD);
    loc.markAsPrologue();

    assert(PD->getTypeInContext()->isMaterializable());

    ++ArgNo;
    if (PD->hasName() || PD->isIsolated()) {
      makeArgumentIntoBinding(loc, PD);
    } else {
      emitAnonymousParam(loc, PD);
    }
  }

  void emitAnonymousParam(SILLocation loc, ParamDecl *PD) {
    // A value bound to _ is unused and can be immediately released.
    Scope discardScope(SGF.Cleanups, CleanupLocation(PD));

    // Manage the parameter.
    auto argrv = makeArgument(loc, PD);

    // Emit debug information for the argument.
    SILDebugVariable DebugVar(PD->isLet(), ArgNo);
    if (argrv.getType().isAddress())
      SGF.B.createDebugValueAddr(loc, argrv.getValue(), DebugVar);
    else
      SGF.B.createDebugValue(loc, argrv.getValue(), DebugVar);
  }
};
} // end anonymous namespace

  
static void makeArgument(Type ty, ParamDecl *decl,
                         SmallVectorImpl<SILValue> &args, SILGenFunction &SGF) {
  assert(ty && "no type?!");
  
  if (ty->is<PackExpansionType>()) {
    ty = PackType::get(SGF.getASTContext(), {ty});
  }

  // Destructure tuple value arguments.
  if (!decl->isInOut()) {
    if (TupleType *tupleTy = ty->getAs<TupleType>()) {
      for (auto fieldType : tupleTy->getElementTypes())
        makeArgument(fieldType, decl, args, SGF);
      return;
    }
  }

  auto loweredTy = SGF.getLoweredTypeForFunctionArgument(ty);
  if (decl->isInOut())
    loweredTy = SILType::getPrimitiveAddressType(loweredTy.getASTType());
  auto arg = SGF.F.begin()->createFunctionArgument(loweredTy, decl);
  args.push_back(arg);
}

void SILGenFunction::bindParameterForForwarding(ParamDecl *param,
                                     SmallVectorImpl<SILValue> &parameters) {
  if (param->hasExternalPropertyWrapper()) {
    param = cast<ParamDecl>(param->getPropertyWrapperBackingProperty());
  }

  makeArgument(param->getTypeInContext(), param, parameters, *this);
}

void SILGenFunction::bindParametersForForwarding(const ParameterList *params,
                                     SmallVectorImpl<SILValue> &parameters) {
  for (auto param : *params)
    bindParameterForForwarding(param, parameters);
}

static void emitCaptureArguments(SILGenFunction &SGF,
                                 GenericSignature origGenericSig,
                                 CapturedValue capture,
                                 uint16_t ArgNo) {
  if (auto *expr = capture.getPackElement()) {
    SILLocation Loc(expr);
    Loc.markAsPrologue();

    auto interfaceType = expr->getType()->mapTypeOutOfContext();

    auto type = SGF.F.mapTypeIntoContext(interfaceType);
    auto &lowering = SGF.getTypeLowering(type);
    SILType ty = lowering.getLoweredType();

    SILValue arg;

    auto expansion = SGF.getTypeExpansionContext();
    auto captureKind = SGF.SGM.Types.getDeclCaptureKind(capture, expansion);
    switch (captureKind) {
    case CaptureKind::Constant:
    case CaptureKind::StorageAddress:
    case CaptureKind::Immutable: {
      auto argIndex = SGF.F.begin()->getNumArguments();
      // Non-escaping stored decls are captured as the address of the value.
      auto param = SGF.F.getConventions().getParamInfoForSILArg(argIndex);
      if (SGF.F.getConventions().isSILIndirect(param))
        ty = ty.getAddressType();

      auto *fArg = SGF.F.begin()->createFunctionArgument(ty, nullptr);
      fArg->setClosureCapture(true);

      arg = fArg;
      break;
    }

    case CaptureKind::ImmutableBox:
    case CaptureKind::Box:
      llvm_unreachable("should be impossible");
    }

    ManagedValue mv = ManagedValue::forBorrowedRValue(arg);
    auto inserted = SGF.OpaqueValues.insert(std::make_pair(expr, mv));
    assert(inserted.second);
    (void) inserted;

    return;
  }

  auto *VD = cast<VarDecl>(capture.getDecl());
  
  SILLocation Loc(VD);
  Loc.markAsPrologue();

  auto interfaceType = VD->getInterfaceType()->getReducedType(
      origGenericSig);

  // If we're capturing a parameter pack, wrap it in a tuple.
  bool isPack = false;
  if (isa<PackExpansionType>(interfaceType)) {
    assert(!VD->supportsMutation() &&
           "Cannot capture a pack as an lvalue");

    SmallVector<TupleTypeElt, 1> elts;
    elts.push_back(interfaceType);
    interfaceType = CanTupleType(TupleType::get(elts, SGF.getASTContext()));
    isPack = true;
  }

  // Local function to get the captured variable type within the capturing
  // context.
  auto getVarTypeInCaptureContext = [&]() -> Type {
    return SGF.F.mapTypeIntoContext(interfaceType);
  };

  auto type = getVarTypeInCaptureContext();
  auto &lowering = SGF.getTypeLowering(getVarTypeInCaptureContext());
  SILType ty = lowering.getLoweredType();

  bool isNoImplicitCopy;
  
  if (ty.isTrivial(SGF.F) || ty.isMoveOnly()) {
    isNoImplicitCopy = false;
  } else if (VD->isNoImplicitCopy()) {
    isNoImplicitCopy = true;
  } else if (auto pd = dyn_cast<ParamDecl>(VD)) {
    switch (pd->getSpecifier()) {
    case ParamSpecifier::Borrowing:
    case ParamSpecifier::Consuming:
      isNoImplicitCopy = true;
      break;
    case ParamSpecifier::ImplicitlyCopyableConsuming:
    case ParamSpecifier::Default:
    case ParamSpecifier::InOut:
    case ParamSpecifier::LegacyOwned:
    case ParamSpecifier::LegacyShared:
      isNoImplicitCopy = false;
      break;
    }
  } else {
    isNoImplicitCopy = false;
  }
    
  SILValue arg;
  SILFunctionArgument *box = nullptr;

  auto expansion = SGF.getTypeExpansionContext();
  auto captureKind = SGF.SGM.Types.getDeclCaptureKind(capture, expansion);
  switch (captureKind) {
  case CaptureKind::Constant: {
    assert(!isPack);

    // Constant decls are captured by value.
    auto *fArg = SGF.F.begin()->createFunctionArgument(ty, VD);
    fArg->setClosureCapture(true);

    ManagedValue val = ManagedValue::forBorrowedRValue(fArg);

    // If the original variable was settable, then Sema will have treated the
    // VarDecl as an lvalue, even in the closure's use.  As such, we need to
    // allow formation of the address for this captured value.  Create a
    // temporary within the closure to provide this address.
    if (VD->isSettable(VD->getDeclContext())) {
      auto addr = SGF.emitTemporary(VD, lowering);
      // We have created a copy that needs to be destroyed.
      val = SGF.B.emitCopyValueOperation(Loc, val);
      // We use the SILValue version of this because the SILGenBuilder version
      // will create a cloned cleanup, which we do not want since our temporary
      // already has a cleanup.
      //
      // MG: Is this the right semantics for createStore? Seems like that
      // should be potentially a different API.
      SGF.B.emitStoreValueOperation(VD, val.forward(SGF), addr->getAddress(),
                                    StoreOwnershipQualifier::Init);
      addr->finishInitialization(SGF);
      val = addr->getManagedAddress();
    }
    
    if (isNoImplicitCopy && !val.getType().isMoveOnly()) {
      val = SGF.B.createGuaranteedCopyableToMoveOnlyWrapperValue(VD, val);
    }

    // If this constant is a move only type, we need to add no_consume_or_assign checking to
    // ensure that we do not consume this captured value in the function. This
    // is because closures can be invoked multiple times which is inconsistent
    // with consuming the move only type.
    if (val.getType().isMoveOnly()) {
      val = val.ensurePlusOne(SGF, Loc);
      val = SGF.B.createMarkUnresolvedNonCopyableValueInst(
          Loc, val,
          MarkUnresolvedNonCopyableValueInst::CheckKind::NoConsumeOrAssign);
    }

    arg = val.getValue();
    break;
  }

  case CaptureKind::ImmutableBox:
  case CaptureKind::Box: {
    assert(!isPack);

    // LValues are captured as a retained @box that owns
    // the captured value.
    bool isMutable = captureKind == CaptureKind::Box;
    // Get the content for the box in the minimal  resilience domain because we
    // are declaring a type.
    ty = SGF.SGM.Types.getLoweredType(type, TypeExpansionContext::minimal());
    auto boxTy = SGF.SGM.Types.getContextBoxTypeForCapture(
        VD, ty.getASTType(), SGF.F.getGenericEnvironment(),
        /*mutable*/ isMutable);
    box = SGF.F.begin()->createFunctionArgument(
        SILType::getPrimitiveObjectType(boxTy), VD);
    box->setClosureCapture(true);
    arg = SGF.B.createProjectBox(VD, box, 0);
    if (isNoImplicitCopy && !arg->getType().isMoveOnly()) {
      arg = SGF.B.createCopyableToMoveOnlyWrapperAddr(VD, arg);
    }
    break;
  }
  case CaptureKind::StorageAddress:
    assert(!isPack);

    LLVM_FALLTHROUGH;

  case CaptureKind::Immutable: {
    auto argIndex = SGF.F.begin()->getNumArguments();
    // Non-escaping stored decls are captured as the address of the value.
    auto argConv = SGF.F.getConventions().getSILArgumentConvention(argIndex);
    bool isInOut = (argConv == SILArgumentConvention::Indirect_Inout ||
                    argConv == SILArgumentConvention::Indirect_InoutAliasable);
    auto param = SGF.F.getConventions().getParamInfoForSILArg(argIndex);
    if (SGF.F.getConventions().isSILIndirect(param)) {
      ty = ty.getAddressType();
    }
    auto *fArg = SGF.F.begin()->createFunctionArgument(ty, VD);
    fArg->setClosureCapture(true);
    arg = SILValue(fArg);
    
    if (isNoImplicitCopy && !arg->getType().isMoveOnly()) {
      switch (argConv) {
      case SILArgumentConvention::Indirect_Inout:
      case SILArgumentConvention::Indirect_InoutAliasable:
      case SILArgumentConvention::Indirect_In:
      case SILArgumentConvention::Indirect_In_Guaranteed:
      case SILArgumentConvention::Pack_Inout:
      case SILArgumentConvention::Pack_Owned:
      case SILArgumentConvention::Pack_Guaranteed:
        arg = SGF.B.createCopyableToMoveOnlyWrapperAddr(VD, arg);
        break;
        
      case SILArgumentConvention::Direct_Owned:
        arg = SGF.B.createOwnedCopyableToMoveOnlyWrapperValue(VD, arg);
        break;
      
      case SILArgumentConvention::Direct_Guaranteed:
        arg = SGF.B.createGuaranteedCopyableToMoveOnlyWrapperValue(VD, arg);
        break;
      
      case SILArgumentConvention::Direct_Unowned:
      case SILArgumentConvention::Indirect_Out:
      case SILArgumentConvention::Pack_Out:
        llvm_unreachable("should be impossible");
      }
    }

    // If we have an inout noncopyable parameter, insert a consumable and
    // assignable.
    //
    // NOTE: If we have an escaping closure, we are going to emit an error later
    // in SIL since it is illegal to capture an inout value in an escaping
    // closure. The later code knows how to handle that we have the
    // mark_unresolved_non_copyable_value here.
    if (isInOut && arg->getType().isMoveOnly()) {
      arg = SGF.B.createMarkUnresolvedNonCopyableValueInst(
          Loc, arg,
          MarkUnresolvedNonCopyableValueInst::CheckKind::
              ConsumableAndAssignable);
    }
    break;
  }
  }

  // If we captured a pack as a tuple, create a pack from the elements
  // of the tuple.
  if (isPack) {
    auto tupleType = ty.castTo<TupleType>();
    assert(tupleType->getNumElements() == 1);

    auto packType =
        SILPackType::get(SGF.getASTContext(),
                         SILPackType::ExtInfo(/*indirect=*/true),
                         {tupleType.getElementType(0)});
    auto packValue = SGF.emitTemporaryPackAllocation(
        Loc, SILType::getPrimitiveObjectType(packType));

    auto formalPackType = cast<TupleType>(type->getCanonicalType())
        .getInducedPackType();
    SGF.projectTupleElementsToPack(Loc, arg, packValue, formalPackType);

    arg = packValue;
  }

  SGF.VarLocs[VD] = SILGenFunction::VarLoc::get(arg, box);
  SILDebugVariable DbgVar(VD->isLet(), ArgNo);
  if (auto *AllocStack = dyn_cast<AllocStackInst>(arg)) {
    AllocStack->setArgNo(ArgNo);
  } else if (box || ty.isAddress()) {
    SGF.B.createDebugValueAddr(Loc, arg, DbgVar);
  } else {
    SGF.B.createDebugValue(Loc, arg, DbgVar);
  }
}

void SILGenFunction::emitProlog(
    DeclContext *DC, CaptureInfo captureInfo, ParameterList *paramList,
    ParamDecl *selfParam, Type resultType, std::optional<Type> errorType,
    SourceLoc throwsLoc) {
  // Emit the capture argument variables. These are placed last because they
  // become the first curry level of the SIL function.
  assert(captureInfo.hasBeenComputed() &&
         "can't emit prolog of function with uncomputed captures");

  bool hasErasedIsolation =
    (TypeContext && TypeContext->ExpectedLoweredType->hasErasedIsolation());

  uint16_t ArgNo = emitBasicProlog(DC, paramList, selfParam, resultType,
                                   errorType, throwsLoc,
                                   /*ignored parameters*/
                                     (hasErasedIsolation ? 1 : 0) +
                                     captureInfo.getCaptures().size());

  // If we're emitting into a type context that expects erased isolation,
  // add (and ignore) the isolation parameter.
  if (hasErasedIsolation) {
    SILType ty = SILType::getOpaqueIsolationType(getASTContext());
    SILValue val = F.begin()->createFunctionArgument(ty);
    (void) val;
  }

  for (auto capture : captureInfo.getCaptures()) {
    if (capture.isDynamicSelfMetadata()) {
      auto selfMetatype = MetatypeType::get(
        captureInfo.getDynamicSelfType());
      SILType ty = getLoweredType(selfMetatype);
      SILValue val = F.begin()->createFunctionArgument(ty);
      (void) val;

      continue;
    }

    if (capture.isOpaqueValue()) {
      OpaqueValueExpr *opaqueValue = capture.getOpaqueValue();
      Type type = opaqueValue->getType()->mapTypeOutOfContext();
      type = F.mapTypeIntoContext(type);
      auto &lowering = getTypeLowering(type);
      SILType ty = lowering.getLoweredType();
      SILValue val = F.begin()->createFunctionArgument(ty);

      // Opaque values are always passed 'owned', so add a clean up if needed.
      //
      // TODO: Should this be tied to the mv?
      if (!lowering.isTrivial())
        enterDestroyCleanup(val);

      ManagedValue mv;
      if (lowering.isTrivial())
        mv = ManagedValue::forObjectRValueWithoutOwnership(val);
      else
        mv = ManagedValue::forUnmanagedOwnedValue(val);

      OpaqueValues[opaqueValue] = mv;

      continue;
    }

    emitCaptureArguments(*this, DC->getGenericSignatureOfContext(),
                         capture, ++ArgNo);
  }

  emitExpectedExecutor();

  // IMPORTANT: This block should be the last one in `emitProlog`,
  // since it terminates BB and no instructions should be insterted after it.
  // Emit an unreachable instruction if a parameter type is
  // uninhabited
  if (paramList) {
    for (auto *param : *paramList) {
      if (param->getTypeInContext()->isStructurallyUninhabited()) {
        SILLocation unreachableLoc(param);
        unreachableLoc.markAsPrologue();
        B.createUnreachable(unreachableLoc);
        break;
      }
    }
  }
}

static void emitIndirectPackParameter(SILGenFunction &SGF,
                                      PackType *resultType,
                                      CanTupleEltTypeArrayRef
                                        resultTypesInContext,
                                      AbstractionPattern origExpansionType,
                                      DeclContext *DC) {
  auto &ctx = SGF.getASTContext();

  bool indirect =
    origExpansionType.arePackElementsPassedIndirectly(SGF.SGM.Types);
  SmallVector<CanType, 4> packElts;
  for (auto substEltType : resultTypesInContext) {
    auto origComponentType
      = origExpansionType.getPackExpansionComponentType(substEltType);
    CanType loweredEltTy =
      SGF.getLoweredRValueType(origComponentType, substEltType);
    packElts.push_back(loweredEltTy);
  }

  SILPackType::ExtInfo extInfo(indirect);
  auto packType = SILPackType::get(ctx, extInfo, packElts);
  auto resultSILType = SILType::getPrimitiveAddressType(packType);

  auto var = new (ctx) ParamDecl(SourceLoc(), SourceLoc(),
                                 ctx.getIdentifier("$return_value"), SourceLoc(),
                                 ctx.getIdentifier("$return_value"),
                                 DC);
  var->setSpecifier(ParamSpecifier::InOut);
  var->setInterfaceType(resultType);
  auto *arg = SGF.F.begin()->createFunctionArgument(resultSILType, var);
  (void)arg;
}

static void emitIndirectResultParameters(SILGenFunction &SGF,
                                         Type resultType,
                                         AbstractionPattern origResultType,
                                         DeclContext *DC) {
  CanType resultTypeInContext =
    DC->mapTypeIntoContext(resultType)->getCanonicalType();

  // Tuples in the original result type are expanded.
  if (origResultType.isTuple()) {
    origResultType.forEachTupleElement(resultTypeInContext,
                                       [&](TupleElementGenerator &elt) {
      auto origEltType = elt.getOrigType();
      auto substEltTypes = elt.getSubstTypes(resultType);

      // If the original element isn't a pack expansion, pull out the
      // corresponding substituted tuple element and recurse.
      if (!elt.isOrigPackExpansion()) {
        emitIndirectResultParameters(SGF, substEltTypes[0], origEltType, DC);
        return;
      }

      // Otherwise, bind a pack parameter.
      PackType *resultPackType = [&] {
        SmallVector<Type, 4> packElts(substEltTypes.begin(),
                                      substEltTypes.end());
        return PackType::get(SGF.getASTContext(), packElts);
      }();
      emitIndirectPackParameter(SGF, resultPackType, elt.getSubstTypes(),
                                origEltType, DC);
    });
    return;
  }

  assert(!resultType->is<PackExpansionType>());

  // If the return type is address-only, emit the indirect return argument.

  // The calling convention always uses minimal resilience expansion.
  auto resultConvType = SGF.SGM.Types.getLoweredType(
      resultTypeInContext, TypeExpansionContext::minimal());

  // And the abstraction pattern may force an indirect return even if the
  // concrete type wouldn't normally be returned indirectly.
  if (!SILModuleConventions::isReturnedIndirectlyInSIL(resultConvType,
                                                       SGF.SGM.M)) {
    if (!SILModuleConventions(SGF.SGM.M).useLoweredAddresses()
        || origResultType.getResultConvention(SGF.SGM.Types) != AbstractionPattern::Indirect)
      return;
  }

  auto &ctx = SGF.getASTContext();
  auto var = new (ctx) ParamDecl(SourceLoc(), SourceLoc(),
                                 ctx.getIdentifier("$return_value"), SourceLoc(),
                                 ctx.getIdentifier("$return_value"),
                                 DC);
  var->setSpecifier(ParamSpecifier::InOut);
  var->setInterfaceType(resultType);
  auto &resultTI =
    SGF.SGM.Types.getTypeLowering(origResultType, resultTypeInContext,
                                  SGF.getTypeExpansionContext());
  SILType resultSILType = resultTI.getLoweredType().getAddressType();
  auto *arg = SGF.F.begin()->createFunctionArgument(resultSILType, var);
  (void)arg;
}

static void emitIndirectErrorParameter(SILGenFunction &SGF,
                                       Type errorType,
                                       AbstractionPattern origErrorType,
                                       DeclContext *DC) {
  CanType errorTypeInContext =
    DC->mapTypeIntoContext(errorType)->getCanonicalType();

  // If the error type is address-only, emit the indirect error argument.

  // The calling convention always uses minimal resilience expansion.
  auto errorConvType = SGF.SGM.Types.getLoweredType(
      origErrorType, errorTypeInContext, TypeExpansionContext::minimal());

  // And the abstraction pattern may force an indirect return even if the
  // concrete type wouldn't normally be returned indirectly.
  if (!SILModuleConventions::isThrownIndirectlyInSIL(errorConvType,
                                                     SGF.SGM.M)) {
    if (!SILModuleConventions(SGF.SGM.M).useLoweredAddresses()
        || origErrorType.getErrorConvention(SGF.SGM.Types)
            != AbstractionPattern::Indirect)
      return;
  }

  auto &ctx = SGF.getASTContext();
  auto var = new (ctx) ParamDecl(SourceLoc(), SourceLoc(),
                                 ctx.getIdentifier("$error"), SourceLoc(),
                                 ctx.getIdentifier("$error"),
                                 DC);
  var->setSpecifier(ParamSpecifier::InOut);
  var->setInterfaceType(errorType);

  auto &errorTI =
    SGF.SGM.Types.getTypeLowering(origErrorType, errorTypeInContext,
                                  SGF.getTypeExpansionContext());
  SILType errorSILType = errorTI.getLoweredType().getAddressType();
  assert(SGF.IndirectErrorResult == nullptr);
  SGF.IndirectErrorResult = SGF.F.begin()->createFunctionArgument(errorSILType, var);
}

uint16_t SILGenFunction::emitBasicProlog(
    DeclContext *DC, ParameterList *paramList, ParamDecl *selfParam,
    Type resultType, std::optional<Type> errorType, SourceLoc throwsLoc,
    unsigned numIgnoredTrailingParameters) {
  // Create the indirect result parameters.
  auto genericSig = DC->getGenericSignatureOfContext();
  resultType = resultType->getReducedType(genericSig);
  if (errorType)
    errorType = (*errorType)->getReducedType(genericSig);

  std::optional<AbstractionPattern> origClosureType;
  if (TypeContext) origClosureType = TypeContext->OrigType;

  AbstractionPattern origResultType = origClosureType
    ? origClosureType->getFunctionResultType()
    : AbstractionPattern(genericSig.getCanonicalSignature(),
                         resultType->getCanonicalType());
  
  emitIndirectResultParameters(*this, resultType, origResultType, DC);

  std::optional<AbstractionPattern> origErrorType;
  if (origClosureType && !origClosureType->isTypeParameterOrOpaqueArchetype()) {
    origErrorType = origClosureType->getFunctionThrownErrorType();
    if (origErrorType && !errorType)
      errorType = origErrorType->getEffectiveThrownErrorType();
  } else if (errorType) {
    origErrorType = AbstractionPattern(genericSig.getCanonicalSignature(),
                                       (*errorType)->getCanonicalType());
  }

  if (origErrorType && errorType &&
      F.getConventions().hasIndirectSILErrorResults()) {
    emitIndirectErrorParameter(*this, *errorType, *origErrorType, DC);
  }

  // Emit the argument variables in calling convention order.
  unsigned ArgNo =
    ArgumentInitHelper(*this, numIgnoredTrailingParameters)
      .emitParams(origClosureType, paramList, selfParam);

  // Record the ArgNo of the artificial $error inout argument. 
  if (errorType && IndirectErrorResult == nullptr) {
    CanType errorTypeInContext =
      DC->mapTypeIntoContext(*errorType)->getCanonicalType();
    auto loweredErrorTy = getLoweredType(*origErrorType, errorTypeInContext);
    ManagedValue undef = emitUndef(loweredErrorTy);
    SILDebugVariable dbgVar("$error", /*Constant*/ false, ++ArgNo);
    RegularLocation loc = RegularLocation::getAutoGeneratedLocation();
    if (throwsLoc.isValid())
      loc = throwsLoc;
    B.createDebugValue(loc, undef.getValue(), dbgVar);
  }

  for (auto &bb : B.getFunction())
    for (auto &i : bb) {
      auto *alloc = dyn_cast<AllocStackInst>(&i);
      if (!alloc)
        continue;
      auto varInfo = alloc->getVarInfo();
      if (!varInfo || varInfo->ArgNo)
        continue;
      // The allocation has a varinfo but no argument number, which should not
      // happen in the prolog. Unfortunately, some copies can generate wrong
      // debug info, so we have to fix it here, by invalidating it.
      alloc->invalidateVarInfo();
    }

  return ArgNo;
}