Skip to content

Commit d5880e8

Browse files
agozillongithub-actions[bot]
authored andcommitted
Automerge: [Flang][OpenMP][Runtime] Minor Flang runtime for OpenMP AMDGPU modifications (#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 [email protected]
2 parents b62b316 + 30d2cb5 commit d5880e8

38 files changed

+191
-97
lines changed

flang-rt/include/flang-rt/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+
runtime::memcpy(buffer_, old + start_, chunk);
162+
runtime::memcpy(buffer_ + chunk, old, length_ - chunk);
163163
FreeMemory(old);
164164
}
165165
start_ = 0;

flang-rt/include/flang-rt/runtime/descriptor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
#include <cstdlib>
3333
#include <cstring>
3434

35+
RT_OFFLOAD_VAR_GROUP_BEGIN
3536
/// Value used for asyncObject when no specific stream is specified.
3637
static constexpr std::int64_t *kNoAsyncObject = nullptr;
38+
RT_OFFLOAD_VAR_GROUP_END
3739

3840
namespace Fortran::runtime {
3941

flang-rt/include/flang-rt/runtime/format-implementation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ RT_API_ATTRS FormatControl<CONTEXT>::FormatControl(const Terminator &terminator,
4949
SubscriptValue at[maxRank];
5050
formatDescriptor->GetLowerBounds(at);
5151
for (std::size_t j{0}; j < elements; ++j) {
52-
std::memcpy(p, formatDescriptor->Element<char>(at), elementBytes);
52+
runtime::memcpy(p, formatDescriptor->Element<char>(at), elementBytes);
5353
p += elementBytes;
5454
formatDescriptor->IncrementSubscripts(at);
5555
}

flang-rt/include/flang-rt/runtime/tools.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,9 @@ RT_API_ATTRS void CopyAndPad(
560560
to[j] = static_cast<TO>(' ');
561561
}
562562
} else if (toChars <= fromChars) {
563-
std::memcpy(to, from, toChars * sizeof(TO));
563+
runtime::memcpy(to, from, toChars * sizeof(TO));
564564
} else {
565-
std::memcpy(to, from, std::min(toChars, fromChars) * sizeof(TO));
565+
runtime::memcpy(to, from, std::min(toChars, fromChars) * sizeof(TO));
566566
for (std::size_t j{fromChars}; j < toChars; ++j) {
567567
to[j] = static_cast<TO>(' ');
568568
}

flang-rt/lib/runtime/array-constructor.cpp

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

flang-rt/lib/runtime/assign.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ RT_API_ATTRS int AssignTicket::Begin(WorkQueue &workQueue) {
288288
if (mustDeallocateLHS) {
289289
// Convert the LHS into a temporary, then make it look deallocated.
290290
toDeallocate_ = &tempDescriptor_.descriptor();
291-
std::memcpy(
291+
runtime::memcpy(
292292
reinterpret_cast<void *>(toDeallocate_), &to_, to_.SizeInBytes());
293293
to_.set_base_addr(nullptr);
294294
if (toDerived_ && (flags_ & NeedFinalization)) {
@@ -307,7 +307,7 @@ RT_API_ATTRS int AssignTicket::Begin(WorkQueue &workQueue) {
307307
auto descBytes{from_->SizeInBytes()};
308308
Descriptor &newFrom{tempDescriptor_.descriptor()};
309309
persist_ = true; // tempDescriptor_ state must outlive child tickets
310-
std::memcpy(reinterpret_cast<void *>(&newFrom), from_, descBytes);
310+
runtime::memcpy(reinterpret_cast<void *>(&newFrom), from_, descBytes);
311311
// Pretend the temporary descriptor is for an ALLOCATABLE
312312
// entity, otherwise, the Deallocate() below will not
313313
// free the descriptor memory.

flang-rt/lib/runtime/character.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,8 @@ void RTDEF(CharacterConcatenate)(Descriptor &accumulator,
616616
from.GetLowerBounds(fromAt);
617617
for (; elements-- > 0;
618618
to += newBytes, p += oldBytes, from.IncrementSubscripts(fromAt)) {
619-
std::memcpy(to, p, oldBytes);
620-
std::memcpy(to + oldBytes, from.Element<char>(fromAt), fromBytes);
619+
runtime::memcpy(to, p, oldBytes);
620+
runtime::memcpy(to + oldBytes, from.Element<char>(fromAt), fromBytes);
621621
}
622622
FreeMemory(old);
623623
}
@@ -698,15 +698,15 @@ void RTDEF(CharacterCompare)(
698698
std::size_t RTDEF(CharacterAppend1)(char *lhs, std::size_t lhsBytes,
699699
std::size_t offset, const char *rhs, std::size_t rhsBytes) {
700700
if (auto n{std::min(lhsBytes - offset, rhsBytes)}) {
701-
std::memcpy(lhs + offset, rhs, n);
701+
runtime::memcpy(lhs + offset, rhs, n);
702702
offset += n;
703703
}
704704
return offset;
705705
}
706706

707707
void RTDEF(CharacterPad1)(char *lhs, std::size_t bytes, std::size_t offset) {
708708
if (bytes > offset) {
709-
std::memset(lhs + offset, ' ', bytes - offset);
709+
runtime::memset(lhs + offset, ' ', bytes - offset);
710710
}
711711
}
712712

@@ -838,7 +838,7 @@ void RTDEF(Repeat)(Descriptor &result, const Descriptor &string,
838838
}
839839
const char *from{string.OffsetElement()};
840840
for (char *to{result.OffsetElement()}; ncopies-- > 0; to += origBytes) {
841-
std::memcpy(to, from, origBytes);
841+
runtime::memcpy(to, from, origBytes);
842842
}
843843
}
844844

flang-rt/lib/runtime/command.cpp

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

5959
static void FillWithSpaces(const Descriptor &value, std::size_t offset = 0) {
6060
if (offset < value.ElementBytes()) {
61-
std::memset(
61+
runtime::memset(
6262
value.OffsetElement(offset), ' ', value.ElementBytes() - offset);
6363
}
6464
}

flang-rt/lib/runtime/copy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "flang-rt/runtime/terminator.h"
1313
#include "flang-rt/runtime/type-info.h"
1414
#include "flang/Runtime/allocatable.h"
15+
#include "flang/Runtime/freestanding-tools.h"
16+
1517
#include <cstring>
1618

1719
namespace Fortran::runtime {
@@ -101,7 +103,7 @@ RT_API_ATTRS void CopyElement(const Descriptor &to, const SubscriptValue toAt[],
101103
char *toPtr{to.Element<char>(toAt)};
102104
char *fromPtr{from.Element<char>(fromAt)};
103105
RUNTIME_CHECK(terminator, to.ElementBytes() == from.ElementBytes());
104-
std::memcpy(toPtr, fromPtr, to.ElementBytes());
106+
runtime::memcpy(toPtr, fromPtr, to.ElementBytes());
105107
return;
106108
}
107109

@@ -148,7 +150,7 @@ RT_API_ATTRS void CopyElement(const Descriptor &to, const SubscriptValue toAt[],
148150
// Moreover, if we came here from an Component::Genre::Data component,
149151
// all the per-element copies are redundant, because the parent
150152
// has already been copied as a whole.
151-
std::memcpy(toPtr, fromPtr, curTo.ElementBytes());
153+
runtime::memcpy(toPtr, fromPtr, curTo.ElementBytes());
152154
--elements;
153155
if (elements != 0) {
154156
currentCopy.IncrementSubscripts(terminator);

flang-rt/lib/runtime/derived.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ RT_API_ATTRS int InitializeTicket::Continue(WorkQueue &workQueue) {
7171
// Explicit initialization of data pointers and
7272
// non-allocatable non-automatic components
7373
std::size_t bytes{component_->SizeInBytes(instance_)};
74-
std::memcpy(rawComponent, init, bytes);
74+
runtime::memcpy(rawComponent, init, bytes);
7575
} else if (component_->genre() == typeInfo::Component::Genre::Pointer) {
7676
// Data pointers without explicit initialization are established
7777
// so that they are valid right-hand side targets of pointer
@@ -108,20 +108,20 @@ RT_API_ATTRS int InitializeTicket::Continue(WorkQueue &workQueue) {
108108
chunk = done;
109109
}
110110
char *uninitialized{rawInstance + done * *stride};
111-
std::memcpy(uninitialized, rawInstance, chunk * *stride);
111+
runtime::memcpy(uninitialized, rawInstance, chunk * *stride);
112112
done += chunk;
113113
}
114114
} else {
115115
for (std::size_t done{1}; done < elements_; ++done) {
116116
char *uninitialized{rawInstance + done * *stride};
117-
std::memcpy(uninitialized, rawInstance, elementBytes);
117+
runtime::memcpy(uninitialized, rawInstance, elementBytes);
118118
}
119119
}
120120
} else { // one at a time with subscription
121121
for (Elementwise::Advance(); !Elementwise::IsComplete();
122122
Elementwise::Advance()) {
123123
char *element{instance_.Element<char>(subscripts_)};
124-
std::memcpy(element, rawInstance, elementBytes);
124+
runtime::memcpy(element, rawInstance, elementBytes);
125125
}
126126
}
127127
}

0 commit comments

Comments
 (0)