Commit Graph

335 Commits

Author SHA1 Message Date
Michael Gottesman
1dd896ded9 [move-only] Implement escaping closure semantics.
NOTE: A few of the test patterns need to be made better, but this patch series
is large enough, I want to get it into tree and iterate.
2023-02-20 11:04:21 -08:00
Michael Gottesman
50af8fd493 [move-only] Box owned arguments like let parameters. 2023-02-20 11:04:21 -08:00
Michael Gottesman
bd8bad3d39 [gardening] Delete unused variable. 2023-02-19 14:56:07 -08:00
Michael Gottesman
852911c668 [silgen] Add support for emitting ImmutableBoxes. 2023-02-17 16:04:47 -08:00
Michael Gottesman
85ea8b5d5b [move-only] Rename CheckKind::NoImplicitCopy -> CheckKind::ConsumableAndAssignable
This fits the name of the check better. The reason I am doing this renaming is
b/c I am going to add a nonconsumable but assignable check for
global_addr/ref_element_addr/captures with var semantics.
2023-02-10 13:46:19 -08:00
Michael Gottesman
87829aaa32 [move-only] Rename CheckKind::NoCopy -> CheckKind::NoConsumeOrAssign.
This reflects better the true meaning of this check which is that a value marked
with this check cannot be consumed on its boundary at all (when performing
let/var checking) and cannot be assigned over when performing var checking.
2023-02-10 13:46:19 -08:00
John McCall
d25a8aec8b Add explicit lowering for value packs and pack expansions.
- SILPackType carries whether the elements are stored directly
  in the pack, which we're not currently using in the lowering,
  but it's probably something we'll want in the final ABI.
  Having this also makes it clear that we're doing the right
  thing with substitution and element lowering.  I also toyed
  with making this a scalar type, which made it necessary in
  various places, although eventually I pulled back to the
  design where we always use packs as addresses.

- Pack boundaries are a core ABI concept, so the lowering has
  to wrap parameter pack expansions up as packs.  There are huge
  unimplemented holes here where the abstraction pattern will
  need to tell us how many elements to gather into the pack,
  but a naive approach is good enough to get things off the
  ground.

- Pack conventions are related to the existing parameter and
  result conventions, but they're different on enough grounds
  that they deserve to be separated.
2023-01-29 03:29:06 -05:00
Nate Chandler
69d1abe0c5 [SIL] Protect owned arg lifetimes at inlining.
Previously, `begin_borrow [lexical]` were created during SILGen for
@owned arguments.  Such borrows could be deleted if trivially dead,
which was the original reason why @owned arguments were considered
lexical and could not have their destroys hoisted.

Those borrows were however important during inlining because they would
maintain the lifetime of the owned argument.  Unless of course the
borrow scope was trivially dead.  In which case the owned argument's
lifetime would not be maintained.  And if the caller's value was
non-lexical, destroys of the value could be hoisted over deinit
barriers.

Here, during inlining, `move_value [lexical]`s are introduced during
inlining whever the caller's value is non-lexical.  This maintains the
lifetime of the owned argument even after inlining.
2023-01-25 11:36:33 -08:00
Doug Gregor
0e8d4c470e [SILGen] Fix formal type of global actor metatype.
Fixes a crash reported in rdar://103255322 .
2022-12-16 12:33:09 -08:00
Michael Gottesman
4901529a72 [move-only-addr] Teach move only address checker that captured vars should be treated like inout arguments.
rdar://103313357
2022-12-15 09:59:36 -08:00
Michael Gottesman
9e44011e4d [sil] Add a new attribute called @closureCaptured to SILFunctionArguments that are closure capture arguments.
I am adding this to make it easy to determine if a SILFunction that is not inout
aliasable is captured. This is useful when emitting certain types of
diagnostics like I need to emit with move only.
2022-12-14 15:16:43 -08:00
Michael Gottesman
0735f9f0bb [move-only] Teach SILGen that lets captured by a closure must be no copy.
The current diagnostic that we are emitting is not perfect, but at least this
prevents us from saying that an error is not occuring here. I am going to file a
bug to track the QoI work of improving the diagnostic.

