Skip to content

Commit 2de4256

Browse files
committed
PerformanceInliner: prevent inlining of lazy property getters
Lazy property getters are usually non tivial functions (otherwise the user would not implement it as lazy property). Inlining such getters would most likely not benefit other optimizations because the top-level switch_enum cannot be constant folded in most cases. Also, not inlining lazy property getters enables optimizing them in CSE.
1 parent ae93e60 commit 2de4256

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

lib/SIL/SILFunctionBuilder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ SILFunction *SILFunctionBuilder::getOrCreateFunction(
179179
if (varDecl && varDecl->getAttrs().hasAttribute<LazyAttr>() &&
180180
accessor->getAccessorKind() == AccessorKind::Get) {
181181
F->setSpecialPurpose(SILFunction::Purpose::LazyPropertyGetter);
182+
183+
// Lazy property getters should not get inlined because they are usually
184+
// non-tivial functions (otherwise the user would not implement it as
185+
// lazy property). Inlining such getters would most likely not benefit
186+
// other optimizations because the top-level switch_enum cannot be
187+
// constant folded in most cases.
188+
// Also, not inlining lazy property getters enables optimizing them in
189+
// CSE.
190+
F->setInlineStrategy(NoInline);
182191
}
183192
}
184193
addFunctionAttributes(F, decl->getAttrs(), mod, getOrCreateDeclaration,

test/SILGen/lazy_properties.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// <rdar://problem/17405715> lazy property crashes silgen of implicit memberwise initializer
44

5-
// CHECK-LABEL: sil hidden [lazy_getter] [ossa] @$s15lazy_properties19StructWithLazyFieldV4onceSivg : $@convention(method) (@inout StructWithLazyField) -> Int
5+
// CHECK-LABEL: sil hidden [lazy_getter] [noinline] [ossa] @$s15lazy_properties19StructWithLazyFieldV4onceSivg : $@convention(method) (@inout StructWithLazyField) -> Int
66

77
// CHECK-LABEL: sil hidden [ossa] @$s15lazy_properties19StructWithLazyFieldV4onceSivs : $@convention(method) (Int, @inout StructWithLazyField) -> ()
88

@@ -23,7 +23,7 @@ func test21057425() {
2323

2424
// Anonymous closure parameters in lazy initializer crash SILGen
2525

26-
// CHECK-LABEL: sil hidden [lazy_getter] [ossa] @$s15lazy_properties22HasAnonymousParametersV1xSivg : $@convention(method) (@inout HasAnonymousParameters) -> Int
26+
// CHECK-LABEL: sil hidden [lazy_getter] [noinline] [ossa] @$s15lazy_properties22HasAnonymousParametersV1xSivg : $@convention(method) (@inout HasAnonymousParameters) -> Int
2727

2828
// CHECK-LABEL: sil private [ossa] @$s15lazy_properties22HasAnonymousParametersV1xSivgS2iXEfU_ : $@convention(thin) (Int) -> Int
2929

@@ -39,7 +39,7 @@ class LazyClass {
3939
lazy var x = 0
4040
}
4141

42-
// CHECK-LABEL: sil hidden [lazy_getter] [ossa] @$s15lazy_properties9LazyClassC1xSivg : $@convention(method) (@guaranteed LazyClass) -> Int
42+
// CHECK-LABEL: sil hidden [lazy_getter] [noinline] [ossa] @$s15lazy_properties9LazyClassC1xSivg : $@convention(method) (@guaranteed LazyClass) -> Int
4343
// CHECK: ref_element_addr %0 : $LazyClass, #LazyClass.$__lazy_storage_$_x
4444
// CHECK: return
4545

test/SILGen/objc_properties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class HasLazyProperty : NSObject, HasProperty {
242242
lazy var window = self.instanceMethod()
243243
}
244244

245-
// CHECK-LABEL: sil hidden [lazy_getter] [ossa] @$s15objc_properties15HasLazyPropertyC6windowSo8NSObjectCSgvg : $@convention(method) (@guaranteed HasLazyProperty) -> @owned Optional<NSObject> {
245+
// CHECK-LABEL: sil hidden [lazy_getter] [noinline] [ossa] @$s15objc_properties15HasLazyPropertyC6windowSo8NSObjectCSgvg : $@convention(method) (@guaranteed HasLazyProperty) -> @owned Optional<NSObject> {
246246
// CHECK: class_method %0 : $HasLazyProperty, #HasLazyProperty.instanceMethod!1 : (HasLazyProperty) -> () -> NSObject?
247247
// CHECK: return
248248

test/SILGen/opaque_result_type.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class Sub2 : Base {}
157157

158158
public class D {
159159
var cond = true
160-
// CHECK-LABEL: sil private [lazy_getter] [ossa] @$s18opaque_result_type1DC1c33_C2C55A4BAF30C3244D4A165D48A91142LLQrvg
160+
// CHECK-LABEL: sil private [lazy_getter] [noinline] [ossa] @$s18opaque_result_type1DC1c33_C2C55A4BAF30C3244D4A165D48A91142LLQrvg
161161
// CHECK: bb3([[RET:%[0-9]+]] : @owned $Base):
162162
// CHECH: return [[RET]]
163163
// CHECK: } // end sil function '$s18opaque_result_type1DC1c33_C2C55A4BAF30C3244D4A165D48A91142LLQrvg'

test/SILGen/optional_to_bool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ extension Int: P {}
55

66
public class A {}
77
public class B: A {
8-
// CHECK-LABEL: sil [lazy_getter] [ossa] @$s16optional_to_bool1BC1x{{[_0-9a-zA-Z]*}}vg
8+
// CHECK-LABEL: sil [lazy_getter] [noinline] [ossa] @$s16optional_to_bool1BC1x{{[_0-9a-zA-Z]*}}vg
99
// CHECK: switch_enum {{%.*}} : $Optional<Int>
1010
public lazy var x: Int = 0
11-
// CHECK-LABEL: sil [lazy_getter] [ossa] @$s16optional_to_bool1BC1y{{[_0-9a-zA-Z]*}}vg
11+
// CHECK-LABEL: sil [lazy_getter] [noinline] [ossa] @$s16optional_to_bool1BC1y{{[_0-9a-zA-Z]*}}vg
1212
// CHECK: switch_enum_addr {{%.*}} : $*Optional<P>
1313
public lazy var y: P = 0
1414
}

0 commit comments

Comments
 (0)