Skip to content

Commit

Permalink
[flang][OpenMP] Use builtins in remaining FlangRuntime routines
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalermo committed Oct 11, 2024
1 parent 15949dd commit 35f077a
Show file tree
Hide file tree
Showing 26 changed files with 77 additions and 68 deletions.
29 changes: 19 additions & 10 deletions flang/include/flang/Runtime/freestanding-tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
#define STD_FILL_N_UNSUPPORTED 1
#endif

#if !defined(STD_MEMSET_UNSUPPORTED) && \
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
#define STD_MEMSET_UNSUPPORTED 1
#endif

#if !defined(STD_MEMCPY_UNSUPPORTED) && \
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
#define STD_MEMCPY_UNSUPPORTED 1
#endif

#if !defined(STD_MEMMOVE_UNSUPPORTED) && \
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
#define STD_MEMMOVE_UNSUPPORTED 1
Expand Down Expand Up @@ -64,16 +74,15 @@
#endif

#if defined(OMP_OFFLOAD_BUILD) || defined(OMP_NOHOST_BUILD)
// #pragma message "OMP_OFFLOAD_BUILD or OMP_NOHOST_BUILD is defined"
#define STD_LIBC_UNSUPPORTED 1
// #pragma message "Using replacements for unsupported std functions"
#define STD_FILL_N_UNSUPPORTED 1
#define STD_MEMSET_BUILTIN 1
#define STD_MEMSET_USE_BUILTIN 1
#define STD_MEMSET_UNSUPPORTED 1
#define STD_MEMCPY_BUILTIN 1
#define STD_MEMCPY_USE_BUILTIN 1
#define STD_MEMCPY_UNSUPPORTED 1
#define STD_MEMMOVE_BUILTIN 1
#define STD_MEMMOVE_USE_BUILTIN 1
#define STD_MEMMOVE_UNSUPPORTED 1
// #define STD_STRLEN_BUILTIN 1 // still resolves to strlen
// #define STD_STRLEN_USE_BUILTIN 1 // still resolves to strlen
#define STD_STRLEN_UNSUPPORTED 1
#define STD_MEMCMP_UNSUPPORTED 1
#define STD_REALLOC_UNSUPPORTED 1
Expand All @@ -99,7 +108,7 @@ fill_n(A *start, std::size_t count, const B &value) {
using std::fill_n;
#endif // !STD_FILL_N_UNSUPPORTED

#if STD_MEMSET_BUILTIN
#if STD_MEMSET_USE_BUILTIN
static inline RT_API_ATTRS void memset(
void *dest, uint8_t value, std::size_t count) {
__builtin_memset(dest, value, count);
Expand All @@ -117,7 +126,7 @@ static inline RT_API_ATTRS void memset(
using std::memset;
#endif

#if STD_MEMCPY_BUILTIN
#if STD_MEMCPY_USE_BUILTIN
static inline RT_API_ATTRS void memcpy(
void *dest, const void *src, std::size_t count) {
__builtin_memcpy(dest, src, count);
Expand All @@ -139,7 +148,7 @@ static inline RT_API_ATTRS void memcpy(
using std::memcpy;
#endif

#if STD_MEMMOVE_BUILTIN
#if STD_MEMMOVE_USE_BUILTIN
static inline RT_API_ATTRS void memmove(
void *dest, const void *src, std::size_t count) {
__builtin_memmove(dest, src, count);
Expand Down Expand Up @@ -173,7 +182,7 @@ static inline RT_API_ATTRS void memmove(
using std::memmove;
#endif // !STD_MEMMOVE_UNSUPPORTED

#if STD_STRLEN_BUILTIN
#if STD_STRLEN_USE_BUILTIN
static inline RT_API_ATTRS std::size_t strlen(const char *str) {
return __builtin_strlen(str);
}
Expand Down
2 changes: 1 addition & 1 deletion flang/runtime/array-constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void RTDEF(PushArrayConstructorSimpleScalar)(
AllocateOrReallocateVectorIfNeeded(vector, terminator, to.Elements(), 1);
SubscriptValue subscript[1]{
to.GetDimension(0).LowerBound() + vector.nextValuePosition};
std::memcpy(to.Element<char>(subscript), from, to.ElementBytes());
Fortran::runtime::memcpy(to.Element<char>(subscript), from, to.ElementBytes());
++vector.nextValuePosition;
}

Expand Down
4 changes: 2 additions & 2 deletions flang/runtime/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ template <typename STORE, std::size_t minBuffer = 65536> class FileFrame {
// Avoid passing a null pointer, since it would result in an undefined
// behavior.
if (old != nullptr) {
std::memcpy(buffer_, old + start_, chunk);
std::memcpy(buffer_ + chunk, old, length_ - chunk);
Fortran::runtime::memcpy(buffer_, old + start_, chunk);
Fortran::runtime::memcpy(buffer_ + chunk, old, length_ - chunk);
FreeMemory(old);
}
start_ = 0;
Expand Down
14 changes: 7 additions & 7 deletions flang/runtime/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ void RTDEF(CharacterConcatenate)(Descriptor &accumulator,
from.GetLowerBounds(fromAt);
for (; elements-- > 0;
to += newBytes, p += oldBytes, from.IncrementSubscripts(fromAt)) {
std::memcpy(to, p, oldBytes);
std::memcpy(to + oldBytes, from.Element<char>(fromAt), fromBytes);
Fortran::runtime::memcpy(to, p, oldBytes);
Fortran::runtime::memcpy(to + oldBytes, from.Element<char>(fromAt), fromBytes);
}
FreeMemory(old);
}
Expand All @@ -611,7 +611,7 @@ void RTDEF(CharacterConcatenateScalar1)(
std::size_t oldLen{accumulator.ElementBytes()};
accumulator.raw().elem_len += chars;
RUNTIME_CHECK(terminator, accumulator.Allocate() == CFI_SUCCESS);
std::memcpy(accumulator.OffsetElement<char>(oldLen), from, chars);
Fortran::runtime::memcpy(accumulator.OffsetElement<char>(oldLen), from, chars);
FreeMemory(old);
}

Expand Down Expand Up @@ -677,15 +677,15 @@ void RTDEF(CharacterCompare)(
std::size_t RTDEF(CharacterAppend1)(char *lhs, std::size_t lhsBytes,
std::size_t offset, const char *rhs, std::size_t rhsBytes) {
if (auto n{std::min(lhsBytes - offset, rhsBytes)}) {
std::memcpy(lhs + offset, rhs, n);
Fortran::runtime::memcpy(lhs + offset, rhs, n);
offset += n;
}
return offset;
}

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

Expand Down Expand Up @@ -817,7 +817,7 @@ void RTDEF(Repeat)(Descriptor &result, const Descriptor &string,
}
const char *from{string.OffsetElement()};
for (char *to{result.OffsetElement()}; ncopies-- > 0; to += origBytes) {
std::memcpy(to, from, origBytes);
Fortran::runtime::memcpy(to, from, origBytes);
}
}

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

std::size_t RTDEF(Verify1)(const char *x, std::size_t xLen, const char *set,
Expand Down
2 changes: 1 addition & 1 deletion flang/runtime/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static std::int64_t StringLength(const char *string) {

static void FillWithSpaces(const Descriptor &value, std::size_t offset = 0) {
if (offset < value.ElementBytes()) {
std::memset(
Fortran::runtime::memset(
value.OffsetElement(offset), ' ', value.ElementBytes() - offset);
}
}
Expand Down
5 changes: 3 additions & 2 deletions flang/runtime/copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "copy.h"
#include "stack.h"
#include "terminator.h"
#include "tools.h"
#include "type-info.h"
#include "flang/Runtime/allocatable.h"
#include "flang/Runtime/descriptor.h"
Expand Down Expand Up @@ -101,7 +102,7 @@ RT_API_ATTRS void CopyElement(const Descriptor &to, const SubscriptValue toAt[],
char *toPtr{to.Element<char>(toAt)};
char *fromPtr{from.Element<char>(fromAt)};
RUNTIME_CHECK(terminator, to.ElementBytes() == from.ElementBytes());
std::memcpy(toPtr, fromPtr, to.ElementBytes());
Fortran::runtime::memcpy(toPtr, fromPtr, to.ElementBytes());
return;
}

Expand Down Expand Up @@ -148,7 +149,7 @@ RT_API_ATTRS void CopyElement(const Descriptor &to, const SubscriptValue toAt[],
// Moreover, if we came here from an Component::Genre::Data component,
// all the per-element copies are redundant, because the parent
// has already been copied as a whole.
std::memcpy(toPtr, fromPtr, curTo.ElementBytes());
Fortran::runtime::memcpy(toPtr, fromPtr, curTo.ElementBytes());
--elements;
if (elements != 0) {
currentCopy.IncrementSubscripts(terminator);
Expand Down
1 change: 0 additions & 1 deletion flang/runtime/derived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ RT_API_ATTRS int Initialize(const Descriptor &instance,
std::size_t bytes{comp.SizeInBytes(instance)};
for (std::size_t j{0}; j++ < elements; instance.IncrementSubscripts(at)) {
char *ptr{instance.ElementComponent<char>(at, comp.offset())};
// std::memcpy(ptr, init, bytes);
Fortran::runtime::memcpy(ptr, init, bytes);
}
} else if (comp.genre() == typeInfo::Component::Genre::Pointer) {
Expand Down
2 changes: 1 addition & 1 deletion flang/runtime/descriptor-io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Fortran::common::optional<bool> DefinedFormattedIo(IoStatementState &io,
if (edit.descriptor == DataEdit::DefinedDerivedType) {
ioType[0] = 'D';
ioType[1] = 'T';
std::memcpy(ioType + 2, edit.ioType, edit.ioTypeChars);
Fortran::runtime::memcpy(ioType + 2, edit.ioType, edit.ioTypeChars);
} else {
runtime::strcpy(
ioType, io.mutableModes().inNamelist ? "NAMELIST" : "LISTDIRECTED");
Expand Down
10 changes: 5 additions & 5 deletions flang/runtime/edit-input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static RT_API_ATTRS bool EditBOZInput(
io.HandleAbsolutePosition(start);
remaining.reset();
// Make a second pass now that the digit count is known
std::memset(n, 0, bytes);
Fortran::runtime::memset(n, 0, bytes);
int increment{isHostLittleEndian ? -1 : 1};
auto *data{reinterpret_cast<unsigned char *>(n) +
(isHostLittleEndian ? significantBytes - 1 : bytes - significantBytes)};
Expand Down Expand Up @@ -280,9 +280,9 @@ RT_API_ATTRS bool EditIntegerInput(
// For kind==8 (i.e. shft==0), the value is stored in low_ in big endian.
if (!isHostLittleEndian && shft >= 0) {
auto l{value.low() << (8 * shft)};
std::memcpy(n, &l, kind);
Fortran::runtime::memcpy(n, &l, kind);
} else {
std::memcpy(n, &value, kind); // a blank field means zero
Fortran::runtime::memcpy(n, &value, kind); // a blank field means zero
}
return true;
} else {
Expand Down Expand Up @@ -1095,7 +1095,7 @@ RT_API_ATTRS bool EditCharacterInput(IoStatementState &io, const DataEdit &edit,
--skipChars;
} else {
char32_t buffer{0};
std::memcpy(&buffer, input, chunkBytes);
Fortran::runtime::memcpy(&buffer, input, chunkBytes);
if ((sizeof *x == 1 && buffer > 0xff) ||
(sizeof *x == 2 && buffer > 0xffff)) {
*x++ = '?';
Expand All @@ -1122,7 +1122,7 @@ RT_API_ATTRS bool EditCharacterInput(IoStatementState &io, const DataEdit &edit,
chunkBytes = std::min<std::size_t>(remainingChars, readyBytes);
chunkBytes = std::min<std::size_t>(lengthChars, chunkBytes);
chunkChars = chunkBytes;
std::memcpy(x, input, chunkBytes);
Fortran::runtime::memcpy(x, input, chunkBytes);
x += chunkBytes;
lengthChars -= chunkChars;
}
Expand Down
10 changes: 5 additions & 5 deletions flang/runtime/extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace Fortran::runtime {

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

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

Expand Down Expand Up @@ -115,8 +115,8 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
void FORTRAN_PROCEDURE_NAME(getlog)(char *arg, std::int64_t length) {
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
if (length >= 1 && getlogin_r(arg, length) == 0) {
auto loginLen{std::strlen(arg)};
std::memset(
auto loginLen{Fortran::runtime::strlen(arg)};
Fortran::runtime::memset(
arg + loginLen, ' ', static_cast<std::size_t>(length) - loginLen);
return;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ std::int64_t FORTRAN_PROCEDURE_NAME(access)(const char *name,
char *newName{nullptr};
if (name[nameLength - 1] != '\0') {
newName = static_cast<char *>(std::malloc(nameLength + 1));
std::memcpy(newName, name, nameLength);
Fortran::runtime::memcpy(newName, name, nameLength);
newName[nameLength] = '\0';
name = newName;
}
Expand Down
4 changes: 2 additions & 2 deletions flang/runtime/external-unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool ExternalFileUnit::OpenUnit(Fortran::common::optional<OpenStatus> status,
bool impliedClose{false};
if (IsConnected()) {
bool isSamePath{newPath.get() && path() && pathLength() == newPathLength &&
std::memcmp(path(), newPath.get(), newPathLength) == 0};
Fortran::runtime::memcmp(path(), newPath.get(), newPathLength) == 0};
if (status && *status != OpenStatus::Old && isSamePath) {
handler.SignalError("OPEN statement for connected unit may not have "
"explicit STATUS= other than 'OLD'");
Expand Down Expand Up @@ -202,7 +202,7 @@ bool ExternalFileUnit::OpenAnonymousUnit(
std::size_t pathMaxLen{32};
auto path{SizedNew<char>{handler}(pathMaxLen)};
std::snprintf(path.get(), pathMaxLen, "fort.%d", unitNumber_);
OpenUnit(status, action, position, std::move(path), std::strlen(path.get()),
OpenUnit(status, action, position, std::move(path), Fortran::runtime::strlen(path.get()),
convert, handler);
return IsConnected();
}
Expand Down
6 changes: 3 additions & 3 deletions flang/runtime/extrema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ inline RT_API_ATTRS void TypedPartialMaxOrMinLoc(const char *intrinsic,
CreatePartialReductionResult(result, x,
Descriptor::BytesFor(TypeCategory::Integer, kind), dim, terminator,
intrinsic, TypeCode{TypeCategory::Integer, kind});
std::memset(
Fortran::runtime::memset(
result.OffsetElement(), 0, result.Elements() * result.ElementBytes());
return;
}
Expand Down Expand Up @@ -518,11 +518,11 @@ template <int KIND, bool IS_MAXVAL> class CharacterExtremumAccumulator {
static_assert(std::is_same_v<A, Type>);
std::size_t byteSize{array_.ElementBytes()};
if (extremum_) {
std::memcpy(p, extremum_, byteSize);
Fortran::runtime::memcpy(p, extremum_, byteSize);
} else {
// Empty array; fill with character 0 for MAXVAL.
// For MINVAL, set all of the bits.
std::memset(p, IS_MAXVAL ? 0 : 255, byteSize);
Fortran::runtime::memset(p, IS_MAXVAL ? 0 : 255, byteSize);
}
}
RT_API_ATTRS bool Accumulate(const Type *x) {
Expand Down
2 changes: 1 addition & 1 deletion flang/runtime/format-implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ RT_API_ATTRS FormatControl<CONTEXT>::FormatControl(const Terminator &terminator,
SubscriptValue at[maxRank];
formatDescriptor->GetLowerBounds(at);
for (std::size_t j{0}; j < elements; ++j) {
std::memcpy(p, formatDescriptor->Element<char>(at), elementBytes);
Fortran::runtime::memcpy(p, formatDescriptor->Element<char>(at), elementBytes);
p += elementBytes;
formatDescriptor->IncrementSubscripts(at);
}
Expand Down
2 changes: 1 addition & 1 deletion flang/runtime/internal-unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ RT_API_ATTRS bool InternalDescriptorUnit<DIR>::Emit(
BlankFill(record + furthestPositionInRecord,
positionInRecord - furthestPositionInRecord);
}
std::memcpy(record + positionInRecord, data, bytes);
Fortran::runtime::memcpy(record + positionInRecord, data, bytes);
positionInRecord += bytes;
furthestPositionInRecord = furthestAfter;
return ok;
Expand Down
2 changes: 1 addition & 1 deletion flang/runtime/io-error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ bool IoErrorHandler::GetIoMsg(char *buffer, std::size_t bufferLength) {
} else if (ok) {
std::size_t copied{Fortran::runtime::strlen(buffer)};
if (copied < bufferLength) {
std::memset(buffer + copied, ' ', bufferLength - copied);
Fortran::runtime::memset(buffer + copied, ' ', bufferLength - copied);
}
return true;
} else {
Expand Down
4 changes: 2 additions & 2 deletions flang/runtime/matmul-transpose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ inline static RT_API_ATTRS void MatrixTransposedTimesMatrix(
std::size_t yColumnByteStride = 0) {
using ResultType = CppTypeFor<RCAT, RKIND>;

std::memset(product, 0, rows * cols * sizeof *product);
Fortran::runtime::memset(product, 0, rows * cols * sizeof *product);
for (SubscriptValue j{0}; j < cols; ++j) {
for (SubscriptValue i{0}; i < rows; ++i) {
for (SubscriptValue k{0}; k < n; ++k) {
Expand Down Expand Up @@ -132,7 +132,7 @@ inline static RT_API_ATTRS void MatrixTransposedTimesVector(
SubscriptValue n, const XT *RESTRICT x, const YT *RESTRICT y,
std::size_t xColumnByteStride = 0) {
using ResultType = CppTypeFor<RCAT, RKIND>;
std::memset(product, 0, rows * sizeof *product);
Fortran::runtime::memset(product, 0, rows * sizeof *product);
for (SubscriptValue i{0}; i < rows; ++i) {
for (SubscriptValue k{0}; k < n; ++k) {
ResultType x_ki;
Expand Down
6 changes: 3 additions & 3 deletions flang/runtime/matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ inline RT_API_ATTRS void MatrixTimesMatrix(
SubscriptValue n, std::size_t xColumnByteStride = 0,
std::size_t yColumnByteStride = 0) {
using ResultType = CppTypeFor<RCAT, RKIND>;
std::memset(product, 0, rows * cols * sizeof *product);
Fortran::runtime::memset(product, 0, rows * cols * sizeof *product);
const XT *RESTRICT xp0{x};
for (SubscriptValue k{0}; k < n; ++k) {
ResultType *RESTRICT p{product};
Expand Down Expand Up @@ -153,7 +153,7 @@ inline RT_API_ATTRS void MatrixTimesVector(
SubscriptValue n, const XT *RESTRICT x, const YT *RESTRICT y,
std::size_t xColumnByteStride = 0) {
using ResultType = CppTypeFor<RCAT, RKIND>;
std::memset(product, 0, rows * sizeof *product);
Fortran::runtime::memset(product, 0, rows * sizeof *product);
[[maybe_unused]] const XT *RESTRICT xp0{x};
for (SubscriptValue k{0}; k < n; ++k) {
ResultType *RESTRICT p{product};
Expand Down Expand Up @@ -203,7 +203,7 @@ inline RT_API_ATTRS void VectorTimesMatrix(
SubscriptValue cols, const XT *RESTRICT x, const YT *RESTRICT y,
std::size_t yColumnByteStride = 0) {
using ResultType = CppTypeFor<RCAT, RKIND>;
std::memset(product, 0, cols * sizeof *product);
Fortran::runtime::memset(product, 0, cols * sizeof *product);
for (SubscriptValue k{0}; k < n; ++k) {
ResultType *RESTRICT p{product};
auto xv{static_cast<ResultType>(*x++)};
Expand Down
Loading

0 comments on commit 35f077a

Please sign in to comment.