Skip to content

Commit 23f8eef

Browse files
committed
Two small improvements on epilogue retain/release matcher.
1. Make sure to abort the data flow as soon as we know we cant find the epilogue retain/release. 2. Ignore retain in the throw block, because we do not use the result or insert retain for it in the throw block on caller side. This is a bug really, we have a test case for it in the functionsigopts.sil. It will be tested once this new epilogue retain matcher is wired up.
1 parent 4904101 commit 23f8eef

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

include/swift/SILOptimizer/Analysis/ARCAnalysis.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,16 @@ class EpilogueARCContext {
455455
/// The exit blocks of the function.
456456
llvm::SmallPtrSet<SILBasicBlock *, 2> ExitBlocks;
457457

458+
/// Return true if this is a function exitting block this epilogue ARC
459+
/// matcher is interested in.
460+
bool isInterestedFunctionExitingBlock(SILBasicBlock *BB) {
461+
if (EpilogueARCKind::Release == Kind)
462+
return BB->getTerminator()->isFunctionExiting();
463+
464+
return BB->getTerminator()->isFunctionExiting() &&
465+
BB->getTerminator()->getTermKind() != TermKind::ThrowInst;
466+
}
467+
458468
/// Return true if this is a function exit block.
459469
bool isExitBlock(SILBasicBlock *BB) {
460470
return ExitBlocks.count(BB);
@@ -489,7 +499,8 @@ class EpilogueARCContext {
489499
// Initialize the epilogue arc data flow context.
490500
initializeDataflow();
491501
// Converge the data flow.
492-
convergeDataflow();
502+
if (!convergeDataflow())
503+
return false;
493504
// Lastly, find the epilogue ARC instructions.
494505
return computeEpilogueARC();
495506
}
@@ -504,7 +515,7 @@ class EpilogueARCContext {
504515
void initializeDataflow();
505516

506517
/// Keep iterating until the data flow is converged.
507-
void convergeDataflow();
518+
bool convergeDataflow();
508519

509520
/// Find the epilogue ARC instructions.
510521
bool computeEpilogueARC();

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ SILInstruction *swift::findReleaseToMatchUnsafeGuaranteedValue(
11731173
void EpilogueARCContext::initializeDataflow() {
11741174
for (auto &B : *F) {
11751175
// Find the exit blocks.
1176-
if (B.getTerminator()->isFunctionExiting()) {
1176+
if (isInterestedFunctionExitingBlock(&B)) {
11771177
ExitBlocks.insert(&B);
11781178
}
11791179
// Allocate the storage.
@@ -1186,11 +1186,13 @@ void EpilogueARCContext::initializeDataflow() {
11861186
llvm::DenseSet<SILValue> Processed;
11871187
ToProcess.push_back(Arg);
11881188
while (!ToProcess.empty()) {
1189-
SILValue Arg = ToProcess.pop_back_val();
1190-
if (Processed.find(Arg) != Processed.end())
1189+
SILValue CArg = ToProcess.pop_back_val();
1190+
if (!CArg)
1191+
continue;
1192+
if (Processed.find(CArg) != Processed.end())
11911193
continue;
1192-
Processed.insert(Arg);
1193-
SILArgument *A = dyn_cast<SILArgument>(Arg);
1194+
Processed.insert(CArg);
1195+
SILArgument *A = dyn_cast<SILArgument>(CArg);
11941196
if (A && !A->isFunctionArg()) {
11951197
// Find predecessor and break the SILArgument to predecessors.
11961198
for (auto X : A->getParent()->getPreds()) {
@@ -1204,7 +1206,7 @@ void EpilogueARCContext::initializeDataflow() {
12041206
}
12051207
}
12061208

1207-
void EpilogueARCContext::convergeDataflow() {
1209+
bool EpilogueARCContext::convergeDataflow() {
12081210
// Keep iterating until Changed is false.
12091211
bool Changed = false;
12101212
do {
@@ -1238,9 +1240,10 @@ void EpilogueARCContext::convergeDataflow() {
12381240
break;
12391241
}
12401242
// This is a transition from 1 to 0 due to a blocking instruction.
1243+
// at this point, its OK to abort the data flow as we have one path
1244+
// which we did not find an epilogue retain before getting blocked.
12411245
if (mayBlockEpilogueARC(&*I, RCFI->getRCIdentityRoot(Arg))) {
1242-
BBSetOut = false;
1243-
break;
1246+
return false;
12441247
}
12451248
}
12461249
}
@@ -1250,6 +1253,7 @@ void EpilogueARCContext::convergeDataflow() {
12501253
BS->BBSetIn = BBSetOut;
12511254
}
12521255
} while(Changed);
1256+
return true;
12531257
}
12541258

12551259
bool EpilogueARCContext::computeEpilogueARC() {

0 commit comments

Comments
 (0)