Skip to content

6.1: [SILCombine] Fix apply(convert_function(@guaranteed)) with an @owned operand. #78529

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

Conversation

nate-chandler
Copy link
Contributor

Explanation: Fix SIL ownership invalidity introduced by SILCombine.

The pass rewrites applies of cast functions to applies of uncast functions, casting the arguments instead. When an argument is a value (not an address), the introduced cast consumes the value. If the argument is owned but the original apply did not consume it, it's incorrect to replace that original apply with a sequence instructions that do consume it (i.e. the cast). To avoid this, in this situation, borrow the value before casting it.

Also, enable this optimization for try_apply instructions.
Scope: Affects optimized code.
Issue: rdar://142570727
Original PR: #78507
Risk: Low.
Testing: Added tests.
Reviewer: Erik Eckstein ( @eeckstein ), Andrew Trick ( @atrick )

The operands to the original apply are cast via an ownership forwarding
instruction to the appropriate type for the rewritten apply.

```
%converted = convert_function %original to $(NewTy) -> ()
apply %converted(%operand)
```
->
```
%cast = cast %operand to $OriginalTy
apply %original(%cast)
```

Previously, when an original operand is owned but the new apply does not
consume that operand, the newly added cast would consume the original
operand (an owned value)--something the original code being replaced did
not do.

```
%converted = convert_function %original to $(NewTy) -> ()
apply %converted(%operand : @guaranteed)
// %operand remains available
```
->
```
%cast = cast %operand to $OriginalTy // consumes %operand!
apply %original(%cast : @guaranteed)
// %operand is not available!
```

This is incorrect for the complementary reasons that the result of the
cast is leaked and any uses of the original operand subsequent to the
new apply are uses-after-consume.

Here, this is fixed by borrowing the original operand before casting in
this case.

rdar://142570727
@nate-chandler nate-chandler changed the base branch from main to release/6.1 January 9, 2025 20:23
@nate-chandler nate-chandler marked this pull request as ready for review January 9, 2025 20:24
@nate-chandler nate-chandler requested a review from a team as a code owner January 9, 2025 20:24
On main type annotations are not always required but on 6.1 they are.
@nate-chandler nate-chandler force-pushed the cherrypick/release/6.1/rdar142570727 branch from 7ab91a0 to a9f4a38 Compare January 9, 2025 21:38
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler merged commit 2f7b5e4 into swiftlang:release/6.1 Jan 10, 2025
5 checks passed
@nate-chandler nate-chandler deleted the cherrypick/release/6.1/rdar142570727 branch January 10, 2025 04:39
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