Skip to content

Commit 71b508e

Browse files
committed
Refactor compatibleOwnershipKind, trivialOrCompatibleOwnershipKinds onto ValueOwnershipKind instead of being free functions.
I have a bunch of work that I have done on a side branch some time ago to split determining what value ownership kinds are able to be associated with a specific operand. This PR just upstreams a small part of the larger change.
1 parent 0ca90b8 commit 71b508e

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

include/swift/SIL/SILValue.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ struct ValueOwnershipKind {
146146
/// ownership kind otherwise.
147147
ValueOwnershipKind getProjectedOwnershipKind(SILModule &M,
148148
SILType Proj) const;
149+
150+
/// Returns true if \p Other can be merged successfully with this, implying
151+
/// that the two ownership kinds are "compatibile".
152+
///
153+
/// The reason why we do not compare directy is to allow for
154+
/// ValueOwnershipKind::Any to merge into other forms of ValueOwnershipKind.
155+
bool isCompatibleWith(ValueOwnershipKind other) const {
156+
return merge(other).hasValue();
157+
}
158+
159+
/// Returns true if \p Other is compatible with ValueOwnershipKind::Trivial or
160+
/// this. See isCompatibleWith for more information on what "compatibility"
161+
/// means.
162+
bool isTrivialOrCompatibleWith(ValueOwnershipKind other) const {
163+
return isCompatibleWith(ValueOwnershipKind::Trivial) ||
164+
isCompatibleWith(other);
165+
}
149166
};
150167

151168
llvm::raw_ostream &operator<<(llvm::raw_ostream &os, ValueOwnershipKind Kind);

lib/SIL/SILOwnershipVerifier.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,6 @@ template <> class PointerLikeTypeTraits<GeneralizedUser> {
170170
// Utility
171171
//===----------------------------------------------------------------------===//
172172

173-
static bool compatibleOwnershipKinds(ValueOwnershipKind K1,
174-
ValueOwnershipKind K2) {
175-
return K1.merge(K2).hasValue();
176-
}
177-
178-
/// Returns true if \p Kind is trivial or if \p Kind is compatible with \p
179-
/// ComparisonKind.
180-
static bool
181-
trivialOrCompatibleOwnershipKinds(ValueOwnershipKind Kind,
182-
ValueOwnershipKind ComparisonKind) {
183-
return compatibleOwnershipKinds(Kind, ValueOwnershipKind::Trivial) ||
184-
compatibleOwnershipKinds(Kind, ComparisonKind);
185-
}
186-
187173
static bool isValueAddressOrTrivial(SILValue V, SILModule &M) {
188174
return V->getType().isAddress() ||
189175
V.getOwnershipKind() == ValueOwnershipKind::Trivial ||
@@ -333,7 +319,7 @@ class OwnershipCompatibilityUseChecker
333319
SILType getType() const { return Op.get()->getType(); }
334320

335321
bool compatibleWithOwnership(ValueOwnershipKind Kind) const {
336-
return compatibleOwnershipKinds(getOwnershipKind(), Kind);
322+
return getOwnershipKind().isCompatibleWith(Kind);
337323
}
338324

339325
bool hasExactOwnership(ValueOwnershipKind Kind) const {
@@ -1049,8 +1035,7 @@ OwnershipUseCheckerResult OwnershipCompatibilityUseChecker::visitEnumArgument(
10491035
} else {
10501036
lifetimeConstraint = UseLifetimeConstraint::MustBeLive;
10511037
}
1052-
return {compatibleOwnershipKinds(ownership, RequiredKind),
1053-
lifetimeConstraint};
1038+
return {ownership.isCompatibleWith(RequiredKind), lifetimeConstraint};
10541039
}
10551040

10561041
// We allow for trivial cases of enums with non-trivial cases to be passed in
@@ -1805,14 +1790,14 @@ void SILValueOwnershipChecker::gatherUsers(
18051790
// TODO: Add a flag that associates the terminator instruction with
18061791
// needing to be verified. If it isn't verified appropriately, assert
18071792
// when the verifier is destroyed.
1808-
if (!trivialOrCompatibleOwnershipKinds(BBArg->getOwnershipKind(),
1809-
OwnershipKind)) {
1793+
auto BBArgOwnershipKind = BBArg->getOwnershipKind();
1794+
if (!BBArgOwnershipKind.isTrivialOrCompatibleWith(OwnershipKind)) {
18101795
// This is where the error would go.
18111796
continue;
18121797
}
18131798

18141799
// If we have a trivial value, just continue.
1815-
if (BBArg->getOwnershipKind() == ValueOwnershipKind::Trivial)
1800+
if (BBArgOwnershipKind == ValueOwnershipKind::Trivial)
18161801
continue;
18171802

18181803
// Otherwise,

0 commit comments

Comments
 (0)