|
| 1 | +// Testcase for an IRGen crash with function pointers in static globals and multi-threaded compilation. |
| 2 | + |
| 3 | +// First test: check if the compilation succeeds and the code is correct. |
| 4 | + |
| 5 | +// RUN: %empty-directory(%t) |
| 6 | +// RUN: touch %t/empty.swift |
| 7 | +// RUN: %target-build-swift -O -wmo -parse-as-library -num-threads 2 -emit-module -emit-module-path=%t/Test.swiftmodule -module-name=Test %s %t/empty.swift -c -o %t/test.o |
| 8 | +// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %S/Inputs/global-functionptr-main.swift -c -o %t/main.o |
| 9 | +// RUN: %target-swiftc_driver %t/main.o %t/test.o -o %t/a.out |
| 10 | +// RUN: %target-codesign %t/a.out |
| 11 | +// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT |
| 12 | + |
| 13 | +// Second test (bonus): check if the optimization is done: statically initialize the array of function pointers. |
| 14 | + |
| 15 | +// RUN: %target-build-swift -O -wmo -parse-as-library -module-name=Test %s -emit-sil | %FileCheck %s -check-prefix=CHECK-SIL |
| 16 | + |
| 17 | +// REQUIRES: executable_test |
| 18 | +// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib |
| 19 | + |
| 20 | +internal protocol P { |
| 21 | + init() |
| 22 | +} |
| 23 | + |
| 24 | +private struct FuncPtr { |
| 25 | + let cl: () -> Void |
| 26 | + |
| 27 | + init<C: P>(_: C.Type) { |
| 28 | + self.cl = { _ = C.init() } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +public struct S: P { |
| 33 | + init() { |
| 34 | + print("init S") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// CHECK-SIL-LABEL: sil_global private @$s4Test8funcPtrs{{.*}}_WZTv_ : $_ContiguousArrayStorage<FuncPtr> = { |
| 39 | +// CHECK-SIL: %0 = function_ref @$s4Test7FuncPtr{{.*}}Tg5 : $@convention(thin) () -> () |
| 40 | +// CHECK-SIL: %initval = object $_ContiguousArrayStorage<FuncPtr> ({{%[0-9]+}} : $_ArrayBody, [tail_elems] {{%[0-9]+}} : $FuncPtr, {{%[0-9]+}} : $FuncPtr) |
| 41 | +private let funcPtrs = [ |
| 42 | + FuncPtr(S.self), |
| 43 | + FuncPtr(S.self)] |
| 44 | + |
| 45 | +public func testit(_ i: Int) { |
| 46 | + // CHECK-OUTPUT: init S |
| 47 | + funcPtrs[i].cl() |
| 48 | +} |
| 49 | + |
0 commit comments