Skip to content

Commit 8b217eb

Browse files
authored
[OPENMP] [FLANG] add support for formatted Io which generates call to… (llvm#1185)
2 parents b6370f5 + 54fa9e2 commit 8b217eb

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

offload/DeviceRTL/include/EmissaryIds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ typedef enum {
5252
_FortranAioOutputLogical_idx,
5353
_FortranAAbort_idx,
5454
_FortranAStopStatementText_idx,
55+
_FortranAioBeginExternalFormattedOutput_idx,
5556
} offload_emis_fortrt_idx;
5657

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

offload/DeviceRTL/src/EmissaryFortrt.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ void *_FortranAioBeginExternalListOutput(uint32_t a1, const char *a2,
4444
_EXTRA_ARGS, a1, a2, a3);
4545
return cookie;
4646
}
47+
48+
void *_FortranAioBeginExternalFormattedOutput(char *fmt, uint64_t fmtlen,
49+
void *ptr, uint32_t val1,
50+
char *source_name,
51+
uint32_t val2) {
52+
fmt[fmtlen - 1] = (char)0;
53+
void *cookie = (void *)_emissary_exec(
54+
_PACK_EMIS_IDS(EMIS_ID_FORTRT,
55+
_FortranAioBeginExternalFormattedOutput_idx),
56+
_EXTRA_ARGS, fmt, fmtlen, ptr, val1, source_name, val2);
57+
return cookie;
58+
}
59+
4760
bool _FortranAioOutputAscii(void *a1, char *a2, uint64_t a3) {
4861
// insert null terminating char so _emissary_exec can correctly
4962
// calculate runtime str length

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
extern "C" {
3737
void *_FortranAioBeginExternalListOutput(uint32_t a1, const char *a2,
3838
uint32_t a3);
39+
void *_FortranAioBeginExternalFormattedOutput(const char *ptr1, uint64_t x1,
40+
void *ptr2, uint32_t x2,
41+
const char *ptr3, uint32_t x3);
3942
bool _FortranAioOutputAscii(void *a1, char *a2, uint64_t a3);
4043
bool _FortranAioOutputInteger32(void *a1, uint32_t a2);
4144
uint32_t _FortranAioEndIoStatement(void *a1);
@@ -63,6 +66,21 @@ extern void *V_FortranAioBeginExternalListOutput(void *fnptr, ...) {
6366
_list_started_cookie = cookie;
6467
return cookie;
6568
}
69+
extern void *V_FortranAioBeginExternalFormattedOutput(void *fnptr, ...) {
70+
va_list args;
71+
va_start(args, fnptr);
72+
const char *p0 = va_arg(args, const char *);
73+
int64_t v0 = va_arg(args, int64_t);
74+
void *p1 = va_arg(args, void *);
75+
int32_t v1 = va_arg(args, int32_t);
76+
const char *p2 = va_arg(args, const char *);
77+
int32_t v2 = va_arg(args, int32_t);
78+
va_end(args);
79+
void *cookie =
80+
_FortranAioBeginExternalFormattedOutput(p0, v0, p1, v1, p2, v2);
81+
_list_started_cookie = cookie;
82+
return cookie;
83+
}
6684
extern bool V_FortranAioOutputAscii(void *fnptr, ...) {
6785
va_list args;
6886
va_start(args, fnptr);
@@ -211,6 +229,7 @@ typedef struct {
211229
uint32_t dfnid; // The dvoideferred function id, in order received
212230
uint64_t *arg_array; // ptr to malloced arg_array
213231
char *c_ptr; // ptr to null terminated char string
232+
char *c_ptr2; // ptr to null terminated char string
214233
uint64_t thread_num;
215234
uint64_t num_threads;
216235
uint64_t team_num;
@@ -242,6 +261,7 @@ extern "C" emis_return_t EmissaryFortrt(char *data, emisArgBuf_t *ab) {
242261
_deferred_fns_ptr = new std::vector<deferred_entry_t *>;
243262

244263
char *c_ptr = nullptr;
264+
char *c_ptr2 = nullptr;
245265
bool defer_for_reorder = true;
246266
bool run_deferred_functions = false;
247267
switch (ab->emisfnid) {
@@ -257,6 +277,26 @@ extern "C" emis_return_t EmissaryFortrt(char *data, emisArgBuf_t *ab) {
257277
a[5] = (emis_argptr_t *)c_ptr;
258278
break;
259279
}
280+
case _FortranAioBeginExternalFormattedOutput_idx: {
281+
_deferred_begin_statements++;
282+
fnptr = (void *)V_FortranAioBeginExternalFormattedOutput;
283+
size_t slen = std::strlen((char *)a[8]) + 1;
284+
c_ptr = (char *)aligned_alloc(sizeof(uint64_t *), slen);
285+
if (!c_ptr)
286+
fprintf(stderr, "MALLOC FAILED for c_ptr size:%ld \n", slen);
287+
std::strncpy(c_ptr, (char *)a[8], slen - 1);
288+
c_ptr[slen - 1] = (char)0;
289+
a[8] = (emis_argptr_t *)c_ptr;
290+
291+
slen = std::strlen((char *)a[4]) + 1;
292+
c_ptr2 = (char *)aligned_alloc(sizeof(uint64_t *), slen);
293+
if (!c_ptr2)
294+
fprintf(stderr, "MALLOC FAILED for c_ptr2 size:%ld \n", slen);
295+
std::strncpy(c_ptr2, (char *)a[4], slen - 1);
296+
c_ptr2[slen - 1] = (char)0;
297+
a[4] = (emis_argptr_t *)c_ptr2;
298+
break;
299+
}
260300
case _FortranAioOutputAscii_idx: {
261301
fnptr = (void *)V_FortranAioOutputAscii;
262302

@@ -362,6 +402,7 @@ extern "C" emis_return_t EmissaryFortrt(char *data, emisArgBuf_t *ab) {
362402
q->arg_array = arg_array;
363403
q->return_value = (emis_return_t)0;
364404
q->c_ptr = c_ptr;
405+
q->c_ptr2 = c_ptr2;
365406
_deferred_fns_ptr->push_back(q);
366407
} else {
367408
// execute a non deferred function
@@ -392,6 +433,8 @@ extern "C" emis_return_t EmissaryFortrt(char *data, emisArgBuf_t *ab) {
392433
for (auto q : *_deferred_fns_ptr) {
393434
if (q->c_ptr)
394435
free(q->c_ptr);
436+
if (q->c_ptr2)
437+
free(q->c_ptr2);
395438
free(q->arg_array);
396439
delete q;
397440
}
@@ -402,7 +445,7 @@ extern "C" emis_return_t EmissaryFortrt(char *data, emisArgBuf_t *ab) {
402445
_max_num_threads = 0;
403446
_max_num_teams = 0;
404447
delete _deferred_fns_ptr;
405-
_deferred_fns_ptr=nullptr;
448+
_deferred_fns_ptr = nullptr;
406449
} // end run_deferred_functions
407450

408451
return return_value;

0 commit comments

Comments
 (0)