[ownership] Try harder to make sure we do not propagate ownership info when ownership is disabled.

Specifically, I made it so that assuming our instruction is inserted into a
block already that we:

1. Return a constraint of {OwnershipKind::Any, UseLifetimeConstraint::NonLifetimeEnding}.
2. Return OwnershipKind::None for all values.

Noticed above I said that if the instruction is already inserted into a block
then we do this. The reason why is that if this is called before an instruction
is inserted into a block, we can't get access to the SILFunction that has the
information on whether or not we are in OSSA form. The only time this can happen
is if one is using these APIs from within SILBuilder since SILBuilder is the
only place where we allow this to happen. In SILBuilder, we already know whether
or not our function is in ossa or not and already does different things as
appropriate (namely in non-ossa does not call getOwnershipKind()). So we know
that if these APIs are called in such a situation, we will only be calling it if
we are in OSSA already. Given that, we just assume we are in OSSA if we do not
have a function.

To make sure that no mistakes are made as a result of that assumption, I put in
a verifier check that all values when ownership is disabled return a
OwnershipKind::None from getOwnershipKind().

The main upside to this is this means that we can write code for both
OSSA/non-OSSA and write code for non-None ownership without needing to check if
ownership is enabled.
This commit is contained in:
Michael Gottesman
2020-10-28 14:49:17 -07:00
parent cd9218c63b
commit 58d4191470
6 changed files with 59 additions and 9 deletions

View File

@@ -583,6 +583,19 @@ ValueOwnershipKindClassifier::visitBuiltinInst(BuiltinInst *BI) {
//===----------------------------------------------------------------------===//
ValueOwnershipKind SILValue::getOwnershipKind() const {
// If we do not have an undef, we should always be able to get to our function
// here. If we do not have ownership enabled, just return none for everything
// to short circuit ownership optimizations. If we have an undef we may still
// get some results that are slightly wonky but hopefully when we lower
// ownership we remove that.
//
// We assume that any time we are in SILBuilder and call this without having a
// value in a block yet, ossa is enabled.
if (auto *block = Value->getParentBlock())
if (auto *f = block->getParent())
if (!f->hasOwnership())
return OwnershipKind::None;
ValueOwnershipKindClassifier Classifier;
auto result = Classifier.visit(const_cast<ValueBase *>(Value));
assert(result && "Returned ownership kind invalid on values");