Commit Graph

808 Commits

Author SHA1 Message Date
John McCall
2012195cd5 Alter the runtime interface for awaiting futures and task groups.
First, just call an async -> T function instead of forcing the caller
to piece together which case we're in and perform its own copy.  This
ensures that the task is actually kept alive properly.

Second, now that we no longer implicitly depend on the waiting tasks
being run synchronously, go ahead and schedule them to run on the
global executor.

This solves some problems which were blocking the work on TLS-ifying
the task/executor state.
2021-02-21 23:48:13 -05:00
Arnold Schwaighofer
4373bdd6d0 Conditionally start using llvm::CallingConv::SwiftTail for async functions
This is conditional on UseAsyncLowering and in the future should also be
conditional on `clangTargetInfo.isSwiftAsyncCCSupported()` once that
support is merged.

Update tests to work either with swiftcc or swifttailcc.
2021-02-18 09:25:15 -08:00
Arnold Schwaighofer
5a6de1ba77 IRGen: Use opaque storage types for non-fixed indirect result types
LLVM does type based analysis on sret storage types. This is a problem
with non-fixed types whose storage type does not contain the non-fixed
part.

rdar://73778591
2021-02-10 16:19:12 -08:00
Richard Wei
18fe723543 Merge pull request #35811 from rxwei/69980056-differentiable-reverse
[AutoDiff] Add '@differentiable(reverse)'.
2021-02-08 04:32:27 -08:00
Richard Wei
af8942d940 [AutoDiff] Rename '@differentiable' to '@differentiable(reverse)'.
Compiler:
- Add `Forward` and `Reverse` to `DifferentiabilityKind`.
- Expand `DifferentiabilityMask` in `ExtInfo` to 3 bits so that it now holds all 4 cases of `DifferentiabilityKind`.
- Parse `@differentiable(reverse)` and `@differentiable(_forward)` declaration attributes and type attributes.
- Emit a warning for `@differentiable` without `reverse`.
- Emit an error for `@differentiable(_forward)`.
- Rename `@differentiable(linear)` to `@differentiable(_linear)`.
- Make `@differentiable(reverse)` type lowering go through today's `@differentiable` code path. We will specialize it to reverse-mode in a follow-up patch.

ABI:
- Add `Forward` and `Reverse` to `FunctionMetadataDifferentiabilityKind`.
- Extend `TargetFunctionTypeFlags` by 1 bit to store the highest bit of differentiability kind (linear). Note that there is a 2-bit gap in `DifferentiabilityMask` which is reserved for `AsyncMask` and `ConcurrentMask`; `AsyncMask` is ABI-stable so we cannot change that.

_Differentiation module:
- Replace all occurrences of `@differentiable` with `@differentiable(reverse)`.
- Delete `_transpose(of:)`.

