Commit Graph

1047 Commits

Author SHA1 Message Date
Doug Gregor
f2dd852f94 [ABI] Optimize out the witness table pattern for resilient conformances.
Resilient conformances now put all witnesses into the resilient witness
table, so optimize away the witness table pattern for such cases. Teach
the runtime to fill in the protocol conformance descriptor, because that’s
the only bit of real information that would be in the pattern.

This is a minor optimization enabled by rdar://problem/46282080.
2018-12-03 20:16:18 -08:00
Doug Gregor
c0519c7185 [ABI] Put mangled inherited protocol witnesses into the resilient witness table
Emit mangled inherited protocol witnesses into the resilient witness table,
and realize them when we instantiate the resilient witness table. Don’t
put mangled inherited protocol witnesses into non-resilient witness tables:
there is no efficient way to make sure they are realized, so keep the
previous instantiation-function approach.

Implements the rest of rdar://problem/46282080.
2018-12-03 17:29:33 -08:00
Doug Gregor
e52a8838bb [IRGen] Enabled mangled inherited conformances for conditional conformances.
Now that the runtime fills in the private slots with conditional requirement
information, we can always use mangled names for inherited conformances.

Fixes rdar://problem/46282080.
2018-11-29 17:05:05 -08:00
Doug Gregor
19941d3494 [Runtime] Have the runtime copy instantiation arguments into private data.
Rather than having the witness table instantiation function copy the 
instantiation arguments that corresponding to conditional requirements
into the private area, have the runtime do it. We’re going to depend on
these lining up anyway (and the witness table can of course have *more*
private slots that it manages some other way), so centralize the code.

More of rdar://problem/46282080.
2018-11-29 17:05:05 -08:00
Doug Gregor
3fce389305 [IRGen] Emit base protocol witnesses as mangled names.
Rather than setting the witness tables for base protocols in a witness
table’s instantiation function, use mangled names as we do for
associated conformances. The runtime will eagerly realize them so clients
of witness tables are not affected.

For now, only do this for unconditional conformances: conditional
conformances won’t work yet because the instantiation function currently
copies the instantiation arguments (which satisfy the conditional
requirements) in to the private area. The runtime will need to take over
this responsibility for conditional conformances to work.

Part of rdar://problem/46282080.
2018-11-29 17:05:05 -08:00
John McCall
49ba9c59d1 Allow Error to conform to itself.
Most of the foundation for this was laid in earlier patches.
2018-11-17 02:51:45 -05:00
Doug Gregor
17699d4e33 [Metadata] Emit complete context descriptors for parents of anonymous contexts
When a (file)private entity occurs inside a generic context, we still need
information about the genericity of the enclosing context to demangle
to metadata. Emit complete context descriptors for parents of anonymous
contexts.

Fixes rdar://problem/46109026.
2018-11-16 21:34:32 -08:00
Doug Gregor
f759296cc8 [Keypaths] Encode generic environment in the key-path pattern.
Extend the key-path pattern with a representation of the generic environment
of the key-path, which includes the generic parameters and generic
requirements of the environment.
2018-11-16 10:13:06 -08:00
John McCall
1065f99c71 Assorted fixes for the self-conformance infrastructure 2018-11-15 22:41:58 -05:00
John McCall
6ffeb4d839 [NFC] Add PrettyStackTraceConformance and use it
Also allow the printFooDescription functions to suppress
the trailing newline.
2018-11-15 18:39:35 -05:00
John McCall
5553224fd4 Support the explicit representation of self-conformances.
Big, but actually NFC because we're never actually creating them.
2018-11-15 16:42:03 -05:00
Doug Gregor
dbe50c6015 [IRGen] Only witness tables that always require instantiation are constant.
The runtime can choose to re-use the pattern of a witness table if in fact
there is nothing dependent about it. Only mark the pattern as constant
when we know it has no inline caching behavior.
2018-11-12 19:59:49 -08:00
Doug Gregor
81610fdc02 [ABI] Use faux mangled names for associated conformances in witness tables
The current representation of an associated conformance in a witness
tables (e.g., Iterator: IteratorProtocol within a witness table for
Sequence) is a function that the client calls.