rdar://103313305
2022-12-14 10:57:34 -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
Michael Gottesman
6f7961b63c [move-only] Mark self of move only struct/enum types in deinits as being move-only.
This ensures that if we try to escape self or assign it to a variable, we
error since at the end of the deinit we always consume self and clean up its
variables.

I did not handle unique classes since it would have required a bit more surgery
around how deinits are handled and we do not need unique classes for our MVP.

rdar://102339259
2022-11-14 14:47:37 -08:00
nate-chandler
6075af2652 Merge pull request #61048 from nate-chandler/no_eager_move_attr
Renamed _noEagerMove attribute.
2022-09-12 13:14:32 -07:00
Nate Chandler
5642a46378 Renamed _noEagerMove attribute.
Avoid introducing extra terminology via an underscored attribute.

rdar://99723104
2022-09-12 08:21:18 -07:00
Michael Gottesman
0a16cc362a [move-only] Implement an initial version of the move only address checker.
Some notes:

1. I added support for both loadable/address only types.

2. These tests are based off of porting the move only object tests for inout,
vars, mutating self, etc.

3. I did not include already written tests for address only types in this
specific merge since I need to change us to borrow move only var like types.
Without that, we get a lot of spurious error msgs and the burden of writing that
is not worth it. So instead in a forthcoming commit where I fix that issue in
SILGen, I will commit the corresponding address only tests for this work.

4. I did not include support for trivial types in this. I am going to do
object/address for that at the same time.
2022-09-11 18:57:32 -07:00
Alex Hoppen
f366263134 Merge pull request #59943 from ahoppen/pr/actorisolation-with-gettype
[Sema] Add custom functions to ActorIsolationChecker to determine expr type and closure actor isolation
2022-09-07 21:49:28 +02:00
nate-chandler
2efc0f6857 Merge pull request #60940 from nate-chandler/eager_move_self
Self parameters can be @_noImplicitCopy or @_eagerMove.
2022-09-07 06:12:53 -07:00
Alex Hoppen
442cf9bd4e [Sema] Add custom functions to ActorIsolationChecker to determine expr type and closure actor isolation
When we get rid of `LeaveClosureBodiesUnchecked` we no longer save closure types to the AST and thus also don’t save their actor isolation to the AST. Hence, we need to extract types and actor isolations of parent closures from the constraint system solution instead of the AST. This prepares `ActorIsolationChecker` to take custom functions to determine the type of an expression or the actor isolation of a closure.
2022-09-07 11:12:23 +02:00
Nate Chandler
cacac0bd83 [SILGen] Allow annotating self @_noImplicitCopy.
Allow @_noImplicitCopy to be applied to methods.  Applying it there
means that the self argument will have the attribute.
2022-09-02 17:32:48 -07:00
Nate Chandler
9dbdfa8e3f [SILGen] Allow annotating self @_eagerMove.
Allow @_eagerMove (and @_lexical) to be applied to methods.  Applying
it there means that the self argument will have the attribute.
2022-09-02 16:13:08 -07:00
Kavon Farvardin
850797d588 [SILGen] correct missing canonicalization of global actor
resolves rdar://96309577 / https://github.com/apple/swift/issues/59705
2022-08-30 16:15:09 -07:00
Doug Gregor
141ffc2b8d Model isolated parameters in ActorIsolation.
A function can be actor instance-isolated to one of its parameters.
Make sure that this is reflected in ActorIsolation, so such a function
doesn't get a different actor isolation.
2022-08-23 23:19:46 -07:00
Nate Chandler
3c78a0bb90 [SILGen] Only lexical types get lexical lifetimes.
Only emit `begin_borrow [lexical]` and only mark `alloc_stack`s
`[lexical]` when the variable in question's lifetime is lexical, not
eager move.
2022-08-22 15:28:00 -07:00
Nate Chandler
7ea2d5c54d [Gardening] Tweaked argument label. 2022-08-21 21:44:31 -07:00
Nate Chandler
0659e16846 [SILGen] Use decl for noImplicitCopy attr.
Rather than using the SILValue of the ManagedValue generated for that
argument, which is not always a SILFunctionArgument, just look directly
at the ParmDecl for the attribute.
2022-08-21 21:44:30 -07:00
Doug Gregor
20be955eed Open existential actors when retrieving their executors.
Fixes rdar://91448324.
2022-08-19 15:08:51 -07:00
Slava Pestov
9d96ed940f AST: Rename 'canonical wrt. generic signature' to 'reduced'
We had two notions of canonical types, one is the structural property
where it doesn't contain sugared types, the other one where it does
not contain reducible type parameters with respect to a generic
signature.

