Skip to content

Move pre-specializations of popular types away from the stdlib #1429

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, 2016
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
22 changes: 22 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,28 @@ function(add_swift_library name)
Core)
endif()

is_build_type_optimized("${SWIFT_STDLIB_BUILD_TYPE}" optimized)
if(NOT optimized)
# All Swift code depends on the SwiftOnoneSupport in non-optmized mode,
# except for the standard library itself.
if(SWIFTLIB_TARGET_LIBRARY AND NOT SWIFTLIB_IS_STDLIB_CORE)
list(APPEND SWIFTLIB_SWIFT_MODULE_DEPENDS SwiftOnoneSupport)
endif()
endif()

if((NOT "${SWIFT_BUILD_STDLIB}") AND
(NOT "${SWIFTLIB_SWIFT_MODULE_DEPENDS}" STREQUAL ""))
list(REMOVE_ITEM SWIFTLIB_SWIFT_MODULE_DEPENDS
SwiftOnoneSupport)
endif()

# swiftSwiftOnoneSupport does not depend on itself,
# obviously.
if("${name}" STREQUAL "swiftSwiftOnoneSupport")
list(REMOVE_ITEM SWIFTLIB_SWIFT_MODULE_DEPENDS
SwiftOnoneSupport)
endif()

translate_flags(SWIFTLIB "${SWIFTLIB_options}")

