Skip to content

Commit ed6f701

Browse files
authored
Merge pull request swiftlang#39663 from atrick/fix-addressuses
Fix OSSA handling of address uses
2 parents 15ac7dd + b4a47b6 commit ed6f701

File tree

7 files changed

+204
-98
lines changed

7 files changed

+204
-98
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ inline bool isForwardingConsume(SILValue value) {
7777
return canOpcodeForwardOwnedValues(value);
7878
}
7979

80-
/// Find all "use points" of \p guaranteedValue that determine its lifetime
81-
/// requirement.
80+
/// Find leaf "use points" of \p guaranteedValue that determine its lifetime
81+
/// requirement. If \p usePoints is nullptr, then the simply returns true if no
82+
/// PointerEscape use was found.
8283
///
8384
/// Precondition: \p guaranteedValue is not a BorrowedValue.
8485
///
@@ -99,10 +100,10 @@ inline bool isForwardingConsume(SILValue value) {
99100
/// When this is called on a value that does not introduce a new scope, none of
100101
/// the use points can be EndBorrows or Reborrows. Those uses are only allowed
101102
/// on borrow-introducing values.
102-
bool findInnerTransitiveGuaranteedUses(SILValue guaranteedValue,
103-
SmallVectorImpl<Operand *> &usePoints);
103+
bool findInnerTransitiveGuaranteedUses(
104+
SILValue guaranteedValue, SmallVectorImpl<Operand *> *usePoints = nullptr);
104105

105-
/// Find all "use points" of a guaranteed value within its enclosing borrow
106+
/// Find leaf "use points" of a guaranteed value within its enclosing borrow
106107
/// scope (without looking through reborrows). To find the use points of the
107108
/// extended borrow scope, after looking through reborrows, use
108109
/// findExtendedTransitiveGuaranteedUses() instead.
@@ -662,12 +663,19 @@ bool getAllBorrowIntroducingValues(SILValue value,
662663
/// introducer, then we return a .some(BorrowScopeIntroducingValue).
663664
BorrowedValue getSingleBorrowIntroducingValue(SILValue inputValue);
664665

666+
enum class AddressUseKind { NonEscaping, PointerEscape, Unknown };
667+
668+
inline AddressUseKind meet(AddressUseKind lhs, AddressUseKind rhs) {
669+
return (lhs > rhs) ? lhs : rhs;
670+
}
671+
665672
/// The algorithm that is used to determine what the verifier will consider to
666673
/// be transitive uses of the given address. Used to implement \see
667674
/// findTransitiveUses.
668-
bool findTransitiveUsesForAddress(
669-
SILValue address, SmallVectorImpl<Operand *> &foundUses,
670-
std::function<void(Operand *)> *onError = nullptr);
675+
AddressUseKind
676+
findTransitiveUsesForAddress(SILValue address,
677+
SmallVectorImpl<Operand *> *foundUses = nullptr,
678+
std::function<void(Operand *)> *onError = nullptr);
671679

672680
class InteriorPointerOperandKind {
673681
public:
@@ -834,15 +842,19 @@ struct InteriorPointerOperand {
834842
llvm_unreachable("Covered switch isn't covered?!");
835843
}
836844

837-
/// Transitively compute the list of uses that this interior pointer operand
838-
/// puts on its parent guaranted value.
845+
/// Transitively compute the list of leaf uses that this interior pointer
846+
/// operand puts on its parent guaranted value.
847+
///
848+
/// If \p foundUses is nullptr, this simply returns true if no PointerEscapes
849+
/// were found.
839850
///
840851
/// Example: Uses of a ref_element_addr can not occur outside of the lifetime
841852
/// of the instruction's operand. The uses of that address act as liveness
842853
/// requirements to ensure that the underlying class is alive at all use
843854
/// points.
844-
bool findTransitiveUses(SmallVectorImpl<Operand *> &foundUses,
845-
std::function<void(Operand *)> *onError = nullptr) {
855+
AddressUseKind
856+
findTransitiveUses(SmallVectorImpl<Operand *> *foundUses = nullptr,
857+
std::function<void(Operand *)> *onError = nullptr) {
846858
return findTransitiveUsesForAddress(getProjectedAddress(), foundUses,
847859
onError);
848860
}
@@ -912,8 +924,8 @@ struct AddressOwnership {
912924
}
913925

914926
/// Transitively compute uses of this base address.
915-
bool findTransitiveUses(SmallVectorImpl<Operand *> &foundUses) {
916-
return findTransitiveUsesForAddress(base.getBaseAddress(), foundUses);
927+
AddressUseKind findTransitiveUses(SmallVectorImpl<Operand *> &foundUses) {
928+
return findTransitiveUsesForAddress(base.getBaseAddress(), &foundUses);
917929
}
918930

919931
/// Return true of all \p uses occur before the end of the address' lifetime

include/swift/SIL/SILValue.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,12 @@ class Operand {
10581058
/// Returns true if this ends the lifetime of an owned operand.
10591059
bool isConsuming() const;
10601060

1061+
bool endsLocalBorrowScope() const {
1062+
auto ownership = getOperandOwnership();
1063+
return ownership == OperandOwnership::EndBorrow
1064+
|| ownership == OperandOwnership::Reborrow;
1065+
}
1066+
10611067
SILBasicBlock *getParentBlock() const;
10621068
SILFunction *getParentFunction() const;
10631069

0 commit comments

Comments
 (0)