Skip to content

Commit 7b9fa21

Browse files
authored
[cmake] Option to create Ninja job pools depending on available resources (#65274)
This PR adds options to let CMake calculate the ninja job pools depending on free memory and available cores. You can provide memory requirements for each compile and link job which is checked against CMake AVAILABLE_PHYSICAL_MEMORY and NUMBER_OF_LOGICAL_CORES. [This information are available since CMake 3.0](https://cmake.org/cmake/help/v3.0/command/cmake_host_system_information.html). This is very helpful in CI environments with multiple jobs per environment or a VM with multiple users. Its different to LLVM_PARALLEL_LINK_JOBS / LLVM_PARALLEL_COMPILE_JOBS (or ninja -j 1) because it tries to use the resources more efficient without being terminated. Only downside currently is that compile and link jobs can run at the same time so there is an offset for link job memory suggested which is added to the documentation. The definitions aren't added as cache because if I understand it correctly this would break it because values could be outdated.
1 parent b081da5 commit 7b9fa21

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

llvm/cmake/modules/HandleLLVMOptions.cmake

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO)
3636
# The following only works with the Ninja generator in CMake >= 3.0.
3737
set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING
3838
"Define the maximum number of concurrent compilation jobs (Ninja only).")
39+
if(LLVM_RAM_PER_COMPILE_JOB OR LLVM_RAM_PER_LINK_JOB)
40+
cmake_host_system_information(RESULT available_physical_memory QUERY AVAILABLE_PHYSICAL_MEMORY)
41+
cmake_host_system_information(RESULT number_of_logical_cores QUERY NUMBER_OF_LOGICAL_CORES)
42+
endif()
43+
if(LLVM_RAM_PER_COMPILE_JOB)
44+
math(EXPR jobs_with_sufficient_memory "${available_physical_memory} / ${LLVM_RAM_PER_COMPILE_JOB}" OUTPUT_FORMAT DECIMAL)
45+
if (jobs_with_sufficient_memory LESS 1)
46+
set(jobs_with_sufficient_memory 1)
47+
endif()
48+
if (jobs_with_sufficient_memory LESS number_of_logical_cores)
49+
set(LLVM_PARALLEL_COMPILE_JOBS "${jobs_with_sufficient_memory}")
50+
else()
51+
set(LLVM_PARALLEL_COMPILE_JOBS "${number_of_logical_cores}")
52+
endif()
53+
endif()
3954
if(LLVM_PARALLEL_COMPILE_JOBS)
4055
if(NOT CMAKE_GENERATOR MATCHES "Ninja")
4156
message(WARNING "Job pooling is only available with Ninja generators.")
@@ -47,6 +62,17 @@ endif()
4762

4863
set(LLVM_PARALLEL_LINK_JOBS "" CACHE STRING
4964
"Define the maximum number of concurrent link jobs (Ninja only).")
65+
if(LLVM_RAM_PER_LINK_JOB)
66+
math(EXPR jobs_with_sufficient_memory "${available_physical_memory} / ${LLVM_RAM_PER_LINK_JOB}" OUTPUT_FORMAT DECIMAL)
67+
if (jobs_with_sufficient_memory LESS 1)
68+
set(jobs_with_sufficient_memory 1)
69+
endif()
70+
if (jobs_with_sufficient_memory LESS number_of_logical_cores)
71+
set(LLVM_PARALLEL_LINK_JOBS "${jobs_with_sufficient_memory}")
72+
else()
73+
set(LLVM_PARALLEL_LINK_JOBS "${number_of_logical_cores}")
74+
endif()
75+
endif()
5076
if(CMAKE_GENERATOR MATCHES "Ninja")
5177
if(NOT LLVM_PARALLEL_LINK_JOBS AND uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
5278
message(STATUS "ThinLTO provides its own parallel linking - limiting parallel link jobs to 2.")

llvm/docs/CMake.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,19 @@ enabled sub-projects. Nearly all of these variable names begin with
755755
**LLVM_PARALLEL_LINK_JOBS**:STRING
756756
Define the maximum number of concurrent link jobs.
757757

758+
**LLVM_RAM_PER_COMPILE_JOB**:STRING
759+
Calculates the amount of Ninja compile jobs according to available resources.
760+
Value has to be in MB, overwrites LLVM_PARALLEL_COMPILE_JOBS. Compile jobs
761+
will be between one and amount of logical cores.
762+
763+
**LLVM_RAM_PER_LINK_JOB**:STRING
764+
Calculates the amount of Ninja link jobs according to available resources.
765+
Value has to be in MB, overwrites LLVM_PARALLEL_LINK_JOBS. Link jobs will
766+
be between one and amount of logical cores. Link jobs will not run
767+
exclusively therefore you should add an offset of one or two compile jobs
768+
to be sure its not terminated in your memory restricted environment. On ELF
769+
platforms also consider ``LLVM_USE_SPLIT_DWARF`` in Debug build.
770+
758771
**LLVM_PROFDATA_FILE**:PATH
759772
Path to a profdata file to pass into clang's -fprofile-instr-use flag. This
760773
can only be specified if you're building with clang.

0 commit comments

Comments
 (0)