-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathclosures.swift
39 lines (33 loc) · 1.45 KB
/
closures.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
// RUN: %target-swift-frontend -emit-sil %s | %FileCheck %s
struct Foo {
var x: Float
var f: @differentiable (Float) -> Float
}
func diffableClosureInStruct(s: Foo) {
_ = gradient(of: s.f)
}
// CHECK-LABEL: @{{.*}}diffableClosureInStruct{{.*}} : $@convention(thin) (@guaranteed Foo) -> () {
// CHECK: [[CLOSURE:%.*]] = struct_extract {{%.*}} : $Foo, #Foo.f
// CHECK: retain_value [[CLOSURE]] : $@differentiable @callee_guaranteed (Float) -> Float
// CHECK: autodiff_function_extract [original] [[CLOSURE]] : $@differentiable @callee_guaranteed (Float) -> Float
public func closureCaptureMutable() {
var val: Float = 10
let clo: (Float) -> Float = { x in
val += 2
return val * x
}
_ = gradient(at: 0, in: clo)
}
// CHECK-LABEL: @AD__{{.*}}closureCaptureMutable{{.*}}___vjp_src_0_wrt_0
// CHECK: bb0({{%.*}} : $Float, [[BOXED_ARG:%.*]] : ${ var Float }):
// CHECK: [[PRIMAL:%.*]] = function_ref @AD__{{.*}}closureCaptureMutable{{.*}}___primal_src_0_wrt_0
// CHECK: {{.*}} = apply [[PRIMAL]]({{.*}}, [[BOXED_ARG]])
// CHECK: [[ADJOINT:%.*]] = function_ref @AD__{{.*}}closureCaptureMutabley{{.*}}___adjoint_src_0_wrt_0
// CHECK: {{.*}} = partial_apply [callee_guaranteed] [[ADJOINT]]({{.*}})
// TF-30: VJP return value should match the return type.
struct TF30 : Differentiable {
var x: Float
@noDerivative var y: @differentiable (Float) -> Float
}
// Make sure this passes SIL verification.
let _: @autodiff (TF30) -> Float = { x in x.x }