From a1f81d24c44da15071196faa9e8d466bcbbd7e97 Mon Sep 17 00:00:00 2001 From: Valentin Clement Date: Wed, 30 Oct 2024 11:52:07 -0700 Subject: [PATCH 1/3] [flang][runtime] Allow different memmov function in assign --- flang/include/flang/Runtime/assign.h | 23 +++++++++++++++++++++++ flang/runtime/assign.cpp | 27 ++++++++------------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/flang/include/flang/Runtime/assign.h b/flang/include/flang/Runtime/assign.h index a1cc9eaf4355f..ddd4d46f99ce3 100644 --- a/flang/include/flang/Runtime/assign.h +++ b/flang/include/flang/Runtime/assign.h @@ -24,11 +24,34 @@ #define FORTRAN_RUNTIME_ASSIGN_H_ #include "flang/Runtime/entry-names.h" +#include "flang/Runtime/freestanding-tools.h" namespace Fortran::runtime { class Descriptor; +class Terminator; + +enum AssignFlags { + NoAssignFlags = 0, + MaybeReallocate = 1 << 0, + NeedFinalization = 1 << 1, + CanBeDefinedAssignment = 1 << 2, + ComponentCanBeDefinedAssignment = 1 << 3, + ExplicitLengthCharacterLHS = 1 << 4, + PolymorphicLHS = 1 << 5, + DeallocateLHS = 1 << 6 +}; + +using MemmoveFct = void *(*)(void *, const void *, std::size_t); + +static void *MemmoveWrapper(void *dest, const void *src, std::size_t count) { + return Fortran::runtime::memmove(dest, src, count); +} + +RT_API_ATTRS void Assign(Descriptor &to, const Descriptor &from, + Terminator &terminator, int flags, MemmoveFct memmoveFct = &MemmoveWrapper); extern "C" { + // API for lowering assignment void RTDECL(Assign)(Descriptor &to, const Descriptor &from, const char *sourceFile = nullptr, int sourceLine = 0); diff --git a/flang/runtime/assign.cpp b/flang/runtime/assign.cpp index d558ada51cd21..8f31fc4d12716 100644 --- a/flang/runtime/assign.cpp +++ b/flang/runtime/assign.cpp @@ -17,17 +17,6 @@ namespace Fortran::runtime { -enum AssignFlags { - NoAssignFlags = 0, - MaybeReallocate = 1 << 0, - NeedFinalization = 1 << 1, - CanBeDefinedAssignment = 1 << 2, - ComponentCanBeDefinedAssignment = 1 << 3, - ExplicitLengthCharacterLHS = 1 << 4, - PolymorphicLHS = 1 << 5, - DeallocateLHS = 1 << 6 -}; - // Predicate: is the left-hand side of an assignment an allocated allocatable // that must be deallocated? static inline RT_API_ATTRS bool MustDeallocateLHS( @@ -250,8 +239,8 @@ static RT_API_ATTRS void BlankPadCharacterAssignment(Descriptor &to, // of elements, but their shape need not to conform (the assignment is done in // element sequence order). This facilitates some internal usages, like when // dealing with array constructors. -RT_API_ATTRS static void Assign( - Descriptor &to, const Descriptor &from, Terminator &terminator, int flags) { +RT_API_ATTRS void Assign(Descriptor &to, const Descriptor &from, + Terminator &terminator, int flags, MemmoveFct memmoveFct) { bool mustDeallocateLHS{(flags & DeallocateLHS) || MustDeallocateLHS(to, from, terminator, flags)}; DescriptorAddendum *toAddendum{to.Addendum()}; @@ -423,14 +412,14 @@ RT_API_ATTRS static void Assign( Assign(toCompDesc, fromCompDesc, terminator, nestedFlags); } else { // Component has intrinsic type; simply copy raw bytes std::size_t componentByteSize{comp.SizeInBytes(to)}; - Fortran::runtime::memmove(to.Element(toAt) + comp.offset(), + memmoveFct(to.Element(toAt) + comp.offset(), from.Element(fromAt) + comp.offset(), componentByteSize); } break; case typeInfo::Component::Genre::Pointer: { std::size_t componentByteSize{comp.SizeInBytes(to)}; - Fortran::runtime::memmove(to.Element(toAt) + comp.offset(), + memmoveFct(to.Element(toAt) + comp.offset(), from.Element(fromAt) + comp.offset(), componentByteSize); } break; @@ -476,14 +465,14 @@ RT_API_ATTRS static void Assign( const auto &procPtr{ *procPtrDesc.ZeroBasedIndexedElement( k)}; - Fortran::runtime::memmove(to.Element(toAt) + procPtr.offset, + memmoveFct(to.Element(toAt) + procPtr.offset, from.Element(fromAt) + procPtr.offset, sizeof(typeInfo::ProcedurePointer)); } } } else { // intrinsic type, intrinsic assignment if (isSimpleMemmove()) { - Fortran::runtime::memmove(to.raw().base_addr, from.raw().base_addr, + memmoveFct(to.raw().base_addr, from.raw().base_addr, toElements * toElementBytes); } else if (toElementBytes > fromElementBytes) { // blank padding switch (to.type().raw()) { @@ -507,8 +496,8 @@ RT_API_ATTRS static void Assign( } else { // elemental copies, possibly with character truncation for (std::size_t n{toElements}; n-- > 0; to.IncrementSubscripts(toAt), from.IncrementSubscripts(fromAt)) { - Fortran::runtime::memmove(to.Element(toAt), - from.Element(fromAt), toElementBytes); + memmoveFct(to.Element(toAt), from.Element(fromAt), + toElementBytes); } } } From 69dcd7af8600c2438996b2d385f3710317f29130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Clement=20=28=E3=83=90=E3=83=AC=E3=83=B3?= =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=B3=20=E3=82=AF=E3=83=AC=E3=83=A1?= =?UTF-8?q?=E3=83=B3=29?= Date: Thu, 31 Oct 2024 09:52:36 -0700 Subject: [PATCH 2/3] Add RT_API_ATTRS --- flang/include/flang/Runtime/assign.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flang/include/flang/Runtime/assign.h b/flang/include/flang/Runtime/assign.h index ddd4d46f99ce3..9b3c2e09e759e 100644 --- a/flang/include/flang/Runtime/assign.h +++ b/flang/include/flang/Runtime/assign.h @@ -43,7 +43,7 @@ enum AssignFlags { using MemmoveFct = void *(*)(void *, const void *, std::size_t); -static void *MemmoveWrapper(void *dest, const void *src, std::size_t count) { +static RT_API_ATTRS void *MemmoveWrapper(void *dest, const void *src, std::size_t count) { return Fortran::runtime::memmove(dest, src, count); } From 6b6af5690128f930b46387b715562ccbabc29acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Clement=20=28=E3=83=90=E3=83=AC=E3=83=B3?= =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=B3=20=E3=82=AF=E3=83=AC=E3=83=A1?= =?UTF-8?q?=E3=83=B3=29?= Date: Thu, 31 Oct 2024 09:58:24 -0700 Subject: [PATCH 3/3] clang-format --- flang/include/flang/Runtime/assign.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flang/include/flang/Runtime/assign.h b/flang/include/flang/Runtime/assign.h index 9b3c2e09e759e..331ec0516dd2d 100644 --- a/flang/include/flang/Runtime/assign.h +++ b/flang/include/flang/Runtime/assign.h @@ -43,7 +43,8 @@ enum AssignFlags { using MemmoveFct = void *(*)(void *, const void *, std::size_t); -static RT_API_ATTRS void *MemmoveWrapper(void *dest, const void *src, std::size_t count) { +static RT_API_ATTRS void *MemmoveWrapper( + void *dest, const void *src, std::size_t count) { return Fortran::runtime::memmove(dest, src, count); }