Replace this with something more like what we do for associated types:
an associated conformance is either a pointer to the witness table (once
it is known) or a pointer to a mangled name that describes that 
conformance. On first access, demangle the mangled name and replace the
entry with the resulting witness table. This will give us a more compact
representation of associated conformances, as well as always caching
them.

For now, the mangled name is a sham: it’s a mangled relative reference to
the existing witness table accessors, not a true mangled name. In time,
we’ll extend the support here to handle proper mangled names.

Part of rdar://problem/38038799.
2018-11-12 09:42:51 -08:00
Doug Gregor
f7b2522bdc [ABI] Retrieve all associated conformances via a runtime function.
Introduce a new runtime entry point, swift_getAssociatedConformanceWitness(),
which extracts an associated conformance witness from a witness table.
Teach IRGen to use this entry point rather than loading the witness
from the witness table and calling it directly.

There’s no advantage to doing this now, but it is staging for changing the
representation of associated conformances in witness tables.
2018-11-08 16:42:08 -08: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
0e817b4357 [IRGen] Narrow the “requires instantiation” bit for conformances.
The “requires instantiation” bit for protocol conformances that
might require runtime instantiation was set for essentially every
resilient conformance, so we would always be forced to create a
copy of the table. Narrow the “requires instantiation” bit to those
cases where there is something in the pattern that needs it to be
copied (for now, only associated type witnesses involving a type
parameter, which need to be cached differently). This optimizes
some narrow cases where we wouldn’t otherwise need a copy.
2018-10-24 21:28:56 -07:00
Doug Gregor
602b38e444 [IRGen] Centralize alignment/default type information in LinkEntity.
Simplify calls to getAddrOfLLVMVariableOrGOTEquivalent() and
getAddrOfLLVMVariable() by moving the computation of the alignment and
default type into LinkEntity.

Co-authored-by: Joe Groff <jgroff@apple.com>
2018-10-23 09:57:03 -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
Slava Pestov
73f1990bc2 AST: Use getSelfInterfaceType() instead of getProtocolSelfType() where possible
The former appears in the code base a lot more frequently than the
latter, which returns a GenericTypeParamType *. Use it only in places
where the more specific type is intended.
2018-10-15 20:34:08 -07:00
Doug Gregor
1c02bfe4e5 [IRGen] Remove dead code for creating resilient witness table globals.
Resilient witness tables are no longer separate entities.
2018-10-12 15:41:45 -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
df7744e484 Merge pull request #19480 from DougGregor/mangled-assoc-type-witness
[ABI] Use mangled names for associated type witnesses.
2018-10-01 06:57:45 -07:00
Doug Gregor
4444ff9294 [IRGen] Mark protocol conformance descriptors as true const. 2018-09-28 23:53:18 -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
Slava Pestov
ce770cdf4e AST: Introduce GenericSignature::forEachParam()
This replaces the inefficient pattern:

  for (auto param : sig->getGenericParams()) {
    if (sig->isCanonicalTypeInContext(param)) {
      ...
    } else {
      ...
    }
  }
2018-09-27 21:28:36 -07:00
Doug Gregor
0c3444337e [IRGen] Mark calls to swift_getAssociatedTypeWitness() nounwind/readnone.
swift_getAssociatedTypeWitness() is logically readnone because it is the
only function that accesses associated type witness information within the
witness table. Mark calls to it as readnone and nounwind.
2018-09-27 15:56:18 -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
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
d537249397 [IRGen] Remove all mention of (default|) associated type access functions.
Everything goes through swift_getAssociatedTypeWitness() now.
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
3b60ae153d AST: Rename AnyFunctionType::Param::getType() to getOldType() 2018-09-26 11:05:23 -07:00
Doug Gregor
789655836f [IRGen] Give property behavior conformances protocol conformance descriptors.
Protocol conformance descriptors are recorded in witness tables and are
needed to evaluate associated type witnesses. Start creating them for
property behavior conformances, but don’t put them in the section used
for reflection.
2018-09-25 20:49:22 -07:00
Arnold Schwaighofer
149b891785 IRGen: Witness method concrete wtable fulfillment
This is a rebase of John Mccall's patch from
https://github.com/rjmccall/swift/tree/concrete-wtable-fulfillment

