Commit Graph

134 Commits

Author SHA1 Message Date
Richard Wei
85406d26d6 [AutoDiff] Update availability in differentiable function metadata test. 2021-07-09 02:30:27 -07:00
Richard Wei
b343cdf21b [AutoDiff] Re-enable a reflection test.
rdar://75916878 is no longer reproducible.
2021-06-30 16:58:05 -07:00
Brad Larson
0f884b8b82 [AutoDiff] [SR-14218] Correctly propagate tangent vectors of inout parameters from functions with multiple basic blocks (#37861)
* Attempt at a patch for SR-14053 and SR-14218, based on Dan Zheng's initial fix.

* Incorporated Dan's cleanup suggestions.

* Converting a bool SmallVector to a SmallBitVector.

* Testing if Windows issues with this test are due to runtime support.

* Simplifying test case.
2021-06-24 08:03:14 -05:00
Mishal Shah
23c3b15f5f Support Xcode 13 beta
* Updating availability versions
* Remove all remaining overlays in stdlib/public/Darwin/*:
   - ObjectiveC
   - Dispatch
   - CoreFoundation
   - CoreGraphics
   - Foundation
2021-06-07 12:04:31 -07:00
Doug Gregor
f3f0b936d1 [AutoDiff] Fix reflection and metadata IRGen for differentiable function types 2021-06-03 11:59:49 -07:00
Nate Chandler
b3997be85b [Test] Disabled several AutoDiff tests for back_deployment_runtime.
rdar://76566029
2021-04-13 15:18:05 -07:00
Richard Wei
fb66de6126 Unify mangling operators for async, @Sendable, @differentiable and @noDerivative.
Repurpose mangling operator `Y` as an umbrella operator that covers new attributes on function types. Free up operators `J`, `j`, and `k`.

```
async ::= 'Ya'                             // 'async' annotation on function types
sendable ::= 'Yb'                          // @Sendable on function types
throws ::= 'K'                             // 'throws' annotation on function types
differentiable ::= 'Yjf'                   // @differentiable(_forward) on function type
differentiable ::= 'Yjr'                   // @differentiable(reverse) on function type
differentiable ::= 'Yjd'                   // @differentiable on function type
differentiable ::= 'Yjl'                   // @differentiable(_linear) on function type
```

Resolves rdar://76299796.
2021-04-07 17:49:10 -07:00
Richard Wei
82886bf77a [AutoDiff] Fix mangling of '@noDerivative' in function types.
`@noDerivative` was not mangled in function types, and was resolved incorrectly when there's an ownership specifier. It is fixed by this patch with the following changes:

* Add `NoDerivative` demangle node represented by a `k` operator.
    ```
    list-type ::= type identifier? 'k'? 'z'? 'h'? 'n'? 'd'?  // type with optional label, '@noDerivative', inout convention, shared convention, owned convention, and variadic specifier
    ```
* Fix `NoDerivative`'s overflown offset in `ParameterTypeFlags` (`7` -> `6`).
* In type decoder and type resolver where attributed type nodes are processed, add support for nested attributed nodes, e.g. `inout @noDerivative T`.
* Add `TypeResolverContext::InoutFunctionInput` so that when we resolve an `inout @noDerivative T` parameter, the `@noDerivative T` checking logic won't get a `TypeResolverContext::None` set by the caller.

Resolves rdar://75916833.
2021-04-07 15:42:12 -07:00
Karoy Lorentey
3532ead7bb [test] Replace wholesale disabling with a runtime version check 2021-03-30 22:55:46 -07:00
Karoy Lorentey
2255b6059f [test] Skip a @differentiable test during back deployment testing
This currently fails when run on the macOS 10.14.4 stdlib:

```
[ RUN      ] FunctionTypeMetadata.Reflect differentiable function type
stdout>>> check failed at /Users/buildslave/jenkins/workspace/fs-swift-main-tools_RA-stdlib_RA-macOS-swift-in-OS-10.14.4/swift/test/AutoDiff/validation-test/function_type_metadata.swift, line 10
stdout>>> expected: "@differentiable(reverse) (Swift.Float) -> Swift.Float" (of type Swift.String)
stdout>>> actual: "-3.8442426e-13" (of type Swift.String)
stderr>>> CRASHED: SIGSEGV
the test crashed unexpectedly
[     FAIL ] FunctionTypeMetadata.Reflect differentiable function type
```

rdar://76039256
2021-03-30 22:49:38 -07:00
Richard Wei
d997526948 Fix function differentiability kind metadata and mangling. (#36601)
* Move differentiability kinds from target function type metadata to trailing objects so that we don't exhaust all remaining bits of function type metadata.
  * Differentiability kind is now stored in a tail-allocated word when function type flags say it's differentiable, located immediately after the normal function type metadata's contents (with proper alignment in between).
  * Add new runtime function `swift_getFunctionTypeMetadataDifferentiable` which handles differentiable function types.
* Fix mangling of different differentiability kinds in function types. Mangle it like `ConcurrentFunctionType` so that we can drop special cases for escaping functions.
    ```
    function-signature ::= params-type params-type async? sendable? throws? differentiable? // results and parameters
    ...
    differentiable ::= 'jf'                    // @differentiable(_forward) on function type
    differentiable ::= 'jr'                    // @differentiable(reverse) on function type
    differentiable ::= 'jd'                    // @differentiable on function type
    differentiable ::= 'jl'                    // @differentiable(_linear) on function type
    ```

Resolves rdar://75240064.
2021-03-30 09:59:06 -07:00
Vojta Molda
57516a908e [AutoDiff] Add tests exercising zero derivative of Array.DifferentiableView.+ and .move
Tests for false positive precondition triggered by a .zero gradient, i.e. empty [] array, back-propagated through concatenation and move methods
Implements a simplified reproducer of SR-14297
2021-03-19 17:18:12 -05: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
8bc6143a4c [AutoDiff] Rename 'move(along:)' to 'move(by:)'.
Rename `move(along:)` to `move(by:)` based on the proposal feedback. The main argument for the change is that tangent vectors specify both a direction and a magnitude, whereas `along:` does not indicate that `self` is being moved by the specified magnitude.
2021-02-23 21:45:01 -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
e3db926e0c [AutoDiff] Remove '_Differentiable.zeroTangentVectorInitializer'. (#35329)
Remove `_Differentiable.zeroTangentVectorInitializer` to address the feedback on the [proposal thread](https://forums.swift.org/t/differentiable-programming-for-gradient-based-machine-learning/42147). The corresponding change has already been made in the [proposal](https://github.com/rxwei/swift-evolution/blob/autodiff/proposals/0000-differentiable-programming.md).

Removed components:
- `zeroTangentVectorInitializer` and `zeroTangentVector` in `Differentiable`, `Array`, `Optional`, `Float`, `Double`, `Float80`, and SIMD types.
- `zeroTangentVectorInitializer` synthesis logic in `Differentiable` derived conformances.
2021-01-20 10:45:03 -08:00
Saleem Abdulrasool
edad6810e1 test: explicitly reference SR (NFC)
Adjust the disabled tests to explicitly reference the SR.  This cleans
up the previous change to enable the autodifferentiation CI tests on
Windows.
2021-01-13 13:17:38 -08:00
Saleem Abdulrasool
d5fadcca5f Merge pull request #35384 from compnerd/windows-differential
CI: disable 4 tests, enable autodiff tests on Windows
2021-01-13 13:15:06 -08:00
Saleem Abdulrasool
0511af57f5 CI: disable 4 tests, enable autodiff tests on Windows
This disables 4 tests:
  - DerivativeRegistrationTests.NonCanonicalizedGenericSignatureComparison
  - Reabstraction.diff param generic => concrete
  - Reabstraction.diff param and nondiff param generic => concrete
  - Reabstraction.result generic => concrete

Simultaneously, enable the remainder of the auto-diff test suite on
Windows.  These tests fail on Windows due to an invalid parameter during
the reabstraction of the generic differentiable parameters.  The
remainder of the auto-differentiation tests pass on all the platforms.
2021-01-12 14:42:50 -08:00
Richard Wei
f79391b684 [AutoDiff] Remove 'differentiableFunction(from:)' and 'linearFunction(from:)'.
Remove unused APIs `differentiableFunction(from:)` and `linearFunction(from:)`. They were never official APIs, are not included in the [initial proposal](https://github.com/rxwei/swift-evolution/blob/autodiff/proposals/0000-differentiable-programming.md#make-a-function-differentiable-using-derivative), and are unused by existing supported client libraries (SwiftFusion and S4TF). Most importantly, they block crucial optimizations on linear map closures (#34935) and would need nontrivial work in SILGen to support.
2021-01-12 11:58:48 -08:00
Richard Wei
ffe6064101 Mangle derivative functions and linear maps.
- `Mangle::ASTMangler::mangleAutoDiffDerivativeFunction()` and `Mangle::ASTMangler::mangleAutoDiffLinearMap()` accept original function declarations and return a mangled name for a derivative function or linear map. This is called during SILGen and TBDGen.
- `Mangle::DifferentiationMangler` handles differentiation function mangling in the differentiation transform. This part is necessary because we need to perform demangling on the original function and remangle it as part of a differentiation function mangling tree in order to get the correct substitutions in the mangled derivative generic signature.

A mangled differentiation function name includes:
- The original function.
- The differentiation function kind.
- The parameter indices for differentiation.
- The result indices for differentiation.
- The derivative generic signature.
2021-01-07 02:21:10 -08:00
Marc Rasi
0142e524e1 [AutoDiff] generated linear maps should be "convention(thin)" 2020-12-28 16:50:34 -08:00
Dan Zheng
a8a95d0a75 [AutoDiff upstream] Enable @differentiable on setters. (#35133)
Enable `@differentiable` attribute on setters of properties and
subscripts in `Differentiable`-conforming types.

Add automatically-differentiated `@differentiable` setter test.

Resolves TF-1166.
2020-12-17 14:05:22 -05:00
Richard Wei
8d8614058b [AudoDiff] NFC: Replace 'SILAutoDiffIndices' with 'AutoDiffConfig'. (#35079)
Resolve rdar://71678394 / SR-13889.
2020-12-14 14:32:40 -08:00
Arnold Schwaighofer
27a4e824c2 The runtime function swift_autoDiffCreateLinearMapContext was recently added
So these tests fail with missing symbols if the test is deployed with stdlib's on older OSes

rdar://71900166
2020-12-02 11:45:22 -08:00
Saleem Abdulrasool
627046d849 test: repair Autodiff/validation-test/optional-property on Win32
The test relies on the module name being `null`.  This is implied by the
`-o /dev/null`.  However, that is not guaranteed.  Explicitly use the
desired module name.
2020-11-23 11:52:42 -08:00
Ben Langmuir
a0c5ffbacd [test] Temporarily disable AutoDiff tests failing with optimizations
These recently started failing when optimized. Disable while we
investigate and fix.

rdar://71642726
2020-11-20 19:47:32 -08:00
Saleem Abdulrasool
2fc5cbdc14 stdlib: remove swiftMSVCRT, replace with swiftCRT on Windows
This replaces swiftMSVCRT with swiftCRT.  The big difference here is
that the `visualc` module is no longer imported nor exported.  The
`visualc` module remains in use for a singular test wrt availability,
but this should effectively remove the need for the `visualc` module.

The difference between the MSVCRT and ucrt module was not well
understood by most.  MSVCRT provided ucrt AND visualc, combining pieces
of the old MSVCRT and the newer ucrt.  The ucrt module is what you
really wanted most of the time, however, would need to use MSVCRT for
the convenience aliases for type-generic math and the deprecated math
constants.

Unfortunately, we cannot shadow the `ucrt` module and create a Swift SDK
overlay for ucrt as that seems to result in circular dependencies when
processing the `_Concurrency` module.

Although this makes using the C library easier for most people, it has a
more important subtle change: it cleaves the dependency on visualc.
This means that this enables use of Swift without Visual Studio for the
singular purpose of providing 3 header files.  Additionally, it removes
the need for the installation of 2 of the 4 support files.  This greatly
simplifies the deployment process on Windows.
2020-10-15 16:02:01 -07:00
Dan Zheng
cfd75a1667 [AutoDiff] Fix forward-mode differentiation ownership verification failure. (#33898)
Previously, `LinearMapInfo::shouldDifferentiateInstruction` had a special case
for `copy_value`, returning true for `copy_value` instructions with an active
operand.

This is unexpected and led to "leaked owned value" ownership verification
failures due to unnecessarily cloned `copy_value` instructions during
differential generation.

Now, the special case is removed, fixing the failures.
`shouldDifferentiateInstruction` returns true for `copy_value` instructions
whose operand and result are both active.

Resolves SR-13530.
2020-09-10 18:49:04 -07:00
Alex Efremov
a532756cd6 [AutoDiff] Split forward_mode.swift test into separate files. (#33728)
Splits the large `forward_mode.swift` test file into 3 files:

- `forward_mode_simple.swift`
- `forward_mode_array.swift`
- `forward_mode_simd.swift`

This significantly speeds up testing time when tests are run in parallel.
2020-09-09 20:49:12 -07:00
Dan Zheng
9de171a51f [AutoDiff] Add missing REQUIRES: asserts to test. (#33874)
`test/AutoDiff/validation-test/optional-property.swift` uses
`-Xllvm -debug-only=differentiation`, which requires assertions enabled.
2020-09-09 15:20:01 -07:00
Alex Efremov
0ceaa5899c [AutoDiff] Fix forward-mode crashes related to tangent buffers (#33633)
Fixes foward-mode crashed related to:
- Missing tangent buffers for non-wrt `inout` parameters.
- Tangent buffers not being initialized due to the corresponding original
  buffer intialization instructions being non-active.
- Non-varied indirect results not being initialized.
- `emitDestroyValue` crashes due to `TangentVector` value category mismatch.

Resolves TF-984 and SR-13447.

Co-authored-by: Dan Zheng <danielzheng@google.com>
2020-09-08 18:15:48 -07:00
Dan Zheng
5c3cb36a7c Merge pull request #33611 from dan-zheng/autodiff-fix-tangent-value-category
[AutoDiff] Fix PullbackCloner tangent value category mismatch issues.
2020-09-03 11:51:56 -07:00
Richard Wei
76d0648e60 [AutoDiff] [Sema] Include certain 'let' properties in 'Differentiable' derived conformances. (#33700)
In `Differentiable` derived conformances, `let` properties are currently treated as if they had `@noDerivative` and excluded from the derived `Differentiable` conformance implementation. This is limiting to properties that have a non-mutating `move(along:)` (e.g. class properties), which can be mathematically treated as differentiable variables.

This patch changes the derived conformances behavior such that `let` properties will be included as differentiable variables if they have a non-mutating `move(along:)`. This unblocks the following code:

```swift
final class Foo: Differentiable {
   let x: ClassStuff // Class type with a non-mutating 'move(along:)'

   // Synthesized code:
   //   struct TangentVector {
   //     var x: ClassStuff.TangentVector
   //   }
   //   ...
   //   func move(along direction: TangentVector) {
   //     x.move(along: direction.x)
   //   }
}
```

Resolves SR-13474 (rdar://67982207).
2020-08-29 21:46:58 -07:00
Dan Zheng
48c048f34d [AutoDiff] Fix PullbackCloner tangent value category mismatch issues.
Fix SIL pullback function type calculation: remap original `unowned` results to
the correct pullback parameter convention depending on the `TangentVector` type
lowering.

Make `PullbackCloner` visitors for the following instructions check and handle
tangent value categories: `struct`, `struct_extract`, `tuple`, `destructure_tuple`.

Add differentiation tests for `Optional` struct and class stored properties,
exercising the instruction visitors above.

Resolves SR-13430.
2020-08-24 10:39:23 -07:00
Alex Efremov
83b2ebe8f1 [AutoDiff] Support forward mode differentiation of functions with inout parameters (#33584)
Adds forward mode support for `apply` instruction with `inout` arguments.

Example of supported code:
```
func add(_ x: inout Float, _ y: inout Float) -> Float {
  var result = x
  result += y
  return result
}
print(differential(at: 1, 1, in: add)(1, 1)) // prints "2"
```
2020-08-21 17:57:39 -07:00
Dan Zheng
e2a1862870 [AutoDiff upstream] Add inout parameter differentiation tests. (#33555) 2020-08-19 17:05:52 -07:00
Alex Efremov
40c448514a [AutoDiff] Support differentiation of non-active try_apply. (#33483)
Add differentiation support for non-active `try_apply` SIL instructions.

Notable pullback generation changes:
* Original basic blocks are now visited in a different order:
* starting from the original basic block, all its predecessors
* are visited in a breadth-first search order. This ensures that
* all successors of any block are visited before the block itself.

Resolves TF-433.
2020-08-15 10:22:28 -07:00
Dan Zheng
4391f4264e [AutoDiff] Fix Optional differentiation crash. (#33386)
Fix `Optional` differentiation crash for non-resilient `Wrapped` reference type.
Add `NonresilientTracked` type to `DifferentiationUnittest` for testing.

Resolves SR-13377.
2020-08-11 09:14:12 -07:00
Alex Efremov
ccca08b7f7 [AutoDiff] Add reverse-mode switch_enum support for Optional. (#33218)
Pullback generation now supports `switch_enum` and `switch_enum_addr`
instructions for `Optional`-typed operands.

Currently, the logic is special-cased to `Optional`, but may be generalized in
the future to support enums following general rules.
2020-08-07 17:56:15 -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
8e7dc70fa8 [AutoDiff] NFC: Garden test. (#33263)
Remove unused commented lines.
2020-08-03 23:19:38 -07:00
Rintaro Ishizaki
8ac2abc81c [Tests] Guard Float80 usage in AutoDiff test
Float80 is avaialble in limited platforms

rdar://problem/66409898
2020-07-31 23:26:14 -07:00
Dan Zheng
9fe0ed24be [AutoDiff] Fix class differentiation. (#33151)
Fix pullback generation for `unchecked_ref_cast` and `upcast` instructions
for class types with an address tangent value category.

Upstream end-to-end class differentiation tests.
2020-07-30 11:22:42 -07:00
Alex Efremov
17eafafbd2 [AutoDiff] [stdlib] Fix a bug in _jvpMultiply in SIMDDifferentiation.swift.gyb (#32963) 2020-07-23 14:25:57 -07:00
Dan Zheng
b6c3ab1e58 [AutoDiff] Disable failing test: SR-13250. (#32981) 2020-07-19 10:05:07 -07:00
Mike Ash
78594e9437 [Test] Mark differentiable_protocol_requirements as requiring executable_test 2020-07-17 17:17:47 -04:00
Dan Zheng
3b8b632687 [AutoDiff upstream] Add @differentiable protocol requirement tests. (#32939) 2020-07-16 21:22:17 -07:00
Alex Efremov
cff144af13 [AutoDiff][stdlib] Add JVPs to SIMDDifferentiation.swift.gyb (#32854) 2020-07-14 12:59:36 -07:00
Alex Efremov
bfcf12bac6 [AutoDiff][stdlib] Add JVPs to ArrayDifferentiation.swift (#32840)
Adds JVPs to `Array` and `Array.DifferentiableView`

`_jvpDifferentiableReduce` is taken from #29324
2020-07-12 15:04:33 -07:00