File: closure_specialize_loop.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 (81 lines) | stat: -rw-r--r-- 3,744 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
// RUN: %{python} %S/../Inputs/timeout.py 10 %target-swift-frontend -O -parse-as-library %s -emit-sil | %FileCheck %s

public func callit() {
    testit { false }
}

// Check if the compiler terminates and does not full into an infinite optimization
// loop between the ClosureSpecializer and CapturePropagation.

// CHECK-LABEL: sil @$s23closure_specialize_loop6testit1cySbyc_tF
public func testit(c: @escaping () -> Bool) {
  if c() {
    testit { !c() }
  }
}

// PR: https://github.com/apple/swift/pull/61956
// Optimizing Expression.contains(where:) should not timeout.
//
// Repeated capture propagation leads to:
//   func contains$termPred@arg0$[termPred$falsePred@arg1]@arg1(expr) {
//     closure = termPred$[termPred$falsePred@arg1]@arg1
//     falsePred(expr)
//     contains$termPred@arg0$termPred$[termPred$falsePred@arg1]@arg1(expr)
//   }
//
//   func contains$termPred@arg0$termPred$[termPred$falsePred@arg1]@arg1(expr) {
//   closure = [termPred(termPred$[termPred$falsePred@arg1]@arg1)]
//   closure(expr)
//   contains$termPred@arg0(expr, closure)
// }
// The Demangled type tree looks like:
//   kind=FunctionSignatureSpecialization
//     kind=SpecializationPassID, index=3
//     kind=FunctionSignatureSpecializationParam
//     kind=FunctionSignatureSpecializationParam
//       kind=FunctionSignatureSpecializationParamKind, index=0
//       kind=FunctionSignatureSpecializationParamPayload, text="$s4test10ExpressionO8contains5whereS3bXE_tFSbACXEfU_S2bXEfU_36$s4test12IndirectEnumVACycfcS2bXEfU_Tf3npf_n"
//
// CHECK-LABEL: $s23closure_specialize_loop10ExpressionO8contains5whereS3bXE_tFSbACXEfU_S2bXEfU_012$s23closure_b7_loop10d44O8contains5whereS3bXE_tFSbACXEfU_S2bXEfU_012g13_b7_loop10d44ijk2_tlm2U_no52U_012g30_B34_loop12IndirectEnumVACycfcnO10U_Tf3npf_nY2_nTf3npf_n
// ---> function signature specialization
//     <Arg[1] = [Constant Propagated Function : function signature specialization
//                <Arg[1] = [Constant Propagated Function : function signature specialization
//                           <Arg[1] = [Constant Propagated Function : closure #1 (Swift.Bool) -> Swift.Bool
//                                      in closure_specialize_loop.IndirectEnum.init() -> closure_specialize_loop.IndirectEnum]>
//                           of closure #1 (Swift.Bool) -> Swift.Bool
//                           in closure #1 (closure_specialize_loop.Expression) -> Swift.Bool
//                           in closure_specialize_loop.Expression.contains(where: (Swift.Bool) -> Swift.Bool) -> Swift.Bool]>
//                of closure #1 (Swift.Bool) -> Swift.Bool
//                in closure #1 (closure_specialize_loop.Expression) -> Swift.Bool
//                in closure_specialize_loop.Expression.contains(where: (Swift.Bool) -> Swift.Bool) -> Swift.Bool]>
//     of closure #1 (Swift.Bool) -> Swift.Bool
//     in closure #1 (closure_specialize_loop.Expression) -> Swift.Bool
//     in closure_specialize_loop.Expression.contains(where: (Swift.Bool) -> Swift.Bool) -> Swift.Bool
//
public indirect enum Expression {
    case term(Bool)
    case list(_ expressions: [Expression])

    public func contains(where predicate: (Bool) -> Bool) -> Bool {
        switch self {
        case let .term(term):
            return predicate(term)
        case let .list(expressions):
            return expressions.contains { expression in
                expression.contains { term in
                    predicate(term)
                }
            }
        }
    }
}

public struct IndirectEnum {
    public init() {
        let containsFalse = Expression.list([.list([.term(true), .term(false)]), .term(true)]).contains { term in
            term == false
        }
        print(containsFalse)
    }
}