SR-7657
rdar://40149340
2018-09-24 07:21:58 -07:00
Doug Gregor
b49780c67a [IRGen] Move the protocol conformance descriptor builder.
Place it right there next to the WitnessTableBuilder, because the
two should be the same thing. NFC
2018-09-21 11:27:34 -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
Doug Gregor
8a9df1ce34 [IRGen] Implement resilient access pattern for associated conformances.
When referencing an associated conformance in a witness table for a
resilient protocol, use the associated conformance descriptor to compute
the index into the witness table at run-time.

Another part of rdar://problem/44167982.
2018-09-17 17:26:05 -07:00
Doug Gregor
2ef9363bd1 [ABI] Add default associated type witnesses to resilient protocols.
When an associated type witness has a default, record that as part of
the protocol and emit a default associated type metadata accessor into the
default witness table. This allows a defaulted associated type to be
added to a protocol resiliently.

This is another part of rdar://problem/44167982, but it’s still very
limiting because the new associated type cannot have any conformances.
2018-09-15 22:04:46 -07:00
Doug Gregor
0fa1590e16 [IRGen] Fix associated type resilience for 32-bit platforms. 2018-09-14 22:29:38 -07:00
Doug Gregor
350391db9d [ABI] Use associated type descriptors for generic parameter references.
Generic parameter references, which occur in generic requirement
metadata, were hardcoding associated type indices. Instead, use
relative references to associated type descriptors and perform the
index calculation at runtime.

Associated types can now be reordered resiliently (without relying on 
sorting), which is the first main step toward rdar://problem/44167982.
2018-09-14 20:59:03 -07:00
Doug Gregor
6948073499 [IRGen] Simplify nondependent associated type witness metadata accessors.
When we’re creating an associated type witness metadata accessor for
resilience reasons, but the associated type witness doesn’t involve any
type parameters, directly form the type metadata reference (and don’t
cache it).

While here… update all of the IRGen/SILGen test cases perturbed by the
introduction of resilient associated type access patterns.
2018-09-14 20:59:03 -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
Doug Gregor
73f51a1a17 [IRGen] Implement resilient associated type witness access pattern.
When accessing the associated type witness metadata for a resilient
protocol, compute the index based on the difference between the
associated type’s descriptor and the protocol requirement base descriptor
to determine the offset into the witness table.
2018-09-14 20:59:03 -07:00
Slava Pestov
c3481ab8c3 IRGen: Refuse to compute witness table layouts of resilient protocols
It's not correct to depend on the order of entries in a witness table
if the protocol is resilient, so don't even try.
2018-09-07 11:22:35 -07:00
Doug Gregor
2e608547ba [IRGen] Use the requirement signature to determine dependent conformances.
The “inherited protocols” are as-written, rather than the minimized
set we should be using in IRGen.
2018-09-05 13:51:26 -07:00
Slava Pestov
b7449d5621 IRGen/Runtime: Method override descriptors 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
Slava Pestov
e78f16fb69 AST: ProtocolConformance::subst() doesn't need to take substType 2018-08-25 00:31:07 -07:00
Arnold Schwaighofer
622ccfaf6f Merge pull request #18760 from aschwaighofer/conditional_conf_instantiation
IRGen: Bind local type metadata from conformance's type in a conditional conformance's instantiation function
2018-08-17 06:06:38 -07:00