[flang] [llvm] [flang] Generate quadmath_wrapper.h for Flang Evaluate. (PR #132817)

Slava Zakharin via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 25 09:06:24 PDT 2025


https://github.com/vzakhari updated https://github.com/llvm/llvm-project/pull/132817

>From 2c3e27a91dc517a66203c47d55762a2547511b31 Mon Sep 17 00:00:00 2001
From: Slava Zakharin <szakharin at nvidia.com>
Date: Mon, 24 Mar 2025 12:05:29 -0700
Subject: [PATCH 1/2] [flang] Generate quadmath_wrapper.h for Flang Evaluate.

When building Flang with Clang, we need to do the same quadmath.h
wrapping as we do for flang-rt. I extracted the CMake code
into FlangCommon.cmake, and cleaned up the arguments passing
to execute_process (note that `-###` was treated as `-` in the original
code, because `#` starts a comment). I believe the Clang command
does not require the input source file, so I removed it as well.
---
 flang-rt/CMakeLists.txt                   | 40 +-------------------
 flang-rt/cmake/clang_gcc_root.cpp         |  1 -
 flang-rt/cmake/quadmath_wrapper.h.in      |  2 +-
 flang-rt/lib/quadmath/CMakeLists.txt      |  2 +-
 flang/cmake/modules/FlangCommon.cmake     | 46 +++++++++++++++++++++++
 flang/cmake/quadmath_wrapper.h.in         | 23 ++++++++++++
 flang/lib/Evaluate/CMakeLists.txt         |  6 +--
 flang/lib/Evaluate/host.h                 |  2 +-
 flang/lib/Evaluate/intrinsics-library.cpp |  2 +-
 9 files changed, 77 insertions(+), 47 deletions(-)
 delete mode 100644 flang-rt/cmake/clang_gcc_root.cpp
 create mode 100644 flang/cmake/quadmath_wrapper.h.in

diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index 38e2613f1fee2..b3b6e00f7c0c8 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -290,44 +290,6 @@ set(HAVE_BACKTRACE ${Backtrace_FOUND})
 set(BACKTRACE_HEADER ${Backtrace_HEADER})
 
 
-if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
-  if (NOT DEFINED FLANG_RT_GCC_RESOURCE_DIR)
-    set(FLANG_RT_GCC_RESOURCE_DIR "FLANG_RT_GCC_RESOURCE_DIR-NOTFOUND")
-    execute_process(
-      COMMAND "${CMAKE_CXX_COMPILER}" -v -c "${FLANG_RT_SOURCE_DIR}/cmake/clang_gcc_root.cpp" ${CMAKE_CXX_FLAGS} -###
-      ERROR_FILE "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result"
-    )
-    file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result" _errorresult)
-    foreach (_line IN LISTS _errorresult)
-      string(REGEX MATCH
-        "^Selected GCC installation: (.+)$"
-        _match
-        "${_line}")
-      if (CMAKE_MATCH_1)
-        set(FLANG_RT_GCC_RESOURCE_DIR "${CMAKE_MATCH_1}")
-        message(STATUS "Found GCC installation selected by Clang: ${FLANG_RT_GCC_RESOURCE_DIR}")
-        break()
-      endif ()
-    endforeach ()
-    set(FLANG_RT_GCC_RESOURCE_DIR "${FLANG_RT_GCC_RESOURCE_DIR}" CACHE INTERNAL "Path to GCC's resource dir selected by Clang" FORCE)
-  endif ()
-endif ()
-
-check_include_file("quadmath.h" FOUND_QUADMATH_H)
-if (FOUND_QUADMATH_H)
-  message(STATUS "quadmath.h found without additional include paths")
-  set(FLANG_RT_INCLUDE_QUADMATH_H "<quadmath.h>")
-elseif (FLANG_RT_GCC_RESOURCE_DIR)
-  cmake_push_check_state()
-    list(APPEND CMAKE_REQUIRED_INCLUDES "${FLANG_RT_GCC_RESOURCE_DIR}/include")
-    check_include_file("quadmath.h" FOUND_GCC_QUADMATH_H)
-  cmake_pop_check_state()
-  if (FOUND_GCC_QUADMATH_H)
-    message(STATUS "quadmath.h found in Clang's selected GCC installation")
-    set(FLANG_RT_INCLUDE_QUADMATH_H "\"${FLANG_RT_GCC_RESOURCE_DIR}/include/quadmath.h\"")
-  endif ()
-endif ()
-
 #####################
 # Build Preparation #
 #####################
@@ -351,7 +313,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED YES)
 
 
 configure_file(cmake/config.h.cmake.in config.h)
-if (FLANG_RT_INCLUDE_QUADMATH_H)
+if (FLANG_INCLUDE_QUADMATH_H)
   configure_file("cmake/quadmath_wrapper.h.in" "${FLANG_RT_BINARY_DIR}/quadmath_wrapper.h")
 endif ()
 
