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

Joseph Huber via Openmp-commits openmp-commits at lists.llvm.org
Wed Jun 3 08:50:15 PDT 2026


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

>From 2e3f8cb8fcbfdccb5e02baaa5fbe35ef5664f880 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 1/2] [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.

comment

commnets

Handle ID for MSCV
---
 flang-rt/lib/runtime/CMakeLists.txt           | 21 ++++++++
 .../modules/LLVMExternalProjectUtils.cmake    |  5 +-
 llvm/runtimes/CMakeLists.txt                  | 48 +++++++++++++++++--
 openmp/module/CMakeLists.txt                  | 13 ++++-
 runtimes/cmake/config-Fortran.cmake           | 26 +++++++---
 5 files changed, 96 insertions(+), 17 deletions(-)

diff --git a/flang-rt/lib/runtime/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt
index 20aa3effeec94..a956cda31b52d 100644
--- a/flang-rt/lib/runtime/CMakeLists.txt
+++ b/flang-rt/lib/runtime/CMakeLists.txt
@@ -305,6 +305,27 @@ if (FLANG_RT_FORTRAN_MODULES)
   add_dependencies(flang_rt.mod flang_rt.mod.intrinsics.barrier)
   flang_module_target(flang_rt.mod PUBLIC)
   add_module_barrier(flang-rt-mod flang_rt.mod)
+
+  if (RUNTIMES_FORTRAN_MODULES)
+    set(_mod_install_dest "${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}/..")
+    cmake_path(NORMAL_PATH _mod_install_dest)
+    install(DIRECTORY "${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}"
+      DESTINATION "${_mod_install_dest}"
+      COMPONENT flang-rt-mod
+      EXCLUDE_FROM_ALL
+    )
+
+    if (NOT CMAKE_CONFIGURATION_TYPES)
+      add_custom_target(install-flang-rt-mod
+        DEPENDS flang-rt-mod
+        COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}"
+                --component flang-rt-mod)
+      add_custom_target(install-flang-rt-mod-stripped
+        DEPENDS flang-rt-mod
+        COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}"
+                --component flang-rt-mod --strip)
+    endif ()
+  endif ()
 endif ()
 
 
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 707451408f091..78b1c196954d4 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -115,6 +115,27 @@ macro(append_passthrough_options out type names)
   endforeach()
 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 IN LISTS ${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})
 
@@ -278,8 +299,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.
@@ -293,8 +317,12 @@ function(runtime_default_target)
     endif ()
   endif ()
   if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
-    # The target flang-rt-mod is a dependee of check-flang.
+    # The target flang-rt-mod is a dependee of check-flang. install-flang-rt-mod
+    # is a separate distribution component so that install-flang-rt does not
+    # require building flang.
     list(APPEND extra_targets "flang-rt-mod")
+    list(APPEND extra_targets "install-flang-rt-mod")
+    list(APPEND extra_targets "install-flang-rt-mod-stripped")
   endif ()
 
   if(LLVM_INCLUDE_TESTS)
@@ -337,6 +365,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)
@@ -491,6 +521,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.
@@ -514,7 +546,6 @@ if(build_runtimes)
   if(LLVM_INCLUDE_TESTS)
     foreach(dep FileCheck
                 clang
-                flang
                 count
                 lld
                 lli
@@ -800,11 +831,18 @@ ${_flag_values}\
   if(LLVM_INCLUDE_TESTS)
     set_property(GLOBAL APPEND PROPERTY LLVM_ALL_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
 
+    # Flang is not in extra_deps to avoid making runtimes configure depend on
+    # it being fully built, but tests still need it.
+    set(extra_test_deps ${extra_deps})
+    if(TARGET flang)
+      list(APPEND extra_test_deps flang)
+    endif()
+
     foreach(target ${test_targets} ${SUB_CHECK_TARGETS})
-      add_dependencies(${target} ${extra_deps})
+      add_dependencies(${target} ${extra_test_deps})
     endforeach()
 
-    set_property(GLOBAL APPEND PROPERTY LLVM_ALL_ADDITIONAL_TEST_TARGETS runtimes ${extra_deps})
+    set_property(GLOBAL APPEND PROPERTY LLVM_ALL_ADDITIONAL_TEST_TARGETS runtimes ${extra_test_deps})
   endif()
 
   # Flang tests depend on module files built by the runtimes.
diff --git a/openmp/module/CMakeLists.txt b/openmp/module/CMakeLists.txt
index 3bd7e7684bb37..8dea994af4bc6 100644
--- a/openmp/module/CMakeLists.txt
+++ b/openmp/module/CMakeLists.txt
@@ -38,7 +38,16 @@ add_dependencies(libomp-mod ${RUNTIMES_FORTRAN_BUILD_DEPS})
 install(FILES
   "${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}/omp_lib.h"
   DESTINATION "${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}"
-    COMPONENT openmp
+  COMPONENT libomp-mod
 )
 
-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 ()
diff --git a/runtimes/cmake/config-Fortran.cmake b/runtimes/cmake/config-Fortran.cmake
index 74fd15021f0a0..a4d3d26b6024d 100644
--- a/runtimes/cmake/config-Fortran.cmake
+++ b/runtimes/cmake/config-Fortran.cmake
@@ -88,15 +88,20 @@ 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}")
+    if (CMAKE_C_SIMULATE_ID)
+      set(CMAKE_Fortran_SIMULATE_ID "${CMAKE_C_SIMULATE_ID}")
+    endif ()
+
     # 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 +152,15 @@ 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.
+# The build-order dependency in add_flang_mod_deps ensures the real binary is
+# built before anything tries to invoke this placeholder.
+if (CMAKE_Fortran_COMPILER_FORCED AND NOT EXISTS "${CMAKE_Fortran_COMPILER}")
+  get_filename_component(_compiler_dir "${CMAKE_Fortran_COMPILER}" DIRECTORY)
+  file(MAKE_DIRECTORY "${_compiler_dir}")
+  file(WRITE "${CMAKE_Fortran_COMPILER}" "")
+endif ()
 
 include(CheckLanguage)
 check_language(Fortran)

>From c5d366e3982f37a1293d8d38e1d5c130f2ca5bdb Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Wed, 3 Jun 2026 10:49:58 -0500
Subject: [PATCH 2/2] comments

---
 llvm/runtimes/CMakeLists.txt        | 24 +++++++++++-------------
 openmp/module/CMakeLists.txt        |  1 +
 runtimes/cmake/config-Fortran.cmake | 16 ++++++++++++----
 3 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index 78b1c196954d4..bbc91a709c553 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -115,26 +115,24 @@ macro(append_passthrough_options out type names)
   endforeach()
 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)
