Skip to content

🍒[cxx-interop][IRGen] Do not try to retain/release a null pointer #73515

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
May 12, 2024

Conversation

egorzhdan
Copy link
Contributor

@egorzhdan egorzhdan commented May 8, 2024

Explanation: This teaches IRGen to only emit a lifetime operation (retain or release) for a C++ foreign reference type if the pointer is not nullptr.
Scope: IRGen of lifetime operations for C++ foreign reference types is changed.
Risk: Low, only affects C++ reference types, and only takes effect when C++ interop is enabled.
Testing: Added compiler tests.
Issue: rdar://97532642
Reviewer: @rjmccall @beccadax @zoecarver

Original PR: #73366

egorzhdan added 2 commits May 8, 2024 19:44
This teaches IRGen to only emit a lifetime operation (retain or release) for a C++ foreign reference type if the pointer is not `nullptr`.

Previously the compiler would in some cases emit a release call for `nullptr`, which breaks the assumption that the argument to a custom release function is `_Nonnull`. For instance:
```
var globalOptional: MyRefType? = nil
func foo() { globalOptional = MyRefType.create() }
```
When emitting IR for the assignment operation to `globalOptional`, the compiler would emit code to first retrieve the existing value of `globalOptional` and release it. If the value is `nil`, it does not need to be released.

rdar://97532642
(cherry picked from commit bd6da5d)
This also re-enables a test that was disabled since 2022.

rdar://97532642
(cherry picked from commit ef47dd6)
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label May 8, 2024
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan egorzhdan marked this pull request as ready for review May 8, 2024 18:49
@egorzhdan egorzhdan requested a review from a team as a code owner May 8, 2024 18:49
egorzhdan added 3 commits May 9, 2024 13:27
This adjusts the test to be correct on 32-bit platforms.

rdar://127795392
(cherry picked from commit d84cbac)
When compiling with optimizations, the compiler might reorder some instructions, leading to test failures.

rdar://127795755
(cherry picked from commit 27829b7)
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan egorzhdan merged commit 76fb0ee into release/6.0 May 12, 2024
@egorzhdan egorzhdan deleted the egorzhdan/6.0-frt-release-nullptr branch May 12, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants