Commit Graph

298 Commits

Author SHA1 Message Date
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
Alex Lorenz
8b7b1347eb [interop] C++ destination record should be destroyed before being copied into during assignWithCopy / assignWithTake 2023-03-15 12:33:31 -07:00
Dario Rexin
732a6ea222 [IRGen] Fix crash when using C union types with layout strings (#63983)
rdar://106056035
2023-03-01 08:31:39 -08:00
Joe Groff
03a2393a6c IRGen: Track IsCopyable through type infos.
And use the new bit to ensure we don't try to lower move-only types
with common layout value witness surrogates. Take a bit in the runtime
value witness flags to represent types that are not copyable.
2023-02-27 18:52:50 -08:00
Joe Groff
a572c3f491 IRGen: Rename internal 'POD' references to 'TriviallyDestroyable'.
Noncopyable types aren't really "POD", but the bit is still useful to track
whether a noncopyable type has a no-op destroy operation, so rename the
existing bit to be more specific within IRGen's implementation.

Don't rename it in the runtime or Builtin names yet, since doing so will
require a naming transition for compatibility.
2023-02-27 18:52:10 -08:00
Dario Rexin
a8d4d57f11 [IRGen] Generate compressed representation of value witnesses (#63813)
rdar://105837040

* WIP: Store layout string in type metadata

* WIP: More cases working

* WIP: Layout strings almost working

* Add layout string pointer to struct metadata

* Fetch bytecode layout strings from metadata in runtime

* More efficient bytecode layout

* Add support for interpreted generics in layout strings

* Layout string instantiation, take and more

* Remove duplicate information from layout strings

* Include size of previous object in next objects offset to reduce number of increments at runtime

* Add support for existentials

* Build type layout strings with StructBuilder to support target sizes and metadata pointers

* Add support for resilient types

* Properly cache layout strings in compiler

* Generic resilient types working

* Non-generic resilient types working

* Instantiate resilient type in layout when possible

* Fix a few issues around alignment and signing

* Disable generics, fix static alignment

* Fix MultiPayloadEnum size when no extra tag is necessary

* Fixes after rebase

* Cleanup

* Fix most tests

* Fix objcImplementattion and non-Darwin builds

* Fix BytecodeLayouts on non-Darwin

* Fix Linux build

* Fix sizes in linux tests

* Sign layout string pointers

* Use nullptr instead of debug value
2023-02-24 15:40:28 -08:00
Alex Lorenz
fedf86ff76 Merge pull request #63809 from hyp/eng/exception1
[interop] add itanium ABI support for trapping on uncaught exceptions when making a foreign call
2023-02-23 19:41:43 -08:00
Joe Groff
afa5837aeb IRGen: Use deinit to destroy move-only structs that have them.
The `deinit` takes full responsibility for destroying the value, using the
user-defined deinit body and implicitly destroying any remaining resources
not consumed during the deinit.

Remaining to do after this patch:

- Run the deinit for enums
- Pass generic arguments for generic move-only types
- Handle deinits that take their parameter indirectly
- Teach value witness layout about when types are move-only and/or have
  deinits, so that we don't attempt to give move-only types standard
  value witness tables or share box metadata with copyable payloads
2023-02-22 16:48:30 -08:00
Alex Lorenz
ee6017334c [interop] ensure we correctly handle copy constructor invokes 2023-02-22 15:46:19 -08:00
Alex Lorenz
9feb76419b [interop] ignore exceptions in existing interop tests 2023-02-22 11:00:51 -08:00
Alex Lorenz
8998f9769a [interop] trap on uncaught C++ copy constructor exceptions 2023-02-22 10:59:53 -08:00
Alex Lorenz
7d8ecfc506 [interop] trap on uncaught C++ destructor exceptions 2023-02-22 10:59:47 -08:00
Meghana Gupta
7b79056efb IRGen to copy non trivial structs with ptrauth qualified field function pointers 2023-01-30 14:54:47 -08:00
Meghana Gupta
1dac5d48d3 Support for address discriminated pointers 2023-01-27 01:56:44 -08:00
Meghana Gupta
5d95667d74 Rename AddressOnlyClangRecordTypeInfo to AddressOnlyCXXClangRecordTypeInfo 2023-01-26 10:36:43 -08:00
Dario Rexin
3cf40ea504 [IRGen] Re-introduce TypeLayout strings (#62059)
* Introduce TypeLayout Strings

Layout strings encode the structure of a type into a byte string that can be
interpreted by a runtime function to achieve a destroy or copy. Rather than
generating ir for a destroy/assignWithCopy/etc, we instead generate a layout
string which encodes enough information for a called runtime function to
perform the operation for us. Value witness functions tend to be quite large,
so this allows us to replace them with a single call instead. This gives us the
option of making a codesize/runtime cost trade off.

* Added Attribute @_GenerateLayoutBytecode

This marks a type definition that should use generic bytecode based
value witnesses rather than generating the standard suite of
value witness functions. This should reduce the codesize of the binary
for a runtime interpretation of the bytecode cost.

* Statically link in implementation

Summary:
This creates a library to store the runtime functions in to deploy to
runtimes that do not implement bytecode layouts. Right now, that is
everything. Once these are added to the runtime itself, it can be used
to deploy to old runtimes.

* Implement Destroy at Runtime Using LayoutStrings

If GenerateLayoutBytecode is enabled, Create a layout string and use it
to call swift_generic_destroy

* Add Resilient type and Archetype Support for BytecodeLayouts

Add Resilient type and Archetype Support to Bytecode Layouts

* Implement Bytecode assign/init with copy/take

Implements swift_generic_initialize and swift_generic_assign to allow copying
types using bytecode based witnesses.

* Add EnumTag Support

* Add IRGen Bytecode Layouts Test

Added a test to ensure layouts are correct and getting generated

* Implement BytecodeLayouts ObjC retain/release

* Fix for Non static alignments in aligned groups

* Disable MultiEnums

MultiEnums currently have some correctness issues with non fixed multienum
types. Disabling them for now then going to attempt a correct implementation in
a follow up patch

* Fixes after merge

* More fixes

* Possible fix for native unowned

* Use TypeInfoeBasedTypeLayoutEntry for all scalars when ForceStructTypeLayouts is disabled

* Remove @_GenerateBytecodeLayout attribute

* Fix typelayout_based_value_witness.swift

Co-authored-by: Gwen Mittertreiner <gwenm@fb.com>
Co-authored-by: Gwen Mittertreiner <gwen.mittertreiner@gmail.com>
2022-11-29 21:05:22 -08:00
Arnold Schwaighofer
3115feae7e Track when we expect the signature to be used for the function type 2022-10-05 09:21:12 -07:00
Arnold Schwaighofer
3905955589 Merge pull request #61232 from aschwaighofer/wip_opaque_ptr_2
IRGen: Pass the elementType of pointers through to operations
2022-10-04 10:56:41 -07:00
Arnold Schwaighofer
d810b0f7e4 IRGen: Pass the elementType of pointers through to operations
In preparation for moving to llvm's opaque pointer representation
replace getPointerElementType and CreateCall/CreateLoad/Store uses that
dependent on the address operand's pointer element type.

This means an `Address` carries the element type and we use
`FunctionPointer` in more places or read the function type off the
`llvm::Function`.
2022-10-03 15:27:12 -07:00
Allan Shortlidge
9217566d94 NFC: Fix some warnings. 2022-10-03 14:15:25 -07:00
zoecarver
63c4758dad [cxx-interop] Add base classes as opaque fields when lowering in IRGen.
Much of IRGen is built around fields (example: enum packing/unpacking). This is a simpler/less bug-prone way to do it.
2022-09-20 20:20:46 -07:00
Arnold Schwaighofer
2b556bc64b IRGen: Don't emit duplicate defintions for opaque type decls
rdar://99874281
2022-09-15 06:20:07 -07:00
Ben Barham
04035b43b7 [next] Temporarily use getPointerElementType
`PointerType::getElementType` has been removed entirely as part of the
opaque pointers migration. Update to `getPointerElementType` for now
until we've also migrated.
2022-05-05 16:25:10 -07:00
swift-ci
823fa99a00 Merge pull request #41709 from aschwaighofer/fix_deprecated_api_use_warning_irgen_89841981
IRGen: Fix use of deprecated API warning
2022-03-08 07:40:52 -08:00
Arnold Schwaighofer
0b0ad4a26d IRGen: Fix use of deprecated API warning
NFC

rdar://89841981
2022-03-08 05:46:24 -08:00
Allan Shortlidge
58fe2e1a05 Merge pull request #41682 from tshortli/fix-warnings
NFC: Fix a number of warnings emitted when building swift-frontend
2022-03-07 15:11:36 -08:00
Allan Shortlidge
1be8913c9b NFC: Fix a number of warnings emitted when building swift-frontend. 2022-03-04 16:15:33 -08:00
zoecarver
8ac98f62a6 [cxx-interop] Add remaining lifetime operations.
We used to only emit "destroy" and "initializeWithCopy". This commit adds support for emitting "assignWithCopy", "initializeWithTake" and "assignWithTake".
2022-03-04 15:22:53 -08:00
zoecarver
fc1e4df039 [cxx-interop] Empty base classes have zero offset.
Make sure empty base classes don't affect the offset of stored properties.
2022-03-03 15:47:15 -08:00
zoecarver
cc08a3124b [cxx-interop] Make sure to use the cannonical base class type. 2022-03-03 15:34:31 -08:00
zoecarver
bb00e8dcfe [cxx-interop] Rudimentary support for importing base classes.
This commit adds very basic support for importing and calling base class methods, getting and setting base class fields, and using types inside of base classes.
2022-02-18 15:59:52 -08:00
Joe Groff
23557b86da Merge pull request #41111 from jckarter/infinite-type-lowering-bailout
SIL: Detect and bail out on infinite aggregates.
2022-02-01 09:19:07 -08:00
Joe Groff
fe19399585 SIL: Detect and bail out on infinite aggregates.
Sema diagnoses obvious cases of value types defined in terms of themselves,
but it can't catch cases that arise as a result of generic substitution,
such as if a generic struct has a field of associated type that becomes the
same as the struct type itself. SIL may end up handling these types, even
though they can't be instantiated (the runtime will complain and crash if
the metadata is requested), either during SILGen because they were written
in source code, or as a result of generic specialization during optimization.
SIL thus can't rely on diagnostics preventing such types from appearing, so
it needs to be able to continue gracefully when they show up. Add checks
in type lowering for when a struct or enum lowering ends up depending on
itself, and generate an infinite type lowering that's opaque and address-only.

Fixes rdar://80310017
2022-01-31 14:40:18 -08:00
zoecarver
323e2b16a7 [cxx-interop] Fix linker errors when using std-vector.
Adds tests for using std-vector and some other interesting types.

This patch fixes four mis conceptions that the compiler was previously making:

	1. Implicit destructors have no side effects. (Yes, this means we were not cleaning up some objects.)
	2. Implicit destructors have bodies. (Technically they do, but the body doesn't include CallExprs that they make when lowered to IR.)
	3. Functions other than methods can be uninstantiated templates.
	4. Uninstantiated templates may have executable code. (I.e., we can never take the fast path.)

And makes sure that we visit the destructor of any VarDecl (including parameters).
2022-01-17 11:56:15 -08:00
Arnold Schwaighofer
5c9de9e656 Revert "Merge pull request #40356 from gmittert/TypeLayoutFixes"
This reverts commit d27e6e1e46, reversing
changes made to f2e85a2b1f.

It causes an execution time failure in
`Interpreter/struct_extra_inhabitants.swift` with

```
ninja -C swift-macosx-x86_64 check-swift-optimize
```

rdar://86054209
2021-12-06 07:49:57 -08:00
Gwen Mittertreiner
13bb43b72e Revert "Revert "Merge pull request #37116 from gmittert/Descaling""
This reverts commit 7c7dd666c6.
2021-11-30 16:19:34 -08:00
Arnold Schwaighofer
7c7dd666c6 Revert "Merge pull request #37116 from gmittert/Descaling"
This reverts commit 5ebb1b2fc6, reversing
changes made to 76260c2235.

This commit causes compiler crashes when using protocol composition
types involving objc.

Repo:

```
import Foundation

public class SomeObject : NSObject {}

public protocol ProtoA{}

public protocol SomeProtoType { }
public typealias Composition = SomeObject & SomeProtoType

public struct Thing<T: ProtoA> {
    let a: Composition
    let b: T

    init(a: Composition,
         b: T
    ) {
        self.a = a
        self.b = b
    }
}

$ swiftc -c Repo.swift -O
```

While looking at this issue I noticed that it is not correct to use a
ScalarEntry of ObjCReference (or other ScalarKind::XXXReference) for
`AddressOnly##Name##ClassExistentialTypeInfo` types. These should be
calling the IGF.emit##Name##Destroy(addr, Refcounting); functions not
objc_release.
It is probably best to use the macro facilities in a similar fashion like
lib/IRGen/GenExistential.cpp does.

rdar://85269025
2021-11-11 11:44:09 -08:00
Arnold Schwaighofer
8f159cfb88 Fix some deprecated uses of IRBuilder API
rdar://84034998
2021-10-11 11:01:22 -07:00
Gwen Mittertreiner
a3ceda653f Teach TypeLayout the Different Types of References
Summary:
As part of SR-14273, the type layout infrastructure needs to be able to be able
to differentiate between types of scalars so it knows how to release/retain
appropriately. Right now, for example, to destroy a scalar, it blindly calls
into typeInfo's irgen functions which means it's not able to generate any of
the needed information for itself.

This patch adds a field to ScalarTypeLayout to allow them to know what kind of
reference they are and strings through the machinery to provide the information
to set it.

This also moves ScalarTypeLayout::destroy to use the new information.

Test Plan: ninja check-swift

Reviewers: mren, #pika_compiler

Reviewed By: mren

Subscribers: apl, phabricatorlinter

Differential Revision: https://phabricator.intern.facebook.com/D30983093

Tasks: T100580959

Tags: swift-adoption

Signature: 30983093:1632340205:3bdd3218ae86ad6b3d199cc1b504a625e3650ec0
2021-09-29 10:40:55 -07:00
Gwen Mittertreiner
12d78cf925 Use TypeLayout Structs instead of TypeInfo
When generating TypeLayouts, rather than using a Scalar Layout, use an
aligned struct so that we use the typelayout path rather than the
typeinfo path when doing irgen.
2021-09-21 22:21:15 -07:00
Gwen Mittertreiner
e94a871236 Improve TypeLayout IRGen for Fixed Structs
If the Aligned Group is fixed sized and alignment, rather than doing an
alignment computation at runtime, we compute the offset for each field
and emit a GEP.
2021-09-21 22:21:15 -07:00
Min-Yih Hsu
b363bbdd78 [IRGen][Patch 2/2] Add IRGen support for SIL DIExpression
This patch is primarily doing two things:
 - Teach IRGen for some of the instructions to generate debug info based
   on SILDebugVariable rather than AST node, which is not always
   available when reading from SIL.
 - Generate corresponding `llvm.dbg.*` intrinsics and `!DIExpression`
   from the newly added SIL DIExpression.
2021-07-21 09:26:51 -07:00
Zoe Carver
74b7341b2d Merge pull request #32378 from zoecarver/cxx/copy-const
[cxx-interop] Use user defined copy constructor to copy C++ objects.
2021-02-03 22:39:57 -08:00
zoecarver
b2b7f7b853 [cxx-interop] Use user defined copy constructor to copy C++ objects.
If a user-defined copy constructor exists, use that to copy imported C++
types.
2021-02-03 14:34:07 -08:00
swift-ci
404c2bf085 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-28 13:32:22 -08:00
zoecarver
ffa0d1cf93 [cxx-interop] Add support for custom C++ destructors.
This patch adds support for custom C++ destructors. The most notable thing here, I think, is that this is the first place a struct type has a custom destructor. I suspect with more code we will expose a few places where optimization passes need to be fixed to account for this.

One of many patches to fix SR-12797.
2021-01-27 12:54:48 -08:00
Nathan Hawes
40b6798d35 Update usages of 'SwiftNewtypeAttr' for the new spelling 'SwiftNewTypeAttr' 2020-09-29 10:46:13 -07:00
Zoe Carver
63875e9e8f [cxx-interop] [IRGen] TypeInfo for address-only types. (#32973)
The current "ClangRecordTypeInfo" derives from "LoadableTypeInfo" and is
only meant for loadable types. While we have not yet run into problems,
this may cause issues in the future and as more logic is needed around
copying, moving, and destroying C++ objects, this needs to be fixed.
2020-08-10 10:41:21 -07:00