Commit Graph

53 Commits

Author SHA1 Message Date
Max Moiseev
488b464f10 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-25 12:21:56 -08:00
John McCall
fc261045a5 Optimize the number of accesses performed on ConcurrentMap
and MetadataCache and fix a re-entrancy bug in metadata
instantiation.

The re-entrancy bug is that we were holding the instantiation
lock of a metadata cache while instantiating metadata.  Doing
so prevents us from creating a different instantiation if
it's needed by the outer instantiation.  This is already
possible, but it's much more likely in a patch I'm working on
to only store the minimal metadata for generic parameters
in generic types.

The same bug could also show up as a deadlock between threads,
so a recursive lock would not be a good fix.  Instead, we add
a condition variable to the metadata cache.  When fetching
metadata, we look for a node in the concurrent map, eagerly
creating an empty one if none currently exists.  If lookup
finds an empty node, we wait on the condition variable for
the node to become populated.  If lookup succeeds in creating
an empty node, we instantiate the metadata, grab the lock,
populate the node, and notify the condition variable.

Safely creating an empty node without any metadata present
requires us to move the key data into the map entry.  That,
plus a few other invariant shifts, makes it sensible to
give the user of ConcurrentMap more control over the
allocation of map nodes and the layout of keys.  That, in
turn, allows us to change the contract so that keys can be
more complex than just a hash code.  Instead of incrementing
hash codes and re-performing the lookup, we just insist
that lookup keys be totally ordered.

For now, I've kept the uniform use of hash codes as a
component of the key for MetadataCaches.  However, hash
codes aren't really profitable for small keys, and we should
probably use direct comparisons instead.

We should also switch the safer metadata caches (i.e. the
ones that don't involve calling an arbitrary instantiation
function, like MetatypeMetadataCache) over to directly use
ConcurrentMap.

LLDB's requirement that we maintain a linked list of metadata
cache instantiations with a known layout means we can't yet
remove the CacheEntry's redundant copy of the generic
arguments.
2016-02-25 01:11:57 -08:00
Max Moiseev
3a3984877a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-15 15:43:34 -08:00
Joe Groff
32872cb74a IRGen/Runtime: Relative-reference the nominal type descriptor and parent type from metadata.
Save a couple relocations per concrete value type, leaving only the value witness table as an absolute symbol.
2016-02-09 15:17:03 -08:00
Nadav Rotem
978e02412f [UnitTest] Add another unit tests to the data structures that contain the metadata.
I am adding this test mainly to check that the code that removed the sentinal
value and replaced it with a nullable pointer and some logic for initializing
the pointer works.
2016-02-08 22:57:51 -08:00
Slava Pestov
3624b1fc6b Runtime: Support for resiliently adding protocol requirements with default implementations
This is the first patch in a series that will allow new protocol
requirements to be added resiliently, with the runtime filling in
default implementations in witness tables.

First, this adds a new flag to the protocol descriptor indicating
that the protocol is resilient. In this case, there are two
additional fields, MinimumWitnessTableSizeInWords and
DefaultWitnessTableSizeInWords, followed by tail-allocated
default witnesses.

The swift_getGenericWitnessTable() entry point now fills in the
default witnesses from the protocol if the given witness table
template is smaller than the expected witness table size.

This also changes the layout of instantiated witness tables to move
the address point to the end of private data. Previously the private
data came after the requirements, but this meant that adding new
requirements would require sliding the private data at runtime and
accessing it indirectly. It is much simpler to access it from
negative offsets instead.

I updated IRGen to emit the new metadata, but currently all protocols
are flagged as not resilient, and default witnesses are not emitted;
this will come in a subsequent patch once some more plumbing is
in place.

