Skip to content

IRGen: Set a "not bitwise borrowable" bit in value witnesses for @_rawLayout types. #76688

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 1 commit into from
Sep 25, 2024

Conversation

jckarter
Copy link
Contributor

For types like Atomic and Mutex, we want to know that even though they are technically bitwise-takable, they differ from other bitwise-takable types until this point because they are not also "bitwise-borrowable"; while borrowed, they are pinned in memory, so they cannot be passed by value as a borrowed parameter, unlike copyable bitwise-takable types. Add a bit to the value witness table flags to record this.

Note that this patch does not include any accompanying runtime support for propagating the flag into runtime-instantiated type metadata. There isn't yet any runtime functionality that varies based on this flag, so that can be implemented separately.

rdar://136396806

@jckarter jckarter requested a review from rjmccall as a code owner September 25, 2024 00:16
@jckarter
Copy link
Contributor Author

@swift-ci Please test

…awLayout` types.

For types like `Atomic` and `Mutex`, we want to know that even though they are
technically bitwise-takable, they differ from other bitwise-takable types until
this point because they are not also "bitwise-borrowable"; while borrowed,
they are pinned in memory, so they cannot be passed by value as a borrowed
parameter, unlike copyable bitwise-takable types. Add a bit to the value witness
table flags to record this.

Note that this patch does not include any accompanying runtime support for
propagating the flag into runtime-instantiated type metadata. There isn't yet
any runtime functionality that varies based on this flag, so that can
be implemented separately.

rdar://136396806
@jckarter
Copy link
Contributor Author

@swift-ci Please test

@@ -3927,8 +3927,8 @@ namespace {
} else if (allSingleRefcount
&& ElementsWithNoPayload.size() <= 1) {
CopyDestroyKind = TaggedRefcounted;
} else if (this->EnumImplStrategy::BitwiseTakable == IsBitwiseTakable &&
Copyable == IsCopyable) {
} else if (this->EnumImplStrategy::BitwiseTakable == IsBitwiseTakableAndBorrowable
Copy link
Contributor

Choose a reason for hiding this comment

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

At first glance, I 'm not sure why CopyDestroyKind would be affected by BitwiseBorrowable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking that it isn't possible for a type to be Copyable and BitwiseTakable without being BitwiseBorrowable, so it didn't really matter which value I compared against here.

@@ -87,7 +87,8 @@ StructLayout::StructLayout(IRGenModule &IGM, std::optional<CanType> type,
if (rawLayout && type) {
auto sd = cast<StructDecl>(decl);
IsKnownTriviallyDestroyable = triviallyDestroyable;
IsKnownBitwiseTakable = bitwiseTakable;
// Raw layout types are never bitwise-borrowable.
IsKnownBitwiseTakable = bitwiseTakable & IsBitwiseTakableOnly;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this be more logically written
IsKnownBitwiseTakable = bitwiseTakable >= IsBitwiseTakableOnly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was trying to keep the patch size down, but now that IsBitwiseTakable_t is a tristate, I want to rip the Is off of these Is*BitwiseTakable names. The code here is masking out the IsBitwiseTakableAndBorrowable possibility since a raw layout type can be at most bitwise-takable without -borrowable.

IsKnownBitwiseTakable = likeFixedType->isBitwiseTakable(ResilienceExpansion::Maximal);
// Raw layout types are still never bitwise-borrowable.
IsKnownBitwiseTakable = likeFixedType->getBitwiseTakable(ResilienceExpansion::Maximal)
& IsBitwiseTakableOnly;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this be more logically written
>= IsBitwiseTakableOnly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above.

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.

Many thanks!

@jckarter jckarter merged commit 4e3f665 into swiftlang:main Sep 25, 2024
5 checks passed
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