Resolves rdar://69980056.
2021-02-07 14:09:46 -08:00
Nate Chandler
3df7b67f1e [IRGen] Sign task resume context in swift_suspend_point.
rdar://72357371
2021-02-03 19:43:39 -08:00
Nate Chandler
5536114296 [IRGen] Return unsigned context from resume_project_context.
In __swift_async_resume_project_context, the context is stored into the
extended frame.  On arm64e, it is signed first.  Previously, that signed
context was returned fro the function.  That resulted in code like

    pacda  x16, x10
    str    x16, [x9]
    ldr    x9, [x16, #0x48]

where the context is signed (pacda), stored into the extended frame
(str) and then an attempt is made to load from the signed context (ldr).
Here, the unsigned context is returned from the function.
2021-02-03 08:25:05 -08:00
Arnold Schwaighofer
daa72d3cc5 Add llvm::Attribute::SwiftAsync to the context parameter
* Adds support for generating code that uses swiftasync parameter lowering.

* Currently only arm64's llvm lowering supports the swift_async_context_addr intrinsic.

* Add arm64e pointer signing of updated swift_async_context_addr.

This commit needs the PR llvm-project#2291.

* [runtime] unittests should use just-built compiler if the runtime did

This will start to matter with the introduction of usage of swiftasync parameters which only very recent compilers support.

rdar://71499498
2021-01-22 10:01:55 -08:00
Erik Eckstein
e697e49582 [concurrency] IRGen:: do ptrauth for hop_to_executor
Sign the resume-function pointer which is stored in AsyncTask.ResumeTask
2020-12-15 15:23:20 +01:00
Nate Chandler
0f2b659a8d [PtrAuth] Pass discriminator to swift_suspend_dispatch.
Previously, swift_suspend_dispatch was passed a pointer to a function
and createAsyncDispatchFn was passed a FunctionPointer.  The latter
constructed a new FunctionPointer using the passed-in function pointer
as the value, because the value inside the FunctionPointer was a value
in a different function.

That worked fine on platforms without pointer authentication.  On
arm64e, however, calling a function can use two values from a
FunctionPointer: the pointer to the function and the discriminator.  The
result was that on arm64e, swift_suspend_dispatch failed verification
because the discriminator value that was used in the call made by
swift_suspend_dispatch did not originate in that function.

Here, that problem is resolved by passing the discriminator to
swift_suspend_dispatch.  Now, createAsyncDispatchFn creates a
FunctionPointer using not just the passed-in function pointer but also
the passed-in discriminator.  The result is that swift_suspend_dispatch
no longer fails verification.
2020-12-04 18:11:05 -08:00
Nate Chandler
f1d263b2ba [Async CC] Fixed ptrauth for dynamic ptrs in partial applies.
A partial apply of an async non-direct function entails storing a
pointer to the function's AsyncFunctionPointer into the thick context
using a specific (IGM.getOptions().PointerAuth.PartialApplyCapture)
ptrauth schema.  Previously, the incorrect schema (derived from the
function type) was used to auth the ptr-to-AsyncFunctionPointer and then
to again sign the ptr-to-function.  Here that error is corrected.  Now,
the pointer-to-AsyncFunctionPointer is auth'd using that specific schema
and then the extracted function pointer is again signed again using it.
2020-12-04 18:11:05 -08:00
Nate Chandler
7091e0ddc3 [PtrAuth] Auth parent async context after loading. 2020-12-04 16:36:36 -08:00
Erik Eckstein
4e296c0c5a IRGen: emit emit_hop_to_executor instructions.
This basically means to create a suspension point and call the swift_task_switch runtime function.

rdar://71099289
2020-12-03 12:42:41 +01:00
Erik Eckstein
bf2be9eb5d [concurrency] IRGen: update task/executor/context on every suspend point
For this, store those 3 values on the stack at function entry and update them with the return values of coro_suspend_async intrinsic calls.

This fixes a correctness issue, because the executor may be different after a resume.
It also is more efficient, because this means that the 3 values don't have to preserved in the context over a suspension point.
2020-12-01 15:19:39 +01:00
Arnold Schwaighofer
beb3951c3f Get rid of getOrCreateAwaitAsyncSupendFn 2020-11-19 13:05:06 -08:00
Arnold Schwaighofer
d75e4c2bd2 IRGen: get/await_async_continuation support.
rdar://71124933
2020-11-19 08:43:26 -08:00
Nate Chandler
b7978b664c [Async CC] Don't add thick placeholder to args.
Now that the convention for partial apply forwarders of async functions
is that the thick context is embedded within the async context, there is
never a need for a placeholder thick context.  Here, the placeholder
thick context is only added when the function for which a partial apply
forwarder is being emitted is not async.
2020-11-16 13:06:22 -08:00
Nate Chandler
eba057fe88 [Async CC] Move thick context into async context.
Previously, the thick context was passed as a fourth parameter to
partial apply forwarders.  Here, the thick context is instead moved into
the async context at the local context position.  To support this, the
local context is made always available.
2020-11-16 12:02:32 -08:00
Arnold Schwaighofer
74419d0885 Lower asynchronous functions to LLVM async coroutine intrinsics
rdar://70097093
2020-11-13 10:41:42 -08:00
Nate Chandler
2d21932672 [Async CC] Add constant "pointer" for async func.
An AsyncFunctionPointer, defined in Task.h, is a struct consisting of
two i32s: (1) the relative address of the async function and (2) the
size of the async context to be allocated when calling that function.

Here, such structs are emitted for every async SILFunction that is
emitted.
2020-11-12 18:20:10 -08:00
Nate Chandler
8e9dec23fc [NFC] Construct AsyncContextLayout from module.
Previously, an IRGenFunction was being passed to the functions that
construct an AsyncContextLayout.  That was not actually necessary and
prevented construction of the layout in contexts where no IRGenFunction
was present.  Here that requirement is eased to requiring an IRGenModule
which is indeed required to construct an AsyncContextLayout.
2020-11-04 18:42:06 -08:00
Nate Chandler
de1c7879ad [Async CC] Forward task and executor through partial apply forwarder.
Previously, the task and executor values passed to a partial apply
forwarder were being ignored. Here, they are passed along to the
partially applied function.
2020-11-04 14:27:28 -08:00
Nate Chandler
b74052cdd2 [Async CC] Added task and executor args.
The previous stage of bringup only had async functions taking a single
argument: the async context.  The next stage will involve the task and
executor.  Here, arguments are added for those values.  To begin with,
null is always passed for these values.
2020-10-26 14:27:16 -07:00
Nate Chandler
506473dfba [Async CC] Supported partial application.
The majority of support comes in the form of emitting partial
application forwarders for partial applications of async functions.
Such a partial application forwarder must take an async context which
has been partially populated at the apply site.  It is responsible for
populating it "the rest of the way".  To do so, like sync partial
application forwarders, it takes a second argument, its context, from
which it pulls the additional arguments which were capture at
partial_apply time.

The size of the async context that is passed to the forwarder, however,
can't be known at the apply site by simply looking at the signature of
the function to be applied (not even by looking at the size associated
with the function in the special async function pointer constant which
will soon be emitted).  The reason is that there are an unknown (at the
apply site) number of additional arguments which will be filled by the
partial apply forwarder (and in the case of repeated partial
applications, further filled in incrementally at each level).  To enable
this, there will always be a heap object for thick async functions.
These heap objects will always store the size of the async context to be
allocated as their first element.  (Note that it may be possible to
apply the same optimization that was applied for thick sync functions
where a single refcounted object could be used as the context; doing so,
however, must be made to interact properly with the async context size
stored in the heap object.)

To continue to allow promoting thin async functions to thick async
functions without incurring a thunk, at the apply site, a null-check
will be performed on the context pointer.  If it is null, then the async
context size will be determined based on the signature.  (When async
function pointers become pointers to a constant with a size i32 and a
relative address to the underlying function, the size will be read from
that constant.)  When it is not-null, the size will be pulled from the
first field of the context (which will in that case be cast to
<{%swift.refcounted, i32}>).

To facilitate sharing code and preserving the original structure of
emitPartialApplicationForwarder (which weighed in at roughly 700 lines
prior to this change), a new small class hierarchy, descending from
PartialApplicationForwarderEmission has been added, with subclasses for
the sync and async case.  The shuffling of arguments into and out of the
final explosion that was being performed in the synchronous case has
been preserved there, though the arguments are added and removed through
a number of methods on the superclass with more descriptive names.  That
was necessary to enable the async class to handle these different
flavors of parameters correctly.

To get some initial test coverage, the preexisting
IRGen/partial_apply.sil and IRGen/partial_apply_forwarder.sil tests have
been duplicated into the async folder.  Those tests cases within these
files which happened to have been crashing have each been extracted into
its own runnable test that both verifies that the compiler does not
crash and also that the partial application forwarder behaves correctly.
The FileChecks in these tests are extremely minimal, providing only
enough information to be sure that arguments are in fact squeezed into
an async context.
2020-10-22 12:21:56 -07:00
Nate Chandler
ee88152d6b [Concurrency] First steps towards async CC.
Here, the following is implemented:
- Construction of SwiftContext struct with the fields needed for calling
  functions.
- Allocating and deallocating these swift context via runtime calls
  before calling async functions and after returning from them.
- Storing arguments (including bindings and the self parameter but not
  including protocol fields for witness methods) and returns (both
  direct and indirect).
- Calling async functions.

Additional things that still need to be done:
- protocol extension methods
- protocol witness methods
- storing yields
- partial applies
2020-10-05 20:43:51 -07:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Arnold Schwaighofer
147144baa6 SIL: Thread type expansion context through to function convention apis
This became necessary after recent function type changes that keep
substituted generic function types abstract even after substitution to
correctly handle automatic opaque result type substitution.

Instead of performing the opaque result type substitution as part of
substituting the generic args the underlying type will now be reified as
part of looking at the parameter/return types which happens as part of
the function convention apis.

rdar://62560867
2020-05-04 13:53:30 -07:00
Robert Widmann
40d9cd8d3f [NFC] Give IRGenModule Exclusive Ownership of an LLVMContext Object 2020-04-16 11:57:44 -07:00
Arnold Schwaighofer
5ab6df8f94 IRGen: Fix capture of indirect values in `[onstack]` closures
A [onstack] closure does not take ownership of its arguments. It is
therefore not correct to use an initWithTake copy of indirect arguments.

Instead we just capture the address of the indirect argument.

rdar://61261982
2020-04-06 13:23:34 -07:00
Dan Zheng
603db8c954 [AutoDiff upstream] Add @differentiable function IRGen.
Lower `@differentiable` and `@differentiable(linear)` functions as structs of
function pointers.
2020-03-22 23:52:28 -07:00
Meghana Gupta
8e800e49bf Recommit #29812 with fixes (#30342) 2020-03-13 19:34:16 -07:00
Rintaro Ishizaki
ccbc26d947 Revert "Use in_guaranteed for let captures (#29812)"
This reverts commit 13b9915c6f.
2020-03-10 16:08:08 -07:00
Meghana Gupta
13b9915c6f Use in_guaranteed for let captures (#29812)
* Use in_guaranteed for let captures

With this all let values will be captured with in_guaranteed convention
by the closure. Following are the main changes :

SILGen changes:
- A new CaptureKind::Immutable is introduced, to capture let values as in_guaranteed.
- SILGen of in_guaranteed capture had to be fixed.
  in_guaranteed captures as per convention are consumed by the closure. And so SILGen should not generate a destroy_addr for an in_guaranteed capture.
  But LetValueInitialization can push Dealloc and Release states of the captured arg in the Cleanup stack, and there is no way to access the CleanupHandle and disable the emission of destroy_addr while emitting the captures in SILGenFunction::emitCaptures.
  So we now create, temporary allocation of the in_guaranteed capture iduring SILGenFunction::emitCaptures without emitting destroy_addr for it.

SILOptimizer changes:
- Handle in_guaranteed in CopyForwarding.
- Adjust dealloc_stack of in_guaranteed capture to occur after destroy_addr for on_stack closures in ClosureLifetimeFixup.

IRGen changes :
  - Since HeapLayout can be non-fixed now, make sure emitSize is used conditionally
  - Don't consider ClassPointerSource kind parameter type for fulfillments while generating code for partial apply forwarder.
    The TypeMetadata of ClassPointSource kind sources are not populated in HeapLayout's NecessaryBindings. If we have a generic parameter on the HeapLayout which can be fulfilled by a ClassPointerSource, its TypeMetaData will not be found while constructing the dtor function of the HeapLayout.
    So it is important to skip considering sources of ClassPointerSource kind, so that TypeMetadata of a dependent generic parameters gets populated in HeapLayout's NecessaryBindings.
2020-03-10 12:23:02 -07:00
Kuba Mracek
84c4864911 [arm64e] Add Swift compiler support for arm64e pointer authentication 2020-02-27 16:10:31 -08:00
Arnold Schwaighofer
e8a7b1bd75 Merge pull request #29859 from aschwaighofer/irgen_typelayout_based_value_witness
IRGen: Type layout based value witness generation
2020-02-21 08:24:17 -08:00
Arnold Schwaighofer
7b65768167 IRGen: Add code to compute type layouts
rdar://51988441
2020-02-19 07:12:55 -08:00
Arnold Schwaighofer
6992712bec IRGen: Fix specialized conformances with abstract conditional requirements in the partial apply forwarder
We need to add abstract requirements of the conditional requirements.

rdar://59456064
2020-02-18 13:43:39 -08:00
Arnold Schwaighofer
4cba76309f IRGen: Add TypeExpansionContext to IRGen 2019-11-11 14:21:52 -08:00
Joe Groff
03c7919b4a SIL: Add fields to SILFunctionType for substituted function types.
https://forums.swift.org/t/improving-the-representation-of-polymorphic-interfaces-in-sil-with-substituted-function-types/29711

This prepares SIL to be able to more accurately preserve the calling convention of
polymorphic generic interfaces by letting the type system represent "substituted function types".
We add a couple of fields to SILFunctionType to support this:

- A substitution map, accessed by `getSubstitutions()`, which maps the generic signature
  of the function to its concrete implementation. This will allow, for instance, a protocol
  witness for a requirement of type `<Self: P> (Self, ...) -> ...` for a concrete conforming
  type `Foo` to express its type as `<Self: P> (Self, ...) -> ... for <Foo>`, preserving the relation
  to the protocol interface without relying on the pile of hacks that is the `witness_method`
  protocol.

- A bool for whether the generic signature of the function is "implied" by the substitutions.
  If true, the generic signature isn't really part of the calling convention of the function.
  This will allow closure types to distinguish a closure being passed to a generic function, like
  `<T, U> in (*T, *U) -> T for <Int, String>`, from the concrete type `(*Int, *String) -> Int`,
  which will make it easier for us to differentiate the representation of those as types, for
  instance by giving them different pointer authentication discriminators to harden arm64e
  code.

This patch is currently NFC, it just introduces the new APIs and takes a first pass at updating
code to use them. Much more work will need to be done once we start exercising these new
fields.

This does bifurcate some existing APIs:

- SILFunctionType now has two accessors to get its generic signature.
  `getSubstGenericSignature` gets the generic signature that is used to apply its
  substitution map, if any. `getInvocationGenericSignature` gets the generic signature
  used to invoke the function at apply sites. These differ if the generic signature is
  implied.
- SILParameterInfo and SILResultInfo values carry the unsubstituted types of the parameters
  and results of the function. They now have two APIs to get that type. `getInterfaceType`
  returns the unsubstituted type of the generic interface, and
  `getArgumentType`/`getReturnValueType` produce the substituted type that is used at
  apply sites.
2019-10-25 13:38:51 -07:00
Michael Munday
bb2740e540 IRGen: fix enum bit packing on big-endian platforms.
This change modifies spare bit masks so that they are arranged in
the byte order of the target platform. It also modifies and
consolidates the code that gathers and scatters bits into enum
values.

All enum-related validation tests are now passing on IBM Z (s390x)
which is a big-endian platform.
2019-08-07 03:54:16 -04:00
Richard Wei
e68c76615e Use IRGenFunction::coerceValue suggested by @aschwaighofer. 2019-02-06 16:13:05 -08:00
Richard Wei
31dc5d76e4 [IRGen] Fix crasher in emitPartialApplicationForwarder when the return type is a tuple that contains a closure.
In `emitPartialApplicationForwarder`, when we handle result types that have a type parameter, the lowered result type could be a struct.
This happens when we have a function result, for instance:

```swift
  %0 = function_ref @returns_closure : $@convention(thin) <τ_0_0> (Empty<τ_0_0>) -> (@owned Empty<τ_0_0>, @owned @callee_guaranteed (Empty<τ_0_0>) -> @owned Empty<τ_0_0>)
  %1 = partial_apply [callee_guaranteed] %0<S>() : $@convention(thin) <τ_0_0> (Empty<τ_0_0>) -> (@owned Empty<τ_0_0>, @owned @callee_guaranteed (Empty<τ_0_0>) -> @owned Empty<τ_0_0>)
```

In this case, we emit code that casts the struct memberwise.

Resolves [SR-9709](https://bugs.swift.org/browse/SR-9709).
2019-02-05 16:14:48 -08:00
Arnold Schwaighofer
3982bdee90 IRGen: Don't emit capture descriptor for stack allocated closures 2019-01-24 13:48:20 -08:00
Arnold Schwaighofer
6f822a227c IRGen for partial_apply [stack] 2019-01-15 11:20:33 -08:00
Saleem Abdulrasool
d3efed2943 IRGen: use ApplyIRLinkage more aggressively (NFC)
Use `ApplyIRLinkage` rather than manually applying the DLLStorage.  This
makes it much more apparent as to the desired semantic linkage desired.
2019-01-04 10:39:03 -08:00
Arnold Schwaighofer
f14df42ecb IRGen: Code simplification coerceValue also handles bitcast
NFC
2018-12-11 08:19:40 -08:00
Arnold Schwaighofer
2dd0719e36 IRGen: Fix building a closure that captures a struct containing a reference using an indirect convention
A <{ %AClass*}> value cannot be bitcasted to a swift.refcounted*.

rdar://46538967
2018-12-10 14:56:11 -08:00
Slava Pestov
ad230a065d IRGen: Remove some InOutType usages 2018-12-10 00:00:49 -05:00
Saleem Abdulrasool
cddc1068ca IRGen: conform to the BlocksRuntime ABI
The BlocksRuntime ABI defines `Block_layout_1` as <{ Int32, Int32 }> on
32-bit and <{ Int64, Int64 }> on 64-bit.  However, we were currently
treating it as <{ IntPtr, IntPtr }>.  This usually gets away with it as
it defined as IntPtrTy which matches this except on LLP64 targets.
Adjust it to match that.
2018-11-13 21:25:42 -08:00
John McCall
885613ac49 [NFC] Extract a helper class for defining pair TypeInfos 2018-10-30 05:26:56 -04:00