Commit Graph

88 Commits

Author SHA1 Message Date
Mike Ash
6aab257c33 [Concurrency] Add compatibility overrides to Concurrency library.
Take the existing CompatibilityOverride mechanism and generalize it so it can be used in both the runtime and Concurrency libraries. The mechanism is preprocessor-heavy, so this requires some tricks. Use the SWIFT_TARGET_LIBRARY_NAME define to distinguish the libraries, and use a different .def file and mach-o section name accordingly.

We want the global/main executor functions to be a little more flexible. Instead of using the override mechanism, we expose function pointers that can be set by the compatibility library, or by any other code that wants to use a custom implementation.

rdar://73726764
2021-03-22 11:09:06 -04:00
Arnold Schwaighofer
cf55973973 Remove unused ResumeParentExecutor 2021-03-18 09:05:45 -07:00
Arnold Schwaighofer
3bdd5cb99a IRGen: async error ABI
Throwing functions pass the error result in `swiftself` to the resume
partial function.
Therefore, `() async -> ()` to `() async throws -> ()` is not ABI compatible.

TODO: go through remaining failing IRGen async tests and replace the
illegal convert_functions.
2021-03-17 17:17:12 -07:00
Arnold Schwaighofer
f75fbb7594 IRGen: Async ABI passing parameter and results directly
The error is still passed in the async context. I will fix this in a
follow-up.
2021-03-17 07:41:01 -07:00
John McCall
6c879d6fd3 Change the async ABI to not pass the active task and executor.
Most of the async runtime functions have been changed to not
expect the task and executor to be passed in.  When knowing the
task and executor is necessary, there are runtime functions
available to recover them.

The biggest change I had to make to a runtime function signature
was to swift_task_switch, which has been altered to expect to be
passed the context and resumption function instead of requiring
the caller to park the task.  This has the pleasant consequence
of allowing the implementation to very quickly turn around when
it recognizes that the current executor is satisfactory.  It does
mean that on arm64e we have to sign the continuation function
pointer as an argument and then potentially resign it when
assigning into the task's resume slot.

rdar://70546948
2021-03-16 22:52:54 -04:00
swift-ci
549923513a Merge pull request #36216 from apple/tsan-swift-concurrency2 2021-03-16 19:11:20 -07:00
Nate Chandler
ee63777332 [IRGen] Fix ABI for thick async functions.
Previously, thick async functions were represented sometimes as a pair
of (AsyncFunctionPointer, nullptr)--when the thick function was produced
via a thin_to_thick_function, e.g.--and sometimes as a pair of
(FunctionPointer, ThickContext)--when the thick function was produced by
a partial_apply--with the size stored in the slot of the ThickContext.

That optimized for the wrong case: partial applies of dynamic async
functions; in that case, there is no appropriate AsyncFunctionPointer to
form when lowering the partial_apply instruction.  The far more common
case is to know exactly which function is being partially applied.  In
that case, we can form the appropriate AsyncFunctionPointer.

Furthermore, the previous representation made calling a thick function
more complex: it was always necessary to check whether the context was
in fact null and then proceed along two different paths depending.

Here, that behavior is corrected by creating a thunk in a mandatory
IRGen SIL pass in the case that the function that is being partially
applied is dynamic.  That new thunk is then partially applied in place
of the original partial_apply of the dynamic function.
2021-03-15 13:37:40 -07:00
Julian Lettner
99bcd87af6 Use swift_once instead of C++ static initializers 2021-03-12 14:52:56 -08:00
Mike Ash
bedca5ed61 Merge pull request #36139 from mikeash/async-task-dispatch-integration
[Concurrency] Make Job/AsyncTask minimally compatible with dispatch object layout
2021-03-11 18:04:04 -05:00
Mike Ash
bd62fdb2db [Concurrency] Make Job/AsyncTask minimally compatible with dispatch object layout
Create a TargetDispatchClassMetadata for Swift metadata that also has a dispatch-compatible vtable. Dispatch leaves room for ObjC class metadata so the two regions don't overlap. (The vtable currently consists of a single dummy entry; this will be filled out later.)

Rearrange the Job and AsyncTask hierarchy so that AsyncTask inherits only from Job, which in turn inherits from HeapObject. This gives all Job instances a dispatch-compatible isa field. It also gives them a refcount word, which is wasted on instances that aren't AsyncTask instances. Maybe we can find some use for that space in the future.