diff --git a/flang-rt/cmake/clang_gcc_root.cpp b/flang-rt/cmake/clang_gcc_root.cpp
deleted file mode 100644
index c3aeba4bb65d9..0000000000000
--- a/flang-rt/cmake/clang_gcc_root.cpp
+++ /dev/null
@@ -1 +0,0 @@
-int main() { return 0; }
\ No newline at end of file
diff --git a/flang-rt/cmake/quadmath_wrapper.h.in b/flang-rt/cmake/quadmath_wrapper.h.in
index c29358efcdb0d..ce90b58fbce76 100644
--- a/flang-rt/cmake/quadmath_wrapper.h.in
+++ b/flang-rt/cmake/quadmath_wrapper.h.in
@@ -6,4 +6,4 @@
  *
  *===----------------------------------------------------------------------===*/
 
-#include ${FLANG_RT_INCLUDE_QUADMATH_H}
+#include ${FLANG_INCLUDE_QUADMATH_H}
diff --git a/flang-rt/lib/quadmath/CMakeLists.txt b/flang-rt/lib/quadmath/CMakeLists.txt
index eaf8202d7bce9..353c72def24de 100644
--- a/flang-rt/lib/quadmath/CMakeLists.txt
+++ b/flang-rt/lib/quadmath/CMakeLists.txt
@@ -78,7 +78,7 @@ target_include_directories(FortranFloat128MathILib INTERFACE
 
 if (FLANG_RUNTIME_F128_MATH_LIB)
   if (${FLANG_RUNTIME_F128_MATH_LIB} STREQUAL "libquadmath")
-    if(FLANG_RT_INCLUDE_QUADMATH_H)
+    if(FLANG_INCLUDE_QUADMATH_H)
       add_compile_definitions(HAS_QUADMATHLIB)
     else()
       message(FATAL_ERROR
diff --git a/flang/cmake/modules/FlangCommon.cmake b/flang/cmake/modules/FlangCommon.cmake
index 9dbd1959fe7b1..e45f0afcc4576 100644
--- a/flang/cmake/modules/FlangCommon.cmake
+++ b/flang/cmake/modules/FlangCommon.cmake
@@ -37,3 +37,49 @@ check_c_source_compiles(
    int main() { return 0; }
   "
   HAVE_LDBL_MANT_DIG_113)
+
+# Discover the GCC installation, when the build compiler is Clang,
+# and try to find quadmath.h there. Set FLANG_INCLUDE_QUADMATH_H
+# to the path to quadmath.h, if found.
+if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+  if (NOT DEFINED FLANG_GCC_RESOURCE_DIR)
+    set(FLANG_GCC_RESOURCE_DIR "FLANG_GCC_RESOURCE_DIR-NOTFOUND")
+    # Prepare CMAKE_CXX_FLAGS so that they can be passed to execute_process
+    # as separate flags.
+    separate_arguments(flags UNIX_COMMAND "${CMAKE_CXX_FLAGS}")
+    set(hash_flag "-###")
+    set(command ${CMAKE_CXX_COMPILER} ${flags} ${hash_flag} -v)
+    execute_process(
+      COMMAND ${command}
+      ERROR_FILE "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result"
+    )
+    file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result" _errorresult)
+    foreach (_line IN LISTS _errorresult)
+      string(REGEX MATCH
+        "^Selected GCC installation: (.+)$"
+        _match
+        "${_line}")
+      if (CMAKE_MATCH_1)
+        set(FLANG_GCC_RESOURCE_DIR "${CMAKE_MATCH_1}")
+        message(STATUS "Found GCC installation selected by Clang: ${FLANG_GCC_RESOURCE_DIR}")
+        break()
+      endif ()
+    endforeach ()
+    set(FLANG_GCC_RESOURCE_DIR "${FLANG_GCC_RESOURCE_DIR}" CACHE INTERNAL "Path to GCC's resource dir selected by Clang" FORCE)
+  endif ()
+endif ()
+
+check_include_file("quadmath.h" FOUND_QUADMATH_H)
+if (FOUND_QUADMATH_H)
+  message(STATUS "quadmath.h found without additional include paths")
+  set(FLANG_INCLUDE_QUADMATH_H "<quadmath.h>")
+elseif (FLANG_GCC_RESOURCE_DIR)
+  cmake_push_check_state()
+    list(APPEND CMAKE_REQUIRED_INCLUDES "${FLANG_GCC_RESOURCE_DIR}/include")
+    check_include_file("quadmath.h" FOUND_GCC_QUADMATH_H)
+  cmake_pop_check_state()
+  if (FOUND_GCC_QUADMATH_H)
+    message(STATUS "quadmath.h found in Clang's selected GCC installation")
+    set(FLANG_INCLUDE_QUADMATH_H "\"${FLANG_GCC_RESOURCE_DIR}/include/quadmath.h\"")
+  endif ()
+endif ()
diff --git a/flang/cmake/quadmath_wrapper.h.in b/flang/cmake/quadmath_wrapper.h.in
new file mode 100644
index 0000000000000..da08ec1230ada
--- /dev/null
+++ b/flang/cmake/quadmath_wrapper.h.in
@@ -0,0 +1,23 @@
+/*===-- cmake/quadmath_wrapper.h.in ---------------------=-----------*- C -*-===
+ *
+ * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+ * See https://llvm.org/LICENSE.txt for license information.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ *
+ *===----------------------------------------------------------------------===*/
+
+#ifdef __clang_major__
+/*
+ * _Complex inside quadmath.h triggers c99-extension warnings,
+ * when this header file is included into a C++ file.
+ * Disable them just during the inclusion of quadmath.h.
+ */
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wc99-extensions"
+#endif
+
+#include ${FLANG_INCLUDE_QUADMATH_H}
+
+#ifdef __clang_major__
+#pragma clang diagnostic pop
+#endif
diff --git a/flang/lib/Evaluate/CMakeLists.txt b/flang/lib/Evaluate/CMakeLists.txt
index e0bdb6b2d24d8..7eb8ff934af46 100644
--- a/flang/lib/Evaluate/CMakeLists.txt
+++ b/flang/lib/Evaluate/CMakeLists.txt
@@ -21,12 +21,12 @@ if (LIBPGMATH_DIR)
   endif()
 endif()
 
-check_include_file(quadmath.h FOUND_QUADMATH_HEADER)
 check_library_exists(quadmath sinq "" FOUND_QUADMATH_LIB)
-if(FOUND_QUADMATH_HEADER AND FOUND_QUADMATH_LIB)
+if (FLANG_INCLUDE_QUADMATH_H AND FOUND_QUADMATH_LIB)
+  configure_file("${FLANG_SOURCE_DIR}/cmake/quadmath_wrapper.h.in" "${CMAKE_CURRENT_BINARY_DIR}/quadmath_wrapper.h")
   add_compile_definitions(HAS_QUADMATHLIB)
   set(QUADMATHLIB quadmath)
-endif()
+endif ()
 
 add_flang_library(FortranEvaluate
   call.cpp
diff --git a/flang/lib/Evaluate/host.h b/flang/lib/Evaluate/host.h
index 7edc254d6d4c7..fbb52f2886a40 100644
--- a/flang/lib/Evaluate/host.h
+++ b/flang/lib/Evaluate/host.h
@@ -18,7 +18,7 @@
 // to safely refer to this hardware type.
 
 #if HAS_QUADMATHLIB
-#include "quadmath.h"
+#include "quadmath_wrapper.h"
 #include "flang/Common/float128.h"
 #endif
 #include "flang/Evaluate/type.h"
diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp
index d2c1be65dca44..6363acfc5eb40 100644
--- a/flang/lib/Evaluate/intrinsics-library.cpp
+++ b/flang/lib/Evaluate/intrinsics-library.cpp
@@ -23,7 +23,7 @@
 #include <complex>
 #include <functional>
 #if HAS_QUADMATHLIB
-#include "quadmath.h"
+#include "quadmath_wrapper.h"
 #endif
 #include "flang/Common/float128.h"
 #include "flang/Common/float80.h"

>From b1aa6ba2eff84da67a70229a35965694c07afd6b Mon Sep 17 00:00:00 2001
From: Slava Zakharin <szakharin at nvidia.com>
Date: Mon, 24 Mar 2025 14:24:00 -0700
Subject: [PATCH 2/2] Removed temporaries.

---
 flang/cmake/modules/FlangCommon.cmake | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/flang/cmake/modules/FlangCommon.cmake b/flang/cmake/modules/FlangCommon.cmake
index e45f0afcc4576..77e93e3abbd0a 100644
--- a/flang/cmake/modules/FlangCommon.cmake
+++ b/flang/cmake/modules/FlangCommon.cmake
@@ -47,10 +47,8 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
     # Prepare CMAKE_CXX_FLAGS so that they can be passed to execute_process
     # as separate flags.
     separate_arguments(flags UNIX_COMMAND "${CMAKE_CXX_FLAGS}")
-    set(hash_flag "-###")
-    set(command ${CMAKE_CXX_COMPILER} ${flags} ${hash_flag} -v)
     execute_process(
-      COMMAND ${command}
+      COMMAND "${CMAKE_CXX_COMPILER}" ${flags} -v "-###"
       ERROR_FILE "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result"
     )
     file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result" _errorresult)



More information about the llvm-commits mailing list