Skip to content

Commit f417630

Browse files
authored
Add option to reduce build times when using ThinLTO and ld64 (#41058)
Internal configurations targeting Darwin employ ThinLTO to improve compiler performance, however using it on all executable causes build time to increase with no matching benefit. To reduce build times in such configurations, we allow some ancillary targets to opt out of LLVM IR optimizations when linking ThinLTO with ld64 (e.g. tools used for bootstrapping or debugging the Swift compiler) -- this behaviour is opt in through a new flag `--swift-tools-ld64-lto-codegen-only-for-supporting-targets`. Addresses rdar://76702687
1 parent 2fa7968 commit f417630

File tree

11 files changed

+62
-2
lines changed

11 files changed

+62
-2
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ set(SWIFT_TOOLS_ENABLE_LTO OFF CACHE STRING "Build Swift tools with LTO. One
192192
option only affects the tools that run on the host (the compiler), and has
193193
no effect on the target libraries (the standard library and the runtime).")
194194

195+
option(SWIFT_TOOLS_LD64_LTO_CODEGEN_ONLY_FOR_SUPPORTING_TARGETS
196+
"When building ThinLTO using ld64 on Darwin, controls whether to opt out of
197+
LLVM IR optimizations when linking targets that will get
198+
little benefit from it (e.g. tools for bootstrapping or
199+
debugging Swift)"
200+
FALSE)
201+
195202
option(BOOTSTRAPPING_MODE [=[
196203
How to build the swift compiler modules. Possible values are
197204
OFF: build without swift modules

cmake/modules/AddSwift.cmake

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ macro(add_swift_lib_subdirectory name)
636636
endmacro()
637637
638638
function(add_swift_host_tool executable)
639-
set(options HAS_SWIFT_MODULES)
639+
set(options HAS_SWIFT_MODULES THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
640640
set(single_parameter_options SWIFT_COMPONENT BOOTSTRAPPING)
641641
set(multiple_parameter_options LLVM_LINK_COMPONENTS)
642642
@@ -859,6 +859,19 @@ function(add_swift_host_tool executable)
859859
endif()
860860
endif()
861861
862+
if(ASHT_THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
863+
string(CONCAT lto_codegen_only_link_options
864+
"$<"
865+
"$<AND:"
866+
"$<BOOL:${LLVM_LINKER_IS_LD64}>,"
867+
"$<BOOL:${SWIFT_TOOLS_LD64_LTO_CODEGEN_ONLY_FOR_SUPPORTING_TARGETS}>,"
868+
"$<STREQUAL:${SWIFT_TOOLS_ENABLE_LTO},thin>"
869+
">:"
870+
"LINKER:-flto-codegen-only"
871+
">")
872+
target_link_options(${executable} PRIVATE "${lto_codegen_only_link_options}")
873+
endif()
874+
862875
if(NOT ${ASHT_SWIFT_COMPONENT} STREQUAL "no_component")
863876
add_dependencies(${ASHT_SWIFT_COMPONENT} ${executable})
864877
swift_install_in_component(TARGETS ${executable}

tools/driver/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*")
1010
SWIFT_COMPONENT no_component
1111
HAS_SWIFT_MODULES
1212
BOOTSTRAPPING 0
13+
THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY
1314
)
1415
target_link_libraries(swift-frontend-bootstrapping0
1516
PRIVATE
@@ -28,6 +29,7 @@ if(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*")
2829
SWIFT_COMPONENT no_component
2930
HAS_SWIFT_MODULES
3031
BOOTSTRAPPING 1
32+
THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY
3133
)
3234
target_link_libraries(swift-frontend-bootstrapping1
3335
PRIVATE

tools/swift-ast-script/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_swift_host_tool(swift-ast-script
44
ASTScriptEvaluator.cpp
55
swift-ast-script.cpp
66
SWIFT_COMPONENT tools
7+
THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY
78
)
89
target_link_libraries(swift-ast-script
910
PRIVATE

tools/swift-dependency-tool/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_swift_host_tool(swift-dependency-tool
22
swift-dependency-tool.cpp
33
SWIFT_COMPONENT tools
4+
THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY
45
)
56
target_link_libraries(swift-dependency-tool
67
PRIVATE

tools/swift-ide-test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_swift_host_tool(swift-ide-test
33
ModuleAPIDiff.cpp
44
XMLValidator.cpp
55
SWIFT_COMPONENT tools
6+
THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY
67
)
78
target_link_libraries(swift-ide-test
89
PRIVATE

tools/swift-remoteast-test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_swift_host_tool(swift-remoteast-test
22
swift-remoteast-test.cpp
33
SWIFT_COMPONENT testsuite-tools
4+
THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY
45
)
56
target_link_libraries(swift-remoteast-test
67
PRIVATE

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,14 @@ def create_argument_parser():
515515
help='the maximum number of parallel link jobs to use when '
516516
'compiling swift tools.')
517517

518+
option('--swift-tools-ld64-lto-codegen-only-for-supporting-targets',
519+
toggle_true,
520+
default=False,
521+
help='When building ThinLTO using ld64 on Darwin, controls whether '
522+
'to opt out of LLVM IR optimizations when linking targets that '
523+
'will get little benefit from it (e.g. tools for '
524+
'bootstrapping or debugging Swift)')
525+
518526
option('--dsymutil-jobs', store_int,
519527
default=defaults.DSYMUTIL_JOBS,
520528
metavar='COUNT',

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
'swift_freestanding_is_darwin': False,
221221
'swift_stdlib_assertions': True,
222222
'swift_stdlib_build_variant': 'Debug',
223+
'swift_tools_ld64_lto_codegen_only_for_supporting_targets': False,
223224
'swift_tools_max_parallel_lto_link_jobs':
224225
defaults.SWIFT_MAX_PARALLEL_LTO_LINK_JOBS,
225226
'swift_user_visible_version': defaults.SWIFT_USER_VISIBLE_VERSION,
@@ -714,6 +715,7 @@ class BuildScriptImplOption(_BaseOption):
714715
IntOption('--jobs', dest='build_jobs'),
715716
IntOption('--llvm-max-parallel-lto-link-jobs'),
716717
IntOption('--swift-tools-max-parallel-lto-link-jobs'),
718+
EnableOption('--swift-tools-ld64-lto-codegen-only-for-supporting-targets'),
717719
IntOption('-j', dest='build_jobs'),
718720
IntOption('--dsymutil-jobs', dest='dsymutil_jobs'),
719721

utils/swift_build_support/swift_build_support/products/swift.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def __init__(self, args, toolchain, source_dir, build_dir):
6565

6666
self.cmake_options.extend(self._enable_stdlib_unicode_data)
6767

68+
self.cmake_options.extend(
69+
self._swift_tools_ld64_lto_codegen_only_for_supporting_targets)
70+
6871
@classmethod
6972
def is_build_script_impl_product(cls):
7073
"""is_build_script_impl_product -> bool
@@ -184,6 +187,11 @@ def _build_swift_private_stdlib(self):
184187
return [('SWIFT_STDLIB_BUILD_PRIVATE:BOOL',
185188
self.args.build_swift_private_stdlib)]
186189

190+
@property
191+
def _swift_tools_ld64_lto_codegen_only_for_supporting_targets(self):
192+
return [('SWIFT_TOOLS_LD64_LTO_CODEGEN_ONLY_FOR_SUPPORTING_TARGETS:BOOL',
193+
self.args.swift_tools_ld64_lto_codegen_only_for_supporting_targets)]
194+
187195
@classmethod
188196
def get_dependencies(cls):
189197
return [cmark.CMark,

0 commit comments

Comments
 (0)