Skip to content

Commit 3784ec6

Browse files
authored
[FLANG] Fix for Stop statements in Fortran emissary API (llvm#1299)
2 parents 2a4392c + 5333e52 commit 3784ec6

File tree

7 files changed

+43
-4
lines changed

7 files changed

+43
-4
lines changed

flang-rt/lib/runtime/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <cfenv>
1313
#include <cstdio>
1414
#include <cstdlib>
15+
#include <thread>
1516

1617
static void ConfigureFloatingPoint() {
1718
#ifdef feclearexcept // a macro in some environments; omit std::
@@ -26,7 +27,10 @@ static void ConfigureFloatingPoint() {
2627
#endif
2728
}
2829

30+
std::thread::id _main_thread_id = std::this_thread::get_id();
2931
extern "C" {
32+
std::thread::id RTNAME(GetMainThreadId)() { return _main_thread_id; }
33+
3034
void RTNAME(ProgramStart)(int argc, const char *argv[], const char *envp[],
3135
const EnvironmentDefaultList *envDefaults) {
3236
std::atexit(Fortran::runtime::NotifyOtherImagesOfNormalEnd);

flang-rt/lib/runtime/stop.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
#include "flang-rt/runtime/file.h"
1414
#include "flang-rt/runtime/io-error.h"
1515
#include "flang-rt/runtime/terminator.h"
16+
#include "flang/Runtime/main.h"
1617
#include <cfenv>
1718
#include <cstdio>
1819
#include <cstdlib>
20+
#include <thread>
1921

2022
#ifdef HAVE_BACKTRACE
2123
#include BACKTRACE_HEADER
@@ -79,6 +81,8 @@ static void CloseAllExternalUnits(const char *why) {
7981
std::fputc('\n', stderr);
8082
DescribeIEEESignaledExceptions();
8183
}
84+
if (RTNAME(GetMainThreadId)() != std::this_thread::get_id())
85+
std::abort();
8286
std::exit(code);
8387
}
8488

@@ -94,6 +98,8 @@ static void CloseAllExternalUnits(const char *why) {
9498
}
9599
DescribeIEEESignaledExceptions();
96100
}
101+
if (RTNAME(GetMainThreadId)() != std::this_thread::get_id())
102+
std::abort();
97103
if (isErrorStop) {
98104
std::exit(EXIT_FAILURE);
99105
} else {

flang/include/flang/Runtime/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
#include "flang/Runtime/c-or-cpp.h"
1313
#include "flang/Runtime/entry-names.h"
14+
#include <thread>
1415

1516
struct EnvironmentDefaultList;
1617

1718
FORTRAN_EXTERN_C_BEGIN
19+
std::thread::id RTNAME(GetMainThreadId)();
1820
void RTNAME(ProgramStart)(
1921
int, const char *[], const char *[], const struct EnvironmentDefaultList *);
2022
void RTNAME(ByteswapOption)(void); // -byteswapio

offload/DeviceRTL/include/EmissaryIds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ typedef enum {
5353
_FortranAAbort_idx,
5454
_FortranAStopStatementText_idx,
5555
_FortranAioBeginExternalFormattedOutput_idx,
56+
_FortranAStopStatement_idx,
5657
} offload_emis_fortrt_idx;
5758

5859
/// This structure is created by emisExtractArgBuf to make it easier

offload/DeviceRTL/src/EmissaryFortrt.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ uint32_t omp_get_num_threads();
3333
uint32_t omp_get_team_num();
3434
uint32_t omp_get_num_teams();
3535

36+
// All Fortran Runtime Functions pass 4 extra args to assist with
37+
// defered execution and debug. The host variadic wrappers do not use
38+
// these arguments when calling the actual Fortran runtime.
3639
#define _EXTRA_ARGS \
3740
omp_get_thread_num(), omp_get_num_threads(), omp_get_team_num(), \
3841
omp_get_num_teams()
42+
#define _START_ARGS(idx) _PACK_EMIS_IDS(EMIS_ID_FORTRT, idx), _EXTRA_ARGS,
3943

4044
void *_FortranAioBeginExternalListOutput(uint32_t a1, const char *a2,
4145
uint32_t a3) {
@@ -116,15 +120,20 @@ bool _FortranAioOutputLogical(void *cookie, bool barg) {
116120
cookie, barg);
117121
}
118122
void _FortranAAbort() {
119-
_emissary_exec(_PACK_EMIS_IDS(EMIS_ID_FORTRT, _FortranAAbort_idx));
123+
_emissary_exec(_PACK_EMIS_IDS(EMIS_ID_FORTRT, _FortranAAbort_idx),
124+
_EXTRA_ARGS);
120125
// When host service _FortranAAbort finishes, we must die from the device.
121126
__builtin_trap();
122127
}
128+
void _FortranAStopStatement(int32_t a1, bool a2, bool a3) {
129+
_emissary_exec(_PACK_EMIS_IDS(EMIS_ID_FORTRT, _FortranAStopStatement_idx),
130+
_EXTRA_ARGS, a1, a2, a3);
131+
__builtin_trap();
132+
}
123133
void _FortranAStopStatementText(char *errmsg, int64_t a1, bool a2, bool a3) {
124-
// TODO: must use string length from a1 arg
125134
errmsg[a1 - 1] = (char)0;
126135
_emissary_exec(_PACK_EMIS_IDS(EMIS_ID_FORTRT, _FortranAStopStatementText_idx),
127-
errmsg, a1, a2, a3);
136+
_EXTRA_ARGS, errmsg, a1, a2, a3);
128137
__builtin_trap();
129138
}
130139

offload/DeviceRTL/src/exports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ printf_*
2323
f90print*
2424
fprintf_*
2525

26-
_FortranAio*
26+
_FortranA*
2727
__ockl_dm_alloc
2828
__ockl_dm_dealloc
2929
__asan*

offload/plugins-nextgen/common/src/EmissaryFortrt.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ bool _FortranAioOutputComplex64(void *cookie, double re, double im);
5252
bool _FortranAioOutputLogical(void *cookie, bool truth);
5353
void _FortranAAbort();
5454
void _FortranAStopStatementText(char *errmsg, int64_t a1, bool a2, bool a3);
55+
void _FortranAStopStatement(int32_t a1, bool a2, bool a3);
5556

5657
// Save the cookie because deferred functions have execution reordered.
5758
static void *_list_started_cookie = nullptr;
@@ -212,6 +213,17 @@ extern void V_FortranAStopStatementText(void *fnptr, ...) {
212213
bool b3 = (bool)a3;
213214
_FortranAStopStatementText(errmsg, a1, b2, b3);
214215
}
216+
extern void V_FortranAStopStatement(void *fnptr, ...) {
217+
va_list args;
218+
va_start(args, fnptr);
219+
int32_t a1 = va_arg(args, int32_t);
220+
uint32_t a2 = va_arg(args, uint32_t);
221+
uint32_t a3 = va_arg(args, uint32_t);
222+
va_end(args);
223+
bool b2 = (bool)a2;
224+
bool b3 = (bool)a3;
225+
_FortranAStopStatement(a1, b2, b3);
226+
}
215227
} // end extern "C"
216228

217229
// Static vars used to defer functions to reorder execution by thread and team.
@@ -367,6 +379,11 @@ extern "C" emis_return_t EmissaryFortrt(char *data, emisArgBuf_t *ab) {
367379
fnptr = (void *)V_FortranAStopStatementText;
368380
break;
369381
}
382+
case _FortranAStopStatement_idx: {
383+
defer_for_reorder = false;
384+
fnptr = (void *)V_FortranAStopStatement;
385+
break;
386+
}
370387
case _FortranAio_INVALID:
371388
default: {
372389
defer_for_reorder = false;

0 commit comments

Comments
 (0)