File: reverse-array.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 (68 lines) | stat: -rw-r--r-- 2,158 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
// RUN: %target-swift-frontend  -primary-file %s -O -module-name=test -emit-sil | %FileCheck %s
// RUN: %target-swift-frontend  -primary-file %s -O -module-name=test -emit-ir | %FileCheck %s -check-prefix=CHECK-LLVM

// Also do an end-to-end test to check all components, including IRGen.
// RUN: %empty-directory(%t) 
// RUN: %target-build-swift -O -module-name=test %s -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT

// REQUIRES: swift_in_compiler,executable_test,swift_stdlib_no_asserts,optimized_stdlib

// Check that we create reasonable optimized code for this function.

@inline(never)
public func reverseArray(_ a: [Int]) -> [Int] {
  var new: [Int] = []
  new.reserveCapacity(a.count)

  var fromIndex = a.count &- 1

  while fromIndex >= 0 {
    new.append(a[fromIndex])
    fromIndex &-= 1
  }

  return new
}


// CHECK-LABEL: sil [noinline] {{.*}}@$s4test12reverseArrayySaySiGACF

// There must not be more than two begin_cow_mutation - end_cow_mutation pairs:
// * the first one for the initial reserveCapacity
// * the second for the append.

// CHECK-NOT: {{.*(_cow_mutation|cond_fail)}}
// CHECK:     begin_cow_mutation
// CHECK-NOT: {{.*(_cow_mutation|cond_fail)}}
// CHECK:     end_cow_mutation
// CHECK:     end_cow_mutation

// In SIL we fail to eliminate the bounds check of the input array.
// But that's okay, because LLVM can do that.
// So we accept one cond_fail in the SIL output.

// CHECK:     cond_fail {{.*}} "Index out of range"

// The second begin_cow_mutation - end_cow_mutation pair:

// CHECK-NOT: {{.*(_cow_mutation|cond_fail)}}
// CHECK:     begin_cow_mutation
// CHECK-NOT: {{.*(_cow_mutation|cond_fail)}}
// CHECK:     end_cow_mutation
// CHECK-NOT: {{.*(_cow_mutation|cond_fail)}}

// CHECK: } // end sil function '$s4test12reverseArrayySaySiGACF'


// Check that there are no cond_fails left in the LLVM output.
// LLVM should be able to optimize away the bounds check of the input array.

// CHECK-LLVM-LABEL: define {{.*}} @"$s4test12reverseArrayySaySiGACF"
// CHECK-LLVM-NOT:     llvm.trap
// CHECK-LLVM:       }


// CHECK-OUTPUT: [3, 2, 1]
print(reverseArray([1, 2, 3]))