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.
Enable `@differentiable` attribute on setters of properties and
subscripts in `Differentiable`-conforming types.
Add automatically-differentiated `@differentiable` setter test.
Resolves TF-1166.
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.
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`.
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 }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
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.
`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.
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.
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.
`getModuleScopeContext()` can produce a `ModuleDecl *` instead of a `FileUnit *`, which happens to be the case for generic-specialized derivative functions.
Resolves rdar://71191415.
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.
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.
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.
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.
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.
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.
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.
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>
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).
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).
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.
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"
```