Skip to content

Commit f713587

Browse files
committed
Fix OSSA support for mark_dependence [nonescaping]
Handle escapes to function results.
1 parent b3a7ec1 commit f713587

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OwnershipLiveness.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ extension OwnershipUseVisitor {
307307
}
308308
// Otherwise, directly visit the scope ending uses.
309309
//
310-
// TODO: change visitScopeEndingOperands to take a non-escaping
311-
// closure and call ownershipLeafUse directly.
310+
// TODO: remove this stack by changign visitScopeEndingOperands to
311+
// take a non-escaping closure that can call ownershipLeafUse.
312312
var stack = Stack<Operand>(context)
313313
defer { stack.deinitialize() }
314314
_ = borrowInst.visitScopeEndingOperands(context) {
@@ -448,9 +448,6 @@ extension OwnershipUseVisitor {
448448
/// MoveValueInst, and Allocation. Then this visitor should assert
449449
/// that the forward-extended lifetime introducer has no pointer
450450
/// escaping uses.
451-
///
452-
/// TODO: Change the operandOwnership of MarkDependenceInst base operand.
453-
/// It should be a borrowing operand, not a pointer escape.
454451
struct InteriorUseWalker {
455452
let functionContext: FunctionPassContext
456453
var context: Context { functionContext }

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,16 @@ OperandOwnershipClassifier::visitMarkDependenceInst(MarkDependenceInst *mdi) {
658658
return getOwnershipKind().getForwardingOperandOwnership(
659659
/*allowUnowned*/true);
660660
}
661-
if (getOperandIndex() == MarkDependenceInst::Base && mdi->isNonEscaping()) {
661+
if (mdi->isNonEscaping()) {
662662
// This creates a "dependent value", just like on-stack partial_apply, which
663663
// we treat like a borrow.
664664
return OperandOwnership::Borrow;
665665
}
666+
if (mdi->hasUnresolvedEscape()) {
667+
// This creates a dependent value that may extend beyond the parent's
668+
// lifetime.
669+
return OperandOwnership::UnownedInstantaneousUse;
670+
}
666671
// FIXME: Add an end_dependence instruction so we can treat mark_dependence as
667672
// a borrow of the base (mark_dependence %base -> end_dependence is analogous
668673
// to a borrow scope).

lib/SIL/IR/SILInstruction.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,15 @@ static bool visitRecursivelyLifetimeEndingUses(
18051805
}
18061806
continue;
18071807
}
1808+
if (auto *ret = dyn_cast<ReturnInst>(use->getUser())) {
1809+
auto fnTy = ret->getFunction()->getLoweredFunctionType();
1810+
assert(!fnTy->getLifetimeDependenceInfo().empty());
1811+
if (!func(use)) {
1812+
return false;
1813+
}
1814+
continue;
1815+
}
1816+
// FIXME: Handle store to indirect result
18081817

18091818
// There shouldn't be any dead-end consumptions of a nonescaping
18101819
// partial_apply that don't forward it along, aside from destroy_value.

0 commit comments

Comments
 (0)