Skip to content

Commit 1f9588e

Browse files
committed
MandatoryPerformanceOptimizations: prevent inlining of dynamic-self class methods
This fixes a compiler crash in embedded swift. rdar://129241915
1 parent 026cb41 commit 1f9588e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ private func shouldInline(apply: FullApplySite, callee: Function, alreadyInlined
204204
return true
205205
}
206206

207+
if callee.mayBindDynamicSelf {
208+
// We don't support inlining a function that binds dynamic self into a global-init function
209+
// because the global-init function cannot provide the self metadata.
210+
return false
211+
}
212+
207213
if apply.parentFunction.isGlobalInitOnceFunction && callee.inlineStrategy == .always {
208214
// Some arithmetic operations, like integer conversions, are not transparent but `inline(__always)`.
209215
// Force inlining them in global initializers so that it's possible to statically initialize the global.

test/embedded/classes.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ func testCasting(_ title: StaticString, _ c: MyClass) {
4343
}
4444
}
4545

46+
public class DynamicSelfClass {
47+
public static let ds = DynamicSelfClass()
48+
public static let i: Int = 42
49+
var x: Int
50+
51+
public init() {
52+
self.x = Self.i
53+
}
54+
}
55+
4656
@main
4757
struct Main {
4858
static var o: (MyClass?, MyClass?, MyClass?) = (nil, nil, nil)
@@ -95,5 +105,8 @@ struct Main {
95105
testCasting("subsub: ", MySubSubClass())
96106
// CHECK: other: -
97107
testCasting("other: ", OtherSubClass())
108+
109+
// CHECK: 42
110+
print(DynamicSelfClass.ds.x)
98111
}
99112
}

0 commit comments

Comments
 (0)