[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:54:52 PDT 2026


https://github.com/joker-eph updated https://github.com/llvm/llvm-project/pull/221724

>From b92b5b92c569748e2e17815b4565d0b16310e07f 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 dependency-only 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.

Record implicit dependencies in the internal-only
LLVM_DEPENDENCY_ONLY_PROJECTS list without changing LLVM_ENABLE_PROJECTS.
Normalize the list after collecting dependencies so duplicate requests are
removed and explicit project selections always take precedence.

Teach project setup to configure the union of both lists and add
dependency-only source directories with EXCLUDE_FROM_ALL. This keeps every
MLIR target available for normal dependency resolution while omitting MLIR-wide
build and install targets. Flang is the first user of this mode.

Let MLIR own its dependency-only test policy: configure test support libraries
for downstream consumers without creating MLIR unit tests or registering its
lit suite. Explicitly enabling MLIR preserves the existing behavior.

Assisted-by: Codex
---
 llvm/CMakeLists.txt              | 29 ++++++++++++++++++++---------
 llvm/cmake/modules/AddLLVM.cmake |  9 +++++++--
 mlir/CMakeLists.txt              | 18 ++++++++++++------
 3 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 44299d51d784a..49388e79b990b 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\".")
+# Internal-only list used to communicate projects that are configured to
+# satisfy another project's dependencies. Unlike LLVM_ENABLE_PROJECTS, these
+# projects are excluded from the default build and install.
+set(LLVM_DEPENDENCY_ONLY_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})
@@ -184,10 +188,7 @@ foreach(proj IN LISTS LLVM_ENABLE_RUNTIMES)
 endforeach()
 
 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")
-  endif()
+  list(APPEND LLVM_DEPENDENCY_ONLY_PROJECTS "mlir")
 
   if (NOT "clang" IN_LIST LLVM_ENABLE_PROJECTS)
     message(STATUS "Enabling clang as a dependency to flang")
@@ -270,6 +271,10 @@ if(LIBOMP_ENABLE_ARM64X)
   set(RUNTIMES_arm64ec-pc-windows-msvc_LIBOMP_ENABLE_ARM64X ON)
 endif()
 
+# Explicitly enabled projects take precedence over dependency-only requests.
+list(REMOVE_DUPLICATES LLVM_DEPENDENCY_ONLY_PROJECTS)
+list(REMOVE_ITEM LLVM_DEPENDENCY_ONLY_PROJECTS ${LLVM_ENABLE_PROJECTS})
+
 # LLVM_ENABLE_PROJECTS_USED is `ON` if the user has ever used the
 # `LLVM_ENABLE_PROJECTS` CMake cache variable.  This exists for
 # several reasons:
@@ -292,8 +297,13 @@ if (LLVM_ENABLE_PROJECTS_USED OR NOT LLVM_ENABLE_PROJECTS STREQUAL "")
   foreach(proj ${LLVM_KNOWN_PROJECTS} ${LLVM_EXTERNAL_PROJECTS})
     string(TOUPPER "${proj}" upper_proj)
     string(REGEX REPLACE "-" "_" upper_proj ${upper_proj})
-    if ("${proj}" IN_LIST LLVM_ENABLE_PROJECTS)
-      message(STATUS "${proj} project is enabled")
+    if ("${proj}" IN_LIST LLVM_ENABLE_PROJECTS OR
+        "${proj}" IN_LIST LLVM_DEPENDENCY_ONLY_PROJECTS)
+      if ("${proj}" IN_LIST LLVM_DEPENDENCY_ONLY_PROJECTS)
+        message(STATUS "${proj} project is enabled as a dependency")
+      else()
+        message(STATUS "${proj} project is enabled")
+      endif()
       set(SHOULD_ENABLE_PROJECT TRUE)
       set(PROJ_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
       if(NOT EXISTS "${PROJ_DIR}" OR NOT IS_DIRECTORY "${PROJ_DIR}")
@@ -312,9 +322,10 @@ if (LLVM_ENABLE_PROJECTS_USED OR NOT LLVM_ENABLE_PROJECTS STREQUAL "")
       set(SHOULD_ENABLE_PROJECT FALSE)
     endif()
     # Force `LLVM_TOOL_${upper_proj}_BUILD` variables to have values that
-    # corresponds with `LLVM_ENABLE_PROJECTS`. This prevents the user setting
-    # `LLVM_TOOL_${upper_proj}_BUILD` variables externally. At some point
-    # we should deprecate allowing users to set these variables by turning them
+    # correspond with `LLVM_ENABLE_PROJECTS` and
+    # `LLVM_DEPENDENCY_ONLY_PROJECTS`. This prevents the user setting
+    # `LLVM_TOOL_${upper_proj}_BUILD` variables externally. At some point we
+    # should deprecate allowing users to set these variables by turning them
     # into normal CMake variables rather than cache variables.
     set(LLVM_TOOL_${upper_proj}_BUILD
       ${SHOULD_ENABLE_PROJECT}
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index a267166eb6c2d..4adc5c8ce6b7c 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_DEPENDENCY_ONLY_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..f8102de9bbeb1 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_DEPENDENCY_ONLY_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