[flang] [llvm] [flang-rt][device] Enable MapException on the device with error message (PR #171705)
Valentin Clement バレンタイン クレメン via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 10 13:13:51 PST 2025
https://github.com/clementval created https://github.com/llvm/llvm-project/pull/171705
As it is done in `flang-rt/lib/runtime/edit-input.cpp`, emit a runtime error message when trying to raise IEEE expiation on the device. `MapException` and `feraiseexcept` are used in the lowering of the nearest intrinsic even on the device.
>From d94ed440892fdbfecf161060ae46053009c07f67 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Wed, 10 Dec 2025 13:11:04 -0800
Subject: [PATCH] [flang-rt][device] Enable MapException on the device with
error message
---
flang-rt/lib/runtime/CMakeLists.txt | 1 +
flang-rt/lib/runtime/exceptions.cpp | 12 ++++++++++--
flang/include/flang/Runtime/exceptions.h | 4 ++--
3 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/flang-rt/lib/runtime/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt
index 4be679d1ca8ac..0cea6c2d52652 100644
--- a/flang-rt/lib/runtime/CMakeLists.txt
+++ b/flang-rt/lib/runtime/CMakeLists.txt
@@ -57,6 +57,7 @@ set(supported_sources
edit-input.cpp
edit-output.cpp
environment.cpp
+ exceptions.cpp
external-unit.cpp
extrema.cpp
file.cpp
diff --git a/flang-rt/lib/runtime/exceptions.cpp b/flang-rt/lib/runtime/exceptions.cpp
index e5ba06c92ca90..fc5021fdb9036 100644
--- a/flang-rt/lib/runtime/exceptions.cpp
+++ b/flang-rt/lib/runtime/exceptions.cpp
@@ -49,9 +49,14 @@ extern "C" {
// Map a set of Fortran ieee_arithmetic module exceptions to a libm fenv.h
// excepts value.
-uint32_t RTNAME(MapException)(uint32_t excepts) {
+uint32_t RTDEF(MapException)(uint32_t excepts) {
Terminator terminator{__FILE__, __LINE__};
+#if defined(RT_DEVICE_COMPILATION)
+ terminator.Crash(
+ "not implemented yet: raising IEEE FP exception in device code: %d",
+ excepts);
+#else
static constexpr uint32_t v{FE_INVALID};
static constexpr uint32_t s{__FE_DENORM};
static constexpr uint32_t z{FE_DIVBYZERO};
@@ -74,6 +79,7 @@ uint32_t RTNAME(MapException)(uint32_t excepts) {
}
uint32_t except_value = map[excepts];
return except_value;
+#endif
}
// The following exception processing routines have a libm call component,
@@ -87,11 +93,13 @@ void RTNAME(feclearexcept)(uint32_t excepts) {
_mm_setcsr(_mm_getcsr() & ~(excepts & _MM_EXCEPT_MASK));
#endif
}
-void RTNAME(feraiseexcept)(uint32_t excepts) {
+void RTDEF(feraiseexcept)(uint32_t excepts) {
+#if !defined(RT_DEVICE_COMPILATION)
feraiseexcept(excepts);
#if defined(_MM_EXCEPT_DENORM)
_mm_setcsr(_mm_getcsr() | (excepts & _MM_EXCEPT_MASK));
#endif
+#endif
}
uint32_t RTNAME(fetestexcept)(uint32_t excepts) {
#if defined(_MM_EXCEPT_DENORM)
diff --git a/flang/include/flang/Runtime/exceptions.h b/flang/include/flang/Runtime/exceptions.h
index c76376e36ed6d..2497a48402233 100644
--- a/flang/include/flang/Runtime/exceptions.h
+++ b/flang/include/flang/Runtime/exceptions.h
@@ -24,12 +24,12 @@ extern "C" {
// Map a set of IEEE_FLAG_TYPE exception values to a libm fenv.h excepts value.
// This mapping is done at runtime to support cross compilation.
-std::uint32_t RTNAME(MapException)(std::uint32_t excepts);
+std::uint32_t RTDECL(MapException)(std::uint32_t excepts);
// Exception processing functions that call the corresponding libm functions,
// and also include support for denormal exceptions where available.
void RTNAME(feclearexcept)(std::uint32_t excepts);
-void RTNAME(feraiseexcept)(std::uint32_t excepts);
+void RTDECL(feraiseexcept)(std::uint32_t excepts);
std::uint32_t RTNAME(fetestexcept)(std::uint32_t excepts);
void RTNAME(fedisableexcept)(std::uint32_t excepts);
void RTNAME(feenableexcept)(std::uint32_t excepts);
More information about the llvm-commits
mailing list