|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | + |
| 3 | +// First test: without cross-module-optimization |
| 4 | + |
| 5 | +// RUN: %target-build-swift -O -wmo -disable-cmo -parse-as-library -DMODULE -emit-module -emit-module-path=%t/Module.swiftmodule -module-name=Module %s |
| 6 | +// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %s -c -emit-sil | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-NOCMO %s |
| 7 | + |
| 8 | +// Second test: with cross-module-optimization (which is the default) |
| 9 | + |
| 10 | +// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -DMODULE -emit-module -emit-module-path=%t/Module.swiftmodule -module-name=Module %s |
| 11 | +// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %s -c -emit-sil | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-CMO %s |
| 12 | + |
| 13 | +#if MODULE |
| 14 | + |
| 15 | +public protocol P { |
| 16 | + func foo(x: [Int]) |
| 17 | + func bar(x: [Int]) |
| 18 | + func baz(x: some RandomAccessCollection<Int>) |
| 19 | +} |
| 20 | + |
| 21 | +public struct S: P { |
| 22 | + public init() {} |
| 23 | + |
| 24 | + @inline(never) |
| 25 | + public func foo(x: some RandomAccessCollection<Int>) { } |
| 26 | + @inline(never) |
| 27 | + public func bar(x: [Int]) { } |
| 28 | + @inline(never) |
| 29 | + public func baz(x: some RandomAccessCollection<Int>) { } |
| 30 | +} |
| 31 | + |
| 32 | +#else |
| 33 | + |
| 34 | +import Module |
| 35 | + |
| 36 | +public struct Local: P { |
| 37 | + @inline(never) |
| 38 | + public func foo(x: some RandomAccessCollection<Int>) { } |
| 39 | + @inline(never) |
| 40 | + public func bar(x: [Int]) { } |
| 41 | + @inline(never) |
| 42 | + public func baz(x: some RandomAccessCollection<Int>) { } |
| 43 | +} |
| 44 | + |
| 45 | +// Don't devirtualize in this case because it's better to call the non-generic function |
| 46 | +// (which can be fully specialized in the module) via the witness table than the generic |
| 47 | +// de-virtualized function. |
| 48 | + |
| 49 | +// CHECK-LABEL: sil @$s4Main24testGenericInOtherModuleyyF |
| 50 | +// CHECK-NOCMO: [[F:%[0-9]+]] = witness_method $S, #P.foo |
| 51 | +// CHECK-CMO: [[F:%[0-9]+]] = function_ref @$s6Module1SV3foo1xyx_tSkRzSi7ElementRtzlFSaySiG_Tgq5{{.*}} |
| 52 | +// CHECK: apply [[F]] |
| 53 | +// CHECK: } // end sil function '$s4Main24testGenericInOtherModuleyyF' |
| 54 | +public func testGenericInOtherModule() { |
| 55 | + let s = S() |
| 56 | + callFoo(s, x: []) |
| 57 | +} |
| 58 | + |
| 59 | +// CHECK-LABEL: sil @$s4Main27testNonGenericInOtherModuleyyF |
| 60 | +// CHECK: [[F:%[0-9]+]] = function_ref @$s6Module1SV3bar1xySaySiG_tF |
| 61 | +// CHECK: apply [[F]] |
| 62 | +// CHECK: } // end sil function '$s4Main27testNonGenericInOtherModuleyyF' |
| 63 | +public func testNonGenericInOtherModule() { |
| 64 | + let s = S() |
| 65 | + callBar(s, x: []) |
| 66 | +} |
| 67 | + |
| 68 | +// CHECK-LABEL: sil @$s4Main35testGenericRequirementInOtherModuleyyF |
| 69 | +// CHECK: [[F:%[0-9]+]] = function_ref @$s6Module1SV3baz1xyx_tSkRzSi7ElementRtzlF{{.*}} |
| 70 | +// CHECK: apply [[F]] |
| 71 | +// CHECK: } // end sil function '$s4Main35testGenericRequirementInOtherModuleyyF' |
| 72 | +public func testGenericRequirementInOtherModule() { |
| 73 | + let s = S() |
| 74 | + callBaz(s, x: []) |
| 75 | +} |
| 76 | + |
| 77 | +// CHECK-LABEL: sil @$s4Main23testGenericInSameModuleyyF |
| 78 | +// CHECK: [[F:%[0-9]+]] = function_ref @$s4Main5LocalV3foo1xyx_tSkRzSi7ElementRtzlFSaySiG_Tg5 |
| 79 | +// CHECK: apply [[F]] |
| 80 | +// CHECK: } // end sil function '$s4Main23testGenericInSameModuleyyF' |
| 81 | +public func testGenericInSameModule() { |
| 82 | + let l = Local() |
| 83 | + callFoo(l, x: []) |
| 84 | +} |
| 85 | + |
| 86 | +func callFoo<T: P>(_ f: T, x: [Int]) { |
| 87 | + f.foo(x: x) |
| 88 | +} |
| 89 | + |
| 90 | +func callBar<T: P>(_ f: T, x: [Int]) { |
| 91 | + f.bar(x: x) |
| 92 | +} |
| 93 | + |
| 94 | +func callBaz<T: P>(_ f: T, x: [Int]) { |
| 95 | + f.baz(x: x) |
| 96 | +} |
| 97 | + |
| 98 | +#endif |
0 commit comments