Commit Graph

129 Commits

Author SHA1 Message Date
Mike Ash
3a0b3924df Merge pull request #85044 from mikeash/emit-into-client-retain-release
[IRGen][Runtime] Add emit-into-client retain/release calls for Darwin ARM64.
2025-10-28 12:09:01 -04: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
Jonathan Grynspan
8feba8d584 Use __ehdr_start on ELF instead of calling dladdr(). (#85012)
This PR replaces our use of `__dso_handle` and (indirectly) `dladdr()`
when figuring out the base address of loaded Swift images on ELF-based
platforms. Instead, we use `__ehdr_start` which refers to the start of
the image's ELF header (i.e. the start of the file) and is provided by
the compiler/linker for this purpose.

This pointer is consumed by `swift_addNewDSOImage()` and, later, by
Swift Testing when running tests, but is otherwise unused, so the less
work we do on it at runtime, the better.

A fallback path is present that, for images that do not contain
`__ehdr_start`, derives the address by calling `dladdr()` on the section
structure's address.

Resolves #84997.
2025-10-24 12:01:09 -07:00
3405691582
9683569f25 OpenBSD 7.8 will require OpaquePointer for FILE. (#84478)
OpenBSD 7.8 snapshots hide the type information for FILE. Therefore, the
types for the standard stdio streams should (regrettably) be
OpaquePointer, due to the well-discussed issue of losing type
information for forward-declared C types.

We explicitly drop to void * in the LibcOverlayShims for backwards
compatibility, since OpenBSD 7.8 is not yet released.
2025-09-27 18:16:58 +05:30
Artem Chikin
07760825ed Revert "utils: Support multiple CMake versions in build.ps1" 2025-09-24 16:30:19 -07:00
Saleem Abdulrasool
841db27f9c Merge pull request #84306 from Steelskin/fabrice/cmake-version-support
utils: Support multiple CMake versions in build.ps1
2025-09-23 10:20:21 -07:00
Fabrice de Gans
36eee08f06 utils: Support multiple CMake versions in build.ps1
CMake 3.30 passes linker flags to the Swift invocation when it is used
as the linker driver. However, Swift cannot parse the linker arguments,
so they need to be passed with the `-Xlinker` prefix. This cannot be
done uncondtionally as the same arguments are also used with the MSVC
toolchain.
CMake 4.0 introduces the CMP0181 policy, which allows CMake to parse the
`LINKER:` syntax on the linker flags passed on the command line, which
simplifies handling of flags.

Note that building for Android still requires CMake 3.29 for the time
being as we pass a linker driver argument to the CMake linker flags to
override the linker used.
2025-09-15 16:52:24 -04:00
Yuta Saito
6915a36895 [NFC][wasm] Fix incorrect comment about reserved low addresses 2025-09-15 06:21:35 +00:00
Allan Shortlidge
c82b9ed5ae stdlib: Fix warning about superfluous 'const' on return type.
Resolves the following warnings:

```
stdlib/public/SwiftShims/swift/shims/CoreFoundationShims.h:86:1: warning: 'const' type qualifier on return type has no effect [-Wignored-qualifiers]
   86 | const _swift_shims_NSUInteger
      | ^~~~~
stdlib/public/stubs/FoundationHelpers.mm:110:1: warning: 'const' type qualifier on return type has no effect [-Wignored-qualifiers]
  110 | const _swift_shims_NSUInteger
      | ^~~~~
```
2025-07-30 12:45:46 -07:00
David Smith
1a56da655f Native implementation of -lengthOfBytesUsingEncoding, plus handling ASCII-subset MacRoman in a few places (#81791)
Fixes rdar://154341146
2025-06-27 13:48:10 -07:00
(null)
d6811260a9 Merge commit '665515c781999a81094bbe4f8302a7cb1a6a6b39' into mchiu/freebsd 2025-06-17 15:12:23 -04:00
Fabrice de Gans
5d50279c2a build: Ensure static headers are always symlinked
When using the Swift static build on Windows, projects like Foundation
use the clang headers from the toolchain build `lib` directory. No
target in the toolchain build depends on the static clang headers, so
they do not get installed. As a workaround, this makes the static clang
headers symlink target a dependency of the regular dynamic version of
the headers, ensuring they are installed as part of the toolchainb
build.
2025-06-09 15:27:13 -07:00
Michael Chiu
7bbafc599d Merge branch 'main' into mchiu/freebsd 2025-06-02 13:33:18 -04:00
Kuba Mracek
544251bdc2 [embedded] Resolve a circular dependency problem in SwiftShims when using pico-libc 2025-05-29 17:37:32 -07:00
michael-yuji
9a948a9c3b Merge branch 'main' into mchiu/freebsd 2025-05-21 16:29:16 -07:00
David Smith
f800fdf3d2 Adopt indirect tagged strings (#80425)
Fixes rdar://142991821
2025-04-01 12:53:29 -07:00
Michael Chiu
7f0f2ac4dd Merge swiftlang#38335 2025-03-14 02:15:15 -04:00
Michael Chiu
0c47c45303 Adding FreeBSD support
This commit adds required conditional compilation blocks to enable bulding on
FreeBSD (tested on x86_64 FreeBSD 14.1-RELEASE-p6). Also implements FreeBSD
synchronization shims using `_umtx_op(2)`
2025-03-14 02:15:15 -04:00
Kuba Mracek
54a0be3509 [SwiftShims] Include Target.h in the modulemap for SwiftShims 2025-02-17 17:13:14 -08:00
Yuta Saito
35c3d7fd0e [wasm] Use __visibility__("default") for all wasm platforms
The condition previously depended on the specific OS, but the image
boundary is the same for all platforms using Wasm as image format. This
fixes the Emscripten build.
2025-02-13 02:07:36 +00:00
Alejandro Alonso
6fdb684fd2 Optimization for single component and fix tests 2025-02-04 15:16:39 -08:00
Alejandro Alonso
11fbd94668 Merge pull request #78664 from Azoy/unicode-16
[stdlib] Unicode 16
2025-01-21 09:29:35 -08:00
Alejandro Alonso
4e087653f2 Update Unicode data files in stdlib 2025-01-15 14:07:46 -08:00
Saleem Abdulrasool
358be09832 stdlib,Runtimes: begin preparing for a static stdlib on Windows
When building the standard library statically on Windows, we should not
use any __declspec(dllimport) or __declspec(dllexport) on the runtime
functions or data. Furthermore, we should internalise all of the
standard library functions so that they are not re-exported if used to
build a shared library. The code generation changes required for this
are easier to identify once a static SDK is available for Windows.
2025-01-08 15:08:14 -08:00
David Smith
26b2f7bb47 Re-land using associated objects to attach bridged Array buffers (#78452) 2025-01-07 19:38:21 -08:00
Alexis Laferrière
0c27a4c38c SwiftShims: memcmp should accept optional pointers on Darwin
Darwin defines memcmp with optional pointers. Update SwiftShims to
define it to the same type to avoid deserialization failures where we
get one over the other and the types don't match anymore.

rdar://140596571
2024-12-04 14:58:32 -08:00
Ian Anderson
052cc2ec0d Merge pull request #77349 from ian-twilightcoder/clang-headers-need-modulemaps
[CMake] clang-builtin-headers needs to copy the module maps as well as the headers
2024-11-08 12:25:48 -08:00
Ian Anderson
90a2bde994 [CMake] clang-builtin-headers needs to copy the module maps as well as the headers
When copying the clang builtin headers, the module maps need to go with them.

rdar://139084511
2024-11-01 11:24:10 -07:00
Kuba Mracek
db94dc7d36 [Concurrency] Fix memory leak around mismatched refcounting in Concurrency 2024-10-31 21:43:02 -07:00
Alejandro Alonso
cfd39367a5 Merge pull request #75048 from Benedicto-H/fix/typos-task
[Shims] Fix typos in RefCount.h
2024-08-29 09:28:11 -07:00
Ian Anderson
07a639ca9c [Shims] Don't redeclare lgamma_r in macOS 15.0 and associated SDKs
macOS 15.0, iOS 18.0, et al has started unconditionally declaring `lgamma_r` in <math.h>, add a __has_include in SwiftShims to detect that and not redeclare it when building against those SDK versions.

rdar://115192929
2024-08-16 09:00:09 -07:00
Mishal Shah
48d5aca28d Revert "Use associated objects to attach contiguous array buffers to lazy one…"
This reverts commit 7c011813f2.
2024-07-21 23:34:57 -07:00
David Smith
7c011813f2 Use associated objects to attach contiguous array buffers to lazy ones (#75148)
rdar://132124808
2024-07-19 23:21:41 -07:00
Jeremy Schonfeld
0216e62179 auto --> __swift_uint32_t 2024-07-12 10:07:42 -07:00
Jeremy Schonfeld
a3c614d924 Fix build failure in _SynchronizationShims module 2024-07-12 09:54:46 -07:00
Benedicto Hong
9ad83e71e0 Fix typos in RefCount.h
- Fixed several typos.
  - this -> `This`
  - constant -> `Constant`
  - Add a period at the end of a sentence `.`
2024-07-08 16:26:13 +09:00
Tim Kientzle
05c74e0d90 Revert "Merge pull request #74394 from tbkka/tbkka-remotemirror-no-more-mpe-metadata-partial"
This reverts commit a1708ef8b1, reversing
changes made to b5930625be.
2024-06-14 15:32:39 -07:00
Tim Kientzle
80e860881f Fix an ABI break
PR #74394 removed a field from `struct MetadataSections` whose layout is ABI.
So restore that field as an unused padding field to preserve the layout of all
the other fields in that struct.
2024-06-14 13:19:10 -07:00
Konrad `ktoso` Malawski
2ec717b115 [Concurrency] TaskExecutor ownership fixes (#74000) 2024-06-14 22:56:33 +09:00
Tim Kientzle
c20ef6de2a Remove RemoteInspection code to fetch no-longer-used reflection metadata
without relying on spare bit information in the reflection metadata
(which was added in #40906).  As a result, we can remove the
code from #40906.

This is the first step in such removal.  It removes the RemoteMirror
code for looking up such metadata.  It leaves behind:

* Sufficient stubs for LLDB to continue to build.  Once LLDB is updated, these stubs can be removed as well.

* The compiler code to emit such metadata.  This allows new binaries to still reflect MPEs on older runtimes.  This will need to be kept for a transitional period.
2024-06-13 09:34:43 -07:00
Alejandro Alonso
66ec7abd54 Underscore some things, add explicit internal
Fix some compile issues
2024-06-04 09:06:37 -07:00
Alejandro Alonso
3a115fc290 Properly handle the syscalls on Linux and do some spins
fix some stuff
2024-06-04 09:06:37 -07:00
Alejandro Alonso
9b389a3df5 Fix tests and toolchain build 2024-06-04 09:06:37 -07:00
Alejandro Alonso
92e6a989b2 Cache TID in TLS on Linux 2024-06-04 09:06:36 -07:00
Alejandro Alonso
882568ca4d Directly call gettid syscall instead of library function 2024-06-04 09:06:36 -07:00
Alejandro Alonso
2a60c88457 Implement Mutex in Synchronization 2024-06-04 09:06:34 -07:00
Alastair Houghton
5e24abc970 [Shims] Fix nullability for musl.
Apparently I got the merge for this slightly wrong.

rdar://128388845
2024-05-20 12:18:59 +01:00
Alastair Houghton
0fa5e65bb2 [Stdlib][Build] Fix the standard library build for fully static Linux.
Mostly this just means adding `Musl` as a module dependency of various
things and making sure that we build things for `swift_static` even
if `SWIFT_BUILD_STATIC_STDLIB` isn't enabled.

There's also a slight difference in the declaration of `memcmp()`;
musl's declaration is more like the one we have on Darwin.

rdar://123508245
2024-05-07 16:56:20 +01:00
Kuba (Brecka) Mracek
70d1d998ab Merge pull request #72530 from kubamracek/embedded-add-ivar-destroyers
[embedded] Add IVarDestroyers into class metadata, add support for throws in initializers
2024-03-27 09:12:34 -07:00
Kuba Mracek
37d9be636a [embedded] Handle NULL IVarDestroyers, expand test 2024-03-26 21:14:21 -07:00