-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathautodiff_function_silgen.swift
45 lines (38 loc) · 2.8 KB
/
autodiff_function_silgen.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
// RUN: %target-swift-frontend -dump-ast %s 2>&1 | %FileCheck %s -check-prefix=CHECK-AST
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s -check-prefix=CHECK-SILGEN
// RUN: %target-swift-frontend -emit-sil %s | %FileCheck %s -check-prefix=CHECK-SIL
func thin(x: Float) -> Float { return x }
func myfunction(_ f: @escaping @differentiable (Float) -> (Float)) -> (Float) -> Float {
// @differentiable functions should be callable.
_ = f(.zero)
return f
}
func apply() {
_ = myfunction(thin)
}
// CHECK-AST-LABEL: (func_decl {{.*}} "myfunction(_:)"
// CHECK-AST: (call_expr type='(Float)'
// CHECK-AST: (declref_expr type='@differentiable (Float) -> (Float)'
// CHECK-AST: (return_stmt
// CHECK-AST: (function_conversion_expr implicit type='(Float) -> Float'
// CHECK-AST: (autodiff_function_extract_original implicit type='(Float) -> (Float)'
// CHECK-AST: (declref_expr type='@differentiable (Float) -> (Float)'
// CHECK-AST-LABEL: (func_decl {{.*}} "apply()"
// CHECK-AST: (autodiff_function implicit type='@differentiable (Float) -> (Float)'
// CHECK-AST: (function_conversion_expr implicit type='(Float) -> (Float)'
// CHECK-AST: (declref_expr type='(Float) -> Float'
// CHECK-SILGEN-LABEL: @{{.*}}myfunction{{.*}}
// CHECK-SILGEN: bb0([[DIFFED:%.*]] : @guaranteed $@differentiable @callee_guaranteed (Float) -> Float):
// CHECK-SILGEN: [[ORIG:%.*]] = copy_value [[DIFFED]] : $@differentiable @callee_guaranteed (Float) -> Float
// CHECK-SILGEN: [[BORROWED_ORIG:%.*]] = begin_borrow [[ORIG]] : $@differentiable @callee_guaranteed (Float) -> Float
// CHECK-SILGEN: apply [[BORROWED_ORIG]]({{%.*}}) : $@differentiable @callee_guaranteed (Float) -> Float
// CHECK-SILGEN: destroy_value [[ORIG]] : $@differentiable @callee_guaranteed (Float) -> Float
// CHECK-SILGEN: [[DIFFED_COPY:%.*]] = copy_value [[DIFFED]] : $@differentiable @callee_guaranteed (Float) -> Float
// CHECK-SILGEN: [[ORIG:%.*]] = autodiff_function_extract [original] [[DIFFED_COPY]] : $@differentiable @callee_guaranteed (Float) -> Float
// CHECK-SILGEN: return [[ORIG]] : $@callee_guaranteed (Float) -> Float
// CHECK-SILGEN-LABEL: @{{.*}}apply{{.*}}
// CHECK-SILGEN: [[ORIG:%.*]] = function_ref @{{.*}}thin{{.*}} : $@convention(thin) (Float) -> Float
// CHECK-SILGEN-NEXT: [[ORIG_THICK:%.*]] = thin_to_thick_function [[ORIG]] : $@convention(thin) (Float) -> Float to $@callee_guaranteed (Float) -> Float
// CHECK-SILGEN-NEXT: [[DIFFED:%.*]] = autodiff_function [wrt 0] [order 1] [[ORIG_THICK]] : $@callee_guaranteed (Float) -> Float
// CHECK-SIL: [[DIFFED:%.*]] = autodiff_function [wrt 0] [order 1] {{%.*}} : $@callee_guaranteed (Float) -> Float
// CHECK-SIL: release_value [[DIFFED]] : $@differentiable @callee_guaranteed (Float) -> Float