Commit Graph

652 Commits

Author SHA1 Message Date
Doug Gregor
cab6bfa6af [ABI] Emit associated type witnesses resiliently.
Emit associated type witnesses into resilient conformance tables, so they
can be re-ordered within the protocol without breaking clients. This will
(eventually) permit adding new, defaulted associated types to protocols
resiliently.
2018-09-14 20:59:03 -07:00
Joe Groff
b4abe8503a Give opaque existential containers extra inhabitants.
We can use the extra inhabitants of the type metadata field as extra inhabitants of the entire
existential container, allowing `Any?` and similar types to be the same size as non-optional
existentials.
2018-09-14 12:07:58 -07:00
Slava Pestov
ca58db21b4 Runtime: Introduce swift_lookUpClassMethod() 2018-09-07 21:50:58 -07:00
Slava Pestov
b7449d5621 IRGen/Runtime: Method override descriptors 2018-09-04 14:46:34 -07:00
Slava Pestov
04143abaf1 Runtime: Refactor getVTableOffset() and getFieldOffsetVectorOffset()
Instead of taking class metadata as a parameter, we can recover
the immediate member offset from the descriptor itself.
2018-09-04 14:46:34 -07:00
Slava Pestov
fcbe997e72 IRGen/Runtime: Use method descriptors instead of dispatch thunks as keys in resilient witness tables 2018-08-31 00:16:22 -07:00
John McCall
140ee562d3 Merge pull request #18840 from rjmccall/dematerializeForSet
Replace materializeForSet with the modify coroutine
2018-08-27 21:02:24 -04:00
Joe Groff
75d9e567dd Merge pull request #18630 from jckarter/extra-inhabitants-from-any-fixed-layout-struct-field
IRGen: Use any field of fixed-layout structs for extra inhabitants.
2018-08-27 09:46:42 -07:00
John McCall
b80618fc80 Replace materializeForSet with the modify coroutine.
Most of this patch is just removing special cases for materializeForSet
or other fairly mechanical replacements.  Unfortunately, the rest is
still a fairly big change, and not one that can be easily split apart
because of the quite reasonable reliance on metaprogramming throughout
the compiler.  And, of course, there are a bunch of test updates that
have to be sync'ed with the actual change to code-generation.

This is SR-7134.
2018-08-27 03:24:43 -04:00
Slava Pestov
8be09fef74 IRGen/Runtime: Rename "InPlaceMetadata" to "SingletonMetadata"
It's not actually "in-place" for resilient classes, which have a
pattern with an allocation function.
2018-08-24 00:52:36 -07:00
Slava Pestov
03cb6d1ff4 IRGen/Runtime: Use a true-const pattern to initialize non-generic resilient class metadata
Previously we would emit class metadata for classes with resilient
ancestry, and relocate it at runtime once the correct size was known.

However most of the fields were blank, so it makes more sense to
construct the metadata from scratch, and store the few bits that we
do need in a true-const pattern where we can use relative pointers.
2018-08-23 23:40:08 -07:00
Slava Pestov
6150e34508 Runtime/IRGen: Two-phase metadata initialization for resilient classes
Similar to the non-resilient case, except we also emit a 'relocation
function'. The class descriptor now contains this relocation function
if the class has resilient ancestry, and the relocation function
calls the runtime's swift_relocateClassMetadata() entry point.

The metadata completion function calls swift_initClassMetadata() and
does layout, just like the non-resilient case.

Fixes <rdar://problem/40810002>.
2018-08-20 16:26:47 -07:00
Slava Pestov
50a037d8ed Runtime: Set the superclass in swift_initClassMetadata()
Now that we don't need the superclass before calling
swift_relocateClassMetadata(), it seems simpler to set it
here instead of doing it in various places in IRGen.
2018-08-20 16:23:07 -07:00
Slava Pestov
3256ee43c0 Runtime: swift_relocateClassMetadata() calculates metadata bounds from the class descriptor
Using the superclass metadata here no longer makes sense with two-phase
init, in case the superclass metadata depends on the class being
instantiated.

It would also be nice to rework the resilient class metadata 'pattern'
to be its own data structure that's true const, instead of just the
prefix of a real class metadata, but for now let's keep the existing
crappy design.
2018-08-20 16:23:07 -07:00
Slava Pestov
a86a71de1e Runtime/IRGen: Two-phase metadata initialization for non-resilient classes
Note that this patch also consolidates the recursive metadata tests
into one place while adding an execution test.
2018-08-20 16:23:07 -07:00
Slava Pestov
24a9a5156c IRGen: In-place initialization for classes with generic ancestry and resiliently-sized fields
If a class has generic ancestry or resiliently-sized fields, but is
itself not generic and does not have resilient ancestry, we must
perform runtime metadata initialization, but we can initialize
the metadata in-place.

As with generic classes or classes with resilient ancestry, we
copy generic requirements and field offset vectors from the
superclass. We also calculate the layout of the class's direct
fields at runtime.

Unlike the fully resilient case, we don't copy vtable entries
from the superclass, or install the direct class's vtable
entries from the type context descriptor. Instead, we statically
emit the vtable as with fixed-size class metadata.

Both the in-place and resilient case call the same runtime
entry point to initialize class metadata; the new HasStaticVTable
flag in ClassLayoutFlags is used to select between the two
behaviors concerning the vtable.
2018-08-20 16:22:35 -07:00
Joe Groff
c11aacc576 KeyPaths: Put an override shim on swift_getKeyPath.
This will let future compilers that support new key path features backward-deploy logic for interpreting new kinds of key path patterns.
2018-08-16 13:15:21 -07:00
Joe Groff
9f02ecd1a5 IRGen: Use any field of structs for extra inhabitants.
This allows us to layout-optimize Optional<T> when T is a struct with an
extra-inhabitant-bearing field anywhere in its definition, not only at
the beginning. rdar://problem/43019427
2018-08-14 12:53:06 -07:00
Joe Groff
fb05ede036 Runtime: Don't attempt to round-trip mangled names for private types.
By design, we don't want private or function-nested types to be accessible by mangled name, since they don't have stable identities, and they could inadvertently become ABI if someone serialized a mangled string and expected to deserialize it into a type. Fixes rdar://problem/39826794 .
2018-08-08 13:52:59 -07:00
John McCall
05c9671902 Change the ABI for the type descriptors of imported declarations.
- Instead of keeping multiple flags in the type descriptor flags,
  just keep a single flag indicating the presence of additional
  import information after the name.

- That import information consists of a sequence of null-terminated
  C strings, terminated by an empty string (i.e. by a double null
  terminator), each prefixed with a character describing its purpose.

- In addition to the symbol namespace and related entity name,
  include the ABI name if it differs from the user-facing name of the
  type, and make the name the user-facing Swift name.

There's a remaining issue here that isn't great: we don't correctly
represent the parent relationship between error types and their codes,
and instead we just use the Clang module as the parent.  But I'll
leave that for a later commit.
2018-08-01 18:37:08 -04:00
John McCall
436a8b273d Add runtime functions to compute tuple layouts from element layouts.
Previously, when a tuple type had non-fixed layout, we would compute
a layout by building the metadata for that tuple type and then
extracting the layout from the VWT.  This can be quite expensive
because it involves constructing the exact metadata for types like
arrays and functions despite those types being fixed-layout across
all instantiations.  It also tends to cause unnecessary recursive-type
issues, especially with enums where tuples are currently used to model
cases with mutliple payloads.  Since we just need a layout, computing
it directly from element layouts instead of constructing metadata for
the formal type lets us take advantage of all the other fast paths for
layout construction, e.g. for fixed types and single-field aggregates.

This is a good improvement overall, but it also serves to alleviate
some of the problems of rdar://40810002 / SR-7876 in a way that
might be suitable for integration to 4.2.
2018-07-29 18:27:27 -04:00
John McCall
db8f23df74 Update the ABI for uniquing foreign type metadata.
- `swift_getForeignTypeMetadata` is now a request/response function.

- The initialization function is now a completion function, and the
  pointer to it has moved into the type descriptor.

- The cache variable is no longer part of the ABI; it's an
  implementation detail of the access function.

- The two points above mean that there is no special header on foreign
  type metadata and therefore that they can be marked constant when
  there isn't something about them that needs to be initialized.

The only foreign-metadata initialization we actually do right now is
of the superclass field of a foreign class, and since that relationship
is a proper DAG, it's not actually possible to have recursive
initialization problems.  But this is the right long-term thing to do,
and it removes one of the last two clients of once-based initialization.
2018-07-29 03:16:35 -04:00
John McCall
d10239313f Reference runtime-only ObjC classes with bare strings.
As part of this, rename TypeMetadataRecordKind to TypeReferenceKind
and consistently give it three bits of storage.

The better modelling of these type references appears to have been
sufficient to make dynamic conformance checks succeed, which is good
but unexpected.
2018-07-27 22:55:22 -04:00
Davide Italiano
44cccd011e Merge pull request #18205 from dcci/valuewittarget
[Runtime] Targetize the layout of ValueWitnessTable.
2018-07-25 14:00:26 -07:00
Davide Italiano
1c3c1904a4 [Runtime] Targetize the layout of ValueWitnessTable.
From what I see the only fields are DATA_VALUE_WITNESS which
all have type size_t. I converted them to use the target-dependent
`StoredSize`. While I was around I fixed also isValueInline()
to do the right thing (it was using ValueBuffer instead of
TargetValueBuffer) and all the getters for the data value witnesses.

<rdar://problem/41546568>
2018-07-25 11:37:57 -07:00
John McCall
dadb51e708 Support in-place value metadata initialization in the runtime. 2018-07-25 03:00:36 -04:00
John McCall
e6fc5cb54e Refactor LockingConcurrentMap to allow it to not use a map; NFC. 2018-07-25 03:00:36 -04:00
Doug Gregor
a54a6d8d7f [ABI] Rework protocol descriptor metadata.
Reimplement protocol descriptors for Swift protocols as a kind of
context descriptor, dropping the Objective-C protocol compatibility
layout. The new protocol descriptors have several advantages over the
current implementation:

* They drop all of the unused fields required for layout-compatibility
  with Objective-C protocols.
* They encode the full requirement signature of the protocol. This
  maintains more information about the protocol itself, including
  (e.g.) correctly encoding superclass requirements.
* They fit within the general scheme of context descriptors, rather than
  being their own thing, which allows us to share more code with
  nominal type descriptors.
* They only use relative pointers, so they’re smaller and can be placed
  in read-only memory

 Implements rdar://problem/38815359.
2018-07-23 22:12:42 -07:00
Doug Gregor
bec722df57 [Runtime/IRGen] Switch swift_getExistentialTypeMetadata() to ProtocolDescriptorRef.
Switch one entry point in the runtime (swift_getExistentialTypeMetadata)
to use ProtocolDescriptorRef rather than a protocol descriptor. Update
IRGen to produce ProtocolDescriptorRef instances for its calls, setting
the discriminator bit appropriately.

Within the runtime, verify that all instances of ProtocolDescriptorRef have
the right layout, i.e., the discriminator bit is set for @objc protocols
but not Swift protocols.
2018-07-21 07:48:34 -07:00
Doug Gregor
3464929638 [ABI] Rework existential type metadata to use ProtocolDescriptorRef.
Use ProtocolDescriptorRefs within the runtime representation of
existential type metadata (TargetExistentialTypeMetadata) instead of
bare protocol descriptor pointers. Start rolling out the use of
ProtocolDescriptorRef in a few places in the runtime that touch this
code. Note that we’re not yet establishing any strong invariants on
the TargetProtocolDescriptorRef instances.

While here, replace TargetExistentialTypeMetadata’s hand-rolled pointer 
arithmetic with swift::ABI::TrailingObjects and centralize knowledge of
its layout better.
2018-07-20 20:54:49 -07:00
Jordan Rose
a2c1aa3624 [runtime] Backwards-deployment support for class_getImageName
Follow-up to 3ed3774e07. On Apple OSs that don't have the new
Objective-C runtime function 'objc_setHook_getImageName', override the
system definition of 'class_getImageName' by literally rewriting
symbol tables at run time.

Yes, you read that correctly.

The low-level part of this patch was written by Greg Parker, then
simplified and tweaked by me to fit the Swift coding style. Don't try
this at home; it comes with all sorts of caveats and won't actually
work on this year's iOS. (Fortunately we don't need it there, because
that will have the new ObjC entry point.)

The rest of the patch is pretty straightforward: the replacement
implementation calls the code that supports Swift objects (the same
code we use on newer OSs), which then chains back to the original
system implementation of class_getImageName. May we never have to
touch this again.

rdar://problem/41535552
2018-07-18 18:28:12 -07:00
Jordan Rose
3ed3774e07 Override ObjC's class_getImageName to handle Swift classes
This not only restores the correct behavior for classes with generic
ancestry, but also handles actual generic classes as well. (This is
the function that backs Foundation's Bundle.init(for: AnyClass)
initializer.)

https://bugs.swift.org/browse/SR-1917
rdar://problem/33450609&40367300
2018-07-12 15:48:58 -07:00
Mike Ash
b964cde3dc [Runtime] In various enumTagSinglePayload functions, don't read getExtraInhabitantIndex or storeExtraInhabitant unless it actually has extra inhabitants.
This code would previously read off the end of the allocated metadata to fetch these values. This was usually harmless, as the value was never used in that case. However, on rare occasions the metadata would be right before unmapped memory, and this read would crash trying to access that unmapped memory.

