Commit Graph

849 Commits

Author SHA1 Message Date
Slava Pestov
3904fe83fb AST: Centralize ABI-related deployment target checks
There were a couple of methods in LangOptions and some related ones in
Availability and ASTContext that were added more recently.

Refactor the three older checks to the newer scheme.
2020-03-03 22:49:19 -05:00
Dan Zheng
f5b40d6705 [AutoDiff upstream] Add SIL derivative function type caching. (#29953)
Upstream https://github.com/apple/swift/pull/29590:
cache `SILFunctionType::getAutoDiffDerivativeFunctionType` results.
2020-02-20 05:17:39 -08:00
Hamish Knight
13bfac1820 Register IRGen SIL passes with the ASTContext
Rather than registering individual IRGen passes
when we want to execute them, store function
pointers to all the pass constructors on the
ASTContext. This will make it easier to requestify
the execution of pass pipelines.
2020-02-14 09:57:27 -08:00
Robert Widmann
1ed846d852 Merge pull request #25696 from kelvin13/comparable-enums
synthesized comparable for enums
2020-02-06 21:38:36 -08:00
John McCall
b46938924f Merge pull request #29670 from rjmccall/ciliainig-teyepee-sieeriiieaileiizieaitieieoiniie
Implement Swift serialization and deserialization of Clang types
2020-02-07 00:04:30 -05:00
John McCall
faee21b626 Implement Swift serialization and deserialization of Clang types.
As part of this, we have to change the type export rules to
prevent `@convention(c)` function types from being used in
exported interfaces if they aren't serializable.  This is a
more conservative version of the original rule I had, which
was to import such function-pointer types as opaque pointers.
That rule would've completely prevented importing function-pointer
types defined in bridging headers and so simply doesn't work,
so we're left trying to catch the unsupportable cases
retroactively.  This has the unfortunate consequence that we
can't necessarily serialize the internal state of the compiler,
but that was already true due to normal type uses of aggregate
types from bridging headers; if we can teach the compiler to
reliably serialize such types, we should be able to use the
same mechanisms for function types.

This PR doesn't flip the switch to use Clang function types
by default, so many of the clang-function-type-serialization
FIXMEs are still in place.
2020-02-06 22:09:00 -05:00
Nate Chandler
4300f711b4 [metadata prespecialization] Available in 5.3. 2020-02-06 11:13:21 -08:00
Kelvin
19a8759cee Merge branch 'master' into comparable-enums 2020-02-04 22:08:40 -06:00
nate-chandler
4bc78376aa Merge pull request #29442 from nate-chandler/generic-metadata-prespecialization-components/future-availability
[metadata prespecialization] Future availability.
2020-01-24 18:21:43 -08:00
Nate Chandler
23ff5f8964 [metadata prespecialization] Future availability.
Metadata prespecialization will be available after Swift 5.2.
2020-01-24 14:46:30 -08:00
kelvin13
1715ebcf9d address more reviewed issues 2020-01-23 22:46:30 -06:00
kelvin13
2701012434 fix some reviewed issues 2020-01-22 21:56:47 -06:00
kelvin13
fb4bd68653 Merge branch 'master' into comparable-enums 2020-01-22 18:38:31 -06:00
Varun Gandhi
191b0d8c02 Revert "[AST] Add functionality for computing Clang types for SIL functions."
This reverts commit bfa2f98d95.
2020-01-22 09:04:52 -08:00
Varun Gandhi
f6e0db5bdc Merge pull request #29239 from varungandhi-apple/vg-clang-types-in-sil
Propagate Clang types through SIL
2020-01-21 09:42:37 -08:00
Dan Zheng
44d937d7c2 [AutoDiff upstream] Add @differentiable declaration attribute type-checking. (#29231)
The `@differentiable` attribute marks a function as differentiable.

Example:
```
@differentiable(wrt: x, jvp: derivativeFoo where T: Differentiable)
func id<T>(_ x: T) -> T { x }
```

The `@differentiable` attribute has an optional `wrt:` clause specifying the
parameters that are differentiated "with respect to", i.e. the differentiability
parameters. The differentiability parameters must conform to the
`Differentiable` protocol.

If the `wrt:` clause is unspecified, the differentiability parameters are
currently inferred to be all parameters that conform to `Differentiable`.

The `@differentiable` attribute also has optional `jvp:` and `vjp:` labels
for registering derivative functions. These labels are deprecated in favor of
the `@derivative` attribute and will be removed soon.

The `@differentiable` attribute also has an optional `where` clause, specifying
extra differentiability requirements for generic functions.

The `@differentiable` attribute is gated by the
`-enable-experimental-differentiable-programming` flag.

Code changes:
- Add `DifferentiableAttributeTypeCheckRequest`.
  - Currently, the request returns differentiability parameter indices, while
    also resolving `JVPFunction`, `VJPFunction`, and
    `DerivativeGenericSignature` and mutating them in-place in
    `DifferentiableAttr`. This was the simplest approach that worked without
    introducing request cycles.
- Add "is type-checked" bit to `DifferentiableAttr`.
  - Alternatively, I tried changing `DifferentiableAttributeTypeCheckRequest` to
    use `CacheKind::Cache` instead of `CacheKind::SeparatelyCached`, but it did
    not seem to work: `@differentiable` attributes in non-primary-files were
    left unchecked.

Type-checking rules (summary):
- `@differentiable` attribute must be declared on a function-like "original"
  declaration: `func`, `init`, `subscript`, `var` (computed properties only).
- Parsed differentiability parameters must be valid (if they exist).
- Parsed `where` clause must be valid (if it exists).
- Differentiability parameters must all conform to `Differentiable`.
- Original result must all conform to `Differentiable`.
- If JVP/VJP functions are specified, they must match the expected type.
  - `@differentiable(jvp:vjp:)` for derivative registration is deprecated in
    favor of `@derivative` attribute, and will be removed soon.
- Duplicate `@differentiable` attributes with the same differentiability
  parameters are invalid.
- For protocol requirements and class members with `@differentiable` attribute,
  conforming types and subclasses must have the same `@differentiable` attribute
  (or one with a superset of differentiability parameter indices) on
  implementing/overriding declarations.
2020-01-19 12:23:04 -08:00
Varun Gandhi
bfa2f98d95 [AST] Add functionality for computing Clang types for SIL functions. 2020-01-17 16:22:39 -08:00
Dan Zheng
a98fe74044 [AutoDiff upstream] Add TangentSpace. (#29107)
`TangentSpace` represents the tangent space of a type.

- For `Differentiable`-conforming types:
  - The tangent space is the `TangentVector` associated type.
- For tuple types:
  - The tangent space is a tuple of the elements' tangent space types, for the
    elements that have a tangent space.
- Other types have no tangent space.

`TypeBase::getAutoDiffTangentSpace` gets the tangent space of a type.

`TangentSpace` is used to:
- Compute the derivative function type of a given original function type.
- Compute the type of tangent/adjoint values during automatic differentiation.

Progress towards TF-828: upstream `@differentiable` attribute type-checking.
2020-01-12 12:29:08 -08:00
Nate Chandler
89278f8cbf [metadata prespecialization] Target only >=5.2.
Compatibility with earlier swift runtimes would require modifying the
runtime compatibility libraries to adjust the behavior of
checkMetadataState by way of typeForMangledNode or even
typeForMangledName.  For now, simply require that a version of swift
whose runtime knows about prespecialized metadata is being targeted.
2020-01-09 17:25:32 -08:00
Varun Gandhi
022314a640 Merge pull request #28643 from kitaisreal/using-located-instead-of-pair
[Compiler]: Using Located<T> instead of std::pair<SourceLoc, T>
2020-01-06 14:22:29 -08:00
kelvin13
ed2552a694 fix merge conflict and update caseless stdlib enums to use synthesized Comparable 2020-01-02 18:40:50 -06:00
Doug Gregor
bbcaf8c669 [Type checker] Introduce value witness constraints.
Introduce a new kind of constraint, the "value witness" constraint,
which captures a reference to a witness for a specific protocol
conformance. It otherwise acts like a more restricted form of a "value
member" constraint, where the specific member is known (as a
ValueDecl*) in advance.

The constraint is effectively dependent on the protocol
conformance itself; if that conformance fails, mark the type variables
in the resolved member type as "holes", so that the conformance
failure does not cascade.

Note that the resolved overload for this constraint always refers to
the requirement, rather than the witness, so we will end up recording
witness-method references in the AST rather than concrete references,
and leave it up to the optimizers to perform devirtualization. This is
demonstrated by the SIL changes needed in tests, and is part of the
wider resilience issue with conformances described by
rdar://problem/22708391.
2020-01-02 12:06:23 -08:00
Kita, Maksim
c1444dea18 SR-11889: Fixed code review issues 2019-12-20 17:18:59 +03:00
Kita, Maksim
b7cb3b67bf SR-11889: Using Located<T> instead of std::pair<SourceLoc, T> 2019-12-20 17:18:58 +03:00
Marc Rasi
637314aeef [AutoDiff] factor derivative typechecking helper out of AttributeChecker 2019-12-19 17:23:38 -08:00
Dan Zheng
bb1052ca3e [AutoDiff upstream] Upstream @derivative attribute type-checking. (#28738)
The `@derivative` attribute registers a function as a derivative of another
function-like declaration: a `func`, `init`, `subscript`, or `var` computed
property declaration.

The `@derivative` attribute also has an optional `wrt:` clause specifying the
parameters that are differentiated "with respect to", i.e. the differentiation
parameters. The differentiation parameters must conform to the `Differentiable`
protocol.

If the `wrt:` clause is unspecified, the differentiation parameters are inferred
to be all parameters that conform to `Differentiable`.

`@derivative` attribute type-checking verifies that the type of the derivative
function declaration is consistent with the type of the referenced original
declaration and the differentiation parameters.

The `@derivative` attribute is gated by the
`-enable-experimental-differentiable-programming` flag.

Resolves TF-829.
2019-12-12 18:18:18 -08:00
Varun Gandhi
e7be41d0cb Merge pull request #27479 from varungandhi-apple/vg-track-clang-function-types
Track Clang function types in the AST
2019-12-04 08:47:35 -08:00
Varun Gandhi
196f358dec [AST] Add ClangTypeConverter, computing C types for FunctionTypes.
Note: The change in ASTBuilder::createFunctionType is functionally minor,
but we need the FunctionType::Params computed _before_ the ExtInfo, so we
need to shuffle a bunch of code around.
2019-11-22 12:42:36 -08:00
Andrew Trick
8052a61a56 Merge pull request #28430 from atrick/fix-existential-conformance
Fix ExistentialSpecializer: inherited conformance
2019-11-22 09:13:21 -08:00
Andrew Trick
7d0a772542 Fix ExistentialSpecializer: inherited conformance
The ExistentialSpecializer incorrectly assumed that an existential's conformances match an opened archetype. They don't. Opened archetypes strip inherited conformances per the ABI for generic argument passing. Existential values retain those inherited conformances (for some inexplicable reason).

- Rename ASTContext::getExistentialSignature() to
  getOpenedArchetypeSiganture() because it was doing exactly the wrong
  thing for existentials.

- Fix ConcreteExistentialInfo to produce the correct SubstitutionMap.

- Fix ExistentialSpecializer to generate the correct conformances for
  init_existential by adding a collectExistentialConformances() helper.

Fixes <rdar://problem/57025861> "Assertion failed: (conformances.size() == numConformanceRequirements)" in ExistentialSpecializer on inlined code
2019-11-22 01:56:20 -08:00
kelvin13
25e4cfc1a2 fix build failures 2019-11-21 18:47:50 -06:00
kelvin13
e4dc295d64 fix conflicts 2019-11-21 12:42:10 -06:00
Arnold Schwaighofer
75c0bf076f Merge pull request #28337 from aschwaighofer/get_type_by_mangled_name_abstract_runtime
Add swift_getTypeByMangledNameInContextInMetadataState such that we can
2019-11-21 07:37:25 -08:00
Slava Pestov
ef05accd31 Sema: Remove even more vestigial TypeChecker usages 2019-11-19 17:40:00 -05:00
Arnold Schwaighofer
0d324d223f Add swift_getTypeByMangledNameInContextInMetadataState such that we can
use getTypeByMangledName when abstract metadata state is requested

This can significantly reduce the code size of apps constructing deeply
nested types with conditional conformances.

Requires a new runtime.

rdar://57157619
2019-11-18 14:41:35 -08:00
Robert Widmann
88ee618a33 Move Vending Top-Level Autoclosure discriminators into ASTContext
This bit has historically survived typechecking and parsing across source files.  Stick it where we stick the other global state.

This also means we don't have to thread TopLevelContext around anymore when invoking high-level typechecking entrypoints.
2019-11-13 15:28:23 -08:00
Robert Widmann
48805b1d44 Give ASTContext TypeCheckerOptions
Strip TypeChecker of all of this state.
2019-11-12 09:56:01 -08:00
Robert Widmann
2f2b8afad8 Remove the list of synthesized decls"
This used to be a lot more relevant a long time ago when typeCheckFunctionsAndExternalDecls actually did type check external functions defined in C.  Now, it serves no purpose.

The validation order change from just type checking these things eagerly doesn't seem to affect anything.
2019-11-11 11:33:59 -08:00
Hamish Knight
18088b04f0 [AST] Consolidate Obj-C types on ASTContext
This commit moves the getNSObjectType and
getObjCSelectorType methods from TypeChecker
onto ASTContext. In addition, it moves the
FOR_KNOWN_FOUNDATION_TYPES macro into a separate
file to define each of the Obj-C type decls
we want to have access to.
2019-11-07 08:26:08 -08:00
Robert Widmann
e907d3bc52 [NFC] Remove the TypeChecker from Typo Correction 2019-11-05 20:29:36 -08:00
Robert Widmann
2b08d1b834 [NFC] ASTContext::getLazyResolver -> ASTContext::getLegacyGlobalTypeChecker 2019-11-05 14:44:41 -08:00
Robert Widmann
56a713f179 Remove LazyResolver! 2019-11-05 14:43:12 -08:00
Richard Wei
9765858d31 Rename to 'IndexSubset' and move it to its own file. 2019-10-11 13:07:53 -07:00
Richard Wei
fd3a8804eb [AutoDiff upstream] [AST] Add 'AutoDiffIndexSubset' data structure.
`AutoDiffIndexSubset` is a fixed-size bit vector that is used for efficiently representing a subset of indices in automatic differentiation, specifically for representing a subset of parameters and results of a function to differentiate with respect to. It is uniqued in `ASTContext`.

This patch adds definition and unit tests for `AutoDiffIndexSubset` along with new files `AutoDiff.h` and `AutoDiff.cpp` into the 'AST' target, with no changes to the compiler's behavior. More data structures used for AutoDiff will be added to these files.

----------------------------

This is part of the ongoing effort to merge the experimental [differentiable programming feature](https://forums.swift.org/t/differentiable-programming-mega-proposal/28547) (informally referred to as "AutoDiff") to the 'master' branch for code reviews and better maintenance.

Upstreaming task: [TF-879](https://bugs.swift.org/browse/TF-879)
2019-10-05 12:14:49 -07:00
Robert Widmann
5a8d0744c3 [NFC] Adopt TypeBase-isms for GenericSignature
Structurally prevent a number of common anti-patterns involving generic
signatures by separating the interface into GenericSignature and the
implementation into GenericSignatureBase.  In particular, this allows
the comparison operators to be deleted which forces callers to
canonicalize the signature or ask to compare pointers explicitly.
2019-09-30 14:04:36 -07:00
Jordan Rose
a6dd630ca3 Eliminate Builtin.UnknownObject as an AST type (#27378)
This removes it from the AST and largely replaces it with AnyObject
at the SIL and IRGen layers. Some notes:

- Reflection still uses the notion of "unknown object" to mean an
  object with unknown refcounting. There's no real reason to make
  this different from AnyObject (an existential containing a
  single object with unknown refcounting), but this way nothing
  changes for clients of Reflection, and it's consistent with how
  native objects are represented.

- The value witness table and reflection descriptor for AnyObject
  use the mangling "BO" instead of "yXl".

- The demangler and remangler continue to support "BO" because it's
  still in use as a type encoding, even if it's not an AST-level
  Type anymore.

- Type-based alias analysis for Builtin.UnknownObject was incorrect,
  so it's a good thing we weren't using it.

- Same with enum layout. (This one assumed UnknownObject never
  referred to an Objective-C tagged pointer. That certainly wasn't how
  we were using it!)
2019-09-26 17:48:04 -07:00
taylorswift
87a6ba21a4 Merge remote-tracking branch 'upstream/master' into comparable-enums 2019-09-19 19:32:58 -05:00
taylorswift
08aa01f9aa fix compiler crash 2019-09-18 18:46:16 -05:00
Brent Royal-Gordon
eb27a59ef2 [NFC] Add and use ASTContext::getNSCopyingDecl() 2019-09-18 15:56:16 -07:00
Slava Pestov
0405ab5ffc AST: GenericContexts store a GenericSignature instead of a GenericEnvironment
This eliminates the entire 'lazy generic environment' concept;
essentially, all generic environments are now lazy, and since
each signature has exactly one environment, their construction
no longer needs to be co-ordinated with deserialization.
2019-09-06 17:16:04 -04:00