Commit Graph

2441 Commits

Author SHA1 Message Date
Joe Groff
6db85203db SIL: Abstraction pattern support for multiple foreign async returns.
An ObjC API maybe imported as async that had multiple non-error arguments to
its completion handler, which we treat in Swift as returning a tuple. Use a new
form of abstraction pattern to represent this return type, to maintain the
correct relation between individual tuple elements and the Clang block parameter
types they map to.
2020-11-12 16:53:21 -08:00
swift_jenkins
2de239cc2f Merge remote-tracking branch 'origin/main' into next 2020-11-11 22:10:14 -08:00
Michael Gottesman
7d05f374f1 Merge pull request #34693 from gottesmm/pr-1fdd1272490dcfe27f62410aee521669e0059430
[ownership] Try harder to make sure we do not propagate ownership info when ownership is disabled.
2020-11-11 21:31:19 -08:00
swift_jenkins
3c28fcaa81 Merge remote-tracking branch 'origin/main' into next 2020-11-11 19:00:10 -08:00
Michael Gottesman
58d4191470 [ownership] Try harder to make sure we do not propagate ownership info when ownership is disabled.
Specifically, I made it so that assuming our instruction is inserted into a
block already that we:

1. Return a constraint of {OwnershipKind::Any, UseLifetimeConstraint::NonLifetimeEnding}.
2. Return OwnershipKind::None for all values.

Noticed above I said that if the instruction is already inserted into a block
then we do this. The reason why is that if this is called before an instruction
is inserted into a block, we can't get access to the SILFunction that has the
information on whether or not we are in OSSA form. The only time this can happen
is if one is using these APIs from within SILBuilder since SILBuilder is the
only place where we allow this to happen. In SILBuilder, we already know whether
or not our function is in ossa or not and already does different things as
appropriate (namely in non-ossa does not call getOwnershipKind()). So we know
that if these APIs are called in such a situation, we will only be calling it if
we are in OSSA already. Given that, we just assume we are in OSSA if we do not
have a function.

To make sure that no mistakes are made as a result of that assumption, I put in
a verifier check that all values when ownership is disabled return a
OwnershipKind::None from getOwnershipKind().

The main upside to this is this means that we can write code for both
OSSA/non-OSSA and write code for non-None ownership without needing to check if
ownership is enabled.
2020-11-11 18:56:59 -08:00
Michael Gottesman
c3681a6eef [membehavior] Fix base memory behavior/releasing of Load/Store for ownership.
load, store in ossa can have side-effects and stores can release. Specifically:

Memory Behavior
---------------

* Load: unqualified, trivial, take have a read side-effect, but copy retains so
  has side-effects.

* Store: unqualified, trivial, init may write but assign releases so it may have
  side-effects.

Release Behavior
----------------

* Load: No changes.

* Store: May release if store has assign as an ownership qualifier.
2020-11-11 15:32:32 -08:00
swift_jenkins
c7ffe688e2 Merge remote-tracking branch 'origin/main' into next 2020-11-11 13:28:17 -08:00
Michael Gottesman
43a89e0c65 [ownership] Some small fixes from Andy's review.
I merged the original PR since it was rather large. These are the remaining
small fixes.
2020-11-10 22:44:13 -08:00
swift_jenkins
332645899e Merge remote-tracking branch 'origin/main' into next 2020-11-10 22:43:49 -08:00
Michael Gottesman
a294ab61ad [ownership] Eliminate OperandOwnershipKindMap in favor of OwnershipConstraint.
I also used this as a moment to clarify the lattice that related
ValueOwnershipKind and OwnershipConstraint.
2020-11-10 19:07:30 -08:00
swift_jenkins
d5be531a83 Merge remote-tracking branch 'origin/main' into next 2020-11-10 19:07:28 -08:00
Michael Gottesman
c026e95cce [ownership] Extract out SILOwnershipKind from ValueOwnershipKind into its own type and rename Invalid -> Any.
This makes it easier to understand conceptually why a ValueOwnershipKind with
Any ownership is invalid and also allowed me to explicitly document the lattice
that relates ownership constraints/value ownership kinds.
2020-11-10 14:29:11 -08:00
swift_jenkins
cbbc53ed90 Merge remote-tracking branch 'origin/main' into next 2020-11-10 13:44:15 -08:00
Michael Gottesman
f16c4ba203 [ownership] Convert ValueOwnershipKind to have an Invalid state instead of using optional.
At Andy's request. If it creates too much noise in covered switches, we may go
back to the original.
2020-11-10 10:34:44 -08:00
swift_jenkins
7e9cbf4c0d Merge remote-tracking branch 'origin/main' into next 2020-11-09 20:52:43 -08:00
Doug Gregor
6798ed1c9c Merge pull request #34627 from DougGregor/create-async-task-builtin
[Concurrency] Implement a builtin createAsyncTask() to create a new task
2020-11-09 20:40:53 -08:00
swift_jenkins
840745e9f0 Merge remote-tracking branch 'origin/main' into next 2020-11-09 19:02:37 -08:00
Michael Gottesman
a0cb311d60 Merge pull request #34644 from gottesmm/pr-e398af60dfa740b58a743d92dc2fdc617e9c9bf0
[ownership] Change ReturnInst to have its ValueOwnershipKind stored within it rather than always recomputing from the function type.
2020-11-09 18:35:19 -08:00
swift_jenkins
869a80d831 Merge remote-tracking branch 'origin/main' into next 2020-11-09 16:20:59 -08:00
Michael Gottesman
b40aa543ac Merge pull request #34637 from gottesmm/pr-027a025e7a95d70fcc7e98300987826a5948c8c4
[ownership] Move type dependent operand check out of the main visitor to eliminate another returning of an empty map from the visitor.
2020-11-09 15:29:39 -08:00
Michael Gottesman
c7051d2929 [ownership] Change ReturnInst to have its ValueOwnershipKind stored within it rather than always recomputing from the function type.
This allows us to hoist the error case of having a function signature with
conflicting ownership requirements into the creation of the return inst instead
of at the time of computing Operand Constraints.

