Skip to content

Commit b43654a

Browse files
authored
Merge pull request swiftlang#18991 from rjmccall/loop-can-duplicate-begin-apply
Update SILLoop::canDuplicate to correctly handle begin_apply
2 parents f5b46fb + 3a04468 commit b43654a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/SIL/LoopInfo.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ bool SILLoop::canDuplicate(SILInstruction *I) const {
7878
return false;
7979
}
8080

81+
// The entire coroutine execution must be within the loop.
82+
// Note that we don't have to worry about the reverse --- a loop which
83+
// contains an end_apply or abort_apply of an external begin_apply ---
84+
// because that wouldn't be structurally valid in the first place.
85+
if (auto BAI = dyn_cast<BeginApplyInst>(I)) {
86+
for (auto UI : BAI->getTokenResult()->getUses()) {
87+
auto User = UI->getUser();
88+
assert(isa<EndApplyInst>(User) || isa<AbortApplyInst>(User));
89+
if (!contains(User))
90+
return false;
91+
}
92+
return true;
93+
}
94+
8195
if (isa<ThrowInst>(I))
8296
return false;
8397

lib/SIL/SILInstruction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,8 @@ bool SILInstruction::isTriviallyDuplicatable() const {
11781178
if (isa<BeginApplyInst>(this))
11791179
return false;
11801180

1181+
// If you add more cases here, you should also update SILLoop:canDuplicate.
1182+
11811183
return true;
11821184
}
11831185

0 commit comments

Comments
 (0)