Commit Graph

1379 Commits

Author SHA1 Message Date
Kavon Farvardin 7d4e8c7155 Merge pull request #88820 from kavon/noncopyable-field-metadata
IRGen/Runtime: emit noncopyable field metadata
2026-05-22 04:36:04 -07:00
Kavon Farvardin 0a73911fe1 Runtime: improve support for noncopyable types
The VWT flags for isNonCopyable aren't always being set
correctly by the runtime when it is creating type metadata.

We can't always set them correctly, in the case of fixed-layout
types, as their VWT is static and read-only, yet could still be
conditionally copyable. For example:

```
struct FactoryOf<T: ~Copyable>: ~Copyable {
  var counter: Int = 0
}
extension FactoryOf: Copyable where T: Copyable {}
```

In the common case of an unconditionally Copyable type, we do
effectively two quick bit-tests: one to see if the VWT thinks
it "could be Copyable" and then delegate to
`checkInvertibleRequirements` which has a fast-path for types
that are unconditionally Copyable by checking some bits in the
type's context descriptor.

related to rdar://176282041
2026-05-21 16:38:36 -07:00
Doug Gregor 14bc0baecf Introduce the notion of an "effective" code generation model
The code generation model for a particular declaration or conformance
can be defined explicitly with `@export(interface)`,
`@export(implementation)`, or `@inlinable` (for declarations),
indicating where the definition will occur.

Embedded Swift also has some limitations on what can be emitted into
IR. For example, a generic function cannot be `@export(interface)`
because Embedded Swift does not support unspecialized generics.

Compute the effective code generation model based on what was
explicitly specified, the limitations of the model, and the default
code generation model for the given module, which defaults to
"inlinable" but can be made "implementation" by the DeferredCodeGen
feature. Use the effective code generation model for IR- and SIL-level
determinations of linkage and where to emit symbols.
[WIP] Start computing and using the "effective" code generation model

FIXUP linkage of the alias symbol
2026-05-20 08:35:11 -07:00
susmonteiro f643b2651d [cxx-interop] Fix abiName of C++ templates 2026-05-15 19:08:23 +01:00
Doug Gregor 09dd9db3e1 [Embedded] Extend @export(interface) to non-generic types
@export(interface) makes it so that Embedded Swift will emit a strong
definition of a symbol in its defining module, and use that symbol in
other modules. Extend this notion to non-generic types, where we will
emit a strong definition for the full type metadata, and let it be
referenced from other modules rather than the lazy, shared emission.

Implements rdar://176392354.
2026-05-08 14:23:21 -07:00
Guillaume Lessard 2861d9e097 Merge pull request #88640 from glessard/rdar139816157-FullyInhabited-protocols 2026-05-01 10:07:53 -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 2aabf1109d [Embedded Swift] Lazily emit metadata for metatypes and existentials
Following the emission of metadata for tuple and function types, also
lazily emit (sparse) metadata for metatype and existential types. This
is needed when forming existential values.
2026-04-27 14:02:04 -07:00
Guillaume Lessard 84bfe1651f [sema] Basic recognition of the FullyInhabited protocols 2026-04-19 21:59:38 -07:00
Doug Gregor aad51cab01 [Embedded] Remove the ability to disable existentials in Embedded Swift
Support for existentials in Embedded Swift has been available for a
little while now and appears to be solid. Remove the ability to disable
them (via `-disable-experimental-feature EmbeddedExistentials`), both
because it simplifies the code and because it's an ABI break to
disable the feature.
2026-04-17 17:38:01 -07:00
Erik Eckstein 68b55ff3ce IRGen: generate conformance entries in vtables for fast existential casts 2026-04-03 07:49:35 +02:00
Kavon Farvardin 28cac41ef9 Reparenting: sign the conformance descriptor
The protocol conformance descriptor passed to swift_getWitnessTable
needs to be signed on arm64e. When reparenting a protocol P with
new parent Q, we emit a small "default conformance accessor" function
that instantiates the type's witness table for Q using its witness
table for P, at runtime. This accessor is only used if the type
conforming to P comes from a module that hasn't been rebuilt since
the reparenting happened.

resolves rdar://171661924
2026-03-03 18:13:53 -08:00
susmonteiro e2752096b6 [cxx-interop] Add CxxBorrowingSequence to the C++ stdlib overlay 2026-02-13 08:37:29 +00:00
Elsa Keirouz 2a0bc57763 [AST] desugar ForEachStmt for BorrowingSequence protocol 2026-02-11 16:02:25 +00:00
Arnold Schwaighofer 3dd5286d48 [embedded] [IRGen] Avoid creating "aliases" for type metadata address points in client modules
We don't need to export the type metadata address point alias in clients that
lazily emit other module's type metadata. There will be an exported
metadata symbol in the originating module for that purpose.

Instead, satisfy any local uses of the metadata address point uses by its
underlying address computation.

This is to workaround a bug where LLVM generates the wrong assembly for
weak aliases that point to an offset of another symbol.

