Commit Graph

636 Commits

Author SHA1 Message Date
Egor Zhdan 3b55b4ce16 Merge pull request #88683 from egorzhdan/egorzhdan/upcast
[cxx-interop] Import inheritance hierarchy of foreign reference types
2026-05-21 13:43:50 +01: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
Egor Zhdan 6c464579db [cxx-interop] Import inheritance hierarchy of foreign reference types
This teaches ClangImporter to represent a base class of a C++ foreign reference type as a Swift superclass if the base class is an FRT itself, and is a primary base class. Such base types are stored at offset zero within the derived type, which makes casting trivial.

This makes the following possible:
* casting an instance of derived FRT to base FRT explicitly with `as`
* passing an instance of derived FRT as a function parameter of type base FRT
* calling a method of base FRT on an instance of derived FRT without the compiler having to clone methods

Several constructs are not supported:
* downcasting, i.e. casting an instance of base FRT to derived FRT
* casting arrays/sets/dictionaries of FRTs
* casting to virtual bases

Support for casting to a base FRT that is not the primary base (not at offset zero) can be added in a follow-up change.

rdar://85881664 / resolves https://github.com/swiftlang/swift/issues/80231
2026-05-20 12:24:20 +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
Dario Rexin 5b0da23150 [IRGen] Add typed allocations for embedded Swift
rdar://158239258

