Commit Graph

858 Commits

Author SHA1 Message Date
Mike Ash
d6fc306b16 [Reflection] Improve size computation for remote type metadata reading.
Take advantage of the ability to iterate over TrailingObjects when computing the size of certain kinds of type metadata. This avoids the occasional issue where a new trailing object is added to a type, the remote metadata reader isn't fully updated for it, and doesn't read enough data. This change fixes an issue with function type metadata where we didn't read the global actor field.

Not all type metadata is amenable to this, as some don't use TrailingObjects for their trailing data (e.g. various nominal types) and extended existentials need to dereference their Shape pointer to determine the number of TrailingObjects, which needs some additional code when done remotely. We are able to automatically calculate the sizes of Existential and Function.

rdar://162855053
2025-11-21 19:55:32 -05:00
Bryce Wilson
b30f63530a [Concurrency] Set thread base priority when running escalated Tasks (#84895) 2025-11-20 09:58:08 +09:00
John McCall
13937fdb4e Merge pull request #84528 from rjmccall/async-let-runtime-realism
Model async let begin/finish as builtins in SIL
2025-11-05 10:24:46 -08:00
John McCall
3cfda35b7c Remove the unused swift_asyncLet_{start,end,wait,wait_throwing} runtime
functions.

These were introduced in an early draft implementation of async let, but
never used by a released compiler. They are not used as symbols by any
app binaries. There's no reason to keep carrying them.

While I'm at it, dramatically improve the documentation of the remaining
async let API functions.
2025-11-03 13:45:18 -08:00
Nate Chandler
e0ead75622 [CoroutineAccessors] Use typed-malloc. 2025-10-30 03:59:18 -07:00
Nate Chandler
76881b1d52 [CoroutineAccessors] Add frame de/alloc functions.
To enable ABIs which store extra info in the frame, add two new slots to
the coroutine allocator function table.  For example, a frame could have
a header containing a context pointer at a negative offset from the
address returned from `swift_coro_alloc_frame`.  The frame deallocation
function would then know to deallocate more space correspondingly.
2025-10-22 21:00:17 -07:00
Nate Chandler
f8666b164b [CoroutineAccessors] Pass cator to allocation fns.
Doing so enables allocators to contain additional context for use by
allocation functions.  Because the allocator is already passed to
_swift_coro_alloc, on the fast path (no allocator, popless) no
allocation function is used, and the allocator is passed in the
swiftcoro register, this is cheap.
2025-10-22 10:59:09 -07:00
nate-chandler
454160bca5 Merge pull request #84831 from nate-chandler/general-coro/20251009/1
[CoroutineAccessors] Sign de/allocation functions.
2025-10-13 20:03:09 -07:00
Nate Chandler
603dc2e0b3 [CoroutineAccessors] Sign de/allocation functions.
Allocator structs are passed in to new ABI yield-once coroutines and
contain pointers to functions to de/allocate memory.  Here, those
pointers are signed.
2025-10-10 16:32:19 -07:00
Adrian Prantl
75bee892be Add LLDB and swift-reflection-dump support for WebAssembly
This patch adds parsing and extracting of the Swift reflection
metadata data segments from within the WebAssembly DATA section and
tests it using swift-reflection-dump. This is needed to allow LLDB to
acces Swift reflection metadata when attached to WebAssembly
processes.

rdar://159217213
2025-08-27 17:14:17 -07:00
Pavel Yaskevich
53ef38e31b [AST] NFC: Rename function type isolation NonisolatedCaller to NonisolatedNonsending
This reduces the number of ways we refer to caller isolated async
functions and matches the name to the attribute spelling.
2025-08-05 17:22:10 -07:00
Max Desiatov
6d042aeb4a Embedded WASI: fix storeEnumTagSinglePayload type mismatch (#83480)
This is a follow up to https://github.com/swiftlang/swift/pull/80862, where `storeEnumTagSinglePayload` in a special implementation of `ResultTypeInfo` for Embedded Swift had a mismatching number of arguments. The actual declaration of it in `ABI/ValueWitness.def` clearly includes one more argument.

```
/// void (*storeEnumTagSinglePayload)(T* enum, UINT_TYPE whichCase,
///                                   UINT_TYPE emptyCases, M *self);
/// Given uninitialized memory for an instance of a single payload enum with a
/// payload of this witness table's type (e.g Optional<ThisType>), store the
/// tag.
FUNCTION_VALUE_WITNESS(storeEnumTagSinglePayload,
                       StoreEnumTagSinglePayload,
                       VOID_TYPE,
                       (MUTABLE_VALUE_TYPE, UINT_TYPE, UINT_TYPE, TYPE_TYPE))
```

This function type mismatch is illegal when targeting Wasm and traps at run time.

Similarly to #80862, we're passing `nullptr` as the newly added argument, which is equivalent to the existing behavior on other platforms.

rdar://157219474
2025-08-01 15:38:59 -07:00
Max Desiatov
1eef09ad34 Embedded WASI: fix InitializeWithCopy VWT type mismatch
Fixes Embedded Swift concurrency tests that crash with this stack trace:

```
Error: Trap: indirect call type mismatch, expected FunctionType(parameters: [WasmTypes.ValueType.i32, WasmTypes.ValueType.i32], results: []), got FunctionType(parameters: [W
asmTypes.ValueType.i32, WasmTypes.ValueType.i32, WasmTypes.ValueType.i32], results: [WasmTypes.ValueType.i32])
    0: swift::AsyncTask::completeFuture(swift::AsyncContext*)
    1: completeTaskAndRelease(swift::AsyncContext*, swift::SwiftError*)
    2: completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*)
    3: $exIeAgHr_xs5Error_pIegHrzo_s8SendableRzs5NeverORs_r0_lTRyt_Tg5TATQ0_
    4: $exIeAgHr_xs5Error_pIegHrzo_s8SendableRzs5NeverORs_r0_lTRyt_Tg5TQ0_
    5: $e1a4MainV4mainyyYaFZyyYacfU_TATQ0_
    6: $e1a4MainV4mainyyYaFZyyYacfU_TY1_
    7: swift_task_switch
    8: $e1a4MainV4mainyyYaFZyyYacfU_TQ0_
    9: $e1a4testSiyYaFTY0_
    10: swift::runJobInEstablishedExecutorContext(swift::Job*)
    11: swift_job_run
    12: $es19CooperativeExecutorC8runUntilyySbyXEKF
    13: $es19CooperativeExecutorCs07RunLoopB0ssACP3runyyKFTW
    14: swift_task_asyncMainDrainQueueImpl
    15: swift_task_asyncMainDrainQueue
    16: main
    17: __main_void
    18: _start
    19: unknown
```
2025-08-01 13:03:10 +01:00
Alastair Houghton
57d056e1a3 Merge pull request #82457 from al45tair/external-executor-test-support
[Concurrency] Add functions to allow testing of external executors.
2025-07-04 09:52:57 +01:00
Alastair Houghton
26878c98de [Concurrency] Add functions to allow testing of external executors.
Added a couple of functions to allow for the testing of executors that
aren't implemented inside the concurrency runtime itself.

rdar://154195821
2025-07-01 16:48:03 +01:00
Joe Groff
7e99e84070 Add an "addressable for dependencies" flag to value witness flags.
This may be useful for type layout of borrow fields in the future, should we
decide that addressable-for-dependencies borrows should always be represented
by a pointer. rdar://153650278
2025-06-18 12:47:16 -07:00
Konrad 'ktoso' Malawski
2c99a669d3 Correct priority cancellation handler signing once more
Resolves rdar://150378890 for real this time, verified on arm64e myself
2025-06-02 21:32:57 +09:00
Mike Ash
ea785c191c [Runtime] Add ptrauth attribute to TargetGlobalActorReference conformance pointer.
When the TargetGlobalActorReference conformance is an indirect pointer, the indirect pointer is signed when ptrauth is enabled.

rdar://151945202
2025-05-28 21:06:20 -04:00
Konrad 'ktoso' Malawski
97a4904c1f [Concurrency] Correct the signing of priority escalation handler 2025-05-23 17:02:19 -07:00
Alejandro Alonso
9e24563bb7 Merge pull request #81365 from Azoy/my-existentials
[AST & Runtime] Correctly mangle extended existentials with inverse requirements
2025-05-12 13:21:07 -07:00
Pavel Yaskevich
001eab867d [stdlib] SE-0472: Rename Task and*TaskGroup APIs to match the proposal
`Task.startSynchronously` -> `Task.immediate`
`*TaskGroup.startTaskSynchronously{UnlessCancelled}` -> `*TaskGroup.addImmediateTask{UnlessCancelled}`
2025-05-09 23:59:30 -07:00
Alejandro Alonso
d26bde798e Start building the runtime demangle tree for extended existentials 2025-05-07 13:56:25 -07:00
Konrad 'ktoso' Malawski
8fccafd2fa [Concurrency] Fix ptr auth for task priority escalation handler
We missed to sign the handler. Along the way the signature of it
changed, so adjust for that.

How to get the number:

```
func PROPER(bar: (TaskPriority, TaskPriority) -> Void) {
    let p = TaskPriority.default
    bar(p, p)
}
```

```
-> % swiftc -target arm64e-apple-macos13 example.swift -S -o - | swift demangle | grep -a3 autda
	stur	x8, [x29, #-64]
	mov	x17, x8
	movk	x17, #11839, lsl #48 <<<<<<<<<
	autda	x16, x17
	ldr	x8, [x16, #64]
	lsr	x8, x8, #0
	add	x8, x8, #15
```

Resolves rdar://150378890
2025-05-01 12:07:35 +09:00
Konrad 'ktoso' Malawski
6872707574 [embedded][Concurrency] Further refine C-api boundary for isIsolating... 2025-04-29 22:29:26 +09:00
Konrad 'ktoso' Malawski
ea16df04ac [Concurrency] adjust how we fail creating an IsIsolatingCurrentContextDecision 2025-04-29 20:08:56 +09:00
Konrad 'ktoso' Malawski
e1dc854f8f [Concurrency] Change isIsolatingCurrent... to return Bool?
This changes the isIsolatingCurrentContext function to return `Bool?`
and removes all the witness table trickery we did previously to detect
if it was implemented or not. This comes at a cost of trying to invoke
it always, before `checkIsolated`, but it makes for an simpler
implementation and more checkable even by third party Swift code which
may want to ask this question.

Along with the `withSerialExecutor` function, this now enables us to
check the isolation at runtime when we have an `any Actor` e.g. from
`#isolation`.

Updates SE-0471 according to
https://forums.swift.org/t/se-0471-improved-custom-serialexecutor-isolation-checking-for-concurrency-runtime/78834/
review discussions
2025-04-29 20:08:55 +09:00
Max Desiatov
adfab01f4e Embedded: fix arguments mismatch in ResultTypeInfo (#80862)
Mismatched number of arguments traps on Wasm. Second argument of `destroy` in VW seems unused, so let's pass `nullptr` as a workaround.
2025-04-23 11:31:13 +01:00
Konrad 'ktoso' Malawski
80f384958a [Concurrency] Parent task cancellation must cancel task group itself.
Seems that during refactorings of child cancellations we somehow missed
also cancelling the group itself. It seems we did not have good test
coverage of the addTaskUnlessCancelled somehow and thus this slipped
through.

This adds a regression test for addTaskUnlessCancelled and fixes how we
handle the cancellation effect in TaskStatus.

resolves #80789
resolves rdar://149177600
2025-04-14 17:06:44 +09:00
Alastair Houghton
ef3ea9bd27 [Concurrency] Fix size of AsyncTask::PrivateStorage.
Because `TaskAllocator` is not a round multiple of the machine word
size on 64-bit platforms, I think we end up with padding before the
`TaskLocal::Storage` following it, which makes the `PrivateStorage`
structure larger than the calculation in `ABI/Task.h`.

rdar://149067144
2025-04-11 17:12:16 +01:00
Mike Ash
8fa76431e2 [Concurrency] Move SWIFT_CONCURRENCY_ENABLE_PRIORITY_ESCALATION to ABI/Task.h.
We check it in Task.h so it needs to be defined by that point.
2025-04-09 16:16:36 -04:00
Mike Ash
4290f0add4 Merge pull request #80447 from mikeash/fix-task-sizes
[Concurrency] Fix task status and private storage sizes.
2025-04-09 13:14:40 -04:00
Nate Chandler
66ca0d8d4b [CoroutineAccessors] Use async bit in descriptors.
To facilitate back deployment, make use of the fact that the async bit
has up to now never been set for read and modify accessors and claim
that set bit to indicate that it is a callee-allocated coroutine.  This
has the virtue of being completely back deployable because like async
function pointers coro function pointers must be auth'd and signed as
data.
2025-04-03 19:49:44 -07:00
Mike Ash
d1cb5d9c26 [Concurrency] Fix task status and private storage sizes.
ActiveTaskStatusSize is 2 words, not 4, on most targets.

PrivateStorageSize is measured in bytes, not words, so the Storage field needs to be a char[].
2025-04-01 16:05:52 -04:00
Mike Ash
ecdcb82b97 Merge pull request #80316 from mikeash/lock-record-begone
[Concurrency] Eliminate StatusRecordLockRecord.
2025-03-28 17:18:40 -04:00
nate-chandler
b878155a5a Merge pull request #80335 from nate-chandler/general-coro/20250326/2
[CoroutineAccessors] Move functions and add dealloc bit.
2025-03-27 19:19:25 -07:00
Nate Chandler
2412ac3dc4 [CoroutineAccessors] Dedicate bit to dealloc time.
Don't rely on the kind for this.  Allow each allocator to decide for
itself for future compatibility.
2025-03-27 11:37:26 -07:00
Doug Gregor
43df05a89c [SE-0470] Prohibit isolated conformances in dynamic casts marked as such
Certain dynamic casts cannot work safely with isolated conformances,
regardless of what executor the code runs on. For such cases, reject
all attempts to conform to the type.
2025-03-26 22:31:52 -07:00
Mike Ash
6397e30856 [Concurrency] Eliminate StatusRecordLockRecord.
Move to a recursive lock inline in the Task. This avoids the need to allocate a lock record and simplifies the code somewhat.

Change Task's OpaquePrivateStorage to compute its size at build time based on the sizes of its components, rather than having it be a fixed size. It appears that the fixed size was intended to be part of the ABI, but that didn't happen and we're free to change this size. We need to expand it slightly when using pthread_mutex as the recursive lock, as pthread_mutex is pretty big. Other recursive locks allow it to shrink slightly.

We don't have a recursive mutex in our Threading support code, so add a RecursiveMutex type.

rdar://113898653
2025-03-26 14:52:37 -04:00
Nate Chandler
5534eb4043 [DefaultOverides] Install in vtables at runtime. 2025-03-25 07:22:44 -07:00
Nate Chandler
2cdbfa77bf [DefaultOverrides] IRGen. 2025-03-25 07:22:43 -07:00
Mike Ash
1e265c24d7 Merge pull request #80096 from mikeash/task-group-cancellation-lock-fix
[Concurrency] Fix a race when using cancelAll on a task group concurrently with child tasks being removed.
2025-03-20 11:53:31 -04:00
Nate Chandler
a4bf94dc95 [CoroutineAccessors] Store CFPs in descriptors.
As with AFPs, store CFPs in various descriptors.  Extend the execution
test correspondingly.
2025-03-19 12:03:24 -07:00
Mike Ash
38c4fce1a8 [Concurrency] Fix a race when using cancelAll on a task group concurrently with child tasks being removed.
_swift_taskGroup_cancelAllChildren relies on there being no concurrent modification when called from the owning task, but this is not guaranteed.

Rearrange things to always take the owning task's status record lock when walking the group's children. Split _swift_taskGroup_cancelAllChildren into two functions, one which assumes/requires the lock is already held, and one which acquires the lock. We don't have the owning task in this case, but we can either get it from the current task, or by looking at the parent of the child task we're working on.

rdar://147172991
2025-03-18 14:52:07 -04:00
Konrad `ktoso` Malawski
85fcd69833 [Concurrency] Implement detecting isIsolatingCurrentContext user impls (#79946)
* [Concurrency] Detect non-default impls of isIsolatingCurrentContext

* [Concurrency] No need for trailing info about isIsolating... in conformance

* Apply changes from review
2025-03-18 09:39:11 +09:00
Konrad `ktoso` Malawski
85e4685f90 [Concurrency] Adjust task escalation APIs to SE accepted shapes (#79988)
* [Concurrency] Adjust task escalation APIs to SE accepted shapes

* adjust test a little bit

* Fix closure lifetime in withTaskPriorityEscalationHandler

* avoid bringing workaround func into abi by marking AEIC
2025-03-18 07:05:11 +09:00
Dave Lee
8ad295aa02 Fix a comment reference to TaskGroupBase (#79622)
Update a reference to `TaskGroupImpl` to `TaskGroupBase`.
2025-03-10 13:16:03 -07:00
Doug Gregor
296e14662a Rework runtime entrypoints for isolated conformance checking
Replace the pair of global actor type/conformance we are passing around with
a general "conformance execution context" that could grow new functionality
over time. Add three external symbols to the runtime:

* swift_conformsToProtocolWithExecutionContext: a conforms-to-protocol check
  that also captures the execution context that should be checked before
  using the conformance for anything. The only execution context right now
  is for an isolated conformance.
* swift_isInConformanceExecutionContext: checks whether the function is
  being executed in the given execution context, i.e., running on the
  executor for the given global actor.
* swift_ConformanceExecutionContextSize: the size of the conformance
  execution context. Client code outside of the Swift runtime can allocate
  a pointer-aligned region of memory of this size to use with the runtime
  functions above.
2025-03-07 23:52:20 -08:00
Doug Gregor
48aa75d86f [Isolated conformances] Cache resolved global actor for conformances
In the prior implementation of runtime resolution of isolated conformances,
the runtime had to look in both the protocol conformance descriptor and
in all conditional conformance requirements (recursively) to find any
isolated conformances. If it found one, it had to demangle the global
actor type to metadata. Since swift_conformsToProtocol is a hot path through
the runtime, we can't afford this non-constant-time work in the common
case.

Instead, cache the resolved global actor and witness table as part of the
conformance cache, so that we have access to this information every time
we look up a witness table for a conformance. Propagate this up through
various callers (e.g., generic requirement checking) to the point where
we either stash it in the cache or check it at runtime. This gets us down
to a very quick check (basically, NULL-or-not) for nonisolated conformances,
and just one check for isolated conformances.
2025-03-07 23:51:46 -08:00
Doug Gregor
6dd141ad54 Replace dlsym of swift_task_isCurrentGlobalActor with a constructor hook
Following the approach taken with the concurrency-specific type
descriptors, register a hook function for the "is current global actor"
check used for isolated conformances.
2025-03-07 23:51:43 -08:00
Doug Gregor
951b535608 Metadata and runtime support for checking isolated conformances at runtime
Extend the metadata representation of protocol conformance descriptors
to include information about the global actor to which the conformance is
isolated (when there is one), as well as the conformance of that type to
the GlobalActor protocol. Emit this metadata whenever a conformance is
isolated.

When performing a conforms-to-protocol check at runtime, check whether
the conformance that was found is isolated. If so, extract the serial
executor for the global actor and check whether we are running on that
executor. If not, the conformance fails.
2025-03-07 23:51:25 -08:00