[Openmp-commits] [flang] [llvm] [openmp] [Flang-RT] Fix GCC 15.1 Fortran Runtime libstdc++ Undefined Symbols (PR #157385)
Patrick Simmons via Openmp-commits
openmp-commits at lists.llvm.org
Mon Oct 6 10:04:13 PDT 2025
https://github.com/linuxrocks123 updated https://github.com/llvm/llvm-project/pull/157385
>From ca5ca0042663a4bc6e413c7ae7a2847c2c8bdcfa Mon Sep 17 00:00:00 2001
From: Patrick Simmons <psimmons at pensando.io>
Date: Sun, 7 Sep 2025 23:44:02 -0500
Subject: [PATCH 1/9] Remaining work necessary for GCC 15
---
flang-rt/cmake/modules/AddFlangRT.cmake | 8 ++++++++
openmp/runtime/src/CMakeLists.txt | 2 +-
2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index ab383bcbe2cdf..bcd60c714391f 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -195,6 +195,14 @@ function (add_flangrt_library name)
# Use same stem name for .a and .so. Common in UNIX environments.
# Not possible in Windows environments.
set_target_properties(${tgtname} PROPERTIES OUTPUT_NAME "${name}")
+
+ # Must use minimum optimization level of -O2 to prevent dependency on libstdc++
+ string(REPLACE "-O0" "-O2" CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS)
+ string(REPLACE "-O1" "-O2" CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS)
+ string(FIND "-O" CMAKE_CXX_FLAGS opt_idx)
+ if (opt_idx EQUAL -1)
+ target_compile_options(${tgtname} PRIVATE -O2)
+ endif ()
endif ()
if (ARG_INSTALL_WITH_TOOLCHAIN)
diff --git a/openmp/runtime/src/CMakeLists.txt b/openmp/runtime/src/CMakeLists.txt
index 6ac047a833fe5..6f84a753c2788 100644
--- a/openmp/runtime/src/CMakeLists.txt
+++ b/openmp/runtime/src/CMakeLists.txt
@@ -170,7 +170,7 @@ endif()
# avoid an unwanted dependency on libstdc++.so.
add_compile_definitions(_GLIBCXX_NO_ASSERTIONS)
if(NOT WIN32)
- add_definitions(-U_GLIBCXX_ASSERTIONS)
+ add_definitions(-U_GLIBCXX_ASSERTIONS -D_GLIBCXX_NO_ASSERTIONS)
endif()
# Add the OpenMP library
>From efd4dee95558ecd4d475096d71e13400df4e054b Mon Sep 17 00:00:00 2001
From: Patrick Simmons <psimmons at pensando.io>
Date: Mon, 8 Sep 2025 21:52:12 -0500
Subject: [PATCH 2/9] Fixed
---
openmp/runtime/src/CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/openmp/runtime/src/CMakeLists.txt b/openmp/runtime/src/CMakeLists.txt
index 6f84a753c2788..6ac047a833fe5 100644
--- a/openmp/runtime/src/CMakeLists.txt
+++ b/openmp/runtime/src/CMakeLists.txt
@@ -170,7 +170,7 @@ endif()
# avoid an unwanted dependency on libstdc++.so.
add_compile_definitions(_GLIBCXX_NO_ASSERTIONS)
if(NOT WIN32)
- add_definitions(-U_GLIBCXX_ASSERTIONS -D_GLIBCXX_NO_ASSERTIONS)
+ add_definitions(-U_GLIBCXX_ASSERTIONS)
endif()
# Add the OpenMP library
>From c16cad3338dead12f46ea9ce382c45c9136e246e Mon Sep 17 00:00:00 2001
From: Patrick Simmons <psimmons at pensando.io>
Date: Thu, 11 Sep 2025 06:00:02 -0500
Subject: [PATCH 3/9] Make suggested change to targets loop
---
flang-rt/cmake/modules/AddFlangRT.cmake | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index bcd60c714391f..a12bb5553bae3 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -190,19 +190,22 @@ function (add_flangrt_library name)
endif ()
endif ()
+ # Must use minimum optimization level of -O2 to prevent dependency on libstdc++
+ foreach (tgtname IN LISTS srctargets)
+ string(REPLACE "-O0" "-O2" CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS)
+ string(REPLACE "-O1" "-O2" CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS)
+ string(FIND "-O" CMAKE_CXX_FLAGS opt_idx)
+ if (opt_idx EQUAL -1)
+ target_compile_options(${tgtname} PRIVATE -O2)
+ endif ()
+ endforeach ()
+
foreach (tgtname IN LISTS libtargets)
if (NOT WIN32)
# Use same stem name for .a and .so. Common in UNIX environments.
# Not possible in Windows environments.
set_target_properties(${tgtname} PROPERTIES OUTPUT_NAME "${name}")
- # Must use minimum optimization level of -O2 to prevent dependency on libstdc++
- string(REPLACE "-O0" "-O2" CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS)
- string(REPLACE "-O1" "-O2" CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS)
- string(FIND "-O" CMAKE_CXX_FLAGS opt_idx)
- if (opt_idx EQUAL -1)
- target_compile_options(${tgtname} PRIVATE -O2)
- endif ()
endif ()
if (ARG_INSTALL_WITH_TOOLCHAIN)
>From a2a496f3ccbf769b9693c77e744031aa4b14c6f7 Mon Sep 17 00:00:00 2001
From: Patrick Simmons <psimmons at pensando.io>
Date: Fri, 19 Sep 2025 05:58:01 -0500
Subject: [PATCH 4/9] Approach 3
---
flang-rt/cmake/modules/AddFlangRT.cmake | 11 -----------
flang/include/flang/Common/variant.h | 3 +++
2 files changed, 3 insertions(+), 11 deletions(-)
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index a12bb5553bae3..ab383bcbe2cdf 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -190,22 +190,11 @@ function (add_flangrt_library name)
endif ()
endif ()
- # Must use minimum optimization level of -O2 to prevent dependency on libstdc++
- foreach (tgtname IN LISTS srctargets)
- string(REPLACE "-O0" "-O2" CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS)
- string(REPLACE "-O1" "-O2" CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS)
- string(FIND "-O" CMAKE_CXX_FLAGS opt_idx)
- if (opt_idx EQUAL -1)
- target_compile_options(${tgtname} PRIVATE -O2)
- endif ()
- endforeach ()
-
foreach (tgtname IN LISTS libtargets)
if (NOT WIN32)
# Use same stem name for .a and .so. Common in UNIX environments.
# Not possible in Windows environments.
set_target_properties(${tgtname} PROPERTIES OUTPUT_NAME "${name}")
-
endif ()
if (ARG_INSTALL_WITH_TOOLCHAIN)
diff --git a/flang/include/flang/Common/variant.h b/flang/include/flang/Common/variant.h
index 1af85876afac0..ac9ca4234c432 100644
--- a/flang/include/flang/Common/variant.h
+++ b/flang/include/flang/Common/variant.h
@@ -24,6 +24,9 @@ using cuda::std::variant_size_v;
using cuda::std::visit;
} // namespace std
#else // !RT_USE_LIBCUDACXX
+#include <initializer_list>
+#undef _GLIBCXX_THROW_OR_ABORT
+#define _GLIBCXX_THROW_OR_ABORT(_EXC) (__builtin_abort())
#include <variant>
#endif // !RT_USE_LIBCUDACXX
>From e9c914093ad1c9eaf87e7ea0de71c5eb738dcce4 Mon Sep 17 00:00:00 2001
From: Patrick Simmons <psimmons at pensando.io>
Date: Tue, 30 Sep 2025 17:30:44 -0500
Subject: [PATCH 5/9] Add documentation
---
flang/include/flang/Common/variant.h | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/flang/include/flang/Common/variant.h b/flang/include/flang/Common/variant.h
index ac9ca4234c432..423285f0ed5a4 100644
--- a/flang/include/flang/Common/variant.h
+++ b/flang/include/flang/Common/variant.h
@@ -24,9 +24,17 @@ using cuda::std::variant_size_v;
using cuda::std::visit;
} // namespace std
#else // !RT_USE_LIBCUDACXX
+
+// initializer_list is included to load bits/c++config, which can't be included
+// directly and which defines a macro we need to redefine.
#include <initializer_list>
+
+// Redefine problematic abort macro to avoid defining symbols related to
+// std::exception. std::exception is defined in the C++ runtime library, which
+// we do not want the Fortran runtime library to depend on.
#undef _GLIBCXX_THROW_OR_ABORT
#define _GLIBCXX_THROW_OR_ABORT(_EXC) (__builtin_abort())
+
#include <variant>
#endif // !RT_USE_LIBCUDACXX
>From f1f3834217e0b1b891de1730e6e3a006aa5e1264 Mon Sep 17 00:00:00 2001
From: Patrick Simmons <linuxrocks123 at netscape.net>
Date: Mon, 6 Oct 2025 11:26:24 -0500
Subject: [PATCH 6/9] Update flang/include/flang/Common/variant.h
Co-authored-by: Michael Kruse <github at meinersbur.de>
---
flang/include/flang/Common/variant.h | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/flang/include/flang/Common/variant.h b/flang/include/flang/Common/variant.h
index 423285f0ed5a4..9c0a529d6281d 100644
--- a/flang/include/flang/Common/variant.h
+++ b/flang/include/flang/Common/variant.h
@@ -29,9 +29,7 @@ using cuda::std::visit;
// directly and which defines a macro we need to redefine.
#include <initializer_list>
-// Redefine problematic abort macro to avoid defining symbols related to
-// std::exception. std::exception is defined in the C++ runtime library, which
-// we do not want the Fortran runtime library to depend on.
+// The macro _GLIBCXX_THROW_OR_ABORT is used by libstdc++ to not throw exceptions in -fno-exceptions mode, but immediatly kill the program. Since libstdc++ 15.1 the macro uses (void)(_EXC) after calling abort() to silence compiler warnings of an parameter. In its use in <variant>, _EXC is the construction of `std::bad_variant_access`. In non-optimized builds, some compilers including Clang will emit a call to that constructor. The constructor is implemented in libstdc++.a/.so which Flang-RT must not depend on (to avoid compatibility problems if a Fortran application itself has parts implemented in C++). Note that _GLIBCXX_THROW_OR_ABORT is not on the list of libstdc++'s documented user-configurable macros.
#undef _GLIBCXX_THROW_OR_ABORT
#define _GLIBCXX_THROW_OR_ABORT(_EXC) (__builtin_abort())
>From 920e9a2ebc145d01b582a66b25718c4dd5f40a61 Mon Sep 17 00:00:00 2001
From: Patrick Simmons <psimmons at pensando.io>
Date: Mon, 6 Oct 2025 11:29:54 -0500
Subject: [PATCH 7/9] reformat
---
flang/include/flang/Common/variant.h | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/flang/include/flang/Common/variant.h b/flang/include/flang/Common/variant.h
index 9c0a529d6281d..5808d070d9103 100644
--- a/flang/include/flang/Common/variant.h
+++ b/flang/include/flang/Common/variant.h
@@ -29,7 +29,16 @@ using cuda::std::visit;
// directly and which defines a macro we need to redefine.
#include <initializer_list>
-// The macro _GLIBCXX_THROW_OR_ABORT is used by libstdc++ to not throw exceptions in -fno-exceptions mode, but immediatly kill the program. Since libstdc++ 15.1 the macro uses (void)(_EXC) after calling abort() to silence compiler warnings of an parameter. In its use in <variant>, _EXC is the construction of `std::bad_variant_access`. In non-optimized builds, some compilers including Clang will emit a call to that constructor. The constructor is implemented in libstdc++.a/.so which Flang-RT must not depend on (to avoid compatibility problems if a Fortran application itself has parts implemented in C++). Note that _GLIBCXX_THROW_OR_ABORT is not on the list of libstdc++'s documented user-configurable macros.
+// The macro _GLIBCXX_THROW_OR_ABORT is used by libstdc++ to not throw
+// exceptions in -fno-exceptions mode, but immediatly kill the program. Since
+// libstdc++ 15.1 the macro uses (void)(_EXC) after calling abort() to silence
+// compiler warnings of an parameter. In its use in <variant>, _EXC is the
+// construction of `std::bad_variant_access`. In non-optimized builds, some
+// compilers including Clang will emit a call to that constructor. The
+// constructor is implemented in libstdc++.a/.so which Flang-RT must not depend
+// on (to avoid compatibility problems if a Fortran application itself has parts
+// implemented in C++). Note that _GLIBCXX_THROW_OR_ABORT is not on the list of
+// libstdc++'s documented user-configurable macros.
#undef _GLIBCXX_THROW_OR_ABORT
#define _GLIBCXX_THROW_OR_ABORT(_EXC) (__builtin_abort())
>From 20cbc5280e44f4b6917c9903e39277c4d9a4379a Mon Sep 17 00:00:00 2001
From: Patrick Simmons <psimmons at pensando.io>
Date: Mon, 6 Oct 2025 11:56:58 -0500
Subject: [PATCH 8/9] Try it a different way
---
flang-rt/cmake/modules/AddFlangRT.cmake | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index ab383bcbe2cdf..0308ccc4cc0a7 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -286,6 +286,12 @@ function (add_flangrt_library name)
# For Flang-RT's configured config.h to be found
target_include_directories(${tgtname} PRIVATE "${FLANG_RT_BINARY_DIR}")
+ # Define our own _GLIBCXX_THROW_OR_ABORT because libstdc++ headers
+ # reference std::exception in its definition, and we do not want
+ # to link against std::exception since doing that would link us to
+ # the C++ runtime.
+ target_compile_definitions(${tgtname} PUBLIC "_GLIBCXX_THROW_OR_ABORT=(__builtin_abort())")
+
# Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS
# build, to avoid an unwanted dependency on libstdc++/libc++.so.
target_compile_definitions(${tgtname} PUBLIC _GLIBCXX_NO_ASSERTIONS)
>From 6c228169e3e6c99655924e48610aeca05c2a151b Mon Sep 17 00:00:00 2001
From: Patrick Simmons <psimmons at pensando.io>
Date: Mon, 6 Oct 2025 11:57:59 -0500
Subject: [PATCH 9/9] Revert changes to variant.h
---
flang/include/flang/Common/variant.h | 18 ------------------
1 file changed, 18 deletions(-)
diff --git a/flang/include/flang/Common/variant.h b/flang/include/flang/Common/variant.h
index 5808d070d9103..1af85876afac0 100644
--- a/flang/include/flang/Common/variant.h
+++ b/flang/include/flang/Common/variant.h
@@ -24,24 +24,6 @@ using cuda::std::variant_size_v;
using cuda::std::visit;
} // namespace std
#else // !RT_USE_LIBCUDACXX
-
-// initializer_list is included to load bits/c++config, which can't be included
-// directly and which defines a macro we need to redefine.
-#include <initializer_list>
-
-// The macro _GLIBCXX_THROW_OR_ABORT is used by libstdc++ to not throw
-// exceptions in -fno-exceptions mode, but immediatly kill the program. Since
-// libstdc++ 15.1 the macro uses (void)(_EXC) after calling abort() to silence
-// compiler warnings of an parameter. In its use in <variant>, _EXC is the
-// construction of `std::bad_variant_access`. In non-optimized builds, some
-// compilers including Clang will emit a call to that constructor. The
-// constructor is implemented in libstdc++.a/.so which Flang-RT must not depend
-// on (to avoid compatibility problems if a Fortran application itself has parts
-// implemented in C++). Note that _GLIBCXX_THROW_OR_ABORT is not on the list of
-// libstdc++'s documented user-configurable macros.
-#undef _GLIBCXX_THROW_OR_ABORT
-#define _GLIBCXX_THROW_OR_ABORT(_EXC) (__builtin_abort())
-
#include <variant>
#endif // !RT_USE_LIBCUDACXX
More information about the Openmp-commits
mailing list