Skip to content

Commit 11c8a7c

Browse files
committed
[cmake] Add support for specifying the number of parallel link jobs for swift.
I am going to use the link job for limiting swift lto compile time. The reason not to use the same variables as LLVM (i.e. LLVM_PARALLEL_LINK_JOBS) is that Swift since it is compiling more IR may have a larger memory representation implying less parallel linking jobs than LLVM can be used on bots.
1 parent 3b423e9 commit 11c8a7c

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ set(SWIFT_NATIVE_SWIFT_TOOLS_PATH "" CACHE STRING
125125
option(SWIFT_ENABLE_LTO
126126
"If set to true, build the swift compiler with link time optimization enabled" FALSE)
127127

128+
# The following only works with the Ninja generator in CMake >= 3.0.
129+
set(SWIFT_PARALLEL_LINK_JOBS "" CACHE STRING
130+
"Define the maximum number of linker jobs for swift.")
131+
128132
#
129133
# User-configurable Darwin-specific options.
130134
#
@@ -623,6 +627,15 @@ foreach(sdk ${SWIFT_SDKS})
623627
unset(UNIVERSAL_LIBRARY_NAMES_${SWIFT_SDK_${sdk}_LIB_SUBDIR} CACHE)
624628
endforeach()
625629

630+
if(SWIFT_PARALLEL_LINK_JOBS)
631+
if(CMAKE_VERSION VERSION_LESS 3.0 OR NOT CMAKE_MAKE_PROGRAM MATCHES "ninja")
632+
message(WARNING "Job pooling is only available with Ninja generators and CMake 3.0 and later.")
633+
else()
634+
set_property(GLOBAL APPEND PROPERTY JOB_POOLS swift_link_job_pool=${SWIFT_PARALLEL_LINK_JOBS})
635+
set(CMAKE_JOB_POOL_LINK swift_link_job_pool)
636+
endif()
637+
endif()
638+
626639
message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}")
627640
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
628641
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")

cmake/modules/AddSwift.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,9 @@ function(_add_swift_executable_single name)
18141814
COMPILE_FLAGS " ${c_compile_flags}")
18151815
set_property(TARGET ${name} APPEND_STRING PROPERTY
18161816
LINK_FLAGS " ${link_flags}")
1817-
1817+
if (SWIFT_PARALLEL_LINK_JOBS)
1818+
set_property(TARGET ${name} PROPERTY JOB_POOL_LINK swift_link_job_pool)
1819+
endif()
18181820
set_output_directory(${name}
18191821
BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR}
18201822
LIBRARY_DIR ${SWIFT_LIBRARY_OUTPUT_INTDIR})

utils/build-script-impl

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ KNOWN_SETTINGS=(
203203
darwin-toolchain-application-cert "" "Application Cert name to codesign xctoolchain"
204204
darwin-toolchain-installer-cert "" "Installer Cert name to create installer pkg"
205205
darwin-toolchain-installer-package "" "The path to installer pkg"
206-
build-jobs "" "The number of parallel build jobs to use"
206+
build-jobs "" "The number of parallel build jobs to use"
207207
darwin-toolchain-alias "" "Swift alias for toolchain"
208208
export-compile-commands "" "set to generate JSON compilation databases for each build product"
209209
)
@@ -227,6 +227,37 @@ function set_lldb_build_mode() {
227227
LLDB_BUILD_MODE="CustomSwift-${LLDB_BUILD_TYPE}"
228228
}
229229

230+
function num_llvm_parallel_lto_link_jobs() {
231+
case "$(uname -s -m)" in
232+
Darwin\ x86_64)
233+
# Currently with -gline-tables-only clang is ~3.5GB on Darwin. Use
234+
# the formula GB Memory/3.5GB to get the number of parallel link
235+
# threads we can support.
236+
echo $(sysctl hw.memsize | cut -f 2 -d " ")/1000000000.0/3.5 | bc
237+
;;
238+
*)
239+
echo "Unknown operating system"
240+
exit 1
241+
;;
242+
esac
243+
}
244+
245+
function num_swift_parallel_lto_link_jobs() {
246+
case "$(uname -s -m)" in
247+
Darwin\ x86_64)
248+
# Currently with -gline-tables-only swift is ~5-6GB on Darwin. Use
249+
# the formula GB Memory/6GB to get the number of parallel link
250+
# threads we can support.
251+
echo $(sysctl hw.memsize | cut -f 2 -d " ")/1000000000/6.0 | bc
252+
;;
253+
*)
254+
echo "Unknown operating system"
255+
exit 1
256+
;;
257+
esac
258+
}
259+
260+
230261
function set_deployment_target_based_options() {
231262
llvm_cmake_options=()
232263
swift_cmake_options=()
@@ -438,7 +469,7 @@ function set_deployment_target_based_options() {
438469
"-DCMAKE_CXX_FLAGS=-O2 -flto -gline-tables-only -fno-stack-protector "
439470
"-DCMAKE_C_FLAGS_RELWITHDEBINFO=-O2 -flto -gline-tables-only -fno-stack-protector "
440471
"-DCMAKE_CXX_FLAGS_RELWITHDEBINFO=-O2 -flto -gline-tables-only -fno-stack-protector "
441-
"-DLLVM_PARALLEL_LINK_JOBS=5"
472+
"-DLLVM_PARALLEL_LINK_JOBS=$(num_llvm_parallel_lto_link_jobs)"
442473
)
443474
fi
444475

@@ -448,6 +479,7 @@ function set_deployment_target_based_options() {
448479
"${swift_cmake_options[@]}"
449480
"-DCMAKE_C_STANDARD_COMPUTED_DEFAULT=AppleClang"
450481
"-DCMAKE_CXX_STANDARD_COMPUTED_DEFAULT=AppleClang"
482+
"-DSWIFT_PARALLEL_LINK_JOBS=$(num_swift_parallel_lto_link_jobs)"
451483
)
452484
fi
453485
fi

0 commit comments

Comments
 (0)