Skip to content

Conversation

nate-chandler
Copy link
Contributor

When a copy_value's only use is a store, in some cases, as an optimization, creating a copy_addr can be skipped. Whether a value is such a copy_value is determined by the isStoreCopy predicate.

If the operand of the copy_value is such that the store is outside the relevant live ranges (those of the guaranteed roots or that of the owned value), skip this optimization and create the copy_addr.

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler marked this pull request as ready for review December 16, 2022 16:06
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

if (llvm::any_of(roots, [](SILValue root) {
return isa<BeginApplyInst>(root->getDefiningInstruction());
SSAPrunedLiveness liveness;
if (llvm::any_of(roots, [&liveness, storeInst](SILValue root) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, this is an example of where I find range-based for loops easier to read (rather than mentally factoring a closure with inverted an return value). Alternatively, you explicitly factor it into something like isStoreCopyRootValid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.... you basically already did that in the following commit

return true;
}
liveness.initializeDef(root);
auto summary = liveness.computeSimple();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonably safe. But in the future we should be more precise and efficient by doing

  • visitBorrowIntroducers (instead of roots)
  • ExtendedLiveness.compute for each introducer (I haven't posted the PR yet)

That would either find a guaranteed argument and do no work at all, or find a begin_borrow and only visit the end_borrows. It could also handle reborrow-extended liveness.

Up to you if you want a TODO here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great! Added a TODO.

Rather than hard-coding an operand number, ask the operand for it.
Move a cheap check that the one user is a store over the expensive check
done when the value copied has guaranteed ownership.
When a copy_value's only use is a store, in some cases, as an
optimization, creating a copy_addr can be skipped.  Whether a value is
such a copy_value is determined by the isStoreCopy predicate.

If the operand of the copy_value is a guaranteed value and any of its
guaranteed roots have live ranges which the store is outside of, skip
this optimization and create the copy_addr.
Skip the store-copy optimization if the store instruction occurs outside
the lifetime of the value being copied.
@nate-chandler nate-chandler force-pushed the opaque-values/2/20221214 branch from e934b46 to 356f2b7 Compare January 4, 2023 15:54
@nate-chandler
Copy link
Contributor Author

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit df00577 into swiftlang:main Jan 4, 2023
@nate-chandler nate-chandler deleted the opaque-values/2/20221214 branch July 5, 2023 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants