Commit Graph

643 Commits

Author SHA1 Message Date
Slava Pestov
4d69e6f5b8 Runtime: Unconditionally realize superclass metadata in swift_updateClassMetadata()
Previously we were only doing this in assert builds, or if the
new Objective-C runtime metadata update hook mechanism was
available.

However, swift_checkMetadataState() gets called on the superclass
of a class, and it assumes that the metadata entry for the
superclass already exists in the singleton cache.

So even on an older runtime when there's no initialization work
to be done, we have to realize the superclass to populate the
singleton cache so that the check can succeed.

Fixes <rdar://problem/45569020>.
2018-10-26 20:40:27 -04:00
Slava Pestov
9024768b0e Runtime: Flesh out swift_updateClassMetadata() to use _objc_realizeClassFromSwift() 2018-10-26 16:54:23 -04:00
Doug Gregor
2668b52aa8 [Runtime] Unique foreign witness tables based on the conformance type descriptor. 2018-10-25 21:43:00 -07:00
Doug Gregor
933dece55d [Runtime] Minor simplification to swift_getWitnessTable(). 2018-10-25 21:43:00 -07:00
Doug Gregor
dd154f6668 [Runtime] Rename swift_instantiateWitnessTable() -> swift_getWitnessTable()
This runtime function doesn’t always perform instantiation; it’s how we
get a witness table given a conformance, type, and set of instantiation
arguments. Name it accordingly.
2018-10-25 20:35:27 -07:00
Doug Gregor
b5bc06e552 [ABI] Eliminate witness table accessors.
Witness table accessors return a witness table for a given type's
conformance to a protocol. They are called directly from IRGen
(when we need the witness table instance) and from runtime conformance
checking (swift_conformsToProtocol digs the access function out of the
protocol conformance record). They have two interesting functions:

1) For witness tables requiring instantiation, they call
swift_instantiateWitnessTable directly.
2) For synthesized witness tables that might not be unique, they call
swift_getForeignWitnessTable.

Extend swift_instantiateWitnessTable() to handle both runtime
uniquing (for #2) as well as handling witness tables that don't have
a "generic table", i.e., don't need any actual instantiation. Use it
as the universal entry point for "get a witness table given a specific
conformance descriptor and type", eliminating witness table accessors
entirely.

Make a few related simplifications:

* Drop the "pattern" from the generic witness table. Instead, store
  the pattern in the main part of the conformance descriptor, always.
* Drop the "conformance kind" from the protocol conformance
  descriptor, since it was only there to distinguish between witness
  table (pattern) vs. witness table accessor.
* Internalize swift_getForeignWitnessTable(); IRGen no longer needs to
  call it.

Reduces the code size of the standard library (+assertions build) by
~149k.

Addresses rdar://problem/45489388.
2018-10-25 20:35:27 -07:00
Doug Gregor
a0e3258ba6 [ABI] Collapse generic witness table into protocol conformance record.
Collapse the generic witness table, which was used only as a uniquing
data structure during witness table instantiation, into the protocol
conformance record. This colocates all of the constant protocol conformance
metadata and makes it possible for us to recover the generic witness table
from the conformance descriptor (including looking at the pattern itself).

Rename swift_getGenericWitnessTable() to swift_instantiateWitnessTable()
to make it clearer what its purpose is, and take the conformance descriptor
directly.
2018-10-22 23:36:31 -07:00
Doug Gregor
cae1c8cee3 [Runtime] Resolve symbolic references in type mangled name round-trip.
When performing the round-tripping verification for mangled type names,
make sure we resolve symbolic references to something
user-comprehensible that can be meaningfully rem angled.

Part of rdar://problem/37551850.
2018-10-16 21:42:02 -07:00
Doug Gregor
41891f6266 [ABI] Move the resilient witness table into the conformance descriptor.
Place resilient witnesses in the protocol conformance descriptor,
tail-allocated after the conditional requirements, so they can be found by
reflection. Drop the resilient witness table and protocol descriptor from
the generic witness table.

Addresses rdar://problem/45228582.
2018-10-12 15:36:20 -07:00
Doug Gregor
aba018c1e8 [ABI] Pass requirement base descriptor to swift_getAssociatedTypeWitness().
Have clients pass the requirement base descriptor to
swift_getAssociatedTypeWitness(), so that the witness index is just one
subtraction away, avoiding several dependent loads (witness table ->
conformance descriptor -> protocol descriptor -> requirement offset)
in the hot path.
2018-10-10 22:45:59 -07:00
Doug Gregor
2b44e8578f [ABI] Use mangled superclass names from class context descriptors.
Rather than rely on the metadata initialization function to compute and
fill in the superclass, use the mangled superclass name to construct the
superclass metadata.
2018-10-04 15:43:24 -07:00
Doug Gregor
0970ab9ef8 [Runtime] Check demangled superclass names against compiler-provided super.
Introduce a sanity check verifying that we can demangle the superclass
of a class when forming type metadata, and that the result matches the
compiler-provided superclass metadata.
2018-10-03 22:19:25 -07:00
Doug Gregor
a4778e1c0c [ABI] Only emit resilient superclass descriptor references in class metadata.
The superclass descriptor reference in class context descriptors is only used
for metadata bound computations when the superclass is resilient. Only
include the superclass descriptor reference when the class has a resilient
superclass, using a trailing record. It’s a tiny space savings for
classes that don’t have resilient superclasses.
2018-10-03 20:05:28 -07:00
Doug Gregor
9a403a0dae [Runtime] Only cache completed type metadata for associated type witnesses.
This is the scheme used for metadata caches elsewhere, and it
eliminates the need to check the metadata state along the hot path of
swift_getAssociatedTypeWitness().
2018-09-30 21:35:15 -07:00
Doug Gregor
65c0c842ed [ABI] Rework the tagging of default associated type witnesses.
Encode default associated type witnesses using a sentinel prefix byte
(0xFF) in the mangled name rather than as a second low bit on the
reference. Align all of the mangled names used for type references to
2 bytes (so we get that low bit regardless) and separate the symbol
names for default associated type witnesses vs. other kinds of
metadata or reflection metadata.
2018-09-28 23:38:38 -07:00
Doug Gregor
22808d1831 [Runtime] Make swift_getAssociatedTypeWitness() bounds checking an assertion. 2018-09-28 10:48:23 -07:00
Doug Gregor
441fee071a [Witness tables] Use a discriminator bit for default associated type witnesses
Indicate whether a particular associated type witness is a default (whose
mangled name is relative to the protocol) vs. being supplied as part of the
conformance (whose mangled name is relative to the conforming type). The
use of pointer identity to distinguish these cases can fail due to the
coalescing of these linker symbols.
2018-09-27 13:26:31 -07:00
Doug Gregor
5d2f55751a [Runtime] Substitute into associated types using the original conforming type
When producing an associated type witness from a mangled name, adjust the
conforming type metadata to find the original conforming type, which may
be a superclass of the conforming type given.
2018-09-26 23:19:34 -07:00
Doug Gregor
f64044e762 [Runtime] Improve fatal error when unable to demangle an associated type witness. 2018-09-26 23:19:33 -07:00
Doug Gregor
5f56d2faf9 [Runtime] Properly turn a const char* into a StringRef for a mangled name.
Use makeSymbolicMangledNameStringRef() to skip over null characters within
symbolic references.
2018-09-26 23:19:33 -07:00
Doug Gregor
c3d0ba8df4 [IRGen/Runtime] Witness tables with dependent associated types need instantiation.
Associated type witnesses in a witness table are cache entries, which are
updated by the runtime when the associated types are first accessed. The
presence of an associated type witness that involves type parameters requires
the runtime to instantiate the witness table; account for that in the runtime.
The presence of any associated type witness makes the witness table
non-constant.
2018-09-26 23:19:33 -07:00
Doug Gregor
b531b3923f [ABI] Use mangled names for associated type witnesses.
Rather than storing associated type metadata access functions in
witness tables, initially store a pointer to a mangled type name.
On first access, demangle that type name and replace the witness
table entry with the resulting type metadata.

This reduces the code size of protocol conformances, because we no
longer need to create associated type metadata access functions for
every associated type, and the mangled names are much smaller (and
sharable). The same code size improvements apply to defaulted
associated types for resilient protocols, although those are more
rare. Witness tables themselves are slightly smaller, because we
don’t need separate private entries in them to act as caches.

On the caller side, associated type metadata is always produced via
a call to swift_getAssociatedTypeWitness(), which handles the demangling
and caching behavior.

In all, this reduces the size of the standard library by ~70k. There
are additional code-size wins that are possible with follow-on work:

* We can stop emitting type metadata access functions for non-resilient
types that have constant metadata (like `Int`), because they’re only
currently used as associated type metadata access functions.
* We can stop emitting separate associated type reflection metadata,
because the reflection infrastructure can use these mangled names
directly.
2018-09-26 23:19:33 -07:00
Slava Pestov
4da47823a5 Runtime/IRGen: Add new initialization pattern for classes with backward deployment layout
If a class has a backward deployment layout:

- We still want to emit it using the FixedClassMetadataBuilder.

- We still want it to appear in the objc_classes section, and get an
  OBJC_CLASS_$_ symbol if its @objc.

- However, we want to use the singleton metadata initialization pattern
  in the metadata accessor.

- We want to emit metadata for all field types, and call the
  swift_updateClassMetadata() function to initialize the class
  metadata.

For now, this function just performs the idempotent initialization of
invoking a static method on the class, causing it to be realized with
the Objective-C runtime.
2018-09-23 21:26:46 -07:00
Slava Pestov
0088055f77 Runtime: Set up the class's runtime name in initGenericObjCClass() 2018-09-23 21:26:46 -07:00
Slava Pestov
0e4248375d Runtime: Introduce a new code path for initializing non-generic class metadata
It's a bit simpler because we don't have to change the ivar descriptors
back and forth.
2018-09-23 21:26:46 -07:00
Slava Pestov
62aecd31b7 Runtime: Split up swift_initClassMetadata()
- Rename _swift_initializeSuperclass() to copySuperclassMetadataToSubclass(),
- Factor out initClassFieldOffsetVector()
- Factor out initClassVTable()
- Factor out initGenericObjCClass()
2018-09-23 21:26:46 -07:00
Slava Pestov
6084dc285e Runtime: Only initialize metaclass superclass if superclass is generic
Otherwise, we emitted a static reference to its metaclass, so there's
no need to overwrite it here. This doesn't really improve anything,
it was just something I noticed while auditing this code in
preparation for a refactoring, so may as well fix it.
2018-09-23 21:26:46 -07:00
Slava Pestov
724830acd5 Runtime: Use the resilient metadata allocator instead of malloc() for resilient classes
Also move getResilientMetadataAllocator() and swift_relocateClassMetadata() to the top
of the "classes" section, instead of putting it in the middle.
2018-09-23 21:26:46 -07:00
Joe Groff
c87d2a8be2 Merge pull request #19340 from jckarter/generalize-tuple-extra-inhabitants
Generalize extra inhabitants of tuples.
2018-09-20 18:55:31 -07:00
Joe Groff
93d85997e8 Generalize extra inhabitants of tuples.
Like we did for structs, make it so that tuple types can also get extra inhabitants from whichever element with the most, not only the first. This lets us move all of the extra inhabitant handling functionality between structs and tuples in IRGen up to the common RecordTypeInfo CRTP base.
2018-09-20 15:39:44 -07:00
Doug Gregor
72ff34e937 Merge pull request #19410 from DougGregor/runtime-remove-duplicate-witnes-check
[Runtime] Remove duplicate witness checking for now.
2018-09-19 22:26:15 -07:00
Doug Gregor
cc32f50691 [Runtime] Remove duplicate witness checking for now.
It's breaking on SwiftPM, so remove it for now and follow-up to
investigate with rdar://problem/44627274.
2018-09-19 20:16:52 -07:00
swift-ci
922c4f0808 Merge pull request #19403 from DougGregor/runtime-remove-warning 2018-09-19 19:12:24 -07:00
Doug Gregor
93f9dc8e76 [Runtime] Remove warning about "missing" witness table entries.
It introduces spurious results.
2018-09-19 15:41:33 -07:00
swift-ci
e5c9d6417e Merge pull request #19398 from DougGregor/resilient-table-init-empty-proto 2018-09-19 15:18:04 -07:00
Doug Gregor
3ed69d4ee7 [Runtime] Instantiate witness tables even with no resilient witnesses.
The witness table for an empty, resilient protocol might need to be
instantiated, if a newer version of the protocol contains defaulted
associated type requirements. In such cases, we would either fail to
instantiate or assert in the runtime. Replace the assertion with a
proper check (to require instantiation in such cases) and cope with
filling in defaults even when the provided generic witness table has
no resilient witnesses.
2018-09-19 14:00:28 -07:00
Doug Gregor
32fd274f5e Merge pull request #19391 from DougGregor/assoc-conformance-default-witnesses
[ABI] Associated conformance defaults
2018-09-19 13:15:46 -07:00
Doug Gregor
ef0aeed788 [Runtime] Reimplement initialization of resilient witness tables.
Replace the quadratic algorithm (currently O(m*n) where m is the number of requirements and n is the number of witnesses) with an O(m+n) algorithm.
Harden the algorithm a against bad and unexpected inputs:
* Fail if the requirement descriptor is out-of-bounds for the protocol
* Skip the witness if the requirement descriptor is null; this can happen
when the witness table was compiled against a newer version of the
protocol, but is deployed to a library containing an older version of the
protocol. It is expected.
* In debug builds of the runtime, complain if an entry is initialized twice
* In debug builds of the runtime, complain if an entry is NULL

Fixes rdar://problem/44434793, which covers the second bullet (backward 
deployment).
2018-09-19 11:46:29 -07:00
Doug Gregor
d076e41f32 [IRGen] Put associated conformance accessors in resilient witness table
For a resilient conformance, emit the associated conformance accessor
functions into the resilient witness table (keyed on the associated
conformance descriptor) rather than in the fixed part of the witness
table. This is another part of resilience for associated conformances,
and a step toward defaults for associated conformances.
2018-09-17 21:58:56 -07:00
Joe Groff
08419544e4 Fix linker error in debug builds. (#19355) 2018-09-17 16:41:36 -07:00
Joe Groff
fbd21e1bd4 Merge pull request #19296 from jckarter/opaque-existential-extra-inhabitants
Give opaque existential containers extra inhabitants.
2018-09-17 10:48:20 -07:00
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