Commit Graph

25 Commits

Author SHA1 Message Date
Daniil Kovalev
1a42a0ce5f [AutoDiff] Support curry thunks differentiation in fragile funcs (#77615)
Inside fragile functions, we expect function derivatives to be public, which could be achieved by either explicitly marking the functions as differentiable or having a public explicit derivative defined for them. This is obviously not
possible for single and double curry thunks which are a special case of `AutoClosureExpr`.

Instead of looking at the thunk itself, we unwrap it and look at the function being wrapped. While the thunk itself and its differentiability witness will not have public visibility, it's not an issue for the case where the function being wrapped (and its witness) have public visibility.

Fixes #54819
Fixes #75776
2025-02-17 14:43:50 -08:00
Slava Pestov
39b4bda1dc AST: Introduce SubstFlags::SubstituteLocalArchetypes 2024-08-21 14:23:37 -04:00
Ellie Shin
5ccc4cd394 SIL function can be serialized with different kinds: [serialized] or
[serialized_for_package] if Package CMO is enabled. The latter kind
allows a function to be serialized even if it contains loadable types,
if Package CMO is enabled. Renamed IsSerialized_t as SerializedKind_t.

The tri-state serialization kind requires validating inlinability
depending on the serialization kinds of callee vs caller; e.g. if the
callee is [serialized_for_package], the caller must be _not_ [serialized].
Renamed `hasValidLinkageForFragileInline` as `canBeInlinedIntoCaller`
that takes in its caller's SerializedKind as an argument. Another argument
`assumeFragileCaller` is also added to ensure that the calle sites of
this function know the caller is serialized unless it's called for SIL
inlining optimization passes.

The [serialized_for_package] attribute is allowed for SIL function, global var,
v-table, and witness-table.

Resolves rdar://128406520
2024-05-23 15:53:02 -07:00
Anthony Latsis
1b2e4f983e Gardening: Migrate test suite to GH issues: AutoDiff/compiler_crashers 2022-09-22 20:28:04 +03:00
Richard Wei
a1cb9bbcc9 [AutoDiff] Move differentiable curry thunk bug reproducer to an XFAIL test.
Due to rdar://87429620, test/AutoDiff/SILOptimizer/differentiation_diagnostics.swift is still using `-requirement-machine=off`. This patch moves the reproducer to a standalone XFAIL test, and removes `-requirement-machine=off` from differentiation_diagnostics.swift.
2022-01-11 18:01:43 -08:00
Richard Wei
6999c8a3dc [AutoDiff] Fix crasher when type-checking mismatched derivative.
When checking the viability of an original function candidate as specified in a `@derivative` attribute, a candidate's signautre can have more generic requirements than the required signature. Such cases need to be checked and diagnosed.

Resolves SR-15530 / rdar://85845512.
2021-11-30 19:04:10 -08:00
Richard Wei
90ec31a068 [AutoDiff] Compute derivative types using requirements from archetypes.
Resolves rdar://84213107 and partially resolves rdar://82549134.
2021-10-13 21:23:17 -07:00
astrotuna201
926a59c31c [AST][AutoDiff] Provide Lit test case for AutoDiff GenericTypeParamDecl AST verification failure. (#38745)
Provides reproducer lit test case for AutoDiff implicit auxiliary struct and enum
declarations that clone implicit GenericTypeParamDecls from a generic
signature into a flat GenericParamList, and lead to a compiler assert about GenericTypeParamDecl depth during merge module operation.

Related to https://github.com/apple/swift/pull/32343.
2021-08-16 22:40:52 -07:00
Richard Wei
e494df2ee6 [AutoDiff] Add differentiability kind to differentiability witnesses and mangle them.
Differentiability witnesses are now keyed by the original function name, the differentiability kind, and the autodiff config.

Updated SIL syntax:
```
differentiability-kind ::= 'forward' | 'reverse' | 'normal' | 'linear'
sil-differentiability-witness ::=
    'sil_differentiability_witness'
    sil-linkage?
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
    sil-differentiability-witness-body?
sil-instruction ::=
    'differentiability_witness_function'
    '[' sil-differentiability-witness-function-kind ']'
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
```
```console
sil_differentiability_witness [reverse] [parameters 0 1] [results 0] <T where T: Differentiable> @foo : <T> $(T) -> T
differentiability_witness_function [vjp] [reverse] [parameters 0] [results 0] <T where T: Differentiable> @foo : $(T) -> T
```

New mangling:
```swift
  global ::= global generic-signature? 'WJ' DIFFERENTIABILITY-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // differentiability witness
```
```console
$s13test_mangling3fooyS2f_S2ftFWJrSpSr ---> reverse differentiability witness for test_mangling.foo(Swift.Float, Swift.Float, Swift.Float) -> Swift.Float with respect to parameters {0} and results {0}
```

Resolves rdar://74380324.
2021-02-17 18:27:42 -05: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
Richard Wei
2f883a3599 [AutoDiff] Fix TBDGen issues for derivative dispatch thunks and method descriptors.
- Add `DispatchThunkDerivative` and `MethodDescriptorDerivative` as link entities. The derivative functions of initializers, subscripts, properties, and methods are **all methods**, so we don't need other link entities for this purpose.
- Mangle dispatch thunks and method descriptors. Make `AutoDiffFunction` a context node since it can be nested.

Resolves SR-13866 (rdar://71318828) and SR-13125 (rdar://65240599).
2021-01-30 16:47:00 -08:00
Dan Zheng
07f632acaa [AutoDiff] NFC: document test suite. (#35194)
Add README files explaining the differentiable programming test suite.

Add lit.local.cfg files so individual tests do not need to add
`REQUIRES: asserts` in these directories:

- test/AutoDiff/compiler_crashers
- test/AutoDiff/compiler_crashers_fixed

Motivation: it is important for compiler crasher tests to require assertions
enabled, because many of these tests crash on compiler assertions.
2020-12-23 07:19:19 -05:00
Richard Wei
35aa99bc1a [AutoDiff] Fix crasher on property getter in library evolution mode. (#34777)
`SILBuilder::createAllocStack` expects a debug variable when the location is a `VarDecl`. Since we are in pullbacks, there's no debug variables so we pass an empty one.

General support for debug-info-in-pullbacks will be added as part of SR-13535.

Also add a negative test for SR-13866.

Resolves SR-13865.
2020-11-17 14:35:19 -08:00
Dan Zheng
9c20198498 [AutoDiff] Disallow differentiation of opaque-result-typed functions. (#32714)
Reject `@differentiable` and `@derivative` attribute for original
functions with opaque result types.

It is not possible to support derivative registration nor the
differentiation transform for such functions.

Resolves SR-12656.
2020-07-05 21:09:34 -07:00
Slava Pestov
653fa07260 GSB: Fix maybeResolveEquivalenceClass() with member type of superclass-constrained type
Name lookup might find an associated type whose protocol is not in our
conforms-to list, if we have a superclass constraint and the superclass
conforms to the associated type's protocol.

We used to return an unresolved type in this case, which would result in
the constraint getting delayed forever and dropped.

While playing wack-a-mole with regressing crashers, I had to do some
refactoring to get all the tests to pass. Unfortuanately these refactorings
don't lend themselves well to being peeled off into their own commits:

- maybeAddSameTypeRequirementForNestedType() was almost identical to
  concretizeNestedTypeFromConcreteParent(), except for superclasses
  instead of concrete same-type constraints. I merged them together.

- We used to drop same-type constraints where the subject type was an
  ErrorType, because maybeResolveEquivalenceClass() would return an
  unresolved type in this case.

  This violated some invariants around nested types of ArchetypeTypes,
  because now it was possible for a nested type of a concrete type to
  be non-concrete, if the type witness in the conformance was missing
  due to an error.

  Fix this by removing the ErrorType hack, and adjusting a couple of
  other places to handle ErrorTypes in order to avoid regressing with
  invalid code.

Fixes <rdar://problem/45216921>, <https://bugs.swift.org/browse/SR-8945>,
<https://bugs.swift.org/browse/SR-12744>.
2020-05-19 20:28:51 -04:00
Dan Zheng
7d2f944086 [AutoDiff] Add TF-1181 negative test. (#31683)
Add negative test for TF-1181: differentiation transform crash for `apply` with
opened existential arguments.
2020-05-10 08:26:23 -07:00
Dan Zheng
e7f856f1ae [AutoDiff] Add SR-12744 negative test. (#31680)
Add negative test for SR-12744: pullback generation crash for unhandled
indirect result.
2020-05-09 15:20:40 -07:00
Dan Zheng
2ea4316838 [AutoDiff] Add SR-12656 negative test. (#31673)
Add negative test for SR-12656: differentiation transform crash for opaque
result types.
2020-05-09 10:01:40 -07:00
marcrasi
ce5e0e774c [AutoDiff] Fix SR-12641: Handle address-only types in derivative fn types (#31496)
* The update in `SILFunctionType.cpp` fixes SR-12641 by making address-only parameters/results in differentials have indirect convention.
* I updated the crasher test to use a resilient struct defined in the test, instead of `Tracked<Float>`, so that the test does not need to depend on `DifferentiationUnittest`.
* The update in `VJPEmitter.cpp` fixes a similar issue with pullbacks that I discovered while investigating.
* I added code that exposes this new issue to the SR-12641 crasher test.
2020-05-05 12:29:10 -07:00
marcrasi
207958c12f [AutoDiff] fix tests to pass on tensorflow branch (#31264) 2020-04-24 09:53:05 -07:00
Dan Zheng
1d4ba599f2 [AutoDiff] Add negative test for SR-12641. (#31207)
Add negative test exposing SR-12641: SILGen verification error regarding
reabstraction thunking for `@differentiable` functions.
2020-04-22 16:19:22 -07:00
Dan Zheng
f7a9eed4de [AutoDiff] Add generated implicit declarations to SynthesizedFileUnit.
Add implicit declarations generated by the differentiation transform to a
`SynthesizedFileUnit` instead of an ad-hoc pre-existing `SourceFile`.

Resolves TF-1232: type reconstruction for AutoDiff-generated declarations.

Previously, type reconstruction failed because retroactively adding declarations
to a `SourceFile` did not update name lookup caches.
2020-04-07 18:29:34 -07:00
Dan Zheng
52374bfcca Add TF-1232 negative test: proper mangling for AutoDiff-generated declarations.
IRGenDebugInfo crash due to lack of proper mangling for AutoDiff-generated
declarations: linear map structs and branching trace enums.
2020-04-05 21:39:38 -07:00
marcrasi
15f512b81b [AutoDiff] fix SR-12493 (#30817)
We simply needed to upstream `TypeSubstCloner::visitDifferentiableFunctionExtractInst`.
The code has a detailed comment explaining what it does.
2020-04-05 19:36:10 -07:00
Dan Zheng
c6f2d7ab0a [AutoDiff] Disable failing test with -O.
Disable test/AutoDiff/stdlib/differential_operators.swift, which currently fails
with `-O`.

SR-12493 tracks fixing the issue. Add negative test.
2020-04-02 13:06:35 -07:00