This is the last part of the Operand Constraint computation that can fail that
once removed will let me use fail to mean any constriant is allowed.
2020-11-09 14:21:04 -08:00
Michael Gottesman
b5c3f713d9 [ownership] Move type dependent operand check out of the main visitor to eliminate another returning of an empty map from the visitor.
I may turn this into an assert, but for now I am preserving the current behavior
albeit moving the bad behavior out of the visitor to the front of the API.
2020-11-09 13:02:37 -08:00
Doug Gregor
d038989fd8 Merge branch 'main' into create-async-task-builtin 2020-11-09 11:26:33 -08:00
Michael Gottesman
c4adc64a75 [ownership] Remove ownership merging from computing Operand constraints for Forwarding Insts.
Another step towards allowing us to use an invalid result to mean an instruction
can accept any value.
2020-11-09 10:44:40 -08:00
swift_jenkins
27e2ffef81 Merge remote-tracking branch 'origin/main' into next 2020-11-09 00:07:47 -08:00
Michael Gottesman
d1b555f59a [ownership] Make destructure_{struct,tuple} true forwarding instructions instead of inferring from results/arguments. 2020-11-08 23:55:10 -08:00
Michael Gottesman
d74dbebf3b [ownership] Make checked_cast_br a true Ownership Forwarding inst instead of always inferring from the SILPhiArguments.
This is an analogous change to the previous change I just made to SwitchEnumInst.
2020-11-08 23:55:10 -08:00
Michael Gottesman
264955ccb3 [ownership] Convert switch_enum to be an ownership forwarding inst and store the forwarding ownership kind within it.
Previously, we always inferred the ownership of the switch_enum from its phi
operands. This forced us to need to model a failure to find a good
OperandOwnershipKindMap in OperandOwnership.cpp. We want to eliminate such
conditions so that we can use failing to find a constraint to mean that a value
can accept any value rather than showing a failure.
2020-11-08 20:32:20 -08:00
Michael Gottesman
f36e8561f1 [ownership] Use a new ADT SwitchEnumBranch instead of SwitchEnumInstBase for generic operations on SwitchEnum{,Addr}Inst.
I have a need to have SwitchEnum{,Addr}Inst have different base classes
(TermInst, OwnershipForwardingTermInst). To do this I need to add a template to
SwitchEnumInstBase so I can switch that BaseTy. Sadly since we are using
SwitchEnumInstBase as an ADT type as well as an actual base type for
Instructions, this is impossible to do without introducing a template in a ton
of places.

Rather than doing that, I changed the code that was using SwitchEnumInstBase as
an ADT to instead use a proper ADT SwitchEnumBranch. I am happy to change the
name as possible see fit (maybe SwitchEnumTerm?).
2020-11-08 19:52:02 -08:00
swift_jenkins
7f5c6c924d Merge remote-tracking branch 'origin/main' into next 2020-11-08 19:00:40 -08:00
Michael Gottesman
2ef7a212cd [ownership] Rename the cases of UseLifetimeConstraint to use clearer language
Specifically,