This change adds logic in the compiler to compute malloc type ids and emit them together with typed allocation calls. It also adds the new runtime function swift_allocObjectTyped, which calls typed malloc.
2026-03-05 12:41:16 -08:00
Arnold Schwaighofer 4d879967a7 [embedded] Feature::EmbeddedExistentials requires Feature::Embedded 2025-12-09 10:21:51 -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
Slava Pestov 819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Hamish Knight 73710e3eef [AST] Introduce Decl::addAttribute
Introduce a convenience entrypoint that also calls `attachToDecl` on
the attribute, and migrate all existing uses of `getAttrs().add` onto
it.
2025-10-16 11:21:54 +01: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
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
Dave Lee ce7a3b39a4 IRGen: Emit objc type encoding for ivars (#81967)
ObjC ivar metadata has up to now emitted an empty string for the ivar's objc type encoding. Conversely, ObjC property metadata is emitted with an objc type encoding. This changes the compiler to also emit an objc type encoding for ivars.

The motivation for this change is for lldb to print ivars of classes declared in Objective-C but implemented in Swift, as defined in [SE-0436](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0436-objc-implementation.md). Without the presence of type encoding for ivars, lldb is unable to present to the user the ivars/properties of instances of such classes.

rdar://138569299
2025-06-25 15:39:23 -07:00
Anthony Latsis 3e9923f0c0 ASTBridging: Bridge swift::AccessorKind directly 2025-06-19 04:26:52 +01:00
Nate Chandler 2cdbfa77bf [DefaultOverrides] IRGen. 2025-03-25 07:22:43 -07:00
Nate Chandler 6581fec9e1 [CoroutineAccessors] PtrAuth. 2025-03-07 11:46:51 -08:00
Nate Chandler 35d06c325d [CoroutineAccessors] Witness and vtable dispatch.
And thunking.
2025-03-07 11:46:50 -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
Kuba Mracek 6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
Ben Barham a7b50f357f Merge remote-tracking branch 'origin/main' into manual-main-merge
Conflicts:
  - `lib/Driver/ToolChains.cpp` conflicting with the `addAllArgs` rename
    for multiple options
2024-09-16 13:53:18 -07:00
Allan Shortlidge 02dbb96b94 AST: Rename AvailabilityContext to AvailabilityRange.
The generality of the `AvailabilityContext` name made it seem like it
encapsulates more than it does. Really it just augments `VersionRange` with
additional set algebra operations that are useful for availability
computations. The `AvailabilityContext` name should be reserved for something
pulls together more than just a single version.
2024-09-13 16:25:18 -07:00
swift-ci b08f7a1307 Merge remote-tracking branch 'origin/main' into rebranch 2024-08-20 13:58:19 -07:00
Slava Pestov 0c2f28fd3d AST: Remove GenericSignature parameter from OpenedArchetypeType::get() 2024-08-20 12:15:27 -04:00
swift-ci e00f4fb59e Merge remote-tracking branch 'origin/main' into rebranch 2024-08-01 09:15:42 -07:00
Arnold Schwaighofer 4b0328f24f IRGen: Also list specialized generic class' objective c ro data in the dedicated linker section
List the emitted ro_data of specialized generic classes in the
__objc_clsrolist section.

rdar://132354480
2024-07-31 12:52:50 -07:00
swift-ci 9b22f7a183 Merge remote-tracking branch 'origin/main' into rebranch 2024-07-05 00:13:41 -07:00
Erik Eckstein 6ed2489aee IRGen: don't stack promote classes for which the layout has not a fixed size
In most cases this was already checked with `ClassLayout::isFixedLayout`. But for classes which are imported from another module and contain implementation-only C-imported stored properties, those properties don't show up in the class layout. Stack promoting such classes will reserve too less space on the stack which leads to all kind of memory corruption problems. The fix is to also check `ClassLayout::isFixedSize`, which returns false for such classes.

Fixes a miscompile
rdar://131067105
2024-07-04 17:03:31 +02:00
Xi Ge 736ccef626 Merge remote-tracking branch 'apple/main' into rebranch 2024-06-20 15:16:55 -07:00
Tim Kientzle 1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
swift-ci efcbe900f9 Merge remote-tracking branch 'origin/main' into rebranch 2024-05-21 10:33:14 -07:00
Becca Royal-Gordon 07b9fe9ce6 Support @objc(CustomName) on extensions
This now specifies a category name that’s used in TBDGen, IRGen, and PrintAsClang. There are also now category name conflict diagnostics; these subsume some @implementation diagnostics.

(It turns out there was already a check for @objc(CustomName) to make sure it wasn’t a selector!)
2024-05-16 13:40:13 -07:00
Becca Royal-Gordon c3225b066e [NFC] Merge two category name accessors 2024-05-01 12:18:19 -07:00
swift-ci 480a3378d9 Merge remote-tracking branch 'origin/main' into rebranch 2024-04-30 20:54:40 -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
Ben Barham cc9091244e [IRGen] Remove bitcasts and zext and sext constant expressions
LLVM did a bunch of opaque pointer cleanup. Migrate IRGen off the
removed APIs.
2024-04-08 08:58:59 -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
Ben Barham f292ec9784 Use the new template deduction guides rather than makeArrayRef
LLVM has removed `make*ArrayRef`, migrate all references to their
constructor equivalent.
2024-02-23 20:04:51 -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
Becca Royal-Gordon 68ec6b68de Support class extensions in objcImpl
Their requirements are now included when typechecking the main body extension, and their conformances are emitted in IRGen.

Fixes rdar://118535473.
2024-02-09 21:32:06 -08: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
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
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
zoecarver 349d37ab24 [embedded] Start fencing parts of IRGen that are not compatible with embedded Swift. 2023-09-06 10:48:17 -07:00
Allan Shortlidge ca8bf981a4 NFC: Hoist queries for unavailable decl optimizations into the AST library.
Moving the query implementation up to the AST library from SIL will allow
conveniences to be written on specific AST element classes. For instance, this
will allow `EnumDecl` to expose a convenience that enumerates element decls
that are available during lowering.

Also, improve naming and documentation for these queries.
2023-08-04 17:39:26 -07:00
Pavel Yaskevich e20e4a546c [IRGen] Use getAllMembers instead of getMembers
Discovered while auditing code that uses `getMembers`.
2023-08-01 10:54:16 -07:00
Erik Eckstein e77e2bcff7 IRGen: don't initialize the object headers of bare objects
For `alloc_ref [bare] [stack]` and `global_value [bare]` omit the object header initialization.
The `bare` flag means that the object header is not used.

This was already done with a peephole optimization inside IRGen for `global_value`. But now rely on the SIL `bare` flag.
2023-06-29 06:57:05 +02: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
Allan Shortlidge 04a22f307e IRGen: Avoid emitting Obj-C method metadata for unavailable methods.
Resolves rdar://109911571
2023-05-31 18:19:56 -07:00
Becca Royal-Gordon bd4108c6be Emit imported @objcImpl conformances
Fixes rdar://108482165.
2023-05-03 19:26:42 -07:00