Skip to content

Commit 0161f43

Browse files
authored
Merge pull request swiftlang#27904 from gottesmm/pr-624292f32572e0362c0566deebe886758e730510
[sil] Refactor out BeginApplyInst::getCoroutineEndPoints() impl from Inliner into method on the class itself.
2 parents cea89e7 + 750c4cc commit 0161f43

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,9 @@ class BeginApplyResult final : public MultipleValueInstructionResult {
22262226
}
22272227
};
22282228

2229+
class EndApplyInst;
2230+
class AbortApplyInst;
2231+
22292232
/// BeginApplyInst - Represents the beginning of the full application of
22302233
/// a yield_once coroutine (up until the coroutine yields a value back).
22312234
class BeginApplyInst final
@@ -2280,6 +2283,13 @@ class BeginApplyInst final
22802283
bool isNonThrowing() const {
22812284
return isNonThrowingApply();
22822285
}
2286+
2287+
void getCoroutineEndPoints(
2288+
SmallVectorImpl<EndApplyInst *> &endApplyInsts,
2289+
SmallVectorImpl<AbortApplyInst *> &abortApplyInsts) const;
2290+
2291+
void getCoroutineEndPoints(SmallVectorImpl<Operand *> &endApplyInsts,
2292+
SmallVectorImpl<Operand *> &abortApplyInsts) const;
22832293
};
22842294

22852295
inline BeginApplyInst *BeginApplyResult::getParent() {

lib/SIL/SILInstructions.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,35 @@ BeginApplyInst::create(SILDebugLocation loc, SILValue callee,
499499
isNonThrowing, specializationInfo);
500500
}
501501

502+
void BeginApplyInst::getCoroutineEndPoints(
503+
SmallVectorImpl<EndApplyInst *> &endApplyInsts,
504+
SmallVectorImpl<AbortApplyInst *> &abortApplyInsts) const {
505+
for (auto *tokenUse : getTokenResult()->getUses()) {
506+
auto *user = tokenUse->getUser();
507+
if (auto *end = dyn_cast<EndApplyInst>(user)) {
508+
endApplyInsts.push_back(end);
509+
continue;
510+
}
511+
512+
abortApplyInsts.push_back(cast<AbortApplyInst>(user));
513+
}
514+
}
515+
516+
void BeginApplyInst::getCoroutineEndPoints(
517+
SmallVectorImpl<Operand *> &endApplyInsts,
518+
SmallVectorImpl<Operand *> &abortApplyInsts) const {
519+
for (auto *tokenUse : getTokenResult()->getUses()) {
520+
auto *user = tokenUse->getUser();
521+
if (isa<EndApplyInst>(user)) {
522+
endApplyInsts.push_back(tokenUse);
523+
continue;
524+
}
525+
526+
assert(isa<AbortApplyInst>(user));
527+
abortApplyInsts.push_back(tokenUse);
528+
}
529+
}
530+
502531
bool swift::doesApplyCalleeHaveSemantics(SILValue callee, StringRef semantics) {
503532
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(callee))
504533
if (auto *F = FRI->getReferencedFunctionOrNull())

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ bool SILInliner::canInlineApplySite(FullApplySite apply) {
7272
return true;
7373
}
7474

75-
namespace swift {
75+
namespace {
76+
7677
/// Utility class for rewiring control-flow of inlined begin_apply functions.
7778
class BeginApplySite {
7879
SILLocation Loc;
@@ -102,14 +103,15 @@ class BeginApplySite {
102103
}
103104

104105
void preprocess(SILBasicBlock *returnToBB) {
105-
// Get the end_apply, abort_apply instructions.
106-
auto Token = BeginApply->getTokenResult();
107-
for (auto *TokenUse : Token->getUses()) {
108-
if (auto End = dyn_cast<EndApplyInst>(TokenUse->getUser())) {
109-
collectEndApply(End);
110-
} else {
111-
collectAbortApply(cast<AbortApplyInst>(TokenUse->getUser()));
112-
}
106+
SmallVector<EndApplyInst *, 1> endApplyInsts;
107+
SmallVector<AbortApplyInst *, 1> abortApplyInsts;
108+
BeginApply->getCoroutineEndPoints(endApplyInsts, abortApplyInsts);
109+
while (!endApplyInsts.empty()) {
110+
auto *endApply = endApplyInsts.pop_back_val();
111+
collectEndApply(endApply);
112+
}
113+
while (!abortApplyInsts.empty()) {
114+
collectAbortApply(abortApplyInsts.pop_back_val());
113115
}
114116
}
115117

@@ -228,7 +230,8 @@ class BeginApplySite {
228230
assert(!BeginApply->hasUsesOfAnyResult());
229231
}
230232
};
231-
} // namespace swift
233+
234+
} // end anonymous namespace
232235

233236
namespace swift {
234237
class SILInlineCloner

0 commit comments

Comments
 (0)