File: curry-thunk-elimination.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 (45 lines) | stat: -rw-r--r-- 1,179 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
// RUN: %target-swift-frontend  -primary-file %s -O -sil-verify-all -module-name=test -emit-sil | %FileCheck %s

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

// REQUIRES: executable_test,swift_stdlib_no_asserts,optimized_stdlib

protocol P {
    func foo() throws -> Int
}

struct S: P {
    var a: Int
    var b: Int
    var c: Int
    var d: Int

    func foo() throws -> Int {
        return a + b + c + d
    }
}

func callClosure<R>(_ body: () throws -> R) rethrows -> R {
    try body()
}

// Check that the optimizer can eliminat all calls to thunks and directly
// try_apply's the witness method.

// CHECK-LABEL: sil hidden [noinline] @$s4test6testitySiSgAA1P_pF
// CHECK:   [[METHOD:%[0-9]+]] = witness_method $@opened{{.*}} #P.foo
// CHECK:   try_apply [[METHOD]]<@opened
// CHECK: // end sil function '$s4test6testitySiSgAA1P_pF'
@inline(never)
func testit(_ p: P) -> Int? {
    return try? callClosure(p.foo)
}

let p: P = S(a: 1, b: 2, c: 3, d: 4)

// CHECK-OUTPUT: 10
print(testit(p)!)