File: derivative_attr.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (163 lines) | stat: -rw-r--r-- 4,336 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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 -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 top-level inout functions.

func topInout1(_ x: inout S) {}

// CHECK: @derivative(of: topInout1, wrt: x)
@derivative(of: topInout1)
func derivativeTopInout1(_ x: inout S) -> (value: Void, pullback: (inout S) -> Void) {
  fatalError()
}

func topInout2(_ x: inout S) -> S {
  x
}

// CHECK: @derivative(of: topInout2, wrt: x)
@derivative(of: topInout2)
func derivativeTopInout2(_ x: inout S) -> (value: S, pullback: (S, inout S) -> Void) {
  fatalError()
}

// 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 })
  }

  // Note: qualified name base types are not yet serialized and are not printed
  // when round-tripping.

  // CHECK: @derivative(of: instanceMethod, wrt: (self, x))
  @derivative(of: S.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 {
    get { self }
    set {}
  }

  // CHECK: @derivative(of: computedProperty, wrt: self)
  @derivative(of: computedProperty, wrt: self)
  func derivativeProperty() -> (value: S, differential: (S) -> S) {
    (self, { $0 })
  }

  // CHECK: @derivative(of: computedProperty.get, wrt: self)
  @derivative(of: computedProperty.get, wrt: self)
  func derivativePropertyGetter() -> (value: S, pullback: (S) -> S) {
    fatalError()
  }

  // CHECK: @derivative(of: computedProperty.set, wrt: (self, newValue))
  @derivative(of: computedProperty.set, wrt: (self, newValue))
  mutating func derivativePropertySetter(_ newValue: S) -> (
    value: (), pullback: (inout S) -> S
  ) {
    fatalError()
  }
}

// Test subscripts.

extension S {
  subscript() -> S {
    get { self }
    set {}
  }

  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 })
  }

  // CHECK: @derivative(of: subscript.get, wrt: self)
  @derivative(of: subscript.get, wrt: self)
  func derivativeSubscriptGetter() -> (value: S, pullback: (S) -> S) {
    fatalError()
  }

  // CHECK: @derivative(of: subscript.set, wrt: (self, newValue))
  @derivative(of: subscript.set, wrt: (self, newValue))
  mutating func derivativeSubscriptSetter(_ newValue: S) -> (
    value: (), pullback: (inout S) -> S
  ) {
    fatalError()
  }
}