rdar://75227953
2021-03-10 10:04:30 -05:00
Joe Groff
872afda50b Merge pull request #36298 from jckarter/created-task-closure-context-leak
SIL: Clean up ownership handling in `createAsyncTask` builtins.
2021-03-09 14:17:13 -08:00
Joe Groff
d9798c0868 Concurrency: Redo non-_f variants of swift_task_create to accept closures as is.
In their previous form, the non-`_f` variants of these entry points were unused, and IRGen
lowered the `createAsyncTask` builtins to use the `_f` variants with a large amount of caller-side
codegen to manually unpack closure values. Amid all this, it also failed to make anyone responsible
for releasing the closure context after the task completed, causing every task creation to leak.
Redo the `swift_task_create_*` entry points to accept the two words of an async closure value
directly, and unpack the closure to get its invocation entry point and initial context size
inside the runtime. (Also get rid of the non-future `swift_task_create` variant, since it's unused
and it's subtly different in a lot of hairy ways from the future forms. Better to add it later
when it's needed than to have a broken unexercised version now.)
2021-03-08 16:54:19 -08:00
Joe Groff
016496384e Concurrency: Remove currently-unused non-_f variants of swift_task_create
I'm about to replace these with new variations that correctly handle spawning a task with
a closure as its initial entry point, without leaking or requiring large amounts of caller-side
code generation.
2021-03-05 12:01:06 -08:00
Konrad `ktoso` Malawski
aedbbe615d [TaskGroup] Towards ABI stability of groups 2021-03-02 20:25:22 +09:00
Konrad `ktoso` Malawski
fcb1c01a36 [TaskLocal] Use the task-local stack discipline allocator 2021-03-02 11:14:41 +09:00
Julian Lettner
a3ccdd29d8 Teach TSan about Swift Task and Actor model
I have identified the following conceptual synchronization points at
which task data and computation can cross thread boundaries.  We need to
model these in TSan to avoid false positives:

Awaiting an async task (`AsyncTask::waitFuture`), which has two cases:
1) The task has completed (`AsyncTask::completeFuture`).  Everything
   that happened during task execution "happened before" before the
   point where we access its result.  We synchronize on the *awaited*
   task.
2) The task is still executing: the current execution is suspended and
   the waiting task is put into the list of "waiters".  Once the awaited
   task completes, the waiters will be scheduled.  In this case, we
   synchronize on the *waiting* task.

Note: there is a similar relationship for task groups which I still have
to investigate.  I will follow-up with an additional patch and tests.

Actor job execution (`swift::runJobInExecutorContext`):
Job scheduling (`swift::swift_task_enqueue`) precedes/happens before job
execution.  Also all job executions (switching actors or suspend/resume)
are serially ordered.

Note: the happens-before edge for schedule->execute isn't strictly
needed in most cases since scheduling calls through to libdispatch's
`dispatch_async_f`, which we already intercept and model in TSan.
However, I am trying to model Swift Task semantics to increase the
chance of things to continue to work in case the "task backend" is
switched out.

rdar://74256733
2021-03-01 13:41:55 -08:00
Konrad `ktoso` Malawski
d7169edc21 [TaskLocals] Cleanly separate locals impl from Task, no need for fragment 2021-03-02 00:54:47 +09:00
Konrad `ktoso` Malawski
de5fdcd2f8 [TaskGroup] fix missing retain in scheduling next() immediately on offer 2021-02-24 18:45:33 +09:00
Konrad `ktoso` Malawski
6f4fca8721 Merge branch 'main' into wip-no-escape-group 2021-02-24 08:59:53 +09:00
John McCall
2012195cd5 Alter the runtime interface for awaiting futures and task groups.
First, just call an async -> T function instead of forcing the caller
to piece together which case we're in and perform its own copy.  This
ensures that the task is actually kept alive properly.

Second, now that we no longer implicitly depend on the waiting tasks
being run synchronously, go ahead and schedule them to run on the
global executor.

This solves some problems which were blocking the work on TLS-ifying
the task/executor state.
2021-02-21 23:48:13 -05:00
Konrad `ktoso` Malawski
07d6715d9f cleanup: remove all println debugging from task infra 2021-02-22 13:26:38 +09:00
Konrad `ktoso` Malawski
999758cc85 [Concurrency][TaskGroup] allow cancelAll be invoked from child tasks 2021-02-22 13:26:33 +09:00
Konrad `ktoso` Malawski
485a894268 [Concurrency] give up on child task (async let) record storage for now 2021-02-22 13:26:33 +09:00
Konrad `ktoso` Malawski
1c4655df6a [Concurrency] track child tasks from group in Task records 2021-02-22 13:26:33 +09:00
Konrad `ktoso` Malawski
efc7d8e627 [Concurrency] rearrange layout of AsyncTask now that task locals and groups coexist 2021-02-22 13:26:33 +09:00
Konrad `ktoso` Malawski
b1e5c4a2c4 test cleanups, additional test to check we dont cancel other groups 2021-02-22 13:26:28 +09:00
Konrad `ktoso` Malawski
1860b20cb7 [Concurrency] cancel all child tasks of a group on cancelAll 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
854cade5d6 cleanups, cancellation 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
a368aa1872 [Concurrency] store child record, cancellation works; async_task_cancellation 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
edbcb1ea9e [Concurrency] task cancellation propagate through children 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
1970dbfdae fix over releasing the current task by group dealloc 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
a226259d84 [Concurrency] TaskGroup moves out of AsyncTask, non escaping body 2021-02-22 13:26:27 +09:00
John McCall
8e9823c369 Store the current task and executor in task-local storage. 2021-02-21 21:39:14 -05:00
Arnold Schwaighofer
4373bdd6d0 Conditionally start using llvm::CallingConv::SwiftTail for async functions
This is conditional on UseAsyncLowering and in the future should also be
conditional on `clangTargetInfo.isSwiftAsyncCCSupported()` once that
support is merged.