Rename the second one to a 'reduced type'.
2022-08-09 12:46:31 -04:00
Joe Groff
0c48517a65 Merge pull request #60167 from maxovtsin/fix-crash-uninhabited-param-closure-executor
[SILGen] Emit unreachable in emitProlog() for uninhabited arguments after other codegen
2022-07-29 09:25:42 -07:00
Michael Gottesman
c2e13f45d5 [silgen] Refactor out a helper methdo from ArgumentInitHelper::makeArgumentIntoBinding.
The name of the refactored method is
ArgumentInitHelper::updateArgumentValueForBinding(). makeArgumentIntoBinding was
becoming too large and was mostly becoming conserned with the logic in the
helper method when that isn't really what it was supposed to be doing.

Beyond that, this refactoring also lets me simplify the logic in the actual
implementation logic by using early-exits.
2022-07-22 12:25:02 -07:00
Michael Gottesman
65c21b61bf [move-only] Begin implementing support for concrete move only types.
This is just the very beginning... I still need to implement more parts of
SILGen for this. But all great things start small. I am going to iterate on top
of this and just wanted to get some initial parts of the work in as I go.
2022-07-21 15:33:17 -07:00
Max Ovtsin
b8b8a20dfe [SILGen] Emit unreachable in emitProlog() for uninhabited arguments after other codegen 2022-07-21 11:01:01 +01:00
Michael Gottesman
f1182a73da [no-implicit-copy] Remove auto +1 param signature change called by noimplicit copy in favor of following normal convention.
I also added a bunch of tests for both the trivial/non-trivial case as well as
some docs to SIL.rst.
2022-07-19 16:39:03 -07:00
Michael Gottesman
c8c2b810ff [gardening] Make a piece of code a little more readable.
Basically the if/else statement of this section of code are unrelated by have
the same condition (albeit inverted). Rather than keep them together and make
them seem as related semantic checks, move the no implicit copy diagnostic into
its own section.

I am doing this as a separate commit partially to slim down the next series of
commits.
2022-07-11 12:28:46 -07:00
Michael Gottesman
6a24087f90 Merge pull request #59611 from gottesmm/moveonly-rebase
[no-implicit-copy] Update SILGen/move checker to work with new patterns from copyable_to_moveonly and friends.
2022-06-28 13:45:15 -07:00
Adrian Prantl
7c1c7b6249 Emit debug locations for hop_to_executor instructions.
The function prologue of async funclets inherits its source location
from the hop_to_executor instruction. This makes it easier to produce
logical backtraces, since the PC in logical frames will always point
to the start if the function.

rdar://89776340
2022-06-23 09:40:25 -07:00
Michael Gottesman
0d11e8ef69 [no-implicit-copy] Update SILGen/move checker to work with new patterns from copyable_to_moveonly and friends.
This involved doing the following:

1. Update the move only checker to look for new patterns.

2. Teach emitSemanticStore to use a moveonlywrapper_to_copyable to store moveonly
   values into memory. The various checkers will validate that this code is
   correct.

3. When emitting an apply, always unwrap move only variables. In the
   future, I am going to avoid this if a parameter is explicitly marked as also
   being moveonly (e.x.: @moveOnly parameter or @noEscape argument).

4. Convert from moveOnly -> copyable on return inst automatically in SILGen.

5. Fix SILGenLValue emission so we emit an error diagnostic later rather than
   crash. This is needed to keep SILGen emitting move only addresses (that is no
   implicit copy address only lets) in a form that the move only checker then
   will error upon. Without this change, SILGen crashes instead of emitting an
   error diagnostic in the following test:
   .//test/SILOptimizer/move_only_checker_addressonly_fail.swift
