[llvm] [mlir] [CMake] Add dependent project mode for Flang's MLIR (PR #221724)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 06:30:15 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Mehdi Amini (joker-eph)

<details>
<summary>Changes</summary>

LLVM_ENABLE_PROJECTS currently treats projects enabled to satisfy another project's dependency the same as projects selected by the user. As a result, enabling Flang pulls the entire MLIR project, install rules, and test suites into aggregate targets.

Track implicitly configured projects in LLVM_DEPENDENT_PROJECTS and pass EXCLUDE_FROM_ALL when adding their source directories. This leaves all MLIR targets available for Flang's normal dependency resolution without attaching the MLIR-wide target to the default build or install.

Also force MLIR_INCLUDE_TESTS off when MLIR is pulled in by Flang. This avoids registering MLIR unit tests and lit suites with check-all. Explicitly listing mlir continues to use existing behavior and honors MLIR_INCLUDE_TESTS.

Assisted-by: Codex

---
Full diff: https://github.com/llvm/llvm-project/pull/221724.diff


3 Files Affected:

- (modified) llvm/CMakeLists.txt (+5) 
- (modified) llvm/cmake/modules/AddLLVM.cmake (+7-2) 
- (modified) mlir/CMakeLists.txt (+12-6) 


``````````diff
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.

``````````

</details>


https://github.com/llvm/llvm-project/pull/221724


More information about the llvm-commits mailing list