Skip to content

Commit d91a675

Browse files
authored
Merge pull request #28760 from gottesmm/pr-7d9b6a2372abe97c9eeed1d16b19b29f3c488a1d
2 parents a6f5c57 + 9efb49a commit d91a675

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

include/swift/SIL/ApplySite.h

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -398,24 +398,6 @@ class ApplySite {
398398
}
399399
}
400400

401-
/// If this is a terminator apply site, then pass the first instruction of
402-
/// each successor to fun. Otherwise, pass std::next(Inst).
403-
///
404-
/// The intention is that this abstraction will enable the compiler writer to
405-
/// ignore whether or not an apply site is a terminator when inserting
406-
/// instructions after an apply site. This results in eliminating unnecessary
407-
/// if-else code otherwise required to handle such situations.
408-
void insertAfter(llvm::function_ref<void(SILBasicBlock::iterator)> func) {
409-
auto *ti = dyn_cast<TermInst>(Inst);
410-
if (!ti) {
411-
return func(std::next(Inst->getIterator()));
412-
}
413-
414-
for (auto *succBlock : ti->getSuccessorBlocks()) {
415-
func(succBlock->begin());
416-
}
417-
}
418-
419401
static ApplySite getFromOpaqueValue(void *p) { return ApplySite(p); }
420402

421403
friend bool operator==(ApplySite lhs, ApplySite rhs) {
@@ -533,6 +515,63 @@ class FullApplySite : public ApplySite {
533515
return getCalleeArgIndex(op) < getNumIndirectSILResults();
534516
}
535517

518+
/// If this is a terminator apply site, then pass the first instruction of
519+
/// each successor to fun. Otherwise, pass std::next(Inst).
520+
///
521+
/// The intention is that this abstraction will enable the compiler writer to
522+
/// ignore whether or not an apply site is a terminator when inserting
523+
/// instructions after an apply site. This results in eliminating unnecessary
524+
/// if-else code otherwise required to handle such situations.
525+
///
526+
/// NOTE: We return std::next() for begin_apply. If one wishes to insert code
527+
/// /after/ the end_apply/abort_apply, please use instead
528+
/// insertAfterFullEvaluation.
529+
void insertAfterInvocation(
530+
function_ref<void(SILBasicBlock::iterator)> func) const {
531+
switch (getKind()) {
532+
case FullApplySiteKind::ApplyInst:
533+
case FullApplySiteKind::BeginApplyInst:
534+
return func(std::next(getInstruction()->getIterator()));
535+
case FullApplySiteKind::TryApplyInst:
536+
for (auto *succBlock :
537+
cast<TermInst>(getInstruction())->getSuccessorBlocks()) {
538+
func(succBlock->begin());
539+
}
540+
return;
541+
}
542+
llvm_unreachable("Covered switch isn't covered");
543+
}
544+
545+
/// Pass to func insertion points that are guaranteed to be immediately after
546+
/// this full apply site has completely finished executing.
547+
///
548+
/// This is just like insertAfterInvocation except that if the full apply site
549+
/// is a begin_apply, we pass the insertion points after the end_apply,
550+
/// abort_apply rather than an insertion point right after the
551+
/// begin_apply. For such functionality, please invoke insertAfterInvocation.
552+
void insertAfterFullEvaluation(
553+
function_ref<void(SILBasicBlock::iterator)> func) const {
554+
switch (getKind()) {
555+
case FullApplySiteKind::ApplyInst:
556+
case FullApplySiteKind::TryApplyInst:
557+
return insertAfterInvocation(func);
558+
case FullApplySiteKind::BeginApplyInst:
559+
SmallVector<EndApplyInst *, 2> endApplies;
560+
SmallVector<AbortApplyInst *, 2> abortApplies;
561+
auto *bai = cast<BeginApplyInst>(getInstruction());
562+
bai->getCoroutineEndPoints(endApplies, abortApplies);
563+
for (auto *eai : endApplies) {
564+
func(std::next(eai->getIterator()));
565+
}
566+
for (auto *aai : abortApplies) {
567+
func(std::next(aai->getIterator()));
568+
}
569+
return;
570+
}
571+
572+
llvm_unreachable("covered switch isn't covered");
573+
}
574+
536575
static FullApplySite getFromOpaqueValue(void *p) { return FullApplySite(p); }
537576

538577
static bool classof(const SILInstruction *inst) {

lib/SILOptimizer/Mandatory/MandatoryInlining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static void fixupReferenceCounts(
159159
// insert a destroy after the apply since the leak will just cover the
160160
// other path.
161161
if (!error.getFoundOverConsume()) {
162-
applySite.insertAfter([&](SILBasicBlock::iterator iter) {
162+
applySite.insertAfterInvocation([&](SILBasicBlock::iterator iter) {
163163
if (hasOwnership) {
164164
SILBuilderWithScope(iter).createEndBorrow(loc, argument);
165165
}
@@ -199,7 +199,7 @@ static void fixupReferenceCounts(
199199
}
200200
}
201201

202-
applySite.insertAfter([&](SILBasicBlock::iterator iter) {
202+
applySite.insertAfterInvocation([&](SILBasicBlock::iterator iter) {
203203
SILBuilderWithScope(iter).emitDestroyValueOperation(loc, v);
204204
});
205205
break;
@@ -246,7 +246,7 @@ static void fixupReferenceCounts(
246246
// Destroy the callee as the apply would have done if our function is not
247247
// callee guaranteed.
248248
if (!isCalleeGuaranteed) {
249-
applySite.insertAfter([&](SILBasicBlock::iterator iter) {
249+
applySite.insertAfterInvocation([&](SILBasicBlock::iterator iter) {
250250
SILBuilderWithScope(iter).emitDestroyValueOperation(loc, calleeValue);
251251
});
252252
}

0 commit comments

Comments
 (0)