Commit Graph

1604 Commits

Author SHA1 Message Date
Mike Ash f7745f2094 [Runtime] Group environment variable types into a types namespace.
Rather than attempt to use standard C++ type names, create a namespace specifically for environment variable types and require vars to use a type from that namespace. This avoids the annoying collision between this `string` and `std::string`, and makes everything a bit more consistent. Rename the types a bit to be more appropriate for this context: boolean, uint8, and uint32, instead of bool, uint8_t, and uint32_t.
2026-05-14 15:16:17 -04:00
Mike Ash 31c4b0e216 Merge pull request #87715 from mikeash/swift-job-run-exception-block
[Concurrency] Block exception propagation through swift_job_run.
2026-05-11 13:21:01 -04:00
Mike Ash 058b1ed822 [Concurrency] Block exception propagation through swift_job_run.
When an exception is thrown through swift_job_run, it leaves the Concurrency runtime in an inconsistent state, which can lead to misbehavior or crashes later on. It's very difficult to work out what the cause is when this happens. Since the program state is doomed once this happens, prevent exceptions from propagating through swift_job_run at all, and terminate immediately when throwing.

rdar://171909991
2026-05-08 10:16:29 -04:00
Alastair Houghton ed225ce9c9 Merge pull request #88685 from al45tair/eng/PR-163401438
[Concurrency][Runtime] Fix `isCurrentGlobalActorHook`.
2026-05-06 09:12:46 +01:00
Alastair Houghton 5408ae3149 [Runtime] Fix function signatures to work for Wasm.
Wasm requires that the `SWIFT_CONTEXT` be the final argument,
because it uses stack-based parameter passing.

rdar://163401438
2026-05-05 16:56:25 +01:00
Alastair Houghton 0f63d6e3d2 [Concurrency][Runtime] Fix isCurrentGlobalActorHook.
We were crashing when using this hook function because the hook points
at a Swift function, which expects `self` to be set, but we weren't
telling the C compiler and so when we called through the function pointer
from C++ code, the relevant register wasn't set and we'd crash.

Fixes #85134