2022-06-21 16:47:58 -07:00
Meghana Gupta
f65dac9824 Use addresses to represent inout/inout_aliasable when using opaque values 2022-04-06 22:26:09 -07:00
Nate Chandler
a532c53cc5 [SIL-opaque] Don't override arg value category.
Previously, when emitting block arguments, the value category of the
SILType was overridden to be address for indirect arguments.  With
opaque types, that distinction is made later during AddressLowering.  So
only do that when opaque types are disabled.
2022-03-23 12:36:14 -07:00
Andrew Trick
caaad424d8 [SIL-opaque] Various SILGen fixes 2022-03-22 17:04:13 -07:00
Doug Gregor
1466afcf44 Ensure that emit unnamed isolated parameters
... because we need to be able to hop to their actors.
Fixes rdar://86753732.
2022-03-10 09:45:03 -08:00
John McCall
b3b6701a50 Implement @_unsafeInheritExecutor.
SE-0338 changed the execution of non-actor async functions
so that they always hop to the generic executor, but some
functions need a way to suppress this so that they inherit
the caller's executor.

The right way to implement this is to have the caller pass
down the target executor in some reliable way and then
switch to it in all the appropriate places in the caller.
We might reasonably be able to build this on top of isolated
parameters, using some sort of default argument, or we might
need a wholly novel mechanism.

But those things are all ABI-breaking absent some sort of
guarantee about switching that we probably don't want to make,
and unfortunately we have functions in the library which we
need to export that need to inherit executors.  So in the
short term, we need some unsafe way of getting back to the
previous behavior.
2022-02-14 20:46:03 -05:00
Doug Gregor
a37f291c18 Remove ActorIsolation::DistributedActorInstance.
The distributed case is distinguishable from the non-distributed case
based on the actor type itself for those rare cases where we care. The
vast majority of code is simplified by treating this identically to
`ActorInstance`.
2022-02-09 11:16:23 -08:00
Michael Gottesman
5ced1d17ef Merge pull request #41085 from gottesmm/pr-f777a3678aa7c0fcd9369e7ad04c5d94a067024a
[no-implicit-copy] Add a new instruction called MarkMustCheckInst and use it in the move checker.
2022-01-29 19:23:51 -08:00
Michael Gottesman
68d37e142b [no-implicit-copy] Add a new instruction called MarkMustCheckInst and use it in the move checker.
This is an instruction that I am going to use to drive some of the ownership
based dataflow optimizations that I am writing now. The instruction contains a
kind that allows one to know what type of checking is required and allows the
need to add a bunch of independent instructions for independent checkers. Each
checker is responsible for removing all of its own mark instructions. NOTE:
MarkMustCheckInst is only allowed in Raw SIL since once we are in Canonical SIL
we want to ensure that all such checking has already occurred.
2022-01-29 14:49:39 -08:00
John McCall
67ec6995fb Look through defer bodies in actor isolation checking.
Fixes rdar://88200899
2022-01-28 16:26:35 -05:00
John McCall
716f4b9e2f Hop to the generic executor in non-actor-isolated async functions.
Async functions are now expected to set ExpectedExecutor in their
prologue (and, generally, immediately hop to it).  I updated the
prologue code for a bunch of function emission, most of which was
uninteresting.  Top-level code was not returning to the main
executor, which is now fixed; fortunately, we weren't assuming
that we were on the main executor yet.

We had some code that only kicked in when an ExpectedExecutor
wasn't set which made us capture the current executor before
a hop and then return to it later.  This code has been removed;
there's no situation in which save-and-return is the semantically
correct thing to do given the possibility of hop optimization.
I suspect it could also have led to crashes if the current
executor is being kept alive only because it's currently running
code.  If we ever add async functions that are supposed to inherit
their caller's executor, we should have the caller pass the right
executor down to it.

This is the first half of SE-0338; the second, sendability
enforcement, is much more complicated, and Doug has volunteered
to do it.

Fixes rdar://79284465, as well as some tests that were XFAILed
on Windows.
2022-01-27 01:53:19 -05:00
Doug Gregor
13de9d6a5f Properly compute actor isolation of local functions with captures.
Non-Sendable local functions can be actor-isolated if they capture an
isolated parameter. Treat them as such. Fixes rdar://83090397.
2021-12-10 15:10:43 -08:00
Michael Gottesman
5d5b0daf9c [silgen] Add the lexical flag on more alloc_stack that are used to represent variables.
And update all of the tests.
2021-12-03 14:33:00 -08:00