-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathClosureLifetimeFixup.cpp
1539 lines (1368 loc) · 58.4 KB
/
ClosureLifetimeFixup.cpp
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
//===--- ClosureLifetimeFixup.cpp - Fixup the lifetime of closures --------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "closure-lifetime-fixup"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/SIL/BasicBlockDatastructures.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
#include "swift/SILOptimizer/Utils/StackNesting.h"
#include "llvm/Support/CommandLine.h"
llvm::cl::opt<bool> DisableConvertEscapeToNoEscapeSwitchEnumPeephole(
"sil-disable-convert-escape-to-noescape-switch-peephole",
llvm::cl::init(false),
llvm::cl::desc(
"Disable the convert_escape_to_noescape switch enum peephole. "),
llvm::cl::Hidden);
llvm::cl::opt<bool> DisableCopyEliminationOfCopyableCapture(
"sil-disable-copy-elimination-of-copyable-closure-capture",
llvm::cl::init(false),
llvm::cl::desc("Don't eliminate copy_addr of Copyable closure captures "
"inserted by SILGen"));
using namespace swift;
/// Given an optional diamond, return the bottom of the diamond.
///
/// That is given that sei is in bb0,
///
/// /---> bb1 ---\
/// / \
/// bb0 ---> bb3
/// \ /
/// \---> bb2 ---/
///
/// this routine will return bb3.
static SILBasicBlock *getOptionalDiamondSuccessor(SwitchEnumInst *sei) {
auto numSuccs = sei->getNumSuccessors();
if (numSuccs != 2)
return nullptr;
auto *succSome = sei->getCase(0).second;
auto *succNone = sei->getCase(1).second;
if (succSome->args_size() != 1)
std::swap(succSome, succNone);
if (succSome->args_size() != 1 || succNone->args_size() != 0)
return nullptr;
auto *succ = succSome->getSingleSuccessorBlock();
if (!succ)
return nullptr;
if (succNone == succ)
return succ;
succNone = succNone->getSingleSuccessorBlock();
if (succNone == succ)
return succ;
if (succNone == nullptr)
return nullptr;
succNone = succNone->getSingleSuccessorBlock();
if (succNone == succ)
return succ;
return nullptr;
}
/// Find a safe insertion point for closure destruction. We might create a
/// closure that captures self in deinit of self. In this situation it is not
/// safe to destroy the closure after we called super deinit. We have to place
/// the closure destruction before that call.
///
/// %deinit = objc_super_method %0 : $C, #A.deinit!deallocator.foreign
/// %super = upcast %0 : $C to $A
/// apply %deinit(%super) : $@convention(objc_method) (A) -> ()
/// end_lifetime %super : $A
static SILInstruction *getDeinitSafeClosureDestructionPoint(SILBasicBlock *bb) {
for (auto &i : llvm::reverse(*bb)) {
if (auto *endLifetime = dyn_cast<EndLifetimeInst>(&i)) {
auto *superInstance = endLifetime->getOperand()->getDefiningInstruction();
assert(superInstance && "Expected an instruction");
return superInstance;
}
}
return bb->getTerminator();
}
static void findReachableExitBlocks(SILInstruction *i,
SmallVectorImpl<SILBasicBlock *> &result) {
BasicBlockWorklist worklist(i->getParent());
while (SILBasicBlock *bb = worklist.pop()) {
if (bb->getTerminator()->isFunctionExiting()) {
result.push_back(bb);
continue;
}
for (SILBasicBlock *succ : bb->getSuccessors()) {
worklist.pushIfNotVisited(succ);
}
}
}
/// We use this to ensure that we properly handle recursive cases by revisiting
/// phi nodes that we are tracking. This just makes it easier to reproduce in a
/// test case.
static llvm::cl::opt<bool> ReverseInitialWorklist(
"sil-closure-lifetime-fixup-reverse-phi-order", llvm::cl::init(false),
llvm::cl::desc(
"Reverse the order in which we visit phis for testing purposes"),
llvm::cl::Hidden);
// Finally, we need to prune phis inserted by the SSA updater that
// only take the .none from the entry block. This means that they are
// not actually reachable from the .some() so we know that we do not
// need to lifetime extend there at all. As an additional benefit, we
// eliminate the need to balance these arguments to satisfy the
// ownership verifier. This occurs since arguments are a place in SIL
// where the trivialness of an enums case is erased.
static void
cleanupDeadTrivialPhiArgs(SILValue initialValue,
SmallVectorImpl<SILPhiArgument *> &insertedPhis) {
// Just for testing purposes.
if (ReverseInitialWorklist) {
std::reverse(insertedPhis.begin(), insertedPhis.end());
}
SmallVector<SILArgument *, 8> worklist(insertedPhis.begin(),
insertedPhis.end());
sortUnique(insertedPhis);
SmallVector<SILValue, 8> incomingValues;
while (!worklist.empty()) {
// Clear the incoming values array after each iteration.
SWIFT_DEFER { incomingValues.clear(); };
auto *phi = worklist.pop_back_val();
{
auto it = lower_bound(insertedPhis, phi);
if (it == insertedPhis.end() || *it != phi)
continue;
}
// TODO: When we split true phi arguments from transformational terminators,
// this will always succeed and the assert can go away.
bool foundPhiValues = phi->getIncomingPhiValues(incomingValues);
(void)foundPhiValues;
assert(foundPhiValues && "Should always have 'true' phi arguments since "
"these were inserted by the SSA updater.");
if (llvm::any_of(incomingValues,
[&](SILValue v) { return v != initialValue; }))
continue;
// Remove it from our insertedPhis list to prevent us from re-visiting this.
{
auto it = lower_bound(insertedPhis, phi);
assert((it != insertedPhis.end() && *it == phi) &&
"Should have found the phi");
insertedPhis.erase(it);
}
// See if any of our users are branch or cond_br. If so, we may have
// exposed additional unneeded phis. Add it back to the worklist in such a
// case.
for (auto *op : phi->getUses()) {
auto *user = op->getUser();
if (!isa<BranchInst>(user) && !isa<CondBranchInst>(user))
continue;
auto *termInst = cast<TermInst>(user);
for (auto succBlockArgList : termInst->getSuccessorBlockArgumentLists()) {
llvm::copy_if(succBlockArgList, std::back_inserter(worklist),
[&](SILArgument *succArg) -> bool {
auto it = lower_bound(insertedPhis, succArg);
return it != insertedPhis.end() && *it == succArg;
});
}
}
// Then RAUW the phi with the entryBlockOptionalNone and erase the
// argument.
phi->replaceAllUsesWith(initialValue);
erasePhiArgument(phi->getParent(), phi->getIndex(),
/*cleanupDeadPhiOp*/ false);
}
}
/// Extend the lifetime of the convert_escape_to_noescape's operand to the end
/// of the function.
/// Create a copy of the escaping closure operand and end its lifetime at
/// function exits. Since, the cvt may not be dominating function exits, we
/// need to create an optional and use the SSAUpdater to extend the lifetime. In
/// order to prevent the optional being optimized away, create a borrow scope
/// and insert a mark_dependence of the non escaping closure on the borrow.
/// NOTE: Since we are lifetime extending a copy that we have introduced, we do
/// not need to consider destroy_value emitted by SILGen unlike
/// copy_block_without_escaping which consumes its sentinel parameter. Unlike
/// that case where we have to consider that destroy_value, we have a simpler
/// time here.
static void extendLifetimeToEndOfFunction(SILFunction &fn,
ConvertEscapeToNoEscapeInst *cvt,
SILSSAUpdater &updater) {
auto escapingClosure = cvt->getOperand();
auto escapingClosureTy = escapingClosure->getType();
auto optionalEscapingClosureTy = SILType::getOptionalType(escapingClosureTy);
auto loc = RegularLocation::getAutoGeneratedLocation();
SmallVector<SILBasicBlock *, 4> exitingBlocks;
fn.findExitingBlocks(exitingBlocks);
auto createLifetimeEnd = [](SILLocation loc, SILInstruction *insertPt,
SILValue value) {
SILBuilderWithScope builder(insertPt);
if (value->getOwnershipKind() == OwnershipKind::Owned) {
builder.emitDestroyOperation(loc, value);
return;
}
builder.emitEndBorrowOperation(loc, value);
};
auto createLifetimeEndAtFunctionExits =
[&](std::function<SILValue(SILBasicBlock *)> getValue) {
for (auto *block : exitingBlocks) {
auto *safeDestructionPoint =
getDeinitSafeClosureDestructionPoint(block);
createLifetimeEnd(loc, safeDestructionPoint, getValue(block));
}
};
// If our cvt is in the initial block, we do not need to use the SSA updater
// since we know cvt cannot be in a loop and must dominate all exits
// (*). Just insert a copy of the escaping closure at the cvt and destroys at
// the exit blocks of the function.
//
// (*) In fact we can't use the SILSSAUpdater::GetValueInMiddleOfBlock.
if (cvt->getParent() == cvt->getFunction()->getEntryBlock()) {
auto *copy = SILBuilderWithScope(cvt).createCopyValue(loc, escapingClosure);
cvt->setLifetimeGuaranteed();
cvt->setOperand(copy);
createLifetimeEndAtFunctionExits([©](SILBasicBlock *) { return copy; });
return;
}
// Create a copy of the convert_escape_to_no_escape.
// NOTE: The SSAUpdater does not support providing multiple values in the same
// block without extra work. So the fact that cvt is not in the entry block
// means that we don't have to worry about overwriting the .none value.
auto *copy = SILBuilderWithScope(cvt).createCopyValue(loc, escapingClosure);
cvt->setLifetimeGuaranteed();
cvt->setOperand(copy);
// Create an optional some to extend the lifetime of copy until function
// exits.
SILBuilderWithScope lifetimeExtendBuilder(std::next(cvt->getIterator()));
auto *optionalSome = lifetimeExtendBuilder.createOptionalSome(
loc, copy, optionalEscapingClosureTy);
// Create a borrow scope and a mark_dependence to prevent the enum being
// optimized away.
auto *borrow = lifetimeExtendBuilder.createBeginBorrow(loc, optionalSome);
auto *mdi =
lifetimeExtendBuilder.createMarkDependence(loc, cvt, borrow,
MarkDependenceKind::Escaping);
// Replace all uses of the non escaping closure with mark_dependence
SmallVector<Operand *, 4> convertUses;
for (auto *cvtUse : cvt->getUses()) {
convertUses.push_back(cvtUse);
}
for (auto *cvtUse : convertUses) {
auto *cvtUser = cvtUse->getUser();
if (cvtUser == mdi)
continue;
cvtUser->setOperand(cvtUse->getOperandNumber(), mdi);
}
auto fixupSILForLifetimeExtension = [&](SILValue value, SILValue entryValue) {
// Use SSAUpdater to find insertion points for lifetime ends.
updater.initialize(value->getFunction(), optionalEscapingClosureTy,
value->getOwnershipKind());
SmallVector<SILPhiArgument *, 8> insertedPhis;
updater.setInsertedPhis(&insertedPhis);
updater.addAvailableValue(fn.getEntryBlock(), entryValue);
updater.addAvailableValue(value->getParentBlock(), value);
{
// Since value maybe in a loop, insert an extra lifetime end. Since we
// used our enum value, this is safe.
SILValue midValue =
updater.getValueInMiddleOfBlock(value->getParentBlock());
createLifetimeEnd(loc, cvt, midValue);
}
// Insert lifetime ends.
createLifetimeEndAtFunctionExits([&updater](SILBasicBlock *block) {
return updater.getValueAtEndOfBlock(block);
});
// Prune the phis inserted by the SSA updater that only take
// the .none from the entry block.
// TODO: Should we sort inserted phis before or after we initialize
// the worklist or maybe backwards? We should investigate how the
// SSA updater adds phi nodes to this list to resolve this question.
cleanupDeadTrivialPhiArgs(entryValue, insertedPhis);
};
// Create an optional none at the function entry.
auto *optionalNone = SILBuilderWithScope(fn.getEntryBlock()->begin())
.createOptionalNone(loc, optionalEscapingClosureTy);
auto *borrowNone = SILBuilderWithScope(optionalNone->getNextInstruction())
.createBeginBorrow(loc, optionalNone);
// Use the SSAUpdater to create lifetime ends for the copy and the borrow.
fixupSILForLifetimeExtension(borrow, borrowNone);
fixupSILForLifetimeExtension(optionalSome, optionalNone);
}
static SILInstruction *lookThroughRebastractionUsers(
SILInstruction *inst,
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized) {
if (inst == nullptr)
return nullptr;
// Try a cached lookup.
auto res = memoized.find(inst);
if (res != memoized.end())
return res->second;
// Cache recursive results.
auto memoizeResult = [&](SILInstruction *from, SILInstruction *toResult) {
memoized[from] = toResult;
return toResult;
};
auto getSingleNonDebugNonRefCountUser =
[](SILValue v) -> SILInstruction* {
SILInstruction *singleNonDebugNonRefCountUser = nullptr;
for (auto *use : getNonDebugUses(v)) {
auto *user = use->getUser();
if (onlyAffectsRefCount(user))
continue;
if (isa<EndBorrowInst>(user))
continue;
if (singleNonDebugNonRefCountUser) {
return nullptr;
}
singleNonDebugNonRefCountUser = user;
}
return singleNonDebugNonRefCountUser;
};
// If we have a convert_function, just look at its user.
if (auto *cvt = dyn_cast<ConvertFunctionInst>(inst))
return memoizeResult(inst, lookThroughRebastractionUsers(
getSingleNonDebugNonRefCountUser(cvt), memoized));
if (auto *cvt = dyn_cast<ConvertEscapeToNoEscapeInst>(inst))
return memoizeResult(inst, lookThroughRebastractionUsers(
getSingleNonDebugNonRefCountUser(cvt), memoized));
// If we have a partial_apply user look at its single (non release) user.
if (auto *pa = dyn_cast<PartialApplyInst>(inst))
return memoizeResult(inst, lookThroughRebastractionUsers(
getSingleNonDebugNonRefCountUser(pa), memoized));
// TODO: If the single user is a borrow, then generally the lifetime of that
// borrow ought to delineate the lifetime of the closure. But some codegen
// patterns in SILGen will try to notionally lifetime-extend the value by
// copying it and putting the lifetime on the copy. So look at the single
// user of the borrow, if any, to determine the lifetime this should have.
if (auto borrow = dyn_cast<BeginBorrowInst>(inst)) {
return memoizeResult(inst, lookThroughRebastractionUsers(
getSingleNonDebugNonRefCountUser(borrow), memoized));
}
return inst;
}
/// Insert a mark_dependence for any non-trivial argument of a partial_apply.
static SILValue insertMarkDependenceForCapturedArguments(PartialApplyInst *pai,
SILBuilder &b) {
SILValue curr(pai);
// Mark dependence on all non-trivial arguments that weren't borrowed.
for (auto &arg : pai->getArgumentOperands()) {
if (isa<BeginBorrowInst>(arg.get())
|| arg.get()->getType().isTrivial(*pai->getFunction()))
continue;
if (auto *m = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(arg.get()))
if (m->hasGuaranteedInitialKind())
continue;
curr = b.createMarkDependence(pai->getLoc(), curr, arg.get(),
MarkDependenceKind::NonEscaping);
}
return curr;
}
/// Returns the (single) "endAsyncLetLifetime" builtin if \p startAsyncLet is a
/// "startAsyncLetWithLocalBuffer" builtin.
static BuiltinInst *getEndAsyncLet(BuiltinInst *startAsyncLet) {
if (startAsyncLet->getBuiltinKind() != BuiltinValueKind::StartAsyncLetWithLocalBuffer)
return nullptr;
BuiltinInst *endAsyncLet = nullptr;
for (Operand *op : startAsyncLet->getUses()) {
auto *endBI = dyn_cast<BuiltinInst>(op->getUser());
if (endBI && endBI->getBuiltinKind() == BuiltinValueKind::EndAsyncLetLifetime) {
// At this stage of the pipeline, it's always the case that a
// startAsyncLet has an endAsyncLet: that's how SILGen generates it.
// Just to be on the safe side, do this check.
if (endAsyncLet)
return nullptr;
endAsyncLet = endBI;
}
}
return endAsyncLet;
}
/// Call the \p insertFn with a builder at all insertion points after
/// a closure is used by \p closureUser.
static void insertAfterClosureUser(SILInstruction *closureUser,
function_ref<void(SILBuilder &)> insertFn) {
{
SILInstruction *userForBorrow = closureUser;
if (auto *m = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(userForBorrow))
if (m->hasGuaranteedInitialKind())
if (auto *svi = dyn_cast<SingleValueInstruction>(m->getOperand()))
userForBorrow = svi;
if (auto *beginBorrow = dyn_cast<BeginBorrowInst>(userForBorrow)) {
// Insert everywhere after the borrow is ended.
SmallVector<EndBorrowInst *, 4> endBorrows;
for (auto eb : beginBorrow->getEndBorrows()) {
endBorrows.push_back(eb);
}
for (auto eb : endBorrows) {
SILBuilderWithScope builder(std::next(eb->getIterator()));
insertFn(builder);
}
return;
}
}
if (auto *startAsyncLet = dyn_cast<BuiltinInst>(closureUser)) {
BuiltinInst *endAsyncLet = getEndAsyncLet(startAsyncLet);
if (!endAsyncLet)
return;
SILBuilderWithScope builder(std::next(endAsyncLet->getIterator()));
insertFn(builder);
return;
}
FullApplySite fas = FullApplySite::isa(closureUser);
assert(fas);
fas.insertAfterApplication(insertFn);
}
static SILValue skipConvert(SILValue v) {
auto *cvt = dyn_cast<ConvertFunctionInst>(v);
if (!cvt)
return v;
auto *pa = dyn_cast<PartialApplyInst>(cvt->getOperand());
if (!pa || !pa->hasOneUse())
return v;
return pa;
}
static SILAnalysis::InvalidationKind
analysisInvalidationKind(const bool &modifiedCFG) {
return modifiedCFG ? SILAnalysis::InvalidationKind::FunctionBody
: SILAnalysis::InvalidationKind::CallsAndInstructions;
}
/// Find the stack closure's lifetime ends. This should be indicated either by
/// direct destruction of the closure after its application, or the destruction
/// of its consuming use, which should be either another function conversion
/// or a partial_apply into a closure that will also be imminently transformed
/// into a stack partial apply. The lifetime of the closure should not escape
/// the current function or we wouldn't be able to embark on this transform.
static void
collectStackClosureLifetimeEnds(SmallVectorImpl<SILInstruction *> &lifetimeEnds,
SILValue v) {
for (Operand *consume : v->getConsumingUses()) {
SILInstruction *consumer = consume->getUser();
if (isa<DestroyValueInst>(consumer)) {
lifetimeEnds.push_back(consumer);
continue;
}
if (auto pa = dyn_cast<PartialApplyInst>(consumer)) {
// The closure may be captured into another partial_apply (usually
// a reabstraction thunk, but possibly a nested closure-in-closure).
// This other partial_apply ought to be imminently changing into
// a nonescaping closure as well, so we want the end of the
// `convert_escape_to_noescape` operation's lifetime rather than the
// original escaping closure's.
//
// Any partial_apply already converted to a stack closure should have
// also been converted to borrowing its captures.
assert(!pa->isOnStack());
SILValue singlePAUser = pa;
do {
SILInstruction *nextUser = nullptr;
for (auto use : singlePAUser->getUses()) {
if (isa<DestroyValueInst>(use->getUser())) {
continue;
}
assert(!nextUser && "more than one non-destroying use?!");
nextUser = use->getUser();
}
assert(nextUser && nextUser->getNumResults() == 1
&& "partial_apply capturing a nonescaping closure that isn't"
"itself nonescaping?!");
singlePAUser = nextUser->getResult(0);
} while (!isa<ConvertEscapeToNoEscapeInst>(singlePAUser));
auto convert = cast<ConvertEscapeToNoEscapeInst>(singlePAUser);
collectStackClosureLifetimeEnds(lifetimeEnds, convert);
continue;
}
// There shouldn't be any other consuming uses of the value that aren't
// forwarding.
assert(consumer->hasResults());
for (auto result : consumer->getResults()) {
collectStackClosureLifetimeEnds(lifetimeEnds, result);
}
}
}
static bool lookThroughMarkDependenceChainForValue(MarkDependenceInst *mark,
PartialApplyInst *pai) {
if (mark->getValue() == pai) {
return true;
}
auto *markChain = dyn_cast<MarkDependenceInst>(mark->getValue());
if (!markChain) {
return false;
}
return lookThroughMarkDependenceChainForValue(markChain, pai);
}
/// Rewrite a partial_apply convert_escape_to_noescape sequence with a single
/// apply/try_apply user to a partial_apply [stack] terminated with a
/// dealloc_stack placed after the apply.
///
/// %p = partial_apply %f(%a, %b)
/// %ne = convert_escape_to_noescape %p
/// apply %f2(%p)
/// destroy_value %p
///
/// =>
///
/// %ab = begin_borrow %a
/// %bb = begin_borrow %b
/// %p = partial_apply [stack] %f(%aa, %bb)
/// apply %f2(%p)
/// destroy_value %p
/// end_borrow %bb
/// end_borrow %aa
static SILValue tryRewriteToPartialApplyStack(
ConvertEscapeToNoEscapeInst *cvt, SILInstruction *closureUser,
DominanceAnalysis *dominanceAnalysis, InstructionDeleter &deleter,
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
ReachableBlocks const &reachableBlocks, const bool &modifiedCFG) {
auto *origPA = dyn_cast<PartialApplyInst>(skipConvert(cvt->getOperand()));
if (!origPA)
return SILValue();
auto *convertOrPartialApply = cast<SingleValueInstruction>(origPA);
if (cvt->getOperand() != origPA)
convertOrPartialApply = cast<ConvertFunctionInst>(cvt->getOperand());
// Whenever we delete an instruction advance the iterator and remove the
// instruction from the memoized map.
auto saveDeleteInst = [&](SILInstruction *i) {
memoized.erase(i);
deleter.forceDelete(i);
};
// Look for a single non ref count user of the partial_apply.
SmallVector<SILInstruction *, 8> refCountInsts;
SILInstruction *singleNonDebugNonRefCountUser = nullptr;
for (auto *use : getNonDebugUses(convertOrPartialApply)) {
auto *user = use->getUser();
if (onlyAffectsRefCount(user)) {
refCountInsts.push_back(user);
continue;
}
if (singleNonDebugNonRefCountUser)
return SILValue();
singleNonDebugNonRefCountUser = user;
}
SILBuilderWithScope b(cvt);
// Remove the original destroy of the partial_apply, if any, since the
// nonescaping closure's lifetime becomes the lifetime of the new
// partial_apply.
if (auto destroy = convertOrPartialApply->getSingleUserOfType<DestroyValueInst>()) {
saveDeleteInst(destroy);
}
// Borrow the arguments that need borrowing.
SmallVector<MoveOnlyWrapperToCopyableValueInst *, 8>
noImplicitCopyWrapperToDelete;
SmallVector<SILValue, 8> args;
for (Operand &arg : origPA->getArgumentOperands()) {
auto argTy = arg.get()->getType();
if (!argTy.isAddress() && !argTy.isTrivial(*cvt->getFunction())) {
SILValue argValue = arg.get();
bool foundNoImplicitCopy = false;
if (auto *mmci = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(argValue)) {
if (mmci->hasOwnedInitialKind() && mmci->hasOneUse()) {
foundNoImplicitCopy = true;
argValue = mmci->getOperand();
noImplicitCopyWrapperToDelete.push_back(mmci);
}
}
SILValue borrow = b.createBeginBorrow(origPA->getLoc(), argValue);
if (foundNoImplicitCopy)
borrow = b.createGuaranteedMoveOnlyWrapperToCopyableValue(
origPA->getLoc(), borrow);
args.push_back(borrow);
} else {
args.push_back(arg.get());
}
}
// The convert_escape_to_noescape is the only user of the partial_apply.
// Convert to a partial_apply [stack].
auto newPA = b.createPartialApply(
origPA->getLoc(), origPA->getCallee(), origPA->getSubstitutionMap(), args,
origPA->getCalleeConvention(), origPA->getResultIsolation(),
PartialApplyInst::OnStackKind::OnStack);
// Insert mark_dependence for any non-trivial address operands to the
// partial_apply.
auto closure = insertMarkDependenceForCapturedArguments(newPA, b);
SILValue closureOp = closure;
// Optionally, replace the convert_function instruction.
if (auto *convert = dyn_cast<ConvertFunctionInst>(convertOrPartialApply)) {
/* DEBUG
llvm::errs() << "=== replacing conversion\n";
convert->dumpInContext();
*/
auto origTy = convert->getType().castTo<SILFunctionType>();
auto origWithNoEscape = SILType::getPrimitiveObjectType(
origTy->getWithExtInfo(origTy->getExtInfo().withNoEscape()));
closureOp = b.createConvertFunction(convert->getLoc(), closure,
origWithNoEscape, false);
/* DEBUG
llvm::errs() << "--- with\n";
closureOp->dumpInContext();
*/
}
// Replace the convert_escape_to_noescape uses with the new
// partial_apply [stack].
cvt->replaceAllUsesWith(closureOp);
saveDeleteInst(cvt);
// Delete the ref count operations on the original partial_apply.
for (auto *refInst : refCountInsts)
saveDeleteInst(refInst);
convertOrPartialApply->replaceAllUsesWith(newPA);
if (convertOrPartialApply != origPA)
saveDeleteInst(convertOrPartialApply);
saveDeleteInst(origPA);
// Delete the mmci of the origPA.
while (!noImplicitCopyWrapperToDelete.empty())
saveDeleteInst(noImplicitCopyWrapperToDelete.pop_back_val());
ApplySite site(newPA);
SILFunctionConventions calleeConv(site.getSubstCalleeType(),
newPA->getModule());
// Since we create temporary allocation for in_guaranteed captures during SILGen,
// the dealloc_stack of it can occur before the apply due to conversion scopes.
// When we insert destroy_addr of the in_guaranteed capture after the apply,
// we may end up with a situation when the dealloc_stack occurs before the destroy_addr.
// The code below proactively removes the dealloc_stack of in_guaranteed capture,
// so that it can be reinserted at the correct place after the destroy_addr below.
for (auto &arg : newPA->getArgumentOperands()) {
unsigned calleeArgumentIndex = site.getCalleeArgIndex(arg);
assert(calleeArgumentIndex >= calleeConv.getSILArgIndexOfFirstParam());
auto paramInfo = calleeConv.getParamInfoForSILArg(calleeArgumentIndex);
if (paramInfo.getConvention() == ParameterConvention::Indirect_In_Guaranteed) {
SILValue argValue = arg.get();
if (auto *mmci = dyn_cast<MoveOnlyWrapperToCopyableAddrInst>(argValue))
argValue = mmci->getOperand();
// go over all the dealloc_stack, remove it
SmallVector<Operand *, 16> Uses(argValue->getUses());
for (auto use : Uses) {
if (auto *deallocInst = dyn_cast<DeallocStackInst>(use->getUser()))
deleter.forceDelete(deallocInst);
}
}
}
// End borrows and insert destroys of arguments after the stack closure's
// lifetime ends.
SmallVector<SILInstruction *, 4> lifetimeEnds;
collectStackClosureLifetimeEnds(lifetimeEnds, closureOp);
// For address-only captures, see if we can eliminate the copy
// that SILGen emitted to allow the original partial_apply to take ownership.
// We do this here because otherwise the move checker will see the copy as an
// attempt to consume the value, which we don't want.
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
SSAPrunedLiveness closureLiveness(cvt->getFunction(), &discoveredBlocks);
closureLiveness.initializeDef(closureOp);
llvm::SmallSetVector<SILValue, 4> borrowedOriginals;
unsigned appliedArgStartIdx =
newPA->getOrigCalleeType()->getNumParameters() - newPA->getNumArguments();
for (unsigned i : indices(newPA->getArgumentOperands())) {
auto &arg = newPA->getArgumentOperands()[i];
SILValue copy = arg.get();
// The temporary should be a local stack allocation.
LLVM_DEBUG(llvm::dbgs() << "considering whether to eliminate copy of capture\n";
copy->printInContext(llvm::dbgs());
llvm::dbgs() << "\n");
auto stack = dyn_cast<AllocStackInst>(copy);
if (!stack) {
LLVM_DEBUG(llvm::dbgs() << "-- not an alloc_stack\n");
continue;
}
if (DisableCopyEliminationOfCopyableCapture) {
if (!copy->getType().isMoveOnly()) {
LLVM_DEBUG(llvm::dbgs() << "-- not move-only\n");
continue;
}
}
// Is the capture a borrow?
auto paramIndex = i + appliedArgStartIdx;
auto param = newPA->getOrigCalleeType()->getParameters()[paramIndex];
LLVM_DEBUG(param.print(llvm::dbgs());
llvm::dbgs() << '\n');
if (!param.isIndirectInGuaranteed()) {
LLVM_DEBUG(llvm::dbgs() << "-- not an in_guaranteed parameter\n";
newPA->getOrigCalleeType()->getParameters()[paramIndex]
.print(llvm::dbgs());
llvm::dbgs() << "\n");
continue;
}
// It needs to have been initialized by copying from somewhere else.
CopyAddrInst *initialization = nullptr;
MarkDependenceInst *markDep = nullptr;
for (auto *use : stack->getUses()) {
auto *user = use->getUser();
// Since we removed the `dealloc_stack`s from the capture arguments,
// the only uses of this stack slot should be the initialization, the
// partial application, and possibly a mark_dependence from the
// buffer to the partial application.
if (use->getUser() == newPA) {
continue;
}
if (auto mark = dyn_cast<MarkDependenceInst>(use->getUser())) {
// When we insert mark_dependence for non-trivial address operands, we
// emit a chain that looks like:
// %md = mark_dependence %pai on %0
// %md2 = mark_dependence %md on %1
// to tie all of those operands together on the same partial_apply.
// Check if we're marking dependence on this stack slot for the current
// partial_apply or it's chain of mark_dependences.
if (!lookThroughMarkDependenceChainForValue(mark, newPA) ||
mark->getBase() != stack) {
LLVM_DEBUG(llvm::dbgs() << "-- had unexpected mark_dependence use\n";
use->getUser()->print(llvm::dbgs()); llvm::dbgs() << "\n");
initialization = nullptr;
break;
}
markDep = mark;
continue;
}
// If we saw more than just the initialization, this isn't a pattern we
// recognize.
if (initialization) {
LLVM_DEBUG(llvm::dbgs()
<< "-- had non-initialization, non-partial-apply use\n";
use->getUser()->print(llvm::dbgs()); llvm::dbgs() << "\n");
initialization = nullptr;
break;
}
if (auto possibleInit = dyn_cast<CopyAddrInst>(use->getUser())) {
// Should copy the source and initialize the destination.
if (possibleInit->isTakeOfSrc() ||
!possibleInit->isInitializationOfDest()) {
LLVM_DEBUG(
llvm::dbgs()
<< "-- had non-initialization, non-partial-apply use\n";
use->getUser()->print(llvm::dbgs()); llvm::dbgs() << "\n");
break;
}
// This is the initialization if there are no other uses.
initialization = possibleInit;
continue;
}
if (isa<DebugValueInst>(user) || isa<DestroyAddrInst>(user) ||
isa<DeallocStackInst>(user)) {
continue;
}
LLVM_DEBUG(llvm::dbgs() << "-- unrecognized use\n");
// Reset initialization on an unrecognized use
initialization = nullptr;
break;
}
if (!initialization) {
LLVM_DEBUG(llvm::dbgs() << "-- failed to find single initializing use\n");
continue;
}
// The source should have no writes in the duration of the partial_apply's
// liveness.
auto orig = initialization->getSrc();
LLVM_DEBUG(llvm::dbgs() << "++ found original:\n";
orig->print(llvm::dbgs());
llvm::dbgs() << "\n");
bool origIsUnmodifiedDuringClosureLifetime = true;
class OrigUnmodifiedDuringClosureLifetimeWalker
: public TransitiveAddressWalker<
OrigUnmodifiedDuringClosureLifetimeWalker> {
SSAPrunedLiveness &closureLiveness;
bool &origIsUnmodifiedDuringClosureLifetime;
public:
OrigUnmodifiedDuringClosureLifetimeWalker(
SSAPrunedLiveness &closureLiveness,
bool &origIsUnmodifiedDuringClosureLifetime)
: closureLiveness(closureLiveness),
origIsUnmodifiedDuringClosureLifetime(
origIsUnmodifiedDuringClosureLifetime) {}
bool visitUse(Operand *origUse) {
LLVM_DEBUG(llvm::dbgs() << "looking at use\n";
origUse->getUser()->printInContext(llvm::dbgs());
llvm::dbgs() << "\n");
// If the user doesn't write to memory, then it's harmless.
if (!origUse->getUser()->mayWriteToMemory()) {
return true;
}
if (closureLiveness.isWithinBoundary(origUse->getUser(),
/*deadEndBlocks=*/nullptr)) {
origIsUnmodifiedDuringClosureLifetime = false;
LLVM_DEBUG(llvm::dbgs() << "-- original has other possibly writing "
"use during closure lifetime\n";
origUse->getUser()->print(llvm::dbgs());
llvm::dbgs() << "\n");
return false;
}
return true;
}
};
OrigUnmodifiedDuringClosureLifetimeWalker origUseWalker(
closureLiveness, origIsUnmodifiedDuringClosureLifetime);
switch (origUseWalker.walk(orig)) {
case AddressUseKind::NonEscaping:
case AddressUseKind::Dependent:
// Dependent uses are ignored because they cannot modify the original.
break;
case AddressUseKind::PointerEscape:
case AddressUseKind::Unknown:
continue;
}
if (!origIsUnmodifiedDuringClosureLifetime) {
continue;
}
// OK, we can use the original. Eliminate the copy and replace it with the
// original.
LLVM_DEBUG(llvm::dbgs() << "++ replacing with original!\n");
arg.set(orig);
if (markDep) {
markDep->setBase(orig);
}
initialization->eraseFromParent();
stack->eraseFromParent();
borrowedOriginals.insert(orig);
}
/* DEBUG
llvm::errs() << "=== found lifetime ends for\n";
closureOp->dump();
llvm::errs() << "--- at\n";
*/
for (auto destroy : lifetimeEnds) {
/* DEBUG
destroy->dump();
*/
SILBuilderWithScope builder(std::next(destroy->getIterator()));
// This getCapturedArg hack attempts to perfectly compensate for all the
// other hacks involved in gathering new arguments above.
// argValue may be 'undef'
auto getArgToDestroy = [&](SILValue argValue) -> SILValue {
// A MoveOnlyWrapperToCopyableValueInst may produce a trivial value. Be
// careful not to emit an extra destroy of the original.
if (argValue->getType().isTrivial(destroy->getFunction()))
return SILValue();
// We may have inserted a new begin_borrow->moveonlywrapper_to_copyvalue
// when creating the new arguments. Now we need to end that borrow.
if (auto *m = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(argValue))
if (m->hasGuaranteedInitialKind())
argValue = m->getOperand();
auto *argBorrow = dyn_cast<BeginBorrowInst>(argValue);
if (argBorrow) {
argValue = argBorrow->getOperand();
builder.createEndBorrow(newPA->getLoc(), argBorrow);
}
// Don't need to destroy if we borrowed in place .
return borrowedOriginals.count(argValue) ? SILValue() : argValue;
};
insertDestroyOfCapturedArguments(newPA, builder, getArgToDestroy,
newPA->getLoc());
}
/* DEBUG
llvm::errs() << "=== function after conversion to stack partial_apply of\n";
newPA->dump();
llvm::errs() << "---\n";
newPA->getFunction()->dump();
*/
// The CFG may have been modified during this run. If it was, the dominance
// analysis would no longer be valid. Invalidate it now if necessary,
// according to the kinds of changes that may have been made. Note that if
// the CFG hasn't been modified, this is a noop thanks to
// DominanceAnalysis::shouldInvalidate's definition.
dominanceAnalysis->invalidate(closureUser->getFunction(),
analysisInvalidationKind(modifiedCFG));
// Insert dealloc_stacks of any in_guaranteed captures.
// Don't run insertDeallocOfCapturedArguments if newPA is in an unreachable
// block insertDeallocOfCapturedArguments will run code that computes the DF
// for newPA that will loop infinitely.
if (!reachableBlocks.isReachable(newPA->getParent()))
return closureOp;
auto getAddressToDealloc = [&](SILValue argAddress) -> SILValue {
if (auto moveWrapper =
dyn_cast<MoveOnlyWrapperToCopyableAddrInst>(argAddress)) {
argAddress = moveWrapper->getOperand();
}
// Don't need to destroy if we borrowed in place .
return borrowedOriginals.count(argAddress) ? SILValue() : argAddress;
};
insertDeallocOfCapturedArguments(
newPA, dominanceAnalysis->get(closureUser->getFunction()),
getAddressToDealloc);
return closureOp;
}
static bool tryExtendLifetimeToLastUse(
ConvertEscapeToNoEscapeInst *cvt, DominanceAnalysis *dominanceAnalysis,