Skip to content

Optimizer: add simplification for begin_borrow #69820

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
Nov 14, 2023

Conversation

eeckstein
Copy link
Contributor

Try to replace a begin_borrow with its owned operand.

This is either possible if the borrowed value (or a forwarded value if it) is copied:

  %1 = begin_borrow %0
  %2 = struct_extract %1     // a chain of forwarding instructions
  %3 = copy_value %1
  // ... uses of %3
  end_borrow %1

->

  %1 = copy_value %0
  %3 = destructure_struct %0 // owned version of the forwarding instructions
  // ... uses of %3

Or if the borrowed value is destroyed immediately after the borrow scope:

  %1 = begin_borrow %0
  %2 = struct_extract %1     // a chain of forwarding instructions
  // ... uses of %2
  end_borrow %1
  destroy_value %1           // the only other use of %0 beside begin_borrow

->

  %2 = destructure_struct %0 // owned version of the forwarding instructions
  // ... uses of %2
  destroy_value %2

* `canForwardGuaranteedValues`
* `canForwardOwnedValues`
* `setForwardingOwnership`
* `Operand.canAccept(ownership:)`
Make filter APIs for UseList chainable by adding them to Sequence where Element == Operand

For example, it allows to write:
```
let singleUse = value.uses.ignoreDebugUses.ignoreUsers(ofType: EndAccessInst.self).singleUse
```

Also, add `UseList.getSingleUser(notOfType:)`
Try to replace a begin_borrow with its owned operand.

This is either possible if the borrowed value (or a forwarded value if it) is copied:

```
  %1 = begin_borrow %0
  %2 = struct_extract %1     // a chain of forwarding instructions
  %3 = copy_value %1
  // ... uses of %3
  end_borrow %1
```
->
```
  %1 = copy_value %0
  %3 = destructure_struct %0 // owned version of the forwarding instructions
  // ... uses of %3
```

Or if the borrowed value is destroyed immediately after the borrow scope:

```
  %1 = begin_borrow %0
  %2 = struct_extract %1     // a chain of forwarding instructions
  // ... uses of %2
  end_borrow %1
  destroy_value %1           // the only other use of %0 beside begin_borrow
```
->
```
  %2 = destructure_struct %0 // owned version of the forwarding instructions
  // ... uses of %2
  destroy_value %2
```
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

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.

@eeckstein this all looks good to me.

We were planing to enable borrow scope canonicalization as soon as someone has time to investigate it. But this is a nice way to express the single-value case in Swift source.

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.

2 participants