|
1 |
| -// RUN: %target-swift-frontend -O -emit-irgen %s -module-name main -parse-as-library -enable-experimental-feature Embedded | %FileCheck %s --check-prefix CHECK-IR |
2 |
| -// RUN: %target-run-simple-swift(-O -enable-experimental-feature Embedded -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s |
| 1 | +// RUN: %target-swift-frontend -parse-as-library -enable-experimental-feature Embedded %s -O -wmo -sil-verify-all -module-name=test -emit-ir | %FileCheck %s |
3 | 2 |
|
4 |
| -// REQUIRES: swift_in_compiler |
5 |
| -// REQUIRES: executable_test |
6 |
| -// REQUIRES: optimized_stdlib |
7 |
| -// REQUIRES: OS=macosx || OS=linux-gnu |
| 3 | +// Also do an end-to-end test to check all components, including IRGen. |
| 4 | +// RUN: %empty-directory(%t) |
| 5 | +// RUN: %target-build-swift -parse-as-library -enable-experimental-feature Embedded -O -wmo -module-name=test %s -o %t/a.out |
| 6 | +// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT |
| 7 | + |
| 8 | +// REQUIRES: executable_test,swift_stdlib_no_asserts,optimized_stdlib |
| 9 | + |
| 10 | +// Check if the optimizer is able to convert array literals to constant statically initialized arrays. |
| 11 | + |
| 12 | +// CHECK-DAG: @"$s4test11arrayLookupyS2iFTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} -1 |
| 13 | +// CHECK-DAG: @"$s4test11returnArraySaySiGyFTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} -1 |
| 14 | +// CHECK-DAG: @"$s4test9passArrayyyFTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} -1 |
| 15 | +// CHECK-DAG: @"$s4test9passArrayyyFTv0_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} -1 |
| 16 | +// CHECK-DAG: @"$s4test10storeArrayyyFTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} -1 |
| 17 | +// CHECK-DAG: @"$s4test3StrV9staticLet_WZTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} -1 |
| 18 | +// CHECK-DAG: @"$s4test3StrV9staticVar_WZTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} -1 |
| 19 | +// CHECK-DAG: @"$s4test3StrV9staticVarSaySiGvpZ" = global {{.*}} ptr @"$s4test3StrV9staticVar_WZTv_r" |
| 20 | +// CHECK-DAG: @"$s4test3StrV14twoDimensionalSaySaySiGGvpZ" = global {{.*}} ptr @"$s4test3StrV14twoDimensional_WZTv{{[0-9]*}}_r" |
| 21 | + |
| 22 | +// Currently, constant static arrays only work on Darwin platforms. |
| 23 | +// REQUIRES: VENDOR=apple |
| 24 | + |
| 25 | + |
| 26 | +public struct Str { |
| 27 | + public static let staticLet = [ 200, 201, 202 ] |
| 28 | + public static var staticVar = [ 300, 301, 302 ] |
| 29 | + public static var twoDimensional = [[1, 2], [3, 4], [5, 6]] |
| 30 | +} |
| 31 | + |
| 32 | +@inline(never) |
| 33 | +public func arrayLookup(_ i: Int) -> Int { |
| 34 | + let lookupTable = [10, 11, 12] |
| 35 | + return lookupTable[i] |
| 36 | +} |
| 37 | + |
| 38 | +@inline(never) |
| 39 | +public func returnArray() -> [Int] { |
| 40 | + return [20, 21] |
| 41 | +} |
| 42 | + |
| 43 | +@inline(never) |
| 44 | +public func modifyArray() -> [Int] { |
| 45 | + var a = returnArray() |
| 46 | + a[1] = 27 |
| 47 | + return a |
| 48 | +} |
| 49 | + |
| 50 | +public var gg: [Int]? |
| 51 | + |
| 52 | +@inline(never) |
| 53 | +public func receiveArray(_ a: [Int]) { |
| 54 | + gg = a |
| 55 | +} |
| 56 | + |
| 57 | +@inline(never) |
| 58 | +public func passArray() { |
| 59 | + receiveArray([27, 28]) |
| 60 | + receiveArray([29]) |
| 61 | +} |
| 62 | + |
| 63 | +@inline(never) |
| 64 | +public func storeArray() { |
| 65 | + gg = [227, 228] |
| 66 | +} |
8 | 67 |
|
9 | 68 | public func stringArray() -> [StaticString] {
|
10 | 69 | return ["a", "b", "c", "d"]
|
11 | 70 | }
|
12 |
| -// CHECK-IR: define {{.*}}@"$s4main11stringArraySays12StaticStringVGyF" |
13 |
| -// CHECK-IR-NEXT: entry: |
14 |
| -// CHECK-IR-NEXT: call {{.*}}@swift_initStaticObject |
15 | 71 |
|
16 |
| -@main |
17 |
| -struct Main { |
| 72 | +@main struct Main { |
18 | 73 | static func main() {
|
| 74 | + |
| 75 | + // CHECK-OUTPUT: [200, 201, 202] |
| 76 | + printArray(Str.staticLet) |
| 77 | + |
| 78 | + // CHECK-OUTPUT: [300, 301, 302] |
| 79 | + printArray(Str.staticVar) |
| 80 | + |
| 81 | + // CHECK-OUTPUT: [1, 2] |
| 82 | + // CHECK-OUTPUT-NEXT: [3, 4] |
| 83 | + // CHECK-OUTPUT-NEXT: [5, 6] |
| 84 | + for x in Str.twoDimensional { |
| 85 | + printArray(x) |
| 86 | + } |
| 87 | + |
| 88 | + // CHECK-OUTPUT-NEXT: 11 |
| 89 | + print(arrayLookup(1)) |
| 90 | + |
| 91 | + // CHECK-OUTPUT-NEXT: [20, 21] |
| 92 | + printArray(returnArray()) |
| 93 | + |
| 94 | + // CHECK-OUTPUT-NEXT: [20, 27] |
| 95 | + // CHECK-OUTPUT-NEXT: [20, 27] |
| 96 | + // CHECK-OUTPUT-NEXT: [20, 27] |
| 97 | + // CHECK-OUTPUT-NEXT: [20, 27] |
| 98 | + // CHECK-OUTPUT-NEXT: [20, 27] |
| 99 | + for _ in 0..<5 { |
| 100 | + printArray(modifyArray()) |
| 101 | + } |
| 102 | + |
| 103 | + passArray() |
| 104 | + // CHECK-OUTPUT-NEXT: [29] |
| 105 | + printArray(gg!) |
| 106 | + |
| 107 | + storeArray() |
| 108 | + // CHECK-OUTPUT-NEXT: [227, 228] |
| 109 | + printArray(gg!) |
| 110 | + |
19 | 111 | for c in stringArray() {
|
| 112 | + // CHECK-OUTPUT-NEXT: a |
| 113 | + // CHECK-OUTPUT-NEXT: b |
| 114 | + // CHECK-OUTPUT-NEXT: c |
| 115 | + // CHECK-OUTPUT-NEXT: d |
20 | 116 | print(c)
|
21 |
| - // CHECK: a |
22 |
| - // CHECK: b |
23 |
| - // CHECK: c |
24 |
| - // CHECK: d |
25 | 117 | }
|
26 | 118 | }
|
27 | 119 | }
|
| 120 | + |
| 121 | +func printArray(_ a: [Int]) { |
| 122 | + print("[", terminator: "") |
| 123 | + for (i, x) in a.enumerated() { |
| 124 | + print(x, terminator: i == a.count - 1 ? "" : ", ") |
| 125 | + } |
| 126 | + print("]") |
| 127 | +} |
| 128 | + |
0 commit comments