Skip to content

Commit 24f6c79

Browse files
committed
[cmake] Add support for building the swift stdlib with LTO.
This is controlled with the option SWIFT_STDLIB_ENABLE_LTO=thin|full.
1 parent b6a614c commit 24f6c79

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

stdlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ option(SWIFT_BUILD_TEST_SUPPORT_MODULES
7979
"Whether to build StdlibUnittest and other test support modules. Defaults to On when SWIFT_BUILD_SDK_OVERLAY is On, or when SWIFT_INCLUDE_TESTS is On."
8080
"${SWIFT_BUILD_TEST_SUPPORT_MODULES_default}")
8181

82+
set(SWIFT_STDLIB_ENABLE_LTO OFF CACHE STRING "Build Swift stdlib with LTO. One
83+
must specify the form of LTO by setting this to one of: 'full', 'thin'. This
84+
option only affects the standard library and runtime, not tools.")
85+
8286
#
8387
# End of user-configurable options.
8488
#

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,8 @@ function(_add_swift_target_library_single target name)
623623
DARWIN_INSTALL_NAME_DIR
624624
SDK
625625
DEPLOYMENT_VERSION_MACCATALYST
626-
MACCATALYST_BUILD_FLAVOR)
626+
MACCATALYST_BUILD_FLAVOR
627+
ENABLE_LTO)
627628
set(SWIFTLIB_SINGLE_multiple_parameter_options
628629
C_COMPILE_FLAGS
629630
DEPENDS
@@ -778,6 +779,7 @@ function(_add_swift_target_library_single target name)
778779
${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword}
779780
${embed_bitcode_arg}
780781
${SWIFTLIB_SINGLE_STATIC_keyword}
782+
ENABLE_LTO "${SWIFTLIB_SINGLE_ENABLE_LTO}"
781783
INSTALL_IN_COMPONENT "${SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT}"
782784
MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}")
783785
add_swift_source_group("${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}")
@@ -1139,7 +1141,7 @@ function(_add_swift_target_library_single target name)
11391141
endif()
11401142

11411143
if (NOT SWIFTLIB_SINGLE_TARGET_LIBRARY)
1142-
set(lto_type "${SWIFT_TOOLS_ENABLE_LTO}")
1144+
set(lto_type "${SWIFT_STDLIB_ENABLE_LTO}")
11431145
endif()
11441146

11451147
_add_target_variant_c_compile_flags(
@@ -1988,7 +1990,7 @@ function(add_swift_target_library name)
19881990
DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}"
19891991
DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}"
19901992
MACCATALYST_BUILD_FLAVOR "${maccatalyst_build_flavor}"
1991-
1993+
ENABLE_LTO "${SWIFT_STDLIB_ENABLE_LTO}"
19921994
GYB_SOURCES ${SWIFTLIB_GYB_SOURCES}
19931995
)
19941996
if(NOT SWIFT_BUILT_STANDALONE AND NOT "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
@@ -2322,15 +2324,15 @@ function(_add_swift_target_executable_single name)
23222324
ARCH "${SWIFTEXE_SINGLE_ARCHITECTURE}"
23232325
BUILD_TYPE "${CMAKE_BUILD_TYPE}"
23242326
ENABLE_ASSERTIONS "${LLVM_ENABLE_ASSERTIONS}"
2325-
ENABLE_LTO "${SWIFT_TOOLS_ENABLE_LTO}"
2327+
ENABLE_LTO "${SWIFT_STDLIB_ENABLE_LTO}"
23262328
ANALYZE_CODE_COVERAGE "${SWIFT_ANALYZE_CODE_COVERAGE}"
23272329
RESULT_VAR_NAME c_compile_flags)
23282330
_add_target_variant_link_flags(
23292331
SDK "${SWIFTEXE_SINGLE_SDK}"
23302332
ARCH "${SWIFTEXE_SINGLE_ARCHITECTURE}"
23312333
BUILD_TYPE "${CMAKE_BUILD_TYPE}"
23322334
ENABLE_ASSERTIONS "${LLVM_ENABLE_ASSERTIONS}"
2333-
ENABLE_LTO "${SWIFT_TOOLS_ENABLE_LTO}"
2335+
ENABLE_LTO "${SWIFT_STDLIB_ENABLE_LTO}"
23342336
LTO_OBJECT_NAME "${name}-${SWIFTEXE_SINGLE_SDK}-${SWIFTEXE_SINGLE_ARCHITECTURE}"
23352337
ANALYZE_CODE_COVERAGE "${SWIFT_ANALYZE_CODE_COVERAGE}"
23362338
RESULT_VAR_NAME link_flags
@@ -2350,6 +2352,7 @@ function(_add_swift_target_executable_single name)
23502352
SDK ${SWIFTEXE_SINGLE_SDK}
23512353
ARCHITECTURE ${SWIFTEXE_SINGLE_ARCHITECTURE}
23522354
COMPILE_FLAGS ${SWIFTEXE_SINGLE_COMPILE_FLAGS}
2355+
ENABLE_LTO "${SWIFT_STDLIB_ENABLE_LTO}"
23532356
IS_MAIN)
23542357
add_swift_source_group("${SWIFTEXE_SINGLE_EXTERNAL_SOURCES}")
23552358

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
include(macCatalystUtils)
22
include(SwiftUtils)
33

4+
function(_compute_lto_swift_flag option out_var)
5+
string(TOLOWER "${option}" lowercase_option)
6+
if (lowercase_option STREQUAL "full")
7+
set(${out_var} "-lto=llvm-full" PARENT_SCOPE)
8+
elseif (lowercase_option STREQUAL "thin")
9+
set(${out_var} "-lto=llvm-thin" PARENT_SCOPE)
10+
endif()
11+
endfunction()
12+
413
# Compute the library subdirectory to use for the given sdk and
514
# architecture, placing the result in 'result_var_name'.
615
function(compute_library_subdir result_var_name sdk arch)
@@ -42,7 +51,7 @@ function(handle_swift_sources
4251
cmake_parse_arguments(SWIFTSOURCES
4352
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
4453
"SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR"
45-
"DEPENDS;COMPILE_FLAGS;MODULE_NAME"
54+
"DEPENDS;COMPILE_FLAGS;MODULE_NAME;ENABLE_LTO"
4655
${ARGN})
4756
translate_flag(${SWIFTSOURCES_IS_MAIN} "IS_MAIN" IS_MAIN_arg)
4857
translate_flag(${SWIFTSOURCES_IS_STDLIB} "IS_STDLIB" IS_STDLIB_arg)
@@ -107,7 +116,12 @@ function(handle_swift_sources
107116
if(sdk IN_LIST SWIFT_APPLE_PLATFORMS OR sdk STREQUAL "MACCATALYST")
108117
list(APPEND swift_compile_flags "-save-optimization-record=bitstream")
109118
endif()
110-
119+
if (SWIFTSOURCES_ENABLE_LTO)
120+
_compute_lto_swift_flag("${SWIFTSOURCES_ENABLE_LTO}" _lto_flag_out)
121+
if (_lto_flag_out)
122+
list(APPEND swift_compile_flags "${_lto_flag_out}")
123+
endif()
124+
endif()
111125
_compile_swift_files(
112126
dependency_target
113127
module_dependency_target

0 commit comments

Comments
 (0)