+# Wire build-order dependencies on flang for targets that invoke it. This
+# allows us to configure the runtimes without first building flang. Attaches the
+# flang dependency to the main build target and any fortran module file builds,
+# like 'install-libomp-mod' or `install-flang-rt-mod'.
+function(add_flang_mod_deps build_target)
   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})
+      add_dependencies(${build_target} flang)
     endif()
-    foreach(tgt IN LISTS ${extra_targets_list})
+    foreach(tgt IN LISTS ARGN)
       if(tgt MATCHES "-mod($|-)")
         if(TARGET ${tgt})
-          add_dependencies(${tgt} ${_fortran_deps})
+          add_dependencies(${tgt} flang)
         endif()
       endif()
     endforeach()
   endif()
-endmacro()
+endfunction()
 
 function(builtin_default_target compiler_rt_path)
   cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
@@ -366,7 +364,7 @@ function(runtime_default_target)
                            FOLDER "Runtimes"
                            ${EXTRA_ARGS} ${ARG_EXTRA_ARGS})
 
-  add_flang_mod_deps(runtimes-build extra_targets)
+  add_flang_mod_deps(runtimes-build ${extra_targets})
 endfunction()
 
 # runtime_register_target(name)
@@ -522,7 +520,7 @@ function(runtime_register_target name)
     add_dependencies(install-${component}-stripped install-${component}-${name}-stripped)
   endforeach()
 
-  add_flang_mod_deps(runtimes-${name}-build ${name}_extra_targets)
+  add_flang_mod_deps(runtimes-${name}-build ${${name}_extra_targets})
 endfunction()
 
 # Check if we have any runtimes to build.
diff --git a/openmp/module/CMakeLists.txt b/openmp/module/CMakeLists.txt
index 8dea994af4bc6..c4b9554bdffa4 100644
--- a/openmp/module/CMakeLists.txt
+++ b/openmp/module/CMakeLists.txt
@@ -39,6 +39,7 @@ install(FILES
   "${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}/omp_lib.h"
   DESTINATION "${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}"
   COMPONENT libomp-mod
+  EXCLUDE_FROM_ALL
 )
 
 if (NOT CMAKE_CONFIGURATION_TYPES)
diff --git a/runtimes/cmake/config-Fortran.cmake b/runtimes/cmake/config-Fortran.cmake
index a4d3d26b6024d..58602934fbaef 100644
--- a/runtimes/cmake/config-Fortran.cmake
+++ b/runtimes/cmake/config-Fortran.cmake
@@ -95,9 +95,7 @@ if (CMAKE_Fortran_COMPILER)
     set(CMAKE_Fortran_COMPILER_ID_RUN TRUE)
     set(CMAKE_Fortran_COMPILER_FORCED TRUE)
     set(CMAKE_Fortran_COMPILER_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
-    if (CMAKE_C_SIMULATE_ID)
-      set(CMAKE_Fortran_SIMULATE_ID "${CMAKE_C_SIMULATE_ID}")
-    endif ()
+    set(CMAKE_Fortran_SIMULATE_ID "GNU")
 
     # 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.
@@ -159,7 +157,17 @@ endif ()
 if (CMAKE_Fortran_COMPILER_FORCED AND NOT EXISTS "${CMAKE_Fortran_COMPILER}")
   get_filename_component(_compiler_dir "${CMAKE_Fortran_COMPILER}" DIRECTORY)
   file(MAKE_DIRECTORY "${_compiler_dir}")
-  file(WRITE "${CMAKE_Fortran_COMPILER}" "")
+  file(WRITE "${CMAKE_Fortran_COMPILER}" "stub")
+  file(CHMOD "${CMAKE_Fortran_COMPILER}"
+    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
+                GROUP_READ GROUP_EXECUTE
+                WORLD_READ WORLD_EXECUTE)
+  # Ninja uses file mtimes to decide whether build outputs are up-to-date.
+  # If this placeholder's mtime is recent it may match what is recorded in
+  # .ninja_log, causing ninja to skip building the real compiler binary.
+  # Set it so that any subsequent real build always has a newer mtime.
+  execute_process(COMMAND touch -t 197001020000 "${CMAKE_Fortran_COMPILER}"
+                  ERROR_QUIET)
 endif ()
 
 include(CheckLanguage)



More information about the Openmp-commits mailing list