Commit Graph

1326 Commits

Author SHA1 Message Date
Dario Rexin
8b48a0d3e0 [Runtime+IRGen] Instantiate layout strings for generic multi payload enum (#66621)
Instantiating layout strings for generic types reduces code size and is expected to improve performance of generic value witnesses.
2023-06-14 09:31:11 -07:00
Slava Pestov
24a1d011d8 Merge pull request #65340 from slavapestov/bind-element-archetype-fix
Fix issues with binding local element archetypes, and misc other stuff
2023-04-21 07:52:31 -04:00
Slava Pestov
0d1e50705e IRGen: Clean up some crusty old code 2023-04-20 22:05:46 -04:00
Dario Rexin
022311e438 [IRGen] Fix layout string generation for pre-specialized metadata (#65162)
* [IRGen] Fix layout string generation for pre-specialized metadata

rdar://108012057

Pre-specialized metadata has to be specifically handled by using the bound generic type instead of the unbound one. All the necessary information is already being passed down as BoundGenericTypeCharacteristics, we just need to apply them when present.

* Add tests and a few fixes

* Fixes after rebase

* Attempt to fix Windows linker issue in test
2023-04-20 13:26:41 -07:00
Dario Rexin
57dc5a6d42 [IRGen] Add flags to enable or disable generation of layout string value witnesses (#64819)
rdar://107477762
2023-04-07 13:45:52 -07:00
Slava Pestov
cca91797ed IRGen: Hollow out NecessaryBindings
This removes the "optimization" where a function type, metatype or
tuple type was split up into structural components, because it seems
that in general we need this structural type metadata again.

Similarly, this no longer tries to split up dependent concrete
conformances and instead passes the witness table in the context.

This makes the context larger potentially, but it avoids calls to
metadata access functions and swift_getWitnessTable() every time the
closure is invoked.
2023-03-28 19:21:45 -04:00
Dario Rexin
bdbab35453 [IRGen] Use ConditionalDominanceScope in AbstractMetadataAccessor::emit (#64585)
* [IRGen] Use ConditionalDominanceScope in AbstractMetadataAccessor::emit

rdar://103179745

Under certain conditions the missing dominance scope caused a cached witness table ref to be used in a block that it was not available in.

* Fix target in test
2023-03-23 18:34:27 -07:00
Konrad `ktoso` Malawski
e0877594da [Concurrency] Custom executors with move-only Job (#63569) 2023-03-18 14:02:43 +09:00
Nuri Amari
fe2f85765e Merge pull request #64043 from apple/ns-option-linkage-conflict
Fix ASTMangler mangling NS_OPTION differently in C++ mode
2023-03-17 09:33:47 -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
Konrad `ktoso` Malawski
41f99fc2ae [Executors][Distributed] custom executors for distributed actor (#64237)
* [Executors][Distributed] custom executors for distributed actor

* harden ordering guarantees of synthesised fields

* the issue was that a non-default actor must implement the is remote check differently

* NonDefaultDistributedActor to complete support and remote flag handling

* invoke nonDefaultDistributedActorInitialize when necessary in SILGen

* refactor inline assertion into method

* cleanup

* [Executors][Distributed] Update module version for NonDefaultDistributedActor

* Minor docs cleanup

* we solved those fixme's

* add mangling test for non-def-dist-actor
2023-03-15 23:42:55 +09:00
John McCall
0e932329c3 Fix fulfillments for type metadata and witness table packs
A lot of the fixes here are adjustments to compensate in the
fulfillment and metadata-path subsystems for the recent pack
substitutions representation change.  I think these adjustments
really make the case for why the change was the right one to make:
the code was clearly not considering the possibility of packs
in these positions, and the need to handle packs makes everything
work out much more cleanly.

There's still some work that needs to happen around type packs;
in particular, we're not caching them or fulfilling them as a
whole, and we do have the setup to do that properly now.
2023-03-10 12:52:37 -05:00
Nuri Amari
709321b69f Fix ASTMangler mangling NS_OPTION differently in C++ mode
CF_OPTIONS is defined differently in the SDK based on
a __cplusplus preprocessor branch. As a result, declarations
referencing CF_OPTIONS are mangled differently depending
on if C++ interop is enabled.

This meant a module compiled with cxx interop on could
not be linked with a module compiled without and vice versa.
This patch modifies the mangler such that the mangled names
are consistent. This is achieved by feeding the mangler a modified
AST node that looks like the Objective-C definition of CF_OPTIONS,
even when we have cxx interop enabled.
2023-03-09 09:30:05 -08:00
Egor Zhdan
ac72084854 Merge pull request #63683 from apple/egorzhdan/cxx-optional
[cxx-interop] Add `CxxOptional` protocol for `std::optional` ergonomics
2023-03-06 10:43:05 +00:00
Dario Rexin
5fe716577b [IRGen] Add layout strings for generic and resilient types (#64023)
* [IRGen] Add layout strings for generic and resilient types

rdar://105837048

* Add some corner cases

* Add flag to enable generic instantiation and some fixes

* Fix resilient types

* Fix metadata accessor function pointers in combined layout strings
2023-03-05 15:26:45 -08:00
Slava Pestov
05849e6268 IRGen: Start to emit generic context metadata for packs 2023-03-03 14:16:54 -05:00
Egor Zhdan
a12986ade4 [cxx-interop] Add CxxOptional protocol for std::optional ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::optional` when used from Swift code.

As of now, the overlay adds an initializer of `Swift.Optional` that takes an instance of `CxxOptional` as a parameter.
2023-03-03 19:41:30 +01:00
Slava Pestov
32caa17b11 Runtime: Finish removing the 'extra argument' notion 2023-03-03 02:21:08 -05:00
Andrew Trick
f1ff6958a3 Merge pull request #63825 from atrick/diagnose-implicit-raw-bitwise
Warn on implicit pointer conversion from nontrivial inout values.
2023-03-02 10:57:30 -08:00
Slava Pestov
480a917fa2 Runtime: Sketch out metadata for variadic generic types 2023-02-28 17:33:06 -05:00
Andrew Trick
bdeb58d61f Warn on implicit pointer conversion from nontrivial inout values.
Fixes a usability problem with implicit inout conversion to raw pointers.

For example, supporting this conversion turns out to be bad:

    void read_void(const void *input);

    func foo(data: inout Data) {
        read_void(&data)
    }

People understandably expect Foundation.Data to have the same sort of
implicit conversion as Array. But it does something very wrong
instead.

We could have added an an attribute to Data and other copy-on-write
containers to selectively suppress implicit conversion. But there is
no good reason to allow implicit conversion from any non-trivial
type. It is extremely dangerous, and almost always accidental. Note
that this problem becomes worse now that the compiler views imported
`char *` arguments as raw pointers. For example:

  void read_char(const char *input);

  var object: AnyObject = ...
  read_void(&object1)

This seems like a good time to correct this old Swift 3 behavior.

Plan: Add a warning now. Convert it to an error in Swift 6 language
mode based on feedback.

Fixes rdar://97963116 (It's really easy to accidentally corrupt a Data
object with the & operator)
2023-02-27 21:51:17 -08:00
Dario Rexin
a0e1810f62 [IRGen] Generate layout strings for subset of enums (#63930)
rdar://105959425
2023-02-27 07:33:34 -08:00
Arnold Schwaighofer
43510bc1fd Merge pull request #63897 from aschwaighofer/colocate_type_descriptors
IRGen: Put type descriptors in their own section to collocated them
2023-02-25 13:09:07 -08:00
Dario Rexin
96d988a431 [Runtime] Move bytecode layout handling into runtime (#63901)
rdar://105904548
2023-02-25 10:18:45 -08:00
Arnold Schwaighofer
8a7b8dc39c IRGen: Put type descriptors in their own section to collocated them
rdar://104697150
2023-02-25 07:51:30 -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
Egor Zhdan
919eea7045 [cxx-interop] Add CxxDictionary protocol for std::map ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::map` and `std::unordered_map` when used from Swift code.

As of now, `CxxDictionary` adds a subscript with an optional return type that mimics the subscript of `Swift.Dictionary`.

Similar to https://github.com/apple/swift/pull/63244.
2023-02-09 14:31:06 +00:00
nate-chandler
7b317c0f1e Merge pull request #63363 from nate-chandler/variadic-generics/irgen/open_pack_element
[IRGen] Lowered open_pack_element.
2023-02-02 20:26:29 -08:00
Nate Chandler
6a44de214d [GenericRequirement] Added pack variants.
Added pack flavors of requirement kinds for metadata and witness tables.

Fixes the function signatures for variadic generic functions which
previously used %swift.type* for variadic generic parameters--those are
lists of metadata and should actually be %swift.type**.
2023-02-02 12:48:39 -08:00
Nate Chandler
28c8c6e0b1 [NFC] GenericRequirement vends an llvm::Type *.
The mapping from requirement kind to llvm::Type is repeated several
places.  Pull it into a helper.
2023-02-02 12:48:39 -08:00
Kavon Farvardin
ab130883a3 Initial ban of move-only types from being used generically
Since values of generic type are currently assumed to always
support copying, we need to prevent move-only types from
being substituted for generic type parameters.

This approach leans on a `_Copyable` marker protocol to which
all generic type parameters implicitly must conform.

A few other changes in this initial implementation:

- Now every concrete type that can conform to Copyable will do so. This fixes issues with conforming to a protocol that requires Copyable.
- Narrowly ban writing a concrete type `[T]` when `T` is move-only.
2023-02-01 23:38:28 -08:00
Egor Zhdan
6366878308 [cxx-interop] Add CxxSet protocol for std::set ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::set`, `std::unordered_set` and `std::multiset` when used from Swift code.

As of now, `CxxSet` adds a `contains` function to C++ sets.

C++ stdlib set types are automatically conformed to `CxxSet`: `std::set`, `unordered_set`, `std::multiset`. Custom user types are not conformed to `CxxSet` automatically: while a custom type might have an interface similar to `std::set`, the semantics might differ, and adding a conformance would cause confusion.
2023-01-26 19:35:30 +00:00
Slava Pestov
9f3267b5c0 IRGen: Refactor NominalMetadataVisitor.h 2022-12-13 11:56:33 -05:00
Slava Pestov
65d2d4d621 IRGen: Encapsulate fields of GenericRequirement better 2022-12-11 22:14:37 -05:00
Arnold Schwaighofer
3ca4a6e3cf Merge pull request #62403 from aschwaighofer/collocate_section_the_3rd
IRGen: Co-locate metadata instatiation/completions/accessor functions in a special section
2022-12-07 06:50:45 -08:00
Doug Gregor
1b8b706dac Merge pull request #62405 from DougGregor/macro-diagnostics 2022-12-05 21:37:27 -08:00
Doug Gregor
f0fc1c51bb Remove compiler plugin support library
The functionality of the compiler plugin support library has been
subsumed into parts of the compiler. Remove the functionality and its
last test.
2022-12-05 12:54:36 -08:00
Arnold Schwaighofer
39e5e3990d IRGen: Co-locate metadata instatiation/completions/accessor functions in a special section
For spatial locality on startup.

Hide collocating metadata functions in a separate section behind a flag.
The default is not to collocate functions.

rdar://101593202
2022-12-05 09:52:04 -08:00
Arnold Schwaighofer
bb80f10ecc Revert "Merge pull request #62275 from aschwaighofer/rdar102481054"
This reverts commit 3617b7603c, reversing
changes made to 58a519a5c1.

This causes issues for the linker and branches accross sections if
addresses are too far apart.
2022-12-01 11:36:04 -08:00
Egor Zhdan
9f542898cd Merge pull request #62293 from apple/egorzhdan/synthesize-cxx-convertible
[cxx-interop] Synthesize conformances to `CxxConvertibleToCollection`
2022-12-01 11:44:02 +00:00
Arnold Schwaighofer
3617b7603c Merge pull request #62275 from aschwaighofer/rdar102481054
Revert "Merge pull request #62203 from apple/revert-61984-colocate_in…
2022-11-29 14:35:25 -08:00
Egor Zhdan
cb562d26b8 [cxx-interop] Synthesize conformances to CxxConvertibleToCollection
This extends the existing auto-conformance mechanism to synthesize the conformances to `CxxConvertibleToCollection` protocol for C++ sequence types.

This means that the developer can now call `Array(myCxxSequence)` or `Set(myCxxSequence)` without adding any extensions manually.
2022-11-29 14:28:17 +00:00
Arnold Schwaighofer
082349ebc7 Revert "Merge pull request #62203 from apple/revert-61984-colocate_instantiation"
This reverts commit 1f3e159cfe, reversing
changes made to 103b4a89c2.

Re-applies "IRGen: Co-locate metadata instatiation/completions/accessor
functions in a special section" for MachO only. The original change broke lldb
on aarch64 linux.

rdar://102481054
2022-11-28 06:27:12 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Mishal Shah
a0a8881012 Revert "IRGen: Co-locate metadata instatiation/completions/accessor functions in a special section" 2022-11-18 21:04:21 -08:00
Arnold Schwaighofer
6a853fa91b IRGen: Co-locate metadata instatiation/completions/accessor functions in a special section
For spatial locality on startup.

rdar://101593202
2022-11-14 06:18:00 -08:00
Egor Zhdan
3cf84214b8 [cxx-interop] Synthesize conformances to CxxRandomAccessCollection
This makes ClangImporter automatically conform C++ collection types to `Cxx.CxxRandomAccessCollection` protocol.

We consider a C++ sequence type to be a random access collection type its iterator conforms to `UnsafeCxxRandomAccessIterator`.
2022-11-03 12:01:18 -07:00
Richard Wei
4ce1ebb120 [Macros] Support user-defined macros as compiler plugins (#61734)
Allow user-defined macros to be loaded from dynamic libraries and evaluated.

- Introduce a _CompilerPluginSupport module installed into the toolchain. Its `_CompilerPlugin` protocol acts as a stable interface between the compiler and user-defined macros.
- Introduce a `-load-plugin-library <path>` attribute which allows users to specify dynamic libraries to be loaded into the compiler.

A macro library must declare a public top-level computed property `public var allMacros: [Any.Type]` and be compiled to a dynamic library. The compiler will call the getter of this property to obtain and register all macros.

Known issues:
- We current do not have a way to strip out unnecessary symbols from the plugin dylib, i.e. produce a plugin library that does not contain SwiftSyntax symbols that will collide with the compiler itself.
- `MacroExpansionExpr`'s type is hard-coded as `(Int, String)`. It should instead be specified by the macro via protocol requirements such as `signature` and `genericSignature`. We need more protocol requirements in `_CompilerPlugin` to handle this.
- `dlopen` is not secure and is only for prototyping use here.

Friend PR: apple/swift-syntax#1022
2022-10-31 14:03:25 -07:00
Egor Zhdan
ea781bf657 Merge pull request #61700 from apple/egorzhdan/cxx-conform-raciter
[cxx-interop] Synthesize conformances to `UnsafeCxxInputIterator`
2022-10-26 18:34:32 +01:00
Egor Zhdan
0efd20d016 [cxx-interop] Synthesize conformances to UnsafeCxxInputIterator
This makes ClangImporter automatically conform C++ sequence types to `Cxx.UnsafeCxxInputIterator` protocol.

We consider a C++ type to be a random access iterator type if conforms to `UnsafeCxxInputIterator`, and additionally defines `operator-` and `operator+=`.
2022-10-24 17:18:42 +01:00