Skip to content

Optimizer: add a new destroy-hoisting optimization #78059

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 5 commits into from
Dec 11, 2024

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented Dec 9, 2024

It hoists destroy_value instructions without shrinking an object's lifetime.
This is done if it can be proved that another copy of a value (either in an SSA value or in memory) keeps the referenced object(s) alive until the original position of the destroy_value.

  %1 = copy_value %0
  ...
  last_use_of %0
  // other instructions
  destroy_value %0       // %1 is still alive here

->

  %1 = copy_value %0
  ...
  last_use_of %0
  destroy_value %0
  // other instructions

The benefit of this optimization is that it can enable copy-propagation by moving destroys above deinit barries and access scopes.

This is part of fixing benchmark regressions when enabling OSSA modules.
rdar://139773406

@eeckstein eeckstein requested a review from jckarter as a code owner December 9, 2024 16:22
@eeckstein eeckstein requested review from atrick, meg-gupta and nate-chandler and removed request for jckarter December 9, 2024 16:22
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

Just don't store the begin instruction.
This led to problem if the "begin" was not actually an instruction but a block argument.
Using the first instruction of that block is not correct in case the range ends immediately at the first instruction, e.g.

```
  bb0(%0 : @owned $C):
    destroy_value %0
```
This avoids the need for checking if a value is a `move_value`, `begin_borrow` or function argument to get the is-lexical information.
and not just for `CollectionLikeSequence`.
To support stdlib's lazy map and filter sequences.
It hoists `destroy_value` instructions  without shrinking an object's lifetime.
This is done if it can be proved that another copy of a value (either in an SSA value or in memory) keeps the referenced object(s) alive until the original position of the `destroy_value`.
```
  %1 = copy_value %0
  ...
  last_use_of %0
  // other instructions
  destroy_value %0       // %1 is still alive here
```
->
```
  %1 = copy_value %0
  ...
  last_use_of %0
  destroy_value %0
  // other instructions
```

The benefit of this optimization is that it can enable copy-propagation by moving destroys above deinit barries and access scopes.
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein merged commit 81c6575 into swiftlang:main Dec 11, 2024
5 checks passed
@eeckstein eeckstein deleted the destroy-hoisting branch December 11, 2024 05:18
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.

1 participant