Update tests to work either with swiftcc or swifttailcc.
2021-02-18 09:25:15 -08:00
Konrad `ktoso` Malawski
40b6b18945 [Concurrency] implement withCancellationHandler via records 2021-02-18 17:27:15 +09:00
Varun Gandhi
7eeabd9300 [Runtime] Fix calling convention of async function.
Otherwise, we run into a type error on turning on swiftasync.
2021-02-17 10:10:35 -08:00
nate-chandler
de63a23e77 Merge pull request #35603 from nate-chandler/concurrency/irgen/rdar71378532
[Async CC] Make error indirect.
2021-02-16 07:39:14 -08:00
Konrad `ktoso` Malawski
d2bd6abe61 [Concurrency] TaskLocals allow configuring inheritance: never 2021-02-13 20:09:11 +09:00
Konrad `ktoso` Malawski
b811b12246 [Concurrency] TaskLocals lookup "skip" optimization 2021-02-13 10:39:22 +09:00
Konrad `ktoso` Malawski
1044723787 [Concurrency] Initial Task Local Values implementation 2021-02-13 10:39:22 +09:00
Nate Chandler
e2a8abc9e5 [Async CC] Make error indirect.
Previously, the error stored in the async context was of type SwiftError
*.  In order to enable the context to be callee released, make it
indirect and change its type to SwiftError **.

rdar://71378532
2021-02-11 11:34:47 -08:00
Nate Chandler
c59b01feee [Runtime] Ptrauth for runAsyncAndBlock.
rdar://72357371
2021-02-04 20:19:26 -08:00
Mike Ash
662b553ffe Merge pull request #35620 from mikeash/debug-var-async-task-metadata
[Concurrency] Add a debug variable pointing to the metadata for AsyncTask.
2021-01-29 23:31:17 -05:00
Saleem Abdulrasool
3cf257fc84 Merge pull request #35585 from compnerd/runless
Concurrency: avoid `CFRunLoopRun` on non-Darwin
2021-01-29 08:50:42 -08:00
Mike Ash
329af74b15 [Concurrency] Add a debug variable pointing to the metadata for AsyncTask.
Debugging tools can use _swift_concurrency_debug_asyncTaskMetadata to identify memory blocks as async task allocations by looking at their isa/metadata pointer.

rdar://72906895
2021-01-27 13:45:22 -05:00
Arnold Schwaighofer
b0d31730de Merge pull request #35303 from aschwaighofer/introduce_swiftasync_lowering
Add llvm::Attribute::SwiftAsync to the context parameter
2021-01-26 06:03:19 -08:00
Saleem Abdulrasool
472e842215 Concurrency: avoid CFRunLoopRun on non-Darwin
The CoreFoundation implementation on non-Darwin platforms included in
Foundation is not meant to be used as a general purpose CoreFoundation
implementation.  Since non-Darwin targets do not default to providing a
CoreFoundation implementation, we should generally assume that the
symbol is not present.  This changes the non-Darwin paths to always use
`dispatch_main` rather than `CFRunLoopRun`.
2021-01-25 09:26:08 -08:00
Saleem Abdulrasool
4f740a4784 Concurrency: support draining the main queue in single threaded mode
When built in single threaded mode, the runtime does not use dispatch to
queue the tasks.  As a result, pumping the dispatch main queue will
simply wait indefinitely.  In the single threaded mode, simply halt
execution and drain all pending tasks before returning.  This allows
forward progress in the single threaded mode.
2021-01-25 09:23:04 -08:00
Saleem Abdulrasool
3c9e76d5fe Concurrency: attempt to handle Windows draining
Rather than just simply invoking abort on Windows, attempt to load
dispatch and execute the main loop by looking up `dispatch_main` in the
module.  Assuming that dispatch was used already, the `LoadLibraryW`
will provide the handle to the module currently mapped in.  This still
is not correct, since we do not link to libdispatch, so we cannot have
invoked any dispatch queuing methods.  However, this is better than the
previous behaviour of simply aborting.

This resolves the symptom in SR-14086, but not the underlying problem.
2021-01-24 14:44:11 -08:00
Konrad `ktoso` Malawski
80ee936a72 Revert "[Concurrency] isCanceled spelling to follow guidance" 2021-01-23 07:27:34 +09:00