Commit Graph

643 Commits

Author SHA1 Message Date
Slava Pestov
c35d9bd092 IRGen: Simplify generated code for generic class metadata instantiation
Move the following from IRGen to runtime:

- Copying generic parameters from superclass to subclass
- Copying field offsets from superclass to subclass
- Initializing the Objective-C runtime name of the subclass

This eliminates some duplication between the generic subclass and
concrete subclass of a generic class cases.

Also this should reduce generated code size and have no impact on
performance (the instantiation logic only runs once per substituted
type).
2015-12-22 02:09:03 -08:00
Slava Pestov
1368db7872 IRGen: Fix layout of concrete subclasses of generic subclasses of imported Objective-C classes
class B<T> : NSFoo {}
class A : B<Int> {}

IRGen computes the ivar layout starting from offset zero, since
the size of the 'NSFoo' is unknown and we rely on the Objective-C
runtime to slide the ivar offsets.

The instantiated metadata for B<Int> would contain a field offset
vector with the correct offsets, because of how
swift_initClassMetadata_UniversalStrategy() works.

However, A's metadata is emitted statically, and this includes a
copy of the field offset vector from the superclass. A's metadata
was initialized by swift_initializeSuperclass(), which did not
copy the field offset vector over from A<Int>. And since the
Objective-C runtime only slides the immediate ivars of a class,
the field offsets corresponding to A<Int>'s fields in B's type
metadata were never slid, resulting in problems when an instance
of B was passed to a function operating on an A<T> generically.