* MustBeLive -> NonLifetimeEnding
* MustBeInvalidated -> LifetimeEnding
2020-11-08 13:23:17 -08:00
Doug Gregor
4c2c2f32e9 [Concurrency] Implement a builtin createAsyncTask() to create a new task.
`Builtin.createAsyncTask` takes flags, an optional parent task, and an
async/throwing function to execute, and passes it along to the
`swift_task_create_f` entry point to create a new (potentially child)
task, returning the new task and its initial context.
2020-11-07 23:05:04 -08:00
swift_jenkins
971243f0fe Merge remote-tracking branch 'origin/main' into next 2020-11-06 15:36:54 -08:00
Michael Gottesman
b785a5f1d4 Merge pull request #34605 from gottesmm/pr-75f04ac2c27715c84382d9a9e4ca7c0337a3ce30
[ownership] Now that we allow reborrows eliminate isForwardingSubValue it is dead.
2020-11-06 15:18:10 -08:00
swift_jenkins
6c8d31207e Merge remote-tracking branch 'origin/main' into next 2020-11-05 21:34:59 -08:00
Michael Gottesman
e5c98ad2c7 [ownership] Now that we allow reborrows eliminate isForwardingSubValue it is dead. 2020-11-05 19:42:10 -08:00
swift_jenkins
edac49f12a Merge remote-tracking branch 'origin/main' into next 2020-11-05 16:05:42 -08:00
Doug Gregor
c291eb596b [Concurrency] Add cancelAsyncTask() builtin.
Implement a new builtin, `cancelAsyncTask()`, to cancel the given
asynchronous task. This lowers down to a call into the runtime
operation `swift_task_cancel()`.

Use this builtin to implement Task.Handle.cancel().
2020-11-05 13:50:17 -08:00
Doug Gregor
81f5528199 [Concurrency] More cleanups for getCurrentAsyncTask builtin.
Michael has ghostwritten all of this to address his own comments.
Thank you!
2020-11-05 12:58:03 -08:00
Doug Gregor
ed9a548c9f [Concurrency] Return getCurrentAsyncTask() as owned.
Rather than produce an "unowned" result from `getCurrentAsyncTask()`,
take advantage of the fact that the task is effectively guaranteed in
the scope. Do so be returning it as "unowned", and push an
end_lifetime cleanup to end the lifetime. This eliminates unnecessary
ref-count traffic as well as introducing another use of unowned.

Approach is thanks to Michael Gottesman, bugs are mine.
2020-11-05 12:18:49 -08:00
Doug Gregor
9566d2e665 [Concurrency] Add a builtin to get the current task in an async function.
This introduces a new builtin, `getCurrentAsyncTask()`, that produces a
reference to the current task. This builtin can only be used within
`async` functions, and IR generation merely grabs the task argument
and packages it up.

The type of this function is `() -> Builtin.NativeObject`, because we
don't currently have a Swift-level representation of tasks, and can
probably handle everything through builtins or runtime calls.
2020-11-05 10:43:33 -08:00
swift_jenkins
288a7c964c Merge remote-tracking branch 'origin/main' into next 2020-11-05 09:12:46 -08:00
Doug Gregor
203d2c20fe Merge pull request #34573 from DougGregor/async-let
[Concurrency] Implement type checking for 'async let' declarations.
2020-11-05 08:44:21 -08:00
swift_jenkins
b2968caeb1 Merge remote-tracking branch 'origin/main' into next 2020-11-05 08:13:27 -08:00
Erik Eckstein
f3de3b4c1f SIL: set the @async attribute for top-level functions correctly 2020-11-05 13:58:40 +01:00
Doug Gregor
33cfbbdd72 [Concurrency] Don't diagnose missing "self." in async let autoclosures. 2020-11-04 23:18:51 -08:00
Arnold Schwaighofer
97e5e01fe2 Merge remote-tracking branch 'origin/main' into next 2020-11-04 12:20:35 -08:00
Erik Eckstein
5434786cad [concurrency] SIL: fix operand ownership of hop_to_executor 2020-11-04 09:53:11 +01:00
swift_jenkins
8d4e42e55e Merge remote-tracking branch 'origin/main' into next 2020-10-30 17:09:28 -07:00
Joe Groff
f72afc8110 Merge pull request #34519 from jckarter/async-sil-tweaks
Async sil tweaks
2020-10-30 16:49:26 -07:00