Skip to content

Commit 755016a

Browse files
authored
[flang-rt] Fixed warnings and miscompilations in CUDA build. (#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.
1 parent 337a4d5 commit 755016a

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed

flang-rt/include/flang-rt/runtime/terminator.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ class Terminator {
115115
RT_API_ATTRS void NotifyOtherImagesOfNormalEnd();
116116
RT_API_ATTRS void NotifyOtherImagesOfFailImageStatement();
117117
RT_API_ATTRS void NotifyOtherImagesOfErrorTermination();
118+
119+
#if defined(RT_DEVICE_COMPILATION)
120+
/// Trap the execution on the device.
121+
[[noreturn]] void RT_API_ATTRS DeviceTrap() {
122+
#if defined(__CUDACC__)
123+
// NVCC supports __trap().
124+
__trap();
125+
#elif defined(__clang__)
126+
// Clang supports __builtin_trap().
127+
__builtin_trap();
128+
#else
129+
#error "unsupported compiler"
130+
#endif
131+
}
132+
#endif
118133
} // namespace Fortran::runtime
119134

120135
namespace Fortran::runtime::io {

flang-rt/lib/runtime/stop.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
extern "C" {
2525

26-
static void DescribeIEEESignaledExceptions() {
26+
[[maybe_unused]] static void DescribeIEEESignaledExceptions() {
2727
#ifdef fetestexcept // a macro in some environments; omit std::
2828
auto excepts{fetestexcept(FE_ALL_EXCEPT)};
2929
#else
@@ -82,15 +82,7 @@ static void CloseAllExternalUnits(const char *why) {
8282
}
8383
std::printf("\n");
8484
}
85-
#if defined(__CUDACC__)
86-
// NVCC supports __trap().
87-
__trap();
88-
#elif defined(__clang__)
89-
// Clang supports __builtin_trap().
90-
__builtin_trap();
91-
#else
92-
#error "unsupported compiler"
93-
#endif
85+
Fortran::runtime::DeviceTrap();
9486
#else
9587
CloseAllExternalUnits("STOP statement");
9688
if (Fortran::runtime::executionEnvironment.noStopMessage && code == 0) {
@@ -119,17 +111,7 @@ static void CloseAllExternalUnits(const char *why) {
119111
"Fortran %s: %s\n", isErrorStop ? "ERROR STOP" : "STOP", code);
120112
}
121113
}
122-
if (isErrorStop) {
123-
#if defined(__CUDACC__)
124-
// NVCC supports __trap().
125-
__trap();
126-
#elif defined(__clang__)
127-
// Clang supports __builtin_trap().
128-
__builtin_trap();
129-
#else
130-
#error "unsupported compiler"
131-
#endif
132-
}
114+
Fortran::runtime::DeviceTrap();
133115
#else
134116
CloseAllExternalUnits("STOP statement");
135117
if (!quiet) {

flang-rt/lib/runtime/terminator.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,7 @@ RT_API_ATTRS void Terminator::CrashHeader() const {
7575
#endif
7676
NotifyOtherImagesOfErrorTermination();
7777
#if defined(RT_DEVICE_COMPILATION)
78-
#if defined(__CUDACC__)
79-
// NVCC supports __trap().
80-
__trap();
81-
#elif defined(__clang__)
82-
// Clang supports __builtin_trap().
83-
__builtin_trap();
84-
#else
85-
#error "unsupported compiler"
86-
#endif
78+
DeviceTrap();
8779
#else
8880
std::abort();
8981
#endif

flang/include/flang/Common/enum-class.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#ifndef FORTRAN_COMMON_ENUM_CLASS_H_
1818
#define FORTRAN_COMMON_ENUM_CLASS_H_
1919

20-
#include "flang/Common/variant.h"
2120
#include <array>
2221
#include <string>
2322

flang/include/flang/Runtime/complex.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
#include "flang/Common/api-attrs.h"
1818

19-
#if RT_USE_LIBCUDACXX && defined(RT_DEVICE_COMPILATION)
19+
#if RT_USE_LIBCUDACXX
2020
#include <cuda/std/complex>
21+
#endif
22+
23+
#if RT_USE_LIBCUDACXX && defined(RT_DEVICE_COMPILATION)
2124
namespace Fortran::runtime::rtcmplx {
2225
using cuda::std::complex;
2326
using cuda::std::conj;

0 commit comments

Comments
 (0)