Skip to content

Commit 554bc78

Browse files
authored
Merge pull request #6831 from gottesmm/small_getownershipvalue_fixes
Small SILValue::getOwnershipValue() fixes
2 parents 280e34d + 2f9a90d commit 554bc78

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

lib/SIL/SILValue.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ CONSTANT_OWNERSHIP_INST(Owned, LoadUnowned)
187187
CONSTANT_OWNERSHIP_INST(Owned, LoadWeak)
188188
CONSTANT_OWNERSHIP_INST(Owned, PartialApply)
189189
CONSTANT_OWNERSHIP_INST(Owned, StrongPin)
190+
CONSTANT_OWNERSHIP_INST(Owned, ThinToThickFunction)
191+
192+
// One would think that these /should/ be unowned. In truth they are owned since
193+
// objc metatypes do not go through the retain/release fast path. In their
194+
// implementations of retain/release nothing happens, so this is safe.
195+
//
196+
// You could even have an optimization that just always removed retain/release
197+
// operations on these objects.
198+
CONSTANT_OWNERSHIP_INST(Owned, ObjCExistentialMetatypeToObject)
199+
CONSTANT_OWNERSHIP_INST(Owned, ObjCMetatypeToObject)
200+
190201
// All addresses have trivial ownership. The values stored at the address may
191202
// not though.
192203
CONSTANT_OWNERSHIP_INST(Trivial, AddressToPointer)
@@ -213,9 +224,6 @@ CONSTANT_OWNERSHIP_INST(Trivial, MarkFunctionEscape)
213224
CONSTANT_OWNERSHIP_INST(Trivial, MarkUninitialized)
214225
CONSTANT_OWNERSHIP_INST(Trivial, MarkUninitializedBehavior)
215226
CONSTANT_OWNERSHIP_INST(Trivial, Metatype)
216-
CONSTANT_OWNERSHIP_INST(Trivial,
217-
ObjCExistentialMetatypeToObject) // Is this right?
218-
CONSTANT_OWNERSHIP_INST(Trivial, ObjCMetatypeToObject) // Is this right?
219227
CONSTANT_OWNERSHIP_INST(Trivial, ObjCProtocol) // Is this right?
220228
CONSTANT_OWNERSHIP_INST(Trivial, ObjCToThickMetatype)
221229
CONSTANT_OWNERSHIP_INST(Trivial, OpenExistentialAddr)
@@ -238,10 +246,8 @@ CONSTANT_OWNERSHIP_INST(Trivial, SuperMethod)
238246
CONSTANT_OWNERSHIP_INST(Trivial, TailAddr)
239247
CONSTANT_OWNERSHIP_INST(Trivial, ThickToObjCMetatype)
240248
CONSTANT_OWNERSHIP_INST(Trivial, ThinFunctionToPointer)
241-
CONSTANT_OWNERSHIP_INST(Trivial, ThinToThickFunction)
242249
CONSTANT_OWNERSHIP_INST(Trivial, TupleElementAddr)
243250
CONSTANT_OWNERSHIP_INST(Trivial, UncheckedAddrCast)
244-
CONSTANT_OWNERSHIP_INST(Trivial, UncheckedBitwiseCast)
245251
CONSTANT_OWNERSHIP_INST(Trivial, UncheckedRefCastAddr)
246252
CONSTANT_OWNERSHIP_INST(Trivial, UncheckedTakeEnumDataAddr)
247253
CONSTANT_OWNERSHIP_INST(Trivial, UncheckedTrivialBitCast)
@@ -392,6 +398,35 @@ FORWARDING_OWNERSHIP_INST(UnconditionalCheckedCast)
392398
FORWARDING_OWNERSHIP_INST(Upcast)
393399
#undef FORWARDING_OWNERSHIP_INST
394400

401+
ValueOwnershipKind
402+
ValueOwnershipKindVisitor::
403+
visitUncheckedBitwiseCastInst(UncheckedBitwiseCastInst *UBCI) {
404+
ValueOwnershipKind OpOwnership = UBCI->getOperand().getOwnershipKind();
405+
bool ResultTypeIsTrivial = UBCI->getType().isTrivial(UBCI->getModule());
406+
407+
// First check if our operand has a trivial value ownership kind...
408+
if (OpOwnership == ValueOwnershipKind::Trivial) {
409+
// If we do have a trivial value ownership kind, see if our result type is
410+
// trivial or non-trivial. If it is trivial, then we have trivial
411+
// ownership. Otherwise, we have unowned ownership since from an ownership
412+
// perspective, the value has instantaneously come into existance and
413+
// nothing has taken ownership of it.
414+
if (ResultTypeIsTrivial) {
415+
return ValueOwnershipKind::Trivial;
416+
}
417+
return ValueOwnershipKind::Unowned;
418+
}
419+
420+
// If our operand has non-trivial ownership, but our result does, then of
421+
// course the result has trivial ownership.
422+
if (ResultTypeIsTrivial) {
423+
return ValueOwnershipKind::Trivial;
424+
}
425+
426+
// Otherwise, we forward our ownership.
427+
return visitForwardingInst(UBCI);
428+
}
429+
395430
// An enum without payload is trivial. One with non-trivial payload is
396431
// forwarding.
397432
ValueOwnershipKind

0 commit comments

Comments
 (0)