Commit Graph

117 Commits

Author SHA1 Message Date
Ben Barham
c163e0fe5e [Tests] Make OS features consistent
lit.py currently allows any substring of `target_triple` to be used as a
feature in REQUIRES/UNSUPPORTED/XFAIL. This results in various forms of
the OS spread across the tests and is also somewhat confusing since they
aren't actually listed in the available features.

Modify all OS-related features to use the `OS=` version that Swift adds
instead. We can later remove `config.target_triple` so that these don't
the non-OS versions don't work in the first place.
2022-05-20 19:51:23 -07:00
Anton Korobeynikov
4f05096594 Propagate location from destination alloca in salvageDebugInfo(). (#58763)
`salvageDebugInfo` is called during SIL Mem2Reg and could produce misleading debug info in the following case:

```
%a = alloc_stack $MyModel.TangentVector, var, name "self", argno 1, implicit, loc "debug2.swift":37:17 ...
...
store %b to %a : $*MyModel.TangentVector
```

Such SIL could be created as a result of inlining (where store comes from the inlined function).
Before this patch we'd end with `debug_value` instruction with variable information, but without or incorrect location.

This caused LLVM IR debug info verifier assertions when there might be another instruction with complete debug info (including location) for the same argument.

After this patch we always reuse it from destination alloca

Fixes #58660
2022-05-11 13:28:23 -07:00
Philip Turner
61a352486e Create sr15884-generic-signature-builder-keypath-iterable.swift 2022-04-29 16:07:37 -04:00
Josh Soref
e8b5204bd0 Spelling autodiff (#42546)
* spelling: differentiated

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: initialization

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: multiplication

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: occasionally

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overriding

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* Rename test

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-25 09:01:37 -07:00
Anton Korobeynikov
c4ebb3249f Properly transfer location info from debug_value into alloc_stack: we need to drop op_deref expression (#42245)
In order to transfer debug info from `debug_value` to `alloc_stack` we need to drop `op_ref` debug expression, as the latter instruction represents the location and not the value itself. Also, fix the debug info verifier as variable type was deduced from `alloc_stack` / `alloc_box` improperly: pointer type of instruction itself was used instead of underlying object type.

The issue is only exposed when both optimizations (SIL mem2reg at least) and debug info are enabled

Resolves SR-15849
2022-04-21 08:03:21 -07:00
Philip Turner
2af1460ce6 [AutoDiff] Introduce regression test for SR-15849 (#41717) 2022-03-08 13:13:00 -08:00
Anton Korobeynikov
4b7baf000c [AutoDiff] Properly collect inout parameter adjoints (#41559)
Apparently, the parameter index calculation in #37861 was not always correct in presence of other pullback parameters (e.g. captures and non-differentiated args). Collect all inout parameters and collect inout parameter adjoints correctly.

Resolves SR-15891
2022-02-25 14:09:59 -08:00
Richard Wei
b0a038bd66 Merge pull request #41401 from asl/sr15205-fix
[AutoDiff] Fix several issues related to captured arguments
2022-02-16 16:45:10 -08:00
Richard Wei
7151608e21 Disable an autodiff test on Linux to unblock CI.
AutoDiff/compiler_crashers_fixed/rdar71191415-nested-differentiation-of-extension-method-optimized.swift is starting to fail recently. Tracked by rdar://89055298.
2022-02-16 16:34:10 -08:00
Anton Korobeynikov
91458b4890 Fix several issues related to captured arguments:
- Introduce a workaround while dealing with getLoweredParameterIndices() results:
    it operates on AST and therefore does not take into account captured arguments
    which are "on side" on AST and explicit in SIL
  - Ensure captured arguments (@inout_aliased) are handled in a same way as ordinary
    @inout arguments while generating a pullback type

Fixes SR-15205
2022-02-17 00:04:12 +03:00
Robert Widmann
3511314952 Disable Flaky Test
Disable an autodiff test that's causing the linker on Ubuntu 20.04 to crash every so often

rdar://87254800
2022-01-07 09:08:07 -08:00
Robert Widmann
e5885871e0 Disable Flaky Compiler Crasher Test
This test appears to cause the linker to crash every so often.

rdar://86041709
2022-01-05 13:31:32 -08:00
Richard Wei
cf2ccf1072 [AutoDiff] Use auto-generated locations for temporary allocations.
In `TangentBuilder` temporary allocations are emitted in order to make generic calls to `AdditiveArithmetic.zero` and `AdditiveArithmetic.+`. When we perform accumulation of two adjoint values that correspond to a lexical value in the original function, the stack allocations require a debug variable, as we use the original instruction's location. However such a debug variable wouldn't really make sense as these temporary allocations do not represent the original variable's adjoint. With lexical borrow scopes, we hit a crasher when creating temporary allocations when accumulating adjoints for lexical `begin_borrow` instructions.

This PR makes `TangentBuilder`'s temporary allocations use an auto-generated location. The existing debug info emission behavior is not changed, as we still emit `debug_value` when materializing an `AdjointValue` after any necessary adjoint accumulation.
2021-12-10 12:32:16 -08:00
Erik Eckstein
7caf9902e8 tests: disable a flaky AutoDiff test 2021-12-05 13:45:48 +01:00
Richard Wei
9bcba98213 Revert "Revert "[AutoDiff] Fix two derivative type calculation bugs caught by RequirementMachine""
This reverts commit 262965418f.
2021-11-05 10:29:08 -07:00
Richard Wei
1a4ea67e2c [AutoDiff] Plumb witness derivative generic signatures through SILGen.
When SILGenWitnessTable creates a decl ref for the witness of a derivative function requirement, it is using the requirement's derivative generic signature in the resulting witness decl ref. This is wrong because the witness may have a different derivative generic signature than the requirement, leading to a crash. This bug was never discovered because GSB's dark magic made it "just work", until requirement machine.

The fix is to store the matched witness derivative generic signature in `Witness` during type checking, and during witness table generation, use the witness' generic signature to create a witness decl ref.

Resolves rdar://84716758, rdar://84213107 and rdar://84987079.
2021-11-05 10:28:33 -07:00
Doug Gregor
262965418f Revert "[AutoDiff] Fix two derivative type calculation bugs caught by RequirementMachine" 2021-11-04 15:56:48 -07: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
Richard Wei
808c7ec783 [AutoDiff] Fix two derivative type calculation bugs caught by RequirementMachine.
1. When calculating the differential type of an original function with an inout parameter and when the inout parameter has a type parameter, the inout parameter should get a generic parameter in the subst generic signature of the differential but it currently doesn't. This causes SILGen to attempt to reabstract the differential value in the JVP protocol witness thunk, whilst the generic signature is lacking requirements, leading to a requirement machine error. This patch fixes the calculation so that the JVP's result type (the differential type) always matches the witness thunk's result type.

    Wrong type:
    ```swift
             sil private [transparent] [thunk] [ossa] @... <τ_0_0 where τ_0_0 : Differentiable> (...) -> @owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.TangentVector, τ_0_0.TangentVector> {
               %6 = differentiable_function_extract [jvp] %5 : $@differentiable(reverse) @convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @noDerivative @inout τ_0_0, @noDerivative SR_13305_Struct) -> () // user: %7
    HERE ====> %7 = apply %6<τ_0_0>(%0, %1, %3) : $@convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @inout τ_0_0, SR_13305_Struct) -> @owned @callee_guaranteed @substituted <τ_0_0> (@in_guaranteed τ_0_0) -> @out τ_0_0 for <τ_0_0.TangentVector>
    ```

    Should be:
    ```swift
      %7 = apply %6<τ_0_0>(%0, %1, %3) : $@convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @inout τ_0_0, SR_13305_Struct) -> @owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.TangentVector, τ_0_0.TangentVector>
    ```

2. `TypeConverter::makeConstantInterfaceType` is not passing down the derivative generic signature to `SILFunctionType::getAutoDiffDerivativeFunctionType` for class methods, and this was caught by RequirementMachine during vtable emission. This patch fixes that.

Partially resolves rdar://82549134. The only remaining tests that require `-requirement-machine=off` are SILOptimizer/semantic_member_accessors_sil.swift and SILOptimizer/differentiation_diagnostics.swift which I will fix next. Then I'll do a proper fix for workaround #39416.
2021-09-30 20:51:36 -07:00
Slava Pestov
bbb71962f0 AutoDiff: Workaround for performing generic signature queries on the wrong signature 2021-09-23 13:24:40 -04:00
Slava Pestov
b57be6dbf1 Revert "AutoDiff: Disable requirement machine when building or testing Differentiation library"
This reverts commit 4f6ba29715.
2021-09-23 13:24:40 -04:00
Hamish Knight
7fd5d461fa Revert "[test] Disable a couple of AutoDiff tests" 2021-08-24 21:41:15 +01:00
Hamish Knight
9bec7c258f [test] Disable a couple of AutoDiff tests
Temporarily disable on non-macOS platforms until
the CI is fixed.

rdar://82240971
2021-08-23 13:52:51 +01:00
Holly Borla
71cbd24750 [Test] Mark sr14240-symbol-in-ir-file-not-tbd-file.swift to require
an executable test.
2021-08-13 09:11:20 -07:00
Brad Larson
4a86174868 [AutoDiff] [SR-14240] Align name mangling for Clang-imported declarations (#38850)
* Adding fix for handling Clang-imported names, along with test case.

* Correct EOL.
2021-08-12 17:09:37 -06:00
Slava Pestov
4f6ba29715 AutoDiff: Disable requirement machine when building or testing Differentiation library
The SIL type lowering logic for AutoDiff gets the substituted generic signature
mixed up with the invocation generic signature, so it tries to ask questions
about DependentMemberTypes in a signature with no requirements. This triggers
assertions when the requirement machine is enabled.

Disable the requirement machine until this is fixed.
2021-07-30 19:42:31 -04:00
Robert Widmann
e87a583cfd Disable Flaky AutoDiff Test
This test fails to link every once in a while.

See https://bugs.swift.org/browse/SR-14775
2021-06-14 13:01:13 -07:00
Brad Larson
2e73e708b5 Proposed fix, with test, for SR-14625's over-consume. 2021-06-03 21:12:30 -05:00
Pavel Prokofyev
841618ea70 [AutoDiff] Use correct debug scope for pullback trampoline block.
Fixes:
[[SR-14290]: SIL verification fails when differentiating a function of [[Double]]](rdar://74876596)
[[SR-14298]: [AutoDiff] SIL verification failed: non-contiguous lexical scope at -Onone](rdar://75032457)
2021-04-20 12:10:54 -07:00
Michael Gottesman
4c1d4a04ce [build-wrangler] Disable broken test. 2021-04-01 16:14:31 -07:00
David Zarzycki
00533e8712 [NFC] Change magic emoji to __ 2021-03-23 13:41:16 -04:00
Richard Wei
0b53a02544 [AutoDiff] Rename 'in:' to 'of:' in differential operators.
Rename the argument label `in:` in `gradient(at:in:)`, `pullback(at:in:)`, etc to `of:`, as suggested in the [pitch thread](https://forums.swift.org/t/differentiable-programming-for-gradient-based-machine-learning/42147).
2021-02-24 01:33:42 -05: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
e4f88b546f Merge pull request #35822 from rxwei/74087329-trampoline-debug-info
[AutoDiff] Fix DI verification failure with trampoline blocks in VJP.
2021-02-08 09:34:44 -08:00
Richard Wei
116baeae42 [AutoDiff] Fix DI verification failure with trampoline blocks in VJP.
- Set the builder's debug scope to the remapped debug scope in the overriden visiters.
- In trampoline blocks where we build predecessor enums, make the trampoline builder inherit the original terminator's remapped debug scope.

Resolves rdar://74087329.
2021-02-08 04:00:50 -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
Dan Zheng
126f1ac6fb [AutoDiff] Disable differentiable_function_extract explicit type as… (#35239)
`differentiability_function_extract` instruction has an optional explicit
extractee type. This is currently used by TypeSubstCloner and the
LoadableByAddress transform to rewrite `differentiability_function_extract`
instructions while preserving `@differentiable` function type invariants.

There is an assertion that `differentiability_function_extract` instructions do
not have explicit extractee types outside of canonical/lowered SIL. However,
this does not handle the SIL deserialization case above: when a function
containing a `differentiable_function_extract` instruction with an explicit type
is deserialized into a raw SIL module (which happens when optimizations are
enabled).

Removing the assertion unblocks this encountered use case.

A more robust longer-term solution may be to change SIL `@differentiable`
function types to explicitly store component original/JVP/VJP function types.

Also fix `differentiable_function_extract` extractee type serialization.

Resolves SR-14004.
2021-01-04 18:40:11 -05:00
Dan Zheng
f2de54678b [AutoDiff] Disable flaky test on Linux.
Disable test that occasionally flake on Linux.

SR-13021 tracks re-enabling related failing AutoDiff tests.
2020-12-23 18:54:04 -05: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
Dan Zheng
2cf7d63a5e [AutoDiff] Fix ownership error in VJPCloner::visitApplyInst. (#35003)
Change the following code pattern:

```
%x1 = convert_function %x0 : $@differentiable (...) -> ...
%x2 = begin_borrow %x1
... // use %x2
%x3 = end_borrow %x2
destroy_value %x0
```

To the following:

```
%x1 = begin_borrow %x0 : $@differentiable (...) -> ...
%x2 = convert_function %x0
... // use %x2
%x3 = end_borrow %x1
destroy_value %x0
```

Resolves SR-13933: "multiple consuming users" ownership error caused by
`VJPCloner::visitApply` related to `@differentiable`-function-typed callees.

Also upstream test/AutoDiff/SILOptimizer/generics.swift from tensorflow branch.
2020-12-08 19:48:31 -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
Richard Wei
517bcc4493 [AutoDiff] Fix differentiation transform crashers in library evolution mode. (#34704)
AD-generated data structures (linear map structs and branching trace enums) do not need to be resilient data structures. These decls ade missing a `@frozen` attribute.

Resolves rdar://71319547.
2020-11-12 13:30:56 -08:00
Richard Wei
c9dc67d383 [AST] Fix AutoDiff crasher in optimized builds.
`getModuleScopeContext()` can produce a `ModuleDecl *` instead of a `FileUnit *`, which happens to be the case for generic-specialized derivative functions.

Resolves rdar://71191415.
2020-11-10 16:50:21 -08:00
Dan Zheng
566a671aa3 [AutoDiff] Fix pullback subset thunk generation crash. (#33968)
Fix pullback subset thunk generation crash due to unmapped parameter index
for `inout` differentiability parameters.

Resolves TF-1315.
2020-09-16 14:40:42 -07:00
Alex Efremov
a434ee8598 [AutoDiff] Fix PullbackCloner tangent value category mismatch crash (#33512)
Fix semantic member getter pullback generation crash due to tangent value category mismatch.

Resolves SR-13411.
2020-08-17 18:31:12 -07:00
Dan Zheng
849c63634c [AutoDiff] Fix differentiation for non-wrt inout parameters. (#33304)
Fix SIL differential function type calculation to handle non-wrt `inout`
parameters.

Patch `SILFunctionType::getDifferentiabilityResultIndices` to prevent returning
empty result indices for `@differentiable` function types with no formal results
where all `inout` parameters are `@noDerivative`. TF-1305 tracks a robust fix.

Resolves SR-13305.

Exposes TF-1305: parameter/result differentiability hole for `inout` parameters.
2020-08-05 09:34:06 -07: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
Dan Zheng
b9341c8713 [AutoDiff] Fix protocol witness SILGen for @differentiable class methods. (#32639)
During protocol witness SILGen for `@differentiable` class methods,
replace the `AutoDiffDerivativeFunctionIdentifier` generic signature
with the witness thunk substitution map's generic signature.

Resolves TF-1180: vtable SIL verification error.
2020-07-01 08:34:18 -07:00
Dan Zheng
542721bbf4 [AutoDiff] Disable flaky tests on Linux. (#32394)
Disable tests that occasionally flake on Linux.

SR-13021 tracks re-enabling these tests.
2020-06-15 18:28:07 -07:00
Dan Zheng
ff97ae798d [AutoDiff] Fix SIL locations and debug scopes.
Fix SIL locations and debug scopes in `VJPEmitter` for:
- Pullback struct `struct` instructions
- Predecessor enums `enum` instructions

These instructions are not directly cloned from the original function and should
have auto-generated locations.

Resolves SR-12887: debug scope error for `VJPEmitter`-generated function.
2020-05-29 02:06:42 -07:00