Skip to content

Commit c978092

Browse files
committed
Merge pull request #1392 from swiftix/wip-prespecializations-module
Move pre-specializations of popular types away from the stdlib to reduce the stdlib code size.
2 parents e99961e + 4d4c2cd commit c978092

File tree

9 files changed

+39
-8
lines changed

9 files changed

+39
-8
lines changed

include/swift/Strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace swift {
2727
static const char LLVM_IR_EXTENSION[] = "ll";
2828
/// The name of the standard library, which is a reserved module name.
2929
static const char STDLIB_NAME[] = "Swift";
30+
/// The name of the Onone support library, which is a reserved module name.
31+
static const char SWIFT_ONONE_SUPPORT[] = "SwiftOnoneSupport";
3032
/// The name of the SwiftShims module, which contains private stdlib decls.
3133
static const char SWIFT_SHIMS_NAME[] = "SwiftShims";
3234
/// The prefix of module names used by LLDB to capture Swift expressions

lib/Frontend/Frontend.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,19 @@ void CompilerInstance::performSema() {
273273
return;
274274
}
275275

276+
const auto &silOptions = Invocation.getSILOptions();
277+
if ((silOptions.Optimization <= SILOptions::SILOptMode::None &&
278+
(options.RequestedAction == FrontendOptions::EmitObject ||
279+
options.RequestedAction == FrontendOptions::Immediate)) ||
280+
(silOptions.Optimization == SILOptions::SILOptMode::None &&
281+
options.RequestedAction >= FrontendOptions::EmitSILGen)) {
282+
// Implicitly import the SwiftOnoneSupport module in non-optimized
283+
// builds. This allows for use of popular specialized functions
284+
// from the standard library, which makes the non-optimized builds
285+
// execute much faster.
286+
Invocation.getFrontendOptions()
287+
.ImplicitImportModuleNames.push_back(SWIFT_ONONE_SUPPORT);
288+
}
276289
break;
277290
}
278291
}

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static bool cacheSpecialization(SILModule &M, SILFunction *F) {
304304

305305
if (M.getOptions().Optimization >= SILOptions::SILOptMode::Optimize &&
306306
F->getLinkage() != SILLinkage::Public &&
307-
F->getModule().getSwiftModule()->getName().str() == STDLIB_NAME) {
307+
F->getModule().getSwiftModule()->getName().str() == SWIFT_ONONE_SUPPORT) {
308308
if (F->getLinkage() != SILLinkage::Public &&
309309
isWhitelistedSpecialization(F->getName())) {
310310

@@ -368,7 +368,8 @@ SILFunction *swift::getExistingSpecialization(SILModule &M,
368368
assert((Specialization->isExternalDeclaration() ||
369369
convertExternalDefinitionIntoDeclaration(Specialization)) &&
370370
"Could not remove body of the found specialization");
371-
if (!convertExternalDefinitionIntoDeclaration(Specialization)) {
371+
if (!Specialization->isExternalDeclaration() &&
372+
!convertExternalDefinitionIntoDeclaration(Specialization)) {
372373
DEBUG(
373374
llvm::dbgs() << "Could not remove body of specialization: "
374375
<< FunctionName << '\n');

lib/Serialization/SerializeSIL.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define DEBUG_TYPE "sil-serialize"
1414
#include "SILFormat.h"
1515
#include "Serialization.h"
16+
#include "swift/Strings.h"
1617
#include "swift/AST/Module.h"
1718
#include "swift/SIL/SILArgument.h"
1819
#include "swift/SIL/SILModule.h"
@@ -1682,8 +1683,16 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
16821683
// Go through all the SILFunctions in SILMod and write out any
16831684
// mandatory function bodies.
16841685
for (const SILFunction &F : *SILMod) {
1685-
if (shouldEmitFunctionBody(F) || ShouldSerializeAll)
1686+
if (shouldEmitFunctionBody(F) || ShouldSerializeAll) {
1687+
if (F.getModule().getSwiftModule()->getNameStr() == SWIFT_ONONE_SUPPORT) {
1688+
// Serialize only the declarations so that it is possible to check the
1689+
// existance of this function later.
1690+
// FIXME: SILLinker::lookupFunction does not work with functions having
1691+
// empty bodies, otherwise we could have serialized only declarations.
1692+
//writeSILFunction(F, true);
1693+
}
16861694
writeSILFunction(F);
1695+
}
16871696
}
16881697

16891698
if (ShouldSerializeAll)

stdlib/public/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if(SWIFT_BUILD_STDLIB)
2626
add_subdirectory(runtime)
2727
add_subdirectory(stubs)
2828
add_subdirectory(core)
29+
add_subdirectory(SwiftOnoneSupport)
2930
endif()
3031

3132
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_swift_library(swiftSwiftOnoneSupport SHARED IS_STDLIB
2+
# This file should be listed the first. Module name is inferred from the
3+
# filename.
4+
SwiftOnoneSupport.swift
5+
SWIFT_COMPILE_FLAGS ${STDLIB_SIL_SERIALIZE_ALL}
6+
INSTALL_IN_COMPONENT stdlib)

stdlib/public/core/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ set(SWIFTLIB_SOURCES
126126
Tuple.swift.gyb
127127
VarArgs.swift
128128
Zip.swift
129-
Prespecialized.swift
130129
)
131130

132131
set(swift_core_link_flags)

test/Driver/emit-sib-single-file.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// RUN: %target-build-swift -emit-sib %s -o %t.sib
1+
// RUN: %target-build-swift -Onone -emit-sib %s -o %t.sib
22
// RUN: %target-build-swift %t.sib -o %t
33
// RUN: %target-run %t | FileCheck %s
44

5-
// RUN: %target-build-swift -c %t.sib -o %t.o
5+
// RUN: %target-build-swift -Onone -c %t.sib -o %t.o
66
// RUN: %target-build-swift %t.o -o %t
77
// RUN: %target-run %t | FileCheck %s
88

9-
// RUN: %target-build-swift -emit-sibgen %s -o %t.sib
9+
// RUN: %target-build-swift -Onone -emit-sibgen %s -o %t.sib
1010
// RUN: %target-build-swift %t.sib -o %t
1111
// RUN: %target-run %t | FileCheck %s
1212

13-
// RUN: %target-build-swift -c %t.sib -o %t.o
13+
// RUN: %target-build-swift -Onone -c %t.sib -o %t.o
1414
// RUN: %target-build-swift %t.o -o %t
1515
// RUN: %target-run %t | FileCheck %s
1616
// REQUIRES: executable_test

0 commit comments

Comments
 (0)