Skip to content

Commit 4779163

Browse files
committed
Change findOwnershipReferenceRoot to stop at phis.
This API should not handle is phis because the phi argument's ownedhip is independent from the phi itself. The client assumes that the returned root is in the same lifetime/scope of the access.
1 parent e9e3ce7 commit 4779163

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -732,12 +732,8 @@ namespace {
732732
// Essentially RC identity where the starting point is already a reference.
733733
class FindReferenceRoot {
734734
SmallPtrSet<SILPhiArgument *, 4> visitedPhis;
735-
bool preserveOwnership;
736735

737736
public:
738-
FindReferenceRoot(bool preserveOwnership)
739-
: preserveOwnership(preserveOwnership) {}
740-
741737
SILValue findRoot(SILValue ref) && {
742738
SILValue root = recursiveFindRoot(ref);
743739
assert(root && "all phi inputs must be reachable");
@@ -749,14 +745,8 @@ class FindReferenceRoot {
749745
SILValue recursiveFindRoot(SILValue ref) {
750746
while (auto *svi = dyn_cast<SingleValueInstruction>(ref)) {
751747
// If preserveOwnership is true, stop at the first owned root
752-
if (preserveOwnership) {
753-
if (!isIdentityAndOwnershipPreservingRefCast(svi)) {
754-
break;
755-
}
756-
} else {
757-
if (!isIdentityPreservingRefCast(svi)) {
758-
break;
759-
}
748+
if (!isIdentityPreservingRefCast(svi)) {
749+
break;
760750
}
761751
ref = svi->getOperand(0);
762752
};
@@ -792,11 +782,22 @@ class FindReferenceRoot {
792782
} // end anonymous namespace
793783

794784
SILValue swift::findReferenceRoot(SILValue ref) {
795-
return FindReferenceRoot(false /*preserveOwnership*/).findRoot(ref);
785+
return FindReferenceRoot().findRoot(ref);
796786
}
797787

788+
// This does not handle phis because a phis is either a consume or a
789+
// reborrow. In either case, the phi argument's ownership is independent from
790+
// the phi itself. The client assumes that the returned root is in the same
791+
// lifetime or borrow scope of the access.
798792
SILValue swift::findOwnershipReferenceRoot(SILValue ref) {
799-
return FindReferenceRoot(true /*preserveOwnership*/).findRoot(ref);
793+
while (auto *svi = dyn_cast<SingleValueInstruction>(ref)) {
794+
if (isIdentityAndOwnershipPreservingRefCast(svi)) {
795+
ref = svi->getOperand(0);
796+
continue;
797+
}
798+
break;
799+
}
800+
return ref;
800801
}
801802

802803
/// Find the first owned aggregate containing the reference, or simply the

0 commit comments

Comments
 (0)