Commit Graph

14787 Commits

Author SHA1 Message Date
Doug Gregor
95ee2f231e Portability fix 2021-04-12 22:23:49 -07:00
Doug Gregor
28fa56df02 Portability for data race runtime reporting 2021-04-12 21:39:56 -07:00
Doug Gregor
e77a27e8ed [Concurrency] Introduce runtime detection of data races.
Through various means, it is possible for a synchronous actor-isolated
function to escape to another concurrency domain and be called from
outside the actor. The problem existed previously, but has become far
easier to trigger now that `@escaping` closures and local functions
can be actor-isolated.

Introduce runtime detection of such data races, where a synchronous
actor-isolated function ends up being called from the wrong executor.
Do this by emitting an executor check in actor-isolated synchronous
functions, where we query the executor in thread-local storage and
ensure that it is what we expect. If it isn't, the runtime complains.
The runtime's complaints can be controlled with the environment
variable `SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL`:

  0 - disable checking
  1 - warn when a data race is detected
  2 - error and abort when a data race is detected

At an implementation level, this introduces a new concurrency runtime
entry point `_checkExpectedExecutor` that checks the given executor
(on which the function should always have been called) against the
executor on which is called (which is in thread-local storage). There
is a special carve-out here for `@MainActor` code, where we check
against the OS's notion of "main thread" as well, so that `@MainActor`
code can be called via (e.g.) the Dispatch library's
`DispatchQueue.main.async`.

