Commit Graph

133 Commits

Author SHA1 Message Date
Joe Groff
8e1e6665a7 IRGen: Start a type verifier to fuzz static type info against runtime type info.
We've had a rash of bugs due to inconsistencies between how IRGen and the runtime think types are laid out. Add a '-verify-type-layout' mode to the frontend that causes IRGen to emit a bunch of code that compares its static assumptions against what the runtime value witness does.

Swift SVN r24918
2015-02-03 16:47:25 +00:00
John McCall
6a91f7a172 Various improvements to the function-type ABI.
Teach IRGen and the runtime about the extra inhabitants
of function pointers, and take advantage of that in
thin and thick function types.

Also add runtime entrypoints for thin function type
metadata.

Swift SVN r24346
2015-01-10 01:45:37 +00:00
Nadav Rotem
d7e6386850 Revert "Get rid of a memory allocation on each call to the getXXXMetadata functions."
This reverts r23919.

Swift SVN r23951
2014-12-16 00:47:25 +00:00
Nadav Rotem
e9ffbd6707 Get rid of a memory allocation on each call to the getXXXMetadata functions.
We were using std::function that allocated memory. Unfortunately c++ lambdas
don't have a type so I had to make the function findOrAdd templated on the
type of the callback. This is not a big deal since the class is already
templated and there is a single call site.

Swift SVN r23919
2014-12-14 05:43:23 +00:00
Joe Groff
e665574c13 Runtime: Use size-specific value witnesses for common POD sizes.
We already have these lying around for the Builtin.Int* types, so let's use them.

Swift SVN r23618
2014-12-02 18:25:21 +00:00
Joe Groff
27f49cd19d Runtime: Optional-like enums with single-refcounted payloads can share the refcounted value witnesses.
swift_retain and objc_retain both handle nil already, so we can bypass the generic Optional value witnesses when instantiating Optional on a class type, which happens kind of a lot.

Swift SVN r23613
2014-12-02 17:34:36 +00:00
Joe Groff
d752636387 Runtime: Drop in POD value witnesses when generic structs or enums are instantiated.
If, after layout, we recognize that a generic struct or enum instance is POD, we can replace the generic value witnesses in the image with POD ones.

Swift SVN r23609
2014-12-02 03:33:40 +00:00
David Farler
3c4428dd78 Emit unique metadata for functions with inout
References to functions that take inout parameters crash the compiler
because InOutType isn't a "real" type in itself and has no special type
metadata to emit. It merely further qualifies the function's input
types.

For example, we would like to have a unique entry in the cache for:

var f: (T, T) -> ()

and

var f2: (inout T, T) -> ()

For each argument type metadata pointer in the function's input, take
advantage of pointer alignment and mark the lowest bit if it is inout.
Since the metadata cache uses pointers to create the key, this creates a
unique entry while still being able to extract the actual pointer.

This fixes <rdar://problem/17655125>, and a couple of other similar
crashes.

Swift SVN r23557
2014-11-22 22:01:23 +00:00
Nadav Rotem
22b5204719 Move the debug flag into the MetaCache implementation, where it is used.
Swift SVN r23336
2014-11-14 22:29:27 +00:00
Nadav Rotem
fd9824366c Move the DenseMap EntryRef entry into MetadataCache.
Swift SVN r23335
2014-11-14 22:29:27 +00:00
Nadav Rotem
70ce09c0f3 Refactor the metadata cache classes into a new file.
Swift SVN r23327
2014-11-14 20:28:07 +00:00
Nadav Rotem
1105667afd Refactor the lock implementation into a new file.
Swift SVN r23326
2014-11-14 20:28:03 +00:00
Joe Groff
32d148c807 Runtime: Improve type safety when working with witness tables.
Declare an opaque WitnessTable type instead of throwing void* around everywhere.

Swift SVN r23149
2014-11-07 01:17:51 +00:00
Greg Parker
e70ca2762d Runtime: Improve diagnostics for failed casts to ObjC class and protocol types.
Swift SVN r23146
2014-11-07 00:53:05 +00:00
Joe Groff
8ba1f42c63 Remove stray "#if 0 ||" and add missing else.
Swift SVN r23125
2014-11-06 01:32:11 +00:00
Joe Groff
763ce97193 Runtime: Tighten up barriers in foreign type metadata.
When there's no init function, we can do relaxed loads and stores, because the metadata record will not change in the course of canonicalization. However, if there is initialization, we need to do a release-consume in order to ensure the initialization is visible to readers.

Swift SVN r23119
2014-11-05 19:16:10 +00:00
Joe Groff
914f47ff47 Runtime: Take the lock off the hot path of getForeignTypeMetadata again.
The race here was benign--we just end up writing the same uniqued value twice to the invasive cache. Relax the assertion that this used to trip.

Swift SVN r23098
2014-11-04 06:16:00 +00:00
Joe Groff
679c66255f Runtime: Be more conservative in locking getForeignTypeMetadata.
The AtomicInt validation test exposed a race here when multiple threads fell into the slow path. Avoid this in the expedient way by locking before the fast path, which sucks but is at least correct.

