[llvm] [runtimes][cmake] Probe Fortran intrinsic modules for the target triple (PR #211137)
Spencer Bryngelson via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 23 07:41:42 PDT 2026
https://github.com/sbryngelson updated https://github.com/llvm/llvm-project/pull/211137
>From 84baae1d15b3e9a4f620b73ba9bcfcfe903873c3 Mon Sep 17 00:00:00 2001
From: Spencer Bryngelson <sbryngelson at gmail.com>
Date: Tue, 21 Jul 2026 17:45:51 -0500
Subject: [PATCH] [runtimes][cmake] Probe Fortran intrinsic modules for the
target triple
check_fortran_builtins_available() decides whether the toolchain provides
Fortran intrinsic modules, but its -print-file-name probe runs with no
--target, and its check_fortran_source_compiles() fallback uses try_compile(),
which only passes CMAKE_Fortran_COMPILER_TARGET from CMake 3.28 on.
Flang's intrinsic modules are built per-target by flang-rt. When a runtime is
configured for a GPU target without flang-rt in its runtime list, the probe
tests the host and succeeds, so RUNTIMES_FORTRAN_MODULES is enabled for a
target that cannot support it. The failure is then deferred to a cascade of
BIND(C) diagnostics compiling omp_lib.F90, all stemming from one
missing-module error.
Pass the triple to the -print-file-name probe, using
CMAKE_Fortran_COMPILE_OPTIONS_TARGET rather than a hard-coded --target=, since
that variable is --target= for Flang and empty for GNU; gfortran rejects
--target= and would otherwise lose its modules.
Setting CMAKE_Fortran_COMPILE_OPTIONS_TARGET ourselves cannot fix the
try_compile() side: CMake sets it in CMakeFortranCompiler.cmake, whose scope
try_compile() inherits, while ours is only directory scope. So on CMake before
3.28 use the -print-file-name probe instead, extending the existing workaround
for CMake before 3.24. From 3.28 on, CMake passes --target= to try_compile()
itself and nothing needs to be added.
---
runtimes/cmake/config-Fortran.cmake | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/runtimes/cmake/config-Fortran.cmake b/runtimes/cmake/config-Fortran.cmake
index ba683c3ce28e6..869e03515e09c 100644
--- a/runtimes/cmake/config-Fortran.cmake
+++ b/runtimes/cmake/config-Fortran.cmake
@@ -39,7 +39,8 @@
# a compiler introspection environment, see
# https://gitlab.kitware.com/cmake/cmake/-/issues/27419
function (check_fortran_builtins_available)
- if (CMAKE_Fortran_COMPILER_FORCED AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
+ if (CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang" AND
+ (CMAKE_Fortran_COMPILER_FORCED OR CMAKE_VERSION VERSION_LESS "3.28"))
# CMake's check_fortran_source_compiles/try_compile does not take a
# user-defined CMAKE_Fortran_PREPROCESS_SOURCE into account. Instead of
# test-compiling, ask Flang directly for the builtin module files.
@@ -47,10 +48,23 @@ function (check_fortran_builtins_available)
# does not natively recognize Flang (see below). Once we bump the required
# CMake version, and because setting CMAKE_Fortran_PREPROCESS_SOURCE has
# been deprecated by CMake, this workaround can be removed.
+ #
+ # The same path is taken for CMake 3.24 to 3.27, which do recognize Flang
+ # but do not pass --target= to try_compile. Setting
+ # CMAKE_Fortran_COMPILE_OPTIONS_TARGET ourselves (see below) does not help:
+ # CMake sets it in CMakeFortranCompiler.cmake, whose scope try_compile
+ # inherits, while ours is only a directory-scope variable. The probe would
+ # therefore compile for the host and report the host's modules.
if (NOT DEFINED FORTRAN_HAS_ISO_C_BINDING_MOD)
message(STATUS "Performing Test ISO_C_BINDING_PATH")
+ # Flang's intrinsic modules are per-target; probe for the target we are
+ # compiling for. Empty for compilers without such an option (e.g. GNU).
+ set(_fortran_target_flag "")
+ if (CMAKE_Fortran_COMPILER_TARGET AND CMAKE_Fortran_COMPILE_OPTIONS_TARGET)
+ set(_fortran_target_flag "${CMAKE_Fortran_COMPILE_OPTIONS_TARGET}${CMAKE_Fortran_COMPILER_TARGET}")
+ endif ()
execute_process(
- COMMAND ${CMAKE_Fortran_COMPILER} ${CMAKE_Fortran_FLAGS} "-print-file-name=iso_c_binding.mod"
+ COMMAND ${CMAKE_Fortran_COMPILER} ${_fortran_target_flag} ${CMAKE_Fortran_FLAGS} "-print-file-name=iso_c_binding.mod"
OUTPUT_VARIABLE ISO_C_BINDING_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
@@ -67,6 +81,8 @@ function (check_fortran_builtins_available)
else ()
cmake_push_check_state(RESET)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
+ # CMake 3.28 and later pass --target= to try_compile themselves, so nothing
+ # to add here. Do not pass it a second time.
check_fortran_source_compiles("
subroutine testroutine
use iso_c_binding
More information about the llvm-commits
mailing list