[flang] [llvm] [Flang][OpenMP][Runtime] Minor Flang runtime for OpenMP AMDGPU (PR #152631)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 7 20:11:08 PDT 2025
https://github.com/agozillon created https://github.com/llvm/llvm-project/pull/152631
We have some modifications downstream to compile the flang runtime for amdgpu using clang OpenMP, some more hacky than others to workaround (hopefully temporary) compiler issues. The additions here are the non-hacky alterations.
Main changes:
* Create freestanding versions of memcpy, strlen and memmove, and replace std:: references with these so that we can default to std:: when it's available, or our own Flang implementation when it's not. * Wrap more bits and pieces of the library in declare target wrappers (RT_* macros). * Fix some warnings that'll pose issues with werror on, in this case having the namespace infront of variables passed to templates.
Another minor issues that'll likely still pop up depending on the program you're linking with is that abort will be undefined, it is perhaps possible to solve it with a freestanding implementation as with memcpy etc. but we end up with multiple definitions in this case. An alternative is to create an empty extern "c" version (which can be empty or forwrd on to the builtin).
Co-author: Dan Palermo Dan.Palermo at amd.com
>From a3304f09dc17be50d7389520acdb81cdd972114c Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Thu, 7 Aug 2025 12:20:30 -0500
Subject: [PATCH] [Flang][OpenMP] Minor Flang runtime for OpenMP AMDGPU
We have some modifications downstream to compile the flang runtime for amdgpu using clang OpenMP, some more
hacky than others to workaround (hopefully temporary) compiler issues. The additions here are the non-hacky
alterations.
Main changes:
* Create freestanding versions of memcpy, strlen and memmove, and replace std:: references with
these so that we can default to std:: when it's available, or our own Flang implementation when
it's not.
* Wrap more bits and pieces of the library in declare target wrappers (RT_* macros).
* Fix some warnings that'll pose issues with werror on, in this case having the namespace infront
of variables passed to templates.
Another minor issues that'll likely still pop up depending on the program you're linking with is that abort will be
undefined, it is perhaps possible to solve it with a freestanding implementation as with memcpy etc. but we end up
with multiple definitions in this case. An alternative is to create an empty extern "c" version (which can be empty
or forwrd on to the builtin).
Co-author: Dan Palermo Dan.Palermo at amd.com
---
flang-rt/include/flang-rt/runtime/buffer.h | 4 +-
.../include/flang-rt/runtime/descriptor.h | 2 +
.../flang-rt/runtime/format-implementation.h | 2 +-
flang-rt/include/flang-rt/runtime/tools.h | 4 +-
.../include/flang-rt/runtime/work-queue.h | 14 ++--
flang-rt/lib/runtime/array-constructor.cpp | 2 +-
flang-rt/lib/runtime/assign.cpp | 5 +-
flang-rt/lib/runtime/character.cpp | 10 +--
flang-rt/lib/runtime/command.cpp | 2 +-
flang-rt/lib/runtime/copy.cpp | 5 +-
flang-rt/lib/runtime/derived.cpp | 8 +-
flang-rt/lib/runtime/descriptor-io.cpp | 2 +-
flang-rt/lib/runtime/descriptor.cpp | 3 +-
flang-rt/lib/runtime/edit-input.cpp | 14 ++--
flang-rt/lib/runtime/extensions.cpp | 10 +--
flang-rt/lib/runtime/external-unit.cpp | 4 +-
flang-rt/lib/runtime/extrema.cpp | 6 +-
flang-rt/lib/runtime/internal-unit.cpp | 2 +-
flang-rt/lib/runtime/io-api-minimal.cpp | 2 +-
flang-rt/lib/runtime/io-error.cpp | 2 +-
flang-rt/lib/runtime/matmul-transpose.cpp | 4 +-
flang-rt/lib/runtime/matmul.cpp | 6 +-
flang-rt/lib/runtime/misc-intrinsic.cpp | 4 +-
flang-rt/lib/runtime/pseudo-unit.cpp | 2 +-
flang-rt/lib/runtime/ragged.cpp | 2 +-
flang-rt/lib/runtime/reduce.cpp | 6 +-
flang-rt/lib/runtime/stat.cpp | 6 +-
flang-rt/lib/runtime/temporary-stack.cpp | 2 +-
flang-rt/lib/runtime/time-intrinsic.cpp | 12 +--
flang-rt/lib/runtime/tools.cpp | 26 +++---
flang-rt/lib/runtime/transformational.cpp | 2 +-
flang-rt/lib/runtime/unit-map.cpp | 2 +-
flang-rt/lib/runtime/unit.cpp | 10 +--
flang-rt/lib/runtime/unit.h | 2 +
flang-rt/lib/runtime/work-queue.cpp | 8 +-
flang/include/flang/Common/constexpr-bitset.h | 3 +-
.../flang/Decimal/binary-floating-point.h | 5 +-
.../flang/Runtime/allocator-registry-consts.h | 4 +
.../flang/Runtime/freestanding-tools.h | 84 ++++++++++++++++++-
flang/include/flang/Runtime/stop.h | 2 +
40 files changed, 194 insertions(+), 101 deletions(-)
diff --git a/flang-rt/include/flang-rt/runtime/buffer.h b/flang-rt/include/flang-rt/runtime/buffer.h
index b5a9ce9e35e91..2791ac3ce728a 100644
--- a/flang-rt/include/flang-rt/runtime/buffer.h
+++ b/flang-rt/include/flang-rt/runtime/buffer.h
@@ -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;
diff --git a/flang-rt/include/flang-rt/runtime/descriptor.h b/flang-rt/include/flang-rt/runtime/descriptor.h
index e440690e40286..4c65abce782d3 100644
--- a/flang-rt/include/flang-rt/runtime/descriptor.h
+++ b/flang-rt/include/flang-rt/runtime/descriptor.h
@@ -32,8 +32,10 @@
#include <cstdlib>
#include <cstring>
+RT_OFFLOAD_VAR_GROUP_BEGIN
/// Value used for asyncObject when no specific stream is specified.
static constexpr std::int64_t *kNoAsyncObject = nullptr;
+RT_OFFLOAD_VAR_GROUP_END
namespace Fortran::runtime {
diff --git a/flang-rt/include/flang-rt/runtime/format-implementation.h b/flang-rt/include/flang-rt/runtime/format-implementation.h
index 85dc922bc31bc..ba2b9f28ae77f 100644
--- a/flang-rt/include/flang-rt/runtime/format-implementation.h
+++ b/flang-rt/include/flang-rt/runtime/format-implementation.h
@@ -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);
}
diff --git a/flang-rt/include/flang-rt/runtime/tools.h b/flang-rt/include/flang-rt/runtime/tools.h
index a1b96f41f4936..38621fe66e2b3 100644
--- a/flang-rt/include/flang-rt/runtime/tools.h
+++ b/flang-rt/include/flang-rt/runtime/tools.h
@@ -559,9 +559,9 @@ RT_API_ATTRS void CopyAndPad(
to[j] = static_cast<TO>(' ');
}
} else if (toChars <= fromChars) {
- std::memcpy(to, from, toChars * sizeof(TO));
+ Fortran::runtime::memcpy(to, from, toChars * sizeof(TO));
} else {
- std::memcpy(to, from, std::min(toChars, fromChars) * sizeof(TO));
+ Fortran::runtime::memcpy(to, from, std::min(toChars, fromChars) * sizeof(TO));
for (std::size_t j{fromChars}; j < toChars; ++j) {
to[j] = static_cast<TO>(' ');
}
diff --git a/flang-rt/include/flang-rt/runtime/work-queue.h b/flang-rt/include/flang-rt/runtime/work-queue.h
index 7d7f8ad991a57..d1ea036a8fc0a 100644
--- a/flang-rt/include/flang-rt/runtime/work-queue.h
+++ b/flang-rt/include/flang-rt/runtime/work-queue.h
@@ -127,8 +127,8 @@ class Elementwise {
const Descriptor &instance_, *from_{nullptr};
std::size_t elements_{instance_.InlineElements()};
std::size_t elementAt_{0};
- SubscriptValue subscripts_[common::maxRank];
- SubscriptValue fromSubscripts_[common::maxRank];
+ SubscriptValue subscripts_[maxRank];
+ SubscriptValue fromSubscripts_[maxRank];
};
// Base class for ticket workers that operate over derived type components.
@@ -162,7 +162,7 @@ class Componentwise {
const typeInfo::DerivedType &derived_;
std::size_t components_{0}, componentAt_{0};
const typeInfo::Component *component_{nullptr};
- StaticDescriptor<common::maxRank, true, 0> componentDescriptor_;
+ StaticDescriptor<maxRank, true, 0> componentDescriptor_;
private:
RT_API_ATTRS void GetFirstComponent() {
@@ -275,7 +275,7 @@ class InitializeCloneTicket
const Descriptor &clone_;
bool hasStat_{false};
const Descriptor *errMsg_{nullptr};
- StaticDescriptor<common::maxRank, true, 0> cloneComponentDescriptor_;
+ StaticDescriptor<maxRank, true, 0> cloneComponentDescriptor_;
};
// Implements derived type instance finalization
@@ -331,7 +331,7 @@ class AssignTicket : public ImmediateTicketRunner<AssignTicket> {
const Descriptor *from_{nullptr};
int flags_{0}; // enum AssignFlags
MemmoveFct memmoveFct_{nullptr};
- StaticDescriptor<common::maxRank, true, 0> tempDescriptor_;
+ StaticDescriptor<maxRank, true, 0> tempDescriptor_;
const typeInfo::DerivedType *declaredType_{nullptr};
const typeInfo::DerivedType *toDerived_{nullptr};
Descriptor *toDeallocate_{nullptr};
@@ -364,7 +364,7 @@ class DerivedAssignTicket
int flags_{0};
MemmoveFct memmoveFct_{nullptr};
Descriptor *deallocateAfter_{nullptr};
- StaticDescriptor<common::maxRank, true, 0> fromComponentDescriptor_;
+ StaticDescriptor<maxRank, true, 0> fromComponentDescriptor_;
};
namespace io::descr {
@@ -392,7 +392,7 @@ class DescriptorIoTicket
common::optional<typeInfo::SpecialBinding> nonTbpSpecial_;
const typeInfo::DerivedType *derived_{nullptr};
const typeInfo::SpecialBinding *special_{nullptr};
- StaticDescriptor<common::maxRank, true, 0> elementDescriptor_;
+ StaticDescriptor<maxRank, true, 0> elementDescriptor_;
};
template <io::Direction DIR>
diff --git a/flang-rt/lib/runtime/array-constructor.cpp b/flang-rt/lib/runtime/array-constructor.cpp
index 858fac7bf2b39..98f9503abeccc 100644
--- a/flang-rt/lib/runtime/array-constructor.cpp
+++ b/flang-rt/lib/runtime/array-constructor.cpp
@@ -173,7 +173,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;
}
diff --git a/flang-rt/lib/runtime/assign.cpp b/flang-rt/lib/runtime/assign.cpp
index 7cf4147a94a95..de599dbad72a3 100644
--- a/flang-rt/lib/runtime/assign.cpp
+++ b/flang-rt/lib/runtime/assign.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "flang/Runtime/assign.h"
+#include "flang/Runtime/stop.h"
#include "flang-rt/runtime/assign-impl.h"
#include "flang-rt/runtime/derived.h"
#include "flang-rt/runtime/descriptor.h"
@@ -279,7 +280,7 @@ RT_API_ATTRS int AssignTicket::Begin(WorkQueue &workQueue) {
if (mustDeallocateLHS) {
// Convert the LHS into a temporary, then make it look deallocated.
toDeallocate_ = &tempDescriptor_.descriptor();
- std::memcpy(
+ Fortran::runtime::memcpy(
reinterpret_cast<void *>(toDeallocate_), &to_, to_.SizeInBytes());
to_.set_base_addr(nullptr);
if (toDerived_ && (flags_ & NeedFinalization)) {
@@ -298,7 +299,7 @@ RT_API_ATTRS int AssignTicket::Begin(WorkQueue &workQueue) {
auto descBytes{from_->SizeInBytes()};
Descriptor &newFrom{tempDescriptor_.descriptor()};
persist_ = true; // tempDescriptor_ state must outlive child tickets
- std::memcpy(reinterpret_cast<void *>(&newFrom), from_, descBytes);
+ Fortran::runtime::memcpy(reinterpret_cast<void *>(&newFrom), from_, descBytes);
// Pretend the temporary descriptor is for an ALLOCATABLE
// entity, otherwise, the Deallocate() below will not
// free the descriptor memory.
diff --git a/flang-rt/lib/runtime/character.cpp b/flang-rt/lib/runtime/character.cpp
index f140d202e118e..0eda774e74a75 100644
--- a/flang-rt/lib/runtime/character.cpp
+++ b/flang-rt/lib/runtime/character.cpp
@@ -616,8 +616,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);
}
@@ -698,7 +698,7 @@ 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;
@@ -706,7 +706,7 @@ std::size_t RTDEF(CharacterAppend1)(char *lhs, std::size_t lhsBytes,
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);
}
}
@@ -838,7 +838,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);
}
}
diff --git a/flang-rt/lib/runtime/command.cpp b/flang-rt/lib/runtime/command.cpp
index a4e8e31ad0274..a60eee3841bd3 100644
--- a/flang-rt/lib/runtime/command.cpp
+++ b/flang-rt/lib/runtime/command.cpp
@@ -58,7 +58,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);
}
}
diff --git a/flang-rt/lib/runtime/copy.cpp b/flang-rt/lib/runtime/copy.cpp
index f990f46e0be66..ab16763d4d017 100644
--- a/flang-rt/lib/runtime/copy.cpp
+++ b/flang-rt/lib/runtime/copy.cpp
@@ -9,6 +9,7 @@
#include "copy.h"
#include "stack.h"
#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/tools.h"
#include "flang-rt/runtime/terminator.h"
#include "flang-rt/runtime/type-info.h"
#include "flang/Runtime/allocatable.h"
@@ -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;
}
@@ -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);
diff --git a/flang-rt/lib/runtime/derived.cpp b/flang-rt/lib/runtime/derived.cpp
index 2dddf079f91db..92e1b00810890 100644
--- a/flang-rt/lib/runtime/derived.cpp
+++ b/flang-rt/lib/runtime/derived.cpp
@@ -71,7 +71,7 @@ RT_API_ATTRS int InitializeTicket::Continue(WorkQueue &workQueue) {
// Explicit initialization of data pointers and
// non-allocatable non-automatic components
std::size_t bytes{component_->SizeInBytes(instance_)};
- std::memcpy(rawComponent, init, bytes);
+ Fortran::runtime::memcpy(rawComponent, init, bytes);
} else if (component_->genre() == typeInfo::Component::Genre::Pointer) {
// Data pointers without explicit initialization are established
// so that they are valid right-hand side targets of pointer
@@ -108,20 +108,20 @@ RT_API_ATTRS int InitializeTicket::Continue(WorkQueue &workQueue) {
chunk = done;
}
char *uninitialized{rawInstance + done * *stride};
- std::memcpy(uninitialized, rawInstance, chunk * *stride);
+ Fortran::runtime::memcpy(uninitialized, rawInstance, chunk * *stride);
done += chunk;
}
} else {
for (std::size_t done{1}; done < elements_; ++done) {
char *uninitialized{rawInstance + done * *stride};
- std::memcpy(uninitialized, rawInstance, elementBytes);
+ Fortran::runtime::memcpy(uninitialized, rawInstance, elementBytes);
}
}
} else { // one at a time with subscription
for (Elementwise::Advance(); !Elementwise::IsComplete();
Elementwise::Advance()) {
char *element{instance_.Element<char>(subscripts_)};
- std::memcpy(element, rawInstance, elementBytes);
+ Fortran::runtime::memcpy(element, rawInstance, elementBytes);
}
}
}
diff --git a/flang-rt/lib/runtime/descriptor-io.cpp b/flang-rt/lib/runtime/descriptor-io.cpp
index a60d0b90da467..bb5e46bf2c2a0 100644
--- a/flang-rt/lib/runtime/descriptor-io.cpp
+++ b/flang-rt/lib/runtime/descriptor-io.cpp
@@ -65,7 +65,7 @@ static RT_API_ATTRS Fortran::common::optional<bool> DefinedFormattedIo(
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");
diff --git a/flang-rt/lib/runtime/descriptor.cpp b/flang-rt/lib/runtime/descriptor.cpp
index 882870a570428..d7d8692cc7e94 100644
--- a/flang-rt/lib/runtime/descriptor.cpp
+++ b/flang-rt/lib/runtime/descriptor.cpp
@@ -15,6 +15,7 @@
#include "flang-rt/runtime/terminator.h"
#include "flang-rt/runtime/type-info.h"
#include "flang/Common/type-kinds.h"
+#include "flang/Runtime/freestanding-tools.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
@@ -26,7 +27,7 @@ RT_OFFLOAD_API_GROUP_BEGIN
RT_API_ATTRS Descriptor::Descriptor(const Descriptor &that) { *this = that; }
RT_API_ATTRS Descriptor &Descriptor::operator=(const Descriptor &that) {
- std::memcpy(reinterpret_cast<void *>(this), &that, that.SizeInBytes());
+ Fortran::runtime::memcpy(this, &that, that.SizeInBytes());
return *this;
}
diff --git a/flang-rt/lib/runtime/edit-input.cpp b/flang-rt/lib/runtime/edit-input.cpp
index 80cc085d99541..8a58d8aa3a9bf 100644
--- a/flang-rt/lib/runtime/edit-input.cpp
+++ b/flang-rt/lib/runtime/edit-input.cpp
@@ -110,7 +110,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)};
@@ -283,18 +283,18 @@ RT_API_ATTRS bool EditIntegerInput(IoStatementState &io, const DataEdit &edit,
auto shft{static_cast<int>(sizeof value - kind)};
if (!isHostLittleEndian && shft >= 0) {
auto shifted{value << (8 * shft)};
- std::memcpy(n, &shifted, kind);
+ Fortran::runtime::memcpy(n, &shifted, kind);
} else {
- std::memcpy(n, &value, kind); // a blank field means zero
+ Fortran::runtime::memcpy(n, &value, kind); // a blank field means zero
}
#else
auto shft{static_cast<int>(sizeof(value.low())) - kind};
// 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
}
#endif
io.GotChar(fastField.got());
@@ -1120,7 +1120,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++ = '?';
@@ -1147,7 +1147,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;
}
diff --git a/flang-rt/lib/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
index a24810b4f344a..57e25b913e3d6 100644
--- a/flang-rt/lib/runtime/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -148,7 +148,7 @@ uid_t RTNAME(GetUID)() {
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)
@@ -172,7 +172,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;
}
@@ -204,8 +204,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;
}
@@ -259,7 +259,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;
}
diff --git a/flang-rt/lib/runtime/external-unit.cpp b/flang-rt/lib/runtime/external-unit.cpp
index b8004d6315994..11d860360a5da 100644
--- a/flang-rt/lib/runtime/external-unit.cpp
+++ b/flang-rt/lib/runtime/external-unit.cpp
@@ -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'");
@@ -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();
}
diff --git a/flang-rt/lib/runtime/extrema.cpp b/flang-rt/lib/runtime/extrema.cpp
index 03e574a8fbff1..35db72a5a2f66 100644
--- a/flang-rt/lib/runtime/extrema.cpp
+++ b/flang-rt/lib/runtime/extrema.cpp
@@ -428,7 +428,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;
}
@@ -584,11 +584,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) {
diff --git a/flang-rt/lib/runtime/internal-unit.cpp b/flang-rt/lib/runtime/internal-unit.cpp
index e344b01e8b34e..1258e814c2285 100644
--- a/flang-rt/lib/runtime/internal-unit.cpp
+++ b/flang-rt/lib/runtime/internal-unit.cpp
@@ -72,7 +72,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;
diff --git a/flang-rt/lib/runtime/io-api-minimal.cpp b/flang-rt/lib/runtime/io-api-minimal.cpp
index fdf7183ed5176..c3c7e40529928 100644
--- a/flang-rt/lib/runtime/io-api-minimal.cpp
+++ b/flang-rt/lib/runtime/io-api-minimal.cpp
@@ -161,4 +161,4 @@ void std::__libcpp_verbose_abort(char const *format, ...) noexcept(
}
#endif
-RT_EXT_API_GROUP_END
+RT_EXT_API_GROUP_END
\ No newline at end of file
diff --git a/flang-rt/lib/runtime/io-error.cpp b/flang-rt/lib/runtime/io-error.cpp
index b350fb66fc25b..1a49a4f077f97 100644
--- a/flang-rt/lib/runtime/io-error.cpp
+++ b/flang-rt/lib/runtime/io-error.cpp
@@ -153,7 +153,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 {
diff --git a/flang-rt/lib/runtime/matmul-transpose.cpp b/flang-rt/lib/runtime/matmul-transpose.cpp
index c9e21502b629e..789f13c585ec5 100644
--- a/flang-rt/lib/runtime/matmul-transpose.cpp
+++ b/flang-rt/lib/runtime/matmul-transpose.cpp
@@ -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) {
@@ -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;
diff --git a/flang-rt/lib/runtime/matmul.cpp b/flang-rt/lib/runtime/matmul.cpp
index 5acb345725212..d409cb1458c90 100644
--- a/flang-rt/lib/runtime/matmul.cpp
+++ b/flang-rt/lib/runtime/matmul.cpp
@@ -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};
@@ -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};
@@ -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++)};
diff --git a/flang-rt/lib/runtime/misc-intrinsic.cpp b/flang-rt/lib/runtime/misc-intrinsic.cpp
index a8797f48fa667..535fbc35c326a 100644
--- a/flang-rt/lib/runtime/misc-intrinsic.cpp
+++ b/flang-rt/lib/runtime/misc-intrinsic.cpp
@@ -42,14 +42,14 @@ static RT_API_ATTRS void TransferImpl(Descriptor &result,
source.GetLowerBounds(sourceAt);
while (resultBytes > 0 && sourceElements > 0) {
std::size_t toMove{std::min(resultBytes, sourceElementBytes)};
- std::memcpy(to, source.Element<char>(sourceAt), toMove);
+ Fortran::runtime::memcpy(to, source.Element<char>(sourceAt), toMove);
to += toMove;
resultBytes -= toMove;
--sourceElements;
source.IncrementSubscripts(sourceAt);
}
if (resultBytes > 0) {
- std::memset(to, 0, resultBytes);
+ Fortran::runtime::memset(to, 0, resultBytes);
}
}
diff --git a/flang-rt/lib/runtime/pseudo-unit.cpp b/flang-rt/lib/runtime/pseudo-unit.cpp
index 7e1f3bc86b294..b0019200b74d0 100644
--- a/flang-rt/lib/runtime/pseudo-unit.cpp
+++ b/flang-rt/lib/runtime/pseudo-unit.cpp
@@ -132,7 +132,7 @@ std::size_t PseudoOpenFile::Write(FileOffset at, const char *buffer,
// TODO: use persistent string buffer that can be reallocated
// as needed, and only freed at destruction of *this.
auto string{SizedNew<char>{handler}(bytes + 1)};
- std::memcpy(string.get(), buffer, bytes);
+ Fortran::runtime::memcpy(string.get(), buffer, bytes);
string.get()[bytes] = '\0';
std::printf("%s", string.get());
return bytes;
diff --git a/flang-rt/lib/runtime/ragged.cpp b/flang-rt/lib/runtime/ragged.cpp
index dddc3ccdfd858..bd0644a101fca 100644
--- a/flang-rt/lib/runtime/ragged.cpp
+++ b/flang-rt/lib/runtime/ragged.cpp
@@ -40,7 +40,7 @@ RT_API_ATTRS RaggedArrayHeader *RaggedArrayAllocate(RaggedArrayHeader *header,
std::size_t bytes{static_cast<std::size_t>(elementSize * size)};
header->bufferPointer = AllocateMemoryOrCrash(terminator, bytes);
if (header->bufferPointer) {
- std::memset(header->bufferPointer, 0, bytes);
+ Fortran::runtime::memset(header->bufferPointer, 0, bytes);
}
return header;
} else {
diff --git a/flang-rt/lib/runtime/reduce.cpp b/flang-rt/lib/runtime/reduce.cpp
index 3c5e815e32d2b..54fd1d88c4d02 100644
--- a/flang-rt/lib/runtime/reduce.cpp
+++ b/flang-rt/lib/runtime/reduce.cpp
@@ -79,16 +79,16 @@ class BufferedReduceAccumulator {
activeTemp_ = 1 - activeTemp_;
} else {
activeTemp_ = 0;
- std::memcpy(&*temp_[activeTemp_], operand, elementBytes_);
+ Fortran::runtime::memcpy(&*temp_[activeTemp_], operand, elementBytes_);
}
return true;
}
template <typename A>
RT_API_ATTRS void GetResult(A *to, int /*zeroBasedDim*/ = -1) {
if (activeTemp_ >= 0) {
- std::memcpy(to, &*temp_[activeTemp_], elementBytes_);
+ Fortran::runtime::memcpy(to, &*temp_[activeTemp_], elementBytes_);
} else if (identity_) {
- std::memcpy(to, identity_, elementBytes_);
+ Fortran::runtime::memcpy(to, identity_, elementBytes_);
} else {
terminator_.Crash("REDUCE() without IDENTITY= has no result");
}
diff --git a/flang-rt/lib/runtime/stat.cpp b/flang-rt/lib/runtime/stat.cpp
index 322b7282b7024..2e34d07f713ed 100644
--- a/flang-rt/lib/runtime/stat.cpp
+++ b/flang-rt/lib/runtime/stat.cpp
@@ -84,10 +84,10 @@ RT_API_ATTRS int ToErrmsg(const Descriptor *errmsg, int stat) {
std::size_t bufferLength{errmsg->ElementBytes()};
std::size_t msgLength{Fortran::runtime::strlen(msg)};
if (msgLength >= bufferLength) {
- std::memcpy(buffer, msg, bufferLength);
+ Fortran::runtime::memcpy(buffer, msg, bufferLength);
} else {
- std::memcpy(buffer, msg, msgLength);
- std::memset(buffer + msgLength, ' ', bufferLength - msgLength);
+ Fortran::runtime::memcpy(buffer, msg, msgLength);
+ Fortran::runtime::memset(buffer + msgLength, ' ', bufferLength - msgLength);
}
}
}
diff --git a/flang-rt/lib/runtime/temporary-stack.cpp b/flang-rt/lib/runtime/temporary-stack.cpp
index 3f6fd8ee15a80..50270d797dbf0 100644
--- a/flang-rt/lib/runtime/temporary-stack.cpp
+++ b/flang-rt/lib/runtime/temporary-stack.cpp
@@ -97,7 +97,7 @@ void DescriptorStorage<COPY_VALUES>::resize(size_type newCapacity) {
// Avoid passing a null pointer, since it would result in an undefined
// behavior.
if (data_ != nullptr) {
- memcpy(newData, data_, capacity_ * sizeof(Descriptor *));
+ Fortran::runtime::memcpy(newData, data_, capacity_ * sizeof(Descriptor *));
FreeMemory(data_);
}
data_ = newData;
diff --git a/flang-rt/lib/runtime/time-intrinsic.cpp b/flang-rt/lib/runtime/time-intrinsic.cpp
index 8988817a40064..67729d00685e1 100644
--- a/flang-rt/lib/runtime/time-intrinsic.cpp
+++ b/flang-rt/lib/runtime/time-intrinsic.cpp
@@ -276,13 +276,13 @@ static void DateAndTimeUnavailable(Fortran::runtime::Terminator &terminator,
char *zone, std::size_t zoneChars,
const Fortran::runtime::Descriptor *values) {
if (date) {
- std::memset(date, static_cast<int>(' '), dateChars);
+ Fortran::runtime::memset(date, static_cast<int>(' '), dateChars);
}
if (time) {
- std::memset(time, static_cast<int>(' '), timeChars);
+ Fortran::runtime::memset(time, static_cast<int>(' '), timeChars);
}
if (zone) {
- std::memset(zone, static_cast<int>(' '), zoneChars);
+ Fortran::runtime::memset(zone, static_cast<int>(' '), zoneChars);
}
if (values) {
auto typeCode{values->type().GetCategoryAndKind()};
@@ -420,7 +420,7 @@ static void GetDateAndTime(Fortran::runtime::Terminator &terminator, char *date,
auto copyBufferAndPad{
[&](char *dest, std::size_t destChars, std::size_t len) {
auto copyLen{std::min(len, destChars)};
- std::memcpy(dest, buffer, copyLen);
+ Fortran::runtime::memcpy(dest, buffer, copyLen);
for (auto i{copyLen}; i < destChars; ++i) {
dest[i] = ' ';
}
@@ -525,8 +525,8 @@ void RTNAME(Etime)(const Descriptor *values, const Descriptor *time,
ULARGE_INTEGER userSystemTime;
ULARGE_INTEGER kernelSystemTime;
- memcpy(&userSystemTime, &userTime, sizeof(FILETIME));
- memcpy(&kernelSystemTime, &kernelTime, sizeof(FILETIME));
+ Fortran::runtime::memcpy(&userSystemTime, &userTime, sizeof(FILETIME));
+ Fortran::runtime::memcpy(&kernelSystemTime, &kernelTime, sizeof(FILETIME));
usrTime = ((double)(userSystemTime.QuadPart)) / 10000000.0;
sysTime = ((double)(kernelSystemTime.QuadPart)) / 10000000.0;
diff --git a/flang-rt/lib/runtime/tools.cpp b/flang-rt/lib/runtime/tools.cpp
index 24d05f369fcbe..60e360ee766ac 100644
--- a/flang-rt/lib/runtime/tools.cpp
+++ b/flang-rt/lib/runtime/tools.cpp
@@ -28,7 +28,7 @@ RT_API_ATTRS OwningPtr<char> SaveDefaultCharacter(
const char *s, std::size_t length, const Terminator &terminator) {
if (s) {
auto *p{static_cast<char *>(AllocateMemoryOrCrash(terminator, length + 1))};
- std::memcpy(p, s, length);
+ Fortran::runtime::memcpy(p, s, length);
p[length] = '\0';
return OwningPtr<char>{p};
} else {
@@ -75,10 +75,10 @@ RT_API_ATTRS void ToFortranDefaultCharacter(
char *to, std::size_t toLength, const char *from) {
std::size_t len{Fortran::runtime::strlen(from)};
if (len < toLength) {
- std::memcpy(to, from, len);
- std::memset(to + len, ' ', toLength - len);
+ Fortran::runtime::memcpy(to, from, len);
+ Fortran::runtime::memset(to + len, ' ', toLength - len);
} else {
- std::memcpy(to, from, toLength);
+ Fortran::runtime::memcpy(to, from, toLength);
}
}
@@ -127,10 +127,10 @@ RT_API_ATTRS void ShallowCopyDiscontiguousToDiscontiguous(
toIt.Advance(), fromIt.Advance()) {
// typeElementBytes == 1 when P is a char - the non-specialised case
if constexpr (typeElementBytes != 1) {
- std::memcpy(
+ Fortran::runtime::memcpy(
toIt.template Get<P>(), fromIt.template Get<P>(), typeElementBytes);
} else {
- std::memcpy(
+ Fortran::runtime::memcpy(
toIt.template Get<P>(), fromIt.template Get<P>(), elementBytes);
}
}
@@ -150,9 +150,9 @@ RT_API_ATTRS void ShallowCopyDiscontiguousToContiguous(
for (std::size_t n{to.Elements()}; n-- > 0;
toAt += elementBytes, fromIt.Advance()) {
if constexpr (typeElementBytes != 1) {
- std::memcpy(toAt, fromIt.template Get<P>(), typeElementBytes);
+ Fortran::runtime::memcpy(toAt, fromIt.template Get<P>(), typeElementBytes);
} else {
- std::memcpy(toAt, fromIt.template Get<P>(), elementBytes);
+ Fortran::runtime::memcpy(toAt, fromIt.template Get<P>(), elementBytes);
}
}
}
@@ -170,9 +170,9 @@ RT_API_ATTRS void ShallowCopyContiguousToDiscontiguous(
for (std::size_t n{to.Elements()}; n-- > 0;
toIt.Advance(), fromAt += elementBytes) {
if constexpr (typeElementBytes != 1) {
- std::memcpy(toIt.template Get<P>(), fromAt, typeElementBytes);
+ Fortran::runtime::memcpy(toIt.template Get<P>(), fromAt, typeElementBytes);
} else {
- std::memcpy(toIt.template Get<P>(), fromAt, elementBytes);
+ Fortran::runtime::memcpy(toIt.template Get<P>(), fromAt, elementBytes);
}
}
}
@@ -187,7 +187,7 @@ RT_API_ATTRS void ShallowCopyInner(const Descriptor &to, const Descriptor &from,
bool toIsContiguous, bool fromIsContiguous) {
if (toIsContiguous) {
if (fromIsContiguous) {
- std::memcpy(to.OffsetElement(), from.OffsetElement(),
+ Fortran::runtime::memcpy(to.OffsetElement(), from.OffsetElement(),
to.Elements() * to.ElementBytes());
} else {
ShallowCopyDiscontiguousToContiguous<P, RANK>(to, from);
@@ -277,7 +277,7 @@ RT_API_ATTRS char *EnsureNullTerminated(
char *str, std::size_t length, Terminator &terminator) {
if (runtime::memchr(str, '\0', length) == nullptr) {
char *newCmd{(char *)AllocateMemoryOrCrash(terminator, length + 1)};
- std::memcpy(newCmd, str, length);
+ Fortran::runtime::memcpy(newCmd, str, length);
newCmd[length] = '\0';
return newCmd;
} else {
@@ -309,7 +309,7 @@ RT_API_ATTRS std::int32_t CopyCharsToDescriptor(const Descriptor &value,
return ToErrmsg(errmsg, StatValueTooShort);
}
- std::memcpy(value.OffsetElement(offset), rawValue, toCopy);
+ Fortran::runtime::memcpy(value.OffsetElement(offset), rawValue, toCopy);
if (static_cast<std::int64_t>(rawValueLength) > toCopy) {
return ToErrmsg(errmsg, StatValueTooShort);
diff --git a/flang-rt/lib/runtime/transformational.cpp b/flang-rt/lib/runtime/transformational.cpp
index 3df314a4e966b..7db273f3f22a5 100644
--- a/flang-rt/lib/runtime/transformational.cpp
+++ b/flang-rt/lib/runtime/transformational.cpp
@@ -115,7 +115,7 @@ static RT_API_ATTRS void DefaultInitialize(
"not yet implemented: CHARACTER(KIND=%d) in EOSHIFT intrinsic", kind);
}
} else {
- std::memset(result.raw().base_addr, 0, bytes);
+ Fortran::runtime::memset(result.raw().base_addr, 0, bytes);
}
}
diff --git a/flang-rt/lib/runtime/unit-map.cpp b/flang-rt/lib/runtime/unit-map.cpp
index 41a03f3319d64..e9cddff81a91c 100644
--- a/flang-rt/lib/runtime/unit-map.cpp
+++ b/flang-rt/lib/runtime/unit-map.cpp
@@ -118,7 +118,7 @@ ExternalFileUnit *UnitMap::Find(const char *path, std::size_t pathLen) {
for (int j{0}; j < buckets_; ++j) {
for (Chain *p{bucket_[j].get()}; p; p = p->next.get()) {
if (p->unit.path() && p->unit.pathLength() == pathLen &&
- std::memcmp(p->unit.path(), path, pathLen) == 0) {
+ Fortran::runtime::memcmp(p->unit.path(), path, pathLen) == 0) {
return &p->unit;
}
}
diff --git a/flang-rt/lib/runtime/unit.cpp b/flang-rt/lib/runtime/unit.cpp
index 5f52fa2781db5..be46dddae0424 100644
--- a/flang-rt/lib/runtime/unit.cpp
+++ b/flang-rt/lib/runtime/unit.cpp
@@ -90,11 +90,11 @@ bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
CheckDirectAccess(handler);
WriteFrame(frameOffsetInFile_, recordOffsetInFrame_ + furthestAfter, handler);
if (positionInRecord > furthestPositionInRecord) {
- std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord, ' ',
+ Fortran::runtime::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord, ' ',
positionInRecord - furthestPositionInRecord);
}
char *to{Frame() + recordOffsetInFrame_ + positionInRecord};
- std::memcpy(to, data, bytes);
+ Fortran::runtime::memcpy(to, data, bytes);
if (swapEndianness_) {
SwapEndianness(to, bytes, elementBytes);
}
@@ -119,7 +119,7 @@ bool ExternalFileUnit::Receive(char *data, std::size_t bytes,
auto need{recordOffsetInFrame_ + furthestAfter};
auto got{ReadFrame(frameOffsetInFile_, need, handler)};
if (got >= need) {
- std::memcpy(data, Frame() + recordOffsetInFrame_ + positionInRecord, bytes);
+ Fortran::runtime::memcpy(data, Frame() + recordOffsetInFrame_ + positionInRecord, bytes);
if (swapEndianness_) {
SwapEndianness(data, bytes, elementBytes);
}
@@ -310,7 +310,7 @@ bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
// Pad remainder of fixed length record
WriteFrame(
frameOffsetInFile_, recordOffsetInFrame_ + *openRecl, handler);
- std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
+ Fortran::runtime::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
isUnformatted.value_or(false) ? 0 : ' ',
*openRecl - furthestPositionInRecord);
furthestPositionInRecord = *openRecl;
@@ -839,7 +839,7 @@ void ExternalFileUnit::PopChildIo(ChildIo &child) {
std::uint32_t ExternalFileUnit::ReadHeaderOrFooter(std::int64_t frameOffset) {
std::uint32_t word;
char *wordPtr{reinterpret_cast<char *>(&word)};
- std::memcpy(wordPtr, Frame() + frameOffset, sizeof word);
+ Fortran::runtime::memcpy(wordPtr, Frame() + frameOffset, sizeof word);
if (swapEndianness_) {
SwapEndianness(wordPtr, sizeof word, sizeof word);
}
diff --git a/flang-rt/lib/runtime/unit.h b/flang-rt/lib/runtime/unit.h
index f266a486bb708..08a214edfbf78 100644
--- a/flang-rt/lib/runtime/unit.h
+++ b/flang-rt/lib/runtime/unit.h
@@ -21,7 +21,9 @@
#include "flang-rt/runtime/lock.h"
#include "flang-rt/runtime/memory.h"
#include "flang-rt/runtime/terminator.h"
+RT_OFFLOAD_VAR_GROUP_BEGIN
#include "flang/Common/constexpr-bitset.h"
+RT_OFFLOAD_VAR_GROUP_END
#include "flang/Common/optional.h"
#include <cstdlib>
#include <cstring>
diff --git a/flang-rt/lib/runtime/work-queue.cpp b/flang-rt/lib/runtime/work-queue.cpp
index 42dbc9064b03b..9ae751ae3367a 100644
--- a/flang-rt/lib/runtime/work-queue.cpp
+++ b/flang-rt/lib/runtime/work-queue.cpp
@@ -14,7 +14,7 @@
namespace Fortran::runtime {
-#if !defined(RT_DEVICE_COMPILATION)
+#if !defined(RT_DEVICE_COMPILATION) && !defined(OMP_OFFLOAD_BUILD)
// FLANG_RT_DEBUG code is disabled when false.
static constexpr bool enableDebugOutput{false};
#endif
@@ -79,7 +79,7 @@ RT_API_ATTRS Ticket &WorkQueue::StartTicket() {
last_ = newTicket;
}
newTicket->ticket.begun = false;
-#if !defined(RT_DEVICE_COMPILATION)
+#if !defined(RT_DEVICE_COMPILATION) && !defined(OMP_OFFLOAD_BUILD)
if (enableDebugOutput &&
(executionEnvironment.internalDebugging &
ExecutionEnvironment::WorkQueue)) {
@@ -93,7 +93,7 @@ RT_API_ATTRS int WorkQueue::Run() {
while (last_) {
TicketList *at{last_};
insertAfter_ = last_;
-#if !defined(RT_DEVICE_COMPILATION)
+#if !defined(RT_DEVICE_COMPILATION) && !defined(OMP_OFFLOAD_BUILD)
if (enableDebugOutput &&
(executionEnvironment.internalDebugging &
ExecutionEnvironment::WorkQueue)) {
@@ -102,7 +102,7 @@ RT_API_ATTRS int WorkQueue::Run() {
}
#endif
int stat{at->ticket.Continue(*this)};
-#if !defined(RT_DEVICE_COMPILATION)
+#if !defined(RT_DEVICE_COMPILATION) && !defined(OMP_OFFLOAD_BUILD)
if (enableDebugOutput &&
(executionEnvironment.internalDebugging &
ExecutionEnvironment::WorkQueue)) {
diff --git a/flang/include/flang/Common/constexpr-bitset.h b/flang/include/flang/Common/constexpr-bitset.h
index 1aafb6eff84c6..e60ff520ec63a 100644
--- a/flang/include/flang/Common/constexpr-bitset.h
+++ b/flang/include/flang/Common/constexpr-bitset.h
@@ -21,7 +21,7 @@
#include <type_traits>
namespace Fortran::common {
-
+RT_OFFLOAD_VAR_GROUP_BEGIN
template <int BITS> class BitSet {
static_assert(BITS > 0 && BITS <= 128);
using Word = HostUnsignedIntType<(BITS <= 32 ? 32 : BITS)>;
@@ -143,5 +143,6 @@ template <int BITS> class BitSet {
private:
Word bits_{0};
};
+RT_OFFLOAD_VAR_GROUP_END
} // namespace Fortran::common
#endif // FORTRAN_COMMON_CONSTEXPR_BITSET_H_
diff --git a/flang/include/flang/Decimal/binary-floating-point.h b/flang/include/flang/Decimal/binary-floating-point.h
index 1e0cde97d98e6..b69ae66c2b290 100644
--- a/flang/include/flang/Decimal/binary-floating-point.h
+++ b/flang/include/flang/Decimal/binary-floating-point.h
@@ -15,6 +15,7 @@
#include "flang/Common/api-attrs.h"
#include "flang/Common/real.h"
#include "flang/Common/uint128.h"
+#include "flang/Runtime/freestanding-tools.h"
#include <cinttypes>
#include <climits>
#include <cstring>
@@ -32,6 +33,7 @@ enum FortranRounding {
template <int BINARY_PRECISION> class BinaryFloatingPointNumber {
public:
+ RT_OFFLOAD_VAR_GROUP_BEGIN
static constexpr common::RealCharacteristics realChars{BINARY_PRECISION};
static constexpr int binaryPrecision{BINARY_PRECISION};
static constexpr int bits{realChars.bits};
@@ -47,7 +49,6 @@ template <int BINARY_PRECISION> class BinaryFloatingPointNumber {
using RawType = common::HostUnsignedIntType<bits>;
static_assert(CHAR_BIT * sizeof(RawType) >= bits);
- RT_OFFLOAD_VAR_GROUP_BEGIN
static constexpr RawType significandMask{(RawType{1} << significandBits) - 1};
constexpr RT_API_ATTRS BinaryFloatingPointNumber() {} // zero
@@ -68,7 +69,7 @@ template <int BINARY_PRECISION> class BinaryFloatingPointNumber {
template <typename A>
explicit constexpr RT_API_ATTRS BinaryFloatingPointNumber(A x) {
static_assert(sizeof raw_ <= sizeof x);
- std::memcpy(reinterpret_cast<void *>(&raw_),
+ Fortran::runtime::memcpy(reinterpret_cast<void *>(&raw_),
reinterpret_cast<const void *>(&x), sizeof raw_);
}
diff --git a/flang/include/flang/Runtime/allocator-registry-consts.h b/flang/include/flang/Runtime/allocator-registry-consts.h
index 70735c2fc7a71..a5f52749e945d 100644
--- a/flang/include/flang/Runtime/allocator-registry-consts.h
+++ b/flang/include/flang/Runtime/allocator-registry-consts.h
@@ -9,6 +9,8 @@
#ifndef FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_CONSTS_H_
#define FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_CONSTS_H_
+RT_OFFLOAD_VAR_GROUP_BEGIN
+
static constexpr unsigned kDefaultAllocator = 0;
// Allocator used for CUF
@@ -17,4 +19,6 @@ static constexpr unsigned kDeviceAllocatorPos = 2;
static constexpr unsigned kManagedAllocatorPos = 3;
static constexpr unsigned kUnifiedAllocatorPos = 4;
+RT_OFFLOAD_VAR_GROUP_END
+
#endif /* FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_CONSTS_H_ */
diff --git a/flang/include/flang/Runtime/freestanding-tools.h b/flang/include/flang/Runtime/freestanding-tools.h
index 3a492c1f320d0..77084cd8aaa33 100644
--- a/flang/include/flang/Runtime/freestanding-tools.h
+++ b/flang/include/flang/Runtime/freestanding-tools.h
@@ -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
@@ -63,6 +73,24 @@
#define STD_TOUPPER_UNSUPPORTED 1
#endif
+#if defined(OMP_OFFLOAD_BUILD) || defined(OMP_NOHOST_BUILD)
+#define STD_FILL_N_UNSUPPORTED 1
+#define STD_MEMSET_USE_BUILTIN 1
+#define STD_MEMSET_UNSUPPORTED 1
+#define STD_MEMCPY_USE_BUILTIN 1
+#define STD_MEMCPY_UNSUPPORTED 1
+#define STD_MEMMOVE_UNSUPPORTED 1
+#define STD_STRLEN_UNSUPPORTED 1
+#define STD_MEMCMP_UNSUPPORTED 1
+#define STD_REALLOC_UNSUPPORTED 1
+#define STD_MEMCHR_UNSUPPORTED 1
+#define STD_STRCPY_UNSUPPORTED 1
+#define STD_STRCMP_UNSUPPORTED 1
+#define STD_TOUPPER_UNSUPPORTED 1
+#define STD_ABORT_USE_BUILTIN 1
+#define STD_ABORT_UNSUPPORTED 1
+#endif
+
namespace Fortran::runtime {
#if STD_FILL_N_UNSUPPORTED
@@ -79,7 +107,52 @@ fill_n(A *start, std::size_t count, const B &value) {
using std::fill_n;
#endif // !STD_FILL_N_UNSUPPORTED
-#if STD_MEMMOVE_UNSUPPORTED
+#if STD_MEMSET_USE_BUILTIN
+static inline RT_API_ATTRS void memset(
+ void *dest, unsigned char value, std::size_t count) {
+ __builtin_memset(dest, value, count);
+}
+#elif STD_MEMSET_UNSUPPORTED
+static inline RT_API_ATTRS void memset(
+ void *dest, unsigned char value, std::size_t count) {
+ char *to{reinterpret_cast<char *>(dest)};
+ while (count--) {
+ *to++ = value;
+ }
+ return;
+}
+#else
+using std::memset;
+#endif
+
+#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);
+}
+#elif STD_MEMCPY_UNSUPPORTED
+static inline RT_API_ATTRS void memcpy(
+ void *dest, const void *src, std::size_t count) {
+ char *to{reinterpret_cast<char *>(dest)};
+ const char *from{reinterpret_cast<const char *>(src)};
+ if (to == from) {
+ return;
+ }
+ while (count--) {
+ *to++ = *from++;
+ }
+ return;
+}
+#else
+using std::memcpy;
+#endif
+
+#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);
+}
+#elif STD_MEMMOVE_UNSUPPORTED
// Provides alternative implementation for std::memmove(), if
// it is not supported.
static inline RT_API_ATTRS void *memmove(
@@ -91,7 +164,7 @@ static inline RT_API_ATTRS void *memmove(
return dest;
}
if (to + count <= from || from + count <= to) {
- std::memcpy(dest, src, count);
+ memcpy(dest, src, count);
} else if (to < from) {
while (count--) {
*to++ = *from++;
@@ -112,13 +185,18 @@ using std::memmove;
using MemmoveFct = void *(*)(void *, const void *, std::size_t);
#ifdef RT_DEVICE_COMPILATION
+[[maybe_unused]]
static RT_API_ATTRS void *MemmoveWrapper(
void *dest, const void *src, std::size_t count) {
return Fortran::runtime::memmove(dest, src, count);
}
#endif
-#if STD_STRLEN_UNSUPPORTED
+#if STD_STRLEN_USE_BUILTIN
+static inline RT_API_ATTRS std::size_t strlen(const char *str) {
+ return __builtin_strlen(str);
+}
+#elif STD_STRLEN_UNSUPPORTED
// Provides alternative implementation for std::strlen(), if
// it is not supported.
static inline RT_API_ATTRS std::size_t strlen(const char *str) {
diff --git a/flang/include/flang/Runtime/stop.h b/flang/include/flang/Runtime/stop.h
index 4ddc5cf49ec8f..81c28904efcbe 100644
--- a/flang/include/flang/Runtime/stop.h
+++ b/flang/include/flang/Runtime/stop.h
@@ -30,7 +30,9 @@ NORETURN void RTNAME(ProgramEndStatement)(NO_ARGUMENTS);
// Extensions
NORETURN void RTNAME(Exit)(int status DEFAULT_VALUE(EXIT_SUCCESS));
+RT_OFFLOAD_API_GROUP_BEGIN
NORETURN void RTNAME(Abort)(NO_ARGUMENTS);
+RT_OFFLOAD_API_GROUP_END
void FORTRAN_PROCEDURE_NAME(backtrace)(NO_ARGUMENTS);
// Crash with an error message when the program dynamically violates a Fortran
More information about the llvm-commits
mailing list