To avoid generating GOT entries for references to protocols defined
in the current module, I had to add some hacks to the existing hack
for this. I'll hopefully clean this up in a principled manner later.
2016-02-04 17:34:55 -08:00
Max Moiseev
f51e708a8f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-04 12:25:25 -08:00
practicalswift
1339b5403b Consistent use of header comment format.
Correct format:
//===--- Name of file - Description ----------------------------*- Lang -*-===//
2016-01-04 13:26:31 +01:00
practicalswift
022efe94c4 Reference file names (and not paths) in file headers. 2016-01-04 09:56:23 +01:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Dmitri Gribenko
feacbc4433 Rename ErrorType to ErrorProtocol 2015-12-09 17:12:19 -08:00
Slava Pestov
54dd7892e8 SIL: Emit native ivar destroyer and store it in class metadata
Swift SVN r32644
2015-10-13 00:27:57 +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
Jordan Rose
a1cd5b5d07 [test] Squash warnings in r30462.
No functionality change.

Swift SVN r30479
2015-07-22 00:14:03 +00:00
Joe Groff
6f47a4a51b Fix bug in runtime test for installCommonValueWitnesses_pod_indirect.
It didn't properly set "withInlineStorage(false)" on the test value witness table, causing it to pick direct-storage value witnesses that overflow a fixed-size buffer and crash on the ASan bot. Fix the test, and add a check for a canary that fails even without ASan.

Swift SVN r30462
2015-07-21 21:52:26 +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
Joe Groff
6d1a623b68 Runtime tests: Fix compile error for non-ObjC build.
Swift SVN r27417
2015-04-17 05:57:35 +00:00
Joe Groff
1b2fcd1852 IRGen/Runtime: AnyObject never has a witness table, even if it isn't @objc.
@objc protocols aren't supported with an ObjC runtime, but we still want values of AnyObject type to be word-sized. Handle this by turning the binary "needsWitnessTable" condition into a "dispatch strategy" enum, so we can recognize the condition "has no methods, so neither swift nor objc dispatch" as distinct from either swift or ObjC protocol representations. Assign this dispatch strategy when we lower AnyObject. Should be NFC for the ObjC-enabled build.

(It would also be beneficial for the ObjC-runtime-enabled version of Swift if AnyObject weren't an @objc protocol; that would mean we could give it a canonical protocol descriptor in the standard library, among other things. There are fairly deep assumptions in Sema that AnyObject is @objc, though, and it's not worth disturbing those assumptions right now.)

Reapplying with updates to the runtime unit tests.

Swift SVN r27341
2015-04-16 00:24:51 +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
Nadav Rotem
d214e367d0 Remove the custom memory allocator. It was not effective in accelerating the metadata lookups and Greg and Dave have suggested other solutions for accelerating this call.
Swift SVN r24869
2015-01-31 00:44:17 +00:00
Nadav Rotem
bdadc015b7 Add a generic parameter to control the number of threads. NFC.
Swift SVN r24868
2015-01-31 00:44:16 +00:00
Nadav Rotem
278bc16f43 Rename the method. NFC.
Swift SVN r24866
2015-01-31 00:44:15 +00:00
Nadav Rotem
26261cd1fe Allow the allocator to free memory.
Swift SVN r24865
2015-01-31 00:44:14 +00:00
Nadav Rotem
2236ff8153 Use std::distance and add the missing typedefs needed for stl
Swift SVN r24863
2015-01-31 00:44:10 +00:00
Nadav Rotem
0b9a79b38b Add a concurrent bump-ptr memory allocator.
Swift SVN r24702
2015-01-24 01:08:04 +00:00
Nadav Rotem
1e02da9465 Add a concurrent memory bank
Swift SVN r24701
2015-01-24 01:08:04 +00:00
Nadav Rotem
1d3a373cbf Add a unit test for the concurrent list.
Swift SVN r24700
2015-01-24 01:08:03 +00:00
Graham Batty
913a76526d Use pthreads in the metadata runtime unit test.
Swift SVN r23381
2014-11-17 21:23:18 +00:00
Graham Batty
b46474ff61 Revert "Use pthreads in the metadata runtime unit test."
This reverts commit r23369.

