Commit Graph

1544 Commits

Author SHA1 Message Date
Rokhini Prabhu
ff10907a8e Change ActiveTaskStatus layout in order to be able to track the identity
of the thread that is executing the task.

Radar-Id: rdar://problem/76127624
2022-02-07 16:34:46 -08:00
Saleem Abdulrasool
1b5b9d3573 Runtime: change atomic implementation for AllocationPool
The `AllocationPool` type is reflected upon and inspected by debugging
tools.  This requires that the layout of the atomically wrapped type
must store the value at offset 0.  However, the `std::atomic` does not
make any such guarantees, and a layout differing would render debug
utilities useless.  Switch instead from a `std::atomic` to
`swift::atomic`.  In order to do so, we also need to introduce the
`compare_exchange_strong` helper which maps to
`compare_exchange_strong_explicit` mirroring `compare_exchange_weak`.
2022-02-06 12:37:48 -08:00
Saleem Abdulrasool
bc9b9a5204 Runtime: make padding explicit for ElementStorage
The `ElementStorage` member `Capacity` will be padded up to the next pointer alignment due to the storage of the `ElemTy` by C's struct layout rules.  However, this is implicit and not entirely guaranteed.  Instead, make the storage a pointer sized value, and then truncate to 32-bits to maintain ABI compatibility.  This becomes important as we expand on the runtime metadata handling.
2022-02-04 16:55:52 -08:00
Rokhini Prabhu
9df1c9a135 Track base priority separately from max priority whereby base priority
is set at creation time. Create a new API for accessing this state

Radar-Id: rdar://problem/86100376
2022-01-24 07:50:27 -08:00
Rokhini Prabhu
8f2ac188fb Make sure that currentPriority actually reads the max QoS in the active
task status instead of the Job priority which is not necessarily in sync

