Commit Graph

31 Commits

Author SHA1 Message Date
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
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
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Evan Wilde
41d59b215a Update Triple.h location
Triple moved from ADT to TargetParser. Updating includes to reflect
that.
2023-07-17 10:53:42 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Andrew Trick
e7cbf1d8ba Add SWIFT_ABI_DEFAULT_REFERENCE_POISON_DEBUG_VALUE
Poison sentinel value recognized by LLDB as a former reference to a
potentially deinitialized object. It uses no spare bits and cannot point to
readable memory.

This is not ABI per-se but does stay in-sync with LLDB. If it becomes
out-of-sync, then users won't see a friendly diagnostic when
inspecting references past their lifetime.
2021-03-12 19:32:55 -08:00
Arnold Schwaighofer
daa72d3cc5 Add llvm::Attribute::SwiftAsync to the context parameter
* Adds support for generating code that uses swiftasync parameter lowering.

* Currently only arm64's llvm lowering supports the swift_async_context_addr intrinsic.

* Add arm64e pointer signing of updated swift_async_context_addr.

This commit needs the PR llvm-project#2291.

* [runtime] unittests should use just-built compiler if the runtime did

This will start to matter with the introduction of usage of swiftasync parameters which only very recent compilers support.

rdar://71499498
2021-01-22 10:01:55 -08:00
Chris Lattner
415cd50ba2 Reduce array abstraction on apple platforms dealing with literals (#13665)
* Reduce array abstraction on apple platforms dealing with literals

Part of the ongoing quest to reduce swift array literal abstraction
penalties: make the SIL optimizer able to eliminate bridging overhead
 when dealing with array literals.

Introduce a new classify_bridge_object SIL instruction to handle the
logic of extracting platform specific bits from a Builtin.BridgeObject
value that indicate whether it contains a ObjC tagged pointer object,
or a normal ObjC object. This allows the SIL optimizer to eliminate
these, which allows constant folding a ton of code. On the example
added to test/SILOptimizer/static_arrays.swift, this results in 4x
less SIL code, and also leads to a lot more commonality between linux
and apple platform codegen when passing an array literal.

This also introduces a couple of SIL combines for patterns that occur
in the array literal passing case.
2018-01-02 15:23:48 -08:00
Joe Groff
24367fa5d9 Runtime: Have native refcounting entry points ignore negative pointer values on x86-64 and arm64.
The high half of the address space is used by the kernel in these architectures, and it would be useful for us to be able to pack small values into places the ABI otherwise requires a refcountable pointer, such as closures and maybe refcounted existentials.
2017-02-24 16:02:11 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Michael Gottesman
f1734ec0fa [gardening] Format two include guards correctly. NFC. 2016-10-18 23:00:35 -07:00
Peter Cooper
7ec0e7348f Add more calls to the objc runtime for getting an object’s ISA.
Be more conservative in terms of masking ISAs.  This reduces tight coupling with the objc runtime.  This commit adds the required calls to IRGen and the runtime, and a test case to make sure IRGen is correct.
2016-03-10 15:07:20 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Joe Groff
b94a0e49b9 IRGen: Prepopulate class ObjC metadata with a null pointer instead of &_objc_empty_vtable.
The absolute symbol reference isn't needed on OS X >=10.9 or any iOS/watchOS, which are the only Darwin platforms Swift targets. Fixes rdar://problem/22339638.

Swift SVN r31367
2015-08-20 21:25:25 +00:00
Jordan Rose
57b341acd3 [IRGen] Eliminate ObjCBoolTypeSize.
We can get this information from Clang, and should.

Swift SVN r29123
2015-05-29 00:05:09 +00:00
Joe Groff
30200dd777 IRGen: Remove stale "MaxScalarsForDirectResult" hack for arm64.
We now let Clang handle C calling conventions, so we don't need a special value for this to kludge CG types into working. No reason for this to vary across archs for now.

Swift SVN r28397
2015-05-10 17:25:59 +00:00
Joe Groff
220dc2c722 IRGen: Make the target OnceDone predicate value 32-bit-safe.
Thanks John.

Swift SVN r28168
2015-05-05 16:44:13 +00:00
Joe Groff
7b2a6e5e8d IRGen: Emit the "done" check for Builtin.once inline.
This matches how dispatch_once works in C, dramatically cutting the cost of a global accessor by avoiding the runtime call in the hot path and giving the global a unique branch for the CPU to predict away. For now, only do this for Darwin; non-ObjC platforms don't necessarily expose their "done" value as ABI like ours do.

While we're here, change "once" to take a thin function pointer. We don't ever emit global initializers with context dependencies, and this simplifies the runtime glue between swift_once and dispatch_once/std::call_once a bit.

Swift SVN r28166
2015-05-05 15:35:57 +00:00
John McCall
275ef489d4 Switch IRGen to use ClusteredBitVector.
IRGen uses a typedef, SpareBitVector, for its principal
purpose of tracking spare bits.  Other uses should not
use this typedef, and I've tried to follow that, but I
did this rewrite mostly with sed and may have missed
some fixups.

This should be almost completely NFC.  There may be
some subtle changes in spare bits for witness tables
and other off-beat pointer types.  I also fixed a bug
where IRGen thought that thin functions were two
pointers wide, but this wouldn't have affected anything
because we never store thin functions anyway, since
they're not a valid AST type.

This commit repplies r24305 with two fixes:

  - It fixes the computation of spare bits for unusual
    integer types to use the already-agreed-upon type
    size instead of recomputing it.  This fixes the
    i386 stdlib build.  Joe and I agreed that we should
    also change the size to use the LLVM alloc size
    instead of the next power of 2, but this patch
    does not do that yet.

  - It changes the spare bits in function types back
    to the empty set.  I'll be changing this in a
    follow-up, but it needs to be tied to runtime
    changes.  This fixes the regression test failures.

Swift SVN r24324
2015-01-09 21:06:37 +00:00
Dmitri Hrybenko
4caca5d098 Revert "Switch IRGen to use ClusteredBitVector."
This reverts commit r24305.  It broke the standard library build.

Swift SVN r24318
2015-01-09 18:25:21 +00:00
John McCall
6e41b3c742 Switch IRGen to use ClusteredBitVector.
IRGen uses a typedef, SpareBitVector, for its principal
purpose of tracking spare bits.  Other uses should not
use this typedef, and I've tried to follow that, but I
did this rewrite mostly with sed and may have missed
some fixups.

This should be almost completely NFC.  There may be
some subtle changes in spare bits for witness tables
and other off-beat pointer types.  I also fixed a bug
where IRGen thought that thin functions were two
pointers wide, but this wouldn't have affected anything
because we never store thin functions anyway, since
they're not a valid AST type.

Swift SVN r24305
2015-01-09 10:05:51 +00:00
Joe Groff
b5b860ad39 IRGen: Properly create ObjC protocols using the runtime in JIT mode.
Just injecting a new protocol descriptor into an already-running ObjC runtime isn't a good idea, since the runtime might have already canonized the protocol somewhere else, and it won't recognize that classes conform to protocols it doesn't know about.

Swift SVN r23313
2014-11-14 01:01:02 +00:00
Graham Batty
7fbf68fa1c Add target object format to swift target info.
Swift SVN r23306
2014-11-13 19:35:11 +00:00
Joe Groff
df13cab7a8 IRGen: Stop marking witness table symbols as dynamically referenced.
This should no longer be necessary now that we no longer directly dlsym their symbol names.

Swift SVN r23085
2014-11-03 17:34:37 +00:00
Dmitri Hrybenko
1bc0053a6c Only emit used markers for witness tables on Darwin
The inline assembly we emit is only valid for Mach-O.

Swift SVN r22511
2014-10-03 23:53:30 +00:00
John McCall
590c25479d Use isa-masking to read the class object pointer from
instances of Swift subclasses of ObjC classes.

We were already doing this in the runtime.  This patch
unhides the runtime's mask word (swift_isaMask) and makes
IR-gen take advantage of it when it can.

Swift SVN r21592
2014-08-29 21:36:53 +00:00
Joe Groff
ac903bf943 IRGen: Make our use of spare bits in heap objects tagged-pointer-safe.
Don't use spare bits on platforms that use ObjC tagged pointers when an enum payload involves a class-constrained existential, archetype, or ObjC-defined class type. If a payload is of a Swift-defined class type, we can still assume it's a real pointer and use its spare bits. Add an @unsafe_no_objc_tagged_pointer attribute that can be applied to protocols to denote that existentials bounded by that protocol can use spare bits; this is necessary to preserve the layout of bridged Array and Dictionary types, which should not be bound to tagged pointer types in practice (fingers crossed). Fixes <rdar://problem/16270219>.

Swift SVN r18781
2014-06-10 17:07:47 +00:00
Ted Kremenek
fad874708e Adjust test cases.
Swift SVN r17964
2014-05-12 22:01:52 +00:00
Joe Groff
c3d1f0d661 IRGen: Sketch out a SwiftTargetInfo class with fun facts about pointers.
Start out with pointer spare bits, heap object alignment, null page size, and ObjC reserved bits, and provide static values for X86-64 and for a worst-case generic platform.

Swift SVN r7833
2013-08-31 17:01:02 +00:00