Skip to content

Commit 1a795c7

Browse files
committed
[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.
1 parent f12e0c9 commit 1a795c7

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

flang/include/flang/Common/api-attrs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,15 @@
178178
#define RT_DEVICE_NOINLINE_HOST_INLINE inline
179179
#endif
180180

181+
/* RT_OPTNONE_ATTR allows disabling optimizations per function. */
182+
#if __has_attribute(optimize)
183+
/* GCC style. */
184+
#define RT_OPTNONE_ATTR __attribute__((optimize("O0")))
185+
#elif __has_attribute(optnone)
186+
/* Clang style. */
187+
#define RT_OPTNONE_ATTR __attribute__((optnone))
188+
#else
189+
#define RT_OPTNONE_ATTR
190+
#endif
191+
181192
#endif /* !FORTRAN_RUNTIME_API_ATTRS_H_ */

flang/runtime/stop.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,20 @@ void RTNAME(PauseStatementText)(const char *code, std::size_t length) {
157157
std::exit(status);
158158
}
159159

160-
static void PrintBacktrace() {
160+
static RT_NOINLINE_ATTR void PrintBacktrace() {
161161
#ifdef HAVE_BACKTRACE
162162
// TODO: Need to parse DWARF information to print function line numbers
163163
constexpr int MAX_CALL_STACK{999};
164164
void *buffer[MAX_CALL_STACK];
165165
int nptrs{(int)backtrace(buffer, MAX_CALL_STACK)};
166166

167167
if (char **symbols{backtrace_symbols(buffer, nptrs)}) {
168-
for (int i = 0; i < nptrs; i++) {
169-
Fortran::runtime::Terminator{}.PrintCrashArgs("#%d %s\n", i, symbols[i]);
168+
// Skip the PrintBacktrace() frame, as it is just a utility.
169+
// It makes sense to start printing the backtrace
170+
// from Abort() or backtrace().
171+
for (int i = 1; i < nptrs; i++) {
172+
Fortran::runtime::Terminator{}.PrintCrashArgs(
173+
"#%d %s\n", i - 1, symbols[i]);
170174
}
171175
free(symbols);
172176
}
@@ -179,14 +183,14 @@ static void PrintBacktrace() {
179183
#endif
180184
}
181185

182-
[[noreturn]] void RTNAME(Abort)() {
186+
[[noreturn]] RT_OPTNONE_ATTR void RTNAME(Abort)() {
183187
#ifdef HAVE_BACKTRACE
184188
PrintBacktrace();
185189
#endif
186190
std::abort();
187191
}
188192

189-
void FORTRAN_PROCEDURE_NAME(backtrace)() { PrintBacktrace(); }
193+
RT_OPTNONE_ATTR void FORTRAN_PROCEDURE_NAME(backtrace)() { PrintBacktrace(); }
190194

191195
[[noreturn]] void RTNAME(ReportFatalUserError)(
192196
const char *message, const char *source, int line) {

0 commit comments

Comments
 (0)