File: dead_array_elim.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 (124 lines) | stat: -rw-r--r-- 4,183 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
// RUN: %target-swift-frontend -O -emit-sil -primary-file %s | grep -v debug_value | %FileCheck %s

// REQUIRES: swift_stdlib_no_asserts
// REQUIRES: swift_in_compiler
// XFAIL: OS=linux-androideabi

// Test needs to be updated for 32bit.
// rdar://74810823
// UNSUPPORTED: PTRSIZE=32

// These tests check whether DeadObjectElimination pass runs as a part of the
// optimization pipeline and eliminates dead array literals in Swift code.
// Note that DeadObjectElimination pass relies on @_semantics annotations on
// the array initializer that is used by the compiler to create array literals.
// This test would fail if in case the initializer used by the compiler to
// initialize array literals doesn't match the one expected by the pass.

// testDeadArrayElimination requires swift_stdlib_no_asserts because,
// with runtime verification enabled, "array.finalize" becomes a
// mutating operation, preventing SILCombine from deleting it when it
// removes dead pure instructions. After inlining,
// DeadObjectElimination is still unable to remove the array because a
// second array is initialized by copying the first. This problem can be
// overcome by handling non-trivial stores in OSSA, as described here:
//   [OSSA] Improve DeadObjectElimination to handle array copies
//   https://github.com/apple/swift/issues/56179
// Once that bug is fixed, remove the requirement: swift_stdlib_no_asserts.

// CHECK-LABEL: sil hidden @$s15dead_array_elim24testDeadArrayEliminationyyF
func testDeadArrayElimination() {
  _ = [1, 2, 3]
    // CHECK: bb0:
    // CHECK-NEXT: %{{.*}} = tuple ()
    // CHECK-NEXT: return %{{.*}} : $()
}

// CHECK-LABEL: sil hidden @$s15dead_array_elim29testEmptyDeadArrayEliminationyyF
func testEmptyDeadArrayElimination() {
  _ = []
    // CHECK: bb0:
    // CHECK-NEXT: %{{.*}} = tuple ()
    // CHECK-NEXT: return %{{.*}} : $()
}

// The use case tested by the following test, where a _fixLifetime call is
// invoked on an array, appears when new os log APIs are  used.
// CHECK-LABEL: sil hidden @$s15dead_array_elim35testDeadArrayElimWithFixLifetimeUseyyF
func testDeadArrayElimWithFixLifetimeUse() {
  let a: [Int] = []
  _fixLifetime(a)
    // CHECK: bb0:
    // CHECK-NEXT: %{{.*}} = tuple ()
    // CHECK-NEXT: return %{{.*}} : $()
}

// FIXME: DeadObjectElimination doesn't optimize this yet.
func testDeadArrayElimWithAddressOnlyValues<T>(x: T, y: T) {
  _ = [x, y]
}

// Adding mark_dependence to array allocate caused this test to break
// RLE needs to handle the new init pattern - rdar://117751668
// TODO-LABEL: sil hidden {{.*}}@$s15dead_array_elim31testDeadArrayAfterOptimizationsySiSSF
// TODO:      bb0(%0 : $String):
// TODO-NEXT:   integer_literal $Builtin.Int{{[0-9]+}}, 21
// TODO-NEXT:   struct $Int
// TODO-NEXT:   return
// TODO:      } // end sil function '$s15dead_array_elim31testDeadArrayAfterOptimizationsySiSSF'
func testDeadArrayAfterOptimizations(_ stringParameter: String) -> Int {
  var sum = 0
  for x in [(1, "hello"),
            (2, "a larger string which does not fit into a small string"),
            (3, stringParameter),
            (4, "hello"),
            (5, "hello"),
            (6, "hello"),
            ] {
    sum += x.0
  }
  return sum
}

// CHECK-LABEL: sil hidden @$s15dead_array_elim15testNestedArraySiyF
// CHECK:      bb0:
// CHECK-NEXT:   integer_literal $Builtin.Int{{[0-9]+}}, 3
// CHECK-NEXT:   struct $Int
// CHECK-NEXT:   return
// CHECK:      } // end sil function '$s15dead_array_elim15testNestedArraySiyF'
func testNestedArray() -> Int {
   struct Str {
     var sa: [String]
     var s: String? = nil
     var b: Bool = true

     static func opt1(_ sa: [String], _ s: String?) -> Self {
       return .init(sa: sa, s: s, b: true)
     }

     static func opt2(_ s1: String, _ s2: String? = nil) -> Self {
       return .opt1([s1], s2)
     }

     static func opt3(_ sa: [String], _ s: String) -> Self {
       return .init(sa: sa, s: s, b: false)
     }
   }

   let strArr: [Str] = [
     .opt1(["1", "2"], "3"),
     .opt3(["4", "5"], "6"),

     .opt2("7"),
     .opt2("8"),
   ]

   var num = 0
   for s in strArr {
     if s.b {
       num += 1
     }
   }
   return num
}