Commit Graph

484 Commits

Author SHA1 Message Date
Doug Gregor
7f3db7fbde [Concurrency] Have future functions write their results directly.
Introduce `FutureAsyncContext` to line up with the async context formed
by IR generation for the type `<T> () async throws -> T`. When allocating
a future task, set up the context with the address of the future's storage
for the successful result and null out the error result, so the caller
will directly fill in the result. This eliminates a bunch of extra
complexity and a copy.
2020-11-14 15:18:54 -08:00
Doug Gregor
0ae6c6337e [Concurrency] Use SchedulerPrivate for the "next waiting task" link.
Reduce the size of AsyncTask by using the first slot of SchedulerPrivate
for the next waiting task. Thanks, John!
2020-11-14 15:18:54 -08:00
Doug Gregor
aa76672298 [Future] Add a test to make sure we're handling object lifetimes properly. 2020-11-14 15:18:54 -08:00
Doug Gregor
85d003ef9b [Concurrency] Implement basic runtime support for task futures.
Extend AsyncTask and the concurrency runtime with basic support for
task futures. AsyncTasks with futures contain a future fragment with
information about the type produced by the future, and where the
future will put the result value or the thrown error in the initial
context.

We still don't have the ability to schedule the waiting tasks on an
executor when the future completes, so this isn't useful for anything
just test, and we can only test limited code paths.
2020-11-14 15:18:54 -08:00
Mike Ash
e82d9e8c7b Move ConditionMutex to ConditionVariable::Mutex and move various other Mutex.h types to be nested. 2020-11-10 14:44:59 -05:00
Mike Ash
dd6c235a2d [Runtime] Use os_unfair_lock for Mutex and StaticMutex on Darwin.
os_unfair_lock is much smaller than pthread_mutex_t (4 bytes versus 64) and a bit faster.

However, it doesn't support condition variables. Most of our uses of Mutex don't use condition variables, but a few do. Introduce ConditionMutex and StaticConditionMutex, which allow condition variables and continue to use pthread_mutex_t.

On all other platforms, we continue to use the same backing mutex type for both Mutex and ConditionMutex.