The new SIL instruction `extract_executor` performs the lowering of an
actor down to its executor, which is implicit in the `hop_to_executor`
instruction. Extend the LowerHopToExecutor pass to perform said
lowering.
2021-04-12 15:19:51 -07:00
Mike Ash
2167f5c797 Merge pull request #36850 from mikeash/isa-superclass-ptrauth-qualifiers
[Runtime] Use ptrauth qualifiers on metadata and superclass fields.
2021-04-12 16:37:09 -04:00
Joe Groff
13e6566e2e Compatibility50: Use __dso_handle for cheaper main executable check.
rdar://70959417
2021-04-12 13:01:45 -07:00
Mike Ash
3da4d20081 [Runtime] Use ptrauth qualifiers on metadata and superclass fields. 2021-04-12 12:59:49 -04:00
Benzi
4e0c6f99b8 [stdlib] Set.intersection iterate over smaller set (#36678)
* [stdlib] Set.intersection iterate over smaller set

When intersecting two sets, it is beneficial to iterate over the smaller sized set of the two, and check membership on the other. This speeds up runtime dramatically for cases where the current set is significantly larger than the set you are intersecting against.

* Review comments - variable names, implicit swap
2021-04-11 11:42:10 -04:00
Arnold Schwaighofer
ae2a4cca14 Merge pull request #36842 from aschwaighofer/fix_withCancellationHandler_arm64e_windows
Fix with cancellation handler arm64e windows
2021-04-10 14:10:12 -07:00
swift-ci
5db3caf9cc Merge pull request #36855 from apple/ktoso-patch-3 2021-04-09 19:53:03 -07:00
swift-ci
63b4cf0c9a Merge pull request #36689 from apple/tsan-task-groups 2021-04-09 18:23:38 -07:00
John McCall
2e8a19cdaf Merge pull request #36838 from rjmccall/task-group-abi-cleanup
Clean up the TaskGroup ABI
2021-04-09 20:15:41 -04:00
Konrad `ktoso` Malawski
34d1e27565 [Concurrency] runDetached shim must be static 2021-04-10 08:54:41 +09:00
Julian Lettner
29107180e5 Teach TSan about TaskGroups
`TaskGroup::offer(completedTask)`:
* Called from `AsyncTask::completeFuture()` which already takes care of
  `release(completedTask)`.  No additional edge is required if no task
  is waiting and completed task is stored so it can be retrieved by
  `group.poll()`.
* If group has waiting task, it will be dequeued and scheduled, add
  `acquire(waitingTask)`.

`TaskGroupImpl::poll(waitingTask)`:
* If no pending tasks, do nothing.
* If returning finished task, `acquire(finishedTask)`.
* If enqueuing waiting task, `release(waitingTask)`.

Note: `release()` should go before, and `acquire()` after the annotated
      synchronization operation.

Radar-Id: rdar://75910632
2021-04-09 14:44:53 -07:00
swift-ci
ea23988b5d Merge pull request #36802 from mikeash/concurrentreadablearray-operator-new-delete 2021-04-09 08:56:36 -07:00
Mike Ash
70354af2b5 [Runtime] Avoid operator new/delete in ConcurrentReadableArray.
The callbacks made in ImageInspectionMachO.cpp are called in a dangerous context, with the dyld and ObjC runtime locks held. C++ allows programs to overload the global operator new/delete, and there's no guarantee that those overloads behave. Ideally, we'd avoid them entirely, but that's a bigger job. For now, avoid the worst trouble by avoiding STL and new/delete in these callbacks. That use came from ConcurrentReadableArray's free list, so we switch that from a std::vector to a linked list.

rdar://75036800
2021-04-09 09:29:20 -04:00
Arnold Schwaighofer
141634aae7 Fix withCancellationHandler on arm64e 2021-04-09 05:30:24 -07:00
Doug Gregor
9fbb897096 Fix alignment of the default actor storage type 2021-04-09 00:36:39 -07:00
John McCall
efeb818161 Clean up the TaskGroup ABI:
- stop storing the parent task in the TaskGroup at the .swift level
- make sure that swift_taskGroup_isCancelled is implied by the parent
  task being cancelled
- make the TaskGroup structs frozen
- make the withTaskGroup functions inlinable
- remove swift_taskGroup_create
- teach IRGen to allocate memory for the task group
- don't deallocate the task group in swift_taskGroup_destroy

To achieve the allocation change, introduce paired create/destroy builtins.

Furthermore, remove the _swiftRetain and _swiftRelease functions and
several calls to them.  Replace them with uses of the appropriate builtins.
I should probably change the builtins to return retained, since they're
working with a managed type, but I'll do that in a separate commit.
2021-04-09 03:06:31 -04:00
Doug Gregor
eb6e74a0a6 Handle demangling/remangling for concurrency-related builtin types.
Fixes rdar://75990281.
2021-04-08 23:49:37 -07:00
Ben Cohen
125902923c Revert lazy mirror children (#36722) 2021-04-08 16:34:50 -10:00
Cassie Jones
37097c130a Merge pull request #36782 from porglezomp-misc/feeling-rather-cross-compiling
Skip adding alias targets when they don't exist
2021-04-08 19:04:51 -04:00
John McCall
82c190af21 Merge pull request #36751 from rjmccall/two-word-executor
Change ExecutorRef and fix the actor runtime
2021-04-08 16:49:37 -04:00
Philippe Hausler
eb0bd6afd3 [Concurrency] YieldingContinuation (#36730)
* Implement a YieldingContinuation type suitable for emitting values more than once via a yielding family of functions and awaiting production via next

* Add availability for YieldingContinuation and tests

* remove UnsafeConcurrentValue

* use UnsafeSendable for now

* Ensure the testing contexts are actually async

* Change the usages of Task.runDetached to Task.detach

* Change the usages of Task.detach to detach

* Transition to a external storage class outside of the generic, move to acqrel atomics, and change the error type to be enforced to Error existentials for next.

* Apply suggestions from code review

Co-authored-by: Nate Cook <natecook@apple.com>

* Remove inlines to allow for resilient changes

* Add unreachable cases in testing

Co-authored-by: Nate Cook <natecook@apple.com>
2021-04-08 12:12:04 -07:00
John McCall
0242d7572e Delay deallocation of default actors when they're currently running.
For ordinary memory-management reasons, this should only ever
happen when there will be no more uses of the actor outside of the
actor runtime.  The actor runtime, meanwhile, doesn't care about
anything except the default-actor control state of the actor.  So
we can just allow the rest of the actor to be destructed when it
isn't needed anymore, then destroy the actor state and deallocate
the object when we get around to switching off the executor.

This does assume that the task doesn't do anything which semantically
detects the executor it's on before switching off it, since doing so
might read a bogus executor.  However, we should only get an executor
in a zombie state like this when a hop has been removed or reordered,
and detection events should count as inhibiting that and forcing the
true executor to be switched to (and thus detected).

(But maybe lifetime optimization can make this happen?  Maybe we
need semantic detection to filter out zombie executors.)
2021-04-08 12:57:12 -04:00
John McCall
be8d2c83fa Give up default actors in swift_job_run if we switch to them.
Previously, if this happened, we simply left the actor in a running
state, causing any further jobs submitted to it to never be executed.
I can only speculate why this wasn't showing up in testing.

Also, change swift_job_run so that it prevents switching if the executor
passed in is not generic.  This is an entrypoint for arbitrary executors
and generally should not allow unexpected switching (if someday custom
executors participate in that scheme).  This infrastructure will also
be useful for implementing the `async let` semantics of running
synchronously until the task reaches a suspension point.

Finally, improve the #if'ed logging code throughout the task/actor runtime.
2021-04-08 12:57:12 -04:00
John McCall
156264f8e8 Make ExecutorRef two words. 2021-04-08 12:57:12 -04:00
Cassie Jones
2d3d5150bc [cmake] Skip primary variant alias in a cleaner way
Adjust the condition and message to skip the primary variant alias in a
way that's more clear and more directly matches the explanation in the
comments.

Co-authored-by: Eric Miotto <emiotto@apple.com>
2021-04-08 02:48:40 -04:00
Richard Wei
fb66de6126 Unify mangling operators for async, @Sendable, @differentiable and @noDerivative.
Repurpose mangling operator `Y` as an umbrella operator that covers new attributes on function types. Free up operators `J`, `j`, and `k`.

```
async ::= 'Ya'                             // 'async' annotation on function types
sendable ::= 'Yb'                          // @Sendable on function types
throws ::= 'K'                             // 'throws' annotation on function types
differentiable ::= 'Yjf'                   // @differentiable(_forward) on function type
differentiable ::= 'Yjr'                   // @differentiable(reverse) on function type
differentiable ::= 'Yjd'                   // @differentiable on function type
differentiable ::= 'Yjl'                   // @differentiable(_linear) on function type
```

Resolves rdar://76299796.
2021-04-07 17:49:10 -07:00
Richard Wei
82886bf77a [AutoDiff] Fix mangling of '@noDerivative' in function types.
`@noDerivative` was not mangled in function types, and was resolved incorrectly when there's an ownership specifier. It is fixed by this patch with the following changes:

* Add `NoDerivative` demangle node represented by a `k` operator.
    ```
    list-type ::= type identifier? 'k'? 'z'? 'h'? 'n'? 'd'?  // type with optional label, '@noDerivative', inout convention, shared convention, owned convention, and variadic specifier
    ```
* Fix `NoDerivative`'s overflown offset in `ParameterTypeFlags` (`7` -> `6`).
* In type decoder and type resolver where attributed type nodes are processed, add support for nested attributed nodes, e.g. `inout @noDerivative T`.
* Add `TypeResolverContext::InoutFunctionInput` so that when we resolve an `inout @noDerivative T` parameter, the `@noDerivative T` checking logic won't get a `TypeResolverContext::None` set by the caller.

Resolves rdar://75916833.
2021-04-07 15:42:12 -07:00
Nate Chandler
19d9ebd761 Revert "[AST] De/mangling for functions with generic sigs."
This reverts commit dbe689a3f2.
2021-04-06 15:41:43 -07:00
Cassie Jones
b2e91be6c7 Skip adding alias targets when they don't exist
The CMake uses the concept of a "primary variant" which isn't
necessarily aligned with either the host or target. In some cases, like
cross-compiling an iOS compiler toolchain for macosx-arm64, the expected
"primary variant" target will be missing and so CMake will fail. We can
skip adding the alias since build-script will call the more specific
target anyway.
2021-04-06 17:38:08 -04:00
nate-chandler
b293ce7953 Merge pull request #36729 from nate-chandler/rdar76118522
[ptrauth] Sign AsyncFunctionPointers as data.
2021-04-06 13:29:57 -07:00
Julian Lettner
e1b82d852d Remove dlsym() lookup for TSan functions from common path (#36478)
* Move TSan function lookup out of the common path

Move TSan function lookup via `dlsym()` out of the common path.  The
TSan runtime will now call `__tsan_on_initialize()` which we can use to
initialize the TSan functions in the Swift runtime.

This avoids paying the cost of `dlsym()` in the common, non-TSan case.

Depends on: https://reviews.llvm.org/D98810

rdar://75493372

* Remove Windows code

Thread Sanitizer is not supported on Windows.

Co-authored-by: Julian Lettner <julian.lettner@apple.com>
2021-04-06 09:16:47 -07:00
swift-ci
56b86b6cf1 Merge pull request #36766 from ktoso/wip-add-deprecated-shims-old-task-api 2021-04-06 00:50:33 -07:00
Konrad `ktoso` Malawski
e6b7e5e989 Merge pull request #36307 from ktoso/wip-yield
[Concurrency] Simple Task.yield implementation
2021-04-06 13:19:34 +09:00
Konrad `ktoso` Malawski
74eee6a79c [Concurrency] shim for deprecated Task.withCancellationHandler 2021-04-06 13:07:11 +09:00
Konrad `ktoso` Malawski
d8b4e3c304 [Concurrency] add deprecated shim for Task.withGroup 2021-04-06 12:51:55 +09:00
Konrad `ktoso` Malawski
39e97c57f2 [Concurrency] add deprecated shim for runDetached 2021-04-06 11:17:25 +09:00
Nate Chandler
d3e0e7af07 [ptrauth] Signed AsyncFunctionPointers as data.
Previously, AsyncFunctionPointer constants were signed as code.  That
was incorrect considering that these constants are in fact data.  Here,
that is fixed.

rdar://76118522
2021-04-05 18:24:41 -07:00
Michael Gottesman
2757feafe8 Merge pull request #36762 from apple/revert-36614-reasync-logical-operators
Revert "stdlib: Add reasync variants of '&&', '||' and '??'"
2021-04-05 17:25:02 -07:00
nate-chandler
b9f38446b9 Merge pull request #36707 from nate-chandler/rdar76061892
[Concurrency] Added swift_deletedAsyncMethodError.
2021-04-05 17:01:53 -07:00
Doug Gregor
fcd5d43457 Revert "stdlib: Add reasync variants of '&&', '||' and '??'" 2021-04-05 16:45:44 -07:00
Alex Martini
1c5c111aab Merge pull request #36709 from amartini51/main
Doc comment corrections
2021-04-05 15:46:31 -07:00
Konrad `ktoso` Malawski
9949bf440d updated to latest Task APIs 2021-04-06 07:20:07 +09:00
Alexis Laferrière
ed16e1bd1c Merge pull request #36666 from xymus/nullable-memcmp
[SwiftShims] Make the void* arguments of memcmp nullable on Apple targets
2021-04-05 13:55:56 -07:00
Nate Chandler
e2d341fb74 [Concurrency] Added swift_deletedAsyncMethodError.
The new async function will be used as a placeholder  in VTables and
WTables for async functions which have been deleted.

rdar://76061892
2021-04-05 08:15:31 -07:00
Konrad `ktoso` Malawski
fde8098a37 [Concurrency] remove workaround for void task handles rdar://74957357 2021-04-05 16:59:00 +09:00
Konrad `ktoso` Malawski
a180f532b5 [Concurrency] Simple Task.yield implementation 2021-04-05 16:59:00 +09:00
Konrad `ktoso` Malawski
67ef887fe8 Merge pull request #36602 from ktoso/wip-task-revisions
[Concurrency] Update Task/TaskGroup APIs based on first review
2021-04-05 12:43:16 +09:00
Michael Ilseman
9ab21d3215 Merge pull request #36623 from xwu/better-atoi
[stdlib][SR-7556] Re-implement string-to-integer parsing
2021-04-03 08:30:25 -06:00