Skip to content

[flang][device] Enable Stop functions on device build #133803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang-rt/lib/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ set(supported_sources
pseudo-unit.cpp
ragged.cpp
stat.cpp
stop.cpp
sum.cpp
support.cpp
terminator.cpp
Expand Down
52 changes: 50 additions & 2 deletions flang-rt/lib/runtime/stop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,33 @@ static void CloseAllExternalUnits(const char *why) {
Fortran::runtime::io::ExternalFileUnit::CloseAll(handler);
}

[[noreturn]] void RTNAME(StopStatement)(
[[noreturn]] RT_API_ATTRS void RTNAME(StopStatement)(
int code, bool isErrorStop, bool quiet) {
#if defined(RT_DEVICE_COMPILATION)
if (Fortran::runtime::executionEnvironment.noStopMessage && code == 0) {
quiet = true;
}
if (!quiet) {
if (isErrorStop) {
std::printf("Fortran ERROR STOP");
} else {
std::printf("Fortran STOP");
}
if (code != EXIT_SUCCESS) {
std::printf(": code %d\n", code);
}
std::printf('\n');
}
#if defined(__CUDACC__)
// NVCC supports __trap().
__trap();
#elif defined(__clang__)
// Clang supports __builtin_trap().
__builtin_trap();
#else
#error "unsupported compiler"
#endif
#else
CloseAllExternalUnits("STOP statement");
if (Fortran::runtime::executionEnvironment.noStopMessage && code == 0) {
quiet = true;
Expand All @@ -80,10 +105,32 @@ static void CloseAllExternalUnits(const char *why) {
DescribeIEEESignaledExceptions();
}
std::exit(code);
#endif
}

[[noreturn]] void RTNAME(StopStatementText)(
[[noreturn]] RT_API_ATTRS void RTNAME(StopStatementText)(
const char *code, std::size_t length, bool isErrorStop, bool quiet) {
#if defined(RT_DEVICE_COMPILATION)
if (!quiet) {
if (Fortran::runtime::executionEnvironment.noStopMessage && !isErrorStop) {
std::printf("%s\n", code);
} else {
std::printf(
"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
}
#else
CloseAllExternalUnits("STOP statement");
if (!quiet) {
if (Fortran::runtime::executionEnvironment.noStopMessage && !isErrorStop) {
Expand All @@ -99,6 +146,7 @@ static void CloseAllExternalUnits(const char *why) {
} else {
std::exit(EXIT_SUCCESS);
}
#endif
}

static bool StartPause() {
Expand Down
7 changes: 4 additions & 3 deletions flang/include/flang/Runtime/stop.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
FORTRAN_EXTERN_C_BEGIN

// Program-initiated image stop
NORETURN void RTNAME(StopStatement)(int code DEFAULT_VALUE(EXIT_SUCCESS),
bool isErrorStop DEFAULT_VALUE(false), bool quiet DEFAULT_VALUE(false));
NORETURN void RTNAME(StopStatementText)(const char *, size_t,
NORETURN RT_API_ATTRS void RTNAME(StopStatement)(
int code DEFAULT_VALUE(EXIT_SUCCESS), bool isErrorStop DEFAULT_VALUE(false),
bool quiet DEFAULT_VALUE(false));
NORETURN RT_API_ATTRS void RTNAME(StopStatementText)(const char *, size_t,
bool isErrorStop DEFAULT_VALUE(false), bool quiet DEFAULT_VALUE(false));
void RTNAME(PauseStatement)(NO_ARGUMENTS);
void RTNAME(PauseStatementInt)(int);
Expand Down