Commit Graph

1052 Commits

Author SHA1 Message Date
Becca Royal-Gordon
14f48dceac [IRGen] Fix crashes involving ObjC generic params
Sema allows you to pass type-pinning parameters into an extension of an Objective-C generic class, but IRGen did not properly handle erasing these types to existential types in runtime metadata. This commit corrects that mistake.
2021-07-19 19:59:27 -07:00
Becca Royal-Gordon
48f245363e [NFC] Rearrange irgen::MetadataSource initializers
Preparing to accommodate additional layouts for MetadataSource.
2021-07-19 19:59:27 -07:00
Becca Royal-Gordon
9da193ec8f [NFC] Unify helpers for checking for ObjC generics
There were two differently-named and slightly differently-implemented checks on nominal type decls. Clean this up a bit.
2021-07-19 19:59:26 -07:00
Saleem Abdulrasool
8da2c377da IRGen: add support for static linking on Windows
This adjusts the IRGen layer to accommodate the Windows linking model.
We assume dynamic linking by default.  The static linking is enabled by
passing `-static` to the driver, which forwards it to the frontend when
building the module statically.  This has already been required when
generating libraries, however, the non-Windows targets are more
forgiving and let it work.  On those platforms, using this hint would
allow for more efficient code generation, reducing load times and some
runtime penalties from the PLT and GOT references formed to symbols
which are module local.

This corrects static linking on Windows, which is one of the last few
items that are missing on Windows.  It also takes advantage of the hint
for the one peculiar difference between Windows and non-Windows:
protocol conformances that span module boundaries are not available as a
constant.  However, when statically linking, we can enable those
conformances to be statically resolved.  This should enable the last
known pattern to work when using static linking.

This support requires further work in the Swift Package Manager to
actually enable building libraries properly.  However, when building
with CMake, this should be sufficient to enable static linking.
2021-05-02 10:02:11 -07:00
Nate Chandler
d3e0e7af07 [ptrauth] Signed AsyncFunctionPointers as data.
Previously, AsyncFunctionPointer constants were signed as code.  That
was incorrect considering that these constants are in fact data.  Here,
that is fixed.

rdar://76118522
2021-04-05 18:24:41 -07:00
Slava Pestov
2a1b985f9e AST: PrettyStackTraceConformance doesn't need to take the ASTContext 2021-03-05 21:30:14 -05:00
Slava Pestov
53e06d69b5 AST: Factor out a new Requirement::getProtocolDecl() utility method 2021-02-25 17:21:18 -05:00
nate-chandler
e82544ce5b Merge pull request #35695 from nate-chandler/rdar73742483
[Async CC] Put extra sources before unfulfilled requirements.
2021-02-01 20:43:00 -08:00
Nate Chandler
0f7e368e80 [Async CC] Put extra sources before unfulfilled requirements.
EmitPolymorphicArguments puts the extra sources before the unfulfilled
requirements into the explosion.  The NecessaryBindings instance must
have the type parameters in the same order.  Previously, though, they
were added after enumerating unfulfilled requirements.  Here, they are
added _before_ enumerating, matching the ordering of
EmitPolymorphicArguments.

rdar://73742483
2021-02-01 16:13:35 -08:00
Slava Pestov
0768d1eb1a IRGen: Fix resilient witness tables and vtables to correctly reference async methods
Resilient witness tables and resilient class vtables are built from
descriptors. Make sure we reference the AsyncFunctionPointer of a
method implementation, and not the implementation itself, if the
method is async.

Part of rdar://problem/73625623.
2021-01-28 21:55:47 -05:00
Nate Chandler
d2726c31bb [Async CC] Don't lookup known archetype conformance.
Previously, when saving NecessaryBindings for an async function, if a
type that was passed-in happened to be an archetype, a lookup for that
type's conformance would always be done.  However, that lookup was not
always necessary or possible such as in the case where both the metadata
and the witness table were provided to the function in an async context.
Here, that is fixed by using the conformance that was seen when
constructing the NecessaryBindings if one is available.

