-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[ownership] Only allow BranchPropagatedUser to be constructed from Operands. #27316
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
gottesmm
merged 3 commits into
swiftlang:master
from
gottesmm:pr-634f227f60306a17bbad2b205f6bc0cea46541cf
Sep 24, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,19 +93,19 @@ class SILValueOwnershipChecker { | |
|
||
/// The list of lifetime ending users that we found. Only valid if check is | ||
/// successful. | ||
SmallVector<BranchPropagatedUser, 16> lifetimeEndingUsers; | ||
SmallVector<Operand *, 16> lifetimeEndingUsers; | ||
|
||
/// The list of non lifetime ending users that we found. Only valid if check | ||
/// is successful. | ||
SmallVector<BranchPropagatedUser, 16> regularUsers; | ||
SmallVector<Operand *, 16> regularUsers; | ||
|
||
/// The list of implicit non lifetime ending users that we found. This | ||
/// consists of instructions like end_borrow that end a scoped lifetime. We | ||
/// must treat those as regular uses and ensure that our value is not | ||
/// destroyed while that sub-scope is valid. | ||
/// | ||
/// TODO: Rename to SubBorrowScopeUsers? | ||
SmallVector<BranchPropagatedUser, 4> implicitRegularUsers; | ||
SmallVector<Operand *, 4> implicitRegularUsers; | ||
|
||
/// The set of blocks that we have visited. | ||
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks; | ||
|
@@ -133,22 +133,25 @@ class SILValueOwnershipChecker { | |
if (!result.getValue()) | ||
return false; | ||
|
||
SmallVector<BranchPropagatedUser, 32> allLifetimeEndingUsers; | ||
llvm::copy(lifetimeEndingUsers, std::back_inserter(allLifetimeEndingUsers)); | ||
SmallVector<BranchPropagatedUser, 32> allRegularUsers; | ||
llvm::copy(regularUsers, std::back_inserter(allRegularUsers)); | ||
llvm::copy(implicitRegularUsers, std::back_inserter(allRegularUsers)); | ||
|
||
LinearLifetimeChecker checker(visitedBlocks, deadEndBlocks); | ||
auto linearLifetimeResult = checker.checkValue( | ||
value, lifetimeEndingUsers, allRegularUsers, errorBehavior); | ||
value, allLifetimeEndingUsers, allRegularUsers, errorBehavior); | ||
result = !linearLifetimeResult.getFoundError(); | ||
|
||
return result.getValue(); | ||
} | ||
|
||
private: | ||
bool checkUses(); | ||
bool gatherUsers(SmallVectorImpl<BranchPropagatedUser> &lifetimeEndingUsers, | ||
SmallVectorImpl<BranchPropagatedUser> ®ularUsers, | ||
SmallVectorImpl<BranchPropagatedUser> &implicitRegularUsers); | ||
bool gatherUsers(SmallVectorImpl<Operand *> &lifetimeEndingUsers, | ||
SmallVectorImpl<Operand *> ®ularUsers, | ||
SmallVectorImpl<Operand *> &implicitRegularUsers); | ||
|
||
bool checkValueWithoutLifetimeEndingUses(); | ||
|
||
|
@@ -157,10 +160,10 @@ class SILValueOwnershipChecker { | |
|
||
bool isGuaranteedFunctionArgWithLifetimeEndingUses( | ||
SILFunctionArgument *arg, | ||
const SmallVectorImpl<BranchPropagatedUser> &lifetimeEndingUsers) const; | ||
const SmallVectorImpl<Operand *> &lifetimeEndingUsers) const; | ||
bool isSubobjectProjectionWithLifetimeEndingUses( | ||
SILValue value, | ||
const SmallVectorImpl<BranchPropagatedUser> &lifetimeEndingUsers) const; | ||
const SmallVectorImpl<Operand *> &lifetimeEndingUsers) const; | ||
|
||
/// Depending on our initialization, either return false or call Func and | ||
/// throw an error. | ||
|
@@ -181,9 +184,9 @@ class SILValueOwnershipChecker { | |
} // end anonymous namespace | ||
|
||
bool SILValueOwnershipChecker::gatherUsers( | ||
SmallVectorImpl<BranchPropagatedUser> &lifetimeEndingUsers, | ||
SmallVectorImpl<BranchPropagatedUser> &nonLifetimeEndingUsers, | ||
SmallVectorImpl<BranchPropagatedUser> &implicitRegularUsers) { | ||
SmallVectorImpl<Operand *> &lifetimeEndingUsers, | ||
SmallVectorImpl<Operand *> &nonLifetimeEndingUsers, | ||
SmallVectorImpl<Operand *> &implicitRegularUsers) { | ||
|
||
// See if Value is guaranteed. If we are guaranteed and not forwarding, then | ||
// we need to look through subobject uses for more uses. Otherwise, if we are | ||
|
@@ -198,19 +201,7 @@ bool SILValueOwnershipChecker::gatherUsers( | |
|
||
// Then gather up our initial list of users. | ||
SmallVector<Operand *, 8> users; | ||
std::copy(value->use_begin(), value->use_end(), std::back_inserter(users)); | ||
|
||
auto addCondBranchToList = [](SmallVectorImpl<BranchPropagatedUser> &list, | ||
CondBranchInst *cbi, unsigned operandIndex) { | ||
if (cbi->isConditionOperandIndex(operandIndex)) { | ||
list.emplace_back(cbi); | ||
return; | ||
} | ||
|
||
bool isTrueOperand = cbi->isTrueOperandIndex(operandIndex); | ||
list.emplace_back(cbi, isTrueOperand ? CondBranchInst::TrueIdx | ||
: CondBranchInst::FalseIdx); | ||
}; | ||
llvm::copy(value->getUses(), std::back_inserter(users)); | ||
|
||
bool foundError = false; | ||
while (!users.empty()) { | ||
|
@@ -267,19 +258,10 @@ bool SILValueOwnershipChecker::gatherUsers( | |
opOwnershipKindMap.getLifetimeConstraint(ownershipKind); | ||
if (lifetimeConstraint == UseLifetimeConstraint::MustBeInvalidated) { | ||
LLVM_DEBUG(llvm::dbgs() << " Lifetime Ending User: " << *user); | ||
if (auto *cbi = dyn_cast<CondBranchInst>(user)) { | ||
addCondBranchToList(lifetimeEndingUsers, cbi, op->getOperandNumber()); | ||
} else { | ||
lifetimeEndingUsers.emplace_back(user); | ||
} | ||
lifetimeEndingUsers.push_back(op); | ||
} else { | ||
LLVM_DEBUG(llvm::dbgs() << " Regular User: " << *user); | ||
if (auto *cbi = dyn_cast<CondBranchInst>(user)) { | ||
addCondBranchToList(nonLifetimeEndingUsers, cbi, | ||
op->getOperandNumber()); | ||
} else { | ||
nonLifetimeEndingUsers.emplace_back(user); | ||
} | ||
nonLifetimeEndingUsers.push_back(op); | ||
} | ||
|
||
// If our base value is not guaranteed, we do not to try to visit | ||
|
@@ -296,12 +278,14 @@ bool SILValueOwnershipChecker::gatherUsers( | |
// For correctness reasons we use indices to make sure that we can | ||
// append to NonLifetimeEndingUsers without needing to deal with | ||
// iterator invalidation. | ||
SmallVector<SILInstruction *, 4> endBorrowInsts; | ||
for (unsigned i : indices(nonLifetimeEndingUsers)) { | ||
if (auto *bbi = dyn_cast<BeginBorrowInst>( | ||
nonLifetimeEndingUsers[i].getInst())) { | ||
llvm::copy(bbi->getEndBorrows(), | ||
std::back_inserter(implicitRegularUsers)); | ||
nonLifetimeEndingUsers[i]->getUser())) { | ||
for (auto *use : bbi->getUses()) { | ||
if (isa<EndBorrowInst>(use->getUser())) { | ||
implicitRegularUsers.push_back(use); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here. |
||
} | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -381,8 +365,8 @@ bool SILValueOwnershipChecker::gatherUsers( | |
// them to ensure that all of BBArg's uses are completely | ||
// enclosed within the end_borrow of this argument. | ||
for (auto *op : succArg->getUses()) { | ||
if (auto *ebi = dyn_cast<EndBorrowInst>(op->getUser())) { | ||
implicitRegularUsers.push_back(ebi); | ||
if (isa<EndBorrowInst>(op->getUser())) { | ||
implicitRegularUsers.push_back(op); | ||
} | ||
} | ||
} | ||
|
@@ -493,33 +477,31 @@ bool SILValueOwnershipChecker::checkValueWithoutLifetimeEndingUses() { | |
|
||
bool SILValueOwnershipChecker::isGuaranteedFunctionArgWithLifetimeEndingUses( | ||
SILFunctionArgument *arg, | ||
const llvm::SmallVectorImpl<BranchPropagatedUser> &lifetimeEndingUsers) | ||
const { | ||
const llvm::SmallVectorImpl<Operand *> &lifetimeEndingUsers) const { | ||
if (arg->getOwnershipKind() != ValueOwnershipKind::Guaranteed) | ||
return true; | ||
|
||
return handleError([&] { | ||
llvm::errs() << " Function: '" << arg->getFunction()->getName() << "'\n" | ||
<< " Guaranteed function parameter with life ending uses!\n" | ||
<< " Value: " << *arg; | ||
for (const auto &user : lifetimeEndingUsers) { | ||
llvm::errs() << " Lifetime Ending User: " << *user; | ||
for (const auto *use : lifetimeEndingUsers) { | ||
llvm::errs() << " Lifetime Ending User: " << *use->getUser(); | ||
} | ||
llvm::errs() << '\n'; | ||
}); | ||
} | ||
|
||
bool SILValueOwnershipChecker::isSubobjectProjectionWithLifetimeEndingUses( | ||
SILValue value, | ||
const llvm::SmallVectorImpl<BranchPropagatedUser> &lifetimeEndingUsers) | ||
const { | ||
const llvm::SmallVectorImpl<Operand *> &lifetimeEndingUsers) const { | ||
return handleError([&] { | ||
llvm::errs() << " Function: '" << value->getFunction()->getName() | ||
<< "'\n" | ||
<< " Subobject projection with life ending uses!\n" | ||
<< " Value: " << *value; | ||
for (const auto &user : lifetimeEndingUsers) { | ||
llvm::errs() << " Lifetime Ending User: " << *user; | ||
for (const auto *use : lifetimeEndingUsers) { | ||
llvm::errs() << " Lifetime Ending User: " << *use->getUser(); | ||
} | ||
llvm::errs() << '\n'; | ||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are still used by some internal details of BranchPropagatedUser. I wanted to keep this commit really clean and easy to read, so I made these private rather than modifying the in-class callers.