Skip to content

Commit 0a94845

Browse files
committed
Add findEnclosingDefs and findBorrowIntroducers utilities.
These APIs are essential for complete OSSA liveness analysis. The existing ad-hoc OSSA logic always misses some of the cases handled by these new utilities. We need to start replacing that ad-hoc logic with new utilities built on top of these APIs to define away potential latent bugs. Add FIXMEs to the inverse API: visitAdjacentBorrowsOfPhi. It should probably be redesigned in terms of these new APIs.
1 parent c2ae7e0 commit 0a94845

File tree

6 files changed

+420
-12
lines changed

6 files changed

+420
-12
lines changed

include/swift/SIL/OwnershipUtils.h

+19
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "swift/SIL/SILBasicBlock.h"
5151
#include "swift/SIL/SILInstruction.h"
5252
#include "swift/SIL/SILValue.h"
53+
#include "swift/SIL/StackList.h"
5354
#include "llvm/ADT/SmallPtrSet.h"
5455
#include "llvm/ADT/SmallVector.h"
5556

@@ -1319,6 +1320,24 @@ bool visitForwardedGuaranteedOperands(
13191320
bool visitAdjacentReborrowsOfPhi(SILPhiArgument *phi,
13201321
function_ref<bool(SILPhiArgument *)> visitor);
13211322

1323+
/// Visit each definition of a scope that immediately encloses a guaranteed
1324+
/// value. The guaranteed value effectively keeps these scopes alive.
1325+
///
1326+
/// This means something different depepending on whether \p value is itself a
1327+
/// borrow introducer vs. a forwarded guaranteed value. If \p value is an
1328+
/// introducer, then this discovers the enclosing borrow scope and visits all
1329+
/// introducers of that scope. If \p value is a forwarded value, then this
1330+
/// visits the introducers of the current borrow scope.
1331+
bool visitEnclosingDefs(SILValue value, function_ref<bool(SILValue)> visitor);
1332+
1333+
/// Visit the values that introduce the borrow scopes that includes \p
1334+
/// value. If value is owned, or introduces a borrow scope, then this only
1335+
/// visits \p value.
1336+
///
1337+
/// Returns false if the visitor returned false and exited early.
1338+
bool visitBorrowIntroducers(SILValue value,
1339+
function_ref<bool(SILValue)> visitor);
1340+
13221341
/// Given a begin of a borrow scope, visit all end_borrow users of the borrow or
13231342
/// its reborrows.
13241343
void visitTransitiveEndBorrows(

include/swift/SIL/SILArgument.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class SILArgument : public ValueBase {
205205
/// Precondition: this->isTerminatorResult()
206206
///
207207
/// TODO: Move this and other APIs into a TerminatorResult abstraction.
208-
const Operand *forwardedTerminatorResultOperand() const;
208+
Operand *forwardedTerminatorResultOperand() const;
209209

210210
/// Return the SILArgumentKind of this argument.
211211
SILArgumentKind getKind() const {

include/swift/SIL/SILInstruction.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -8500,9 +8500,14 @@ class TermInst : public NonValueInstruction {
85008500
/// example, a switch forwards ownership of the enum type into ownership of
85018501
/// the payload.
85028502
///
8503-
/// Postcondition: each successor has zero or one block arguments which
8504-
/// represents the forwaded result.
8503+
/// Postcondition: if the result is non-null, then each successor has zero or
8504+
/// one block arguments which represents the forwaded result.
85058505
const Operand *forwardedOperand() const;
8506+
8507+
Operand *forwardedOperand() {
8508+
return const_cast<Operand *>(
8509+
static_cast<const TermInst *>(this)->forwardedOperand());
8510+
}
85068511
};
85078512

85088513
// Forwards the first operand to a result in each successor block.

lib/SIL/Utils/MemAccessUtils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ SILValue swift::findOwnershipReferenceRoot(SILValue ref) {
862862
return ref;
863863
}
864864

865-
void swift::findGuaranteedReferenceRoots(SILValue value,
865+
void swift::findGuaranteedReferenceRoots(SILValue referenceValue,
866866
bool lookThroughNestedBorrows,
867867
SmallVectorImpl<SILValue> &roots) {
868868
ValueWorklist worklist(referenceValue->getFunction());

0 commit comments

Comments
 (0)