21
21
#include " swift/SIL/BasicBlockDatastructures.h"
22
22
#include " swift/SILOptimizer/PassManager/Passes.h"
23
23
#include " swift/SILOptimizer/PassManager/Transforms.h"
24
+ #include " swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
24
25
#include " swift/SILOptimizer/Utils/CFGOptUtils.h"
25
26
#include " swift/SILOptimizer/Utils/InstOptUtils.h"
26
27
#include " swift/SILOptimizer/Utils/SILSSAUpdater.h"
@@ -477,6 +478,7 @@ static SILValue tryRewriteToPartialApplyStack(
477
478
ConvertEscapeToNoEscapeInst *cvt, SILInstruction *closureUser,
478
479
DominanceAnalysis *dominanceAnalysis, InstructionDeleter &deleter,
479
480
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
481
+ llvm::DenseSet<SILBasicBlock *> &unreachableBlocks,
480
482
const bool &modifiedCFG) {
481
483
482
484
auto *origPA = dyn_cast<PartialApplyInst>(skipConvert (cvt->getOperand ()));
@@ -583,6 +585,13 @@ static SILValue tryRewriteToPartialApplyStack(
583
585
dominanceAnalysis->invalidate (closureUser->getFunction (),
584
586
analysisInvalidationKind (modifiedCFG));
585
587
// Insert dealloc_stacks of any in_guaranteed captures.
588
+
589
+ // Don't run insertDeallocOfCapturedArguments if newPA is in an unreachable
590
+ // block insertDeallocOfCapturedArguments will run code that computes the DF
591
+ // for newPA that will loop infinetly.
592
+ if (unreachableBlocks.count (newPA->getParent ()))
593
+ return closure;
594
+
586
595
insertDeallocOfCapturedArguments (
587
596
newPA, dominanceAnalysis->get (closureUser->getFunction ()));
588
597
return closure;
@@ -591,6 +600,7 @@ static SILValue tryRewriteToPartialApplyStack(
591
600
static bool tryExtendLifetimeToLastUse (
592
601
ConvertEscapeToNoEscapeInst *cvt, DominanceAnalysis *dominanceAnalysis,
593
602
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
603
+ llvm::DenseSet<SILBasicBlock *> &unreachableBlocks,
594
604
InstructionDeleter &deleter, const bool &modifiedCFG) {
595
605
// If there is a single user that is an apply this is simple: extend the
596
606
// lifetime of the operand until after the apply.
@@ -614,7 +624,7 @@ static bool tryExtendLifetimeToLastUse(
614
624
615
625
if (SILValue closure = tryRewriteToPartialApplyStack (
616
626
cvt, singleUser, dominanceAnalysis, deleter, memoized,
617
- /* const*/ modifiedCFG)) {
627
+ unreachableBlocks, /* const*/ modifiedCFG)) {
618
628
if (auto *cfi = dyn_cast<ConvertFunctionInst>(closure))
619
629
closure = cfi->getOperand ();
620
630
if (endAsyncLet && isa<MarkDependenceInst>(closure)) {
@@ -1009,6 +1019,22 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,
1009
1019
return true ;
1010
1020
}
1011
1021
1022
+ static void computeUnreachableBlocks (
1023
+ llvm::DenseSet<SILBasicBlock*> &unreachableBlocks,
1024
+ SILFunction &fn) {
1025
+
1026
+ ReachableBlocks isReachable (&fn);
1027
+ llvm::DenseSet<SILBasicBlock *> reachable;
1028
+ isReachable.visit ([&] (SILBasicBlock *block) -> bool {
1029
+ reachable.insert (block);
1030
+ return true ;
1031
+ });
1032
+ for (auto &block : fn) {
1033
+ if (!reachable.count (&block))
1034
+ unreachableBlocks.insert (&block);
1035
+ }
1036
+ }
1037
+
1012
1038
static bool fixupClosureLifetimes (SILFunction &fn,
1013
1039
DominanceAnalysis *dominanceAnalysis,
1014
1040
bool &checkStackNesting, bool &modifiedCFG) {
@@ -1018,6 +1044,9 @@ static bool fixupClosureLifetimes(SILFunction &fn,
1018
1044
// queries.
1019
1045
llvm::DenseMap<SILInstruction *, SILInstruction *> memoizedQueries;
1020
1046
1047
+ llvm::DenseSet<SILBasicBlock *> unreachableBlocks;
1048
+ computeUnreachableBlocks (unreachableBlocks, fn);
1049
+
1021
1050
for (auto &block : fn) {
1022
1051
SILSSAUpdater updater;
1023
1052
@@ -1045,7 +1074,7 @@ static bool fixupClosureLifetimes(SILFunction &fn,
1045
1074
}
1046
1075
1047
1076
if (tryExtendLifetimeToLastUse (cvt, dominanceAnalysis, memoizedQueries,
1048
- updater.getDeleter (),
1077
+ unreachableBlocks, updater.getDeleter (),
1049
1078
/* const*/ modifiedCFG)) {
1050
1079
changed = true ;
1051
1080
checkStackNesting = true ;
0 commit comments