Swift SVN r23086
2014-11-03 17:34:38 +00:00
Joe Groff
88ebe10a7a Runtime: Put some conservative locking on the foreign type uniquing cache.
Swift SVN r23082
2014-11-03 06:12:16 +00:00
Joe Groff
e773d9a1aa IRGen: Unique the metadata for imported structs and enums at runtime.
Move the uniquing information for ForeignTypeMetadata behind the address point so we can share the layout between foreign classes and the existing layout for struct and enum metadata. Emit metadata records for imported structs and enums as foreign metadata candidates, and dynamically unique references to the metadata by calling swift_getForeignTypeMetadata.

Swift SVN r23081
2014-11-03 06:12:13 +00:00
Joe Groff
01554448fc Runtime: Walk up superclass chains to find class conformances.
Swift SVN r23065
2014-11-02 15:41:00 +00:00
Joe Groff
139bf0323d Runtime: Cache nondependent generic witnesses for lookup.
We can share a lookup cache entry under the generic metadata pattern when the witness table for a protocol conformance is shared among all instances of the type. (This happens to always be the case currently.)

Swift SVN r23062
2014-11-02 01:17:14 +00:00
Joe Groff
2dc7678253 Runtime: Handle the easy cases for protocol lookup.
Set up the basic logic for first looking into a cache then pulling in conformances from enqueued images and trying again for exact-matchable types (pretty much just nongeneric native value types).

Swift SVN r23053
2014-11-01 02:46:52 +00:00
Joe Groff
6fd9724476 Runtime: We don't need a metadata cache for block types if we don't have ObjC interop.
Swift SVN r23018
2014-10-30 20:32:08 +00:00
John McCall
89e60f31aa Add protocol witness tables to existential metatype
layouts.  Introduce new SIL instructions to initialize
and open existential metatype values.

Don't actually, y'know, lift any of the restriction on
existential metatypes; just pointlessly burn extra
memory storing them.

Swift SVN r22592
2014-10-08 01:20:13 +00:00
Dmitri Hrybenko
6d7a95246a Runtime: move swift_getObjectType() to a C++ file
This function is not ObjC-specific, and it is used when no ObjC runtime
is present.

Swift SVN r22524
2014-10-05 00:55:25 +00:00
Dmitri Hrybenko
872a02f205 Runtime: don't use metadata for Builtin.UnknownObject when
!SWIFT_OBJC_INTEROP

Swift SVN r22510
2014-10-03 23:43:01 +00:00
Dmitri Hrybenko
f2abd76761 Runtime: untie Lazy<T> from libdispatch
Swift SVN r22429
2014-10-01 16:05:30 +00:00
Graham Batty
ac9e6d3696 stdlib: Include header for condition_variable
Swift SVN r22364
2014-09-29 21:14:37 +00:00
Joe Groff
93cb4dd83e Runtime: Remove dead HeapArrayMetadata kind.
Swift SVN r22294
2014-09-26 03:12:21 +00:00
Greg Parker
0aef787331 Use read-write locks to protect metadata caches.
Fixes rdar://18346143.


Swift SVN r21966
2014-09-16 02:48:14 +00:00
Joe Groff
1c4e08a9c8 Fix up a tangle of issues related to reabstraction.
- A spot fix in SILGen for reabstracting the result of a downcast, which fixes checked casts to function types.
- Associate the layout information in type metadata records with the most abstract representation of the type. This is the correct thing to do in cases where we need the metadata as a tag for an opaque value--if we store a value in an Any, or pass it as an unconstrained generic parameter, we must maximally reabstract it. This fixes the value semantics of existentials containing trivial metatypes.
- To ensure that we get runtime layout of structs and enums correct when they contain reabstractable types, introduce a "metadata for layout" concept, which doesn't need to describe the canonical metadata for the type, but only needs to describe a type with equivalent layout and value semantics. This is a correctness fix that allows us to correctly lay out generic types containing dependent tuples and functions, and although we don't really take advantage of it here, it's also a potential runtime performance win down the road, because we could potentially produce direct metadata for a primitive type that's layout-equivalent with a runtime-instantiated type. To aid in type safety here, push SILType deeper into IRGen in places where we potentially care about specific representations of types.
- Finally, fix an inconsistency between the runtime and IRGen's concept of what spare bits unmanaged references and thick metatypes have.

Together, these fixes address rdar://problem/16406907, rdar://problem/17822208, rdar://problem/18189508, and likely many other related issues, and also fixes crash suite cases 012 and 024.

Swift SVN r21963
2014-09-16 01:44:34 +00:00
Greg Parker
215d852f8d Fix cached metadata lookup for generics that have an address point offset.
Attempt to detect thread races in metadata caches.


Swift SVN r21962
2014-09-16 01:27:38 +00:00
John McCall
c1b03784df Instead of passing type metadata to swift_initClassMetadata,
pass the size and alignment of each field.  Take advantage
of this to pass a constant size and alignment when
possible.

