Skip to content

[flang][runtime] Disable optimization for traceback related functions. #124172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions flang/include/flang/Common/api-attrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
14 changes: 9 additions & 5 deletions flang/runtime/stop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,20 @@ 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};
void *buffer[MAX_CALL_STACK];
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);
}
Expand All @@ -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) {
Expand Down
Loading