[embedded] Make CMO's 'serialize everything' mode even more aggressive and allow serialization of private and shared functions

This commit is contained in:
Kuba Mracek
2023-09-21 16:35:58 -07:00
parent 40362469ca
commit 770dcd1614
5 changed files with 113 additions and 41 deletions

View File

@@ -69,7 +69,8 @@ enum class DestroyHoistingOption : uint8_t {
enum class CrossModuleOptimizationMode : uint8_t {
Off = 0,
Default = 1,
Aggressive = 2
Aggressive = 2,
Everything = 3,
};
class SILModule;

View File

@@ -3117,6 +3117,7 @@ bool CompilerInvocation::parseArgs(
if (LangOpts.hasFeature(Feature::Embedded)) {
IRGenOpts.InternalizeAtLink = true;
IRGenOpts.DisableLegacyTypeInfo = true;
SILOpts.CMOMode = CrossModuleOptimizationMode::Everything;
}
return false;

View File

@@ -60,11 +60,15 @@ class CrossModuleOptimization {
/// avoid code size increase.
bool conservative;
/// True if CMO should serialize literally everything in the module,
/// regardless of linkage.
bool everything;
typedef llvm::DenseMap<SILFunction *, bool> FunctionFlags;
public:
CrossModuleOptimization(SILModule &M, bool conservative)
: M(M), conservative(conservative) { }
CrossModuleOptimization(SILModule &M, bool conservative, bool everything)
: M(M), conservative(conservative), everything(everything) { }
void serializeFunctionsInModule();
@@ -164,11 +168,12 @@ void CrossModuleOptimization::serializeFunctionsInModule() {
// Start with public functions.
for (SILFunction &F : M) {
if (F.getLinkage() == SILLinkage::Public) {
if (canSerializeFunction(&F, canSerializeFlags, /*maxDepth*/ 64))
if (F.getLinkage() == SILLinkage::Public || everything) {
if (canSerializeFunction(&F, canSerializeFlags, /*maxDepth*/ 64)) {
serializeFunction(&F, canSerializeFlags);
}
}
}
}
/// Recursively walk the call graph and select functions to be serialized.
@@ -189,6 +194,11 @@ bool CrossModuleOptimization::canSerializeFunction(
// it to true at the end of this function.
canSerializeFlags[function] = false;
if (everything) {
canSerializeFlags[function] = true;
return true;
}
if (DeclContext *funcCtxt = function->getDeclContext()) {
if (!canUseFromInline(funcCtxt))
return false;
@@ -392,6 +402,9 @@ static bool couldBeLinkedStatically(DeclContext *funcCtxt, SILModule &module) {
/// Returns true if the \p declCtxt can be used from a serialized function.
bool CrossModuleOptimization::canUseFromInline(DeclContext *declCtxt) {
if (everything)
return true;
if (!M.getSwiftModule()->canBeUsedForCrossModuleOptimization(declCtxt))
return false;
@@ -410,6 +423,9 @@ bool CrossModuleOptimization::canUseFromInline(DeclContext *declCtxt) {
/// Returns true if the function \p func can be used from a serialized function.
bool CrossModuleOptimization::canUseFromInline(SILFunction *function) {
if (everything)
return true;
if (DeclContext *funcCtxt = function->getDeclContext()) {
if (!canUseFromInline(funcCtxt))
return false;
@@ -439,14 +455,12 @@ bool CrossModuleOptimization::shouldSerialize(SILFunction *function) {
if (function->isSerialized())
return false;
if (everything)
return true;
if (function->hasSemanticsAttr("optimize.no.crossmodule"))
return false;
// In embedded Swift we serialize everything.
if (SerializeEverything ||
function->getASTContext().LangOpts.hasFeature(Feature::Embedded))
return true;
if (!conservative) {
// The basic heuristic: serialize all generic functions, because it makes a
// huge difference if generic functions can be specialized or not.
@@ -654,21 +668,27 @@ class CrossModuleOptimizationPass: public SILModuleTransform {
return;
bool conservative = false;
// In embedded Swift we serialize everything.
if (!M.getASTContext().LangOpts.hasFeature(Feature::Embedded)) {
bool everything = SerializeEverything;
switch (M.getOptions().CMOMode) {
case swift::CrossModuleOptimizationMode::Off:
return;
break;
case swift::CrossModuleOptimizationMode::Default:
conservative = true;
break;
case swift::CrossModuleOptimizationMode::Aggressive:
conservative = false;
break;
}
case swift::CrossModuleOptimizationMode::Everything:
everything = true;
break;
}
CrossModuleOptimization CMO(M, conservative);
if (!everything &&
M.getOptions().CMOMode == swift::CrossModuleOptimizationMode::Off) {
return;
}
CrossModuleOptimization CMO(M, conservative, everything);
CMO.serializeFunctionsInModule();
}
};

View File

@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-ir -target armv7-apple-none-macho -parse-stdlib -module-name Swift %s -enable-experimental-feature Embedded -wmo -parse-as-library | %FileCheck %s
// RUN: %target-swift-emit-ir -target armv7-apple-none-macho -parse-stdlib -module-name Swift %s -enable-experimental-feature Embedded -wmo -parse-as-library | %FileCheck %s --check-prefix CHECK --check-prefix CHECK-NONOPT
// RUN: %target-swift-emit-ir -target armv7-apple-none-macho -parse-stdlib -module-name Swift %s -enable-experimental-feature Embedded -wmo -parse-as-library -O | %FileCheck %s
// REQUIRES: swift_in_compiler
@@ -98,19 +98,19 @@ extension MyPublicEnum {
public static var static_mypublicstruct_4: MyPublicStruct = MyPublicStruct(x: 0, y: 0)
}
// CHECK-NOT: global_int_1
// CHECK-NONOPT: @"$ss12global_int_133_056BEF60D619AD2945081A9CBFC2AAE9LLSivp" = {{.*}}zeroinitializer
// CHECK: @"$ss12global_int_2Sivp" = {{.*}}zeroinitializer
// CHECK-NOT: global_int_3
// CHECK-NONOPT: @"$ss12global_int_333_056BEF60D619AD2945081A9CBFC2AAE9LLSivp" = {{.*}}zeroinitializer
// CHECK: @"$ss12global_int_4Sivp" = {{.*}}zeroinitializer
// CHECK-NOT: static_int_1
// CHECK-NONOPT: @"$ss12MyPublicEnumO12static_int_133_056BEF60D619AD2945081A9CBFC2AAE9LLSivpZ" = {{.*}}zeroinitializer
// CHECK: @"$ss12MyPublicEnumO12static_int_2SivpZ" = {{.*}}zeroinitializer
// CHECK-NOT: static_int_3
// CHECK-NONOPT: @"$ss12MyPublicEnumO12static_int_333_056BEF60D619AD2945081A9CBFC2AAE9LLSivpZ" = {{.*}}zeroinitializer
// CHECK: @"$ss12MyPublicEnumO12static_int_4SivpZ" = {{.*}}zeroinitializer
// CHECK-NOT: global_my_publicstruct_1
// CHECK-NONOPT: @"$ss24global_my_publicstruct_133_056BEF60D619AD2945081A9CBFC2AAE9LLs14MyPublicStructVvp" = {{.*}}zeroinitializer
// CHECK: @"$ss24global_my_publicstruct_2s14MyPublicStructVvp" = {{.*}}zeroinitializer
// CHECK-NOT: global_my_publicstruct_3
// CHECK-NONOPT: @"$ss24global_my_publicstruct_333_056BEF60D619AD2945081A9CBFC2AAE9LLs14MyPublicStructVvp" = {{.*}}zeroinitializer
// CHECK: @"$ss24global_my_publicstruct_4s14MyPublicStructVvp" = {{.*}}zeroinitializer
// CHECK-NOT: stati_my_publicstruct_1
// CHECK-NOT: static_my_publicstruct_1
// CHECK: @"$ss12MyPublicEnumO23static_mypublicstruct_2s0aB6StructVvpZ" = {{.*}}zeroinitializer
// CHECK-NOT: stati_my_publicstruct_3
// CHECK-NOT: static_my_publicstruct_3
// CHECK: @"$ss12MyPublicEnumO23static_mypublicstruct_4s0aB6StructVvpZ" = {{.*}}zeroinitializer

View File

@@ -0,0 +1,50 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
// RUN: %target-swift-frontend -c -I %t %t/Main.swift -enable-experimental-feature Embedded -o %t/a.o
// RUN: %target-clang -x c -c %S/Inputs/tiny-runtime-dummy-refcounting.c -o %t/runtime.o
// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o
// RUN: %target-clang %t/a.o %t/print.o %t/runtime.o -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: VENDOR=apple
// REQUIRES: OS=macosx
// BEGIN MyModule.swift
@_silgen_name("putchar")
func putchar(_: UInt8)
public func print(_ s: StaticString, terminator: StaticString) {
var p = s.utf8Start
while p.pointee != 0 {
putchar(p.pointee)
p += 1
}
p = terminator.utf8Start
while p.pointee != 0 {
putchar(p.pointee)
p += 1
}
}
@_silgen_name("print_long")
func print_long(_: Int)
public func print(_ n: Int, terminator: StaticString) {
print_long(n)
print("", terminator: terminator)
}
// BEGIN Main.swift
import MyModule
func test() {
print("Hello world", terminator: "\n") // CHECK: Hello world
print(42, terminator: "\n") // CHECK-NEXT: 42
}
test()