[llvm] [mlir] [CMake] Add dependent project mode for Flang's MLIR (PR #221724)
Mehdi Amini via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 7 06:29:35 PDT 2026
https://github.com/joker-eph updated https://github.com/llvm/llvm-project/pull/221724
>From b6fa24f493c312370a0e6e4aecd0c1e084723a59 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Mon, 7 Sep 2026 06:11:56 -0700
Subject: [PATCH] [CMake] Add dependent project mode for Flang's MLIR
LLVM_ENABLE_PROJECTS historically treats projects enabled to satisfy another
project's dependency the same as projects selected by the user. Consequently,
enabling Flang attaches the complete MLIR project, install rules, and test
suites to LLVM's aggregate targets.
Track implicitly configured projects in LLVM_DEPENDENT_PROJECTS and add their
source directories with EXCLUDE_FROM_ALL. This keeps every MLIR target
available for normal dependency resolution while omitting the MLIR-wide build
and install targets. Flang is the first user of this mode.
Let MLIR own its dependent-mode test policy. When MLIR tests are enabled,
configure the test support libraries for downstream consumers, but do not
create MLIR unit-test targets or register the MLIR lit suite with check-all.
Explicitly enabling MLIR preserves the existing build and test behavior.
Assisted-by: Codex
---
llvm/CMakeLists.txt | 5 +++++
llvm/cmake/modules/AddLLVM.cmake | 9 +++++++--
mlir/CMakeLists.txt | 18 ++++++++++++------
3 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 44299d51d784a..5d3c13428348d 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -146,6 +146,10 @@ set(LLVM_EXTRA_PROJECTS "flang" "libc" "compiler-rt")
set(LLVM_KNOWN_PROJECTS "${LLVM_ALL_PROJECTS};${LLVM_EXTRA_PROJECTS}")
set(LLVM_ENABLE_PROJECTS "" CACHE STRING
"Semicolon-separated list of projects to build (${LLVM_KNOWN_PROJECTS}), or \"all\".")
+# Projects that are configured only to satisfy another project's dependencies.
+# Their targets are available to the build but are excluded from the default
+# build and install.
+set(LLVM_DEPENDENT_PROJECTS "")
# Make sure expansion happens first to not handle "all" in rest of the checks.
if( LLVM_ENABLE_PROJECTS STREQUAL "all" )
set( LLVM_ENABLE_PROJECTS ${LLVM_ALL_PROJECTS})
@@ -187,6 +191,7 @@ if ("flang" IN_LIST LLVM_ENABLE_PROJECTS)
if (NOT "mlir" IN_LIST LLVM_ENABLE_PROJECTS)
message(STATUS "Enabling MLIR as a dependency to flang")
list(APPEND LLVM_ENABLE_PROJECTS "mlir")
+ list(APPEND LLVM_DEPENDENT_PROJECTS "mlir")
endif()
if (NOT "clang" IN_LIST LLVM_ENABLE_PROJECTS)
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index a267166eb6c2d..7d951d499e34a 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1762,6 +1762,9 @@ function(add_llvm_subdirectory project type name)
if("${add_llvm_external_dir}" STREQUAL "")
set(add_llvm_external_dir ${name})
endif()
+ if("${name}" IN_LIST LLVM_DEPENDENT_PROJECTS)
+ set(exclude_from_all EXCLUDE_FROM_ALL)
+ endif()
canonicalize_tool_name(${name} nameUPPER)
set(canonical_full_name ${project}_${type}_${nameUPPER})
get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED)
@@ -1776,7 +1779,8 @@ function(add_llvm_subdirectory project type name)
"Whether to build ${name} as part of ${project}" On)
mark_as_advanced(${project}_${type}_${name}_BUILD)
if(${canonical_full_name}_BUILD)
- add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
+ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}
+ ${add_llvm_external_dir} ${exclude_from_all})
endif()
else()
set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
@@ -1794,7 +1798,8 @@ function(add_llvm_subdirectory project type name)
${${canonical_full_name}_BUILD_DEFAULT})
if (${canonical_full_name}_BUILD)
if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
- add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
+ add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}
+ ${add_llvm_external_dir} ${exclude_from_all})
elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
endif()
diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt
index ec4b8f7293490..f5151ba7dd0a1 100644
--- a/mlir/CMakeLists.txt
+++ b/mlir/CMakeLists.txt
@@ -270,14 +270,20 @@ add_subdirectory(lib/CAPI)
if (MLIR_INCLUDE_TESTS)
add_definitions(-DMLIR_INCLUDE_TESTS)
- add_custom_target(MLIRUnitTests)
- set_target_properties(MLIRUnitTests PROPERTIES FOLDER "MLIR/Tests")
- if (TARGET llvm_gtest)
- add_subdirectory(unittests)
+ if ("mlir" IN_LIST LLVM_DEPENDENT_PROJECTS)
+ # Make test support libraries available to dependent projects without
+ # registering MLIR's own unit and regression tests with check-all.
+ add_subdirectory(test/lib)
else()
- message(WARNING "gtest not found, unittests will not be available")
+ add_custom_target(MLIRUnitTests)
+ set_target_properties(MLIRUnitTests PROPERTIES FOLDER "MLIR/Tests")
+ if (TARGET llvm_gtest)
+ add_subdirectory(unittests)
+ else()
+ message(WARNING "gtest not found, unittests will not be available")
+ endif()
+ add_subdirectory(test)
endif()
- add_subdirectory(test)
endif()
# Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
# Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
More information about the llvm-commits
mailing list