This avoids the need to recursively find type metadata for
every field type, allowing generic recursively-structured
classes to be built.  There are a number of more complicated
cases that this approach isn't good enough for, but this
is good enough for now to fix rdar://18067671.

Also make an effort to properly support generic subclasses
of Objective-C classes.

Swift SVN r21506
2014-08-28 01:27:53 +00:00
John McCall
c367362f22 Properly flag opaque existential types as not being
bitwise-takable.

Swift SVN r20803
2014-07-31 02:03:56 +00:00
Greg Parker
fe8618a3d2 Scrap the custom allocator until we have time to fix it.
The allocator's crimes include:
* It uses OS SPI that must not be used by non-OS apps.
* It does not play well with memory debugging tools like Instruments.
* It does not return memory to the OS in response to memory pressure.
* It is less tested than we would like because many configurations 
  inadvertently turn it off (such as running from Xcode).
* Its per-thread magazine implementation does not actually work.
* Its "try alloc" flag is incompletely implemented and never used.
* Its "zero fill" flag is unimplemented and inconsistently used.



Swift SVN r20757
2014-07-30 14:11:40 +00:00
John McCall
80dd63b38d Don't allocate values out-of-line just because they're
not bitwise takable.

Finishes rdar://16755731

Swift SVN r20742
2014-07-30 08:24:18 +00:00
John McCall
584795e505 Replace the typeOf value witness with an
initializeBufferWithTakeOfBuffer value witness.

Attempt to use initializeBufferWithTakeOfBuffer in
some appropriate places.

There are some changes enabled by this which are
coming in a follow-up patch.

Swift SVN r20741
2014-07-30 08:24:12 +00:00
John McCall
934527a029 Eliminate all uses of the typeof value witness.
Swift SVN r20734
2014-07-30 04:40:58 +00:00
John McCall
64aa0ea950 When instantiating a generic class, compensate for
unexpected forematter from the superclass.

This requires a pretty substantial shift in the
generic-metadata allocation/initialization dance
because (1) we can't allocate class metadata without
knowing what the superclass is and (2) the offset
from the metadata cache entry to the address point is
no longer determined solely by the metadata pattern.

While I'm making invasive changes to metadata, fix
two race conditions in metadata creation.  The first
is that we need to ensure that only one thread succeeds
at lazily creating a generic-metadata cache.  The second
is that we need to ensure that only one thread actually
attempts to create a particular metadata; any others
should block until the metadata is successfully built.

This commit finishes rdar://17776354.  LLDB will
need to adjust to the runtime-private metadata layout
changes.

Swift SVN r20537
2014-07-25 10:08:51 +00:00
John McCall
a581511069 Permit LLVM's cast operators to be used on Metadata
instances, and add ExistentialTypeMetadata::isClassBounded().

Swift SVN r18814
2014-06-12 00:13:08 +00:00
John McCall
1f5fa04724 Move the rest of the dynamic-cast code over to Casting.cpp.
Swift SVN r18759
2014-06-09 21:42:30 +00:00
John McCall
fd9778165b Split the dynamic-cast code out into its own file.
Swift SVN r18756
2014-06-09 21:16:09 +00:00
Doug Gregor
fa71c0d0bb Teach _dynamicCastToExistential to check conformance to Objective-C protocols.
Narrowly address the problem of downcasting an array to an array of
Objective-C existentials by checking conformance for Objective-C
protocols. Addresses <rdar://problem/17165071>, but this area still
needs a lot of work.


Swift SVN r18745
2014-06-09 15:13:58 +00:00
Dmitri Hrybenko
02b977fab9 stdlib/runtime: fix a bug in dynamicCastToExistential1() that prevented print()
of values typed as Any to work

The cast used to return values with Existential metadata.

rdar://17026363


Swift SVN r18634
2014-05-26 17:56:29 +00:00
Joe Groff
8c60c4af1f IRGen/Runtime: Emit distinct type metadata for block types.
Blocks need their own type metadata with value witnesses appropriate to the block representation. Fixes <rdar://problem/16918740> and <rdar://problem/16981126>.

Swift SVN r18508
2014-05-21 20:24:11 +00:00
Greg Parker
e2ca13c993 <rdar://16529903> Make UIViewController's nib search find Swift class names.
UIViewController is able to find nib files that have names derived from the 
UIViewController subclass's name. This change modifies UIViewController's 
lookup so it is able to find nib files for Swift class names.


Swift SVN r18470
2014-05-20 23:14:52 +00:00
Dave Zarzycki
4c6b8d070f Runtime: Lock the metadata caches
This fixes: <rdar://problem/15889015>

Swift SVN r18434
2014-05-19 22:30:00 +00:00
Joe Groff
182986b595 Use swift_unknownRelease instead of swift_release in swift_bridgeNonVerbatimFromObjectiveC.
Swift SVN r18424
2014-05-19 18:08:24 +00:00
Dave Abrahams
bbbf548a28 [stdlib] Do verbatim bridging entirely in Swift
This not only removes some code, but should allow more optimization and
inlining in the common case where the Swift type is a class or @objc
existential such as AnyObject

Swift SVN r18237
2014-05-17 05:01:04 +00:00