[flang-commits] [flang] [flang][runtime] Disable optimization for traceback related functions. (PR #124172)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Thu Jan 23 10:34:05 PST 2025


https://github.com/vzakhari created https://github.com/llvm/llvm-project/pull/124172

The backtrace may at least print the backtrace name in the call stack,
but this does not happen with the release builds of the runtime.
Surprisingly, specifying "no-omit-frame-pointer" did not work
with GCC, so I decided to fall back to -O0 for these functions.


>From 1a795c78cdda475ab6ff2799008d3212111abf88 Mon Sep 17 00:00:00 2001
From: Slava Zakharin <szakharin at nvidia.com>
Date: Thu, 23 Jan 2025 10:06:32 -0800
Subject: [PATCH] [flang][runtime] Disable optimization for traceback related
 functions.

The backtrace may at least print the backtrace name in the call stack,
but this does not happen with the release builds of the runtime.
Surprisingly, specifying "no-omit-frame-pointer" did not work
with GCC, so I decided to fall back to -O0 for these functions.
---
 flang/include/flang/Common/api-attrs.h | 11 +++++++++++
 flang/runtime/stop.cpp                 | 14 +++++++++-----
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/flang/include/flang/Common/api-attrs.h b/flang/include/flang/Common/api-attrs.h
index d73e60996bc81f..1ee91ca8e0d9d3 100644
--- a/flang/include/flang/Common/api-attrs.h
+++ b/flang/include/flang/Common/api-attrs.h
@@ -178,4 +178,15 @@
 #define RT_DEVICE_NOINLINE_HOST_INLINE inline
 #endif
 
+/* RT_OPTNONE_ATTR allows disabling optimizations per function. */
+#if __has_attribute(optimize)
+/* GCC style. */
+#define RT_OPTNONE_ATTR __attribute__((optimize("O0")))
+#elif __has_attribute(optnone)
+/* Clang style. */
+#define RT_OPTNONE_ATTR __attribute__((optnone))
+#else
+#define RT_OPTNONE_ATTR
+#endif
+
 #endif /* !FORTRAN_RUNTIME_API_ATTRS_H_ */
diff --git a/flang/runtime/stop.cpp b/flang/runtime/stop.cpp
index a7be8a082e026b..f8c180e0aaffa1 100644
--- a/flang/runtime/stop.cpp
+++ b/flang/runtime/stop.cpp
@@ -157,7 +157,7 @@ void RTNAME(PauseStatementText)(const char *code, std::size_t length) {
   std::exit(status);
 }
 
-static void PrintBacktrace() {
+static RT_NOINLINE_ATTR void PrintBacktrace() {
 #ifdef HAVE_BACKTRACE
   // TODO: Need to parse DWARF information to print function line numbers
   constexpr int MAX_CALL_STACK{999};
@@ -165,8 +165,12 @@ static void PrintBacktrace() {
   int nptrs{(int)backtrace(buffer, MAX_CALL_STACK)};
 
   if (char **symbols{backtrace_symbols(buffer, nptrs)}) {
-    for (int i = 0; i < nptrs; i++) {
-      Fortran::runtime::Terminator{}.PrintCrashArgs("#%d %s\n", i, symbols[i]);
+    // Skip the PrintBacktrace() frame, as it is just a utility.
+    // It makes sense to start printing the backtrace
+    // from Abort() or backtrace().
+    for (int i = 1; i < nptrs; i++) {
+      Fortran::runtime::Terminator{}.PrintCrashArgs(
+          "#%d %s\n", i - 1, symbols[i]);
     }
     free(symbols);
   }
@@ -179,14 +183,14 @@ static void PrintBacktrace() {
 #endif
 }
 
-[[noreturn]] void RTNAME(Abort)() {
+[[noreturn]] RT_OPTNONE_ATTR void RTNAME(Abort)() {
 #ifdef HAVE_BACKTRACE
   PrintBacktrace();
 #endif
   std::abort();
 }
 
-void FORTRAN_PROCEDURE_NAME(backtrace)() { PrintBacktrace(); }
+RT_OPTNONE_ATTR void FORTRAN_PROCEDURE_NAME(backtrace)() { PrintBacktrace(); }
 
 [[noreturn]] void RTNAME(ReportFatalUserError)(
     const char *message, const char *source, int line) {



More information about the flang-commits mailing list