Skip to content

[ownership] Add helper method Operand::isConsumingUse() const. #29830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/SIL/SILValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,14 @@ class Operand {
OperandOwnershipKindMap
getOwnershipKindMap(bool isForwardingSubValue = false) const;

/// Returns true if this operand acts as a use that consumes its associated
/// value.
bool isConsumingUse() const {
auto map = getOwnershipKindMap();
auto constraint = map.getLifetimeConstraint(get().getOwnershipKind());
return constraint == UseLifetimeConstraint::MustBeInvalidated;
}

private:
void removeFromCurrent() {
if (!Back) return;
Expand Down
105 changes: 51 additions & 54 deletions lib/SILOptimizer/Mandatory/SemanticARCOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,75 +79,72 @@ class LiveRange {

LiveRange::LiveRange(SILValue value)
: destroys(), generalForwardingInsts(), unknownConsumingUsers() {
SmallVector<Operand *, 32> worklist(value->getUses());
assert(value.getOwnershipKind() == ValueOwnershipKind::Owned);

// We know that our silvalue produces an @owned value. Look through all of our
// uses and classify them as either consuming or not.
SmallVector<Operand *, 32> worklist(value->getUses());
while (!worklist.empty()) {
auto *op = worklist.pop_back_val();

// Skip type dependent operands.
if (op->isTypeDependent())
continue;

auto *user = op->getUser();
// Do a quick check that we did not add ValueOwnershipKind that are not
// owned to the worklist.
assert(op->get().getOwnershipKind() == ValueOwnershipKind::Owned &&
"Added non-owned value to worklist?!");

// We know that a copy_value produces an @owned value. Look through all of
// our uses and classify them as either invalidating or not
// invalidating. Make sure that all of the invalidating ones are
// destroy_value since otherwise the live_range is not complete.
auto map = op->getOwnershipKindMap();
auto constraint = map.getLifetimeConstraint(ValueOwnershipKind::Owned);
switch (constraint) {
case UseLifetimeConstraint::MustBeInvalidated: {
// See if we have a destroy value. If we don't we have an
// unknown consumer. Return false, we need this live range.
if (auto *dvi = dyn_cast<DestroyValueInst>(user)) {
destroys.push_back(dvi);
continue;
}
auto *user = op->getUser();

// Otherwise, see if we have a forwarding value that has a single
// non-trivial operand that can accept a guaranteed value. If not, we can
// not recursively process it, so be conservative and assume that we /may
// consume/ the value, so the live range must not be eliminated.
//
// DISCUSSION: For now we do not support forwarding instructions with
// multiple non-trivial arguments since we would need to optimize all of
// the non-trivial arguments at the same time.
//
// NOTE: Today we do not support TermInsts for simplicity... we /could/
// support it though if we need to.
if (isa<TermInst>(user) || !isGuaranteedForwardingInst(user) ||
1 != count_if(user->getOperandValues(
true /*ignore type dependent operands*/),
[&](SILValue v) {
return v.getOwnershipKind() ==
ValueOwnershipKind::Owned;
})) {
unknownConsumingUsers.push_back(user);
continue;
}
// Ok, this constraint can take something owned as live. Assert that it
// can also accept something that is guaranteed. Any non-consuming use of
// an owned value should be able to take a guaranteed parameter as well
// (modulo bugs). We assert to catch these.
if (!op->isConsumingUse()) {
continue;
}

// Ok, this is a forwarding instruction whose ownership we can flip from
// owned -> guaranteed. Visit its users recursively to see if the the
// users force the live range to be alive.
generalForwardingInsts.push_back(user);
for (SILValue v : user->getResults()) {
if (v.getOwnershipKind() != ValueOwnershipKind::Owned)
continue;
llvm::copy(v->getUses(), std::back_inserter(worklist));
}
// Ok, we know now that we have a consuming use. See if we have a destroy
// value, quickly up front. If we do have one, stash it and continue.
if (auto *dvi = dyn_cast<DestroyValueInst>(user)) {
destroys.push_back(dvi);
continue;
}
case UseLifetimeConstraint::MustBeLive:
// Ok, this constraint can take something owned as live. Assert that it
// can also accept something that is guaranteed. Any non-consuming use of
// an owned value should be able to take a guaranteed parameter as well
// (modulo bugs). We assert to catch these.
assert(map.canAcceptKind(ValueOwnershipKind::Guaranteed) &&
"Any non-consuming use of an owned value should be able to take a "
"guaranteed value");

// Otherwise, we have some form of consuming use that either forwards or
// that we do not understand. See if we have a forwarding value that has a
// single non-trivial operand that can accept a guaranteed value. If not, we
// can not recursively process it, so be conservative and assume that we
// /may consume/ the value, so the live range must not be eliminated.
//
// DISCUSSION: For now we do not support forwarding instructions with
// multiple non-trivial arguments since we would need to optimize all of
// the non-trivial arguments at the same time.
//
// NOTE: Today we do not support TermInsts for simplicity... we /could/
// support it though if we need to.
if (isa<TermInst>(user) || !isGuaranteedForwardingInst(user) ||
1 != count_if(user->getOperandValues(
true /*ignore type dependent operands*/),
[&](SILValue v) {
return v.getOwnershipKind() ==
ValueOwnershipKind::Owned;
})) {
unknownConsumingUsers.push_back(user);
continue;
}

// Ok, this is a forwarding instruction whose ownership we can flip from
// owned -> guaranteed. Visit its users recursively to see if the the
// users force the live range to be alive.
generalForwardingInsts.push_back(user);
for (SILValue v : user->getResults()) {
if (v.getOwnershipKind() != ValueOwnershipKind::Owned)
continue;
llvm::copy(v->getUses(), std::back_inserter(worklist));
}
}
}

Expand Down
7 changes: 2 additions & 5 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,10 @@ static void destroyConsumedOperandOfDeadInst(Operand &operand) {
assert(!isEndOfScopeMarker(deadInst) && !isa<DestroyValueInst>(deadInst) &&
!isa<DestroyAddrInst>(deadInst) &&
"lifetime ending instruction is deleted without its operand");
ValueOwnershipKind operandOwnershipKind = operandValue.getOwnershipKind();
UseLifetimeConstraint lifetimeConstraint =
operand.getOwnershipKindMap().getLifetimeConstraint(operandOwnershipKind);
if (lifetimeConstraint == UseLifetimeConstraint::MustBeInvalidated) {
if (operand.isConsumingUse()) {
// Since deadInst cannot be an end-of-scope instruction (asserted above),
// this must be a consuming use of an owned value.
assert(operandOwnershipKind == ValueOwnershipKind::Owned);
assert(operandValue.getOwnershipKind() == ValueOwnershipKind::Owned);
SILBuilderWithScope builder(deadInst);
builder.emitDestroyValueOperation(deadInst->getLoc(), operandValue);
}
Expand Down