File: globalopt_resilience.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 (79 lines) | stat: -rw-r--r-- 3,509 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
// RUN: %target-swift-frontend  -O -module-name=test -enable-library-evolution -emit-sil -primary-file %s | %FileCheck %s

// REQUIRES: swift_in_compiler

// Check if GlobalOpt generates the optimal getter for a static property with a resilient type.
// The (resilient) getter should just return the literal (and not lazily initialize a global variable).

// CHECK-LABEL: sil @$s4test15ResilientStructV9staticValACvgZ :
// CHECK:     bb0(%0 : $*ResilientStruct{{.*}}):
// CHECK:       [[L:%[0-9]+]] = integer_literal {{.*}}, 27
// CHECK:       [[I:%[0-9]+]] = struct $Int ([[L]] : {{.*}})
// CHECK:       [[S:%[0-9]+]] = struct $ResilientStruct ([[I]] : $Int)
// CHECK:       store [[S]] to %0
// CHECK:       return

public struct ResilientStruct {
  var rawValue: Int

  public static let staticVal = ResilientStruct(rawValue: 27)

  @_optimize(none)
  public func method() {}
}

public func cannotConvertToValueUse() {
  // Previously we could not optimize this because the method takes the
  // resilient value as @in_guaranteed. But now SILGen produces better
  // code for us up-front, so now its optimized anyway.
  ResilientStruct.staticVal.method()
}

@inlinable public func cannotConvertToValueUseAlt() {
  // SILGen still produces address-only code in the inlinable case.
  // We also optimize this correctly, but with a slightly different
  // pattern.
  ResilientStruct.staticVal.method()
}

// CHECK-LABEL: sil @$s4test23cannotConvertToValueUseyyF : $@convention(thin) () -> ()
// CHECK: [[INT:%.*]] = integer_literal $Builtin.Int{{32|64}}, 27
// CHECK: [[S1:%.*]] = struct $Int ([[INT]] : $Builtin.Int{{32|64}})
// CHECK: [[S2:%.*]] = struct $ResilientStruct ([[S1]] : $Int)
// CHECK: [[TMP:%.*]] = alloc_stack $ResilientStruct
// CHECK: store [[S2]] to [[TMP]] : $*ResilientStruct
// CHECK: [[METHOD:%.*]] = function_ref @$s4test15ResilientStructV6methodyyF : $@convention(method) (@in_guaranteed ResilientStruct) -> ()
// CHECK: apply [[METHOD]]([[TMP]]) : $@convention(method) (@in_guaranteed ResilientStruct) -> ()
// CHECK: [[RESULT:%.*]] = tuple ()
// CHECK: return [[RESULT]] : $()

// CHECK-LABEL: sil @$s4test26cannotConvertToValueUseAltyyF : $@convention(thin) () -> ()
// CHECK: [[TMP:%.*]] = alloc_stack $ResilientStruct
// CHECK: [[INT:%.*]] = integer_literal $Builtin.Int{{32|64}}, 27
// CHECK: [[S1:%.*]] = struct $Int ([[INT]] : $Builtin.Int{{32|64}})
// CHECK: [[S2:%.*]] = struct $ResilientStruct ([[S1]] : $Int)
// CHECK: store [[S2]] to [[TMP]] : $*ResilientStruct
// CHECK: [[METHOD:%.*]] = function_ref @$s4test15ResilientStructV6methodyyF : $@convention(method) (@in_guaranteed ResilientStruct) -> ()
// CHECK: apply [[METHOD]]([[TMP]]) : $@convention(method) (@in_guaranteed ResilientStruct) -> ()
// CHECK: [[RESULT:%.*]] = tuple ()
// CHECK: return [[RESULT]] : $()

internal struct WrapperStruct {
  var inner: ResilientStruct = .staticVal

  init() {}
}

func returnWrapperStruct() -> WrapperStruct {
  return WrapperStruct()
}

// CHECK-LABEL: sil hidden @$s4test19returnWrapperStructAA0cD0VyF : $@convention(thin) () -> @out WrapperStruct
// CHECK: bb0(%0 : $*WrapperStruct):
// CHECK: [[INT:%.*]] = integer_literal $Builtin.Int{{32|64}}, 27
// CHECK: [[S1:%.*]] = struct $Int ([[INT]] : $Builtin.Int{{32|64}})
// CHECK: [[S2:%.*]] = struct $ResilientStruct ([[S1]] : $Int)
// CHECK: [[S3:%.*]] = struct $WrapperStruct ([[S2]] : $ResilientStruct)
// CHECK: store [[S3]] to %0 : $*WrapperStruct
// CHECK: [[RESULT:%.*]] = tuple ()
// CHECK: return [[RESULT]] : $()