Commit Graph

318 Commits

Author SHA1 Message Date
susmonteiro
2c9bb7a6eb Revert "[cxx-interop] Implicitly defined copy constructors"
This reverts commit 1c5bbe0290.
2025-11-05 10:42:05 +00:00
susmonteiro
dd921adc05 Revert "[cxx-interop] Implicitly defined move constructors"
This reverts commit c3cd9939c1.
2025-11-05 10:41:38 +00:00
susmonteiro
c3cd9939c1 [cxx-interop] Implicitly defined move constructors 2025-11-03 16:16:06 +00:00
susmonteiro
1c5bbe0290 [cxx-interop] Implicitly defined copy constructors 2025-10-30 14:17:19 +00:00
Anthony Latsis
5730344cfe IRGen: Fix rebranch build
`getTrailingRequiresClause` has a different result type in
`stable/21.x`.
2025-09-30 21:34:07 +01:00
swift-ci
88da350809 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-30 01:34:08 -07:00
susmonteiro
e8b9b52c5d [cxx-interop] Emit diagnostic to mark C++ type as non-copyable 2025-09-29 11:57:29 +01:00
swift-ci
e61c65951b Merge remote-tracking branch 'origin/main' into rebranch 2025-09-19 14:36:30 -07:00
Gabor Horvath
e4e54236ca [cxx-interop] Fix over-releasing reference members of trival C++ types
Large trivial types were copied via memcpy instead of doing a field-wise
copy. This is incorrect for types with reference fields where we also
need to bump the corresponding refcounts.

rdar://160315343
2025-09-19 14:28:33 +01:00
swift-ci
4838c6e954 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-01 23:15:28 -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
3f841b515c Merge remote-tracking branch 'origin/main' into jepa-rebranch 2025-07-30 14:31:33 +01:00
Doug Gregor
6ba560fb4b [Clang importer] Allow noncopyable C structs to define "destroy" operation
A C struct can be imported as noncopyable, but C doesn't have
destructors, so there is no way to provide user-defined logic to
perform the destruction. Introduce a new swift_attr that applies to
imported noncopyable types and which provides such a "destroy"
operation. It can be used like this:

    typedef struct __attribute__((swift_attr("~Copyable")))
                   __attribute__((swift_attr("destroy:wgpuAdapterInfoFreeMembers")))
      WGPUAdapterInfo { /*...*/ } WGPUAdapterInfo;

    void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo adapterInfo);

This will bring the WGPUAdapterInfo struct in as a noncopyable type
that will be cleaned up by calling wgpuAdapterInfoFreeMembers once it
is no longer in use.

Implements rdar://156889370.
2025-07-28 00:02:31 -07:00
swift-ci
367f0082fc Merge remote-tracking branch 'origin/main' into rebranch 2025-07-22 22:17:45 -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
swift-ci
ae49f6807e Merge remote-tracking branch 'origin/main' into rebranch 2025-05-30 14:34:38 -07:00
Egor Zhdan
a7c174431f [cxx-interop] Do not crash when emitting layout of std::string
If a `[[no_unique_address]]` field has zero size according to Clang, and field has a type that isn't representable in Swift, Swift would previously try to add an opaque field of size 1 for it.

This is wrong and was causing crashes for `std::string` while emitting a padding field:
```
_LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);
```

rdar://151941799
2025-05-30 17:32:15 +01:00
swift-ci
dd8c99e095 Merge remote-tracking branch 'origin/main' into rebranch 2025-04-25 10:17:16 -07:00
Gabor Horvath
939ee49dcb [cxx-interop] Fix a crash with [[no_unique_address]]
Swift does not support storing fields in the padding of the previous
fields just yet, so let's not import fields like that from C++.
Represent them as opaque blobs instead.

Fixes #80764

rdar://149072458
2025-04-25 12:46:31 +01:00
Anthony Latsis
5996a95c21 IRGen: Adjust uses of clang::FieldDecl::getBitWidthValue (parameter removed)
Per cfe26358e3051755961fb1f3b46328dc2c326895 (llvm-project).
2025-04-24 01:52:48 +01:00
susmonteiro
51357a952c [cxx-interop] Prevent usage in Swift of C++ move constructor with default args 2025-03-07 16:02:38 +00:00
susmonteiro
bc6573ec32 [cxx-interop] Prevent usage in Swift of C++ copy constructor with default args 2025-03-03 13:22:11 +00:00
Gabor Horvath
998591e9f9 [cxx-interop] Do not import zero-sized fields
Zero sized fields are messing up the offset calculations when we import
C++ fields to Swift. We assume that the size of the field is determined
by the type of the field. This is not true for fields marked with
no_unique_address. Those fields can have 0 size while the
sizeof(decltype(field)) is still 1.

rdar://143907490
2025-02-03 11:55:54 +00:00
Egor Zhdan
fe5b047bdc [cxx-interop] NFC: Remove unused variable
This fixes a `-Wunused-variable` warning that got introduced in 57c7ecd2.
2024-12-19 16:50:44 +00: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
Gabor Horvath
f1ac9af3f9 [cxx-interop] Remove stale reference to rdar
There is no reason to require a body for move/copy ctors to use them.
2024-12-03 10:58:26 +00:00
Gabor Horvath
179786b784 [cxx-interop] Simplify finding copy/move ctors in IRGen 2024-11-27 15:51:57 +00:00
Egor Zhdan
d3e43bbe7b [cxx-interop] Fix IRGen for C++ types that use tail padding of their bases
In C++, a field of a derived class might be placed into the tail padding of a base class. Swift was not handling this case correctly, causing an asserts-disabled compiler to run out of RAM, and an asserts-enabled compiler to fail with an assertion.

