Skip to content

[cmake] Add support for building the swift stdlib with LTO. #36570

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
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
4 changes: 4 additions & 0 deletions stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ option(SWIFT_BUILD_TEST_SUPPORT_MODULES
"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."
"${SWIFT_BUILD_TEST_SUPPORT_MODULES_default}")

set(SWIFT_STDLIB_ENABLE_LTO OFF CACHE STRING "Build Swift stdlib with LTO. One
must specify the form of LTO by setting this to one of: 'full', 'thin'. This
option only affects the standard library and runtime, not tools.")

#
# End of user-configurable options.
#
Expand Down
13 changes: 8 additions & 5 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,8 @@ function(_add_swift_target_library_single target name)
DARWIN_INSTALL_NAME_DIR
SDK
DEPLOYMENT_VERSION_MACCATALYST
MACCATALYST_BUILD_FLAVOR)
MACCATALYST_BUILD_FLAVOR
ENABLE_LTO)
set(SWIFTLIB_SINGLE_multiple_parameter_options
C_COMPILE_FLAGS
DEPENDS
Expand Down Expand Up @@ -778,6 +779,7 @@ function(_add_swift_target_library_single target name)
${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword}
${embed_bitcode_arg}
${SWIFTLIB_SINGLE_STATIC_keyword}
ENABLE_LTO "${SWIFTLIB_SINGLE_ENABLE_LTO}"
INSTALL_IN_COMPONENT "${SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT}"
MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}")
add_swift_source_group("${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}")
Expand Down Expand Up @@ -1139,7 +1141,7 @@ function(_add_swift_target_library_single target name)
endif()

if (NOT SWIFTLIB_SINGLE_TARGET_LIBRARY)
set(lto_type "${SWIFT_TOOLS_ENABLE_LTO}")
set(lto_type "${SWIFT_STDLIB_ENABLE_LTO}")
endif()

_add_target_variant_c_compile_flags(
Expand Down Expand Up @@ -1988,7 +1990,7 @@ function(add_swift_target_library name)
DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}"
DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}"
MACCATALYST_BUILD_FLAVOR "${maccatalyst_build_flavor}"

ENABLE_LTO "${SWIFT_STDLIB_ENABLE_LTO}"
GYB_SOURCES ${SWIFTLIB_GYB_SOURCES}
)
if(NOT SWIFT_BUILT_STANDALONE AND NOT "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
Expand Down Expand Up @@ -2322,15 +2324,15 @@ function(_add_swift_target_executable_single name)
ARCH "${SWIFTEXE_SINGLE_ARCHITECTURE}"
BUILD_TYPE "${CMAKE_BUILD_TYPE}"
ENABLE_ASSERTIONS "${LLVM_ENABLE_ASSERTIONS}"
ENABLE_LTO "${SWIFT_TOOLS_ENABLE_LTO}"
ENABLE_LTO "${SWIFT_STDLIB_ENABLE_LTO}"
ANALYZE_CODE_COVERAGE "${SWIFT_ANALYZE_CODE_COVERAGE}"
RESULT_VAR_NAME c_compile_flags)
_add_target_variant_link_flags(
SDK "${SWIFTEXE_SINGLE_SDK}"
ARCH "${SWIFTEXE_SINGLE_ARCHITECTURE}"
BUILD_TYPE "${CMAKE_BUILD_TYPE}"
ENABLE_ASSERTIONS "${LLVM_ENABLE_ASSERTIONS}"
ENABLE_LTO "${SWIFT_TOOLS_ENABLE_LTO}"
ENABLE_LTO "${SWIFT_STDLIB_ENABLE_LTO}"
LTO_OBJECT_NAME "${name}-${SWIFTEXE_SINGLE_SDK}-${SWIFTEXE_SINGLE_ARCHITECTURE}"
ANALYZE_CODE_COVERAGE "${SWIFT_ANALYZE_CODE_COVERAGE}"
RESULT_VAR_NAME link_flags
Expand All @@ -2350,6 +2352,7 @@ function(_add_swift_target_executable_single name)
SDK ${SWIFTEXE_SINGLE_SDK}
ARCHITECTURE ${SWIFTEXE_SINGLE_ARCHITECTURE}
COMPILE_FLAGS ${SWIFTEXE_SINGLE_COMPILE_FLAGS}
ENABLE_LTO "${SWIFT_STDLIB_ENABLE_LTO}"
IS_MAIN)
add_swift_source_group("${SWIFTEXE_SINGLE_EXTERNAL_SOURCES}")

Expand Down
18 changes: 16 additions & 2 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
include(macCatalystUtils)
include(SwiftUtils)

function(_compute_lto_swift_flag option out_var)
string(TOLOWER "${option}" lowercase_option)
if (lowercase_option STREQUAL "full")
set(${out_var} "-lto=llvm-full" PARENT_SCOPE)
elseif (lowercase_option STREQUAL "thin")
set(${out_var} "-lto=llvm-thin" PARENT_SCOPE)
endif()
endfunction()

# Compute the library subdirectory to use for the given sdk and
# architecture, placing the result in 'result_var_name'.
function(compute_library_subdir result_var_name sdk arch)
Expand Down Expand Up @@ -42,7 +51,7 @@ function(handle_swift_sources
cmake_parse_arguments(SWIFTSOURCES
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
"SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR"
"DEPENDS;COMPILE_FLAGS;MODULE_NAME"
"DEPENDS;COMPILE_FLAGS;MODULE_NAME;ENABLE_LTO"
${ARGN})
translate_flag(${SWIFTSOURCES_IS_MAIN} "IS_MAIN" IS_MAIN_arg)
translate_flag(${SWIFTSOURCES_IS_STDLIB} "IS_STDLIB" IS_STDLIB_arg)
Expand Down Expand Up @@ -107,7 +116,12 @@ function(handle_swift_sources
if(sdk IN_LIST SWIFT_APPLE_PLATFORMS OR sdk STREQUAL "MACCATALYST")
list(APPEND swift_compile_flags "-save-optimization-record=bitstream")
endif()

if (SWIFTSOURCES_ENABLE_LTO)
_compute_lto_swift_flag("${SWIFTSOURCES_ENABLE_LTO}" _lto_flag_out)
if (_lto_flag_out)
list(APPEND swift_compile_flags "${_lto_flag_out}")
endif()
endif()
_compile_swift_files(
dependency_target
module_dependency_target
Expand Down