Commit Graph

92 Commits

Author SHA1 Message Date
Doug Gregor d0957a8933 [Embedded] Centralize allocation size/alignment computation for boxes
swift_allocBox was correctly computing allocation size/alignment.
swift_deallocBox was compiting it differently (and incorrectly).
Factor out the logic into a separate function that we use from both
places.
2026-05-12 10:45:13 -07:00
Doug Gregor 9782c5d82f [Embedded] Implement swift_getObjectType and swift_compareWitnessTables
The concurrency runtime makes use of these functions, which have
trivial implementations. Implement them in the Embedded Swift runtime.
2026-05-05 16:40:10 -07:00
Doug Gregor 1de4cf6afd [Embedded] Split dynamic casting machinery into a separate file 2026-04-28 18:27:41 -07:00
Doug Gregor 4c3d37ec11 Deal with 32-bit platforms 2026-04-28 18:23:10 -07:00
Doug Gregor 29ea399833 [Embedded] Reimplement dynamic casting for Embedded Swift
The Embedded Swift dynamic casting logic was getting a bit tangled, and
used some heuristics for figuring out the existential representation
that didn't always work, e.g., with class-bound types.

Reimplement the entire thing using the same general approach that the
non-Embedded runtime uses, albeit simplified greatly because Embedded
Swift doesn't permit casting to existential types (only *from*
existential types). The new implementation correctly unwraps all levels
of existential, short-circuits where appropriate, and "takes" wherever
possible.

This change does require emitting some more metadata than we did
before, because it is necessary to handle the different existential
representations correctly. Previously, the compiler would pass a NULL
pointer as the "source" metadata to `swift_dynamicCast`, then try to
figure out what it was given by inspecting the source pointer. This is
insufficient for distinguishing the various existential representations
(opaque, class-bound, error) and relied on heuristics. Start passing
destination metadata to this function, just as we do in non-Embedded
Swift. This means we'll end up generating some more metadata for
existential types (like `any P`) that we wouldn't have before, but
it's still pay-per-use and they are relatively small. All of the other
metadata that can flow here will already have been created for other
reasons, e.g., because classes already have it in their vtable.

This also extends the metadata associated with existential types by one
more word (from 2 to 3). The new word is used to provide the
existential representation, which is needed for dynamic casting as
described above.
2026-04-28 15:07:56 -07:00
Doug Gregor e0fdde7403 [Embedded Swift] Deal with multi-wrapped "any Error" existentials when casting
Teach swift_dynamicCast to repeatedly unwrap "any Error" existentials
before matching the concrete type.

