Skip to content

Commit 15ac7dd

Browse files
authored
Merge pull request swiftlang#39679 from meg-gupta/escapeanossa
Improve OSSA instructions handling in EscapeAnalysis
2 parents 4424696 + 52e88c7 commit 15ac7dd

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/SILOptimizer/Analysis/EscapeAnalysis.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,12 +2097,13 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
20972097
}
20982098
}
20992099

2100-
if (isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I)) {
2100+
if (isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I) ||
2101+
isa<DestroyValueInst>(I)) {
21012102
// Treat the release instruction as if it is the invocation
21022103
// of a deinit function.
21032104
if (RecursionDepth < MaxRecursionDepth) {
21042105
// Check if the destructor is known.
2105-
auto OpV = cast<RefCountingInst>(I)->getOperand(0);
2106+
auto OpV = I->getOperand(0);
21062107
if (buildConnectionGraphForDestructor(OpV, I, FInfo, BottomUpOrder,
21072108
RecursionDepth))
21082109
return;
@@ -2157,10 +2158,21 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
21572158
return isPointer(result);
21582159
}));
21592160
return;
2160-
2161+
case SILInstructionKind::BeginBorrowInst:
2162+
case SILInstructionKind::CopyValueInst: {
2163+
auto svi = cast<SingleValueInstruction>(I);
2164+
CGNode *resultNode = ConGraph->getNode(svi);
2165+
if (CGNode *opNode = ConGraph->getNode(svi->getOperand(0))) {
2166+
ConGraph->defer(resultNode, opNode);
2167+
return;
2168+
}
2169+
ConGraph->setEscapesGlobal(svi);
2170+
return;
2171+
}
21612172
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
21622173
case SILInstructionKind::Name##ReleaseInst:
21632174
#include "swift/AST/ReferenceStorage.def"
2175+
case SILInstructionKind::DestroyValueInst:
21642176
case SILInstructionKind::StrongReleaseInst:
21652177
case SILInstructionKind::ReleaseValueInst: {
21662178
// A release instruction may deallocate the pointer operand. This may

test/SILOptimizer/escape_analysis.sil

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,3 +1981,37 @@ bb0 (%0 : $*X):
19811981
return %2 : $()
19821982
}
19831983

1984+
// CHECK-LABEL: CG of test_localval_ossarefcountinsts
1985+
// CHECK: Val [ref] %0 Esc: , Succ: (%0.1)
1986+
// CHECK: Con [int] %0.1 Esc: %2, Succ: (%0.2)
1987+
// CHECK: Con [ref] %0.2 Esc: %2, Succ:
1988+
// CHECK: Val [ref] %1 Esc: , Succ: %0
1989+
// CHECK: End
1990+
// CHECK: MayEscape: %0 = alloc_ref $X
1991+
// CHECK: to destroy_value %1 : $X
1992+
// CHECK: MayEscape: %1 = copy_value %0 : $X
1993+
// CHECK: to destroy_value %1 : $X
1994+
sil [ossa] @test_localval_ossarefcountinsts : $@convention(thin) () -> () {
1995+
bb0:
1996+
%o1 = alloc_ref $X
1997+
%copy = copy_value %o1 : $X
1998+
destroy_value %copy : $X
1999+
dealloc_ref %o1 : $X
2000+
%2 = tuple()
2001+
return %2 : $()
2002+
}
2003+
2004+
// CHECK-LABEL: CG of test_localval_borrowinsts
2005+
// CHECK: Val [ref] %0 Esc: , Succ:
2006+
// CHECK: Val [ref] %1 Esc: , Succ: %0
2007+
// CHECK: End
2008+
sil [ossa] @test_localval_borrowinsts : $@convention(thin) () -> () {
2009+
bb0:
2010+
%o1 = alloc_ref $X
2011+
%borrow = begin_borrow %o1 : $X
2012+
end_borrow %borrow : $X
2013+
dealloc_ref %o1 : $X
2014+
%2 = tuple()
2015+
return %2 : $()
2016+
}
2017+

0 commit comments

Comments
 (0)