-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathClosureLifetimeFixup.cpp
1022 lines (905 loc) · 37.6 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/Defer.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/BasicBlockDatastructures.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.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);
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());
}
}
/// Extend the lifetime of the convert_escape_to_noescape's operand to the end
/// of the function.
///
/// 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) {
auto escapingClosure = cvt->getOperand();
auto escapingClosureTy = escapingClosure->getType();
auto optionalEscapingClosureTy = SILType::getOptionalType(escapingClosureTy);
auto loc = RegularLocation::getAutoGeneratedLocation();
// If our Cvt is in the initial block, we do not need to use the SSA updater
// since we know Cvt can not 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 *innerCVI =
SILBuilderWithScope(cvt).createCopyValue(loc, escapingClosure);
cvt->setLifetimeGuaranteed();
cvt->setOperand(innerCVI);
SmallVector<SILBasicBlock *, 4> exitingBlocks;
fn.findExitingBlocks(exitingBlocks);
for (auto *block : exitingBlocks) {
auto *safeDestructPoint = getDeinitSafeClosureDestructionPoint(block);
SILBuilderWithScope(safeDestructPoint).createDestroyValue(loc, innerCVI);
}
return;
}
// Ok. At this point we know that Cvt is not in the entry block... so we can
// use SILSSAUpdater::GetValueInMiddleOfBlock() to extend the object's
// lifetime respecting loops.
SmallVector<SILPhiArgument *, 8> insertedPhis;
SILSSAUpdater updater(&insertedPhis);
updater.initialize(optionalEscapingClosureTy, fn.hasOwnership()
? OwnershipKind::Owned
: OwnershipKind::None);
// Create an Optional<() -> ()>.none in the entry block of the function and
// add it as an available value to the SSAUpdater.
//
// Since we know that Cvt is not in the entry block and this must be, we know
// that it is safe to use the SSAUpdater's getValueInMiddleOfBlock with this
// value.
SILValue entryBlockOptionalNone = [&]() -> SILValue {
SILBuilderWithScope b(fn.getEntryBlock()->begin());
return b.createOptionalNone(loc, optionalEscapingClosureTy);
}();
updater.addAvailableValue(fn.getEntryBlock(), entryBlockOptionalNone);
// Create a copy of the convert_escape_to_no_escape and add it as an available
// value to the SSA updater.
//
// 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 *cvi = [&]() -> CopyValueInst * {
auto *innerCVI =
SILBuilderWithScope(cvt).createCopyValue(loc, escapingClosure);
cvt->setLifetimeGuaranteed();
cvt->setOperand(innerCVI);
SILBuilderWithScope b(std::next(cvt->getIterator()));
updater.addAvailableValue(
cvt->getParent(),
b.createOptionalSome(loc, innerCVI, optionalEscapingClosureTy));
return innerCVI;
}();
// Then we use the SSA updater to insert a destroy_value before the cvt and at
// the reachable exit blocks.
SmallVector<SILBasicBlock *, 4> exitingBlocks;
findReachableExitBlocks(cvt, exitingBlocks);
{
// Before the copy value, insert an extra destroy_value to handle
// loops. Since we used our enum value this is safe.
SILValue v = updater.getValueInMiddleOfBlock(cvi->getParent());
SILBuilderWithScope(cvi).createDestroyValue(loc, v);
}
for (auto *block : exitingBlocks) {
auto *safeDestructionPt = getDeinitSafeClosureDestructionPoint(block);
SILValue v = updater.getValueAtEndOfBlock(block);
SILBuilderWithScope(safeDestructionPt).createDestroyValue(loc, v);
}
// Finally, we need to prune 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(entryBlockOptionalNone, insertedPhis);
}
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 (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));
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.
for (auto &arg : pai->getArgumentOperands()) {
if (arg.get()->getType().isTrivial(*pai->getFunction()))
continue;
curr = b.createMarkDependence(pai->getLoc(), curr, arg.get());
}
return curr;
}
/// 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
///
/// =>
///
/// %p = partial_apply [stack] %f(%a, %b)
/// %md = mark_dependence %p on %a
/// %md2 = mark_dependence %md on %b
/// apply %f2(%md2)
/// dealloc_stack %p
/// destroy_value %a
/// destroy_value %b
///
/// Note: If the rewrite succeeded we have inserted a dealloc_stack. This
/// dealloc_stack still needs to be balanced with other dealloc_stacks i.e the
/// caller needs to use the StackNesting utility to update the dealloc_stack
/// nesting.
static bool tryRewriteToPartialApplyStack(
SILLocation &loc, PartialApplyInst *origPA,
ConvertEscapeToNoEscapeInst *cvt, SILInstruction *singleApplyUser,
SILBasicBlock::iterator &advanceIfDelete,
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized) {
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) {
if (&*advanceIfDelete == i)
++advanceIfDelete;
memoized.erase(i);
i->eraseFromParent();
};
// 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 false;
singleNonDebugNonRefCountUser = user;
}
SILBuilderWithScope b(cvt);
// The convert_escape_to_noescape is the only user of the partial_apply.
// Convert to a partial_apply [stack].
SmallVector<SILValue, 8> args;
for (auto &arg : origPA->getArgumentOperands())
args.push_back(arg.get());
auto newPA = b.createPartialApply(
origPA->getLoc(), origPA->getCallee(), origPA->getSubstitutionMap(), args,
origPA->getType().getAs<SILFunctionType>()->getCalleeConvention(),
PartialApplyInst::OnStackKind::OnStack);
// Insert mark_dependence for any non-trivial operand to the partial_apply.
auto closure = insertMarkDependenceForCapturedArguments(newPA, b);
// Optionally, replace the convert_function instruction.
if (auto *convert = dyn_cast<ConvertFunctionInst>(convertOrPartialApply)) {
auto origTy = convert->getType().castTo<SILFunctionType>();
auto origWithNoEscape = SILType::getPrimitiveObjectType(
origTy->getWithExtInfo(origTy->getExtInfo().withNoEscape()));
closure = b.createConvertFunction(convert->getLoc(), closure,
origWithNoEscape, false);
convert->replaceAllUsesWith(closure);
}
// Replace the convert_escape_to_noescape uses with the new
// partial_apply [stack].
cvt->replaceAllUsesWith(closure);
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);
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) {
// go over all the dealloc_stack, remove it
SmallVector<Operand*, 16> Uses(arg.get()->getUses());
for (auto use : Uses)
if (auto *deallocInst = dyn_cast<DeallocStackInst>(use->getUser()))
deallocInst->eraseFromParent();
}
}
// Insert destroys of arguments after the apply and the dealloc_stack.
if (auto *apply = dyn_cast<ApplyInst>(singleApplyUser)) {
auto insertPt = std::next(SILBasicBlock::iterator(apply));
// Don't insert dealloc_stacks at unreachable.
if (isa<UnreachableInst>(*insertPt))
return true;
SILBuilderWithScope b3(insertPt);
b3.createDeallocStack(loc, newPA);
insertDestroyOfCapturedArguments(newPA, b3);
// dealloc_stack of the in_guaranteed capture is inserted
insertDeallocOfCapturedArguments(newPA, b3);
} else if (auto *tai = dyn_cast<TryApplyInst>(singleApplyUser)) {
for (auto *succBB : tai->getSuccessorBlocks()) {
SILBuilderWithScope b3(succBB->begin());
b3.createDeallocStack(loc, newPA);
insertDestroyOfCapturedArguments(newPA, b3);
// dealloc_stack of the in_guaranteed capture is inserted
insertDeallocOfCapturedArguments(newPA, b3);
}
} else {
llvm_unreachable("Unknown FullApplySite instruction kind");
}
return true;
}
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 bool tryExtendLifetimeToLastUse(
ConvertEscapeToNoEscapeInst *cvt,
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
SILBasicBlock::iterator &advanceIfDelete) {
// If there is a single user that is an apply this is simple: extend the
// lifetime of the operand until after the apply.
auto *singleUser = lookThroughRebastractionUsers(cvt, memoized);
if (!singleUser)
return false;
// Handle an apply.
if (auto singleApplyUser = FullApplySite::isa(singleUser)) {
// FIXME: Don't know how-to handle begin_apply/end_apply yet.
if (isa<BeginApplyInst>(singleApplyUser.getInstruction())) {
return false;
}
auto loc = RegularLocation::getAutoGeneratedLocation();
auto origPA = dyn_cast<PartialApplyInst>(skipConvert(cvt->getOperand()));
if (origPA && tryRewriteToPartialApplyStack(
loc, origPA, cvt, singleApplyUser.getInstruction(),
advanceIfDelete, memoized))
return true;
// Insert a copy at the convert_escape_to_noescape [not_guaranteed] and
// change the instruction to the guaranteed form.
auto escapingClosure = cvt->getOperand();
auto *closureCopy =
SILBuilderWithScope(cvt).createCopyValue(loc, escapingClosure);
cvt->setLifetimeGuaranteed();
cvt->setOperand(closureCopy);
// Insert a destroy after the apply.
if (auto *apply = dyn_cast<ApplyInst>(singleApplyUser.getInstruction())) {
auto insertPt = std::next(SILBasicBlock::iterator(apply));
SILBuilderWithScope(insertPt).createDestroyValue(loc, closureCopy);
} else if (auto *tai =
dyn_cast<TryApplyInst>(singleApplyUser.getInstruction())) {
for (auto *succBB : tai->getSuccessorBlocks()) {
SILBuilderWithScope(succBB->begin())
.createDestroyValue(loc, closureCopy);
}
} else {
llvm_unreachable("Unknown FullApplySite instruction kind");
}
return true;
}
return false;
}
/// Ensure the lifetime of the closure across a two step optional conversion
/// from:
///
/// optional<@escaping () -> ()>
///
/// to:
///
/// optional<@noescape () -> ()>
///
/// to:
///
/// optional<@noescape @convention(block) () -> ()>
///
/// and all uses of the block. The pattern that we are looking for is:
///
/// switch_enum %optional_closure (1)
/// / \
/// %trivial_closure = CVT %closure nil (2)
/// \ /
/// switch_enum %optional_trivial_closure (3)
/// / \
/// %b = convertToBlock %trivial_closure nil (4)
/// \ /
/// ... uses of %optional_block ...
/// destroy_value %optional_block
///
/// where CVT is convert_escape_to_no_escape [not_guaranteed]. We assume that
/// the %optional_block is going through a conversion sequence in SILGen meaning
/// that we should only have a single destroy of the optional block.
///
/// NOTE: There is a *lifetime gap* during the usage of the trivial_closure!
/// This means we must be careful when lifetime extending. We can only assume
/// that the underlying closure is alive immediately at the CVT. So to perform
/// our lifetime extend, we do the following:
///
/// 1. We copy and borrow optional_closure, right before the switch_enum in
/// (1).
///
/// 2. We rewrite the convert_escape_to_no_escape guaranteed to use the copy
/// instead.
///
/// 3. To make sure that even after ossa is complete, we do not move any
/// destroys above the convert_escape_to_no_escape by putting a mark_dependence
/// on %closure
///
/// 4. We insert an end_borrow, destroy for the copy at the destroy of the
/// optional block.
static bool trySwitchEnumPeephole(ConvertEscapeToNoEscapeInst *cvt) {
auto *blockArg = dyn_cast<SILArgument>(cvt->getOperand());
if (!blockArg)
return false;
auto *convertSuccessorBlock = cvt->getParent()->getSingleSuccessorBlock();
if (!convertSuccessorBlock)
return false;
auto *predBB = cvt->getParent()->getSinglePredecessorBlock();
if (!predBB)
return false;
auto *switchEnum1 = dyn_cast<SwitchEnumInst>(predBB->getTerminator());
if (!switchEnum1)
return false;
auto *diamondSucc = getOptionalDiamondSuccessor(switchEnum1);
if (!diamondSucc)
return false;
auto *switchEnum2 = dyn_cast<SwitchEnumInst>(diamondSucc->getTerminator());
if (!switchEnum2)
return false;
auto *diamondSucc2 = getOptionalDiamondSuccessor(switchEnum2);
if (!diamondSucc2)
return false;
if (diamondSucc2->getNumArguments() != 1)
return false;
// Look for the last and only destroy of the diamond succ 2's argument. This
// is going to be the place where we destroy the lifetime extending copy.
SILInstruction *onlyDestroy = [&]() -> SILInstruction * {
SILInstruction *lastDestroy = nullptr;
for (auto *use : diamondSucc2->getArgument(0)->getUses()) {
SILInstruction *user = use->getUser();
if (isa<ReleaseValueInst>(user) || isa<StrongReleaseInst>(user) ||
isa<DestroyValueInst>(user)) {
if (lastDestroy)
return nullptr;
lastDestroy = user;
}
}
return lastDestroy;
}();
if (!onlyDestroy)
return false;
// Extend the lifetime.
auto loc = RegularLocation::getAutoGeneratedLocation();
SILValue copy, borrow;
std::tie(copy, borrow) = ([&]() -> std::pair<SILValue, SILValue> {
SILBuilderWithScope builder(switchEnum1);
auto copy = builder.emitCopyValueOperation(loc, switchEnum1->getOperand());
auto borrow = builder.emitBeginBorrowOperation(loc, copy);
return {copy, borrow};
})(); // end std::tie(copy, borrow).
{
SILBuilderWithScope builder(cvt);
auto value = builder.emitExtractOptionalPayloadOperation(loc, borrow);
cvt->setOperand(value);
cvt->setLifetimeGuaranteed();
}
{
SILBuilderWithScope builder(onlyDestroy);
builder.emitEndBorrowOperation(loc, borrow);
builder.emitDestroyValueOperation(loc, copy);
}
return true;
}
/// Look for a single destroy user and possibly unowned apply uses.
static SILInstruction *getOnlyDestroy(CopyBlockWithoutEscapingInst *cb) {
SILInstruction *onlyDestroy = nullptr;
for (auto *use : getNonDebugUses(cb)) {
SILInstruction *inst = use->getUser();
// If this an apply use, only handle unowned parameters.
if (auto apply = FullApplySite::isa(inst)) {
SILArgumentConvention conv = apply.getArgumentConvention(*use);
if (conv != SILArgumentConvention::Direct_Unowned)
return nullptr;
continue;
}
// We have already seen one destroy.
if (onlyDestroy)
return nullptr;
if (isa<DestroyValueInst>(inst) || isa<ReleaseValueInst>(inst) ||
isa<StrongReleaseInst>(inst)) {
onlyDestroy = inst;
continue;
}
// Some other instruction.
return nullptr;
}
if (!onlyDestroy)
return nullptr;
// Now look at whether the dealloc_stack or the destroy postdominates and
// return the post dominator.
auto *blockInit = dyn_cast<InitBlockStorageHeaderInst>(cb->getBlock());
if (!blockInit)
return nullptr;
auto *asi = dyn_cast<AllocStackInst>(blockInit->getBlockStorage());
if (!asi)
return nullptr;
auto *dealloc = asi->getSingleDeallocStack();
if (!dealloc || dealloc->getParent() != onlyDestroy->getParent())
return nullptr;
// Return the later instruction.
for (auto it = SILBasicBlock::iterator(onlyDestroy),
ie = dealloc->getParent()->end();
it != ie; ++it) {
if (&*it == dealloc)
return dealloc;
}
return onlyDestroy;
}
/// Lower a copy_block_without_escaping instruction.
///
/// This involves replacing:
///
/// %copy = copy_block_without_escaping %block withoutEscaping %closure
///
/// ...
/// destroy_value %copy
///
/// by (roughly) the instruction sequence:
///
/// %copy = copy_block %block
///
/// ...
/// destroy_value %copy
/// %e = is_escaping %closure
/// cond_fail %e
/// destroy_value %closure
static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,
bool &modifiedCFG) {
// Find the end of the lifetime of the copy_block_without_escaping
// instruction.
auto &fn = *cb->getFunction();
// If we find a single destroy, this destroy is going to be a destroy that may
// be in the same block as CB. It is important that we make sure that the
// destroy is in a different block than CB or any terminating blocks to ensure
// that we can use the SSAUpdater if needed.
auto *singleDestroy = getOnlyDestroy(cb);
if (singleDestroy && singleDestroy->getParent() == cb->getParent()) {
modifiedCFG = true;
{
SILBuilderWithScope b(singleDestroy);
splitBasicBlockAndBranch(b, singleDestroy, nullptr, nullptr);
}
{
SILBuilderWithScope b(singleDestroy);
auto *term = singleDestroy->getParent()->getTerminator();
if (term->isFunctionExiting()) {
splitBasicBlockAndBranch(b, &*std::next(singleDestroy->getIterator()),
nullptr, nullptr);
}
}
}
auto sentinelClosure = cb->getClosure();
auto loc = cb->getLoc();
// At this point, we transform our copy_block_without_escaping into a
// copy_block. This has a few important implications:
//
// 1. copy_block_without_escaping takes the sentinel value at +1. We will need
// to balance that +1.
// 2. The destroy_value associated with the copy_block_without_escaping will
// be on the copy_block value.
SILBuilderWithScope b(cb);
auto *newCB = b.createCopyBlock(loc, cb->getBlock());
cb->replaceAllUsesWith(newCB);
cb->eraseFromParent();
auto autoGenLoc = RegularLocation::getAutoGeneratedLocation();
// If CB is in the entry block, we know that our definition of SentinelClosure
// must be as well. Thus we know that we do not need to worry about loops or
// dominance issues and can just insert destroy_values for the sentinel at the
// lifetime end points.
if (newCB->getParent() == newCB->getFunction()->getEntryBlock()) {
// Our single destroy must not be in the entry block since if so, we would
// have inserted an edge to appease the SSA updater.
if (singleDestroy) {
SILBuilderWithScope b(std::next(singleDestroy->getIterator()));
SILValue v = sentinelClosure;
SILValue isEscaping = b.createIsEscapingClosure(
loc, v, IsEscapingClosureInst::ObjCEscaping);
b.createCondFail(loc, isEscaping, "non-escaping closure has escaped");
b.createDestroyValue(loc, v);
return true;
}
// If we couldn't find a specific destroy_value, lifetime extend to the end
// of the function.
SmallVector<SILBasicBlock *, 4> ExitingBlocks;
fn.findExitingBlocks(ExitingBlocks);
for (auto *Block : ExitingBlocks) {
SILBuilderWithScope B(Block->getTerminator());
SILValue V = sentinelClosure;
SILValue isEscaping = B.createIsEscapingClosure(
loc, V, IsEscapingClosureInst::ObjCEscaping);
B.createCondFail(loc, isEscaping, "non-escaping closure has escaped");
B.createDestroyValue(loc, V);
}
return true;
}
// Otherwise, we need to be more careful since we can have loops and may not
// transitively dominate all uses of the closure. So we:
//
// 1. Create an Optional<T>.none at the entry.
// 2. Create a destroy_value(val), val = Optional<T>.some(sentinel) in the cvt
// block.
// 3. Create a destroy_value at all exits of the value.
//
// and then use the SSAUpdater to ensure that we handle loops correctly.
auto optionalEscapingClosureTy =
SILType::getOptionalType(sentinelClosure->getType());
SmallVector<SILPhiArgument *, 8> insertedPhis;
SILSSAUpdater updater(&insertedPhis);
updater.initialize(optionalEscapingClosureTy, fn.hasOwnership()
? OwnershipKind::Owned
: OwnershipKind::None);
// Create the Optional.none as the beginning available value.
SILValue entryBlockOptionalNone;
{
SILBuilderWithScope b(fn.getEntryBlock()->begin());
entryBlockOptionalNone =
b.createOptionalNone(autoGenLoc, optionalEscapingClosureTy);
updater.addAvailableValue(fn.getEntryBlock(), entryBlockOptionalNone);
}
assert(entryBlockOptionalNone);
// Then create the Optional.some(closure sentinel).
//
// NOTE: We return the appropriate insertion point to insert the destroy_value
// before the value (to ensure we handle loops). We need to get all available
// values first though.
auto *initialValue = [&]() -> EnumInst * {
SILBuilderWithScope b(newCB);
// Create the closure sentinel (the copy_block_without_escaping closure
// operand consumed at +1, so we don't need a copy) to it.
auto *result = b.createOptionalSome(autoGenLoc, sentinelClosure,
optionalEscapingClosureTy);
updater.addAvailableValue(result->getParent(), result);
return result;
}();
// If we had a single destroy, creating a .none after it and add that as a
// value to the SSA updater.
if (singleDestroy) {
SILBuilderWithScope b(std::next(singleDestroy->getIterator()));
auto *result = b.createOptionalNone(autoGenLoc, optionalEscapingClosureTy);
updater.addAvailableValue(result->getParent(), result);
}
// Now that we have all of our available values, insert a destroy_value before
// the initial Optional.some value using the SSA updater to ensure that we
// handle loops correctly.
{
SILValue v = updater.getValueInMiddleOfBlock(initialValue->getParent());
SILBuilderWithScope(initialValue).createDestroyValue(autoGenLoc, v);
}
// And insert an is_escaping_closure, cond_fail, destroy_value at each of the
// lifetime end points. This ensures we do not expand our lifetime too much.
if (singleDestroy) {
SILBuilderWithScope b(std::next(singleDestroy->getIterator()));
SILValue v = updater.getValueInMiddleOfBlock(singleDestroy->getParent());
SILValue isEscaping =
b.createIsEscapingClosure(loc, v, IsEscapingClosureInst::ObjCEscaping);
b.createCondFail(loc, isEscaping, "non-escaping closure has escaped");
b.createDestroyValue(loc, v);
}
// Then to be careful with regards to loops, insert at each of the destroy
// blocks destroy_value to ensure that we obey ownership invariants.
{
SmallVector<SILBasicBlock *, 4> exitingBlocks;
findReachableExitBlocks(newCB, exitingBlocks);
for (auto *block : exitingBlocks) {
auto *safeDestructionPt = getDeinitSafeClosureDestructionPoint(block);
SILValue v = updater.getValueAtEndOfBlock(block);
SILBuilderWithScope(safeDestructionPt).createDestroyValue(autoGenLoc, v);
}
}
// Finally, we need to prune 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(entryBlockOptionalNone, insertedPhis);
return true;
}
static bool fixupClosureLifetimes(SILFunction &fn, bool &checkStackNesting,
bool &modifiedCFG) {
bool changed = false;
// tryExtendLifetimeToLastUse uses a cache of recursive instruction use
// queries.
llvm::DenseMap<SILInstruction *, SILInstruction *> memoizedQueries;
for (auto &block : fn) {
auto i = block.begin();
while (i != block.end()) {
SILInstruction *inst = &*i;
++i;
// Handle, copy_block_without_escaping instructions.
if (auto *cb = dyn_cast<CopyBlockWithoutEscapingInst>(inst)) {
if (fixupCopyBlockWithoutEscaping(cb, modifiedCFG)) {
changed = true;
}
continue;
}
// Otherwise, look at convert_escape_to_noescape [not_guaranteed]
// instructions.
auto *cvt = dyn_cast<ConvertEscapeToNoEscapeInst>(inst);
if (!cvt || cvt->isLifetimeGuaranteed())
continue;
// First try to peephole a known pattern.
if (!DisableConvertEscapeToNoEscapeSwitchEnumPeephole) {
if (trySwitchEnumPeephole(cvt)) {
changed = true;
continue;
}
}
if (tryExtendLifetimeToLastUse(cvt, memoizedQueries, i)) {
changed = true;
checkStackNesting = true;
continue;
}
// Otherwise, extend the lifetime of the operand to the end of the
// function.
extendLifetimeToEndOfFunction(fn, cvt);
changed = true;
}
}
return changed;
}
/// Fix-up the lifetime of the escaping closure argument of
/// convert_escape_to_noescape [not_guaranteed] instructions.
///
/// convert_escape_to_noescape [not_guaranteed] assume that someone guarantees
/// the lifetime of the operand for the duration of the trivial closure result.
/// SILGen does not guarantee this for '[not_guaranteed]' instructions so we
/// ensure it here.
namespace {
class ClosureLifetimeFixup : public SILFunctionTransform {
/// The entry point to the transformation.
void run() override {
// Don't rerun diagnostics on deserialized functions.
if (getFunction()->wasDeserializedCanonical())
return;
// Fixup convert_escape_to_noescape [not_guaranteed] and
// copy_block_without_escaping instructions.
bool checkStackNesting = false;