Skip to content

[Flang][Runtime] Handle missing definitions in <cfenv> #101242

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 2 commits into from
Aug 21, 2024
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/runtime/edit-input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,18 +507,29 @@ static RT_API_ATTRS void RaiseFPExceptions(
#define RAISE std::feraiseexcept
#endif
#endif // !defined(RT_DEVICE_COMPILATION)

// Some environment (e.g. emscripten, musl) don't define FE_OVERFLOW as allowed
// by c99 (but not c++11) :-/
#if defined(FE_OVERFLOW) || defined(RT_DEVICE_COMPILATION)
if (flags & decimal::ConversionResultFlags::Overflow) {
RAISE(FE_OVERFLOW);
}
#endif
#if defined(FE_UNDERFLOW) || defined(RT_DEVICE_COMPILATION)
if (flags & decimal::ConversionResultFlags::Underflow) {
RAISE(FE_UNDERFLOW);
}
#endif
#if defined(FE_INEXACT) || defined(RT_DEVICE_COMPILATION)
if (flags & decimal::ConversionResultFlags::Inexact) {
RAISE(FE_INEXACT);
}
#endif
#if defined(FE_INVALID) || defined(RT_DEVICE_COMPILATION)
if (flags & decimal::ConversionResultFlags::Invalid) {
RAISE(FE_INVALID);
}
#endif
#undef RAISE
}

Expand Down
24 changes: 23 additions & 1 deletion flang/runtime/exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,26 @@
#include "terminator.h"
#include <cfenv>

// When not supported, these macro are undefined in cfenv.h,
// set them to zero in that case.
#ifndef FE_INVALID
#define FE_INVALID 0
#endif
Comment on lines +17 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think those are conforming definitions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved to crashing if we want to report a failure that's not supported by the runtime because that shouldn't happen. Thanks for the insight.

#ifndef __FE_DENORM
#define __FE_DENORM 0 // denorm is nonstandard
#endif
#ifndef FE_DIVBYZERO
#define FE_DIVBYZERO 0
#endif
#ifndef FE_OVERFLOW
#define FE_OVERFLOW 0
#endif
#ifndef FE_UNDERFLOW
#define FE_UNDERFLOW 0
#endif
#ifndef FE_INEXACT
#define FE_INEXACT 0
#endif

namespace Fortran::runtime {

Expand Down Expand Up @@ -45,7 +62,12 @@ uint32_t RTNAME(MapException)(uint32_t excepts) {
if (excepts == 0 || excepts >= mapSize) {
terminator.Crash("Invalid excepts value: %d", excepts);
}
return map[excepts];
uint32_t except_value = map[excepts];
if (except_value == 0) {
terminator.Crash(
"Excepts value %d not supported by flang runtime", excepts);
}
return except_value;
}

// Verify that the size of ieee_modes_type and ieee_status_type objects from
Expand Down
10 changes: 10 additions & 0 deletions flang/runtime/stop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,31 @@ static void DescribeIEEESignaledExceptions() {
#endif
if (excepts) {
std::fputs("IEEE arithmetic exceptions signaled:", stderr);
#ifdef FE_DIVBYZERO
if (excepts & FE_DIVBYZERO) {
std::fputs(" DIVBYZERO", stderr);
}
#endif
#ifdef FE_INEXACT
if (excepts & FE_INEXACT) {
std::fputs(" INEXACT", stderr);
}
#endif
#ifdef FE_INVALID
if (excepts & FE_INVALID) {
std::fputs(" INVALID", stderr);
}
#endif
#ifdef FE_OVERFLOW
if (excepts & FE_OVERFLOW) {
std::fputs(" OVERFLOW", stderr);
}
#endif
#ifdef FE_UNDERFLOW
if (excepts & FE_UNDERFLOW) {
std::fputs(" UNDERFLOW", stderr);
}
#endif
std::fputc('\n', stderr);
}
}
Expand Down
Loading