Swift SVN r23372
2014-11-17 17:54:30 +00:00
Graham Batty
ec44f52f4e Use pthreads in the metadata runtime unit test.
Swift SVN r23369
2014-11-17 17:15:12 +00:00
Joe Groff
dd6fdc4041 Update metatype metadata unit tests.
Metatype metadata now always refers to the maximally-abstracted @thick metatype representation, so metatypes are never trivial from the runtime's point of view.

Swift SVN r21975
2014-09-16 16:55:46 +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
c367362f22 Properly flag opaque existential types as not being
bitwise-takable.

Swift SVN r20803
2014-07-31 02:03:56 +00:00
John McCall
a611637c5a Add a unit test to validate that the new runtime machinery
constructs generic class metadata properly when the
superclass has an unexpected prefix.

Also fix a bug in the constructor for ClassMetadata (which is
only actually constructed this way in unit tests).

Swift SVN r20586
2014-07-26 01:04:58 +00:00
John McCall
d1ccc7e10a Fix the metadata unit test and ensure that it gets set up
correctly for at least my build configuration.

Swift SVN r20566
2014-07-25 22:18:01 +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
ae0dfd4676 Change the layout of the extended class-object header: add
a flags field, add an instance address point field, and reserve
some additional space.

This change must be coordinated with a corresponding change
to ObjC runtime bits in libarclite; without this, dynamic
subclassing features like KVO will break.

The actual contents of the new fields can change without
bothering the ObjC runtime.

Swift SVN r20183
2014-07-18 21:39:43 +00:00
John McCall
63072df530 Add class size and address point fields to class metadata.
I've put these fields on the class object for now, just
so we can at least theoretically update them.  A superclass
that grew left rather than right could maybe even be made
to work with this schema, but probably not.

rdar://16705821

Swift SVN r16880
2014-04-26 10:57:50 +00:00
Joe Groff
87c26c3fcb Unit-test that existential metadata is instantiated with the right kind.
Swift SVN r9878
2013-11-01 20:21:46 +00:00
Joe Groff
98f0a5fc03 Update runtime unit tests for getExistentialTypeMetadata name change.
Swift SVN r9875
2013-11-01 18:27:43 +00:00
Joe Groff
37e652b2ba Runtime: Add swift_getExistentialMetadata entry point.
Set up a metadata cache for existential type metadata. Instantiate existential metadata by first sorting the protocol list, so that it is order invariant, precomputing the overall witness table count and class constraint of the composition so it can be cached in the existential metadata.

We still need to implement value witnesses for existential containers in the runtime before this is complete. We can at least test the uniquing and flags computations at this point.

Swift SVN r9727
2013-10-28 20:53:20 +00:00
Joe Groff
3a8f81433f IRGen: Add a nominal type descriptor to class metadata.
Swift SVN r9503
2013-10-18 22:51:27 +00:00
Dmitri Hrybenko
28c24ea5a9 Fix Metadata unittest after r9249
Swift SVN r9256
2013-10-12 07:32:32 +00:00
Joe Groff
d516b1fe59 Update metadata unittest for generic metadata pattern ABI change.
Swift SVN r7902
2013-09-04 17:50:42 +00:00
John McCall
045d7e4934 Fix the unittests suite.
Just assume that there's a snarky comment about the magic of cmake here.

Swift SVN r4533
2013-03-29 03:17:15 +00:00
Dave Zarzycki
2e802a3376 Fix some build failures introduced by r3397
Swift SVN r3412
2012-12-07 21:39:48 +00:00
John McCall
65db19395d Make metatype layout compatible with struct objc_class.
The principal difficulty here is that we need accessing the
value witness table for a type to be an efficient operation,
but there (obviously) isn't a VWT field for ObjC classes.
Placing this field after the metatype would tend to bloat
metatypes by quite a bit.  Placing it before is best, but
it introduces an unfortunate difference between the address
point of a metatype and the address of the global symbol.
That, however, can be fixed with appropriate linker support.
Still, for now this is rather unfortunately over-subtle.

Swift SVN r3307
2012-11-30 02:47:01 +00:00
John McCall
dda270495f Add a runtime entrypoint to map up a level of metatype.
Swift SVN r3179
2012-11-14 08:45:13 +00:00