Skip to content

[Package CMO] Require library-evolution flag. #76278

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
Sep 6, 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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ ERROR(no_allocations_without_embedded,none,
ERROR(no_swift_sources_with_embedded,none,
"embedded swift cannot be enabled in a compiler built without Swift sources", ())

ERROR(package_cmo_requires_library_evolution, none,
"Library evolution must be enabled for Package CMO", ())

ERROR(experimental_not_supported_in_production,none,
"experimental feature '%0' cannot be enabled in production compiler",
(StringRef))
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,8 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
Diags.diagnose(SourceLoc(), diag::ignoring_option_requires_option,
"-package-cmo",
"-allow-non-resilient-access");
} else if (!FEOpts.EnableLibraryEvolution) {
Diags.diagnose(SourceLoc(), diag::package_cmo_requires_library_evolution);
} else {
Opts.EnableSerializePackage = true;
Opts.CMOMode = CrossModuleOptimizationMode::Default;
Expand Down
13 changes: 11 additions & 2 deletions lib/SILOptimizer/IPO/CrossModuleOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,9 +947,15 @@ void CrossModuleOptimization::makeSubstUsableFromInline(
class CrossModuleOptimizationPass: public SILModuleTransform {
void run() override {
auto &M = *getModule();
if (M.getSwiftModule()->isResilient() &&
!M.getSwiftModule()->serializePackageEnabled())
if (M.getSwiftModule()->serializePackageEnabled()) {
assert(M.getSwiftModule()->isResilient() &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the assert, we should also return when M is resilient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup it's in the else if line below

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see. Thank you!

"Package CMO requires library-evolution");
} else if (M.getSwiftModule()->isResilient()) {
// If no Package CMO flags are passed and library
// evolution is enabled, just return.
return;
}

if (!M.isWholeModule())
return;

Expand All @@ -974,6 +980,9 @@ class CrossModuleOptimizationPass: public SILModuleTransform {
return;
}

if (isPackageCMOEnabled(M.getSwiftModule()))
assert(conservative && "Package CMO requires conservative CMO mode");

CrossModuleOptimization CMO(M, conservative, everything);
CMO.serializeFunctionsInModule(PM);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -O -wmo -Xfrontend -experimental-package-cmo -Xfrontend -experimental-allow-non-resilient-access -parse-as-library -emit-module -emit-module-path=%t/Submodule.swiftmodule -module-name=Submodule -package-name Pkg %S/Inputs/cross-module/default-submodule.swift -c -o %t/submodule.o
// RUN: %target-build-swift -O -wmo -Xfrontend -experimental-package-cmo -Xfrontend -experimental-allow-non-resilient-access -parse-as-library -emit-module -emit-module-path=%t/Module.swiftmodule -module-name=Module -package-name Pkg -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/module.o
// RUN: %target-build-swift -O -wmo -Xfrontend -experimental-package-cmo -Xfrontend -experimental-allow-non-resilient-access -parse-as-library -emit-tbd -emit-tbd-path %t/ModuleTBD.tbd -emit-module -emit-module-path=%t/ModuleTBD.swiftmodule -module-name=ModuleTBD -package-name Pkg -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/moduletbd.o -Xfrontend -tbd-install_name -Xfrontend module
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-module -emit-module-path=%t/Submodule.swiftmodule -module-name=Submodule -package-name Pkg %S/Inputs/cross-module/default-submodule.swift -c -o %t/submodule.o
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-module -emit-module-path=%t/Module.swiftmodule -module-name=Module -package-name Pkg -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/module.o
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-tbd -emit-tbd-path %t/ModuleTBD.tbd -emit-module -emit-module-path=%t/ModuleTBD.swiftmodule -module-name=ModuleTBD -package-name Pkg -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/moduletbd.o -Xfrontend -tbd-install_name -Xfrontend module

// RUN: %target-build-swift -O -wmo -module-name=Main -package-name Pkg -I%t -I%S/Inputs/cross-module %s -emit-sil -o %t/Main.sil
// RUN: %FileCheck %s < %t/Main.sil
Expand Down
33 changes: 33 additions & 0 deletions test/SILOptimizer/package-cmo-require-library-evolution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %empty-directory(%t)

// RUN: not %target-build-swift %s \
// RUN: -module-name=Lib -package-name Pkg \
// RUN: -emit-module -o %t/Lib.swiftmodule -I%t \
// RUN: -Xfrontend -package-cmo \
// RUN: -Xfrontend -allow-non-resilient-access \
// RUN: -O -wmo 2>&1 | %FileCheck %s --check-prefix CHECK-DIAGS
// CHECK-DIAGS: error: Library evolution must be enabled for Package CMO

// RUN: %target-build-swift %s \
// RUN: -module-name=Lib -package-name Pkg \
// RUN: -emit-module -o %t/Lib.swiftmodule -I%t \
// RUN: -Xfrontend -package-cmo \
// RUN: -Xfrontend -allow-non-resilient-access \
// RUN: -enable-library-evolution \
// RUN: -O -wmo

// RUN: llvm-bcanalyzer %t/Lib.swiftmodule | %FileCheck %s -check-prefix=CHECK-BC
// CHECK-BC: SERIALIZE_PACKAGE_ENABLED

// REQUIRES: swift_in_compiler

public struct S {
public init() {}
package var x: Int {
get { return 0 }
set {}
}
package func f() -> Int {
return 1
}
}
5 changes: 4 additions & 1 deletion test/SILOptimizer/package-cmo-resilient-mode.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

/// Enable Package CMO; conservative mode on resilient module.
// RUN: %target-build-swift %t/Lib.swift \
// RUN: -module-name=Lib -package-name Pkg \
// RUN: -parse-as-library -emit-module -emit-module-path %t/Lib.swiftmodule -I%t \
Expand All @@ -18,10 +19,12 @@

// RUN: rm -rf %t/Lib.swiftmodule

/// Enable non-package CMO; conservative mode on non-resilient module,
/// and compare results with Package CMO.
// RUN: %target-build-swift %t/Lib.swift \
// RUN: -module-name=Lib -package-name Pkg \
// RUN: -parse-as-library -emit-module -emit-module-path %t/Lib.swiftmodule -I%t \
// RUN: -Xfrontend -experimental-package-cmo -Xfrontend -experimental-allow-non-resilient-access \
// RUN: -Xfrontend -enable-default-cmo \
// RUN: -O -wmo

// RUN: %target-sil-opt %t/Lib.swiftmodule -sil-verify-all -o %t/Lib-non-res.sil
Expand Down