Commit Graph

473 Commits

Author SHA1 Message Date
Emil Pedersen
0faa055c11 [DebugInfo] Add debug info support for recursive SIL SROA
This removes the restriction that only one fragment is allowed at
the end of a SIL DIExpression.

rdar://100046900
2024-02-28 12:48:17 -08:00
Michael Gottesman
ac08efdab4 [sil] Add a SILVerifier check that all SILUndef's parent function back pointer contains the SILFunction that maps to it. 2024-02-27 13:14:48 -08:00
Erik Eckstein
3488cd364f SIL: remove instruction leaks checking
It's not thread safe and can cause false alarms in case multiple modules exist in different threads. E.g. when building swiftmodules from interfaces.
The leaking check is not important anymore because the builder APIs enforce that instructions are not leaking.
I.e. it's not possible to create an instruction without inserting it into a basic block. Also, it's not possible to remove an instruction from a block without deleting it.

rdar://122169263
2024-02-22 14:09:11 +01: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
John McCall
868fc6ad46 Basic SILGen for @isolated(any).
The main piece that's still missing here is support for closures;
they actually mostly work, but they infer the wrong isolation for
actor-isolated closures (it's not expressed in the type, so obviously
they're non-isolated), so it's not really functional.  We also have
a significant problem where reabstraction thunks collide incorrectly
because we don't mangle (or represent!) formal isolation into
SILFunctionType; that's another follow-up.  Otherwise, I think SILGen
is working.
2024-02-19 21:21:03 -05:00
John McCall
d5142668f4 SIL and IRGen support for @isolated(any). SILGen to come. 2024-02-13 03:04:13 -05:00
Nate Cook
e317febc9d Revert "Allow normal function results of @yield_once coroutines (#69843)"
This reverts commit aa5b505014.
2024-02-07 14:57:31 -06:00
Anton Korobeynikov
aa5b505014 Allow normal function results of @yield_once coroutines (#69843)
This adds SIL-level support and LLVM codegen for normal results of a coroutine.

The main user of this will be autodiff as VJP of a coroutine must be a coroutine itself (in order to produce the yielded result) and return a pullback closure as a normal result.

For now only direct results are supported, but this seems to be enough for autodiff purposes.
2024-02-06 22:13:15 -08:00
Ellie Shin
72a7760027 Support package SIL linkage.
Decls with a package access level are currently set to public SIL
linkages. This limits the ability to have more fine-grained control
and optimize around resilience and serialization.
This PR introduces a separate SIL linkage and FormalLinkage for
package decls, pipes them down to IRGen, and updates linkage checks
at call sites to include package linkage.

Resolves rdar://121409846
2024-02-06 01:23:14 -08:00
Michael Gottesman
94acfd2873 [region-isolation] Enable address walker verification on AllocPackInst.
This provides a guarantee that the address walker will not miss walking any
addresses due to the walker not being updated.
2024-02-02 14:50:10 -08:00
Michael Gottesman
f531f705cf [sil] Dump the unknown users when the address walker fails to visit an instructions.
Just making this easier to debug. Previously, one would have to go into the
debugger to figure out what the issue is. Now upon failure, one just gets the
exact instruction that needs to be added.
2024-02-02 14:50:10 -08:00
Kavon Farvardin
107475b224 SILType: Avoid asking ASTType if it is Noncopyable
It's better to ask SILType if it is MoveOnly than go to the AST type and
 ask if it is noncopyable, because some types in SIL do not have a
 well-defined notion of conformance in the AST.
2024-01-23 22:42:38 -08:00
Ellie Shin
ab3b5add3a Currently decls defined with package access level have a different
access level for optimization: `public`. It requires an extra check for
the actual access level that was declared when determining serialization
since the behavior should be different.

This PR sets its effective access level to `package` as originally defined,
updates call sites to make appropriate acces level comparisons, and removes
`package` specific checks.
2024-01-23 11:02:49 -08:00
Andrew Trick
246da83f0f Merge pull request #71055 from atrick/lifetime-dependence
Lifetime dependence utilities
2024-01-23 08:19:24 -08:00
Andrew Trick
37171e698a Handle mark_dependence [nonescaping] like a borrowing instruction. 2024-01-22 23:57:03 -08:00
Joe Groff
960938f87e SILGen: Always match noncopyable values by borrowing.
Even if the final pattern ends up consuming the value, the match itself
must be nondestructive, because any match condition could fail and cause
us to have to go back to the original aggregate. For copyable values,
we can always copy our way out of consuming operations, but we don't
have that luxury for noncopyable types, so the entire match operation
has to be done as a borrow.

For address-only enums, this requires codifying part of our tag layout
algorithm in SIL, namely that an address-only enum will never use
spare bits or other overlapping storage for the enum tag. This allows
us to assume that `unchecked_take_enum_data_addr` is safely non-side-
effecting and match an address-only noncopyable enum as a borrow.
I put TODOs to remove defensive copies from various parts of our
copyable enum codegen, as well as to have the instruction report
its memory behavior as `None` when the projection is nondestructive,
but this disturbs SILGen for existing code in ways SIL passes aren't
yet ready for, so I'll leave those as is for now.

This patch is enough to get simple examples of noncopyable enum switches
to SILGen correctly. Additional work is necessary to stage in the binding
step of the pattern match; for a consuming switch, we'll need to end
the borrow(s) and then reproject the matched components so we can
consume them moving them into the owned bindings. The move-only checker
also needs to be updated because it currently always tries to convert
a switch into a consuming operation.
2024-01-19 13:59:04 -08:00
Erik Eckstein
bc99986cf9 SIL: add a dependency token operand to global_addr
Optionally, the dependency to the initialization of the global can be specified with a dependency token `depends_on <token>`.
This is usually a `builtin "once"` which calls the initializer for the global variable.
2024-01-10 09:33:58 +01:00
nate-chandler
7787da12a2 Merge pull request #70738 from nate-chandler/opaque-values/20240105/1/init-tuple-typed
[OpaqueValues] Assign tuple into tuple-typed memory.
2024-01-08 07:06:42 -08:00
Nate Chandler
5db0339f5d [SIL] tuple_addr_constructor reqs lowered addrs.
The instruction exists to express the atomic initialization of a
tuple-typed address in address-lowered mode.  In opaque-values mode,
this is expressed as an assign/store.
2024-01-05 13:14:48 -08:00
Nate Chandler
73d68a3f28 [IRGen] Pack metadata may be alloc'd for layouts.
Previously, mayRequirePackMetadata only considered whether a type
involved a pack.  That failed to account for the case of outlined value
functions that require pack metadata when the type involves a pack in
its layout.  Here, mayRequirePackMetadata now considers also whether the
layout corresponding to a type involves a pack.

rdar://119829826
2024-01-04 12:21:47 -08:00
Michael Gottesman
58a6e2aa4d [region-isolation] Add Asserting support for {alloc,dealloc}_pack_metadata since they only appear in Lowered SIL.
I also added a note to the SIL.rst and an assert into the SILVerifier to better
document this requirement.
2023-12-18 12:35:40 -08:00
Alejandro Alonso
f365316d74 Merge pull request #70327 from Azoy/inject-get-enum-tag-builtins
[IRGen] Add getEnumTag and injectEnumTag builtins
2023-12-12 16:07:56 -08:00
Michael Gottesman
486b7ceb94 Merge pull request #70394 from gottesmm/pr-f328e7893b344f5252d43a4358baf49fb4589abe
[region-isolation] Add the ability to write SIL tests for actor isolation
2023-12-12 15:47:12 -06:00
Konrad `ktoso` Malawski
828f589be4 Initial Task Executor implementation Task(on:), addTask(on:) etc. (#68793)
Co-authored-by: John McCall <rjmccall@gmail.com>
2023-12-12 17:14:24 +09:00
Michael Gottesman
f328e7893b [region-isolation] Add support for representing ApplyIsolationCrossing at the SIL level on apply, begin_apply, try_apply.
Some notes:

This is not emitted by SILGen. This is just intended to be used so I can write
SIL test cases for transfer non sendable. I did this by adding an
ActorIsolationCrossing field to all FullApplySites rather than adding it into
the type system on a callee. The reason that this makes sense from a modeling
perspective is that an actor isolation crossing is a caller concept since it is
describing a difference in between the caller's and callee's isolation. As a
bonus it makes this a less viral change.

For simplicity, I made it so that the isolation is represented as an optional
modifier on the instructions:

  apply [callee_isolation=XXXX] [caller_isolation=XXXX]

where XXXX is a printed representation of the actor isolation.

When neither callee or caller isolation is specified then the
ApplyIsolationCrossing is std::nullopt. If only one is specified, we make the
other one ActorIsolation::Unspecified.

This required me to move ActorIsolationCrossing from AST/Expr.h ->
AST/ActorIsolation.h to work around compilation issues... Arguably that is where
it should exist anyways so it made sense.

rdar://118521597
2023-12-11 19:27:27 -06:00
Alejandro Alonso
49b0a23561 Add getEnumTag and injectEnumTag builtins 2023-12-11 10:58:29 -08:00
Erik Eckstein
e652f2c92e SIL: add the alloc_vector and vector instructions
* `alloc_vector`: allocates an uninitialized vector of elements on the stack or in a statically initialized global
* `vector`: creates an initialized vector in a statically initialized global
2023-12-09 18:49:55 +01:00
Erik Eckstein
0897d8a720 MemoryLifetimeVerifier: use CalleeCache instead of AliasAnalysis
To verify if a function may read from an indirect argument, don't use AliasAnalysis.
Instead use the CalleeCache to get the list of callees of an apply instruction.
Then use a simple call-back into the swift Function to check if a callee has any relevant memory effect set.

This avoids a dependency from SIL to the Optimizer.
It fixes a linker error when building some unit tests in debug.
2023-12-01 19:20:18 +01:00
Pavel Yaskevich
229e580174 [SIL] Add a way to retrieve key path type from KeyPathInst
Since the type of the instruction could be existential it's
better to keep retrival logic in one place.
2023-11-28 13:02:17 -08:00
Allan Shortlidge
f208e4ab7a SILVerifier: Fix crashes when verifying type dependent arguments.
`getRootLocalArchetypeDef()` may return a `PlaceholderValue` which makes the
`cast<SingleValueInstruction>` invalid. Use `dyn_cast` and then update the
callers of `getRootLocalArchetypeDefInst()` to handle `nullptr`.
2023-11-17 13:57:05 -08:00
Doug Gregor
0042201f1f Merge pull request #69700 from DougGregor/typed-throws-reabstraction-thunks
[Typed throws] Implement reabstraction thunks that change the error
2023-11-08 21:01:49 -08:00
Michael Gottesman
48b4ca0b24 Merge pull request #69686 from gottesmm/rdar117880194
[region-isolation] When assigning RValues into memory, use tuple_addr_constructor instead of doing it in pieces.
2023-11-07 20:15:58 -08:00
Michael Gottesman
b1f69030fc [region-isolation] When assigning RValues into memory, use tuple_addr_constructor instead of doing it in pieces.
I also included changes to the rest of the SIL optimizer pipeline to ensure that
the part of the optimizer pipeline before we lower tuple_addr_constructor (which
is right after we run TransferNonSendable) work as before.

The reason why I am doing this is that this ensures that diagnostic passes can
tell the difference in between:

```
x = (a, b, c)
```

and

```
x.0 = a
x.1 = b
x.2 = c
```

This is important for things like TransferNonSendable where assigning over the
entire tuple element is treated differently from if one were to initialize it in
pieces using projections.

rdar://117880194
2023-11-07 15:38:33 -08:00
Doug Gregor
0ba605a4b9 [Typed throws] Implement reabstraction thunks that change the error
Introduce SILGen support for reabstractions thunks that change the
error, between indirect and direct errors as well as conversions
amongst error types (e.g., from concrete to `any Error`).
2023-11-07 11:39:56 -08:00
Erik Eckstein
d6e86a8cd4 Fix specialization of class method calls in embedded swift.
Unlike in regular swift, The class_method instruction references the specialized version of a class method.
This must be handled in ReabstractionInfo: it needs to work without a concrete callee SIL function.

Also, the SILVerifier must handle the case that a class_method instruction references a specialized method.
2023-11-06 21:08:22 +01:00
Doug Gregor
53c8e84a1f [Typed throws] Handle throw_addr in the same places as throw. 2023-11-03 19:02:58 -07:00
Slava Pestov
05ccd9734c SIL: Introduce ThrowAddrInst 2023-10-31 16:58:54 -04:00
Arnold Schwaighofer
9482b0c86b Preliminary SIL and IRGen support for error_indirect
IRGen lowering of non-fixed-sized typed errors and the SIL support necessary to
spell out IRGen test cases.
2023-10-12 18:09:52 -07:00
Andrew Trick
69a884565a Fix ownership of select_enum instruction
This instruction was given forwarding ownership in the original OSSA
implementation. That will obviously lead to memory leaks. Remove
ownership from this instruction and verify that it is never used for
non-trivial types.
2023-10-08 01:34:48 -07:00
Kuba Mracek
8f335b1a28 [embedded] Allow serialized internal/private functions in embedded mode 2023-09-23 14:00:31 -07:00
Kavon Farvardin
a69bcf8a61 Merge pull request #67930 from kavon/copyable-requirement
Copyable as a Requirement Against the Machine
2023-09-21 11:49:23 -07:00
swift-ci
71cb84b24a Merge pull request #68657 from nate-chandler/nfc/20230920/1/add-null-check
[SILVerifier] Add null check for dead end blocks.
2023-09-20 17:18:49 -07:00
Kavon Farvardin
f1142d5da4 [nfc] rename or eliminate isPureMoveOnly APIs
I think from SIL's perspective, it should only worry about whether the
type is move-only. That includes MoveOnlyWrapped SILTypes and regular
types that cannot be copied.

Most of the code querying `SILType::isPureMoveOnly` is in SILGen, where
it's very likely that the original AST type is sitting around already.
In such cases, I think it's fine to ask the AST type if it is
noncopyable. The clarity of only asking the ASTType if it's noncopyable
is beneficial, I think.
2023-09-20 15:23:17 -07:00
Nate Chandler
1e5a7aab9a [SILVerifier] Added null check.
The deadEndBlocks field isn't always present so first check that it's
defined.
2023-09-20 13:38:06 -07:00
Yuta Saito
c5314bd3af Centralize KeyPath accessor calling convention logic to IRGen
KeyPath's getter/setter/hash/equals functions have their own calling
convention, which receives generic arguments and embedded indices from a
given KeyPath argument buffer.
The convention was previously implemented by:
1. Accepting an argument buffer as an UnsafeRawPointer and casting it to
   indices tuple pointer in SIL.
2. Bind generic arguments info from the given argument buffer while emitting
   prologue in IRGen by creating a new forwarding thunk.

This 2-phase lowering approach was not ideal, as it blocked KeyPath
projection optimization [^1], and also required having a target arch
specific signature lowering logic in SIL-level [^2].

This patch centralizes the KeyPath accessor calling convention logic to
IRGen, by introducing `@convention(keypath_accessor_XXX)` convention in
SIL and lowering it in IRGen. This change unblocks the KeyPath projection
optimization while capturing subscript indices, and also makes it easier
to support WebAssembly target.

[^1]: https://github.com/apple/swift/pull/28799
[^2]: https://forums.swift.org/t/wasm-support/16087/21
2023-09-20 11:25:39 -07:00
Erik Eckstein
f0b811c45f SIL: add the end_init_let_ref instruction
This instructions marks the point where all let-fields of a class are initialized.
This is important to ensure the correctness of ``ref_element_addr [immutable]`` for let-fields,
because in the initializer of a class, its let-fields are not immutable, yet.
2023-09-19 15:10:30 +02:00
Erik Eckstein
e5eb15dcbe Swift SIL: replace the set_deallocating instruction with begin_dealloc_ref
Codegen is the same, but `begin_dealloc_ref` consumes the operand and produces a new SSA value.
This cleanly splits the liferange to the region before and within the destructor of a class.
2023-09-19 15:10:30 +02:00
Kuba Mracek
d2bb064548 [embedded] Don't SILVerify VTable ABIs in embedded mode, as the specialized vtables have intentionally different ABIs from the non-generic one 2023-09-12 13:58:11 -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
Michael Gottesman
ba06693745 Merge pull request #68228 from gottesmm/pr-088dba5d9c0854c0e00da0603eb99b770d99709c
[move-only] Rename mark_must_check -> mark_unresolved_non_copyable_value
2023-08-31 09:50:55 -07:00