File: differentiation_function_canonicalization.sil

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 (141 lines) | stat: -rw-r--r-- 7,778 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
// RUN: %target-sil-opt -differentiation %s | %FileCheck %s

sil_stage raw

import _Differentiation
import Swift

// Test `function_ref` instructions.

sil @basic : $@convention(thin) (Float) -> Float {
bb0(%0 : $Float):
  return %0 : $Float
}

sil @test_function_ref : $@convention(thin) () -> () {
bb0:
  %1 = function_ref @basic : $@convention(thin) (Float) -> Float
  %2 = thin_to_thick_function %1 : $@convention(thin) (Float) -> Float to $@callee_guaranteed (Float) -> Float
  %3 = differentiable_function [parameters 0] [results 0] %2 : $@callee_guaranteed (Float) -> Float
  %void = tuple ()
  return %void : $()
}

// CHECK-LABEL: sil @test_function_ref
// CHECK:   [[ORIG_FN_REF:%.*]] = function_ref @basic : $@convention(thin) (Float) -> Float
// CHECK:   [[ORIG_FN:%.*]] = thin_to_thick_function [[ORIG_FN_REF]]
// CHECK:   [[JVP_FN_REF:%.*]] = differentiability_witness_function [jvp] [reverse] [parameters 0] [results 0] @basic
// CHECK:   [[JVP_FN:%.*]] = thin_to_thick_function [[JVP_FN_REF]]
// CHECK:   [[VJP_FN_REF:%.*]] = differentiability_witness_function [vjp] [reverse] [parameters 0] [results 0] @basic
// CHECK:   [[VJP_FN:%.*]] = thin_to_thick_function [[VJP_FN_REF]]
// CHECK:   differentiable_function [parameters 0] [results 0] [[ORIG_FN]] : $@callee_guaranteed (Float) -> Float with_derivative {[[JVP_FN]] : $@callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float), [[VJP_FN]] : $@callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float)}
// CHECK: }

// Test `differentiable_function_extract` instructions.

sil @test_differentiable_function_extract : $@convention(thin) (@differentiable(reverse) @callee_guaranteed (Float) -> Float) -> () {
bb0(%0 : $@differentiable(reverse) @callee_guaranteed (Float) -> Float):
  %1 = differentiable_function_extract [original] %0 : $@differentiable(reverse) @callee_guaranteed (Float) -> Float
  %2 = differentiable_function [parameters 0] [results 0] %1 : $@callee_guaranteed (Float) -> Float
  %void = tuple ()
  return %void : $()
}

// CHECK-LABEL: sil @test_differentiable_function_extract
// CHECK: bb0([[ARG:%.*]] : $@differentiable(reverse) @callee_guaranteed (Float) -> Float):
// CHECK:   [[ORIG_FN:%.*]] = differentiable_function_extract [original] [[ARG]]
// CHECK:   [[JVP_FN:%.*]] = differentiable_function_extract [jvp] [[ARG]]
// CHECK:   [[VJP_FN:%.*]] = differentiable_function_extract [vjp] [[ARG]]
// CHECK:   differentiable_function [parameters 0] [results 0] [[ORIG_FN]] : {{.*}} with_derivative {[[JVP_FN]] : {{.*}}, [[VJP_FN]] : {{.*}}}
// CHECK: }

// Test `witness_method` instructions.

protocol Protocol {
  @differentiable(reverse, wrt: x)
  func method(_ x: Float) -> Float
}

sil @test_witness_method : $@convention(thin) <T where T : Protocol> (@in_guaranteed T) -> () {
bb0(%0 : $*T):
  %1 = witness_method $T, #Protocol.method : <Self where Self : Protocol> (Self) -> (Float) -> Float : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (Float, @in_guaranteed τ_0_0) -> Float
  %2 = differentiable_function [parameters 0] [results 0] %1 : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (Float, @in_guaranteed τ_0_0) -> Float
  %void = tuple ()
  return %void : $()
}

// CHECK-LABEL: sil @test_witness_method
// CHECK:   [[ORIG_FN:%.*]] = witness_method $T, #Protocol.method
// CHECK:   [[JVP_FN:%.*]] = witness_method $T, #Protocol.method!jvp.SU
// CHECK:   [[VJP_FN:%.*]] = witness_method $T, #Protocol.method!vjp.SU
// CHECK:   differentiable_function [parameters 0] [results 0] [[ORIG_FN]] : {{.*}} with_derivative {[[JVP_FN]] : {{.*}}, [[VJP_FN]] : {{.*}}}
// CHECK: }

sil @test_witness_method_partial_apply : $@convention(thin) <T where T : Protocol> (@in_guaranteed T) -> () {
bb0(%0 : $*T):
  %1 = witness_method $T, #Protocol.method : <Self where Self : Protocol> (Self) -> (Float) -> Float : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (Float, @in_guaranteed τ_0_0) -> Float
  %2 = partial_apply [callee_guaranteed] %1<T>(%0) : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (Float, @in_guaranteed τ_0_0) -> Float
  %3 = differentiable_function [parameters 0] [results 0] %2 : $@callee_guaranteed (Float) -> Float
  %void = tuple ()
  return %void : $()
}

