[flang] [llvm] [flang-rt] Fixed warnings and miscompilations in CUDA build. (PR #134470)
Slava Zakharin via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 4 18:49:57 PDT 2025
https://github.com/vzakhari created https://github.com/llvm/llvm-project/pull/134470
* DescribeIEEESignaledExceptions() is unused on the device - warning.
* StopStatementText() could return while marked noreturn - warning.
* Including cuda/std/complex only in the device compilation
may cause nvcc to try to register variables in `cuda` namespace,
while they are not defined in the host compilation - error.
I decided to include cuda/std/complex always under RT_USE_LIBCUDACXX.
>From c7835e2a7559c03223fe2a332c9faaf85f3b841d Mon Sep 17 00:00:00 2001
From: Slava Zakharin <szakharin at nvidia.com>
Date: Fri, 4 Apr 2025 18:34:30 -0700
Subject: [PATCH] [flang-rt] Fixed warnings and miscompilations in CUDA build.
* DescribeIEEESignaledExceptions() is unused on the device - warning.
* StopStatementText() could return while marked noreturn - warning.
* Including cuda/std/complex only in the device compilation
may cause nvcc to try to register variables in `cuda` namespace,
while they are not defined in the host compilation - error.
I decided to include cuda/std/complex always under RT_USE_LIBCUDACXX.
---
.../include/flang-rt/runtime/terminator.h | 15 ++++++++++++
flang-rt/lib/runtime/stop.cpp | 24 +++----------------
flang-rt/lib/runtime/terminator.cpp | 10 +-------
flang/include/flang/Common/enum-class.h | 1 -
flang/include/flang/Runtime/complex.h | 5 +++-
5 files changed, 23 insertions(+), 32 deletions(-)
diff --git a/flang-rt/include/flang-rt/runtime/terminator.h b/flang-rt/include/flang-rt/runtime/terminator.h
index 4815f0674c849..a20f50ee05a8a 100644
--- a/flang-rt/include/flang-rt/runtime/terminator.h
+++ b/flang-rt/include/flang-rt/runtime/terminator.h
@@ -115,6 +115,21 @@ class Terminator {
RT_API_ATTRS void NotifyOtherImagesOfNormalEnd();
RT_API_ATTRS void NotifyOtherImagesOfFailImageStatement();
RT_API_ATTRS void NotifyOtherImagesOfErrorTermination();
+
+#if defined(RT_DEVICE_COMPILATION)
+/// Trap the execution on the device.
+[[noreturn]] void RT_API_ATTRS DeviceTrap() {
+#if defined(__CUDACC__)
+ // NVCC supports __trap().
+ __trap();
+#elif defined(__clang__)
+ // Clang supports __builtin_trap().
+ __builtin_trap();
+#else
+#error "unsupported compiler"
+#endif
+}
+#endif
} // namespace Fortran::runtime
namespace Fortran::runtime::io {
diff --git a/flang-rt/lib/runtime/stop.cpp b/flang-rt/lib/runtime/stop.cpp
index c4d25813d5e93..66b3c1d2a9b4e 100644
--- a/flang-rt/lib/runtime/stop.cpp
+++ b/flang-rt/lib/runtime/stop.cpp
@@ -23,7 +23,7 @@
extern "C" {
-static void DescribeIEEESignaledExceptions() {
+[[maybe_unused]] static void DescribeIEEESignaledExceptions() {
#ifdef fetestexcept // a macro in some environments; omit std::
auto excepts{fetestexcept(FE_ALL_EXCEPT)};
#else
@@ -82,15 +82,7 @@ static void CloseAllExternalUnits(const char *why) {
}
std::printf("\n");
}
-#if defined(__CUDACC__)
- // NVCC supports __trap().
- __trap();
-#elif defined(__clang__)
- // Clang supports __builtin_trap().
- __builtin_trap();
-#else
-#error "unsupported compiler"
-#endif
+ Fortran::runtime::DeviceTrap();
#else
CloseAllExternalUnits("STOP statement");
if (Fortran::runtime::executionEnvironment.noStopMessage && code == 0) {
@@ -119,17 +111,7 @@ static void CloseAllExternalUnits(const char *why) {
"Fortran %s: %s\n", isErrorStop ? "ERROR STOP" : "STOP", code);
}
}
- if (isErrorStop) {
-#if defined(__CUDACC__)
- // NVCC supports __trap().
- __trap();
-#elif defined(__clang__)
- // Clang supports __builtin_trap().
- __builtin_trap();
-#else
-#error "unsupported compiler"
-#endif
- }
+ Fortran::runtime::DeviceTrap();
#else
CloseAllExternalUnits("STOP statement");
if (!quiet) {
diff --git a/flang-rt/lib/runtime/terminator.cpp b/flang-rt/lib/runtime/terminator.cpp
index 8a57ba06b1304..97ca824342b15 100644
--- a/flang-rt/lib/runtime/terminator.cpp
+++ b/flang-rt/lib/runtime/terminator.cpp
@@ -75,15 +75,7 @@ RT_API_ATTRS void Terminator::CrashHeader() const {
#endif
NotifyOtherImagesOfErrorTermination();
#if defined(RT_DEVICE_COMPILATION)
-#if defined(__CUDACC__)
- // NVCC supports __trap().
- __trap();
-#elif defined(__clang__)
- // Clang supports __builtin_trap().
- __builtin_trap();
-#else
-#error "unsupported compiler"
-#endif
+ DeviceTrap();
#else
std::abort();
#endif
diff --git a/flang/include/flang/Common/enum-class.h b/flang/include/flang/Common/enum-class.h
index 8c254c8bc6a70..41575d45091a8 100644
--- a/flang/include/flang/Common/enum-class.h
+++ b/flang/include/flang/Common/enum-class.h
@@ -17,7 +17,6 @@
#ifndef FORTRAN_COMMON_ENUM_CLASS_H_
#define FORTRAN_COMMON_ENUM_CLASS_H_
-#include "flang/Common/variant.h"
#include <array>
#include <string>
diff --git a/flang/include/flang/Runtime/complex.h b/flang/include/flang/Runtime/complex.h
index be477d244155b..774038101e529 100644
--- a/flang/include/flang/Runtime/complex.h
+++ b/flang/include/flang/Runtime/complex.h
@@ -16,8 +16,11 @@
#include "flang/Common/api-attrs.h"
-#if RT_USE_LIBCUDACXX && defined(RT_DEVICE_COMPILATION)
+#if RT_USE_LIBCUDACXX
#include <cuda/std/complex>
+#endif
+
+#if RT_USE_LIBCUDACXX && defined(RT_DEVICE_COMPILATION)
namespace Fortran::runtime::rtcmplx {
using cuda::std::complex;
using cuda::std::conj;
More information about the llvm-commits
mailing list