Skip to content

Commit 01889de

Browse files
authored
[flang][device] Enable Stop functions on device build (#133803)
Update `StopStatement` and `StopStatementText` to be build for the device.
1 parent 75242a8 commit 01889de

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

flang-rt/lib/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ set(supported_sources
5757
pseudo-unit.cpp
5858
ragged.cpp
5959
stat.cpp
60+
stop.cpp
6061
sum.cpp
6162
support.cpp
6263
terminator.cpp

flang-rt/lib/runtime/stop.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,33 @@ static void CloseAllExternalUnits(const char *why) {
6565
Fortran::runtime::io::ExternalFileUnit::CloseAll(handler);
6666
}
6767

68-
[[noreturn]] void RTNAME(StopStatement)(
68+
[[noreturn]] RT_API_ATTRS void RTNAME(StopStatement)(
6969
int code, bool isErrorStop, bool quiet) {
70+
#if defined(RT_DEVICE_COMPILATION)
71+
if (Fortran::runtime::executionEnvironment.noStopMessage && code == 0) {
72+
quiet = true;
73+
}
74+
if (!quiet) {
75+
if (isErrorStop) {
76+
std::printf("Fortran ERROR STOP");
77+
} else {
78+
std::printf("Fortran STOP");
79+
}
80+
if (code != EXIT_SUCCESS) {
81+
std::printf(": code %d\n", code);
82+
}
83+
std::printf('\n');
84+
}
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
94+
#else
7095
CloseAllExternalUnits("STOP statement");
7196
if (Fortran::runtime::executionEnvironment.noStopMessage && code == 0) {
7297
quiet = true;
@@ -80,10 +105,32 @@ static void CloseAllExternalUnits(const char *why) {
80105
DescribeIEEESignaledExceptions();
81106
}
82107
std::exit(code);
108+
#endif
83109
}
84110

85-
[[noreturn]] void RTNAME(StopStatementText)(
111+
[[noreturn]] RT_API_ATTRS void RTNAME(StopStatementText)(
86112
const char *code, std::size_t length, bool isErrorStop, bool quiet) {
113+
#if defined(RT_DEVICE_COMPILATION)
114+
if (!quiet) {
115+
if (Fortran::runtime::executionEnvironment.noStopMessage && !isErrorStop) {
116+
std::printf("%s\n", code);
117+
} else {
118+
std::printf(
119+
"Fortran %s: %s\n", isErrorStop ? "ERROR STOP" : "STOP", code);
120+
}
121+
}
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+
}
133+
#else
87134
CloseAllExternalUnits("STOP statement");
88135
if (!quiet) {
89136
if (Fortran::runtime::executionEnvironment.noStopMessage && !isErrorStop) {
@@ -99,6 +146,7 @@ static void CloseAllExternalUnits(const char *why) {
99146
} else {
100147
std::exit(EXIT_SUCCESS);
101148
}
149+
#endif
102150
}
103151

104152
static bool StartPause() {

flang/include/flang/Runtime/stop.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
FORTRAN_EXTERN_C_BEGIN
1818

1919
// Program-initiated image stop
20-
NORETURN void RTNAME(StopStatement)(int code DEFAULT_VALUE(EXIT_SUCCESS),
21-
bool isErrorStop DEFAULT_VALUE(false), bool quiet DEFAULT_VALUE(false));
22-
NORETURN void RTNAME(StopStatementText)(const char *, size_t,
20+
NORETURN RT_API_ATTRS void RTNAME(StopStatement)(
21+
int code DEFAULT_VALUE(EXIT_SUCCESS), bool isErrorStop DEFAULT_VALUE(false),
22+
bool quiet DEFAULT_VALUE(false));
23+
NORETURN RT_API_ATTRS void RTNAME(StopStatementText)(const char *, size_t,
2324
bool isErrorStop DEFAULT_VALUE(false), bool quiet DEFAULT_VALUE(false));
2425
void RTNAME(PauseStatement)(NO_ARGUMENTS);
2526
void RTNAME(PauseStatementInt)(int);

0 commit comments

Comments
 (0)