Skip to content

[semantic-arc-opts] Add a large comment to the guaranteed copy value … #21061

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
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
32 changes: 32 additions & 0 deletions lib/SILOptimizer/Mandatory/SemanticARCOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ static bool canHandleOperand(SILValue operand, SmallVectorImpl<SILValue> &out) {
return all_of(out, [](SILValue v) { return isa<SILFunctionArgument>(v); });
}

// Eliminate a copy of a borrowed value, if:
//
// 1. All of the copies users do not consume the copy (and thus can accept a
// borrowed value instead).
// 2. The copies's non-destroy_value users are strictly contained within the
// scope of the borrowed value.
//
// Example:
//
// %0 = @guaranteed (argument or instruction)
// %1 = copy_value %0
// apply %f(%1) : $@convention(thin) (@guaranteed ...) ...
// other_non_consuming_use %1
// destroy_value %1
// end_borrow %0 (if an instruction)
//
// =>
//
// %0 = @guaranteed (argument or instruction)
// apply %f(%0) : $@convention(thin) (@guaranteed ...) ...
// other_non_consuming_use %0
// end_borrow %0 (if an instruction)
//
// NOTE: This means that the destroy_value technically can be after the
// end_borrow. In practice, this will not be the case but we use this to avoid
// having to reason about the ordering of the end_borrow and destroy_value.
//
// NOTE: Today we only perform this for guaranteed parameters since this enables
// us to avoid doing the linear lifetime check to make sure that all destroys
// are within the borrow scope.
//
// TODO: This needs a better name.
static bool performGuaranteedCopyValueOptimization(CopyValueInst *cvi) {
SmallVector<SILValue, 16> borrowIntroducers;

Expand Down