rdar://problem/45412121
2020-11-06 13:05:37 -05:00
Mike Ash
12d114713f [Runtime] Switch MetadataCache to ConcurrentReadableHashMap. (#34307)
* [Runtime] Switch MetadataCache to ConcurrentReadableHashMap.

Use StableAddressConcurrentReadableHashMap. MetadataCacheEntry's methods for awaiting a particular state assume a stable address, where it will repeatedly examine `this` in a loop while waiting on a condition variable, so we give it a stable address to accommodate that. Some of these caches may be able to tolerate unstable addresses if this code is changed to perform the necessary table lookup each time through the loop instead. Some of them store metadata inline and we assume metadata never moves, so they'll have to stay this way.

* Have StableAddressConcurrentReadableHashMap remember the last found entry and check that before doing a more expensive lookup.

* Make a SmallMutex type that stores the mutex data out of line, and use it to get LockingConcurrentMapStorage to fit into the available space on 32-bit.

rdar://problem/70220660
2020-11-04 15:10:50 -05:00
John McCall
a06d18ce81 Add API for creating unscheduled tasks.
Make all tasks into heap objects.
2020-10-22 00:53:16 -04:00
John McCall
8ac4362754 Implement a simple library for task cancellation and status management.
There are things about this that I'm far from sold on.  In
particular, I'm concerned that in order to implement escalation
correctly, we're going to have to add a status record for the
fact that the task is being executed, which means we're going
to have to potentially wait to acquire the status lock; overall,
that means making an extra runtime function call and doing some
atomics whenever we resume or suspend a task, which is an
uncomfortable amount of overhead.

The testing here is pretty grossly inadequate, but I wanted to
lay down the groundwork here.
2020-10-15 00:36:36 -04:00
Mike Ash
630aff7b19 [Runtime] Change SimpleGlobalCache to use ConcurrentReadableHashMap instead of ConcurrentMap.
This gives us faster lookups and a small advantage in memory usage. Most of these maps need stable addresses for their entries, so we add a level of indirection to ConcurrentReadableHashMap for these cases to accommodate that. This costs some extra memory, but it's still a net win.

A new StableAddressConcurrentReadableHashMap type handles this indirection and adds a convenience getOrInsert to take advantage of it.

ConcurrentReadableHashMap is tweaked to avoid any global constructors or destructors when using it as a global variable.

ForeignWitnessTables does not need stable addresses and it now uses ConcurrentReadableHashMap directly.

rdar://problem/70056398
2020-10-08 14:57:39 -04:00
Simon Evans
960d0e30ee Linux: Fix -static-executable and remove swiftImageInspectionShared
- Remove references to swiftImageInspectionShared on Linux and dont have
  split libraries between static/non-static linked executables.

- -static-executable now links correctly Linux.

  Note: swift::lookupSymbol() will not work on statically linked
        executables but this can be worked around by using the
        https://github.com/swift-server/swift-backtrace package.
2020-10-05 10:16:03 +01:00
Mike Ash
fd6922f92d Add error reporting when looking up types by demangled name. 2020-08-28 14:43:51 -04:00
Mike Ash
ecd6d4ddec Add a new ConcurrentReadableHashMap type. Switch the protocol conformance cache
to use it.

ConcurrentReadableHashMap is lock-free for readers, with writers using a lock to
ensure mutual exclusion amongst each other. The intent is to eventually replace
all uses ConcurrentMap with ConcurrentReadableHashMap.

ConcurrentReadableHashMap provides for relatively quick lookups by using a hash
table. Rearders perform an atomic increment/decrement in order to inform writers
that there are active readers. The design attempts to minimize wasted memory by
storing the actual elements out-of-line, and having the table store indices into
a separate array of elements.

The protocol conformance cache now uses ConcurrentReadableHashMap, which
provides faster lookups and less memory use than the previous ConcurrentMap
implementation. The previous implementation caches
ProtocolConformanceDescriptors and extracts the WitnessTable after the cache
lookup. The new implementation directly caches the WitnessTable, removing an
extra step (potentially a quite slow one) from the fast path.

The previous implementation used a generational scheme to detect when negative
cache entries became obsolete due to new dynamic libraries being loaded, and
update them in place. The new implementation just clears the entire cache when
libraries are loaded, greatly simplifying the code and saving the memory needed
to track the current generation in each negative cache entry. This means we need
to re-cache all requested conformances after loading a dynamic library, but
loading libraries at runtime is rare and slow anyway.

rdar://problem/67268325
2020-08-20 13:05:30 -04:00
Mike Ash
cbb1fd3f27 Add some comments to ConcurrentReadableArrayTest.MultiThreaded. Add a second test to stress the snapshot code. 2020-08-13 15:32:23 -04:00
Mike Ash
1ac565d6ef Add a multithreaded test for ConcurrentReadableArray. 2020-08-07 11:17:38 -04:00
Mike Ash
9134e65fca Move Mutex test's threading helpers to a separate header so other tests can use them. 2020-08-07 11:17:38 -04:00
Tony Allevato
5b1daa9055 Conditionally wrap (de)mangling symbols in an inline namespace.
Since libDemangling is included in the Swift standard library,
ODR violations can occur on platforms that allow statically
linking stdlib if Swift code is linked with other compiler
libraries that also transitively pull in libDemangling, and if
the stdlib version and compiler version do not match exactly
(even down to commit drift between releases). This lets the
runtime conditionally segregate its copies of the libDemangling
symbols from those in the compiler using an inline namespace
without affecting usage throughout source.
2020-06-19 11:20:56 -07:00
Saleem Abdulrasool
fb58228f62 runtime: extract swiftDemangling into a support library
`swiftDemangling` was built three times:
1. swiftc
2. swiftRuntime
3. swiftReflection

Fold the last two instances into a single build, sharing the objects
across both the target libraries.  This ensures that `swiftDemangling`
is built with the same compiler as the target libraries and that the
target library build remains self-contained.
2020-05-30 12:26:57 -07:00
Saleem Abdulrasool
1e74c202ef build: optimize the build of LLVMSupport
Rather than build multiple copies of LLVMSupport (4x!) build it one and
merge it into the various targets.  This would ideally not be needed to
be named explicitly everywhere, but that requires using `add_library`
rather than `add_swift_target_library`.
2020-05-19 17:04:59 +00:00
Saleem Abdulrasool
3fa1d1fe3f runtime: ingest LLVMSupport into the runtime
This adds a new copy of LLVMSupport into the runtime.  This is the final
step before changing the inline namespace for the runtime support.  This
will allow us to avoid the ODR violations from the header definitions of
LLVMSupport.

LLVMSupport forked at: 22492eead218ec91d349c8c50439880fbeacf2b7
Changes made to LLVMSupport from that revision:
  process.inc forward declares `_beginthreadex` due to compilation issues due to custom flag handling

API changes required that we alter the `Deallocate` routine to account
for the alignment.

This is a temporary state, meant to simplify the process.  We do not use
the entire LLVMSupport library and there is no value in keeping the
entire library.  Subsequent commits will prune the library to the needs
for the runtime.
2020-05-15 09:55:36 -07:00
Joe Groff
4981da4a4d Merge pull request #30632 from Azoy/swift-5-3-hacks
Bump backward compatibility to 5.3
2020-03-26 11:16:01 -07:00
David Smith
40e67b53c9 Merge pull request #30034 from Catfish-Man/what-i-tell-you-three-times-is-true
Avoid mishandling retain_n of immortal objects where n >= 3
2020-03-26 11:00:46 -07:00
David Smith
78c693ba2d Avoid mishandling retain_n of immortal objects where n >= 3 2020-03-25 15:53:31 -07:00
Azoy
d8ea42adfb Bump backward compatibility to 5.3 2020-03-25 11:41:15 -04:00
Dario Rexin
4c0b8fa5c3 build: Don't link against libatomic on Linux
The dependency on libatomic was removed in 97df90fbf1. This commit removes leftover cases that still link against libatomic.
2020-03-02 17:34:53 -08:00
Daniel Rodríguez Troitiño
59bf0862df [windows] Move ENABLE_EXTENDED_ALIGN_STORAGE into AddSwiftUnittest
It will apply to everything that uses add_swift_unittest and hopefully it will avoid all the problems.
2020-02-13 20:49:07 -08:00
Daniel Rodríguez Troitiño
5c66f0ee85 [windows] Compare SWIFT_HOST_VARIANT against lowercase value.
SWIFT_HOST_VARIANT is lowercase. SWIFT_HOST_VARIANT_SDK is uppercase.

Compare against the right one.

This tries to fix #29805, which didn't fix the CI for Windows VS2017.
2020-02-13 13:56:43 -08:00
Saleem Abdulrasool
3dfec1b47b unittests: adjust for VS2017 15.8+
Repair the VS2017 build similar to the standard library.
2020-02-12 22:44:07 -08:00
Saleem Abdulrasool
5ae2368eb5 unittests: add stub for __SwiftValue
This placeholds the `__SwiftValue` metadata necessary for the Swift
runtime tests.
2020-02-12 14:59:12 -08:00
David Smith
f36a4db856 Update fast dealloc to use new-style interposing and support objc weak refs 2020-01-22 13:55:27 -08:00
Joe Groff
f7caa2d618 Bump runtime to load backward compatibility hacks from __swift52_hooks 2019-12-10 13:10:08 -08:00
Doug Gregor
996f2ab6de [Long runtime tests] Link the Objective-C runtime.
The long-running runtime tests depend on the Objective-C runtime on
Darwin, so link it directly rather than relying on various uses of
__builtin_available to link CoreFoundation for us.
2019-11-07 09:37:41 -08:00
Saleem Abdulrasool
c4f448578e Revert "runtime: add a workaround for Windows build"
This reverts commit efaf1fbefa.
Add a much more palatable workaround for the unit tests.  Rather than
adding the dllimport for the symbols, locally define the required
symbols.  This list is sufficient to restore the ability to build tests
for Windows.
2019-09-16 15:31:01 -07:00
David Smith
a84af6f68b Revert "Revert "Revert "Use the remaining half bit in the refcount to bypass ObjC deallocation overhead"""
This reverts commit c51294671b.
2019-07-01 14:29:40 -07:00
David Smith
c51294671b Revert "Revert "Use the remaining half bit in the refcount to bypass ObjC deallocation overhead""
And add availability checking for back deployment

This reverts commit 817ea129f2.
2019-06-18 16:16:38 -07:00
Mishal Shah
817ea129f2 Revert "Use the remaining half bit in the refcount to bypass ObjC deallocation overhead" 2019-06-05 23:10:34 -07:00
David Smith
8abffa7d89 Use the remaining half bit in the refcount to bypass ObjC deallocation overhead 2019-06-05 14:10:19 -07:00
Joe Groff
05dfec0cda Push the conformance accessor hack down into getTypeByMangledName. 2019-04-17 14:44:40 -07:00
Mike Ash
855dd8d5fb [Tests] Use a plain array of std::atomic rather than a vector of atomic.
The restrictions on std::atomic make a it fragile to have a vector of them, and this failed to compile on Linux in CI.

While I'm in there, clean up a couple of tests that repeated a raw `10` for the thread count.

rdar://problem/49709062
2019-04-09 10:52:53 -04:00
Mishal Shah
c4626baf88 Un-set CMAKE_C_COMPILER_LAUNCHER and CMAKE_CXX_COMPILER_LAUNCHER in s… (#23806)
[CMake] Add check for distcc before setting CMAKE_C_COMPILER_LAUNCHER and CMAKE_CXX_COMPILER_LAUNCHER
2019-04-08 12:53:35 -07:00
Mike Ash
6172f72af9 [Tests] Make Mutex.cpp tests more reliable.
Many of these tests would fail if one of the test threads didn't begin execution within 100 milliseconds. They would hit while (!done) the very first time and never execute the loop body. That does happen from time to time, and the result would be a spurious failure. Change the loops to do {} while(!done) to ensure they always execute at least one iteration.

Many tests also had the test threads concurrently write results to a std::vector, which appeared to be causing some failures in my local testing when I had extra sleeps inserted to simulate pathological scheduling. Change the results vectors to be vectors of std::atomic to ensure that this concurrent writing is safe.

These changes shouldn't affect its ability to test the functionality it intends to test.

rdar://problem/49386389
2019-04-05 16:40:08 -04:00
Saleem Abdulrasool
4b9f8464e4 unittests: add a hack to use the host compiler
Unfortunately, ASAN breaks with the just built compiler.  The runtime
and the runtime tests should really use the same compiler.  As a
workaround, if the host compiler is clang, just use that for the time
being.  This should fix the build on the ASAN bots.
2019-02-27 11:37:45 -08:00
Saleem Abdulrasool
27fab1d582 build: use the just-built clang for runtime unittests
The runtime is meant to be built with the just built clang (as this
absolutely requires clang) as the runtime is a target library.  The host
tools can be built with the host compiler.  Swap out the compiler for
the unittests as we do for the runtime itself.
2019-02-25 16:13:12 -08:00
Saleem Abdulrasool
9897716e4b unittests: fixes for Swift's custom build system
Because we do not have proper libraries in our system, we cannot attach
interface link libraries, especially in light of the
incorporate_object_library implementation.  Explicitly add the
dependency on DbgHelp for the unit tests.
2019-02-21 22:39:00 -08:00
John McCall
d07efd37e0 Requestify the mangling-to-metadata APIs.
Note that I've called out a couple of suspicious places where we
are requesting abstract metadata for superclasses but probably
need to be requesting something more complete.
2019-02-05 16:20:48 -05:00
John McCall
de4564877a Separate CC attrs in the compatibility-override macro; NFC. 2019-02-05 16:20:48 -05:00
John McCall
724c192120 Propagate the XI count into the get/store XI tag callbacks.
This allows callers to avoid needing to reload these tags in common cases.
2018-12-11 22:18:44 -05:00
John McCall
2ba7090fe8 Remove the extra-inhabitant value witness functions.
This is essentially a long-belated follow-up to Arnold's #12606.
The key observation here is that the enum-tag-single-payload witnesses
are strictly more powerful than the XI witnesses: you can simulate
the XI witnesses by using an extra case count that's <= the XI count.
Of course the result is less efficient than the XI witnesses, but
that's less important than overall code size, and we can work on
fast-paths for that.

The extra inhabitant count is stored in a 32-bit field (always present)
following the ValueWitnessFlags, which now occupy a fixed 32 bits.
This inflates non-XI VWTs on 32-bit targets by a word, but the net effect
on XI VWTs is to shrink them by two words, which is likely to be the
more important change.  Also, being able to access the XI count directly
should be a nice win.
2018-12-11 22:18:44 -05:00
Jason Mittertreiner
4e0b092fbe Fixing Driver tests on Windows (#20209) 2018-12-10 18:27:37 -08:00
Joe Groff
bce1f5ef4a Runtime: Provide ABI space for source location info in unconditional casts.
Currently ignored, but this will allow future compilers to pass down source location information for cast
failure runtime errors without backward deployment constraints.
2018-12-06 14:58:14 -08:00