if("${SWIFTLIB_INSTALL_IN_COMPONENT}" STREQUAL "")
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace swift {
static const char LLVM_IR_EXTENSION[] = "ll";
/// The name of the standard library, which is a reserved module name.
static const char STDLIB_NAME[] = "Swift";
/// The name of the Onone support library, which is a reserved module name.
static const char SWIFT_ONONE_SUPPORT[] = "SwiftOnoneSupport";
/// The name of the SwiftShims module, which contains private stdlib decls.
static const char SWIFT_SHIMS_NAME[] = "SwiftShims";
/// The prefix of module names used by LLDB to capture Swift expressions
Expand Down
14 changes: 14 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ void CompilerInstance::performSema() {
return;
}

const auto &silOptions = Invocation.getSILOptions();
if ((silOptions.Optimization <= SILOptions::SILOptMode::None &&
(options.RequestedAction == FrontendOptions::EmitObject ||
options.RequestedAction == FrontendOptions::Immediate ||
options.RequestedAction == FrontendOptions::EmitSIL)) ||
(silOptions.Optimization == SILOptions::SILOptMode::None &&
options.RequestedAction >= FrontendOptions::EmitSILGen)) {
// Implicitly import the SwiftOnoneSupport module in non-optimized
// builds. This allows for use of popular specialized functions
// from the standard library, which makes the non-optimized builds
// execute much faster.
Invocation.getFrontendOptions()
.ImplicitImportModuleNames.push_back(SWIFT_ONONE_SUPPORT);
}
break;
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/SILOptimizer/Utils/Generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static bool cacheSpecialization(SILModule &M, SILFunction *F) {

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

Expand Down Expand Up @@ -209,7 +209,8 @@ SILFunction *swift::getExistingSpecialization(SILModule &M,
assert((Specialization->isExternalDeclaration() ||
convertExternalDefinitionIntoDeclaration(Specialization)) &&
"Could not remove body of the found specialization");
if (!convertExternalDefinitionIntoDeclaration(Specialization)) {
if (!Specialization->isExternalDeclaration() &&
!convertExternalDefinitionIntoDeclaration(Specialization)) {
DEBUG(
llvm::dbgs() << "Could not remove body of specialization: "
<< FunctionName << '\n');
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if(SWIFT_BUILD_STDLIB)
add_subdirectory(runtime)
add_subdirectory(stubs)
add_subdirectory(core)
add_subdirectory(SwiftOnoneSupport)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
Expand Down
6 changes: 6 additions & 0 deletions stdlib/public/SwiftOnoneSupport/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_swift_library(swiftSwiftOnoneSupport SHARED IS_STDLIB
# This file should be listed the first. Module name is inferred from the
# filename.
SwiftOnoneSupport.swift
SWIFT_COMPILE_FLAGS ${STDLIB_SIL_SERIALIZE_ALL} "-parse-stdlib"
INSTALL_IN_COMPONENT stdlib)
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
//===----------------------------------------------------------------------===//
// Pre-specialization of some popular generic classes and functions.
//===----------------------------------------------------------------------===//
import Swift

struct _Prespecialize {
class C {}

// Create specializations for the arrays of most
// popular builtin integer and floating point types.
static internal func _specializeArrays() {
Expand All @@ -25,8 +28,9 @@ struct _Prespecialize {
let _ = a[0]

// Set array elements
for j in 0..<a.count {
for j in 1..<a.count {
a[0] = a[j]
a[j-1] = a[j]
}

for i1 in 0..<a.count {
Expand Down Expand Up @@ -56,12 +60,61 @@ struct _Prespecialize {
let _ = a.sort { (a:Element, b:Element) in a < b }
a.sortInPlace { (a:Element, b:Element) in a < b }

// force specialization of append.
a.append(a[0])

// force specialization of print<Element>
print(sampleValue)
print("Element:\(sampleValue)")
}

func _createArrayUserWithoutSorting<Element>(sampleValue: Element) {
// Initializers.
let _: [Element] = [ sampleValue ]
var a = [Element](count: 1, repeatedValue: sampleValue)

// Read array element
let _ = a[0]

// Set array elements
for j in 0..<a.count {
a[0] = a[j]
}

for i1 in 0..<a.count {
for i2 in 0..<a.count {
a[i1] = a[i2]
}
}

a[0] = sampleValue

// Get length and capacity
let _ = a.count + a.capacity

// Iterate over array
for e in a {
print(e)
print("Value: \(e)")
}

print(a)

// Reserve capacity
a.removeAll()
a.reserveCapacity(100)


// force specialization of append.
a.append(a[0])

// force specialization of print<Element>
print(sampleValue)
print("Element:\(sampleValue)")
}

// Force pre-specialization of arrays with elements of different
// integer types.
_createArrayUser(1 as Int)
_createArrayUser(1 as Int8)
_createArrayUser(1 as Int16)
Expand All @@ -72,9 +125,23 @@ struct _Prespecialize {
_createArrayUser(1 as UInt16)
_createArrayUser(1 as UInt32)
_createArrayUser(1 as UInt64)
_createArrayUser("a" as String)

// Force pre-specialization of arrays with elements of different
// floating point types.
_createArrayUser(1.5 as Float)
_createArrayUser(1.5 as Double)

// Force pre-specialization of string arrays
_createArrayUser("a" as String)

// Force pre-specialization of arrays with elements of different
// character and unicode scalar types.
_createArrayUser("a" as Character)
_createArrayUser("a" as UnicodeScalar)
_createArrayUserWithoutSorting("a".utf8)
_createArrayUserWithoutSorting("a".utf16)
_createArrayUserWithoutSorting("a".unicodeScalars)
_createArrayUserWithoutSorting("a".characters)
}

// Force pre-specialization of Range<Int>
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ set(SWIFTLIB_SOURCES
Tuple.swift.gyb
VarArgs.swift
Zip.swift
Prespecialized.swift
)
set(GROUP_INFO_JSON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/GroupInfo.json)
set(swift_core_link_flags)
Expand Down
8 changes: 4 additions & 4 deletions test/Driver/emit-sib-single-file.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// RUN: %target-build-swift -emit-sib %s -o %t.sib
// RUN: %target-build-swift -Onone -emit-sib %s -o %t.sib
// RUN: %target-build-swift %t.sib -o %t
// RUN: %target-run %t | FileCheck %s

// RUN: %target-build-swift -c %t.sib -o %t.o
// RUN: %target-build-swift -Onone -c %t.sib -o %t.o
// RUN: %target-build-swift %t.o -o %t
// RUN: %target-run %t | FileCheck %s

// RUN: %target-build-swift -emit-sibgen %s -o %t.sib
// RUN: %target-build-swift -Onone -emit-sibgen %s -o %t.sib
// RUN: %target-build-swift %t.sib -o %t
// RUN: %target-run %t | FileCheck %s

// RUN: %target-build-swift -c %t.sib -o %t.o
// RUN: %target-build-swift -Onone -c %t.sib -o %t.o
// RUN: %target-build-swift %t.o -o %t
// RUN: %target-run %t | FileCheck %s
// REQUIRES: executable_test
Expand Down