Radar-Id: rdar://problem/86100376
2022-01-21 12:12:12 -08:00
Evan Wilde
d89a524ee7 Merge pull request #40865 from etcwilde/ewilde/remove-constexpr-if
Remove constexpr-if from runtime
2022-01-15 15:05:48 -08:00
Evan Wilde
d452f68e70 Remove constexpr-if from runtime
constexpr-if is a C++17 feature. Swift is C++14, so this results in a
lot of warnings. Clang is smart enough that the constant propagation
will elide the unused side of the branch anyway. Since we're doing a
template check on a type, we should always have enough information at
compile to elide. We would need a constexpr/std::enable_if-level
approach if the API's were different to tell clang not to look at one
side of the branch. That's not the case here so we can let the optimizer
do it's thing and let clang look at both sides.
2022-01-14 16:21:15 -08:00
tbkka
184488d3f2 Conditionalize the fix for SR-14635 (#40822)
* Refactor Bincompat

Organize everything around internal functions that test for
a particular OS version.

Correctly handle cases where we don't know the version of the app.

Make all bincompat functions consistently return `true` for the
legacy semantics, `false` for new semantics.  Consistently name
them all to reflect this.

* Conditionalize the support for SR-14635

SR-14635 pointed out a hole in the updated dynamic casting logic
that allowed certain casts that should have been illegal.

In particular, when casting certain types to Obj-C protocols,
the Swift value gets boxed; we would permit the cast to succeed
whenever the resulting box satisfied the protocol.  For example,
this allowed any Swift value to be cast to `NSCopying` regardless of
whether or not it implemented the required `copy(with:)` method.

This was fixed in #37683 to reject such casts but of course some folks were
depending on this behavior to pass Swift data into Obj-C functions.
(The properly supported approach for passing arbitrary Swift data into
Obj-C functions is to cast the Swift value to `AnyObject`.)

This change makes that new behavior conditional.  For now,
the legacy semantics are enabled on Apple platforms and the
new semantics are in use everywhere else.  This will allow
us to gradually enable enforcement of the new behavior over
time.

* Just skip this test on Apple platforms, since it is inconsistently implemented there (and is therefore not really testable)
2022-01-14 11:56:25 -08:00
Rokhini Prabhu
a4fe57f230 Merge pull request #40606 from apple/rokhinip/86347801-task-creation-escalation-race
Resolve race between task creation and concurrent escalation and cancellation.
2022-01-12 12:14:09 +08:00
David Smith
48399afb03 Merge branch 'main' into unicodectomy 2022-01-03 22:57:37 -08:00
Rokhini Prabhu
e35eba06d2 Remove old addChild and detachChild stuff from files
Radar-Id: rdar://problem/86347801
2021-12-31 03:23:52 -08:00
Rokhini Prabhu
1947102ebd Change the logic for adding new task status records to a task
This change has two parts to it:

1. Add in a new interface (addStatusRecordWithChecks) for adding task
status records that also takes in a function ref. This function ref will
be used to evaluate if current state of the parent task has any changes
that need to be propagated to the child task that has been created.

This is necessary to prevent the following race between task creation
and concurrent cancellation and escalation:

a. Parent task create child task. It does lazy relaxed loads on its own
   state while doing so and propagates this state to the child.
b. Child task is created but has not been attached to the parent
   task/task group.
c. Parent task gets cancelled by another thread.
d. Child task gets linked into the parent’s task status records but no
   reevaluation has happened to account for changes that might have happened to
   the parent after (a).

2. Move status record management functions from the
Runtime/Concurrency.h to TaskPrivate.h. Remove any corresponding
overrides that are no longer needed. Remove unused tryAddStatusRecord
method whose functionality is provided by addStatusRecordWithChecks.

Radar-Id: rdar://problem/86347801
2021-12-31 03:23:52 -08:00
David Smith
f1a5e27821 Add a build flag to disable building Unicode support 2021-12-21 17:26:15 -08:00
John McCall
3343331ded Remove the ConditionVariable abstraction. 2021-12-20 18:04:19 -05:00
Pavel Yaskevich
d65906b1e8 [Runtime] Expose accessible function lookup as stdlib SPI
`findAccessibleFunction` has to be accessible from Concurrency
and Distributed modules to be able to lookup distributed accessors.
2021-12-17 10:52:56 -08:00
Kuba (Brecka) Mracek
c92f2a24ed When ptrauth-copying vtable/wtables, allow NULL entries (due to VFE) (#40578)
* When ptrauth-copying vtable/wtables, allow NULL entries (due to VFE)

* Mark virtual-function-elimination-generics-exec.swift UNSUPPORTED: arm64e until the rebranch

* Fix test expectations
2021-12-16 16:49:24 -08:00
John McCall
c9bb3041f2 Support work-or-wait patterns a little better in AtomicWaitQueue.
Add a tryReloadAndWait method to Worker (which can only be used
when not the worker thread).  Revise the docs to describe this
sort of pattern as the more standard pattern, which I think is
likely to better reflect common use.
2021-12-16 03:07:45 -05:00
John McCall
8289d04eb1 Make it harder to forget to update an existing AtomicWaitQueue.
The existing uses of AWQ don't need arguments during construction,
but uses that do almost certainly need to update existing instances
if createQueue happens to re-use one.  Users probably aren't going
to think about this proactively by doing something wild like reading
the documentation.  We can point this mistake out to them by making
their code not compile if they call createQueue with arguments
without providing a special method.  This pattern also makes the
actual update code much easier to write, since callers don't need
to specially detect this case.
2021-12-16 03:07:45 -05:00
John McCall
4969407352 Merge pull request #40542 from rjmccall/atomic-wait-queue-metadata
Eliminate priority inversions in the metadata completion runtime
2021-12-15 21:42:17 -05:00
Alastair Houghton
0e5a620917 Merge pull request #40331 from al45tair/problem/84393438
[Runtime] Don't use threading APIs when building single threaded.
2021-12-14 22:19:20 +00:00
John McCall
b09e712db2 Add AtomicWaitQueue, a lock/condvar for atomic slow paths. 2021-12-14 15:31:46 -05:00
John McCall
6efad8c918 [NFC] Assorted API improvements in Mutex.h
- take critical sections by reference
- forward critical sections into calls (except when called
  multiple times)
- add withLock methods where they were missing
2021-12-14 15:31:46 -05:00
Jonathan Grynspan
cd6755bdd8 Merge pull request #40521 from apple/jgrynspan/ConcurrentReadableArray-copy-bug
`ConcurrentReadableArray`: Use `std::uninitialized_copy_n()` instead of `std::copy()` to avoid calling destructors on uninitialized memory
2021-12-13 08:40:39 -05:00
Jonathan Grynspan
c9f813604a ConcurrentReadableArray: Use std::uninitialized_copy_n() instead of std::copy() to avoid calling destructors on uninitialized memory 2021-12-11 13:25:51 -05:00
Pavel Yaskevich
be594749e7 Merge pull request #40495 from xedin/improvements-to-async-signature
[Concurrency/Runtime] Implement function/continuation type generation…
2021-12-10 09:30:54 -08:00
Pavel Yaskevich
9a00b33378 [Concurrency/Runtime] Implement function/continuation type generation for AsyncSignature
`AsyncFunctionTypeImpl` has its `type` defaulted to `TaskContinuationFunction`
which is incorrect because it has to append arguments, result type and account
for throws bit.

These changes expand `AsyncSignature` with `ContinuationType` and expand `FunctionType`
to include all of the appropriate information.
2021-12-09 16:52:53 -08:00
Connor Wakamo
f2e85a2b1f Merge pull request #40250 from cwakamo/eng/PR-85511438-treat-Playgrounds-trains-like-Xcode-trains
[CMake] Also treat builds in Playgrounds trains as "Xcode" builds.

This addresses <rdar://problem/85511438>.
2021-12-03 10:09:59 -08:00
Saleem Abdulrasool
349af3707d Merge pull request #40305 from compnerd/semitruck
gardening: make c++98-compat-extra-semi an error
2021-11-30 08:18:36 -08:00
Alastair Houghton
2d335062e2 [Runtime] Don't use threading related APIs when building single threaded.
Mostly this consists of #if-ing out code that we don't need when running
single threaded.

rdar://84393438
2021-11-30 16:18:26 +00:00
Saleem Abdulrasool
910fbee14e gardening: make c++98-compat-extra-semi an error
This cleans up 90 instances of this warning and reduces the build spew
when building on Linux.  This helps identify actual issues when
building which can get lost in the stream of warning messages.  It also
helps restore the ability to build the compiler with gcc.
2021-11-27 11:40:17 -08:00
Devin Coughlin
3abd7b0e1f Add a targetOSVersionAtLeast builtin.
This allows the compiler to parse the Swift swiftinterface file of a recent SDK.
2021-11-22 11:59:56 +01:00
Connor Wakamo
9367125f85 [CMake] Also treat builds in Playgrounds trains as "Xcode" builds.
These builds are meant to be as close to Xcode train builds as possible, so it
makes sense to treat them this way instead of letting them be treated as OS
builds or making a new kind of B&I build for them.

Playgrounds trains are identifiable via the `RC_PLAYGROUNDS` environment
variable being set to `YES`; if that isn't set, then it's not a Playgrounds
train.

This addresses <rdar://problem/85511438>.
2021-11-18 16:08:24 -08:00
John McCall
7880972608 Merge pull request #40198 from rjmccall/coop-global-executor-improvements
Miscellaneous improvements to the cooperative global executor
2021-11-16 12:16:26 -05:00
Arnold Schwaighofer
b90732cbbc Merge pull request #40089 from aschwaighofer/irgen_annotate_runtime_funs_with_willreturn
IRGen: Annotate runtime calls with `willreturn` attribute
2021-11-16 07:55:17 -08:00
John McCall
c45f2c4334 [NFC] Introduce a typedef for job delay in nanoseconds. 2021-11-16 01:50:52 -05:00
Kuba (Brecka) Mracek
d0cc5757b9 Avoid #including pthread.h when SWIFT_STDLIB_SINGLE_THREADED_RUNTIME is set (#40175) 2021-11-14 15:14:06 -08:00
Arnold Schwaighofer
2e4a86986c IRGen: Annotate runtime calls with willreturn attribute
`willreturn`
This function attribute indicates that a call of this function will
either exhibit undefined behavior or comes back and continues execution
at a point in the existing call stack that includes the current
invocation. Annotated functions may still raise an exception, i.a.,
`nounwind` is not implied. If an invocation of an annotated function does
not return control back to a point in the call stack, the behavior is
undefined.

I conservatively did not assume that the deinit is willreturn therefore
release like operations are not marked `willreturn`.

rdar://73574236
2021-11-12 07:58:11 -08:00
Erik Eckstein
ff2db93922 SIL/IRGen: analysis of runtime effects for an instruction
Define the possible runtime effects of an instruction in an enum `RuntimeEffect`.
Add a new utility `swift:getRuntimeEffect` to estimate the runtime effects of an instruction.

Also, add a mechanism to validate the correctness of the analysis in IRGen: annotate all runtime functions in RuntimeFunctions.def with the actual effect what the runtime function has or can have. Then check if the effects of emitted runtime functions for an instruction match what `getRuntimeEffect` predicts.
This check is only enabled on demand by defining the CHECK_RUNTIME_EFFECT_ANALYSIS macro in RuntimeEffect.h
2021-10-28 18:43:14 +02:00
Alastair Houghton
e339feb2da [Runtime] Add format string attributes.
Added some attributes to mark functions as taking format strings.

rdar://84571859
2021-10-25 13:46:05 +01:00
Kavon Farvardin
006e2b446b Replace destoryDistributedActor builtin with destroyDefaultActor
Currently, they both end up doing the exact same thing, so there's
no need for both.
2021-10-21 19:41:31 -07:00
Doug Gregor
305752e724 Simplify @objc actor back-deploy IR and switch to objc_getRequiredClass.
`objc_getRequiredClass` will produce a fatal error if the class isn't
found, which will prevent a malformed program using back-deployed @objc
actor from launching. Also eliminate the spurious `objc_opt_self`
call, which is unneeded given that we're realizing the metadata.

Thanks to Mike Ash for the review.
2021-10-06 08:47:04 -07:00
Doug Gregor
2551a04621 Back-deploy @objc actor types.
@objc actors implicitly inherit from the new, hidden
`SwiftNativeNSObject` class that inherits from `NSObject` yet provides
Swift-native reference counting, which is important for the actor
runtime's handling of zombies. However, `SwiftNativeNSObject` is only
available in the Swift runtime in newer OS versions (e.g., macOS
12.0/iOS 15.0), and is available in the back-deployed _Concurrency
library, but there is no stable place to link against for
back-deployed code. Tricky, tricky.

When back-deploying @objc actors, record `NSObject` as the superclass
in the metadata in the binary, because we cannot reference
`SwiftNativeNSObject`. Then, emit a static initializer to
dynamically look up `SwiftNativeNSObject` by name (which will find it
in either the back-deployment library, on older systems, or in the
runtime for newer systems), then swizzle that in as the superclass of
the @objc actor.

Fixes rdar://83919973.
2021-10-05 23:04:57 -07:00
John McCall
a76b304f00 Allow building a concurrent libSwiftConcurrency without libdispatch
The goal here is not to eventually implement a concurrent thread
pool ourselves.  We're just making it easier for integrators who
have their own pool and don't want to use Dispatch to build the
Swift concurrency runtime.  Just hook the right functions and
you should be fine.

The necessary functions to hook are:
- swift_task_enqueueGlobal
- swift_task_enqueueGlobalAfterDelay

The following functions *would* be necessary to hook:
- swift_task_enqueueMainExecutor
- swift_task_asyncMainDrainQueue (only if you have an async main?)
However, this configuration does not currently properly support
the main executor, and so `@MainActor` should be avoided for now.

rdar://83513751
2021-10-01 02:15:03 -04:00
Ben Langmuir
0e507f2f24 Revert "Allow building a concurrent libSwiftConcurrency without libdispatch" 2021-09-28 10:08:46 -07:00
John McCall
1df455bc14 Allow building a concurrent libSwiftConcurrency without libdispatch
The goal here is not to eventually implement a concurrent thread
pool ourselves.  We're just making it easier for integrators who
have their own pool and don't want to use Dispatch to build the
Swift concurrency runtime.  Just hook the right functions and
you should be fine.

The necessary functions to hook are:
- swift_task_enqueueGlobal
- swift_task_enqueueGlobalAfterDelay

The following functions *would* be necessary to hook:
- swift_task_enqueueMainExecutor
- swift_task_asyncMainDrainQueue (only if you have an async main?)
However, this configuration does not currently properly support
the main executor, and so `@MainActor` should be avoided for now.

rdar://83513751
2021-09-28 02:14:26 -04:00
Konrad `ktoso` Malawski
703595e396 [Concurrent] Warnings/errors should use "sendable" not "concurrent"
Resolves rdar://83416205
2021-09-23 12:45:07 +09:00
Kuba (Brecka) Mracek
bc1647a6dc Use vsnprintf instead of vsprintf in Portability.h (#39366) 2021-09-19 14:47:40 -07:00
Kuba (Brecka) Mracek
1142f613f8 (v)asprintf is supposed to set the returned pointer to NULL if printing fails, let's fix that in swift_vasprintf (#39338) 2021-09-16 19:18:55 -07:00
Kuba (Brecka) Mracek
de015c6464 Unify asprintf/vasprintf implementations and make them truly portable by using vsnprintf (#39314) 2021-09-15 21:09:03 -07:00
Doug Gregor
5b027ca456 Back-deploy creation of global-actor-qualified function type metadata.
When back-deploying, create global-actor-qualified function types via a
separate entrypoint
(`swift_getFunctionTypeMetadataGlobalActorBackDeploy`) in the
compatibility library, which checks whether it is running with a
new-enough runtime to use `swift_getFunctionTypeMetadataGlobalActor`.
Failing that, it calls into a separate copy of the implementation that
exists only in the back-deployed concurrency library.

Fixes rdar://79153988.
2021-09-13 10:35:47 -07:00