rdar://163401438
2026-04-27 17:29:34 +01:00
Konrad `ktoso` Malawski e4b0a2c8e8 [Concurrency] Allow getting unsafe task from Job and name as well (#84370) 2026-04-17 21:34:50 +09:00
Allan Shortlidge 0218eb610e Concurrency: Fix availability of task priority escalation runtime functions.
They are only available starting in the Swift 6.2 runtime.

Resolves rdar://174586474.
2026-04-12 10:06:11 -07:00
Mike Ash db4692534e [Concurrency] Add more info to tracing calls.
* Have job_run include the actor, executor, and task name.
* Make task_status_changed only trace if one of the tracked values actually changed.
* Add a job_enqueue_executor that covers enqueueing jobs on serial executors (default actors, custom executors, main actor, etc.).
* Actually end the actor lifetime signpost interval.
* Include the actor metadata and context descriptor pointers in actor enqueue/dequeue so the type can be identified.
* Add the task pointer to all task-related signpost messages.
* Emit job_enqueue_main_executor from the swift_task_enqueueImpl path so main actor enqueues appear in traces.
* Add TracingExecutorKind enum and log executorKind in job_enqueue_executor and job_run to distinguish global, default actor, main actor, custom serial executor, and task executor contexts.

While we're here, finally add a test for Concurrency signposts. This is difficult because the signposts are disabled by default and there isn't a good way to turn them on in an automated fashion. Resolve this by adding an environment variable, SWIFT_CONCURRENCY_TRACING_SUBSYSTEM, which overrides the subsystem used by the Concurrency signpost code. The test can use this to set a subsystem that isn't disabled by default, which then allows `log stream` to capture the signposts.
2026-03-26 15:10:48 -04:00
Dario Rexin b595e463d4 Merge pull request #87663 from drexin/wip-typed-allocation-embedded
[IRGen] Add typed allocations for embedded Swift
2026-03-16 08:51:57 -07:00
John McCall 8b07a6f311 Mark swift_task_alloc and swift_task_dealloc as accessing inaccessible
memory so that they cannot be arbitrary moved around in the function.
2026-03-06 03:14:46 -05:00
Dario Rexin 5b0da23150 [IRGen] Add typed allocations for embedded Swift
rdar://158239258

This change adds logic in the compiler to compute malloc type ids and emit them together with typed allocation calls. It also adds the new runtime function swift_allocObjectTyped, which calls typed malloc.
2026-03-05 12:41:16 -08:00
Konrad `ktoso` Malawski 34b17dd3e4 [Distributed] Distributed remote ref should be as small as possible (#87449)
Because we guaranteed that one cannot access a distribted "remote"
instance's state via type system, we can allocate the instance much
smaller because we never access those user initialized fields.

This way a remote distributed actor reference is always 128 bytes, this
is because actors have special storage 12*8 bytes of PrivateData in
addition to the 16 bytes object header.

With this optimization, regardless how many fields an actor has, the
remote ref always is the same size.

resolves rdar://81825648

Used claude to generate a bunch of tests to check the sizes of actors
etc, I think this is correct (and getting it wrong totally just crashes
immediately anyway). Could use a review though, thank you!

cc @mikeash @xedin
2026-03-02 10:08:20 +09:00
Alastair Houghton cb57f30912 [CompatibilityPacks] Don't export symbols.
Mark the symbols in libswiftCompatibilityPacks.a as hidden.

rdar://169305717
2026-02-18 10:41:30 +00:00
Konrad `ktoso` Malawski 68c8641321 Task Cancellation Shields (#85637)
This is a follow up from the "async" `deinit` work, which will allow us
to guarantee cleanup code to run in deinitializers, even if they need to
call asynchronous code, and even if they may be run in a task that was
cancelled: by "shielding" it from cancellation.

This is incomplete, the child handling needs some more love.

SE proposal: https://github.com/swiftlang/swift-evolution/pull/3037/
2026-02-16 09:35:27 +09:00
Alastair Houghton ceaba82d6d [Backtracing] Updates after review.
Tweaked the comment in `Runtime/Config.h`.

Fixed a couple of incorrect ARM64 instruction mnemonics.  This still needs
testing on ARM64 Windows.

Fixed an out-of-date comment in `swift-backtrace`.

Use a macro in `Backtrace.cpp` to guarantee we don't overrun the buffer,
and in the process simplify the code slightly.

rdar://101623384
2026-02-06 08:53:16 +00:00
Alastair Houghton 3463b1e145 [Backtracing] Disable backtracing for 32-bit Windows.
Right now, the backtracer won't work on 32-bit Windows because we
aren't properly dealing with the `stdcall` calling convention on
the Win32 API calls.

rdar://101623384
2026-02-03 18:23:29 +00:00
Alastair Houghton d13988bd5f [Backtracing][Windows] Initial Windows support.
This doesn't have a working symbolicator yet, but it does build and
it can obtain a basic backtrace.

It also doesn't include a working `swift-backtrace` program yet.

rdar://101623384
2026-02-03 18:23:29 +00:00
Alejandro Alonso 02d0bdfbcd Implement extra inhabitant counts for borrows 2026-01-23 08:02:10 -08:00
Alejandro Alonso 632d6f4257 WIP: Dependent borrow layout 2026-01-23 08:02:10 -08:00
Konrad `ktoso` Malawski 40dabefdb4 Reapply: Runtime: expose demangle() in Runtime module (#84788) (#86510) 2026-01-14 21:44:28 +09:00
eeckstein 4766678f3f Revert "Runtime: expose demangle() in Runtime module (#84788)"
This reverts commit ab69fc0d2c.
2026-01-12 10:26:14 +01:00
Konrad `ktoso` Malawski ab69fc0d2c Runtime: expose demangle() in Runtime module (#84788)
We should expose the demangle functionality; It's been widely used by
calling into internal _swift_demangle and instead we should offer a real
API. It's also already used in the Runtime module already when forming
backtraces.

[Previous
discussions](https://forums.swift.org/t/demangle-function/25416/15)
happened between 2019 and 2024, and just never materialized in a
complete implementation and proposal.

Right now, even more tools are in need of this API, as we are building
continious profiling solutions etc, so it is a good time to revisit this
topic.

This PR is roughly based off @Azoy's

https://github.com/swiftlang/swift/pull/25314/files#diff-fd379a721cc9a1c9ef6486eae713e945da842b42170d4d069029a57334371eba
from 2019, however it brings it over to the new Runtime module which is
a great place to put this functionality - even Backtrace had to recently
reinvent calling the demangling infra in this module.

Pending SE review, [proposal
here](https://github.com/swiftlang/swift-evolution/pull/2989).

cc @azoy @al45tair

---------

Co-authored-by: Alastair Houghton <alastair@alastairs-place.net>
2026-01-09 09:46:19 -08:00
Bryce Wilson 84121f59d2 [Concurrency] Set correct availability for functions in DispatchShims.h 2025-11-23 16:00:40 -08:00
Konrad `ktoso` Malawski 80913591cd Adjust availability checks in DispatchShims.h (#85634) 2025-11-20 21:23:21 -08:00
Bryce Wilson 5f83c54f8c [Concurrency] Set thread base priority when running escalated Tasks 2025-11-20 11:15:22 +09:00
Bryce Wilson b30f63530a [Concurrency] Set thread base priority when running escalated Tasks (#84895) 2025-11-20 09:58:08 +09:00
Arnold Schwaighofer 17447a3378 Merge pull request #85551 from aschwaighofer/wip_embedded_exit
Preliminary support for existential in embedded Swift
2025-11-18 15:10:40 -05:00
Arnold Schwaighofer 62ac48a17e Complete support for outline existential storage
... or so I believe
2025-11-17 12:48:45 -08:00
Michael Gottesman 01c2e0fde9 [irgen] Make AsyncLetBegin and AsyncLetFinish UNKNOWN_MEMEFFECTS instead of ArgMemOnly.
In a discussion with @rjmccall, we agreed that these should really be
UNKNOWN_MEMEFFECTS so we are conservative.

Just slicing this off from a larger patch stream.
2025-11-14 15:08:41 -08:00
Mike Ash cc8e6fd877 Merge pull request #85260 from mikeash/client-rr-library-rename
[Runtime] Rename ClientRetainRelease library to SwiftDirectRuntime.
2025-11-08 12:40:27 -05:00
Mike Ash f149a8bce4 Merge pull request #85259 from mikeash/task-allocator-disable-option
[Concurrency] Add environment variable for disabling async stack slab allocator.
2025-11-08 07:53:56 -05:00
Mike Ash a62f08e050 [Concurrency] Add environment variable for disabling async stack slab allocator.
Add SWIFT_DEBUG_ENABLE_TASK_SLAB_ALLOCATOR, which is on by default. When turned off, async stack allocations call through to malloc/free. This allows memory debugging tools to be used on async stack allocations.
2025-11-07 22:48:41 -05:00
Mike Ash 1898b01ce6 [Runtime] Rename ClientRetainRelease library to SwiftDirectRuntime.
This library will likely become home to other fast-path-in-client functions, so give it a more general name.
2025-11-07 16:36:29 -05:00
Michael Gottesman 97b0e2ebae [concurrency] Create builtins for invoking specific concurrency runtime functions.
Specifically:

1. swift_task_addCancellationHandler
2. swift_task_removeCancellationHandler
3. swift_task_addPriorityEscalationHandler
4. swift_task_removePriorityEscalationHandler
5. swift_task_localValuePush
6. swift_task_localValuePop

rdar://109850951
2025-11-05 11:03:44 -08: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
Kavon Farvardin e55d18a66b Merge pull request #85313 from mikeash/rr-preservemost-no-embedded
[Runtime] Don't use custom retain/release calling convention in embedded Swift.
2025-11-04 11:06:48 -08:00
Mike Ash 7036784480 [Runtime] Don't use custom retain/release calling convention in embedded Swift.
In embedded mode, some things call retain/release using the C++ declarations, but the implementations are in Swift. The Swift implementations don't use preservemost, so the C++ declarations must not declare preservemost in that context.

rdar://163940783
2025-11-04 13:30:11 -05:00
John McCall a7d7970e29 Turn finishAsyncLet into a builtin.
This is necessary because we need to model its stack-allocation
behavior, although I'm not yet doing that in this patch because
StackNesting first needs to be taught to not try to move the
deallocation.

I'm not convinced that `async let` *should* be doing a stack allocation,
but it undoubtedly *is* doing a stack allocation, and until we have an
alternative to that, we will need to model it properly.
2025-11-03 16:33:40 -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 7bf535c546 [Gardening] IRGen: Delete this xmacro entry.
The swift_coroFrameAllocStub function isn't in the runtime.
2025-10-30 03:59:17 -07:00
Mike Ash 93fae78e04 [IRGen][Runtime] Add emit-into-client retain/release calls for Darwin ARM64.
This is currently disabled by default. Building the client library can be enabled with the CMake option SWIFT_BUILD_CLIENT_RETAIN_RELEASE, and using the library can be enabled with the flags -Xfrontend -enable-client-retain-release.

To improve retain/release performance, we build a static library containing optimized implementations of the fast paths of swift_retain, swift_release, and the corresponding bridgeObject functions. This avoids going through a stub to make a cross-library call.

IRGen gains awareness of these new functions and emits calls to them when the functionality is enabled and the target supports them. Two options are added to force use of them on or off: -enable-client-retain-release and -disable-client-retain-release. When enabled, the compiler auto-links the static library containing the implementations.

The new calls also use LLVM's preserve_most calling convention. Since retain/release doesn't need a large number of scratch registers, this is mostly harmless for the implementation, while allowing callers to improve code size and performance by spilling fewer registers around refcounting calls. (Experiments with an even more aggressive calling convention preserving x2 and up showed an insignificant savings in code size, so preserve_most seems to be a good middle ground.)

Since the implementations are embedded into client binaries, any change in the runtime's refcounting implementation needs to stay compatible with this new fast path implementation. This is ensured by having the implementation use a runtime-provided mask to check whether it can proceed into its fast path. The mask is provided as the address of the absolute symbol _swift_retainRelease_slowpath_mask_v1. If that mask ANDed with the object's current refcount field is non-zero, then we take the slow path. A future runtime that changes the refcounting implementation can adjust this mask to match, or set the mask to all 1s to disable the old embedded fast path entirely (as long as the new representation never uses 0 as a valid refcount field value).

As part of this work, the overall approach for bridgeObjectRetain is changed slightly. Previously, it would mask off the spare bits from the native pointer and then call through to swift_retain. This either lost the spare bits in the return value (when tail calling swift_retain) which is problematic since it's supposed to return its parameter, or it required pushing a stack frame which is inefficient. Now, swift_retain takes on the responsibility of masking off spare bits from the parameter and preserving them in the return value. This is a trivial addition to the fast path (just a quick mask and an extra register for saving the original value) and makes bridgeObjectRetain quite a bit more efficient when implemented correctly to return the exact value it was passed.

The runtime's implementations of swift_retain/release are now also marked as preserve_most so that they can be tail called from the client library. preserve_most is compatible with callers expecting the standard calling convention so this doesn't break any existing clients. Some ugly tricks were needed to prevent the compiler from creating unnecessary stack frames with the new calling convention. Avert your eyes.

To allow back deployment, the runtime now has aliases for these functions called swift_retain_preservemost and swift_release_preservemost. The client library brings weak definitions of these functions that save the extra registers and call through to swift_retain/release. This allows them to work correctly on older runtimes, with a small performance penalty, while still running at full speed on runtimes that have the new preservemost symbols.

Although this is only supported on Darwin at the moment, it shouldn't be too much work to adapt it to other ARM64 targets. We need to ensure the assembly plays nice with the other platforms' assemblers, and make sure the implementation is correct for the non-ObjC-interop case.

rdar://122595871
2025-10-27 12:00:28 -04:00
Mads Odgaard c92e5b47f3 Merge pull request #84574 from madsodgaard/android-availability 2025-10-20 10:40:37 +09:00
Tim Kientzle f0679b363e Re-apply PR #82750: Reimplement floating-point description implementation in Swift.
This reverts PR #84576, which was a revert of PR #82750
It reverts commit 4ac18aa32e, reversing
changes made to b46eddbabd.
2025-10-07 07:11:57 -07:00
Tim Kientzle 7d85d75f78 Revert "Merge pull request #82750 from tbkka/tbkka-swift-floatingpointtostring"
This reverts commit 54627fb49b, reversing
changes made to dda4608a84.
2025-09-29 15:13:19 -07:00
Tim Kientzle 54627fb49b Merge pull request #82750 from tbkka/tbkka-swift-floatingpointtostring
Reimplement floating-point description implementation in Swift.
2025-09-29 07:55:25 -07:00
Mike Ash 3f610e94a3 Merge pull request #84446 from mikeash/swift-inspect-scan-search
[swift-inspect] Add a mode to search for and dump generic metadata without metadata iteration enabled.
2025-09-26 19:46:00 -04:00
Mike Ash 513f5fb17a [swift-inspect] Add a mode to search for and dump generic metadata without metadata iteration enabled.
We scan the target's initial allocation pool, and all 16kB heap allocations. We check each pointer-aligned offset within those areas, and try to read it as Swift metadata and get a name from it. If that fails, quietly move on. It's very unlikely for some random memory to look enough like Swift metadata for this to produce a name, so this works very well to print the generic metadata instantiated in the remote process without requiring `SWIFT_DEBUG_ENABLE_METADATA_ALLOCATION_ITERATION`.

rdar://161120936
2025-09-26 10:52:35 -04:00
Mike Ash 05dcc6ba86 Merge pull request #83989 from DmT021/wp/conformance-cache-by-descriptor-2
Protocol conformance cache for generic types
2025-09-24 11:12:11 -04:00
Tim Kientzle 875297bc24 Remove old C implementation 2025-09-22 12:40:10 -07:00