rdar://problem/39866044
2018-07-11 11:17:23 -04:00
Jordan Rose
2345a54ed2 Make sure artificial subclasses work with '===' and casting (#17765)
Dynamic subclasses aren't /really/ valid Swift type metadata, but
they can still be used as values of type AnyClass. Make sure we
don't assert when that happens.

No intended functionality change.
2018-07-05 15:54:45 -07:00
Ben Cohen
2b04e9f105 Suppress a number of warnings in no-assert builds (#17721)
* Supress a number of warnings about things used only in asserts

* Re-use a couple of variables instead of supressing the warning
2018-07-04 07:15:14 -07:00
Joe Groff
e2dd2ff11f Runtime: Handle synthesized decl "related entity" tags.
Clang-importer-synthesized declarations get an extra tag character included in their mangling, which was not being preserved in type context descriptors. This caused runtime lookup for these synthesized types to fail. Fix this by adding the tag information to type context descriptors and teaching the runtime to match it up when fetching metadata by mangled name. Fixes rdar://problem/40878715.
2018-06-28 12:11:12 -07:00
Lily Vulcano
ce6316ed51 Expose swift_getTypeContextDescriptor() on Darwin. 2018-06-15 13:36:26 -07:00
Lily Vulcano
4c857c5f49 Mark as API; use expanded-from-template return type. 2018-06-08 09:45:49 -07:00
Lily Vulcano
02bf6fd217 Expose SPI to get type descriptor for Bundle(for:) 2018-06-08 09:42:53 -07:00
Arnold Schwaighofer
98506e03cf IRGen: Opaque existentials are now bitwise takable
Now that the existential inline buffer is bitwise takable so are opaque
existentials.
2018-05-23 11:17:54 -07:00
Arnold Schwaighofer
6267860a7e IRGen: Remove initializeBufferWithTakeOfBuffer in favor of memcpy
And update the existential container's initializeWithTake implementation
in the runtime. After only allowing bitwise takable values in the
inline buffer we can use memcpy to move existential container values.

rdar://31414907
SR-343
2018-05-22 13:05:00 -07:00
Arnold Schwaighofer
298067496d ABI: Only store bitwise take-able values inline
SR-343
rdar://31414907
2018-05-21 14:02:12 -07:00
Joe Groff
681a96b45c Runtime: Tolerate unknown metadata kinds.
We want to be able to potentially introduce new metadata kinds in future Swift compilers, so a runtime ought to be able to degrade gracefully in the face of metadata kinds it doesn't know about. Remove attempts to exhaustively switch over metadata kinds and instead treat unknown metadata kinds as opaque.
2018-05-17 15:35:06 -07:00
Arnold Schwaighofer
b83941795a Unique synthesized foreign type conformances
- Add swift_getForeignWitnessTable to unique non-unique foreign type
   witness tables

 - IRGen: Call the foreign witness uniquing runtime function

rdar://24958043
2018-05-14 13:52:41 -07:00
Mike Ash
b821a2ef6d [Runtime] Silence warnings for offsetof usage on TupleCacheEntry.
rdar://problem/39739801
2018-05-09 13:51:59 -04:00
Mike Ash
09e1e355a4 [Runtime] Disable mangled name verification unless specifically enabled with an environment variable.
rdar://problem/39821779 rdar://problem/39821761 rdar://problem/39821758
2018-04-30 10:56:09 -04:00
Mike Ash
91619c4cd0 [Runtime] Make mangled name roundtrip verification a soft failure. Disable it entirely when running tests and when bootstrapping swiftpm, for now.
rdar://problem/37551850
2018-04-27 10:09:06 -04:00
Mike Ash
77e9b967f3 [Runtime] When asserts are enabled, verify that each newly created Metadata successfully roundtrips its mangled name through the demangler.
rdar://problem/37551850
2018-04-25 16:26:23 -04:00
Pavel Yaskevich
78822bc23e [IRGen] Adjust element size of offset vector to 32-bit for structs
Type of elements contained by field offsets vector can be adjusted
to 32-bit integers (from being pointer sized) to safe space in the
binary since segment size is limited to 4 GB.

Resolves: rdar://problem/36560486
2018-04-03 13:32:06 -07:00
John McCall
6c31586128 Add cyclic-metadata support to tuples.
I was going to put this off for awhile, but it turns out that a lot of
my testcases are enums with multi-payload cases, which we currently
compile as tuples, so they were all still hanging until this patch.
2018-04-01 19:23:57 -04:00