Skip to content

Commit e6d8875

Browse files
authored
Merge pull request #34684 from gottesmm/pr-fb32a04889940253215b38aefec663e911d6e3d6
[ownership] Some small fixes from Andy's review.
2 parents dfe92be + 43a89e0 commit e6d8875

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

include/swift/SIL/SILValue.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,9 @@ class OwnershipConstraint {
597597
return lifetimeConstraint;
598598
}
599599

600-
static OwnershipConstraint anyValueAcceptingConstraint() {
600+
/// Return a constraint that is appropriate for an operand that can accept a
601+
/// value with any ownership kind without ending said value's lifetime.
602+
static OwnershipConstraint any() {
601603
return {OwnershipKind::Any, UseLifetimeConstraint::NonLifetimeEnding};
602604
}
603605

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ CONSTANT_OR_NONE_OWNERSHIP_INST(Owned, LifetimeEnding, DeinitExistentialValue)
246246
#define ACCEPTS_ANY_OWNERSHIP_INST(INST) \
247247
OwnershipConstraint OwnershipConstraintClassifier::visit##INST##Inst( \
248248
INST##Inst *i) { \
249-
return OwnershipConstraint::anyValueAcceptingConstraint(); \
249+
return OwnershipConstraint::any(); \
250250
}
251251
ACCEPTS_ANY_OWNERSHIP_INST(BeginBorrow)
252252
ACCEPTS_ANY_OWNERSHIP_INST(CopyValue)
@@ -342,13 +342,13 @@ OwnershipConstraint OwnershipConstraintClassifier::visitDeallocPartialRefInst(
342342
return {OwnershipKind::Owned, UseLifetimeConstraint::LifetimeEnding};
343343
}
344344

345-
return OwnershipConstraint::anyValueAcceptingConstraint();
345+
return OwnershipConstraint::any();
346346
}
347347

348348
OwnershipConstraint
349349
OwnershipConstraintClassifier::visitSelectEnumInst(SelectEnumInst *i) {
350350
if (getValue() == i->getEnumOperand()) {
351-
return OwnershipConstraint::anyValueAcceptingConstraint();
351+
return OwnershipConstraint::any();
352352
}
353353

354354
auto kind = i->getOwnershipKind();
@@ -360,14 +360,14 @@ OwnershipConstraint
360360
OwnershipConstraintClassifier::visitAllocRefInst(AllocRefInst *i) {
361361
assert(i->getNumOperands() != 0 &&
362362
"If we reach this point, we must have a tail operand");
363-
return OwnershipConstraint::anyValueAcceptingConstraint();
363+
return OwnershipConstraint::any();
364364
}
365365

366366
OwnershipConstraint OwnershipConstraintClassifier::visitAllocRefDynamicInst(
367367
AllocRefDynamicInst *i) {
368368
assert(i->getNumOperands() != 0 &&
369369
"If we reach this point, we must have a tail operand");
370-
return OwnershipConstraint::anyValueAcceptingConstraint();
370+
return OwnershipConstraint::any();
371371
}
372372

373373
OwnershipConstraint
@@ -401,7 +401,7 @@ OwnershipConstraint
401401
OwnershipConstraintClassifier::visitCondBranchInst(CondBranchInst *cbi) {
402402
// In ossa, cond_br insts are not allowed to take non-trivial values. Thus, we
403403
// just accept anything since we know all of our operands will be trivial.
404-
return OwnershipConstraint::anyValueAcceptingConstraint();
404+
return OwnershipConstraint::any();
405405
}
406406

407407
OwnershipConstraint
@@ -444,7 +444,7 @@ OwnershipConstraintClassifier::visitThrowInst(ThrowInst *i) {
444444
/* but it does not touch the strong reference count of the value. We */ \
445445
/* also just care about liveness for the dest. So just match everything */ \
446446
/* as must be live. */ \
447-
return OwnershipConstraint::anyValueAcceptingConstraint(); \
447+
return OwnershipConstraint::any(); \
448448
}
449449
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
450450
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, "...")
@@ -456,7 +456,7 @@ OwnershipConstraintClassifier::visitStoreBorrowInst(StoreBorrowInst *i) {
456456
return {OwnershipKind::Guaranteed,
457457
UseLifetimeConstraint::NonLifetimeEnding};
458458
}
459-
return OwnershipConstraint::anyValueAcceptingConstraint();
459+
return OwnershipConstraint::any();
460460
}
461461