// CHECK-LABEL: sil @test_witness_method_partial_apply
// CHECK: bb0([[ARG:%.*]] : $*T):
// CHECK:  [[ORIG_FN:%.*]] = witness_method $T, #Protocol.method
// CHECK:  [[ARGCOPY1:%.*]] = alloc_stack $T
// CHECK:  copy_addr [[ARG]] to [init] [[ARGCOPY1]] : $*T
// CHECK:  [[ARGCOPY2:%.*]] = alloc_stack $T
// CHECK:  copy_addr [[ARG]] to [init] [[ARGCOPY2]] : $*T
// CHECK:  [[ORIG_FN_PARTIALLY_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[ORIG_FN]]<T>([[ARG]])
// CHECK:  [[JVP_FN:%.*]] = witness_method $T, #Protocol.method!jvp.SU
// CHECK:  [[JVP_FN_PARTIALLY_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[JVP_FN]]<T>([[ARGCOPY1]])
// CHECK:  [[VJP_FN:%.*]] = witness_method $T, #Protocol.method!vjp.SU
// CHECK:  [[VJP_FN_PARTIALLY_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[VJP_FN]]<T>([[ARGCOPY2]])
// CHECK:  dealloc_stack [[ARGCOPY2]]
// CHECK:  dealloc_stack [[ARGCOPY1]]
// CHECK:  differentiable_function [parameters 0] [results 0] [[ORIG_FN_PARTIALLY_APPLIED]] : {{.*}} with_derivative {[[JVP_FN_PARTIALLY_APPLIED]] : {{.*}}, [[VJP_FN_PARTIALLY_APPLIED]] : {{.*}}}
// CHECK: }

// Test `class_method` instructions.

class Class {
  @differentiable(reverse, wrt: x)
  func method(_ x: Float) -> Float
}

sil @test_class_method : $@convention(thin) <T where T : Class> (@guaranteed Class) -> () {
bb0(%0 : $Class):
  %1 = class_method %0 : $Class, #Class.method : (Class) -> (Float) -> Float, $@convention(method) (Float, @guaranteed Class) -> Float
  %2 = differentiable_function [parameters 0] [results 0] %1 : $@convention(method) (Float, @guaranteed Class) -> Float
  %void = tuple ()
  return %void : $()
}

// CHECK-LABEL: sil @test_class_method
// CHECK: bb0([[ARG:%.*]] : $Class):
// CHECK:   [[ORIG_FN:%.*]] = class_method [[ARG]] : $Class, #Class.method
// CHECK:   [[JVP_FN:%.*]] = class_method [[ARG]] : $Class, #Class.method!jvp.SU
// CHECK:   [[VJP_FN:%.*]] = class_method [[ARG]] : $Class, #Class.method!vjp.SU
// CHECK:   differentiable_function [parameters 0] [results 0] [[ORIG_FN]] : {{.*}} with_derivative {[[JVP_FN]] : {{.*}}, [[VJP_FN]] : {{.*}}}
// CHECK: }

sil @test_class_method_partial_apply : $@convention(thin) <T where T : Class> (@guaranteed Class) -> () {
bb0(%0 : $Class):
  %1 = class_method %0 : $Class, #Class.method : (Class) -> (Float) -> Float, $@convention(method) (Float, @guaranteed Class) -> Float
  %2 = partial_apply [callee_guaranteed] %1(%0) : $@convention(method) (Float, @guaranteed Class) -> Float
  %3 = differentiable_function [parameters 0] [results 0] %2 : $@callee_guaranteed (Float) -> Float
  %void = tuple ()
  return %void : $()
}

// CHECK-LABEL: sil @test_class_method_partial_apply
// CHECK: bb0([[ARG:%.*]] : $Class):
// CHECK:  [[ORIG_FN:%.*]] = class_method [[ARG]] : $Class, #Class.method
// CHECK:  [[ORIG_FN_PARTIALLY_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[ORIG_FN]]([[ARG]])
// CHECK:  [[JVP_FN:%.*]] = class_method [[ARG]] : $Class, #Class.method!jvp.SU
// CHECK:  [[JVP_FN_PARTIALLY_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[JVP_FN]]([[ARG]])
// CHECK:  [[VJP_FN:%.*]] = class_method [[ARG]] : $Class, #Class.method!vjp.SU
// CHECK:  [[VJP_FN_PARTIALLY_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[VJP_FN]]([[ARG]])
// CHECK:  differentiable_function [parameters 0] [results 0] [[ORIG_FN_PARTIALLY_APPLIED]] : {{.*}} with_derivative {[[JVP_FN_PARTIALLY_APPLIED]] : {{.*}}, [[VJP_FN_PARTIALLY_APPLIED]] : {{.*}}}
// CHECK: }