```
@"$e1C7MyClassCySiGN" = weak_odr hidden alias %swift.type, getelementptr
inbounds (<{ ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }>,
ptr @"$e1C7MyClassCySiGMf", i32 0, i32 1)
```

Generates:

```
.weak_reference _$e1C7MyClassCySiGN
.private_extern _$e1C7MyClassCySiGN
.alt_entry  _$e1C7MyClassCySiGN
  _$e1C7MyClassCySiGN = _$e1C7MyClassCySiGMf+8
```

Instead of

```
.weak_definition _$e1C7MyClassCySiGN
.private_extern _$e1C7MyClassCySiGN
.alt_entry  _$e1C7MyClassCySiGN
  _$e1C7MyClassCySiGN = _$e1C7MyClassCySiGMf+8
```

Leading for "weak"ness to be ignored and duplicate type medata symbol linkage
errors.

rdar://169573918
2026-02-06 12:24:08 -08:00
Kavon Farvardin 006a0a494a Reparenting: require an extension
This change forces you to write ``@reparented` relationships
on an extension of a protocol, rather than on the protocol
itself.

The ProtocolConformance needs to be associated with some
GenericContext and IRGen expects it to be an ExtensionDecl.
That environment defines under what conditions the conformance
exists. We also need to define witnesses for the new parent's
requirements in an extension anyway, so it's a natural fit.

The previous workaround for this was kind of awful, as it'd
require searching all the protocol's extensions and "guess"
which extension they want to represent the conformance. While
we could try to synthesize an extension, there's two
challenges there:

1. Due to SuppressedAssociatedTypes, it's not so simple to
synthesize an unconstrained ExtensionDecl.
2. We currently rely on same-type requirements to pin the
associated types to particular witnesses of those requirements
in the extension. So it's not purely unconstrained! For example,

```
extension Seq: @reparented BorrowSeq where Iter == MyIter {}
```

The constraints that are disallowed (but not yet diagnosed)
are conditional conformance requirements, as the default
conformance for a reparenting cannot depend on those.

Thus, it's better that programmers to specify the extension.
2026-02-03 16:40:21 -08:00
Kavon Farvardin eb7fe3c8a3 Reparenting: initial functionality 2026-02-03 16:39:19 -08:00
Dario Rexin 88d3282897 Merge pull request #86764 from drexin/wip-168715030
[IRGen] Don't set HasLayoutString flag on lazy type context descripto…
2026-01-28 01:38:25 -08:00
Dario Rexin 0bbc3b7f07 [IRGen] Don't set HasLayoutString flag on lazy type context descriptor of non-copyable types
rdar://168715030

There was a copyability check missing, that caused the flag to be set for non-copyable types.
2026-01-27 10:49:33 -08:00
Meghana Gupta 7c6b13f511 Remove unnecessary Borrow and Mutate kinds from method descriptors and protocol requirements
Changing runtime in anyway can effect backward deployability.

Treeat borrow and mutate accessors as a regular method since they don't need any differentiated
runtime handling.
2026-01-20 11:44:18 -08:00
Meghana Gupta a3adcf103a Update borrow and mutate accessors as opaque accessors 2026-01-20 11:43:59 -08:00
Tim Kientzle 8eabeeb8ca [SE-0474] Read2/Modify2 => YieldingBorrow/YieldingMutate
This updates a large number of internal symbols, function names,
and types to match the final approved terminology.  Matching the
surface language terminology and the compiler internals should
make the code easier for people to understand into the future.
2026-01-03 16:05:12 -08:00
Arnold Schwaighofer 4d879967a7 [embedded] Feature::EmbeddedExistentials requires Feature::Embedded 2025-12-09 10:21:51 -08:00
Arnold Schwaighofer d06929ccd2 [embedded] Add support for some foreign metadata 2025-12-08 15:35:43 -08:00
Arnold Schwaighofer 4764ad70e7 Fix typos/needless whitespace 2025-12-05 13:10:54 -08:00
Arnold Schwaighofer 93ab16283b [embedded] Lazily emit class metadata in embedded mode 2025-12-05 09:03:16 -08:00
Arnold Schwaighofer b9f6454386 [embedded] Add support for storing/casting function types 2025-11-21 14:55:32 -08:00
Arnold Schwaighofer 4285a2169d IRGen: Start support for embedded existentials
Allow storing struct, enum, and tuple types in an any.
2025-11-17 12:46:35 -08:00
Slava Pestov 819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Nate Chandler e0ead75622 [CoroutineAccessors] Use typed-malloc. 2025-10-30 03:59:18 -07:00
nate-chandler 580f2e4fd6 Merge pull request #84635 from nate-chandler/rdar161606892
[VariadicGenerics] Fix memeffect of metadata accessor.
2025-10-02 19:14:52 -07:00
Nate Chandler 0c685d07f6 [VariadicGenerics] Fix memeffect of metadata accessor.
The metadata accessor for a variadic generic type takes as arguments
packs of metadata records and witness tables, and each such pack is
passed in a buffer.  So any such accessor is not correctly annotated
`memory(none)`.

rdar://161606892
2025-10-01 15:43:07 -07:00
Ellis Hoag 2c919e138e [ObjC][Gen] Emit ObjC strings to respective sections (#84300)
Clang emits ObjC strings into special sections

https://github.com/llvm/llvm-project/blob/d5e7c27d53887e6ae490d8e26193a54987728458/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
Slava Pestov 6bf5213d41 Merge pull request #84251 from slavapestov/fix-rdar151171381
Fix @_opaqueReturnTypeOf module interface syntax for parameter packs
2025-09-13 08:44:44 -04:00
Slava Pestov 10fa09cae6 IRGen: Remove a bit of duplicated code 2025-09-12 15:43:31 -04:00
John McCall fb9a1e301b Fix IRGen to pass complete metadata to various concurrency builtins
Fixes rdar://146155888
2025-09-11 18:27:39 -04:00
Gábor Horváth 56a525b0c9 Merge pull request #84152 from Xazax-hun/add-nullptr-to-metadata
[cxx-interop] Make the size of anonymous types metadata is unchanged
2025-09-08 17:44:43 +01:00
Gabor Horvath cae23fd098 [cxx-interop] Make the size of anonymous types metadata is unchanged
Add a null pointer to the value witness table instead of completely
skipping emitting the pointer.
2025-09-08 11:51:10 +01:00
John McCall 64d020ec45 [NFC] Introduce a convenience specialization of CanTypeVisitor that
forwards the paired nominal type methods to common implementations.
2025-09-05 14:02:36 -04:00
Gabor Horvath 0c909a8395 [cxx-interop] Basic support for anonymous structs with non-copyable fields
Anonymous structs cannot be copied or moved, these operations only can
happen to their enclosing non-anonymous types. Stop trying to emit
special member functions and value witness tables for these structs.
2025-09-04 15:20:17 +01:00
Dario Rexin 69cafe42a3 Merge pull request #83837 from drexin/wip-157795547
[IRGen] Do not set HasLayoutString flag for non-copyable types requir…
2025-08-27 13:58:59 -07:00
Dario Rexin 67d0a22e2f [IRGen] Do not set HasLayoutString flag for non-copyable types requiring metadata instantiation
rdar://157795547

When types contain stored properties of resilient types, we instantiate their metadata at runtime. If those types are non-copyable, they won't have layout strings, so we must not set the flag.
2025-08-20 17:12:59 -07:00
Egor Zhdan b440c4ff70 [cxx-interop] Enable reference-counted types on Windows
This enables the use of reference-counted foreign reference types on Windows. As it turns out, the assertion that was failing on Windows previously was unnecessary. This also enabled many of the tests on Windows.

rdar://154694125 / resolves https://github.com/swiftlang/swift/issues/82643
2025-08-20 12:35:10 +01:00
Allan Shortlidge 78f50ba741 AST: Refactor ConditionallyAvailableSubstitutions to use AvailabilityQuery.
Conditionally available opaque return types should support availability
conditions that are evaluated in any availability domain. Update
`ConditionallyAvailableSubstitutions` to model its conditions with
`AvailabilityQuery` instead of assuming that conditions are always a single
version query for the current platform.
2025-08-06 18:01:10 -07: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
Konrad 'ktoso' Malawski 1f87f60d6c Revert revert & fix distributed protocol and TBD handling
This reverts a revert that was done internally here https://stashweb.sd.apple.com/projects/DEVTOOLS/repos/swift/pull-requests/8697/overview
and corrects some issues with the reverted code while at it.

This fix is crucial to land for correctness and necessary to fix the
rdar://144568615 which had a fix submitted however that assumed that
THIS change also was present, so the fix submitted for 144568615 is
incomplete without this change as well.

This enables, and adds more TBD testing and was explicitly checked
against the GameKit reproducer project from rdar://144568615.

I will also upstream the necessary parts missing from OSS repo there so
we have alignment between all branches.
2025-06-30 11:13:05 +09:00
Anthony Latsis 1ceeb7089b Merge pull request #82338 from AnthonyLatsis/jepa
ASTBridging: Bridge more enums directly
2025-06-20 23:13:50 +01:00
Anthony Latsis 3e9923f0c0 ASTBridging: Bridge swift::AccessorKind directly 2025-06-19 04:26:52 +01:00
Dario Rexin 5ec58209dc [IRGen] Fix placeholder logic for emission of conditionally inverted protocols
rdar://153681688

Instead fo counting the actual conformances, the logic took the size of the bit field, i.e. used the highest set bit, so when a type had a conditional conformance only on ~Escapable, but not on ~Copyable, it would still add 2 placeholders, but only fill one.
2025-06-18 11:28:13 -07:00
Susana Monteiro 6164af2f46 Merge pull request #81832 from swiftlang/susmonteiro/class-metadata-private-fields
[cxx-interop] Support for printing C++ foreign references
2025-06-11 11:58:45 +01:00