Commit Graph

925 Commits

Author SHA1 Message Date
John McCall
0bb884546b Add a runtime interface for working with Builtin.IntegerLiteral values. 2018-10-31 04:58:18 -04:00
Slava Pestov
4cecc268dc IRGen: Emit Objective-C metadata update callbacks 2018-10-26 16:54:23 -04: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
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
Slava Pestov
87ec607233 IRGen: Add a new 'Legacy' lowering mode that loads type info from a YAML file
The YAML format is the same one produced by the -dump-type-info
frontend mode.

For now this is only enabled if the -read-type-info-path frontend
flag is specified.

Progress on <rdar://problem/17528739>.
2018-10-04 23:33:17 -04:00
Slava Pestov
c969abdf1d IRGen: Remove some unused and used-once functions
A few utility methods would bypass computing the TypeInfo for a tuple,
but they were only used in assertions or used in places where I can't
imagine tuples coming up often enough for it to matter.
2018-10-04 20:01:23 -04: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
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
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
Jordan Rose
de07fdfb04 Remove "StartElem" from perform{SIL,IR}Generation
This was only used by the integrated REPL, and is now a dead option.

The "StartElem" option for performTypeChecking is still used for
reading SIL files, which have AST and SIL blocks alternate.
2018-09-25 09:13:52 -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
fb62977c2b [IRGen] Emit default associated conformance witnesses.
For a resilient protocol that has defaulted associated types, emit
default associated conformance witnesses that compute associated
conformances based on that default witness.