Fixes <rdar://problem/23200051>.
2015-12-21 16:23:39 -08:00
Joe Groff
fd457fb343 Revert "IRGen/Runtime: Use relative addresses in nominal type descriptors."
This reverts commit fbb832665a. It causes LLVM to complain with
"Cannot represent a subtraction with a weak symbol" when targeting Linux.
2015-12-11 15:41:11 -08:00
Joe Groff
fbb832665a IRGen/Runtime: Use relative addresses in nominal type descriptors.
Decrease the size of nominal type descriptors and make them true-const by relative-addressing the other metadata they need to reference, which should all be included in the same image as the descriptor itself. Relative-referencing string constants exposes a bug in the Apple linker, which crashes when resolving relative relocations to coalesceable symbols (rdar://problem/22674524); work around this for now by revoking the `unnamed_addr`-ness of string constants that we take relative references to. (I haven't tested whether GNU ld or gold also have this problem on Linux; it may be possible to conditionalize the workaround to only apply to Darwin targets for now.)
2015-12-11 15:21:12 -08:00
Max Moiseev
d610fa0d1c Merge remote-tracking branch 'origin' into swift-3-api-guidelines 2015-12-10 10:29:52 -08:00
Dmitri Gribenko
feacbc4433 Rename ErrorType to ErrorProtocol 2015-12-09 17:12:19 -08:00
Andrew Trick
a98de1ba1c Add an Optional metadata kind for runtime casts.
Reuses the enum metadata layout and builder because most of the logic is
also required for Optional (generic arg and payload). We may want to
optimize this at some point (Optional doesn't have a Parent), but I
don't see much opportunity.

Note that with this approach there will be no change in metadata layout.
Changing the kind still breaks the ABI of course.

Also leaves the MirrorData summary string as "(Enum Value)". We should
consider changing it.
2015-12-09 15:01:33 -08:00
Joe Groff
a3b6bef4e5 Runtime: Use new(InPlace) in more places to avoid unwanted null checks. 2015-11-17 14:13:48 -08:00
Joe Groff
385f4cc38c Demangle: Use our own isDigit instead of isdigit.
Darwin's isdigit is strangely heavy, looking up traits in _DefaultRuneLocale instead of a simple comparison pair.
2015-11-17 14:13:48 -08:00
Slava Pestov
6057ede7d1 Runtime: Add new swift_getResilientMetadata() entry point 2015-11-16 11:11:07 -08:00
Joe Groff
69a206229d Runtime: Start splitting out stubs only needed by the standard library.
Set up a separate libSwiftStubs.a archive for C++ stub functionality that's needed by the standard library but not part of the core runtime interface. Seed it with the Stubs.cpp and LibcShims.cpp files, which consist only of stubs, though a few stubs are still strewn across the runtime code base.
2015-11-11 17:28:57 -08:00
Michael Gottesman
654ca4e0c5 Move Lazy.h from stdlib/public/runtime => include/swift/Basic
I am going to use this in a unittest for BlotMapVector.
2015-11-02 09:22:30 -08:00
Michael Gottesman
e696331e06 Fix a duplicate symbol linker error by making an inline definition out of line.
Swift SVN r32801
2015-10-21 21:45:14 +00:00
Joe Groff
42c71b7972 Don't mangle directness into type metadata symbols.
Anywhere we can't directly address type metadata in Swift, we've found we need a function call. Directness isn't useful here.

Swift SVN r32626
2015-10-12 17:22:40 +00:00
Roman Levenstein
0f7722eec6 [runtime-lib] Any Swift mmap /vm_allocate allocations should use a custom tag.
We now have dedicated tags for Swift on Apple operating systems. They are called: VM_MEMORY_SWIFT_RUNTIME and VM_MEMORY_SWIFT_METADATA.
Let's make use of them. This would allow tools like Instruments provide a more detailed analysis and statistics.

For now, we only tag the metadata allocations, as this is the only place where we use mmap at the moment.

rdar://22376248

Swift SVN r32584
2015-10-09 22:28:41 +00:00
Roman Levenstein
18057b65a9 Get rid of static. NFC.
Swift SVN r31392
2015-08-21 21:21:08 +00:00
Roman Levenstein
19155fcd69 Change #if __APPLE__ to #if defined(__APPLE__). NFC.
This is required, because  __APPLE__ can be either defined to 1 or not defined at all.

Swift SVN r31389
2015-08-21 20:43:21 +00:00
Roman Levenstein
b7e023c2a7 [swift-runtime] Use vm_page_mask if we are compiling for an Apple OS.
Thanks to this, we can get rid of these two dirty symbols:
19f0f24e0 __ZZN17MetadataAllocator5allocEmE12pagesizeMask 8
19f0f24e8 __ZGVZN17MetadataAllocator5allocEmE12pagesizeMask 8

This is part of the effort to put the stdlib into a shared cache.

rdar://22375554

Swift SVN r31388
2015-08-21 19:57:03 +00:00
Joe Groff
ec61fa4c5a IRGen/Runtime: Use only the 'layout' subset of the vwtable to perform value type layout.
Full type metadata isn't necessary to calculate the runtime layout of a dependent struct or enum; we only need the non-function data from the value witness table (size, alignment, extra inhabitant count, and POD/BT/etc. flags). This can be generated more efficiently than the type metadata for many types--if we know a specific instantiation is fixed-layout, we can regenerate the layout information, or if we know the type has the same layout as another well-known type, we can get the layout from a common value witness table. This breaks a deadlock in most (but not all) cases where a value type is recursive using classes or fixed-layout indirected structs like UnsafePointer. rdar://problem/19898165

This time, factor out the ObjC-dependent parts of the tests so they only run with ObjC interop.

Swift SVN r30266
2015-07-16 15:38:17 +00:00
Ted Kremenek
a3d88266b2 Revert "IRGen/Runtime: Use only the 'layout' subset of the vwtable to perform value type layout."
This reverts commit r30243.

This appears to be breaking the Linux build.

Swift SVN r30253
2015-07-16 06:28:24 +00:00
Joe Groff
2641d566ac IRGen/Runtime: Use only the 'layout' subset of the vwtable to perform value type layout.
Full type metadata isn't necessary to calculate the runtime layout of a dependent struct or enum; we only need the non-function data from the value witness table (size, alignment, extra inhabitant count, and POD/BT/etc. flags). This can be generated more efficiently than the type metadata for many types--if we know a specific instantiation is fixed-layout, we can regenerate the layout information, or if we know the type has the same layout as another well-known type, we can get the layout from a common value witness table. This breaks a deadlock in most (but not all) cases where a value type is recursive using classes or fixed-layout indirected structs like UnsafePointer. rdar://problem/19898165

Swift SVN r30243
2015-07-16 01:28:42 +00:00
Joe Groff
7e78f0d3dc Add missing return.
Swift SVN r30076
2015-07-10 18:51:09 +00:00
Joe Groff
a1a00e7a24 Runtime: Fix incorrect implementation of pod_indirect_initializeBufferWithTakeOfBuffer.
Fixes rdar://problem/21375421.

Swift SVN r30073
2015-07-10 17:23:30 +00:00
Joe Groff
16c8a3b6c0 Runtime: Fix crash when first allocation for a metadata cache is page-sized or greater.
We incorrectly tested the uninitialized "next" pointer against MAP_FAILED, instead of the real result of mmap. Fixes rdar://problem/21659505.

Swift SVN r30030
2015-07-09 18:19:59 +00:00
Mark Lacey
081425fa96 Small clean-up.
Consistently use the local that has been initialized with the witness
table flags rather than sometimes using it, and sometimes not.

Swift SVN r29985
2015-07-08 19:45:28 +00:00
Joe Groff
8ec59e5c11 Runtime: Implementation for generic typed boxes.
Provide new swift_{alloc,dealloc,project}Box2 entry points that allocate, project, and deallocate typed boxes using runtime-instantiated metadata. Give these a new metadata kind, so that external tools recognize the difference and can interpret the metadata appropriately.

Swift SVN r29714
2015-06-26 00:06:17 +00:00
Joe Groff
1ab995b477 Runtime: Don't try to reuse integer value witnesses for under-aligned tuples.
Fixes rdar://problem/20891471.

Swift SVN r29308
2015-06-04 21:24:45 +00:00
Joe Groff
c97ba8e914 IRGen: Allow concrete subclasses of generic base classes.
Now that we have lazy metadata accessors for classes and vtable thunking, we don't have any reason to prevent concrete subclasses of generic base classes. Wire up IRGen to lazily instantiate the superclass for concrete derived classes when their metadata is accessed, using a runtime function that installs all the necessary pointers and metadata and registers the fully-initialized class with the ObjC runtime.

Swift SVN r28520
2015-05-13 18:54:03 +00:00
Joe Groff
9b4dc24721 Runtime: (Almost) cleanse of global constructors and destructors.
Configure the runtime to build with -Wglobal-constructors, and Lazy-fy almost everything that gets flagged. (I gave "swift_isaMask" a pass since that's almost definitely hot enough to warrant a static initialization.) Make some improvements to the Lazy wrapper, using aligned_storage to ensure that it's trivially constructed and destructed.

Swift SVN r28199
2015-05-06 04:59:56 +00:00
Joe Groff
498a71ceed Runtime: Handle large metadata allocations.
John notes that it wouldn't be impossible for, say, a generic class metadata record with 500+ methods and properties to blow a page.

Swift SVN r27869
2015-04-28 14:27:25 +00:00
Dmitri Hrybenko
7abe2062e0 runtime: getpagesize() is unportable, use sysconf()
Swift SVN r27861
2015-04-28 06:17:26 +00:00
Joe Groff
b4436bfc13 Runtime: Use a per-cache bump allocator to instantiate metadata.
This has a couple benefits:

- Since metadata allocations are already guarded by a lock, the allocator doesn't require synchronization, and can be much much simpler and a little faster than malloc.
- By bypassing malloc, we also avoid tools like 'heap' prying into our metadata cache and misrepresenting cache entries keyed on classes as live objects, fixing rdar://problem/20562886.

In my unscientific local tests, this appeared to give a small across-the-board improvement to Onone performance in the perf test suite, though not far enough from noise for me to declare that definitively. Fixing the bug is the bigger point here.

Swift SVN r27856
2015-04-28 04:14:06 +00:00
Joe Groff
21960c9814 Runtime: #if a function that's only used in ObjC interop.
Suppress a warning in the non-ObjC build.

Swift SVN r27652
2015-04-23 22:58:13 +00:00
Joe Groff
00f7520888 IRGen/Runtime: Combine the separate per-convention function metadata caches into one.
We have enough flag bits on function types now to warrant stashing an extra word in the metadata key alongside the arguments and results, so add one, and pack the number of arguments, function convention, and 'throws' bit in there. This lets us merge the separate metadata caches for thick/thin/block/C functions into one, saving a bit of runtime memory, and simplifying a bunch of repetitive code in the runtime and IRGen.

This also fixes a subtle bug we had where the runtime getFunctionTypeMetadata function expected the result argument to be passed in the arguments array, but IRGen was passing it as a separate argument, which would have caused function type metadata to fail to be uniqued by result type.

Swift SVN r27651
2015-04-23 22:58:11 +00:00
Joe Groff
c4ec47ddea IRGen/Runtime: Metadata for @convention(c) function types.
For now, C&P a new metadata cache for C function pointers, like we do for blocks and thin types.

Swift SVN r27595
2015-04-22 18:06:23 +00:00
Joe Groff
225a0ebd70 Runtime: Non-ObjC-interoperable version of ErrorObject implementation.
This gets the runtime building again with ObjC interop disabled, though the standard library still has issues.

Swift SVN r27326
2015-04-15 20:27:24 +00:00
Joe Pamer
980bd819a2 Propagate 'throws' information into function type metadata.
Swift SVN r26956
2015-04-03 21:45:33 +00:00
Chris Lattner
79ed57f9f2 standardize naming of tuples and tuple patterns on "elements".
Previously some parts of the compiler referred to them as "fields",
and most referred to them as "elements".  Use the more generic 'elements'
nomenclature because that's what we refer to other things in the compiler
(e.g. the elements of a bracestmt).

At the same time, make the API better by providing "getElement" consistently
and using it, instead of getElements()[i].

NFC.



Swift SVN r26894
2015-04-02 20:23:49 +00:00
Joe Groff
ff5f87f56b Runtime: Add ExistentialTypeMetadata helpers for taking values.
Add a 'mayTakeValue' function, which returns true if we're allowed to take the value from an existential container (assuming we own the existential container value). This is true for class and opaque existential containers, but not for boxed existentials, where multiple owners may share references to the boxed value. Also add a 'deinitExistentialContainer' function to do any cleanup that must happen after the value has been consumed out of an existential container. Fix up an off-by-one indirection level in the SwiftError implementations of the existing existential container methods.

Swift SVN r26561
2015-03-25 23:02:56 +00:00
Joe Groff
668928cc83 Runtime: Handle ErrorType's layout in ExistentialTypeMetadata accessors.
Teach ExistentialTypeMetadata how to project out the value pointer, type metadata, and protocol witness table from ErrorType containers. NFC yet; the dynamic casting and reflection machinery needs further work to correctly handle ErrorType boxes.

Swift SVN r26509
2015-03-25 00:56:16 +00:00
Joe Groff
feb5371e27 Runtime: Use ObjC pointer value witness for ErrorType.
Swift SVN r26497
2015-03-24 19:42:08 +00:00
Joe Groff
ef8cc448d0 Runtime: Instantiate existential metadata with special protocol set.
If an existential type for a special protocol (not a composition) is instantiated, carry the special protocol identifier from that protocol to the existential, allowing us to easily recognize existentials with unique runtime characteristics.

Swift SVN r26436
2015-03-23 17:06:22 +00:00
Dmitri Hrybenko
350248dae5 Reorganize the directory structure under 'stdlib'
The standard library has grown significantly, and we need a new
directory structure that clearly reflects the role of the APIs, and
allows future growth.

See stdlib/{public,internal,private}/README.txt for more information.

Swift SVN r25876
2015-03-09 05:26:05 +00:00