Commit Graph

102 Commits

Author SHA1 Message Date
Saleem Abdulrasool
0174528f81 stdlib: fix the problem swept under the rug in 58a97f1603
The `-Winvalid-offsetof` warning is valid in this case. `offsetof` is
being applied to types with a non-standard layout. The layout of this
type is undefined by the specification. There is no guarantee that the
type layout is uniform across all ABIs. It is not possible to portably
compute the offset statically, especially efficiently.

Sink this check into a unit test to avoid performing this test at
runtime. In order to do this in the standard library, we would need to
do this check through a global constructor.
2025-02-03 09:25:06 -08:00
Mike Ash
98229fe4db [Runtime] Don't attempt to look up prespecialized metadata involving pointers outside the shared cache.
The descriptor and arguments for prespecialized metadata will always be in the shared cache. Skip creating the mangling for any lookup involving pointers outside the shared cache, as an optimization.
2024-04-05 10:19:34 -04:00
Mike Ash
0491192337 [Runtime] Remove ExternalGenericMetadataBuilder. 2024-03-21 17:56:17 -04:00
Mike Ash
29c350e813 [Runtime] Create an external generic metadata builder.
Create a version of the metadata specialization code which is abstracted so that it can work in different contexts, such as building specialized metadata from dylibs on disk rather than from inside a running process.

The GenericMetadataBuilder class is templatized on a ReaderWriter. The ReaderWriter abstracts out everything that's different between in-process and external construction of this data. Instead of reading and writing pointers directly, the builder calls the ReaderWriter to resolve and write pointers. The ReaderWriter also handles symbol lookups and looking up other Swift types by name.

This is accompanied by a simple implementation of the ReaderWriter which works in-process. The abstracted calls to resolve and write pointers are implemented using standard pointer dereferencing.

A new SWIFT_DEBUG_VALIDATE_EXTERNAL_GENERIC_METADATA_BUILDER environment variable uses the in-process ReaderWriter to validate the builder by running it in parallel with the existing metadata builder code in the runtime. When enabled, the GenericMetadataBuilder is used to build a second copy of metadata built by the runtime, and the two are compared to ensure that they match. When this environment variable is not set, the new builder code is inactive.

The builder is incomplete, and this initial version only works on structs. Any unsupported type produces an error, and skips the validation.

rdar://116592420
2024-01-11 09:15:02 -05:00
Mike Ash
fc6cfbbf32 [Runtime] Have MetadataCacheKey::operator== try memcmp before deeper comparison.
Experiment shows that the vast majority (~99.99%) of keys with equal hashes have equal bytes. Fast-path that case by doing a memcmp before doing the deeper comparison.

rdar://110478978
2023-10-23 13:04:28 -04:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Hiroshi Yamauchi
50ef384f88 Fix the swift-inspect dump-generic-metadata operation.
ReflectionContext::allocationMetadataPointer() was reading the
metadata pointer from a wrong offset because of the out-of-sync struct
layouts and dump-generic-metadata was not working correctly.