rdar://problem/72397303
2020-12-16 17:20:31 -08:00
Nate Chandler
f33f1d3646 [Async CC] Always add full type metadata to bindings.
NecessaryBindings are used by both async functions and partial apply
forwarders.  The latter are able to avoid bindings in some cases because
a new function is generated where the information that would otherwise
be available in the bindings can be made available.  That is not the
case for async functions.  A generic async function requires all of the
metadata and witness tables be passed along to it: unlike a partial
apply forwarder it isn't in any way specialized so this information
can't be recovered.

Previously, metadata bindings were always passed along to async
functions.  However, destructuring that can be done for partial apply
forwarders was still being applied.  This resulted in an inappropriate
and unexpected number of bindings in NecessaryBindings.

Here, that destructuring is avoided for metadata passed to async
functions.

Now, the full metadata required by async functions are passed along to
them as necessary.

rdar://problem/71816041
2020-11-30 14:02:56 -08:00
Nate Chandler
b676a98b84 [Async CC] Add AsyncFunctionPointer to wtable. 2020-11-12 18:20:10 -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
Alejandro Alonso
424802fb34 Revert SE-0283 (#34492)
Reverted despite build failures.
2020-10-29 17:32:06 -07:00
Alejandro Alonso
84e5fd2137 Merge pull request #28833 from Azoy/void-is-equatable
[SE-0283] Implement Equatable, Comparable, and Hashable conformance for Tuples
2020-10-26 13:53:29 -04:00
nate-chandler
d1bfe027c0 Merge pull request #34402 from nate-chandler/concurrency/irgen/tests-all-async
[Async CC] Test: Mark functions that call async functions async.
2020-10-26 09:59:48 -07:00
Nate Chandler
989ea5bf5e [IRGen] Track repeated requirements for async functions in NecessaryBindings.
Previously, NecessaryBindings used a SetVector to store the
GenericRequirements that it accumulates.  That was a problem for the
layout of async contexts where it is possible for the same generic
argument to be repeated.  Meanwhile, that is correct for partial
application forwarders where there is a single thunk for every partial
apply and consequently the bindings can be packed in without
duplication.

Here, a SetVector is used only when the NecessaryBindings are for
partial apply forwarders.  When the NecessaryBindings are instead for
async functions, a SmallVector is used.
2020-10-23 11:06:03 -07:00
Azoy
e60ef84bd2 Implement Tuple Equatable Conformance 2020-10-22 18:24:28 -04:00
nate-chandler
c9115dc7c5 Merge pull request #34277 from nate-chandler/concurrency/irgen/thread-emission-through-emit-polymorphic
[Async CC] Pull polymorphic parameters from entry point emission.
2020-10-12 14:13:03 -07:00
Nate Chandler
6fce6d9363 [Async CC] Pull poly params from entry point emission.
Previously, EmitPolymorphicParameters dealt directly with an Explosion
from which it pulled values.  In one place, there was a conditional
check for async which handled some cases.  There was however another
place where the polymorphic parameter was pulled directly from the
explosion.  That missed case resulted in attempting to pull a
polymorphic parameter directly from an Explosion which contains only a
%swift.context* per the async calling convention.

Here, those parameters are now pulled from an EntryPointArgumentEmission
subclasses of which are able to provide the relevant definition of what
pulling a parameter means.

rdar://problem/70144083
2020-10-12 10:52:23 -07:00
nate-chandler
d32b9350b2 Merge pull request #34211 from nate-chandler/concurrency/irgen/protocol-witness-methods
[Async CC] Support for protocol witness methods.
2020-10-07 14:37:47 -07:00
John McCall
171e0e25fe Merge pull request #34213 from rjmccall/abstract-assoc-type-for-conformance
Fetch associated types abstractly when fetching an associated conformance
2020-10-07 11:22:51 -04:00
John McCall
2546c2d78d Fetch associated types abstractly when fetching an associated conformance.
By default, emitTypeMetadataRef does a blocking request for complete
metadata, which is the right thing to do for most purposes in IRGen.
Unfortunately, it's actively dangerous in code that can be called
during metadata completion, like an associated conformance accessor,
because it can cause artificial dependency cycles that the runtime
isn't equipped to detect, much less solve.

This is a partial fix for rdar://69901318, which also exposes a bad
metadata access path that seems to be causing an artificial problem.
2020-10-06 23:11:17 -04:00
Nate Chandler
7d74a8614d [Concurrency] Async CC supports witness methods.
Previously, the AsyncContextLayout did not make space for the trailing
witness fields (self metadata and self witness table) and the
AsyncNativeCCEntryPointArgumentEmission could consequently not vend
these fields.  Here, the fields are added to the layout.
2020-10-06 17:03:03 -07:00
Nate Chandler
1a106d329f [NFC] Tweaked name of NecessaryBindings factory method. 2020-10-06 11:55:25 -07:00
Nate Chandler
15d90d8f5b [IRGen] Pull async polymorphic params from explosion.
Previously, the polymorphic arguments were being discarded.  Here, that
situation is improved by pulling the polymorphic arguments out of the
explosion when having the polymorphic parameters via the
NecessaryBindings instance.  In order to eanble that, an overload of
NecessaryBindings::save is added which takes an explosion and asserts
that the polymorphic parameter pulled from the explosion matches with
the polymorphic parameter in the NecessaryBindings instance.
2020-10-06 11:55:10 -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
Nate Chandler
94b5f76654 Revert "[SIL] Add SILFunctionType flag for async."
This reverts commit 9b8828848d.
2020-08-25 13:37:26 -07:00
Nate Chandler
9b8828848d [SIL] Add SILFunctionType flag for async. 2020-08-19 11:29:58 -07:00
Joe Groff
7d3a4b1c0c IRGen: Name the symbol for the private use area attached to protocol conformance descriptors.
Make it easier to track the memory usage of these symbols.
2020-07-14 10:02:43 -07:00
Arnold Schwaighofer
8a2bfb046b No need to lookup the conformance we can just take the protocol from the requirement 2020-06-11 08:19:59 -07:00
Arnold Schwaighofer
478e13b9c9 Address review feedback 2020-06-10 06:29:02 -07:00
Arnold Schwaighofer
2b47815973 IRGen: Lookup the conformance of an archetype in the right substitution map when computing necessary bindings of abstract cnditional requirements
SR-12853
2020-06-09 13:19:52 -07:00
Anthony Latsis
44a92a926c [NFC] GenericSignatureImpl: Spell conformsToProtocol & getConformsTo in terms of requirements 2020-05-14 22:51:44 +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
Slava Pestov
ac4ca6dffa IRGen: Don't call getConditionalRequirementsIfAvailable()
We should not fail silently here if the conditional requirements
could not be computed due to a cycle.
2020-03-23 18:59:43 -04:00
Dan Zheng
723b2d2cbe [AutoDiff upstream] Add derivative function witness/vtable entry SILGen. (#30569)
`@differentiable` attribute on protocol requirements and non-final class
members now produces derivative function entries in witness tables and vtables.

This enables `witness_method` and `class_method` differentiation.

Existing type-checking rules:

- Witness declarations of `@differentiable` protocol requirements must have a
  `@differentiable` attribute with the same configuration (or a configuration
  with superset parameter indices).
  - Witness table derivative function entries are SILGen'd for `@differentiable`
    witness declarations.

- Class vtable derivative function entries are SILGen'd for non-final
  `@differentiable` class members.
  - These derivative entries can be overridden or inherited, just like other
    vtable entries.

Resolves TF-1212.
2020-03-22 16:59:01 -07:00
Meghana Gupta
8e800e49bf Recommit #29812 with fixes (#30342) 2020-03-13 19:34:16 -07:00
Slava Pestov
84890b6079 IRGen: Move a few things from WitnessTableBuilderBase to FragileWitnessTableBuilder 2020-03-11 14:38:26 -04:00
Slava Pestov
3eb9b15be3 IRGen: Split up WitnessTableBuilder into {Fragile,Resilient}WitnessTableBuilder
This eliminates the need to 'abandon' the initializer for the
fragile witness table, which resilient conformances do not need
or use.
2020-03-11 14:28:55 -04:00
Slava Pestov
305af0477d IRGen: Split off WitnessTableBuilderBase from WitnessTableBuilder 2020-03-11 14:06:06 -04: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 (Brecka) Mracek
b2edf20a81 Merge pull request #30112 from apple/mracek/arm64e
Add arm64e and pointer authentication support for Swift
2020-03-07 20:38:36 -08:00
Kuba Mracek
8a4a226cd6 Undo unintentional llvm::MaybeAlign changes 2020-03-07 13:44:16 -08:00
John McCall
ceff414820 Distinguish invocation and pattern substitutions on SILFunctionType.
In order to allow this, I've had to rework the syntax of substituted function types; what was previously spelled `<T> in () -> T for <X>` is now spelled `@substituted <T> () -> T for <X>`.  I think this is a nice improvement for readability, but it did require me to churn a lot of test cases.

Distinguishing the substitutions has two chief advantages over the existing representation.  First, the semantics seem quite a bit clearer at use points; the `implicit` bit was very subtle and not always obvious how to use.  More importantly, it allows the expression of generic function types that must satisfy a particular generic abstraction pattern, which was otherwise impossible to express.

As an example of the latter, consider the following protocol conformance:

```
protocol P { func foo() }
struct A<T> : P { func foo() {} }
```

The lowered signature of `P.foo` is `<Self: P> (@in_guaranteed Self) -> ()`.  Without this change, the lowered signature of `A.foo`'s witness would be `<T> (@in_guaranteed A<T>) -> ()`, which does not preserve information about the conformance substitution in any useful way.  With this change, the lowered signature of this witness could be `<T> @substituted <Self: P> (@in_guaranteed Self) -> () for <A<T>>`, which nicely preserves the exact substitutions which relate the witness to the requirement.

When we adopt this, it will both obviate the need for the special witness-table conformance field in SILFunctionType and make it far simpler for the SILOptimizer to devirtualize witness methods.  This patch does not actually take that step, however; it merely makes it possible to do so.

As another piece of unfinished business, while `SILFunctionType::substGenericArgs()` conceptually ought to simply set the given substitutions as the invocation substitutions, that would disturb a number of places that expect that method to produce an unsubstituted type.  This patch only set invocation arguments when the generic type is a substituted type, which we currently never produce in type-lowering.

My plan is to start by producing substituted function types for accessors.  Accessors are an important case because the coroutine continuation function is essentially an implicit component of the function type which the current substitution rules simply erase the intended abstraction of.  They're also used in narrower ways that should exercise less of the optimizer.
2020-03-07 16:25:59 -05:00
Kuba Mracek
84c4864911 [arm64e] Add Swift compiler support for arm64e pointer authentication 2020-02-27 16:10:31 -08:00
Arnold Schwaighofer
449766f32a IRGen: Fix specialized conformances with abstract conditional requirements in the partial apply forwarder
2nd attempt.

rdar://59723512
2020-02-24 14:23:19 -08:00
Arnold Schwaighofer
626e6e0fe5 Merge pull request #29912 from aschwaighofer/irgen_partial_apply_forwarder_specialized_conformance_with_abstract_conditional_requirements
IRGen: Fix specialized conformances with abstract conditional requirements in the partial apply forwarder
2020-02-21 05:58:34 -08:00