Skip to content

Commit ece3925

Browse files
authored
[flang-rt][device] Enable MapException on the device with error message (#171705)
As it is done in `flang-rt/lib/runtime/edit-input.cpp`, emit a runtime error message when trying to raise IEEE exception on the device. `MapException` and `feraiseexcept` are used in the lowering of the nearest intrinsic even on the device.
1 parent e61c2d4 commit ece3925

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

flang-rt/lib/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(supported_sources
3333
edit-input.cpp
3434
edit-output.cpp
3535
environment.cpp
36+
exceptions.cpp
3637
external-unit.cpp
3738
extrema.cpp
3839
file.cpp

flang-rt/lib/runtime/exceptions.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ extern "C" {
4949

5050
// Map a set of Fortran ieee_arithmetic module exceptions to a libm fenv.h
5151
// excepts value.
52-
uint32_t RTNAME(MapException)(uint32_t excepts) {
52+
uint32_t RTDEF(MapException)(uint32_t excepts) {
5353
Terminator terminator{__FILE__, __LINE__};
5454

55+
#if defined(RT_DEVICE_COMPILATION)
56+
terminator.Crash(
57+
"not implemented yet: raising IEEE FP exception in device code: %d",
58+
excepts);
59+
#else
5560
static constexpr uint32_t v{FE_INVALID};
5661
static constexpr uint32_t s{__FE_DENORM};
5762
static constexpr uint32_t z{FE_DIVBYZERO};
@@ -74,6 +79,7 @@ uint32_t RTNAME(MapException)(uint32_t excepts) {
7479
}
7580
uint32_t except_value = map[excepts];
7681
return except_value;
82+
#endif
7783
}
7884

7985
// The following exception processing routines have a libm call component,
@@ -87,11 +93,13 @@ void RTNAME(feclearexcept)(uint32_t excepts) {
8793
_mm_setcsr(_mm_getcsr() & ~(excepts & _MM_EXCEPT_MASK));
8894
#endif
8995
}
90-
void RTNAME(feraiseexcept)(uint32_t excepts) {
96+
void RTDEF(feraiseexcept)(uint32_t excepts) {
97+
#if !defined(RT_DEVICE_COMPILATION)
9198
feraiseexcept(excepts);
9299
#if defined(_MM_EXCEPT_DENORM)
93100
_mm_setcsr(_mm_getcsr() | (excepts & _MM_EXCEPT_MASK));
94101
#endif
102+
#endif
95103
}
96104
uint32_t RTNAME(fetestexcept)(uint32_t excepts) {
97105
#if defined(_MM_EXCEPT_DENORM)

flang/include/flang/Runtime/exceptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ extern "C" {
2424

2525
// Map a set of IEEE_FLAG_TYPE exception values to a libm fenv.h excepts value.
2626
// This mapping is done at runtime to support cross compilation.
27-
std::uint32_t RTNAME(MapException)(std::uint32_t excepts);
27+
std::uint32_t RTDECL(MapException)(std::uint32_t excepts);
2828

2929
// Exception processing functions that call the corresponding libm functions,
3030
// and also include support for denormal exceptions where available.
3131
void RTNAME(feclearexcept)(std::uint32_t excepts);
32-
void RTNAME(feraiseexcept)(std::uint32_t excepts);
32+
void RTDECL(feraiseexcept)(std::uint32_t excepts);
3333
std::uint32_t RTNAME(fetestexcept)(std::uint32_t excepts);
3434
void RTNAME(fedisableexcept)(std::uint32_t excepts);
3535
void RTNAME(feenableexcept)(std::uint32_t excepts);

0 commit comments

Comments
 (0)