[Openmp-commits] [llvm] [openmp] [Runtimes] Drop 'flang' from runtimes configure dependency (PR #198205)

Joseph Huber via Openmp-commits openmp-commits at lists.llvm.org
Sun May 17 12:09:40 PDT 2026


https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/198205

Summary:
This PR cuts `flang` out of the core runtimes configure dependency. We
will need the runtimes infrastructure to handle `flang` module
generation, but this dependency poisons the entire dependency stack.
`flang` and `mlir` are by far the heavily parts of the LLVM stack and
for distribution we want to only build it when absolutely necessary,
which as of now is only to install flang module files.

The approach here is to simply remove the `flang` target from the core
dependency tree, but intead configure it for the top-level `module`
targets which are part of `all`. To make this work we need to pass
COMPILER_WORKS and set up a dummy location so that configuration passes.
it's a little backdoor, but this is an extremely important quality of
life improvement for LLVM distribution support.


>From 59c00cd37992288ee9716fb0a6a34794c3733019 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Sun, 17 May 2026 13:23:36 -0500
Subject: [PATCH] [Runtimes] Drop 'flang' from runtimes configure dependency

Summary:
This PR cuts `flang` out of the core runtimes configure dependency. We
will need the runtimes infrastructure to handle `flang` module
generation, but this dependency poisons the entire dependency stack.
`flang` and `mlir` are by far the heavily parts of the LLVM stack and
for distribution we want to only build it when absolutely necessary,
which as of now is only to install flang module files.

The approach here is to simply remove the `flang` target from the core
dependency tree, but intead configure it for the top-level `module`
targets which are part of `all`. To make this work we need to pass
COMPILER_WORKS and set up a dummy location so that configuration passes.
it's a little backdoor, but this is an extremely important quality of
life improvement for LLVM distribution support.
---
 .../modules/LLVMExternalProjectUtils.cmake    |  5 +--
 llvm/runtimes/CMakeLists.txt                  | 39 ++++++++++++++-----
 openmp/module/CMakeLists.txt                  | 15 +++++--
 runtimes/cmake/config-Fortran.cmake           | 19 ++++++---
 4 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/llvm/cmake/modules/LLVMExternalProjectUtils.cmake b/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
index 1c44a106e94af..47b12dc9a3d8c 100644
--- a/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
+++ b/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
@@ -100,9 +100,6 @@ function(llvm_ExternalProject_Add name source_dir)
 
   if(NOT ARG_TOOLCHAIN_TOOLS)
     set(ARG_TOOLCHAIN_TOOLS clang)
-    if (ARG_ENABLE_FORTRAN)
-      list(APPEND ARG_TOOLCHAIN_TOOLS flang)
-    endif ()
     # AIX 64-bit XCOFF and big AR format is not yet supported in some of these tools.
     if(NOT _cmake_system_name STREQUAL "AIX")
       list(APPEND ARG_TOOLCHAIN_TOOLS lld llvm-ar llvm-ranlib llvm-nm llvm-objdump)
@@ -153,7 +150,7 @@ function(llvm_ExternalProject_Add name source_dir)
     set(CLANG_IN_TOOLCHAIN On)
   endif()
 
-  if(flang IN_LIST TOOLCHAIN_TOOLS)
+  if(ARG_ENABLE_FORTRAN AND TARGET flang)
     set(FLANG_IN_TOOLCHAIN On)
   endif()
 
diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index e31e1c935554f..2c76596fced1b 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -91,6 +91,27 @@ macro(set_enable_per_target_runtime_dir)
   endif()
 endmacro()
 
+# Wire build-order dependencies on flang for targets that invoke it. This allows
+# us to build the runtimes separately without first building flang.
+macro(add_flang_mod_deps build_target extra_targets_list)
+  if(TARGET flang)
+    set(_fortran_deps flang)
+    if(TARGET module_files)
+      list(APPEND _fortran_deps module_files)
+    endif()
+    if(TARGET ${build_target})
+      add_dependencies(${build_target} ${_fortran_deps})
+    endif()
+    foreach(tgt ${${extra_targets_list}})
+      if(tgt MATCHES "-mod($|-)")
+        if(TARGET ${tgt})
+          add_dependencies(${tgt} ${_fortran_deps})
+        endif()
+      endif()
+    endforeach()
+  endif()
+endmacro()
+
 function(builtin_default_target compiler_rt_path)
   cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
 
@@ -263,8 +284,11 @@ function(runtime_default_target)
   endforeach()
   if ("openmp" IN_LIST LLVM_ENABLE_RUNTIMES)
     # The target libomp-mod is a dependee of check-flang needed to run its
-    # OpenMP tests
+    # OpenMP tests. install-libomp-mod is a separate distribution component
+    # so that install-openmp does not require building flang.
     list(APPEND extra_targets "libomp-mod")
+    list(APPEND extra_targets "install-libomp-mod")
+    list(APPEND extra_targets "install-libomp-mod-stripped")
 
     # Let the "doxygen" build target also build openmp's doxygen, just like
     # "check" and "install" also apply to the runtimes builds.
@@ -318,6 +342,8 @@ function(runtime_default_target)
                            TARGET_TRIPLE ${LLVM_TARGET_TRIPLE}
                            FOLDER "Runtimes"
                            ${EXTRA_ARGS} ${ARG_EXTRA_ARGS})
+
+  add_flang_mod_deps(runtimes-build extra_targets)
 endfunction()
 
 # runtime_register_target(name)
