Skip to content

Commit

Permalink
[CMake] Add tablegen job pool support (#84762)
Browse files Browse the repository at this point in the history
Add the ability to set the number of tablegen jobs that can run in
parallel
similar to the LLVM_PARALLEL_[COMPILE|LINK]_JOBS options that already
exist.
  • Loading branch information
dstutt committed Mar 12, 2024
1 parent bba4a1d commit 9228859
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
24 changes: 23 additions & 1 deletion llvm/cmake/modules/HandleLLVMOptions.cmake
Expand Up @@ -36,7 +36,7 @@ string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO)
# The following only works with the Ninja generator in CMake >= 3.0.
set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING
"Define the maximum number of concurrent compilation jobs (Ninja only).")
if(LLVM_RAM_PER_COMPILE_JOB OR LLVM_RAM_PER_LINK_JOB)
if(LLVM_RAM_PER_COMPILE_JOB OR LLVM_RAM_PER_LINK_JOB OR LLVM_RAM_PER_TABLEGEN_JOB)
cmake_host_system_information(RESULT available_physical_memory QUERY AVAILABLE_PHYSICAL_MEMORY)
cmake_host_system_information(RESULT number_of_logical_cores QUERY NUMBER_OF_LOGICAL_CORES)
endif()
Expand Down Expand Up @@ -86,6 +86,28 @@ elseif(LLVM_PARALLEL_LINK_JOBS)
message(WARNING "Job pooling is only available with Ninja generators.")
endif()

set(LLVM_PARALLEL_TABLEGEN_JOBS "" CACHE STRING
"Define the maximum number of concurrent tablegen jobs (Ninja only).")
if(LLVM_RAM_PER_TABLEGEN_JOB)
math(EXPR jobs_with_sufficient_memory "${available_physical_memory} / ${LLVM_RAM_PER_TABLEGEN_JOB}" OUTPUT_FORMAT DECIMAL)
if (jobs_with_sufficient_memory LESS 1)
set(jobs_with_sufficient_memory 1)
endif()
if (jobs_with_sufficient_memory LESS number_of_logical_cores)
set(LLVM_PARALLEL_TABLEGEN_JOBS "${jobs_with_sufficient_memory}")
else()
set(LLVM_PARALLEL_TABLEGEN_JOBS "${number_of_logical_cores}")
endif()
endif()
if(LLVM_PARALLEL_TABLEGEN_JOBS)
if(NOT CMAKE_GENERATOR MATCHES "Ninja")
message(WARNING "Job pooling is only available with Ninja generators.")
else()
set_property(GLOBAL APPEND PROPERTY JOB_POOLS tablegen_job_pool=${LLVM_PARALLEL_TABLEGEN_JOBS})
# Job pool for tablegen is set on the add_custom_command
endif()
endif()

if( LLVM_ENABLE_ASSERTIONS )
# MSVC doesn't like _DEBUG on release builds. See PR 4379.
if( NOT MSVC )
Expand Down
7 changes: 7 additions & 0 deletions llvm/cmake/modules/TableGen.cmake
Expand Up @@ -125,6 +125,12 @@ function(tablegen project ofn)
set(tablegen_exe ${${project}_TABLEGEN_EXE})
set(tablegen_depends ${${project}_TABLEGEN_TARGET} ${tablegen_exe})

if(LLVM_PARALLEL_TABLEGEN_JOBS)
set(LLVM_TABLEGEN_JOB_POOL JOB_POOL tablegen_job_pool)
else()
set(LLVM_TABLEGEN_JOB_POOL "")
endif()

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
COMMAND ${tablegen_exe} ${ARG_UNPARSED_ARGUMENTS} -I ${CMAKE_CURRENT_SOURCE_DIR}
${tblgen_includes}
Expand All @@ -139,6 +145,7 @@ function(tablegen project ofn)
${local_tds} ${global_tds}
${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
${LLVM_TARGET_DEPENDS}
${LLVM_TABLEGEN_JOB_POOL}
COMMENT "Building ${ofn}..."
)

Expand Down
8 changes: 8 additions & 0 deletions llvm/docs/CMake.rst
Expand Up @@ -762,6 +762,9 @@ enabled sub-projects. Nearly all of these variable names begin with
**LLVM_PARALLEL_LINK_JOBS**:STRING
Define the maximum number of concurrent link jobs.

**LLVM_PARALLEL_TABLEGEN_JOBS**:STRING
Define the maximum number of concurrent tablegen jobs.

**LLVM_RAM_PER_COMPILE_JOB**:STRING
Calculates the amount of Ninja compile jobs according to available resources.
Value has to be in MB, overwrites LLVM_PARALLEL_COMPILE_JOBS. Compile jobs
Expand All @@ -775,6 +778,11 @@ enabled sub-projects. Nearly all of these variable names begin with
to be sure its not terminated in your memory restricted environment. On ELF
platforms also consider ``LLVM_USE_SPLIT_DWARF`` in Debug build.

**LLVM_RAM_PER_TABLEGEN_JOB**:STRING
Calculates the amount of Ninja tablegen jobs according to available resources.
Value has to be in MB, overwrites LLVM_PARALLEL_TABLEGEN_JOBS. Tablegen jobs
will be between one and amount of logical cores.

**LLVM_PROFDATA_FILE**:PATH
Path to a profdata file to pass into clang's -fprofile-instr-use flag. This
can only be specified if you're building with clang.
Expand Down
6 changes: 3 additions & 3 deletions llvm/docs/GettingStarted.rst
Expand Up @@ -90,11 +90,11 @@ Getting the Source Code and Building LLVM
is installed on your system. This can dramatically speed up link times
if the default linker is slow.

* ``-DLLVM_PARALLEL_{COMPILE,LINK}_JOBS=N`` --- Limit the number of
compile/link jobs running in parallel at the same time. This is
* ``-DLLVM_PARALLEL_{COMPILE,LINK,TABLEGEN}_JOBS=N`` --- Limit the number of
compile/link/tablegen jobs running in parallel at the same time. This is
especially important for linking since linking can use lots of memory. If
you run into memory issues building LLVM, try setting this to limit the
maximum number of compile/link jobs running at the same time.
maximum number of compile/link/tablegen jobs running at the same time.

* ``cmake --build build [--target <target>]`` or the build system specified
above directly.
Expand Down

0 comments on commit 9228859

Please sign in to comment.