Skip to content

Commit 61c99c9

Browse files
authored
[flang][OpenMP] Use builtins in remaining FlangRuntime routines (#181)
1 parent 15949dd commit 61c99c9

26 files changed

+77
-68
lines changed

flang/include/flang/Runtime/freestanding-tools.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
#define STD_FILL_N_UNSUPPORTED 1
2424
#endif
2525

26+
#if !defined(STD_MEMSET_UNSUPPORTED) && \
27+
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
28+
#define STD_MEMSET_UNSUPPORTED 1
29+
#endif
30+
31+
#if !defined(STD_MEMCPY_UNSUPPORTED) && \
32+
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
33+
#define STD_MEMCPY_UNSUPPORTED 1
34+
#endif
35+
2636
#if !defined(STD_MEMMOVE_UNSUPPORTED) && \
2737
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
2838
#define STD_MEMMOVE_UNSUPPORTED 1
@@ -64,16 +74,15 @@
6474
#endif
6575

6676
#if defined(OMP_OFFLOAD_BUILD) || defined(OMP_NOHOST_BUILD)
67-
// #pragma message "OMP_OFFLOAD_BUILD or OMP_NOHOST_BUILD is defined"
68-
#define STD_LIBC_UNSUPPORTED 1
77+
// #pragma message "Using replacements for unsupported std functions"
6978
#define STD_FILL_N_UNSUPPORTED 1
70-
#define STD_MEMSET_BUILTIN 1
79+
#define STD_MEMSET_USE_BUILTIN 1
7180
#define STD_MEMSET_UNSUPPORTED 1
72-
#define STD_MEMCPY_BUILTIN 1
81+
#define STD_MEMCPY_USE_BUILTIN 1
7382
#define STD_MEMCPY_UNSUPPORTED 1
74-
#define STD_MEMMOVE_BUILTIN 1
83+
#define STD_MEMMOVE_USE_BUILTIN 1
7584
#define STD_MEMMOVE_UNSUPPORTED 1
76-
// #define STD_STRLEN_BUILTIN 1 // still resolves to strlen
85+
// #define STD_STRLEN_USE_BUILTIN 1 // still resolves to strlen
7786
#define STD_STRLEN_UNSUPPORTED 1
7887
#define STD_MEMCMP_UNSUPPORTED 1
7988
#define STD_REALLOC_UNSUPPORTED 1
@@ -99,7 +108,7 @@ fill_n(A *start, std::size_t count, const B &value) {
99108
using std::fill_n;
100109
#endif // !STD_FILL_N_UNSUPPORTED
101110

102-
#if STD_MEMSET_BUILTIN
111+
#if STD_MEMSET_USE_BUILTIN
103112
static inline RT_API_ATTRS void memset(
104113
void *dest, uint8_t value, std::size_t count) {
105114
__builtin_memset(dest, value, count);
@@ -117,7 +126,7 @@ static inline RT_API_ATTRS void memset(
117126
using std::memset;
118127
#endif
119128

120-
#if STD_MEMCPY_BUILTIN
129+
#if STD_MEMCPY_USE_BUILTIN
121130
static inline RT_API_ATTRS void memcpy(
122131
void *dest, const void *src, std::size_t count) {
123132
__builtin_memcpy(dest, src, count);
@@ -139,7 +148,7 @@ static inline RT_API_ATTRS void memcpy(
139148
using std::memcpy;
140149
#endif
141150

142-
#if STD_MEMMOVE_BUILTIN
151+
#if STD_MEMMOVE_USE_BUILTIN
143152
static inline RT_API_ATTRS void memmove(
144153
void *dest, const void *src, std::size_t count) {
145154
__builtin_memmove(dest, src, count);
@@ -173,7 +182,7 @@ static inline RT_API_ATTRS void memmove(
173182
using std::memmove;
174183
#endif // !STD_MEMMOVE_UNSUPPORTED
175184

176-
#if STD_STRLEN_BUILTIN
185+
#if STD_STRLEN_USE_BUILTIN
177186
static inline RT_API_ATTRS std::size_t strlen(const char *str) {
178187
return __builtin_strlen(str);
179188
}

flang/runtime/array-constructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void RTDEF(PushArrayConstructorSimpleScalar)(
176176
AllocateOrReallocateVectorIfNeeded(vector, terminator, to.Elements(), 1);
177177
SubscriptValue subscript[1]{
178178
to.GetDimension(0).LowerBound() + vector.nextValuePosition};
179-
std::memcpy(to.Element<char>(subscript), from, to.ElementBytes());
179+
Fortran::runtime::memcpy(to.Element<char>(subscript), from, to.ElementBytes());
180180
++vector.nextValuePosition;
181181
}
182182

flang/runtime/buffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ template <typename STORE, std::size_t minBuffer = 65536> class FileFrame {
158158
// Avoid passing a null pointer, since it would result in an undefined
159159
// behavior.
160160
if (old != nullptr) {
161-
std::memcpy(buffer_, old + start_, chunk);
162-
std::memcpy(buffer_ + chunk, old, length_ - chunk);
161+
Fortran::runtime::memcpy(buffer_, old + start_, chunk);
162+
Fortran::runtime::memcpy(buffer_ + chunk, old, length_ - chunk);
163163
FreeMemory(old);
164164
}
165165
start_ = 0;

flang/runtime/character.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,8 @@ void RTDEF(CharacterConcatenate)(Descriptor &accumulator,
596596
from.GetLowerBounds(fromAt);
597597
for (; elements-- > 0;
598598
to += newBytes, p += oldBytes, from.IncrementSubscripts(fromAt)) {
599-
std::memcpy(to, p, oldBytes);
600-
std::memcpy(to + oldBytes, from.Element<char>(fromAt), fromBytes);
599+
Fortran::runtime::memcpy(to, p, oldBytes);
600+
Fortran::runtime::memcpy(to + oldBytes, from.Element<char>(fromAt), fromBytes);
601601
}
602602
FreeMemory(old);
603603
}
@@ -611,7 +611,7 @@ void RTDEF(CharacterConcatenateScalar1)(
611611
std::size_t oldLen{accumulator.ElementBytes()};
612612
accumulator.raw().elem_len += chars;
613613
RUNTIME_CHECK(terminator, accumulator.Allocate() == CFI_SUCCESS);
614-
std::memcpy(accumulator.OffsetElement<char>(oldLen), from, chars);
614+
Fortran::runtime::memcpy(accumulator.OffsetElement<char>(oldLen), from, chars);
615615
FreeMemory(old);
616616
}
617617

@@ -677,15 +677,15 @@ void RTDEF(CharacterCompare)(
677677
std::size_t RTDEF(CharacterAppend1)(char *lhs, std::size_t lhsBytes,
678678
std::size_t offset, const char *rhs, std::size_t rhsBytes) {
679679
if (auto n{std::min(lhsBytes - offset, rhsBytes)}) {
680-
std::memcpy(lhs + offset, rhs, n);
680+
Fortran::runtime::memcpy(lhs + offset, rhs, n);
681681
offset += n;
682682
}
683683
return offset;
684684
}
685685

686686
void RTDEF(CharacterPad1)(char *lhs, std::size_t bytes, std::size_t offset) {
687687
if (bytes > offset) {
688-
std::memset(lhs + offset, ' ', bytes - offset);
688+
Fortran::runtime::memset(lhs + offset, ' ', bytes - offset);
689689
}
690690
}
691691

@@ -817,7 +817,7 @@ void RTDEF(Repeat)(Descriptor &result, const Descriptor &string,
817817
}
818818
const char *from{string.OffsetElement()};
819819
for (char *to{result.OffsetElement()}; ncopies-- > 0; to += origBytes) {
820-
std::memcpy(to, from, origBytes);
820+
Fortran::runtime::memcpy(to, from, origBytes);
821821
}
822822
}
823823

@@ -847,7 +847,7 @@ void RTDEF(Trim)(Descriptor &result, const Descriptor &string,
847847
result.Establish(string.type(), resultBytes, nullptr, 0, nullptr,
848848
CFI_attribute_allocatable);
849849
RUNTIME_CHECK(terminator, result.Allocate() == CFI_SUCCESS);
850-
std::memcpy(result.OffsetElement(), string.OffsetElement(), resultBytes);
850+
Fortran::runtime::memcpy(result.OffsetElement(), string.OffsetElement(), resultBytes);
851851
}
852852

853853
std::size_t RTDEF(Verify1)(const char *x, std::size_t xLen, const char *set,

flang/runtime/command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static std::int64_t StringLength(const char *string) {
5454

5555
static void FillWithSpaces(const Descriptor &value, std::size_t offset = 0) {
5656
if (offset < value.ElementBytes()) {
57-
std::memset(
57+
Fortran::runtime::memset(
5858
value.OffsetElement(offset), ' ', value.ElementBytes() - offset);
5959
}
6060
}

flang/runtime/copy.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "copy.h"
1010
#include "stack.h"
1111
#include "terminator.h"
12+
#include "tools.h"
1213
#include "type-info.h"
1314
#include "flang/Runtime/allocatable.h"
1415
#include "flang/Runtime/descriptor.h"
@@ -101,7 +102,7 @@ RT_API_ATTRS void CopyElement(const Descriptor &to, const SubscriptValue toAt[],
101102
char *toPtr{to.Element<char>(toAt)};
102103
char *fromPtr{from.Element<char>(fromAt)};
103104
RUNTIME_CHECK(terminator, to.ElementBytes() == from.ElementBytes());
104-
std::memcpy(toPtr, fromPtr, to.ElementBytes());
105+
Fortran::runtime::memcpy(toPtr, fromPtr, to.ElementBytes());
105106
return;
106107
}
107108

@@ -148,7 +149,7 @@ RT_API_ATTRS void CopyElement(const Descriptor &to, const SubscriptValue toAt[],
148149
// Moreover, if we came here from an Component::Genre::Data component,
149150
// all the per-element copies are redundant, because the parent
150151
// has already been copied as a whole.
151-
std::memcpy(toPtr, fromPtr, curTo.ElementBytes());
152+
Fortran::runtime::memcpy(toPtr, fromPtr, curTo.ElementBytes());
152153
--elements;
153154
if (elements != 0) {
154155
currentCopy.IncrementSubscripts(terminator);

flang/runtime/derived.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ RT_API_ATTRS int Initialize(const Descriptor &instance,
7373
std::size_t bytes{comp.SizeInBytes(instance)};
7474
for (std::size_t j{0}; j++ < elements; instance.IncrementSubscripts(at)) {
7575
char *ptr{instance.ElementComponent<char>(at, comp.offset())};
76-
// std::memcpy(ptr, init, bytes);
7776
Fortran::runtime::memcpy(ptr, init, bytes);
7877
}
7978
} else if (comp.genre() == typeInfo::Component::Genre::Pointer) {

flang/runtime/descriptor-io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Fortran::common::optional<bool> DefinedFormattedIo(IoStatementState &io,
3232
if (edit.descriptor == DataEdit::DefinedDerivedType) {
3333
ioType[0] = 'D';
3434
ioType[1] = 'T';
35-
std::memcpy(ioType + 2, edit.ioType, edit.ioTypeChars);
35+
Fortran::runtime::memcpy(ioType + 2, edit.ioType, edit.ioTypeChars);
3636
} else {
3737
runtime::strcpy(
3838
ioType, io.mutableModes().inNamelist ? "NAMELIST" : "LISTDIRECTED");

flang/runtime/edit-input.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static RT_API_ATTRS bool EditBOZInput(
121121
io.HandleAbsolutePosition(start);
122122
remaining.reset();
123123
// Make a second pass now that the digit count is known
124-
std::memset(n, 0, bytes);
124+
Fortran::runtime::memset(n, 0, bytes);
125125
int increment{isHostLittleEndian ? -1 : 1};
126126
auto *data{reinterpret_cast<unsigned char *>(n) +
127127
(isHostLittleEndian ? significantBytes - 1 : bytes - significantBytes)};
@@ -280,9 +280,9 @@ RT_API_ATTRS bool EditIntegerInput(
280280
// For kind==8 (i.e. shft==0), the value is stored in low_ in big endian.
281281
if (!isHostLittleEndian && shft >= 0) {
282282
auto l{value.low() << (8 * shft)};
283-
std::memcpy(n, &l, kind);
283+
Fortran::runtime::memcpy(n, &l, kind);
284284
} else {
285-
std::memcpy(n, &value, kind); // a blank field means zero
285+
Fortran::runtime::memcpy(n, &value, kind); // a blank field means zero
286286
}
287287
return true;
288288
} else {
@@ -1095,7 +1095,7 @@ RT_API_ATTRS bool EditCharacterInput(IoStatementState &io, const DataEdit &edit,
10951095
--skipChars;
10961096
} else {
10971097
char32_t buffer{0};
1098-
std::memcpy(&buffer, input, chunkBytes);
1098+
Fortran::runtime::memcpy(&buffer, input, chunkBytes);
10991099
if ((sizeof *x == 1 && buffer > 0xff) ||
11001100
(sizeof *x == 2 && buffer > 0xffff)) {
11011101
*x++ = '?';
@@ -1122,7 +1122,7 @@ RT_API_ATTRS bool EditCharacterInput(IoStatementState &io, const DataEdit &edit,
11221122
chunkBytes = std::min<std::size_t>(remainingChars, readyBytes);
11231123
chunkBytes = std::min<std::size_t>(lengthChars, chunkBytes);
11241124
chunkChars = chunkBytes;
1125-
std::memcpy(x, input, chunkBytes);
1125+
Fortran::runtime::memcpy(x, input, chunkBytes);
11261126
x += chunkBytes;
11271127
lengthChars -= chunkChars;
11281128
}

flang/runtime/extensions.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace Fortran::runtime {
6060

6161
void GetUsernameEnvVar(const char *envName, char *arg, std::int64_t length) {
6262
Descriptor name{*Descriptor::Create(
63-
1, std::strlen(envName) + 1, const_cast<char *>(envName), 0)};
63+
1, Fortran::runtime::strlen(envName) + 1, const_cast<char *>(envName), 0)};
6464
Descriptor value{*Descriptor::Create(1, length, arg, 0)};
6565

6666
RTNAME(GetEnvVariable)
@@ -83,7 +83,7 @@ void FORTRAN_PROCEDURE_NAME(fdate)(char *arg, std::int64_t length) {
8383
char str[26];
8484
// Insufficient space, fill with spaces and return.
8585
if (length < 24) {
86-
std::memset(arg, ' ', length);
86+
Fortran::runtime::memset(arg, ' ', length);
8787
return;
8888
}
8989

@@ -115,8 +115,8 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
115115
void FORTRAN_PROCEDURE_NAME(getlog)(char *arg, std::int64_t length) {
116116
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
117117
if (length >= 1 && getlogin_r(arg, length) == 0) {
118-
auto loginLen{std::strlen(arg)};
119-
std::memset(
118+
auto loginLen{Fortran::runtime::strlen(arg)};
119+
Fortran::runtime::memset(
120120
arg + loginLen, ' ', static_cast<std::size_t>(length) - loginLen);
121121
return;
122122
}
@@ -170,7 +170,7 @@ std::int64_t FORTRAN_PROCEDURE_NAME(access)(const char *name,
170170
char *newName{nullptr};
171171
if (name[nameLength - 1] != '\0') {
172172
newName = static_cast<char *>(std::malloc(nameLength + 1));
173-
std::memcpy(newName, name, nameLength);
173+
Fortran::runtime::memcpy(newName, name, nameLength);
174174
newName[nameLength] = '\0';
175175
name = newName;
176176
}

0 commit comments

Comments
 (0)