@@ -497,6 +523,8 @@ function(runtime_register_target name)
     add_dependencies(install-${component} install-${component}-${name})
     add_dependencies(install-${component}-stripped install-${component}-${name}-stripped)
   endforeach()
+
+  add_flang_mod_deps(runtimes-${name}-build ${name}_extra_targets)
 endfunction()
 
 # Check if we have any runtimes to build.
@@ -520,7 +548,6 @@ if(build_runtimes)
   if(LLVM_INCLUDE_TESTS)
     foreach(dep FileCheck
                 clang
-                flang
                 count
                 lld
                 lli
@@ -596,7 +623,7 @@ if(build_runtimes)
       endif()
     endforeach()
   endif()
-  # Allow openmp to see the Fortran compiler
+  # Allow openmp to see the Fortran compiler without requiring it to be built.
   if ("openmp" IN_LIST LLVM_ENABLE_RUNTIMES AND "flang" IN_LIST LLVM_ENABLE_PROJECTS)
     list(APPEND extra_args ENABLE_FORTRAN)
   endif()
@@ -605,12 +632,6 @@ if(build_runtimes)
       message(STATUS "Configuring build of omp_lib.mod and omp_lib_kinds.mod via flang")
       set(LIBOMP_FORTRAN_MODULES_COMPILER "${CMAKE_BINARY_DIR}/bin/flang")
       set(LIBOMP_MODULES_INSTALL_PATH "${CMAKE_INSTALL_INCLUDEDIR}/flang")
-      # TODO: This is a workaround until flang becomes a first-class project
-      # in llvm/CMakeList.txt.  Until then, this line ensures that flang is
-      # built before "openmp" is built as a runtime project.  Besides "flang"
-      # to build the compiler, we also need to add "module_files" to make sure
-      # that all .mod files are also properly build.
-      list(APPEND extra_deps "flang" "module_files")
     endif()
     foreach(dep opt llvm-link llvm-extract clang llvm-offload-binary clang-nvlink-wrapper)
       if(TARGET ${dep})
diff --git a/openmp/module/CMakeLists.txt b/openmp/module/CMakeLists.txt
index 9265e70dd78cb..872b69f6c386e 100644
--- a/openmp/module/CMakeLists.txt
+++ b/openmp/module/CMakeLists.txt
@@ -85,8 +85,17 @@ if (BUILD_FORTRAN_MODULES)
     "${CMAKE_CURRENT_BINARY_DIR}/../runtime/src/omp_lib_kinds.mod"
     "${CMAKE_CURRENT_BINARY_DIR}/../runtime/src/omp_lib.h"
     DESTINATION ${destination}
-    COMPONENT openmp
+    COMPONENT libomp-mod
   )
-endif ()
 
-openmp_register_meta_dep(libomp-mod)
+  if (NOT CMAKE_CONFIGURATION_TYPES)
+    add_custom_target(install-libomp-mod
+      DEPENDS libomp-mod
+      COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}"
+              --component libomp-mod)
+    add_custom_target(install-libomp-mod-stripped
+      DEPENDS libomp-mod
+      COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}"
+              --component libomp-mod --strip)
+  endif ()
+endif ()
diff --git a/runtimes/cmake/config-Fortran.cmake b/runtimes/cmake/config-Fortran.cmake
index f5cea1fc67e29..6ad63e68b8b4a 100644
--- a/runtimes/cmake/config-Fortran.cmake
+++ b/runtimes/cmake/config-Fortran.cmake
@@ -88,15 +88,17 @@ if (CMAKE_Fortran_COMPILER)
   # cannot use CMAKE_Fortran_COMPILER_ID.
   cmake_path(GET CMAKE_Fortran_COMPILER STEM _Fortran_COMPILER_STEM)
   if (_Fortran_COMPILER_STEM STREQUAL "flang-new" OR _Fortran_COMPILER_STEM STREQUAL "flang")
+    # Force the compiler ID so CMake does not try to run the compiler for
+    # identification. In a bootstrapping build the Flang binary may not be
+    # built yet at configure time (only CMAKE_Fortran_COMPILER_WORKS is set).
+    set(CMAKE_Fortran_COMPILER_ID "LLVMFlang")
+    set(CMAKE_Fortran_COMPILER_ID_RUN TRUE)
+    set(CMAKE_Fortran_COMPILER_FORCED TRUE)
+    set(CMAKE_Fortran_COMPILER_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
+
     # CMake 3.24 is the first version of CMake that directly recognizes Flang.
     # LLVM's requirement is only CMake 3.20, teach CMake 3.20-3.23 how to use Flang, if used.
     if (CMAKE_VERSION VERSION_LESS "3.24")
-      include(CMakeForceCompiler)
-      CMAKE_FORCE_Fortran_COMPILER("${CMAKE_Fortran_COMPILER}" "LLVMFlang")
-
-      set(CMAKE_Fortran_COMPILER_ID "LLVMFlang")
-      set(CMAKE_Fortran_COMPILER_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
-
       set(CMAKE_Fortran_SUBMODULE_SEP "-")
       set(CMAKE_Fortran_SUBMODULE_EXT ".mod")
 
@@ -147,6 +149,11 @@ else ()
   return ()
 endif ()
 
+# In a bootstrapping build the Fortran compiler may not have been built yet.
+# Create a placeholder so CMake's enable_language() existence check passes.
+if (CMAKE_Fortran_COMPILER_FORCED AND NOT EXISTS "${CMAKE_Fortran_COMPILER}")
+  file(TOUCH "${CMAKE_Fortran_COMPILER}")
+endif ()
 
 include(CheckLanguage)
 check_language(Fortran)



More information about the Openmp-commits mailing list