File: devirt_witness_cross_module.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; 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 (98 lines) | stat: -rw-r--r-- 3,212 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
// RUN: %empty-directory(%t)

// First test: without cross-module-optimization

// RUN: %target-build-swift -O -wmo -disable-cmo -parse-as-library -DMODULE -emit-module -emit-module-path=%t/Module.swiftmodule -module-name=Module %s
// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %s -c -emit-sil | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-NOCMO %s

// Second test: with cross-module-optimization (which is the default)

// 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
// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %s -c -emit-sil | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-CMO %s

#if MODULE

public protocol P {
  func foo(x: [Int])
  func bar(x: [Int])
  func baz(x: some RandomAccessCollection<Int>)
}

public struct S: P {
  public init() {}

  @inline(never)
  public func foo(x: some RandomAccessCollection<Int>) { }
  @inline(never)
  public func bar(x: [Int]) { }
  @inline(never)
  public func baz(x: some RandomAccessCollection<Int>) { }
}

#else

import Module

public struct Local: P {
  @inline(never)
  public func foo(x: some RandomAccessCollection<Int>) { }
  @inline(never)
  public func bar(x: [Int]) { }
  @inline(never)
  public func baz(x: some RandomAccessCollection<Int>) { }
}

// Don't devirtualize in this case because it's better to call the non-generic function
// (which can be fully specialized in the module) via the witness table than the generic
// de-virtualized function.

// CHECK-LABEL: sil @$s4Main24testGenericInOtherModuleyyF
// CHECK-NOCMO:   [[F:%[0-9]+]] = witness_method $S, #P.foo
// CHECK-CMO:     [[F:%[0-9]+]] = function_ref @$s6Module1SV3foo1xyx_tSkRzSi7ElementRtzlFSaySiG_Ttgq5{{.*}}
// CHECK:         apply [[F]]
// CHECK:       } // end sil function '$s4Main24testGenericInOtherModuleyyF'
public func testGenericInOtherModule() {
  let s = S()
  callFoo(s, x: [])
}

// CHECK-LABEL: sil @$s4Main27testNonGenericInOtherModuleyyF
// CHECK:         [[F:%[0-9]+]] = function_ref @$s6Module1SV3bar1xySaySiG_tF
// CHECK:         apply [[F]]
// CHECK:       } // end sil function '$s4Main27testNonGenericInOtherModuleyyF'
public func testNonGenericInOtherModule() {
  let s = S()
  callBar(s, x: [])
}

// CHECK-LABEL: sil @$s4Main35testGenericRequirementInOtherModuleyyF
// CHECK:         [[F:%[0-9]+]] = function_ref @$s6Module1SV3baz1xyx_tSkRzSi7ElementRtzlF{{.*}}
// CHECK:         apply [[F]]
// CHECK:       } // end sil function '$s4Main35testGenericRequirementInOtherModuleyyF'
public func testGenericRequirementInOtherModule() {
  let s = S()
  callBaz(s, x: [])
}

// CHECK-LABEL: sil @$s4Main23testGenericInSameModuleyyF
// CHECK:         [[F:%[0-9]+]] = function_ref @$s4Main5LocalV3foo1xyx_tSkRzSi7ElementRtzlFSaySiG_Ttg5
// CHECK:         apply [[F]]
// CHECK:       } // end sil function '$s4Main23testGenericInSameModuleyyF'
public func testGenericInSameModule() {
  let l = Local()
  callFoo(l, x: [])
}

func callFoo<T: P>(_ f: T, x: [Int]) {
  f.foo(x: x)
}

func callBar<T: P>(_ f: T, x: [Int]) {
  f.bar(x: x)
}

func callBaz<T: P>(_ f: T, x: [Int]) {
  f.baz(x: x)
}

#endif