Skip to content

[SILOptimizer] Don't apply CMO to key paths that reference inaccessib… #79506

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
Feb 25, 2025
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
15 changes: 15 additions & 0 deletions lib/SILOptimizer/IPO/CrossModuleOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,21 @@ bool CrossModuleOptimization::canSerializeFieldsByInstructionKind(
canUse = methodScope.isPublicOrPackage();
}
});
auto pattern = KPI->getPattern();
for (auto &component : pattern->getComponents()) {
if (!canUse) {
break;
}
switch (component.getKind()) {
case KeyPathPatternComponent::Kind::StoredProperty: {
auto property = component.getStoredPropertyDecl();
canUse = isPackageOrPublic(property->getEffectiveAccess());
break;
}
default:
break;
}
}
return canUse;
}
if (auto *MI = dyn_cast<MethodInst>(inst)) {
Expand Down
8 changes: 8 additions & 0 deletions test/SILOptimizer/Inputs/cross-module/cross-module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,11 @@ public struct S<each T : Visitable> {
_ = (repeat (each storage).visit())
}
}

public struct StructWithInternal {
var internalVar: Int
}

public func getKP() -> KeyPath<StructWithInternal, Int> {
return \StructWithInternal.internalVar
}
6 changes: 5 additions & 1 deletion test/SILOptimizer/cross-module-optimization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func testPrivateVar() {
print(getRandom())
}

func testKeyPathAccess() -> KeyPath<StructWithInternal, Int> {
return getKP()
}

testNestedTypes()
testClass()
testError()
Expand All @@ -182,4 +186,4 @@ testMisc()
testGlobal()
testImplementationOnly()
testPrivateVar()

testKeyPathAccess()