This completes the implementation of resilience protocols that
add new, defaulted associated types, rdar://problem/44167982.
2018-09-19 10:56:20 -07:00
Jordan Rose
d412f4b4ba [IRGen] Adopt std::unique_ptr for IRGenDebugInfo (#19369) 2018-09-18 11:10:10 -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
4549fcd673 [ABI] Add associated conformance descriptors.
Associated conformance descriptors are aliases that refer to associated
conformance requirements within a protocol descriptor’s list of
requirements. They will be used to provide protocol resilience against
the addition of new associated conformance requirements (which only makes 
sense for newly-introduced, defaulted associated types).
2018-09-17 16:32:29 -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
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
Doug Gregor
bbe56b284a [ABI] Add protocol requirements base descriptor.
Introduce an alias that refers one element prior to the start of a
protocol descriptor’s protocol requirements. This can be subtracted from
an associated type descriptor address to determine the offset of the
associated type accessor within a corresponding witness table. The code
generation for the latter is not yet implemented.
2018-09-14 20:59:03 -07:00
Doug Gregor
9873d52814 [ABI] Emit associated type descriptors referencing each requirement.
Emit associated type descriptors (as aliases) to reference each associated
type requirement within a resilient protocol.
2018-09-14 20:59:03 -07:00
swift-ci
cc329fee03 Merge pull request #19141 from aschwaighofer/remove_constant_string_literal 2018-09-10 15:51:47 -07:00
Slava Pestov
9347a072e5 IRGen: Emit method lookup function 2018-09-07 21:50:58 -07:00
Slava Pestov
100679aa1e IRGen: Use a method descriptor to identify methods where possible
This eliminates the 'non-resilient class' case here entirely since
all class methods have method descriptors.
2018-09-07 12:15:24 -07:00
Arnold Schwaighofer
73df12c09f Remove dead constant_string_literal
constant_string_literal was added to support a one word representation
of String that never materialized.
2018-09-05 12:13:57 -07:00
Slava Pestov
b7449d5621 IRGen/Runtime: Method override descriptors 2018-09-04 14:46:34 -07:00
Slava Pestov
44c4497ac5 IRGen/Runtime: Protocol requirement descriptors don't need to reference the dispatch thunk 2018-08-31 00:16:22 -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
771eacb4df IRGen: Emit method descriptors 2018-08-31 00:20:39 -06:00
Slava Pestov
dd19b69bc4 IRGen: Factor out part of defineTypeMetadata()
When we define type metadata, the 'full' symbol points at the
entire struct, whereas the 'address point' symbol points at an
offset inside.

The logic for setting this up is a bit tricky because the
'address point' symbol may have been forward declared, and
has to be replaced.
2018-08-31 00:20:38 -06:00
John McCall
c1f99b5fdb Cap type alignment in Swift at 16.
rdar://31411216
2018-08-28 16:13:50 -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
120be251db IRGen: Metadata patterns are always true-const
They were, already, but remove the isConstant parameter to
getAddrOfTypeMetadataPattern(), and just assert that its true for
patterns in defineTypeMetadata() instead.

Also, metadata patterns are i8*, not i8**. In fact they don't contain any
absolute pointers at all.

Should be NFC other than the LLVM type change.
2018-08-23 23:40:08 -07:00
Slava Pestov
d7a9a2a010 IRGen: Remove unused 'isIndirect' parameter to defineTypeMetadata() 2018-08-21 22:29:16 -07:00
Joe Groff
6f45c2a36e IRGen/Runtime: Reference field descriptor directly from type context descriptors.
This saves us some expensive cross-referencing and caching in the runtime, and lets us reclaim the `isReflectable` bit from the context descriptor flags (since a null field descriptor is a suitable and more accurate indicator of whether a type is reflectable).
2018-08-15 14:19:53 -07:00
Jordan Rose
84f471b031 [IRGen] Handle ProtocolInfo for protocols whose members aren't used (#18692)
Certain uses of protocols only formally need the requirement
signature, not any of the method requirements. This results in IRGen
seeing a protocol where none of the members have been validated except
the associated types. Account for this by allowing ProtocolInfo to
only contain the layout for the base protocols and associated types,
if requested.

Note that this relies on the layout of a witness table always putting
the "requirement signature part" at the front, or at least at offsets
that aren't affected by function requirements.

rdar://problem/43260117
2018-08-14 11:10:02 -07:00
Jordan Rose
d52dad3732 [IRGen] Extract getConformance from ProtocolInfo (#18681)
We were using this just as a convenient way to share an existing
DenseMap, but it's not really related; we don't need to compute
witness table layout just to generate a conformance reference.

I started working on this because the "Cub" source compat project was
hitting issues here, but now I can't reproduce it. Still, this is a
reasonable cleanup.
2018-08-13 17:16:41 -07:00
Doug Gregor
4d39506c4a [IRGen] Use a more precise computation for the kind of reference count.
TypeBase::usesNativeReferenceCounting() was doing a lot of work to
find the class that a type refers to, then determine whether it
would use the native reference-counting scheme. Its primary caller
in IRGen would use an overly-conservative approximation to decide
between the “Objective-C” and “unknown” cases, which resulted in
uses of “unknown” reference counting for some obviously-ObjC cases
(e.g., values of “NSObject”).

Moreover, the approximation would try to call into the type checker
(because it relied unnecessarily on the superclass *type* of a class
declaration), causing an assertion.

Fixes rdar://problem/42828798.
2018-08-06 17:04:47 -07:00
Doug Gregor
3c1f6f72c9 [IRGen] Only emit conformances local to a nominal type along with its metadata.
Rather than using getAllConformances() to emit all conformances for a
nominal type whenever we emit its type metadata, use
getLocalConformances() consistently--on the nominal type and on any
extension--to emit the conformances in the appropriate source files.
2018-08-03 09:23:26 -07:00
Slava Pestov
bb59562192 IRGen: Create a new IRGenModule constructor for dummy instances 2018-07-30 18:37:09 -07: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
John McCall
dc052e6364 Resolve metadata cycles through non-generic value types with resilient layout.
The central thrust of this patch is to get these metadata initializations
off of `swift_once` and onto the metadata-request system where we can
properly detect and resolve dependencies.  We do this by first introducing
runtime support for resolving metadata requests for "in-place"
initializations (committed previously) and then teaching IRGen to actually
generate code to use them (this patch).

A non-trivial amount of this patch is just renaming and refactoring some of
existing infrastructure that was being used for in-place initializations to
try to avoid unnecessary confusion.

The remaining cases that are still using `swift_once` resolution of
metadata initialization are:

- non-generic classes that can't statically fill their superclass or
  have resilient internal layout

- foreign type metadata

Classes require more work because I'd like to switch at least the
resilient-superclass case over to using a pattern much more like what
we do with generic class instantiation.  That is, I'd like in-place
initialization to be reserved for classes that actually don't need
relocation.

Foreign metadata should also be updated to the request/dependency scheme
before we declare ABI stability.  I'm not sure why foreign metadata
would ever require a type to be resolved, but let's assume it's possible.

Fixes part of SR-7876.
2018-07-25 15:21:55 -04:00
Doug Gregor
a2b2798de2 [ABI] Eliminate the now-unused protocol requirement array.
This is a holdover from the old protocol descriptor layout, which is no
longer useful.
2018-07-24 17:33:16 -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