Skip to content

[6.0] MandatoryPerformanceOptimizations: prevent inlining of dynamic-self class methods #74678

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 2 commits into from
Jun 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ extension Function {
}
return nil
}

var mayBindDynamicSelf: Bool {
self.bridged.mayBindDynamicSelf()
}
}

extension FullApplySite {
Expand Down
1 change: 1 addition & 0 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ struct BridgedFunction {
BRIDGED_INLINE bool hasSemanticsAttr(BridgedStringRef attrName) const;
BRIDGED_INLINE bool hasUnsafeNonEscapableResult() const;
BRIDGED_INLINE bool hasResultDependsOnSelf() const;
bool mayBindDynamicSelf() const;
BRIDGED_INLINE EffectsKind getEffectAttribute() const;
BRIDGED_INLINE PerformanceConstraints getPerformanceConstraints() const;
BRIDGED_INLINE InlineStrategy getInlineStrategy() const;
Expand Down
7 changes: 7 additions & 0 deletions lib/SILOptimizer/PassManager/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,13 @@ void SwiftPassInvocation::endVerifyFunction() {

SwiftPassInvocation::~SwiftPassInvocation() {}

//===----------------------------------------------------------------------===//
// SIL Bridging
//===----------------------------------------------------------------------===//
bool BridgedFunction::mayBindDynamicSelf() const {
return swift::mayBindDynamicSelf(getFunction());
}

//===----------------------------------------------------------------------===//
// OptimizerBridging
//===----------------------------------------------------------------------===//
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)
}
}