462462
// FIXME: Why not use SILArgumentConvention here?
@@ -478,12 +478,12 @@ OwnershipConstraintClassifier::visitCallee(CanSILFunctionType substCalleeType) {
478478
case ParameterConvention::Indirect_InoutAliasable:
479479
llvm_unreachable("Illegal convention for callee");
480480
case ParameterConvention::Direct_Unowned:
481-
return OwnershipConstraint::anyValueAcceptingConstraint();
481+
return OwnershipConstraint::any();
482482
case ParameterConvention::Direct_Owned:
483483
return {OwnershipKind::Owned, UseLifetimeConstraint::LifetimeEnding};
484484
case ParameterConvention::Direct_Guaranteed:
485485
if (substCalleeType->isNoEscape())
486-
return OwnershipConstraint::anyValueAcceptingConstraint();
486+
return OwnershipConstraint::any();
487487
return {OwnershipKind::Guaranteed,
488488
UseLifetimeConstraint::NonLifetimeEnding};
489489
}
@@ -509,7 +509,7 @@ OwnershipConstraintClassifier::visitFullApply(FullApplySite apply) {
509509

510510
// Indirect return arguments are address types.
511511
if (apply.isIndirectResultOperand(op)) {
512-
return OwnershipConstraint::anyValueAcceptingConstraint();
512+
return OwnershipConstraint::any();
513513
}
514514

515515
// We should have early exited if we saw a type dependent operand, so we
@@ -527,12 +527,12 @@ OwnershipConstraintClassifier::visitFullApply(FullApplySite apply) {
527527
return visitApplyParameter(OwnershipKind::Owned,
528528
UseLifetimeConstraint::LifetimeEnding);
529529
case ParameterConvention::Direct_Unowned:
530-
return OwnershipConstraint::anyValueAcceptingConstraint();
530+
return OwnershipConstraint::any();
531531

532532
case ParameterConvention::Indirect_In: {
533533
// This expects an @trivial if we have lowered addresses and @
534534
if (conv.useLoweredAddresses()) {
535-
return OwnershipConstraint::anyValueAcceptingConstraint();
535+
return OwnershipConstraint::any();
536536
}
537537
// TODO: Once trivial is subsumed in any, this goes away.
538538
auto map = visitApplyParameter(OwnershipKind::Owned,
@@ -543,7 +543,7 @@ OwnershipConstraintClassifier::visitFullApply(FullApplySite apply) {
543543
case ParameterConvention::Indirect_In_Guaranteed: {
544544
// This expects an @trivial if we have lowered addresses and @
545545
if (conv.useLoweredAddresses()) {
546-
return OwnershipConstraint::anyValueAcceptingConstraint();
546+
return OwnershipConstraint::any();
547547
}
548548
return visitApplyParameter(OwnershipKind::Guaranteed,
549549
UseLifetimeConstraint::NonLifetimeEnding);
@@ -554,7 +554,7 @@ OwnershipConstraintClassifier::visitFullApply(FullApplySite apply) {
554554
case ParameterConvention::Indirect_In_Constant:
555555
case ParameterConvention::Indirect_Inout:
556556
case ParameterConvention::Indirect_InoutAliasable:
557-
return OwnershipConstraint::anyValueAcceptingConstraint();
557+
return OwnershipConstraint::any();
558558

559559
case ParameterConvention::Direct_Guaranteed:
560560
// A +1 value may be passed to a guaranteed argument. From the caller's
@@ -586,7 +586,7 @@ OwnershipConstraint
586586
OwnershipConstraintClassifier::visitPartialApplyInst(PartialApplyInst *i) {
587587
// partial_apply [stack] does not take ownership of its operands.
588588
if (i->isOnStack())
589-
return OwnershipConstraint::anyValueAcceptingConstraint();
589+
return OwnershipConstraint::any();
590590

591591
// All non-trivial types should be captured.
592592
return {OwnershipKind::Owned, UseLifetimeConstraint::LifetimeEnding};
@@ -599,7 +599,7 @@ OwnershipConstraintClassifier::visitYieldInst(YieldInst *i) {
599599
//
600600
// TODO: Change this to check if this operand is an indirect result
601601
if (isAddressOrTrivialType())
602-
return OwnershipConstraint::anyValueAcceptingConstraint();
602+
return OwnershipConstraint::any();
603603

604604
auto fnType = i->getFunction()->getLoweredFunctionType();
605605
auto yieldInfo = fnType->getYields()[getOperandIndex()];
@@ -611,7 +611,7 @@ OwnershipConstraintClassifier::visitYieldInst(YieldInst *i) {
611611
case ParameterConvention::Indirect_In_Constant:
612612
case ParameterConvention::Direct_Unowned:
613613
// We accept unowned, owned, and guaranteed in unowned positions.
614-
return OwnershipConstraint::anyValueAcceptingConstraint();
614+
return OwnershipConstraint::any();
615615
case ParameterConvention::Indirect_In_Guaranteed:
616616
case ParameterConvention::Direct_Guaranteed:
617617
return visitApplyParameter(OwnershipKind::Guaranteed,
@@ -627,7 +627,7 @@ OwnershipConstraintClassifier::visitYieldInst(YieldInst *i) {
627627
OwnershipConstraint
628628
OwnershipConstraintClassifier::visitAssignInst(AssignInst *i) {
629629
if (getValue() != i->getSrc()) {
630-
return OwnershipConstraint::anyValueAcceptingConstraint();
630+
return OwnershipConstraint::any();
631631
}
632632

633633
return {OwnershipKind::Owned, UseLifetimeConstraint::LifetimeEnding};
@@ -636,7 +636,7 @@ OwnershipConstraintClassifier::visitAssignInst(AssignInst *i) {
636636
OwnershipConstraint OwnershipConstraintClassifier::visitAssignByWrapperInst(
637637
AssignByWrapperInst *i) {
638638
if (getValue() != i->getSrc()) {
639-
return OwnershipConstraint::anyValueAcceptingConstraint();
639+
return OwnershipConstraint::any();
640640
}
641641

642642
return {OwnershipKind::Owned, UseLifetimeConstraint::LifetimeEnding};
@@ -645,7 +645,7 @@ OwnershipConstraint OwnershipConstraintClassifier::visitAssignByWrapperInst(
645645
OwnershipConstraint
646646
OwnershipConstraintClassifier::visitStoreInst(StoreInst *i) {
647647
if (getValue() != i->getSrc()) {
648-
return OwnershipConstraint::anyValueAcceptingConstraint();
648+
return OwnershipConstraint::any();
649649
}
650650

651651
return {OwnershipKind::Owned, UseLifetimeConstraint::LifetimeEnding};
@@ -659,7 +659,7 @@ OwnershipConstraintClassifier::visitCopyBlockWithoutEscapingInst(
659659
return {OwnershipKind::Owned, UseLifetimeConstraint::LifetimeEnding};
660660
}
661661

662-
return OwnershipConstraint::anyValueAcceptingConstraint();
662+
return OwnershipConstraint::any();
663663
}
664664

665665
OwnershipConstraint OwnershipConstraintClassifier::visitMarkDependenceInst(
@@ -668,7 +668,7 @@ OwnershipConstraint OwnershipConstraintClassifier::visitMarkDependenceInst(
668668
if (getValue() == mdi->getValue()) {
669669
auto kind = mdi->getOwnershipKind();
670670
if (kind == OwnershipKind::None)
671-
return OwnershipConstraint::anyValueAcceptingConstraint();
671+
return OwnershipConstraint::any();
672672
auto lifetimeConstraint = kind.getForwardingLifetimeConstraint();
673673
return {kind, lifetimeConstraint};
674674
}
@@ -677,7 +677,7 @@ OwnershipConstraint OwnershipConstraintClassifier::visitMarkDependenceInst(
677677
// "base". This means that any use that would destroy "value" can not be moved
678678
// before any uses of "base". We treat this as non-consuming and rely on the
679679
// rest of the optimizer to respect the movement restrictions.
680-
return OwnershipConstraint::anyValueAcceptingConstraint();
680+
return OwnershipConstraint::any();
681681
}
682682

683683
OwnershipConstraint
@@ -702,7 +702,7 @@ struct OperandOwnershipKindBuiltinClassifier
702702
llvm::Intrinsic::ID id) {
703703
// LLVM intrinsics do not traffic in ownership, so if we have a result, it
704704
// must be trivial.
705-
return OwnershipConstraint::anyValueAcceptingConstraint();
705+
return OwnershipConstraint::any();
706706
}
707707

708708
// BUILTIN_TYPE_CHECKER_OPERATION does not live past the type checker.
@@ -720,7 +720,7 @@ struct OperandOwnershipKindBuiltinClassifier
720720
#define ANY_OWNERSHIP_BUILTIN(ID) \
721721
OwnershipConstraint OperandOwnershipKindBuiltinClassifier::visit##ID( \
722722
BuiltinInst *, StringRef) { \
723-
return OwnershipConstraint::anyValueAcceptingConstraint(); \
723+
return OwnershipConstraint::any(); \
724724
}
725725
ANY_OWNERSHIP_BUILTIN(ErrorInMain)
726726
ANY_OWNERSHIP_BUILTIN(UnexpectedError)

lib/SIL/IR/SILValue.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -352,28 +352,24 @@ bool Operand::satisfiesConstraints() const {
352352
bool Operand::isLifetimeEnding() const {
353353
auto constraint = getOwnershipConstraint();
354354

355-
// If we got back none, then our operand is for a type dependent operand. So
356-
// return false.
355+
// If we got back Optional::None, then our operand is for a type dependent
356+
// operand. So return false.
357357
if (!constraint)
358358
return false;
359359

360360
// If our use lifetime constraint is NonLifetimeEnding, just return false.
361361
if (!constraint->isLifetimeEnding())
362362
return false;
363363

364-
// Otherwise, we may have a lifetime ending use. If we narrowed to None then
365-
// we have a non lifetime ending use, otherwise we still have a lifetime
366-
// ending use. If our value was not None, then obviously narrowing didn't
367-
// happen.
368-
if (get().getOwnershipKind() != OwnershipKind::None)
369-
return true;
370-
371-
// Otherwise, narrowing only happened if our ownership constraint did not have
372-
// an OwnershipKind of None, but we disallow that in any case, so we can just
373-
// unconditionally return false.
374-
assert(constraint->getPreferredKind() != OwnershipKind::None &&
375-
"None values should never have a lifetime ending constraint");
376-
return false;
364+
// Otherwise, we may have a lifetime ending use. We consider two cases here:
365+
// the case where our value has OwnershipKind::None and one where it has some
366+
// other OwnershipKind. Note that values with OwnershipKind::None ownership
367+
// can not have their lifetime ended since they are outside of the ownership
368+
// system. Given such a case, if we have such a value we return
369+
// isLifetimeEnding() as false even if the constraint itself has a constraint
370+
// that says a value is LifetimeEnding. If we have a value that has a
371+
// non-OwnershipKind::None ownership then we just return true as expected.
372+
return get().getOwnershipKind() != OwnershipKind::None;
377373
}
378374

379375
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)