- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathtranspose_attr.swift
97 lines (79 loc) · 2.31 KB
/
transpose_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
// 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 -enable-sil-verify-all %t/transpose_attr.swiftmodule -o - | %FileCheck %s
// BCANALYZER-NOT: UnknownCode
import _Differentiation
// Dummy `Differentiable`-conforming type.
structS:Differentiable&AdditiveArithmetic{
staticvarzero:S{S()}
staticfunc+(_:S, _:S)->S{S()}
staticfunc-(_:S, _:S)->S{S()}
typealiasTangentVector=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.
extensionS{
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
}
// Note: qualified name base types are not yet serialized and are not printed
// when round-tripping.
// CHECK: @transpose(of: instanceMethod, wrt: self)
@transpose(of:S.instanceMethod, wrt:self)
staticfunc transposeInstanceMethodWrtSelf(_ other:S, t:S)->S{
other + t
}
}
// Test static methods.
extensionS{
staticfunc staticMethod(x:S)->S{
x
}
// CHECK: @transpose(of: staticMethod, wrt: 0)
@transpose(of: staticMethod, wrt:0)
staticfunc transposeStaticMethod(t:S)->S{
t
}
}
// Test computed properties.
extensionS{
varcomputedProperty:S{self}
// CHECK: @transpose(of: computedProperty, wrt: self)
@transpose(of: computedProperty, wrt:self)
staticfunc transposeProperty(t:Self)->Self{
t
}
}
// Test subscripts.
extensionS{
subscript<T:Differentiable>(x:T)->Self{self}
// CHECK: @transpose(of: subscript, wrt: self)
@transpose(of:subscript(_:), wrt:self)
staticfunc transposeSubscript<T:Differentiable>(x:T, t:Self)->Self{
t
}
}