File: e2e_optimizations.swift

package info (click to toggle)
swiftlang 6.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,644 kB
  • sloc: cpp: 9,901,738; ansic: 2,201,433; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (120 lines) | stat: -rw-r--r-- 4,234 bytes parent folder | download | duplicates (2)
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
// RUN: %target-swift-frontend -emit-sil -O %s | %FileCheck %s
// REQUIRES: swift_in_compiler

// Checks for inlining depends on code-size but cow check adds some
// amount of extra code
// UNSUPPORTED: array_cow_checks

import _Differentiation

@_silgen_name("blackHole")
@inline(never)
@discardableResult
func blackHole<T>(_ x: T) -> T { x }

func float(_ x0: Float) -> Float {
  let x1 = x0 * x0
  let x2 = x1 + x1
  let x3 = x2 - x1
  let x4 = x3 / x2
  return x4
}

@_silgen_name("test_gradient_float")
func test_gradient_float() {
  blackHole(gradient(at: 10, of: float))
}

// Check that `apply`s are fully inlined.
// CHECK-LABEL: sil hidden @test_gradient_float : $@convention(thin) () -> ()
// CHECK-NOT: apply
// CHECK: [[GRADIENT_RESULT:%.*]] = struct $Float ({{.*}} : $Builtin.FPIEEE32)
// CHECK: [[FN_REF:%.*]] = function_ref @$s9blackHoleSf_Tg5 : $@convention(thin) (Float) -> Float
// CHECK-NEXT: apply [[FN_REF:%.*]]([[GRADIENT_RESULT]])
// CHECK-NOT: apply
// CHECK-LABEL: } // end sil function 'test_gradient_float'
func float_mutation(_ x: Float) -> Float {
  var result = x * x
  result = result + result
  result = result - x
  result = result / x
  return result
}

@_silgen_name("test_gradient_float_mutation")
func test_gradient_float_mutation() {
  blackHole(gradient(at: 10, of: float_mutation))
}

// Check that `apply`s are fully inlined.
// CHECK-LABEL: sil hidden @test_gradient_float_mutation : $@convention(thin) () -> ()
// CHECK-NOT: apply
// CHECK: [[GRADIENT_RESULT:%.*]] = struct $Float ({{.*}} : $Builtin.FPIEEE32)
// CHECK: [[FN_REF:%.*]] = function_ref @$s9blackHoleSf_Tg5 : $@convention(thin) (Float) -> Float
// CHECK-NEXT: apply [[FN_REF:%.*]]([[GRADIENT_RESULT]])
// CHECK-NOT: apply
// CHECK-LABEL: } // end sil function 'test_gradient_float_mutation'
func float_conditional(_ x: Float, _ bool: Bool) -> Float {
  var result = x * x
  if bool {
    result = result + result
    result = result - x
  } else {
    result = result / x
  }
  return result
}

@_silgen_name("test_gradient_float_conditional")
func test_gradient_float_conditional() {
  blackHole(gradient(at: 10, of: { float_conditional($0, true) }))
}

// Check that `apply`s are fully inlined.
// CHECK-LABEL: sil hidden @test_gradient_float_conditional : $@convention(thin) () -> ()
// CHECK-NOT: apply
// CHECK: [[GRADIENT_RESULT:%.*]] = struct $Float ({{.*}} : $Builtin.FPIEEE32)
// CHECK: [[FN_REF:%.*]] = function_ref @$s9blackHoleSf_Tg5 : $@convention(thin) (Float) -> Float
// CHECK-NEXT: apply [[FN_REF:%.*]]([[GRADIENT_RESULT]])
// CHECK-NOT: apply
// CHECK-LABEL: } // end sil function 'test_gradient_float_conditional'
func float_loop(_ x: Float, count: Int) -> Float {
  var result: Float = 0
  for _ in 0..<count {
    result = result * x
  }
  return result
}

@_silgen_name("test_gradient_float_loop")
func test_gradient_float_loop() {
  blackHole(gradient(at: 10, of: { float_loop($0, count: 10) }))
}

// Check whether `apply`s are inlined.
// CHECK-LABEL: sil hidden @test_gradient_float_loop : $@convention(thin) () -> ()
// CHECK:  = function_ref @${{.*24test_gradient_float_loopyyFS2fcfU_TJrSpSr|sSf16_DifferentiationE12_vjpMultiply3lhs3rhsSf5value_Sf_SftSfc8pullbacktSf_SftFZSf_SftSfcfU_}}
// CHECK: [[FN_REF:%.*]] = function_ref @$s9blackHoleSf_Tg5 : $@convention(thin) (Float) -> Float
// CHECK-NEXT: apply [[FN_REF:%.*]]
// CHECK-NOT: apply
// CHECK-LABEL: } // end sil function 'test_gradient_float_loop'
func array_loop(_ array: [Float]) -> Float {
  var result: Float = 0
  for i in withoutDerivative(at: array.indices) {
    result += array[i]
  }
  return result
}

@_silgen_name("test_gradient_array_loop")
func test_gradient_array_loop() {
  blackHole(gradient(at: [3, 4, 5], of: array_loop))
}

// Check whether `apply`s are inlined.
// Currently, the VJP is not inlined.
// CHECK-LABEL: sil hidden @test_gradient_array_loop : $@convention(thin) () -> ()
// CHECK: [[VJP_FN_REF:%.*]] = function_ref @{{.*}}10array_loopySfSaySfGFTJrSpSr : $@convention(thin) (@guaranteed Array<Float>) -> (Float, @owned @callee_guaranteed (Float) -> @owned Array<Float>.DifferentiableView)
// CHECK: [[VJP_RESULT:%.*]] = apply [[VJP_FN_REF]]
// CHECK-LABEL: } // end sil function 'test_gradient_array_loop'