Skip to content

[4.2] Perform value operations opaquely on ABI-inaccessible types #16642

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 3 commits into from
May 16, 2018

Conversation

rjmccall
Copy link
Contributor

This is the swift-4.2-branch version of #16615.

Track whether types are fixed-ABI and ABI-accessible at the SIL level.

Fixed ABI means that we can do value operations on the type without any metadata: value-allocations, copies, and destroys. It's currently equivalent to being fixed-size, but (1) being fixed-size isn't useful by itself at the SIL level and (2) you can imagine resilience or generics micro-optimizations where there's like an attribute that tells us the size of a type without actually telling us how to copy it. All types are fixed-ABI except:

  • layout-unconstrained generic types,
  • resilient value types, and
  • value types which contain a subobject of such a type (except within indirect enum cases).

ABI-accessible means that it's legal to perform value operations on the type. We cannot perform value operations on a type T if it's not fixed-ABI and we can't construct metadata for it in the current file. The latter will be true if T is private to a different file (in non-WMO builds) or internal to a different module because the metadata-accessor symbol will be private or hidden, respectively. Importantly, in these cases, it always illegal to use T directly in the current file, which means that SIL generated from that source code won't contain any direct value operations on T. However, we may be able use some type C that's itself permitted to use T and which is accessible to the current file. If C contains subobjects of type T, value operations on C must perform value operations on T recursively. This is okay as long as we don't either:

  • inline code that contains value operations on T or
  • naively attempt to recursively expand a value operation on C into value operations on its subobjects.

(Note that we cannot simply treat C as an opaque type because we may need to perform operations on it that would not be legal on a totally opaque type, such as directly accessing a known-stored property. It is tempting to try to define that away by saying that we never use direct property access on such types and instead go through property accessors. However, this would require us to guarantee the emission of property accessors for properties, which we'd like to avoid. Also, it's not actually possible to define this problem completely, because the current file might define a type C2 which contains C as a subobject. The same recursive trouble applies to C2 as would apply to C, but we must be able to directly access the properties of C2 in order to define its constructors and accessors. Any model which can support directly accessing the properties of C2 can equally support it for C as well.)

The SIL optimizer currently never tries to expand value operations on objects in memory. (It does sometimes expand value operations on scalars, and this may be a problem for the opaque-values effort.) However, IRGen currently recursively expands value operations on non-resilient types. That is fixed as the final commit in this PR.

The SIL verification that I've added here is definitely incomplete.

Fixes rdar://39763787 / SR-7549.

@rjmccall
Copy link
Contributor Author

@swift-ci Please test.

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - ba18503dae6fdc2bc070495f1e5932e34e564b8d

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - ba18503dae6fdc2bc070495f1e5932e34e564b8d

rjmccall added 2 commits May 15, 2018 17:57
Fixed-ABI means that we can do value operations on the type without
any metadata: value-allocations, copies, and destroys.  It's currently
equivalent to being fixed-size, but (1) being fixed-size isn't useful
by itself at the SIL level and (2) you can imagine resilience or generics
micro-optimizations where there's like an attribute that tells us the
size of a type without actually telling us how to copy it.  All types
are fixed-ABI except:
  - layout-unconstrained generic types,
  - resilient value types, and
  - value types which contain a subobject of such a type (except within
    indirect enum cases).

ABI-accessible means that we can perform value operations at all.
We might not be able to if the type is not fixed-ABI and it is private
to a different file (in non-WMO builds) or internal to a different
module, because in such cases we will not be able to access its metadata.
In general, we can't use such types `T` directly, but we may be able to
use types `C` that contain such types as subobjects.  Furthermore, we
may be reasonably exposed to SIL that performs operations that treat `C`
as non-opaque, e.g. if `C` is frozen (as it will be by default for
modules in Swift 5).  We can still achieve correctness in these cases
as long as we don't either:
  - inline code that contains value operations on `T` or
  - attempt to recursively expand a value operation on `T` into value
    operations on its subobjects.

The SIL optimizer currently never tries to expand value operations on
objects in memory.  However, IRGen always recursively expands value
operations on frozen types; that will be fixed in a follow-up patch.

The SIL verification that I've added here is definitely incomplete.
@rjmccall rjmccall force-pushed the abi-accessibility-4.2 branch from ba18503 to 03677f5 Compare May 15, 2018 21:58
@rjmccall
Copy link
Contributor Author

@swift-ci Please test.

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - ba18503dae6fdc2bc070495f1e5932e34e564b8d

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - ba18503dae6fdc2bc070495f1e5932e34e564b8d

@rjmccall rjmccall merged commit 0e9fe96 into swiftlang:swift-4.2-branch May 16, 2018
@rjmccall rjmccall deleted the abi-accessibility-4.2 branch May 16, 2018 04:55
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