This change resync's the layouts and adds a static_assert to verify
that the offsets match between GenericMetadataCacheEntry and
GenericCacheEntry.
2023-06-09 18:33:06 -07:00
Kuba (Brecka) Mracek
9124c0ef36 Guard packIdx with #ifndef NDEBUG (#66228) 2023-05-31 15:18:08 -07:00
Kuba (Brecka) Mracek
1125e45cca Don't include <thread> in MetadataCache.h, all we need there is <atomic> (#65826) 2023-05-12 10:34:31 -07:00
Kuba (Brecka) Mracek
b58818f153 To be able to correctly exercise Concurrency on freestanding, default to using 'pthread' threading package (#65329)
To be able to correctly exercise Concurrency on freestanding, default to using 'pthread' threading package

rdar://102259828
2023-04-21 14:47:51 -07:00
Konrad `ktoso` Malawski
0586c14b60 [Concurrency] SerialExecutor.isSameExclusiveExecutionContext (#64604) 2023-03-28 15:56:28 +09:00
Slava Pestov
5f0d7320df Runtime: Fix MetadataCacheKey::installInto() for variadic generics 2023-03-22 16:53:25 -04:00
Slava Pestov
9acd2446cc Runtime: MetadataCacheKey is bit too large to pass by value 2023-03-22 16:53:25 -04:00
Slava Pestov
ae2c3a5820 Runtime: Fix silly bug in MetadataCacheKey::operator== 2023-03-09 01:54:51 -05:00
Slava Pestov
66ed09e72b Runtime: Variadic generic type fixes 2023-03-03 02:24:01 -05:00
Slava Pestov
01acda7730 Runtime: Remove linear order on metadata cache keys 2023-03-02 10:22:11 -05:00
Slava Pestov
ac199ac7c6 Runtime: Metadata cache key support for packs 2023-03-01 15:48:01 -05:00
Slava Pestov
e5eafbc446 Runtime: Factor out GenericSignatureLayout::compare() 2023-03-01 15:11:02 -05:00
Arnold Schwaighofer
0f9bfac94b Remove undefined behavior in RelativeWitnessTable::getDescription() 2023-02-02 07:02:21 -08:00
Arnold Schwaighofer
770648f161 Initial runtime changes to support relative protocol witness tables 2023-01-31 10:59:37 -08:00
Egor Zhdan
84a1ffcb33 [Shims] Include SwiftShims headers without ../
This replaces a number of `#include`-s like this:
```
#include "../../../stdlib/public/SwiftShims/Visibility.h"
```
with this:
```
#include "swift/shims/Visibility.h"
```

This is needed to allow SwiftCompilerSources to use C++ headers which include SwiftShims headers. Currently trying to do that results in errors:
```
swift/swift/include/swift/Demangling/../../../stdlib/public/SwiftShims/module.modulemap:1:8: error: redefinition of module 'SwiftShims'
module SwiftShims {
       ^
Builds.noindex/swift/swift/bootstrapping0/lib/swift/shims/module.modulemap:1:8: note: previously defined here
module SwiftShims {
       ^
```
This happens because the headers in both the source dir and the build dir refer to SwiftShims headers by relative path, and both the source root and the build root contain SwiftShims headers (which are equivalent, but since they are located in different dirs, Clang treats them as different modules).
2022-09-14 11:14:50 +01:00
Alastair Houghton
0e9318cec5 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
f5bdb858e0 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alex Hoppen
4aa2bbbf06 Revert "Merge pull request #42447 from al45tair/eng/PR-90776105"
This reverts commit 8bcb71140f, reversing
changes made to c4dd271d36.
2022-06-02 18:03:23 +02:00
Alastair Houghton
b5bd267ff1 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-05-24 14:57:41 +01:00
Alastair Houghton
63a09007a1 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-05-24 14:57:39 +01:00
Jonathan Grynspan
9644c7390f 58711 - swift_slowAlloc() and friends should be marked SWIFT_NODISCARD 2022-05-06 11:05:12 -04:00
Jonathan Grynspan
770fd107de 58686: swift_slowAlloc() _et al._ should be marked returns-nonnull to improve codegen 2022-05-05 23:18:48 -04:00
Jonathan Grynspan
b749dd395d Revert "runtime: allow over-aligned types in the runtime"
This reverts commit b694ce4634.
2022-05-05 11:10:12 -04:00
Robert Widmann
e15ca51526 Update Runtime Generalized Existential Metadata
Tidy up the metadata definitions.

* Generalize a number of metadata kinds for out-of-process clients
* Introduce conveniences to make runtime lookups easier
* Introduce TargetExistentialTypeExpression to TrailingObjects stops complaining about OverloadTokens being ambiguous

Note that there is no impact on the layout of the metadata - the changes here are all ABI-compatible.
2022-04-25 16:43:51 -07:00
Saleem Abdulrasool
b694ce4634 runtime: allow over-aligned types in the runtime
Not all targets have a 16-byte type alignment guarantee.  For the types
which are not naturally aligned, provide a type specific `operator new`
overload to ensure that we are properly aligning the type on allocation
as we run the risk of under-aligned allocations otherwise.

This should no longer be needed with C++17 and newer which do a two
phase `operator new` lookup preferring
`operator new(std::size, std::align_val_t)` if needed.  The base type
would be fully pre-processed away.  The empty base class optimization
should help ensure that we do not pay any extra size costs for the
alignment fixes.

As we are a C++14 codebase, we must locally implement some of the
standard type_traits utilities, namely `void_t`.  We take the minimal
definition here, assuming that the compiler is up-to-date with C++14 DR
reports which fixed an issue in SFINAE.  We use the SFINAE for detecting
the presence of the `operator new` overload to guide the over-alignment,
which is inherited through the new `swift::overaligned_type<>`  base
type.

Annotate the known classes which request explicit alignment which is
non-pointer alignment.  This list was identified by
`git grep ' alignas(.*) '`.
2022-04-04 18:26:20 -07:00
John McCall
91c1d79665 [NFC] Add GenericSignatureLayout and use it in metadata caching
We'll need more information than this when we introduce packs,
so we might as well create a common abstraction for it now.
2022-03-24 00:53:45 -04:00
John McCall
749ed09f8c Eliminate priority inversions in the metadata completion runtime.
The current system is based on MetadataCompletionQueueEntry
objects which are allocated and then enqueued on dependencies.
Blocking is achieved using a condition variable associated
with the lock on the appropriate metadata cache.  Condition
variables are inherently susceptible to priority inversions
because the waiting threads have no dynamic knowledge of
which thread will notify the condition.  In the current system,
threads that unblock dependencies synchronously advance their
dependent metadata completions, which means the signaling
thread is unreliable even if we could represent it in condition
variables.  As a result, the current system is wholly unsuited
for eliminating these priority inversions.

An AtomicWaitQueue is an object containing a lock.  The queue
is eagerly allocated, and the lock is held, whenever a thread
is doing work that other threads might wish to block on.  In
the metadata completion system, this means whenever we construct
a metadata cache entry and the metadata isn't already allocated
and transitively complete after said construction.  Blocking
is done by safely acquiring a shared reference to the queue
object (which, in the current implementation, requires briefly
taking a lock that's global to the surrounding metadata cache)
and then acquiring the contained lock.  For typical lock
implementations, this avoids priority inversions by temporarily
propagating the priority of waiting threads to the locking
threads.

Dependencies are unblocked by simply releasing the lock held
in the queue.  The unblocking thread doesn't know exactly what
metadata are blocked on it and doesn't make any effort to
directly advance their completion; instead, the blocking
thread will wake up and then attempt to advance the dependent
metadata completion itself, eliminating a source of priority
overhang that affected the old system.  Successive rounds of
unblocking (e.g. when a metadata makes partial progress but
isn't yet complete) can be achieved by creating a new queue
and unlocking the old one.  We can still record dependencies
and use them to dynamically diagnose metadata cycles.

The new system allocates more eagerly than the old one.
Formerly, metadata completions which were never blocked never
needed to allocate a MetadataCompletionQueueEntry; we were
then unable to actually deallocate those entries once they
were allocated.  The new system will allocate a queue for
most metadata completions, although, on the positive side,
we can reliably deallocate these queues.  Cache entries are
also now slightly smaller because some of the excess storage
for status has been folded into the queue.

The fast path of an actual read of the metadata remains a
simple load-acquire.  Slow paths may require a bit more
locking.  On Darwin, the metadata cache lock can now use
os_unfair_lock instead of pthread_mutex_t (which is a massive
improvement) because it does not need to support associated
condition variables.

The excess locking could be eliminated with some sort of
generational scheme.  Sadly, those are not portable, and I
didn't want to take it on up-front.

rdar://76127798
2021-12-14 22:18:46 -05:00
Kuba (Brecka) Mracek
5ff64f8443 Introduce a CMake option to disable MetadataAllocator and use malloc+free instead, use it for freestandning preset (#39678) 2021-10-12 16:20:08 -07: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
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
John McCall
0fb407943f [NFC] Rename swift_runtime_unreachable to swift_unreachable and make it use LLVM's support when available. 2020-10-03 02:54:56 -04:00
Kuba (Brecka) Mracek
9ead8d57fa Add a single-threaded stdlib mode, use it for the 'minimal' stdlib (#33437) 2020-08-25 06:03:14 -07:00
Nate Chandler
6d0c34caf3 [Runtime] Add entry point to compare conformance descriptors.
The new function swift_compareProtocolConformanceDescriptors calls
through to the preexisting code in MetadataCacheKey which has been
extracted out from MetadataCacheKey::compareWitnessTables into a new
public static function
MetadataCacheKey::compareProtocolConformanceDescriptors.

The new function's availability is "future" for now.
2020-06-22 15:01:28 -07:00
Mike Ash
2c7f09cef7 [Tools] Add some metadata printing to swiftdt.
rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
22cfe461ec [Tools] Super rough draft of swiftdt dumping MetadataAllocator contents.
rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Saleem Abdulrasool
f0413f6170 build: remove last llvm:: reference in stdlib
This removes the last reference to the `llvm::` namespace in the
standard library.  All uses of the LLVMSupport library now are
namespaced into the `__swift::__runtime` namespace.  This allows us to
incrementally vend the LLVMSupport library and make the separation
explicit.
2020-05-18 11:41:57 -07: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
Saleem Abdulrasool
a7415423e6 runtime: add and switch to SWIFT_FALLTHROUGH (NFC)
This duplicates and switches the uses of `LLVM_FALLTHROUGH` to a local
macro for the same annotation.
2020-05-07 11:50:22 -07:00
Saleem Abdulrasool
c8e8b177fa runtime: fix a UBSAN issue
This was identified by UBSAN: signed-integer-overflow.  Explicitly mark
the value as unsigned to ensure that the value does not overflow.  There
is a second instance of a raw literal, however, because it is a `*=` the
value is implicitly understood to be unsigned.
2020-04-09 12:21:20 -07:00
Slava Pestov
4bc1d03283 Runtime: Use accessor method to get TargetWitnessTable::Description 2020-03-11 19:34:10 -04:00
Doug Gregor
39481149a3 [Runtime] Compute generic metadata cache info once per type.
Rather than scanning through the generic parameters and generic requirements
each time we form a key for the generic metadata cache, compute these
values once, when the cache itself is first initialized.
2018-11-26 10:46:42 -08:00