Skip to content

MandatoryPerformanceOptimizations: prevent inlining of dynamic-self class methods #74657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ private func shouldInline(apply: FullApplySite, callee: Function, alreadyInlined
return true
}

if callee.mayBindDynamicSelf {
// We don't support inlining a function that binds dynamic self into a global-init function
// because the global-init function cannot provide the self metadata.
return false
}

if apply.parentFunction.isGlobalInitOnceFunction && callee.inlineStrategy == .always {
// Some arithmetic operations, like integer conversions, are not transparent but `inline(__always)`.
// Force inlining them in global initializers so that it's possible to statically initialize the global.
Expand Down
13 changes: 13 additions & 0 deletions test/embedded/classes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func testCasting(_ title: StaticString, _ c: MyClass) {
}
}

public class DynamicSelfClass {
public static let ds = DynamicSelfClass()
public static let i: Int = 42
var x: Int

public init() {
self.x = Self.i
}
}

@main
struct Main {
static var o: (MyClass?, MyClass?, MyClass?) = (nil, nil, nil)
Expand Down Expand Up @@ -95,5 +105,8 @@ struct Main {
testCasting("subsub: ", MySubSubClass())
// CHECK: other: -
testCasting("other: ", OtherSubClass())

// CHECK: 42
print(DynamicSelfClass.ds.x)
}
}