Skip to content

Commit 4f1de83

Browse files
[Clang][NFC][Coroutine] Do not leave dangling pointers after removing CoroBegin (llvm#98546)
1 parent dcf6b7a commit 4f1de83

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ static void handleNoSuspendCoroutine(coro::Shape &Shape) {
12481248
}
12491249

12501250
CoroBegin->eraseFromParent();
1251+
Shape.CoroBegin = nullptr;
12511252
}
12521253

12531254
// SimplifySuspendPoint needs to check that there is no calls between
@@ -1970,9 +1971,17 @@ splitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
19701971
}
19711972

19721973
/// Remove calls to llvm.coro.end in the original function.
1973-
static void removeCoroEnds(const coro::Shape &Shape) {
1974-
for (auto *End : Shape.CoroEnds) {
1975-
replaceCoroEnd(End, Shape, Shape.FramePtr, /*in resume*/ false, nullptr);
1974+
static void removeCoroEndsFromRampFunction(const coro::Shape &Shape) {
1975+
if (Shape.ABI != coro::ABI::Switch) {
1976+
for (auto *End : Shape.CoroEnds) {
1977+
replaceCoroEnd(End, Shape, Shape.FramePtr, /*in resume*/ false, nullptr);
1978+
}
1979+
} else {
1980+
for (llvm::AnyCoroEndInst *End : Shape.CoroEnds) {
1981+
auto &Context = End->getContext();
1982+
End->replaceAllUsesWith(ConstantInt::getFalse(Context));
1983+
End->eraseFromParent();
1984+
}
19761985
}
19771986
}
19781987

@@ -1981,18 +1990,6 @@ static void updateCallGraphAfterCoroutineSplit(
19811990
const SmallVectorImpl<Function *> &Clones, LazyCallGraph::SCC &C,
19821991
LazyCallGraph &CG, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR,
19831992
FunctionAnalysisManager &FAM) {
1984-
if (!Shape.CoroBegin)
1985-
return;
1986-
1987-
if (Shape.ABI != coro::ABI::Switch)
1988-
removeCoroEnds(Shape);
1989-
else {
1990-
for (llvm::AnyCoroEndInst *End : Shape.CoroEnds) {
1991-
auto &Context = End->getContext();
1992-
End->replaceAllUsesWith(ConstantInt::getFalse(Context));
1993-
End->eraseFromParent();
1994-
}
1995-
}
19961993

19971994
if (!Clones.empty()) {
19981995
switch (Shape.ABI) {
@@ -2120,6 +2117,7 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
21202117
const coro::Shape Shape =
21212118
splitCoroutine(F, Clones, FAM.getResult<TargetIRAnalysis>(F),
21222119
OptimizeFrame, MaterializableCallback);
2120+
removeCoroEndsFromRampFunction(Shape);
21232121
updateCallGraphAfterCoroutineSplit(*N, Shape, Clones, C, CG, AM, UR, FAM);
21242122

21252123
ORE.emit([&]() {

0 commit comments

Comments
 (0)