Required to make rdar://175590869 work through AsyncStream.
2026-04-27 14:02:05 -07:00
Max Desiatov b589f05c10 Support untyped throws in Embedded Swift (#87617)
**Explanation**: We would like untyped throws to be available in Embedded Swift when system allocator is available.
**Scope**: limited to Embedded Swift.
**Risk**: low due to isolated scope, additive nature of the change, and no adoption of untyped throws in Embedded Swift so far.
**Testing**: added new lit tests.
**Issue**: rdar://171325402
2026-04-14 18:05:14 +01:00
Doug Gregor 91257bb488 [Embedded] Only include detailed exclusivity failure message in debug builds
In release builds that enable dynamic exclusivity checking, we are
including all of the printing code for producing the nice
"simultaneous access" failure message. That increases code size too
much, so only include this in debug builds. For release builds, we
just trap here.

Fixes rdar://173338152.
2026-03-25 10:53:55 -07:00
Doug Gregor fbf00872a5 [Embedded runtime] Add a missing "unsafe" with the Embedded Swift Platform 2026-03-23 11:26:38 -07:00
Allan Shortlidge 00a6ebf101 Embedded: Fix typos in EmbeddedRuntime.swift.
Fixes build failure introduced by https://github.com/swiftlang/swift/pull/86831.
2026-03-21 05:40:37 -07: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
Doug Gregor a7cdd02c6e [Embedded platform] Replace _swift_free with _swiftAlignedFree
Pass size and alignment through to the free function, to account for
allocators that need to know this information.
2026-03-03 09:05:30 -08:00
Doug Gregor 84b653fb73 Provide a C header that describes Embedded Swift's upcoming platform requirements
Embedded Swift depends on a number of functions from the platform,
including posix_memalign, free, and arc4random_buf. While these
dependencies are documented, they aren't documented in a manner that
makes them easy to implement. Additionally, most of these are C
library functions that may or may not be available, and can have
requirements that are more stringent than Embedded Swift actually
requires.

Try to address both problems at once by introducing a new header,
`swift/EmbeddedPlatform.h`, that provides a new set of C declarations
for functions that need to be provided by a platform. For
each function, we provide documentation on what it does, what Embedded
Swift facilities will end up using it (e.g.,
`_swift_generateRandomHashSeed` is needed for seeding the hash
functions used for `Set` and `Dictionary`, among other things), and
some guidance on how to implement it---including those cases where
directly calling an existing C standard library or POSIX function will
suffice.

There is a new CMake option, SWIFT_USE_SWIFT_EMBEDDED_PLATFORM, that
enables the use of these entrypoints in the Embedded Swift runtime. It
is currently disabled, because enabling it would break existing
clients of Embedded Swift. We're looking for a way to stage this in.

Addresses rdar://164057124.
2026-03-03 09:05:29 -08:00
Doug Gregor 4d4974cf35 [Embedded runtime] Route all deallocation through swift_slowDealloc
Centralize calls to "free" in the Embedded Swift runtime by calling
through swift_slowDealloc everywhere.
2026-02-26 14:29:07 -08:00
Doug Gregor d003f1dcca Use condfail_message in _debugPrecondition and make it more transparent
Improve debugging from core dumps for Embedded Swift by using condfail
rather than trap within _debugPrecondition, and make the call stack
transparent enough that we promote these out to callers.

Fixes rdar://159471659.
2026-02-06 11:33:38 -08:00
Arnold Schwaighofer e6ea70720c Merge pull request #86691 from aschwaighofer/embedded_existential_fix_outline_path
[embedded] [existentials] Fix the on-heap storage paths - what was I thinking
2026-01-23 07:24:41 -08:00
Arnold Schwaighofer 25dd981911 [embedded] [existentials] Fix the on-heap storage paths
Call swift_releaseBox on all the copy-on-write paths of existentials.
2026-01-21 09:38:27 -08:00
Doug Gregor 3e5eb9a369 Enable dynamic exclusivity checking runtime in Embedded Swift
Start building the dynamic exclusivity checking code in Embedded Swift.
It will be used when dynamic exclusivity checking is enabled (via
`-enforce-exclusivity=checked`), and dead-code-stripped otherwise. Note
that we deliberately avoid linking `swift_(begin|end)Access` in
modules that don't have exclusivity enforcement enabled to aid with
dead-code stripping.

Also note that, unlike in non-Embedded Swift, exclusivity checking
cannot be disabled at runtime by setting
_swift_disableExclusivityChecking to `false`. We need the ability to
constant-initialize global variables to bring that functionality into
Embedded Swift.

Tracked by rdar://159115910.
2026-01-20 20:07:38 -08:00
Doug Gregor d73cd34805 Always suppress swift_(begin|end)Access calls with -enforce-exclusivity=unchecked
The exclusivity enforcement command-line flags currently impact the
generation of SIL within the current module. However, it does not
impact any SIL that was deserialized from another module, which means
that `-enforce-exclusivity=unchecked` doesn't actually remove all of
the dynamic exclusivity checks.

When dynamic exclusivity is disabled, lower SIL
begin_access/end_access instructions to nothing to ensure that we
won't do any dynamic exclusivity checks.

Use this to better model the reality of dynamic exclusivity checking
in Embedded Swift, which effectively turned off all dynamic
exclusivity checking by having empty stub implementations of
swift_(begin|end)Access. Instead, have Embedded Swift default to
`-enforce-exclusivity=unchecked`, so that it never emits calls to
swift_(begin|end)Access. Remove the stub implementations of
swift_(begin|end)Access from the Embedded Swift runtime, since we will
no longer generate calls to them by default.

Moving forward, this will allow us to conditionally enable the new
implementation of dynamic exclusivity checking by specifying
`-enforce-exclusivity=checked` for Embedded Swift builds. We'll stage
that in over time to avoid breaking existing Embedded Swift clients.
2026-01-09 09:26:39 -08:00
Arnold Schwaighofer 0491c59946 [embedded] Fix the type of the type descriminator parameter of swift_coroFrameAlloc on 32 bit platforms
The type of malloc_type_id_t is a 64bit integer (unsigned long long).

https://github.com/swiftlang/swift/issues/86069
2026-01-05 09:21:26 -08:00
Guillaume Lessard 5d40870bf9 Merge pull request #82215 from valeriyvan/typos-EmbeddedRuntime
Fix typos in EmbeddedRuntime.swift
2025-12-20 20:43:51 -08:00
Rauhul Varma ce02872adf Add Embedded Swift Cxx exception personality
Users frequently run into a missing runtime symbol for Cxx exceptions
(`_swift_exceptionPersonality`) when mixing Embedded Swift and Cxx with
exceptions enabled. This leads to a confusing an hard to debug linker
error. This commit adds an implementation of this function to the
Embedded Swift runtime which simply fatal errors if a cxx exception is
caught in a Swift frame.

Issue: rdar://164423867
Issue: #85490
2025-11-21 11:12:10 -08:00
Arnold Schwaighofer 3cff05d540 [embedded] Implement swift_dynamicCast suport for casts from existential to concrete type 2025-11-19 14:41:37 -08:00
Arnold Schwaighofer 02d7fe5110 Document the difference of embedded swift's swift_allocBox 2025-11-17 12:48:48 -08:00
Arnold Schwaighofer 62ac48a17e Complete support for outline existential storage
... or so I believe
2025-11-17 12:48:45 -08:00
Arnold Schwaighofer eda5eadfd4 Start outline storage support: Add swift_allocBox/deallocBox
Code using the outline heap storage path will crash and burn because
support is incomplete. But at least inline storage existential
inhabitants should compile and run.
2025-11-17 12:46:35 -08:00
Doug Gregor 9ea8671c3f Use @c instead of @_cdecl in the Embedded Swift runtime
This change moves us toward the official feature, and eliminates the
extra level of "thunk" that was implied by `@_cdecl`. Amusingly, this
trips up the LLVM-level ARC optimizations, because we are trying to
perform ARC optimizations within the retain/release runtime functions.
Teach those optimization passes to leave swift_retainN et al alone.
2025-11-13 18:39:10 -08:00
Valeriy Van c53d71bb2a Fix typos 2025-10-04 12:53:44 +02:00
Tim Kientzle ff50f8f2be Fill in two missing functions for Embedded Swift
`swift_coroFrameAlloc` is needed by `_read`/`_modify` accessors

`swift_deallocUninitializedObject` which I believe is needed for failable class initializers

Resolves rdar://157028375
Resolves rdar://157276375
2025-08-29 16:57:08 -07:00
Gabor Horvath 402ad33463 [StrictMemorySafety] Check the safety of return types of calls
Previously, we skipped checking the return type of a function for safety
as we expected to warn at the use of the returned value:

  let x = returnsUnsafe()
  usesUnsafe(x) // warn here

Unfortunately, this resulted in missing some unsafe constructs that can
introduce memory safety issues when the use of the return value had a
different shape resulting in false negatives for cases like:

  return returnsUnsafe()

or

  usesUnsafe(returnsUnsafe())

This PR changes the analysis to always take return types of function
calls into account.

rdar://157237301
2025-08-05 12:16:44 +01:00
Valeriy Van fca136338a Fix typos in EmbeddedRuntime.swift 2025-06-12 20:31:21 +03:00
Allan Shortlidge f8fb99320a Embedded: Suppress spurious unused result warnings.
The results of `swift_retain(object:)` and `swift_bridgeObjectRetain(object:)`
should be discardable since it's typcial to not need the pointer returned from
these calls. Suppresses unused result warnings that were generated previously.
2025-05-18 10:40:13 -07:00
Doug Gregor 050a514588 [Strict memory safety] Update standard library for unsafe treated as a call effect 2025-04-25 21:54:23 -07:00
Kuba Mracek c0ea02f86d [embedded] Add swift_allocEmptyBox to the embedded runtime 2025-04-21 15:47:34 -07:00
Doug Gregor f668feaf64 [Standard library] Additional strict-memory-safety annotations 2025-03-10 15:42:05 -07:00
Kuba Mracek 01d7e231c3 [embedded] Allow string-interpolating fatalError in Embedded Swift 2025-02-28 08:53:09 -08:00
Doug Gregor 22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
Kuba Mracek f4c69e1ca0 [embedded] Also allow nil in swift_isEscapingClosureAtFileLocation to match the C++ runtime 2025-01-27 10:20:23 -08:00
Kuba Mracek 8e3ee38324 [embedded] Support withoutActuallyEscaping in Embedded Swift 2025-01-25 15:24:20 -08:00
Kuba Mracek db94dc7d36 [Concurrency] Fix memory leak around mismatched refcounting in Concurrency 2024-10-31 21:43:02 -07:00
Kuba Mracek 0048c1c624 [embedded] Document the details of the Embedded Swift's refcounting scheme 2024-10-21 08:56:22 -07:00
Kuba Mracek 8666aef39f [embedded] Fix a memory leak caused by incorrect refcounting logic around doNotFreeBit 2024-10-18 23:19:40 -07:00
Konrad `ktoso` Malawski 45b97f146b Merge branch 'main' into wip-experimental-isolated-deinit 2024-09-30 13:47:39 +09:00
Kuba (Brecka) Mracek d3c5149714 Merge pull request #76658 from kubamracek/embedded-managed-buffer
[embedded] Add support for ManagedBuffer to Embedded Swift
2024-09-25 02:54:55 -07:00
Kuba Mracek c7a5569c4f [embedded] Add support for ManagedBuffer to Embedded Swift
This makes ManagedBuffer available and usable in Embedded Swift, by:

- Removing an internal consistency check from ManagedBuffer that relies on metatypes.
- Making the .create() API transparent (to hoist the metatype to the callee).
- Adding a AllocRefDynamicInst simplification to convert `alloc_ref_dynamic` to `alloc_ref`, which removes a metatype use.
- Adding tests for the above.
2024-09-23 13:32:50 -07:00
Konrad `ktoso` Malawski 7d1ce789ad Revert "Revert "Isolated synchronous deinit"" 2024-09-17 17:35:38 +09:00
Kuba (Brecka) Mracek 142df19324 Merge pull request #76231 from kubamracek/embedded-deinit-release
[embedded] Handle retain/retain ops inside deinit in Embedded Swift's swift_release
2024-09-16 16:13:08 -07:00
Kuba Mracek b4431db2c5 [embedded] Add a more detailed explaining comment to swift_release_n_ 2024-09-16 10:37:40 -07:00
Alex Hoppen c5aa49ba64 Revert "Isolated synchronous deinit" 2024-09-03 18:11:26 -07:00
Kuba Mracek 249f7e6073 [embedded] Handle retain/retain ops inside deinit in Embedded Swift's swift_release 2024-09-03 14:38:03 -07:00