Commit Graph

1350 Commits

Author SHA1 Message Date
Alejandro Alonso
d380bf9ae3 Merge pull request #73277 from Azoy/fix-conditional-invertible
[IRGen] Fix misalignment of conditional invertible requirement counts
2024-05-01 08:10:46 -07:00
Becca Royal-Gordon
f94ed6d606 Hide objcImpl resilience support behind feature
Resilence support will require changes to the Objective-C runtime to expand support for metadata initialization functions. Add a separate experimental feature flag to help with staging that support in, and modify diagnostics to not suggest increasing the minimum deployment target for now.
2024-04-30 12:03:46 -07:00
Becca Royal-Gordon
1dceb24802 Diagnose resilient properties in objcImpl
…when the deployment target is not high enough to support them.
2024-04-30 12:03:46 -07:00
Becca Royal-Gordon
d1f14e5814 Eliminate objcImpl field offset vectors
We really don’t need ‘em; we can just adjust the direct field offsets.

The runtime entry point currently uses a weird little hack that we will refactor away shortly.
2024-04-30 12:03:46 -07:00
Becca Royal-Gordon
427386feea Support resilient stored properties in objcImpl
When an @objc @implementation class requires the use of `ClassMetadataStrategy::Update` because some of its stored properties do not have fixed sizes, we adjust the direct field offsets during class realization by emitting a custom metadata update function which calls a new entry point in the Swift runtime. That entry point adjusts field offsets like `swift_updateClassMetadata2()`, but it only assumes that the class has Objective-C metadata, not Swift metadata.

This commit introduces an alternative mechanism which does the same thing without using any Swift-only metadata. It’s a rough implementation with important limitations:

• We’re currently using the field offset vector, which means that field offsets are being emitted into @objc @implementation classes; these will be removed.
• The new Swift runtime entry point duplicates a lot of `swift_updateClassMetadata2()`’s implementation; it will be refactored into something much smaller and more compact.
• Availability bounds for this feature have not yet been implemented.

Future commits in this PR will correct these issues.
2024-04-30 12:03:44 -07:00
Alejandro Alonso
0e673f70a4 Fix misalignment of conditional invertible requirement counts 2024-04-25 17:58:13 -07:00
Becca Royal-Gordon
9e83099279 [NFC] Refactor objcImpl matadata layout logic
Rather than having individual methods return without doing anything, arrange for ClassMetadataVisitor to never call them in the first place. This makes it so ClassMetadataScanner also knows which fields are omitted and it can compute offsets correctly.

The old logic is still present for field offsets to make this change NFC.
2024-04-24 11:39:24 -07:00
Doug Gregor
ae01d20428 Runtime checking for associated types conforming to invertible protocols
Emit metadata for runtime checks of conformances of associated types to
invertible protocols, e.g., `T.Assoc: Copyable`. This allows us to
correctly handle, e.g., dynamic casting involving conditional
conformances that have such constraints.

The model we use here is to emit an invertible-protocol constraint
that leaves only the specific bit clear in the invertible protocol
set.
2024-04-02 16:42:16 -07:00
Doug Gregor
b84f8ab080 Rename "suppressible protocols" to "invertible protocols".
We've decided to use the "invertible protocols" terminology throughout
the runtime and compiler, so move over to that terminology
consistently.
2024-03-29 11:31:48 -07:00
Doug Gregor
757ebe2979 Future-proof the mangling of invertible protocols
Invertible protocols are currently always mangled with `Ri`, followed by
a single letter for each invertible protocol (e.g., `c` and `e` for
`Copyable` and `Escapable`, respectively), followed by the generic
parameter index. However, this requires that we extend the mangling
for any future invertible protocols, which mean they won't be
backward compatible.

Replace this mangling with one that mangles the bit # for the
invertible protocol, e.g., `Ri_` (followed by the generic parameter
index) is bit 0, which is `Copyable`. `Ri0_` (then generic parameter
index) is bit 1, which is `Escapable`. This allows us to round-trip
through mangled names for any invertible protocol, without any
knowledge of what the invertible protocol is, providing forward
compatibility. The same forward compatibility is present in all
metadata and the runtime, allowing us to add more invertible
protocols in the future without updating any of them, and also
allowing backward compatibility.