Fixes this IRGen assertion:
```
Assertion failed: (offset >= NextOffset && "adding fields out of order"), function addField, file GenStruct.cpp, line 1509.
```

rdar://138764929
2024-11-04 13:44:43 +00:00
Alejandro Alonso
7219daf418 Collect metadata from like type and add type information when GEPing into raw layouts 2024-09-05 18:04:47 -07:00
Alejandro Alonso
8b773dcdf7 Add an ArrayLayoutEntry and dispatch to that for array raw layout 2024-09-04 15:13:28 -07:00
Arnold Schwaighofer
c7b91a9a1b Address review comments 2024-08-22 07:50:52 -07:00
Arnold Schwaighofer
ad6f6dec6e IRGen: We cannot call destroy through metadata of private types of non-copyable fields
So call the destroy on the closing type instead.

Amends the concepts areFieldsABIAccessible/isABIAccessible to take metadata
accessibility of non-copyable types into account.

rdar://133990500
2024-08-21 11:22:30 -07:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Gábor Horváth
1b4bba3cec Update lib/IRGen/GenStruct.cpp
Co-authored-by: John McCall <rjmccall@apple.com>
2024-07-17 09:32:23 +01:00
Gábor Horváth
824fccac92 Update lib/IRGen/GenStruct.cpp
Co-authored-by: John McCall <rjmccall@apple.com>
2024-07-17 09:32:17 +01:00
Gabor Horvath
ce3fa734ce [cxx-interop] Fix wrong field offsets with value types using inheritance
Instead of adding opaque fields for the base subobjects this patch
introduces a recursive walk to add all the base fields to the generated
Swift struct.

rdar://126754931
2024-07-11 17:20:18 +01:00
Slava Pestov
3fcda140bb AST: ModuleDecl::checkConformance() is a static method 2024-07-06 12:05:46 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Alex Lorenz
af36238004 Merge pull request #74640 from hyp/eng/virtual-destructor-vtable-ref-irgen-linux-android
[cxx-interop] ensure that the body of implicit virtual destructor is …
2024-06-26 10:32:51 -07:00
Alex Lorenz
cd1ca50c63 [cxx-interop] ensure that the body of implicit virtual destructor is defined before emitting the clang decl for it
This ensures that the destructor definition is emitted into the module's LLVM IR, fixing a linker error.
2024-06-21 17:00:15 -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
Slava Pestov
d5985158cb IRGen: Fix some more incorrect conformance checks 2024-05-07 10:37:49 -04:00
Erik Eckstein
1b1d5ed020 IRGen: support the @sensitive attribute
Call `swift_clearSensitive` after destroying or taking "sensitive" struct types.

Also, support calling C-functions with "sensitive" parameters or return values. In SIL, sensitive types are address-only and so are sensitive parameters/return values.
Though, (small) sensitive C-structs are passed directly to/from C-functions. We need re-abstract such parameter and return values for C-functions.
2024-04-09 12:01:11 +02:00
Egor Zhdan
802f20ec00 [cxx-interop][IRGen] Assign Clang function types to copy constructors
This fixes an assertion failure when building certain projects for arm64e with `-use-clang-function-types` Swift compiler flag:
```
Expected non-null Clang type for @convention(c)/@convention(block) function but found nullptr
```

rdar://121227452
2024-04-03 19:26:32 +01:00
Erik Eckstein
c11408ea2f IRGen: Don't let the offset of an empty field be undef.
If the field address comes from a struct_element_addr which is a result of an optimized `MemoryLayout<S>.offset(of: \.field)` we cannot return undef.
We have to be consistent with `offset(of:)`, which returns 0.
Therefore we need to return the base address of the struct.

rdar://117265274
2024-03-11 15:21:03 +01:00
Akira Hatanaka
b3f302b96b [IRGen] Fix a bug where an argument wasn't annotated with sret (#71459)
Fix a bug in expandExternalSignatureTypes where it wasn't annotating a function call parameter type with sret when the result was being returned indirectly.

The bug was causing calls to ObjC methods that return their results indirectly to crash.

Additionally, fix the return type for C++ constructors computed in expandExternalSignatureTypes. Previously, the return type was always void even on targets that require constructors to return this (e.g., Apple arm64), which was causing C++ constructor thunks to be emitted needlessly.

Resolves rdar://121618707
2024-02-22 14:14:47 -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
Nate Chandler
d1bd039ebd [IRGen] Use BC TI for resilient conformers.
Resilient types conforming to BitwiseCopyable mostly shouldn't go
through the VWT for value operations, they should just use memcpy.
2024-02-16 11:14:26 -08:00
Slava Pestov
af3ab14986 SILGen: Clean up substitution map construction to deal with Copyable 2024-01-31 21:55:52 -05:00
Kavon Farvardin
b4985f7fde [NFC] replace canBeNoncopyable
First, "can have an absence of Copyable" is a rather confusing notion,
so the query is flipped to "can be Copyable". Next, it's more robust to
ask if a conformance exists for the TypeDecl to answer that question,
rather than trying to replicate what happens within that conformance
lookup.

Also renames `TypeDecl::isEscapable` to match.
2024-01-23 22:42:38 -08:00