-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathderivative_attr.swift
106 lines (86 loc) · 2.73 KB
/
derivative_attr.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -emit-module -parse-as-library -o %t
// RUN: llvm-bcanalyzer %t/derivative_attr.swiftmodule | %FileCheck %s -check-prefix=BCANALYZER
// RUN: %target-sil-opt -disable-sil-linking -enable-sil-verify-all %t/derivative_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: @derivative(of: top1, wrt: x)
@derivative(of: top1, wrt: x)
func derivativeTop1(_ x: S) -> (value: S, differential: (S) -> S) {
(x, { $0 })
}
func top2<T, U>(_ x: T, _ i: Int, _ y: U) -> U {
y
}
// CHECK: @derivative(of: top2, wrt: (x, y))
@derivative(of: top2, wrt: (x, y))
func derivativeTop2<T: Differentiable, U: Differentiable>(
_ x: T, _ i: Int, _ y: U
) -> (value: U, differential: (T.TangentVector, U.TangentVector) -> U.TangentVector) {
(y, { (dx, dy) in dy })
}
// Test instance methods.
extension S {
func instanceMethod(_ x: S) -> S {
self
}
// CHECK: @derivative(of: instanceMethod, wrt: x)
@derivative(of: instanceMethod, wrt: x)
func derivativeInstanceMethodWrtX(_ x: S) -> (value: S, differential: (S) -> S) {
(self, { _ in .zero })
}
// CHECK: @derivative(of: instanceMethod, wrt: self)
@derivative(of: instanceMethod, wrt: self)
func derivativeInstanceMethodWrtSelf(_ x: S) -> (value: S, differential: (S) -> S) {
(self, { $0 })
}
// CHECK: @derivative(of: instanceMethod, wrt: (self, x))
@derivative(of: instanceMethod, wrt: (self, x))
func derivativeInstanceMethodWrtAll(_ x: S) -> (value: S, differential: (S, S) -> S) {
(self, { (dself, dx) in self })
}
}
// Test static methods.
extension S {
static func staticMethod(_ x: S) -> S {
x
}
// CHECK: @derivative(of: staticMethod, wrt: x)
@derivative(of: staticMethod, wrt: x)
static func derivativeStaticMethod(_ x: S) -> (value: S, differential: (S) -> S) {
(x, { $0 })
}
}
// Test computed properties.
extension S {
var computedProperty: S {
self
}
// CHECK: @derivative(of: computedProperty, wrt: self)
@derivative(of: computedProperty, wrt: self)
func derivativeProperty() -> (value: S, differential: (S) -> S) {
(self, { $0 })
}
}
// Test subscripts.
extension S {
subscript<T: Differentiable>(x: T) -> S {
self
}
// CHECK: @derivative(of: subscript, wrt: self)
@derivative(of: subscript(_:), wrt: self)
func derivativeSubscript<T: Differentiable>(x: T) -> (value: S, differential: (S) -> S) {
(self, { $0 })
}
}