Only the demangling to human-readable strings maps the bit numbers
back to their names, and there's a fallback printing with just the bit
number when appropriate.

Also generalize the mangling a bit to allow for mangling of invertible
requirements on associated types, e.g., `S.Sequence: ~Copyable`. This
is currently unsupported by the compiler or runtime, but that may
change, and it was easy enough to finish off the mangling work for it.
2024-03-28 21:26:13 -07:00
Doug Gregor
bbfdf7b36a Merge pull request #72470 from DougGregor/dynamic-suppressible-protocols
Metadata and runtime support for suppressible protocol requirements
2024-03-27 11:49:00 -07:00
Kuba Mracek
b8bd832fba [embedded] Add IVarDestroyers into class metadata, add support for throws in initializers 2024-03-26 20:52:17 -07:00
Konrad `ktoso` Malawski
6132386371 [Distributed] Complete handling of protocol calls and witnesses using adjusted mangling scheme (#72416) 2024-03-23 23:54:23 +09:00
Doug Gregor
b167eece42 Metadata and runtime support for suppressible protocol requirements
Introduce metadata and runtime support for describing conformances to
"suppressible" protocols such as `Copyable`. The metadata changes occur
in several different places:

* Context descriptors gain a flag bit to indicate when the type itself has
  suppressed one or more suppressible protocols (e.g., it is `~Copyable`).
  When the bit is set, the context will have a trailing
  `SuppressibleProtocolSet`, a 16-bit bitfield that records one bit for
  each suppressed protocol. Types with no suppressed conformances will
  leave the bit unset (so the metadata is unchanged), and older runtimes
  don't look at the bit, so they will ignore the extra data.
* Generic context descriptors gain a flag bit to indicate when the type
  has conditional conformances to suppressible protocols. When set,
  there will be trailing metadata containing another
  `SuppressibleProtocolSet` (a subset of the one in the main context
  descriptor) indicating which suppressible protocols have conditional
  conformances, followed by the actual lists of generic requirements
  for each of the conditional conformances. Again, if there are no
  conditional conformances to suppressible protocols, the bit won't be
  set. Old runtimes ignore the bit and any trailing metadata.
* Generic requirements get a new "kind", which provides an ignored
  protocol set (another `SuppressibleProtocolSet`) stating which
  suppressible protocols should *not* be checked for the subject type
  of the generic requirement. For example, this encodes a requirement
  like `T: ~Copyable`. These generic requirements can occur anywhere
  that there is a generic requirement list, e.g., conditional
  conformances and extended existentials. Older runtimes handle unknown
  generic requirement kinds by stating that the requirement isn't
  satisfied.

Extend the runtime to perform checking of the suppressible
conformances on generic arguments as part of checking generic
requirements. This checking follows the defaults of the language, which
is that every generic argument must conform to each of the suppressible
protocols unless there is an explicit generic requirement that states
which suppressible protocols to ignore. Thus, a generic parameter list
`<T, Y where T: ~Escapable>` will check that `T` is `Copyable` but
not that it is `Escapable`, and check that `U` is both `Copyable` and
`Escapable`. To implement this, we collect the ignored protocol sets
from these suppressed requirements while processing the generic
requirements, then check all of the generic arguments against any
conformances not suppressed.

Answering the actual question "does `X` conform to `Copyable`?" (for
any suppressible protocol) looks at the context descriptor metadata to
answer the question, e.g.,

1. If there is no "suppressed protocol set", then the type conforms.
This covers types that haven't suppressed any conformances, including
all types that predate noncopyable generics.
2. If the suppressed protocol set doesn't contain `Copyable`, then the
type conforms.
3. If the type is generic and has a conditional conformance to
`Copyable`, evaluate the generic requirements for that conditional
conformance to answer whether it conforms.

The procedure above handles the bits of a `SuppressibleProtocolSet`
opaquely, with no mapping down to specific protocols. Therefore, the
same implementation will work even with future suppressible protocols,
including back deployment.

The end result of this is that we can dynamically evaluate conditional
conformances to protocols that depend on conformances to suppressible
protocols.

Implements rdar://123466649.
2024-03-21 14:57:47 -07:00
Dario Rexin
8e72a01616 [IRGen] Add ability to disable compact value witnesses from block list (#72335)
rdar://124629183

Allows usage of compact value witnesses to be disabled using a block list.
2024-03-20 12:53:35 -07:00
Doug Gregor
223645c1e3 Remove never-used "version" field from ContextDescriptorFlags. 2024-03-19 16:22:29 -07:00
Konrad `ktoso` Malawski
143d8f9801 [Distributed] Handle distributed thunk requirements/witnesses (#72151)
Co-authored-by: Pavel Yaskevich <pyaskevich@apple.com>
2024-03-11 02:07:22 -07:00
Pavel Yaskevich
b443266580 [IRGen] Distributed: Start emitting distributed actor accessors for protocol requirements 2024-03-04 15:59:57 -08: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
Ellie Shin
72a7760027 Support package SIL linkage.
Decls with a package access level are currently set to public SIL
linkages. This limits the ability to have more fine-grained control
and optimize around resilience and serialization.
This PR introduces a separate SIL linkage and FormalLinkage for
package decls, pipes them down to IRGen, and updates linkage checks
at call sites to include package linkage.

Resolves rdar://121409846
2024-02-06 01:23:14 -08:00
Konrad `ktoso` Malawski
bd19d937f0 [TaskExecutor] Remove underscore from new APIs (#71145) 2024-01-30 06:27:54 +09:00
Ellie Shin
0e8ffef6cd Merge pull request #70897 from apple/es-pkg-fx
Change effectiveAccessLevel of `package` decls from `public` to `package`.
2024-01-24 07:35:13 +09:00
Ellie Shin
ab3b5add3a Currently decls defined with package access level have a different
access level for optimization: `public`. It requires an extra check for
the actual access level that was declared when determining serialization
since the behavior should be different.

This PR sets its effective access level to `package` as originally defined,
updates call sites to make appropriate acces level comparisons, and removes
`package` specific checks.
2024-01-23 11:02:49 -08:00
Doug Gregor
97ea19d191 Introduce a builtin and API for getting the local actor from a distributed one
When an actual instance of a distributed actor is on the local node, it is
has the capabilities of `Actor`. This isn't expressible directly in the type
system, because not all `DistributedActor`s are `Actor`s, nor is the
opposite true.

Instead, provide an API `DistributedActor.asLocalActor` that can only
be executed when the distributed actor is known to be local (because
this API is not itself `distributed`), and produces an existential
`any Actor` referencing that actor. The resulting existential value
carries with it a special witness table that adapts any type
conforming to the DistributedActor protocol into a type that conforms
to the Actor protocol. It is "as if" one had written something like this:

    extension DistributedActor: Actor { }

which, of course, is not permitted in the language. Nonetheless, we
lovingly craft such a witness table:

* The "type" being extended is represented as an extension context,
rather than as a type context. This hasn't been done before, all Swift
runtimes support it uniformly.

* A special witness is provided in the Distributed library to implement
the `Actor.unownedExecutor` operation. This witness back-deploys to the
Swift version were distributed actors were introduced (5.7). On Swift
5.9 runtimes (and newer), it will use
`DistributedActor.unownedExecutor` to support custom executors.

* The conformance of `Self: DistributedActor` is represented as a
conditional requirement, which gets satisfied by the witness table
that makes the type a `DistributedActor`. This makes the special
witness work.

* The witness table is *not* visible via any of the normal runtime
lookup tables, because doing so would allow any
`DistributedActor`-conforming type to conform to `Actor`, which would
break the safety model.

* The witness table is emitted on demand in any client that needs it.
In back-deployment configurations, there may be several witness tables
for the same concrete distributed actor conforming to `Actor`.
However, this duplication can only be observed under fairly extreme
circumstances (where one is opening the returned existential and
instantiating generic types with the distributed actor type as an
`Actor`, then performing dynamic type equivalence checks), and will
not be present with a new Swift runtime.

All of these tricks together mean that we need no runtime changes, and
`asLocalActor` back-deploys as far as distributed actors, allowing it's
use in `#isolation` and the async for...in loop.
2024-01-22 17:27:31 -08:00
Slava Pestov
79d4260ae2 SIL: Remove the incorrect old getTypeLinkage() 2024-01-03 14:45:29 -05:00
Konrad `ktoso` Malawski
828f589be4 Initial Task Executor implementation Task(on:), addTask(on:) etc. (#68793)
Co-authored-by: John McCall <rjmccall@gmail.com>
2023-12-12 17:14:24 +09:00
Kavon Farvardin
e99ce1cc5d [NCGenerics] add ~Escapable
Basic implementation of `~Escapable` in the type system.

rdar://119216918
2023-12-10 01:25:43 -08:00
Nate Chandler
b266abe5c0 [BitwiseCopyable] Add a marker protocol.
Behind the experimental feature BitwiseCopyable.
2023-11-30 15:25:46 -08:00
Erik Eckstein
b33bb3828d IRGen: fix the superclass pointer for generic classes in embedded swift
fixes in IRGen crash
2023-11-06 21:08:22 +01:00
Konrad `ktoso` Malawski
9e75142911 Task Executors: Prepare for new TaskExecutor protocol & builtins 2023-11-01 16:02:39 +09:00
Nuri Amari
87b3127697 [swift-vfe] Disable for @objc classes
Swift methods can be called from Objective-C without a vtable
load, and more importantly without emitting an `llvm.type.checked.load`
instruction. These are effectively virtual callsites that are not
visible to the VFE logic in GlobalDCE.

To to safely enable swift-vfe we must prevent vtables whose functions
are callable from Objective-C from being eliminated.

To do this we ensure such vtables have public vcall_visibility, which
indirectly prevents GlobalDCE from stripping them.
2023-10-26 11:37:10 -07:00
swift-ci
5b72fbae64 Merge remote-tracking branch 'origin/main' into rebranch 2023-10-06 17:15:26 -07:00
Kuba Mracek
7da86b1148 [embedded] Serialize+deserialize vtables, fix using non-generic classes from other modules in embedded Swift 2023-10-06 10:25:22 -07:00
swift-ci
447482505a Merge remote-tracking branch 'origin/main' into rebranch 2023-10-05 08:05:25 -07:00
Egor Zhdan
0b2048d096 Merge pull request #68578 from apple/egorzhdan/cxx-convertible-to-bool
[cxx-interop] Add conversion to Bool for types that define `operator bool()`
2023-10-05 15:52:03 +01:00
Evan Wilde
700aa8aa70 Merge remote-tracking branch 'upstream/main' into rebranch
Merge conflict while removing `nocapture` from
`s21move_function_dbginfo20addressOnlyValueTestyyxAA1PRzlF`. Resolution
was to remove nocapture from the expected output in both cases:
```
// CHECK-LABEL: define swiftcc void @"$s21move_function_dbginfo20addressOnlyValueTestyyxAA1PRzlF"(ptr noalias[-nocapture-] %0, ptr %T, ptr %T.P)
```

Conflict cause: 4858cb6225
This is the same as the original change to this file. The conflict seems
to be due to the next line changing, when moving from llvm.dbg.addr to
llvm.dbg.value.

Conflicts:
  test/DebugInfo/move_function_dbginfo.swift
2023-09-26 13:36:57 -07:00
Kuba Mracek
a454d9a8a9 [embedded] Fix incorrectly ptrauth signed destructor in vtables 2023-09-25 12:18:36 -07:00
Egor Zhdan
f0be52accd [cxx-interop] Add conversion to Bool for types that define operator bool()
C++ `operator bool()` is currently imported into Swift as `__convertToBool()`, which shouldn't be used by clients directly.

This adds a new protocol into the C++ stdlib overlay: `CxxConvertibleToBool`, along with an intitializer for `Swift.Bool` taking an instance of `CxxConvertibleToBool`.

rdar://115074954
2023-09-18 14:54:45 +01:00
swift-ci
190021146d Merge remote-tracking branch 'origin/main' into rebranch 2023-09-13 21:59:05 -07:00
Alejandro Alonso
60774fb722 Merge pull request #68450 from Azoy/fix-array-like
[IRGen] Some fixes for raw layout types and noncopyable types in the stdlib
2023-09-13 21:49:48 -07:00
swift-ci
ae30cba296 Merge remote-tracking branch 'origin/main' into rebranch 2023-09-13 10:20:48 -07:00
Kuba Mracek
e2de477609 [embedded] avoid metadata accessors, fix vtable map, add a diagnostic on non-final generic functions 2023-09-12 16:57:09 -07:00
Kuba Mracek
d0c2a4ccf8 [embedded] Initial support for generic classes in embedded Swift
- VTableSpecializer, a new pass that synthesizes a new vtable per each observed concrete type used
- Don't use full type metadata refs in embedded Swift
- Lazily emit specialized class metadata (LazySpecializedClassMetadata) in IRGen
- Don't emit regular class metadata for a class decl if it's generic (only emit the specialized metadata)
2023-09-12 09:44:54 -07:00
Alejandro Alonso
ab143ee3ee Some fixes for raw layout types and noncopyable types in the stdlib 2023-09-11 20:45:09 -07:00
swift-ci
9977c4ec7f Merge remote-tracking branch 'origin/main' into rebranch 2023-09-11 17:55:02 -07:00
Kuba Mracek
51f54b4efb [embedded] add test for calling methods on classes, fix layout 2023-09-11 12:26:01 -07:00
Kuba Mracek
ae63e7549b [embedded] Add support for (non-generic) classes in embedded Swift
- In embedded Swift, classes get a simplified metadata: Basically just a vtable + destructor + superclass pointer.
- Only non-resilient (intended as permanent restriction), non-generic classes (for now) supported.
- Relax the check that prohibits metadata emission and usage to allow classes.
2023-09-11 11:22:11 -07:00
swift-ci
f52fc06ac7 Merge remote-tracking branch 'origin/main' into rebranch 2023-09-06 18:14:46 -07:00
zoecarver
349d37ab24 [embedded] Start fencing parts of IRGen that are not compatible with embedded Swift. 2023-09-06 10:48:17 -07:00
Nate Chandler
6ccd08b4e0 Merge branch 'main' into rebranch.
Resolve conflicts introduced in
https://github.com/apple/swift/pull/67944 as follows:

```
diff --git a/include/swift/Runtime/RuntimeFunctions.def b/include/swift/Runtime/RuntimeFunctions.def
index 3c973b5884b..44cde707d17 100644
--- a/include/swift/Runtime/RuntimeFunctions.def
+++ b/include/swift/Runtime/RuntimeFunctions.def
@@ -2537,16 +2537,10 @@ FUNCTION(AutoDiffCreateLinearMapContextWithType,
          swift_autoDiffCreateLinearMapContextWithType, SwiftCC,
          DifferentiationAvailability,
          RETURNS(RefCountedPtrTy),
-<<<<<<< HEAD
-         ARGS(SizeTy),
+         ARGS(TypeMetadataPtrTy),
          ATTRS(NoUnwind),
          EFFECT(AutoDiff),
          MEMEFFECTS(ArgMemOnly))
-=======
-         ARGS(TypeMetadataPtrTy),
-         ATTRS(NoUnwind, ArgMemOnly),
-         EFFECT(AutoDiff))
->>>>>>> public-github/main

 // void *swift_autoDiffProjectTopLevelSubcontext(AutoDiffLinearMapContext *);
 FUNCTION(AutoDiffProjectTopLevelSubcontext,
@@ -2563,16 +2557,10 @@ FUNCTION(AutoDiffAllocateSubcontextWithType,
          swift_autoDiffAllocateSubcontextWithType, SwiftCC,
          DifferentiationAvailability,
          RETURNS(Int8PtrTy),
-<<<<<<< HEAD
-         ARGS(RefCountedPtrTy, SizeTy),
+         ARGS(RefCountedPtrTy, TypeMetadataPtrTy),
          ATTRS(NoUnwind),
          EFFECT(AutoDiff),
          MEMEFFECTS(ArgMemOnly))
-=======
-         ARGS(RefCountedPtrTy, TypeMetadataPtrTy),
-         ATTRS(NoUnwind, ArgMemOnly),
-         EFFECT(AutoDiff))
->>>>>>> public-github/main
```
2023-08-24 16:24:32 -07:00