Commit Graph

925 Commits

Author SHA1 Message Date
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
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
Nate Chandler
e0ead75622 [CoroutineAccessors] Use typed-malloc. 2025-10-30 03:59:18 -07:00
Nate Chandler
511050ed73 [NFC] IRGen: Lift this function from IGF to IGM. 2025-10-30 03:59:18 -07:00
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
Slava Pestov
41ff7383d9 IRGen: Clean up opaque type specialization wrappers 2025-10-23 15:36:54 -04:00
Michael Gottesman
2fa3908e94 [concurrency] Add a new type Builtin.ImplicitActor.
This is currently not wired up to anything. I am going to wire it up in
subsequent commits.

The reason why we are introducing this new Builtin type is to represent that we
are going to start stealing bits from the protocol witness table pointer of the
Optional<any Actor> that this type is bitwise compatible with. The type will
ensure that this value is only used in places where we know that it will be
properly masked out giving us certainty that this value will not be used in any
manner without it first being bit cleared and transformed back to Optional<any
Actor>.
2025-10-16 10:51:13 -07:00
Ellis Hoag
2c919e138e [ObjC][Gen] Emit ObjC strings to respective sections (#84300)
Clang emits ObjC strings into special sections

d5e7c27d53/clang/lib/CodeGen/CGObjCMac.cpp (L4056-L4072)

As far as I can tell from `CGObjCMac.cpp`:
* types go into `__objc_methtype`
* class, category, and protocol names go into `__objc_classname`
* method names, property names, and property types go into
`__objc_methname`

See also https://github.com/swiftlang/swift/pull/84236.
2025-09-30 08:42:11 -07:00
Anton Korobeynikov
7668666ad2 Support differentiation of wrapped value modify accessors (#78794)
Some fixes for coroutines with normal results and `partial_apply` of coroutines were required.

Fixes #55084
2025-09-10 02:30:26 -07:00
Doug Gregor
dbc30dc5c9 [IRGen] Cache the layout of the LLVM struct type for statically-initialized objects
Lazy emission of SIL global variables caused us to go through the
creation of two different LLVM struct types for the same
initialization, tripping an assertion in LLVM. Cache it along with
other information about the static-initialized object rather than
rebuilding it.

Also fix the lazy_globals test to account for the laziness,
generalizing it to also run on arm64 so I won't miss it locally.
2025-09-04 15:58:32 -07:00
Doug Gregor
4009814783 [IRGen] Lazily emit SIL global variables
Delay the emission of SIL global variables that aren't externally
visible until they are actually used. This is the same lazy emission
approach that we use for a number of other entities, such as SIL
functions.

Part of rdar://158363967.
2025-09-04 13:03:15 -07:00
John McCall
46be95847b Extract TypeLowering's recursive type properties into a header, add
functions to compute them directly without a TypeLowering object, and
change a lot of getTypeLowering call sites to just use that.

There is one subtle change here that I think is okay: SILBuilder used to
use different TypeExpansionContexts when inserting into a global:
- getTypeLowering() always used a minimal context when inserting into
  a global
- getTypeExpansionContext() always returned a maximal context for the
  module scope
The latter seems more correct, as AFAIK global initializers are never
inlinable. If they are, we probably need to configure the builder with
an actual context properly rather than making global assumptions.

This is incremental progress towards computing this for most types
without a TypeLowering, and hopefully eventually removing TL entirely.
2025-08-01 15:00:57 -04:00
Anthony Latsis
f8577a2731 IRGen: Address llvm::Type::getPointerTo deprecation
See https://github.com/llvm/llvm-project/pull/113331.
2025-07-21 12:37:15 +01:00
Tobias Stadler
76568372f4 [IRGen] Setup LLVMRemarkStreamer using existing RemarkStreamer
Calling setupLLVMOptimizationRemarks overwrites the MainRemarkStreamer
in the LLVM context. This prevents LLVM from serializing the remark meta
information for the already emitted SIL remarks into the object file.
Without the meta information bitstream remarks don't work correctly.

Instead, emit SIL remarks and LLVM remarks to the same RemarkSerializer,
and keep the file stream alive until after CodeGen.
2025-06-12 18:31:41 +01:00
Nate Chandler
ce61e2ea11 [IRGen] Fix type of deleted coro error func.
It was previously erroneously an async function pointer.  Also fix the
name.
2025-04-04 18:18:11 -07:00
Slava Pestov
9148ae32bd Merge pull request #80482 from slavapestov/abstract-conformance-cleanup
AST: Simplify ProtocolConformanceRef operations a little bit
2025-04-04 10:37:49 -04:00
nate-chandler
0e2ca9a07d Merge pull request #80483 from nate-chandler/general-coro/20250327/2
[CoroutineAccessors] Open-code a task_dealloc_thru
2025-04-03 21:21:06 -07:00
Augusto Noronha
cac0abbfde Merge pull request #80380 from augusto2112/fix-opaque
[DebugInfo] Use underlying type of global variables with opaque type
2025-04-03 15:39:11 -07:00
Slava Pestov
e475b08011 AST: Remove AssociatedType 2025-04-03 17:35:35 -04:00
Augusto Noronha
6abbc0d754 [DebugInfo] Use underlying type of global variables with opaque type
rdar://144881938
2025-03-31 15:52:09 -07:00
Nate Chandler
41fa97f62d [IRGen] Add convenience to define transient types.
Expose the createStructType helper to clients of IRGenModule which want
to define types which won't ever be used elsewhere.  This is just a
convenience--such clients could already have directly used the API on
llvm::Module directly.
2025-03-31 13:48:39 -07:00
Nate Chandler
24bb9bb738 [CoroutineAccessors] Adopt swiftcoro param attr. 2025-03-28 17:10:53 -07:00
Nate Chandler
0c2a38b10b [CoroutineAccessors] Directly reference allocators
Replace the call to a runtime function that looks up the allocator with
a direct reference to a just-emittedd sought-after global allocator.
2025-03-26 08:35:35 -07:00
Nate Chandler
2cdbfa77bf [DefaultOverrides] IRGen. 2025-03-25 07:22:43 -07:00
Tony Allevato
d94bd80c62 Add support for raw identifiers.
Raw identifiers are backtick-delimited identifiers that can contain any
non-identifier character other than the backtick itself, CR, LF, or other
non-printable ASCII code units, and which are also not composed entirely
of operator characters.
2025-03-11 17:18:43 -04:00
nate-chandler
c0ba520fac Merge pull request #79781 from nate-chandler/general-coro/20250227/1
[CoroutineAccessors] Dispatch and PtrAuth.
2025-03-07 20:08:42 -08:00
Konrad `ktoso` Malawski
fda7f539fb Reapply "Task names" (#79562) (#79600) 2025-03-08 10:58:49 +09:00
Nate Chandler
35d06c325d [CoroutineAccessors] Witness and vtable dispatch.
And thunking.
2025-03-07 11:46:50 -08:00
Nate Chandler
d1f1b4c86b [CoroutineAccessors] Use swiftcorocc if available.
When it's available, use an open-coded allocator function that returns
an alloca without popping if the allocator is nullptr and otherwise
calls swift_coro_alloc.  When it's not available, use the malloc
allocator in the synchronous context.
2025-02-27 18:20:53 -08:00
Nate Chandler
dd8cbe3e0a [CoroutineAccessors] Use retcon.once variant.
Allocate a coroutine frame in the caller based on the size in the
corresponding "function pointer" and pass it along with an allocator to
the callee.
2025-02-27 07:53:58 -08:00
Nate Chandler
fdd3eb0fce [IRGen] Define coroutine function pointer types.
They're isomorphic to async function pointers.  Even so, having this
distinction will be clearer than reusing AFPs for this other purpose.
2025-02-27 07:53:13 -08:00
Konrad `ktoso` Malawski
09003d6f11 Revert "Merge pull request #77609 from ktoso/wip-task-names" (#79562)
This reverts commit 4ab5d2604f.
2025-02-23 22:59:21 -08:00
Konrad `ktoso` Malawski
4ab5d2604f Merge pull request #77609 from ktoso/wip-task-names
[Concurrency] Task names
2025-02-21 22:28:33 +09:00
Saleem Abdulrasool
5770d59e4d Merge pull request #78427 from compnerd/internals
IRGen: special case VWT emission linkage computation
2025-01-25 08:51:06 -08:00
Saleem Abdulrasool
6df1c54c70 IRGen: remove unused method (NFCI)
This function is not used anywhere in the compiler or runtime, so remove
it.
2025-01-21 08:42:11 -08:00
Saleem Abdulrasool
59e7c1b538 IRGen: special case VWT emission linkage computation
The well known builtin and structural types are strongly defined in the
runtime which is compacted into the standard library. Given that the VWT
is defined in the runtime, it is not visible to the Swift compilation
process and as we do not provide a Swift definition, we would previously
compute the linkage as being module external (`dllimport` for shared
library builds). This formed incorrect references to these variables and
would require thunking to adjust the references.

One special case that we add here is the "any function" type
representation (`@escaping () -> ()`) as we do use the VWT for this type
in the standard library but do not consider it part of the well known
builtin or structural type enumeration.

These errors were previously being swallowed by the build system and
thus escaped from being fixed when the other cases of incorrect DLL
storage were.
2025-01-07 10:45:13 -08:00
Saleem Abdulrasool
ddfb26b1c6 Merge pull request #78226 from compnerd/standard-standards
IRGen: simplify the stdlib special casing for Windows
2024-12-18 14:35:01 -08:00
Saleem Abdulrasool
c052d1b935 IRGen: simplify the stdlib special casing for Windows
Remove `IRGenModule::useDllStorage()` as there is a standalone version
that is available and the necessary information is public from the
`IRGenModule` type. Additionally, avoid the wrapped `isStandardLibrary`
preferring to use the same method off of the public accessors. This
works towards removing some of the standard library special casing so
that it is possible to support both static and dynamic standard
libraries on Windows.
2024-12-16 14:46:38 -08:00
Egor Zhdan
57c7ecd244 [cxx-interop] Fix memory layout for structs with out-of-order base types
In C++, a primary base class that is placed in the beginning of the type's memory layout isn't always the type that is the first in the list of bases – the base types might be laid out in memory in a different order.

This makes sure that IRGen handles base types of C++ structs in the correct order.

This fixes an assertion in asserts-enabled compilers, and an out-of-memory error in asserts-disabled compilers. The issue was happening for both value types and foreign reference types. This change also includes a small refactoring to reuse the logic between the two code paths.

rdar://140848603
2024-12-16 15:53:43 +00:00
Arnold Schwaighofer
f9941b339a IRGen: Outlined value functions of types that might expand differently in
different TUs must use private linkage

rdar://136376117
2024-11-12 13:03:13 -08:00
swift-ci
ffb7eef5ab Merge remote-tracking branch 'origin/main' into rebranch 2024-10-17 05:15:25 -07:00
Arnold Schwaighofer
8ebb3ec473 IRGen: Add the ability to mark certain generic entry points in back traces
Mark generic function calls with concrete parameters, generic v-table calls and
generic witness table calls where self is generic.
2024-10-14 14:06:10 -07:00
Ben Barham
c15bc19946 Merge remote-tracking branch 'origin/main' into main-to-rebranch
Conflicts:
  - `lib/Serialization/ModuleFileSharedCore.cpp` new headers on main
2024-09-26 16:01:56 -07:00
eeckstein
8c5d7ee452 Merge pull request #76669 from eeckstein/class-existentials
embedded: support class existentials with generic classes
2024-09-26 07:37:35 +02:00
Doug Gregor
624570db57 Improve naming of the isResilientConformance flag that disables optimizations
Thank you, Arnold, for talking through this with me.
2024-09-25 13:05:22 -07:00
Doug Gregor
21b2f099de Use "resilient conformance" logic for deciding protocol dependencies involving Sendable
When compiling with library evolution and a pre-Swift 6.0 deployment
target, a mismatch between the notion of resilience used for determining
whether a protocol that inherits Sendable might need to be treated as
"dependent" differed from how other parts of IR generation decided
whether to conformance should be considered as resilient. The
difference came when both the protocol and its conforming type are in
the same module as the user.

Switch over to the "is this conformance resilient?" query that takes
into account such conformances.

Fixes rdar://136586922.
2024-09-25 12:09:40 -07:00
Joe Groff
4e3f665cc8 Merge pull request #76688 from jckarter/atomic-layout
IRGen: Set a "not bitwise borrowable" bit in value witnesses for `@_rawLayout` types.
2024-09-25 08:09:56 -10:00
Erik Eckstein
2950e4521e SIL: representation for specialized witness tables
The main change here is to associate a witness table with a `ProtocolConformance` instead of a `RootProtocolConformance`.
A `ProtocolConformance` is the base class and can be a `RootProtocolConformance` or a `SpecializedProtocolConformance`.
2024-09-25 19:32:08 +02:00
Joe Groff
57a56e5804 IRGen: Set a "not bitwise borrowable" bit in value witnesses for @_rawLayout types.
For types like `Atomic` and `Mutex`, we want to know that even though they are
technically bitwise-takable, they differ from other bitwise-takable types until
this point because they are not also "bitwise-borrowable"; while borrowed,
they are pinned in memory, so they cannot be passed by value as a borrowed
parameter, unlike copyable bitwise-takable types. Add a bit to the value witness
table flags to record this.

Note that this patch does not include any accompanying runtime support for
propagating the flag into runtime-instantiated type metadata. There isn't yet
any runtime functionality that varies based on this flag, so that can
be implemented separately.

rdar://136396806
2024-09-24 19:08:50 -07:00