Files
swift-mirror/test/AutoDiff/Serialization/transpose_attr.swift
Dan Zheng 1308fc69c5 [AutoDiff] Simplify conditions enabling differentiable programming. (#30765)
Previously, two conditions were necessary to enable differentiable programming:
- Using the `-enable-experimental-differentiable-programming` frontend flag.
- Importing the `_Differentiation` module.

Importing the `_Differentiation` module is the true condition because it
contains the required compiler-known `Differentiable` protocol. The frontend
flag is redundant and cumbersome.

Now, the frontend flag is removed.
Importing `_Differentiation` is the only condition.
2020-04-02 03:24:03 -07:00

95 lines
2.2 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -emit-module -parse-as-library -o %t
// RUN: llvm-bcanalyzer %t/transpose_attr.swiftmodule | %FileCheck %s -check-prefix=BCANALYZER
// RUN: %target-sil-opt -disable-sil-linking -enable-sil-verify-all %t/transpose_attr.swiftmodule -o - | %FileCheck %s
// BCANALYZER-NOT: UnknownCode
import _Differentiation
// Dummy `Differentiable`-conforming type.
struct S: Differentiable & AdditiveArithmetic {
static var zero: S { S() }
static func + (_: S, _: S) -> S { S() }
static func - (_: S, _: S) -> S { S() }
typealias TangentVector = S
}
// Test top-level functions.
func top1(_ x: S) -> S {
x
}
// CHECK: @transpose(of: top1, wrt: 0)
@transpose(of: top1, wrt: 0)
func transposeTop1(v: S) -> S {
v
}
func top2<T, U>(_ x: T, _ i: Int, _ y: U) -> U {
y
}
// CHECK: @transpose(of: top2, wrt: (0, 2))
@transpose(of: top2, wrt: (0, 2))
func transposeTop2<T, U>(_ int: Int, v: U) -> (T, U)
where T: Differentiable, U: Differentiable,
T == T.TangentVector, U == U.TangentVector {
(.zero, v)
}
// Test instance methods.
extension S {
func instanceMethod(_ other: S) -> S {
self + other
}
// CHECK: @transpose(of: instanceMethod, wrt: 0)
@transpose(of: instanceMethod, wrt: 0)
func transposeInstanceMethod(t: S) -> S {
self + t
}
// CHECK: @transpose(of: instanceMethod, wrt: self)
@transpose(of: instanceMethod, wrt: self)
static func transposeInstanceMethodWrtSelf(_ other: S, t: S) -> S {
other + t
}
}
// Test static methods.
extension S {
static func staticMethod(x: S) -> S {
x
}
// CHECK: @transpose(of: staticMethod, wrt: 0)
@transpose(of: staticMethod, wrt: 0)
static func transposeStaticMethod(t: S) -> S {
t
}
}
// Test computed properties.
extension S {
var computedProperty: S { self }
// CHECK: @transpose(of: computedProperty, wrt: self)
@transpose(of: computedProperty, wrt: self)
static func transposeProperty(t: Self) -> Self {
t
}
}
// Test subscripts.
extension S {
subscript<T: Differentiable>(x: T) -> Self { self }
// CHECK: @transpose(of: subscript, wrt: self)
@transpose(of: subscript(_:), wrt: self)
static func transposeSubscript<T: Differentiable>(x: T, t: Self) -> Self {
t
}
}