Skip to content

Commit 7b8f790

Browse files
committed
Addressed review comments.
1 parent a42a04c commit 7b8f790

File tree

6 files changed

+32
-29
lines changed

6 files changed

+32
-29
lines changed

flang/include/flang/Runtime/memory.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ class Terminator;
2323

2424
[[nodiscard]] RT_API_ATTRS void *AllocateMemoryOrCrash(
2525
const Terminator &, std::size_t bytes);
26-
template <typename A> [[nodiscard]] A &AllocateOrCrash(const Terminator &t) {
26+
template <typename A>
27+
[[nodiscard]] RT_API_ATTRS A &AllocateOrCrash(const Terminator &t) {
2728
return *reinterpret_cast<A *>(AllocateMemoryOrCrash(t, sizeof(A)));
2829
}
30+
RT_API_ATTRS void *ReallocateMemoryOrCrash(
31+
const Terminator &, void *ptr, std::size_t newByteSize);
2932
RT_API_ATTRS void FreeMemory(void *);
3033
template <typename A> RT_API_ATTRS void FreeMemory(A *p) {
3134
FreeMemory(reinterpret_cast<void *>(p));
3235
}
33-
template <typename A> void FreeMemoryAndNullify(A *&p) {
36+
template <typename A> RT_API_ATTRS void FreeMemoryAndNullify(A *&p) {
3437
FreeMemory(p);
3538
p = nullptr;
3639
}

flang/runtime/array-constructor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ static RT_API_ATTRS void AllocateOrReallocateVectorIfNeeded(
7474
// realloc is undefined with zero new size and ElementBytes() may be null
7575
// if the character length is null, or if "from" is a zero sized array.
7676
if (newByteSize > 0) {
77-
void *p{Fortran::runtime::realloc(to.raw().base_addr, newByteSize)};
78-
RUNTIME_CHECK(terminator, p);
77+
void *p{ReallocateMemoryOrCrash(
78+
terminator, to.raw().base_addr, newByteSize)};
7979
to.set_base_addr(p);
8080
}
8181
vector.actualAllocationSize = requestedAllocationSize;

flang/runtime/dot-product.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ static inline RT_API_ATTRS CppTypeFor<RCAT, RKIND> DoDotProduct(
9191
for (SubscriptValue j{0}; j < n; ++j) {
9292
// std::conj() may instantiate its argument twice,
9393
// so xp has to be incremented separately.
94+
// This is a workaround for an alleged bug in clang,
95+
// that shows up as:
96+
// warning: multiple unsequenced modifications to 'xp'
9497
accum += std::conj(static_cast<AccumType>(*xp)) *
9598
static_cast<AccumType>(*yp++);
9699
xp++;

flang/runtime/freestanding-tools.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@
4242
#define STD_REALLOC_UNSUPPORTED 1
4343
#endif
4444

45-
#if !defined(STD_CALLOC_UNSUPPORTED) && \
46-
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
47-
#define STD_CALLOC_UNSUPPORTED 1
48-
#endif
49-
5045
namespace Fortran::runtime {
5146

5247
#if STD_FILL_N_UNSUPPORTED
@@ -129,7 +124,7 @@ using std::memcmp;
129124
#endif // !STD_MEMCMP_UNSUPPORTED
130125

131126
#if STD_REALLOC_UNSUPPORTED
132-
static inline RT_API_ATTRS void *realloc(void *ptr, std::size_t new_size) {
127+
static inline RT_API_ATTRS void *realloc(void *ptr, std::size_t newByteSize) {
133128
// Return nullptr and let the callers assert that.
134129
// TODO: we can provide a straightforward implementation
135130
// via malloc/memcpy/free.
@@ -139,19 +134,5 @@ static inline RT_API_ATTRS void *realloc(void *ptr, std::size_t new_size) {
139134
using std::realloc;
140135
#endif // !STD_REALLOC_UNSUPPORTED
141136

142-
#if STD_CALLOC_UNSUPPORTED
143-
static inline RT_API_ATTRS void *calloc(std::size_t num, std::size_t size) {
144-
std::size_t bytes{num * size};
145-
void *ptr{nullptr};
146-
if (bytes != 0) {
147-
ptr = std::malloc(bytes);
148-
ptr = std::memset(ptr, 0, bytes);
149-
}
150-
return ptr;
151-
}
152-
#else // !STD_CALLOC_UNSUPPORTED
153-
using std::calloc;
154-
#endif // !STD_CALLOC_UNSUPPORTED
155-
156137
} // namespace Fortran::runtime
157138
#endif // FORTRAN_RUNTIME_FREESTANDING_TOOLS_H_

flang/runtime/memory.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "flang/Runtime/memory.h"
1010
#include "terminator.h"
11+
#include "tools.h"
1112
#include <cstdlib>
1213

1314
namespace Fortran::runtime {
@@ -26,6 +27,19 @@ RT_API_ATTRS void *AllocateMemoryOrCrash(
2627
return nullptr;
2728
}
2829

30+
RT_API_ATTRS void *ReallocateMemoryOrCrash(
31+
const Terminator &terminator, void *ptr, std::size_t newByteSize) {
32+
if (void *p{Fortran::runtime::realloc(ptr, newByteSize)}) {
33+
return p;
34+
}
35+
if (newByteSize > 0) {
36+
terminator.Crash("Fortran runtime internal error: memory realloc returned "
37+
"null, needed %zd bytes",
38+
newByteSize);
39+
}
40+
return nullptr;
41+
}
42+
2943
RT_API_ATTRS void FreeMemory(void *p) { std::free(p); }
3044

3145
RT_OFFLOAD_VAR_GROUP_END

flang/runtime/ragged.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ RT_API_ATTRS RaggedArrayHeader *RaggedArrayAllocate(RaggedArrayHeader *header,
3434
header->flags = (rank << 1) | isHeader;
3535
header->extentPointer = extentVector;
3636
if (isHeader) {
37-
header->bufferPointer =
38-
Fortran::runtime::calloc(sizeof(RaggedArrayHeader), size);
39-
} else {
40-
header->bufferPointer =
41-
static_cast<void *>(Fortran::runtime::calloc(elementSize, size));
37+
elementSize = sizeof(RaggedArrayHeader);
38+
}
39+
Terminator terminator{__FILE__, __LINE__};
40+
std::size_t bytes{static_cast<std::size_t>(elementSize * size)};
41+
header->bufferPointer = AllocateMemoryOrCrash(terminator, bytes);
42+
if (header->bufferPointer) {
43+
std::memset(header->bufferPointer, 0, bytes);
4244
}
4345
return header;
4446
} else {

0 commit comments

Comments
 (0)