Skip to content

[5.9] Support all BorrowedValueKind in hasPointerEscape(BorrowedValue) api #64711

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
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
83 changes: 34 additions & 49 deletions lib/SIL/Utils/OwnershipUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,44 @@

using namespace swift;

bool swift::hasPointerEscape(BorrowedValue value) {
assert(value.kind == BorrowedValueKind::BeginBorrow ||
value.kind == BorrowedValueKind::LoadBorrow);
GraphNodeWorklist<Operand *, 8> worklist;
for (Operand *use : value->getUses()) {
if (use->getOperandOwnership() != OperandOwnership::NonUse)
worklist.insert(use);
}

while (Operand *op = worklist.pop()) {
switch (op->getOperandOwnership()) {
case OperandOwnership::NonUse:
case OperandOwnership::TrivialUse:
case OperandOwnership::ForwardingConsume:
case OperandOwnership::DestroyingConsume:
llvm_unreachable("this operand cannot handle an inner guaranteed use");
bool swift::hasPointerEscape(BorrowedValue original) {
ValueWorklist worklist(original->getFunction());
worklist.push(*original);

case OperandOwnership::ForwardingUnowned:
case OperandOwnership::PointerEscape:
if (auto *phi = SILArgument::asPhi(*original)) {
phi->visitTransitiveIncomingPhiOperands([&](auto *phi, auto *operand) {
worklist.pushIfNotVisited(operand->get());
return true;
});
}

case OperandOwnership::Borrow:
case OperandOwnership::EndBorrow:
case OperandOwnership::InstantaneousUse:
case OperandOwnership::UnownedInstantaneousUse:
case OperandOwnership::InteriorPointer:
case OperandOwnership::BitwiseEscape:
break;
case OperandOwnership::Reborrow: {
SILArgument *phi = cast<BranchInst>(op->getUser())
->getDestBB()
->getArgument(op->getOperandNumber());
for (auto *use : phi->getUses()) {
if (use->getOperandOwnership() != OperandOwnership::NonUse)
worklist.insert(use);
while (auto value = worklist.pop()) {
for (auto *op : value->getUses()) {
switch (op->getOperandOwnership()) {
case OperandOwnership::ForwardingUnowned:
case OperandOwnership::PointerEscape:
return true;

case OperandOwnership::Reborrow: {
SILArgument *phi = PhiOperand(op).getValue();
worklist.pushIfNotVisited(phi);
break;
}
break;
}
case OperandOwnership::GuaranteedForwarding: {
// This may follow a guaranteed phis.
ForwardingOperand(op).visitForwardedValues([&](SILValue result) {
// Do not include transitive uses with 'none' ownership
if (result->getOwnershipKind() == OwnershipKind::None)

case OperandOwnership::GuaranteedForwarding: {
// This may follow guaranteed phis.
ForwardingOperand(op).visitForwardedValues([&](SILValue result) {
// Do not include transitive uses with 'none' ownership
if (result->getOwnershipKind() == OwnershipKind::None)
return true;
worklist.pushIfNotVisited(result);
return true;
for (auto *resultUse : result->getUses()) {
if (resultUse->getOperandOwnership() != OperandOwnership::NonUse) {
worklist.insert(resultUse);
}
}
return true;
});
break;
}
});
break;
}
default:
break;
}
}
}
return false;
Expand All @@ -97,7 +82,7 @@ bool swift::hasPointerEscape(SILValue original) {
return true;
});
}
while (auto value = worklist.popAndForget()) {
while (auto value = worklist.pop()) {
for (auto use : value->getUses()) {
switch (use->getOperandOwnership()) {
case OperandOwnership::PointerEscape:
Expand Down
16 changes: 10 additions & 6 deletions lib/SILOptimizer/Transforms/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,16 @@ void DCE::markLive() {
}
break;
}
case SILInstructionKind::EndBorrowInst:
case SILInstructionKind::EndBorrowInst: {
auto phi = PhiValue(I.getOperand(0));
// If there is a pointer escape or phi is lexical, disable DCE.
if (phi && (hasPointerEscape(phi) || phi->isLexical())) {
markInstructionLive(&I);
}
// The instruction is live only if it's operand value is also live
addReverseDependency(I.getOperand(0), &I);
break;
}
case SILInstructionKind::EndLifetimeInst: {
// The instruction is live only if it's operand value is also live
addReverseDependency(I.getOperand(0), &I);
Expand Down Expand Up @@ -311,11 +320,6 @@ void DCE::markLive() {
disableBorrowDCE(root);
}
}
// If we have a lexical borrow scope or a pointer escape, disable DCE.
if (borrowInst->isLexical() ||
hasPointerEscape(BorrowedValue(borrowInst))) {
disableBorrowDCE(borrowInst);
}
break;
}
case SILInstructionKind::LoadBorrowInst: {
Expand Down
29 changes: 29 additions & 0 deletions test/SILOptimizer/ownership-utils-unit.sil
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import Builtin

class C {}

enum FakeOptional<T> {
case none
case some(T)
}

sil @getOwned : $@convention(thin) () -> (@owned C)

sil @borrow : $@convention(thin) (@guaranteed C) -> ()
Expand Down Expand Up @@ -42,3 +47,27 @@ exit(%instance : @owned $C):
%retval = tuple ()
return %retval : $()
}

sil [ossa] @test_loop_phi : $@convention(thin) () -> () {
entry:
%instance_1 = enum $FakeOptional<C>, #FakeOptional.none!enumelt
br loop_entry(%instance_1 : $FakeOptional<C>)

loop_entry(%18 : @owned $FakeOptional<C>):
test_specification "has-pointer-escape @block.argument"
br loop_body

loop_body:
cond_br undef, loop_back, loop_exit

loop_back:
br loop_entry(%18 : $FakeOptional<C>)

loop_exit:
destroy_value %18 : $FakeOptional<C>
br exit

exit:
%retval = tuple ()
return %retval : $()
}