Commit Graph

295 Commits

Author SHA1 Message Date
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
Kavon Farvardin
9606f523c1 [IRGen] fix createCXXCopyConstructorFunctionType
The previous implementation assumes `UnsafePointer` has no requirements
on its generic parameter. This fix handles any requirements that may be
present on the parameter. With NoncopyableGenerics, the generic
parameter actually does gain a requirement.
2024-01-23 22:41:59 -08:00
Arnold Schwaighofer
2f94890e5f IRGen: Remove supportsTypedPointers, getNonOpaquePointerElementType, isOpaqueOrPointeeTypeMatches
Typed pointers are no longer supported by llvm.

rdar://121083958
2024-01-17 07:33:30 -08:00
Kuba Mracek
5e4ae5e0ec [embedded] Avoid compiler crash when using an opaque result type 2023-11-27 22:00:18 -08:00
Kavon Farvardin
a9c64baaa3 [Sema] refactor TypeDecl::isNoncopyable
In preparation for reporting whether the inverse marking on a TypeDecl
was inferred instead of explicit.
2023-11-06 15:40:12 -08:00
Kavon Farvardin
148897ac1a [nfc] refactor ValueDecl::isMoveOnly
I've renamed the method to `TypeDecl::isNoncopyable`, because the query
doesn't make sense for many other kinds of `ValueDecl`'s beyond the
`TypeDecl`'s. In fact, it looks like no one was relying on that anyway.

Thus, we now have a distinction where in Sema, you ask whether
a `Type` or `TypeDecl` is "Noncopyable". But within SIL, we still
preserve the notion of "move-only" since there is additionally the
move-only type wrapper for types that otherwise support copying.
2023-10-18 13:45:50 -07:00
Alejandro Alonso
6fde84eb70 Fix size and alignment for fixed 2023-09-11 20:59:46 -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
zoecarver
349d37ab24 [embedded] Start fencing parts of IRGen that are not compatible with embedded Swift. 2023-09-06 10:48:17 -07:00
Dario Rexin
27b1764d13 [Runtime+IRGen] Fix layout string flag in type layout and add array functions 2023-08-21 10:54:35 -07:00
Alejandro Alonso
44198d16a4 Merge pull request #67611 from Azoy/raw-types-are-cool!
[WIP] Implement dependent layouts for raw types
2023-08-16 06:55:42 -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
Alejandro Alonso
df17f7be90 Implement dependent layouts for raw types
Update raw_layout.swift

Allow for concrete specializations of raw layout

Make test platform agnostic
2023-08-01 14:09:23 -07: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
Alex Lorenz
138def7627 Merge pull request #65922 from hyp/eng/excp-cons-spec
[cxx-interop] evaluate default constructor's unevaluated exception sp…
2023-06-08 15:35:23 -07:00
Alex Lorenz
b0dab0b48c [cxx-interop] evaluate default constructor's unevaluated exception spec if needed when emitting C++ constructor call
Fixes https://github.com/apple/swift/issues/65891
2023-06-08 11:02:29 -07:00
Dario Rexin
97381e6411 [IRGen] Support additional single payload enum cases in layout strings (#66042)
* [IRGen] Support additional single payload enum cases in layout strings

rdar://105837101

Adds layout string support for single payload enums with simple (non-scattered) extra inhabitant patterns

* Add more test cases
2023-05-23 13:17:51 -07:00
Allan Shortlidge
367dbd069d IRGen: Skip lowering unavailable opaque type descriptors.
Part of rdar://107425181
2023-03-31 09:36:42 -07:00
zoecarver
d283aa050f [cxx-interop] Destroy things 🤷 2023-03-20 15:47:23 -07:00
zoecarver
1e43ba0514 [cxx-interp] Don't select the move contstructor if it hasn't been defined. 2023-03-20 15:47:23 -07:00
zoecarver
a10bf21fcd [cxx-interop] 'Support' C++ move only types; fix a few bugs in the object model.
The implemented object model should now match the object model outlined in the Forward Vision document.
2023-03-20 15:47:22 -07:00
Alex Lorenz
de56accb96 Merge pull request #64397 from hyp/eng/destroy-before-assigncopytake
[interop] C++ destination record should be destroyed before being cop…
2023-03-17 16:14:06 -07:00
Dario Rexin
2f8c1a402c [IRGen] Make pointers to accessor functions in layout strings relative (#64155)
* [IRGen] Make pointers to accessor functions in layout strings relative

rdar://106319336

Pointers embedded in static layout strings should always be relative, so layout strings can reside in read-only memory.

* Properly handle reference storage ownership

* Pass layout tag and metadata / type layout ppointers separately

* Layout string instantiation fully working

* Fix cases where hasLayoutString flag was not set when it should have

* Update include/swift/ABI/Metadata.h
2023-03-17 09:02:51 -07:00