Commit Graph

656 Commits

Author SHA1 Message Date
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
swift_jenkins
65c2c20305 Merge remote-tracking branch 'origin/main' into next 2020-12-23 04:38:06 -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
swift_jenkins
fcec26a47c Merge remote-tracking branch 'origin/main' into next 2020-12-17 11:21:35 -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
swift_jenkins
20836fd582 Merge remote-tracking branch 'origin/main' into next 2020-12-14 14:33:29 -08:00
Richard Wei
8d8614058b [AudoDiff] NFC: Replace 'SILAutoDiffIndices' with 'AutoDiffConfig'. (#35079)
Resolve rdar://71678394 / SR-13889.
2020-12-14 14:32:40 -08:00
swift_jenkins
9550869d12 Merge remote-tracking branch 'origin/main' into next 2020-12-08 17:14:36 -08: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
swift_jenkins
b5f0ea4c18 Merge remote-tracking branch 'origin/main' into next 2020-12-03 22:28:07 -08:00
Anthony Latsis
413352b77c Merge pull request #34620 from AnthonyLatsis/parse-misc
Parse: Only diagnose dollar-prefixed identifiers that are Swift declarations
2020-12-04 09:09:45 +03:00
Arnold Schwaighofer
e49bccc2d1 More sret fixes 2020-12-03 10:15:24 -08:00
Arnold Schwaighofer
3ae4e6291e Merge pull request #34924 from aschwaighofer/fix_autodiff_test_failure_on_os_stdlib
The runtime function swift_autoDiffCreateLinearMapContext was recently added
2020-12-03 05:02:36 -08:00
Michael Gottesman
ec71713fb0 [ownership] Teach CanonicalizeInstruction how to eliminate trivial copies/borrows.
I am going to be using in inst-simplify/sil-combine/canonicalize instruction a
RAUW everything against everything API (*). This creates some extra ARC
traffic/borrows. It is going to be useful to have some simple peepholes that
gets rid of some of the extraneous traffic.

(*) Noting that we are not going to support replacing non-trivial
OwnershipKind::None values with non-trivial OwnershipKind::* values. This is a
corner case that only comes up with non-trivial enums that have a non-payloaded
or trivial enum case. It is much more complex to implement that transform, but
it is an edge case, so we are just not going to support those for now.

----

I also eliminated the dependence of SILGenCleanup on Swift/SwiftShims. This
speeds up iterating on the test case with a debug compiler since we don't need
those modules.
2020-12-02 17:25:13 -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
Michael Gottesman
3fd4aaeb37 Merge pull request #34898 from gottesmm/pr-d3b6d903b097410b535d20940005a1fa67d3a1bf
[auto-diff] Fix a bunch of places in the *Cloners where we were not closing borrow scopes.
2020-12-01 12:42:10 -08:00
marcrasi
4e048bfc35 Merge pull request #34893 from marcrasi/make-it-automatically-inherit
inherit required protocols during TangentVector synthesis
2020-11-30 23:51:40 -08:00
Marc Rasi
6378c3ef0e fix SourceLoc-related crasher and add tests 2020-11-30 16:47:24 -08:00
Michael Gottesman
affeba98f9 [auto-diff] Fix a bunch of places in the *Cloners where we were not closing borrow scopes.
These were all just trying to open a borrow scope, so I changed them to use the
API SILBuilder::emitScopedBorrowOperation(SILLocation, SIL).
2020-11-30 16:05:37 -08:00
Richard Wei
de2dbe57ed [AutoDiff] Bump-pointer allocate pullback structs in loops. (#34886)
In derivatives of loops, no longer allocate boxes for indirect case payloads. Instead, use a custom pullback context in the runtime which contains a bump-pointer allocator.

When a function contains a differentiated loop, the closure context is a `Builtin.NativeObject`, which contains a `swift::AutoDiffLinearMapContext` and a tail-allocated top-level linear map struct (which represents the linear map struct that was previously directly partial-applied into the pullback). In branching trace enums, the payloads of previously indirect cases will be allocated by `swift::AutoDiffLinearMapContext::allocate` and stored as a `Builtin.RawPointer`.
2020-11-30 15:49:38 -08:00
Saleem Abdulrasool
b9122ef428 Merge pull request #34866 from compnerd/not-so-optional-property
test: repair Autodiff/validation-test/optional-property on Win32
2020-11-30 08:32:02 -08:00
Richard Wei
16daae9fef [AutoDiff] [Sema] Reject function types where all parameters are '@noDerivative'. (#34861)
A `@differentiable` function type require at least 1 differentiability parameter. This PR adds a diagnostic that rejects cases where all parameters are marked with `@noDerivative`. This fixes a compiler crasher.

```swift
test2.swift:3:24: error: '@differentiable' function type requires at least one differentiability parameter, i.e. a non-'@noDerivative' parameter whose type conforms to 'Differentiable'
let _: @differentiable (@noDerivative Float) -> Float = { _ in 0 }
                       ^~~~~~~~~~~~~~~~~~~~~
test2.swift:4:32: error: '@differentiable' function type requires at least one differentiability parameter, i.e. a non-'@noDerivative' parameter whose type conforms to 'Differentiable' with its 'TangentVector' equal to itself
let _: @differentiable(linear) (@noDerivative Float, @noDerivative Int) -> Float = { _, _ in 0 }
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
2020-11-29 23:33:15 -08:00
Marc Rasi
9e9de1d6d9 inherit required protocols during TangentVector synthesis 2020-11-24 18:33:55 -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
Xiaodi Wu
59f1f10b77 Merge pull request #34799 from kkshinkai/main
Fix misspellings
2020-11-20 08:25:54 -05:00
Anthony Latsis
47ce1529f0 Parse: Only diagnose dollar-prefixed identifiers that are Swift declarations 2020-11-19 20:30:53 +03:00
Kk Shinkai
a0f0747283 Fix misspellings 2020-11-18 11:47:16 +08: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
Michael Gottesman
58d4191470 [ownership] Try harder to make sure we do not propagate ownership info when ownership is disabled.
Specifically, I made it so that assuming our instruction is inserted into a
block already that we:

1. Return a constraint of {OwnershipKind::Any, UseLifetimeConstraint::NonLifetimeEnding}.
2. Return OwnershipKind::None for all values.

Noticed above I said that if the instruction is already inserted into a block
then we do this. The reason why is that if this is called before an instruction
is inserted into a block, we can't get access to the SILFunction that has the
information on whether or not we are in OSSA form. The only time this can happen
is if one is using these APIs from within SILBuilder since SILBuilder is the
only place where we allow this to happen. In SILBuilder, we already know whether
or not our function is in ossa or not and already does different things as
appropriate (namely in non-ossa does not call getOwnershipKind()). So we know
that if these APIs are called in such a situation, we will only be calling it if
we are in OSSA already. Given that, we just assume we are in OSSA if we do not
have a function.

To make sure that no mistakes are made as a result of that assumption, I put in
a verifier check that all values when ownership is disabled return a
OwnershipKind::None from getOwnershipKind().

The main upside to this is this means that we can write code for both
OSSA/non-OSSA and write code for non-None ownership without needing to check if
ownership is enabled.
2020-11-11 18:56:59 -08:00
swift-ci
d74c261f09 Merge pull request #34681 from rxwei/71191415-debug-scope 2020-11-10 21:23:42 -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
Robert Widmann
363b66a7ad Fully Qualify the Parent Type When Diagnosing Missing Member Types
Use the FullyQualified<Type> abstraction from the prior commit plus DescriptiveDeclKind to give a bit more information when issuing a missing member type diagnostic during type resolution.
2020-11-09 17:10:18 -08:00
Richard Wei
e29c19cf8b [AutoDiff] [Sema] Fix '@differentiable' witness matching regression. (#34533)
When checking protocol conformances with `@differentiable` requirements, the type checker is supposed to accept omissions of `@differentiable` attributes when there exsits an attribute that covers a superset of the differentiation configuration. This was accidentally regressed in apple/swift#33776 which made the following test case fail to compile. This is fixed by adjusting the witness matching conditions.

```swift
// rdar://70348904 reproducer:
public protocol P: Differentiable {
    @differentiable(wrt: self)
    @differentiable(wrt: (self, x))
    func foo(_ x: Float) -> Float
}

public struct S: P {}

extension S {
    // This had worked until apple/swift#33776.
    @differentiable(wrt: (self, x))
    public func foo(_ x: Float) -> Float { x }
}
```

Also fix some suboptimal diagnostics where more information could be shown.

Resolves rdar://70348904.
2020-11-01 17:10:03 -08:00
Michael Gottesman
8c5737d1d5 [ownership] Change thin_to_thick function to always produce a none value.
It can already only accept values with none ownership and the merging of
ownership around ownership phis ensure that if we phi this with a partial_apply
or the like, we get the appropriate ownership on any such ownership phi values.

We are now out of SILGen emitting fewer destroy_value unnecessarily on
thin_to_thick functions. This changed some codegen and also forced me to update
some tests/fix AutoDiff.

I also deleted the DebugInfo test mandatoryinlining-wrongdebugscope.swift since:

1. It was depending on these destroys being there.

2. Given the need to improve the test @aprantl suggested I just eliminate it
solving the test failure for me.
2020-10-23 17:49:12 -07: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
9afad737eb [AutoDiff] [Sema] Limit implicit @differentiable attribute creation. (#33776)
During protocol witness matching for a protocol requirement with
`@differentiable` attributes, implicit `@differentiable` attributes may be
created for the witness under specific conditions (when the witness has a
`@differentiable` attribute with superset differentiability parameters, or
when the witness has less-than-public visibility).

Do not generate implicit `@differentiable` attributes for protocol witnesses
when the protocol conformance is declared from a separate file or type context
from the witness.

Resolves SR-13455.
2020-09-19 10:49:09 -07: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
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
Richard Wei
aa20633f4e [AutoDiff] [Sema] Fix missing access-level mismatch diagnostic in -enable-testing builds. (#33800)
When a derivative function is internal and its original function is
public, there should be an error when the derivative is not `@usableFromInline`.
This patch fixes a bug where the error does not appear in SwiftPM debug
build configurations (or any configurations with `-enable-testing`).

`ValueDecl::getEffectiveAccess()` should not be used when we perform
formal access checking for AutoDiff-related declaration attributes because
it will treat all internal declarations as public when `-enable-testing` is
enabled. Instea we should use a combination of `ValueDecl::getFormalAccess()`
and `ValueDecl::isUsableFromInline()`.

Also, add a `RUN` line with `-enable-testing` to all AutoDiff declaration
attribute test files.

Resolves SR-13500 (rdar://68336300).
2020-09-04 10:23:47 -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
83b50cd8e3 [AutoDiff] Add missing withoutDerivative(at:) fix-its. (#33660)
Add `withoutDerivative(at:)` fix-its for errors regarding non-differentiable
arguments and results.
2020-08-27 11:53:33 -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