[llvm] [flang-rt] Optimise ShallowCopy and elemental copies in Assign (PR #140569)

Kajetan Puchalski via llvm-commits llvm-commits at lists.llvm.org
Mon May 19 14:52:11 PDT 2025


================
@@ -114,40 +114,78 @@ RT_API_ATTRS void CheckIntegerKind(
   }
 }
 
+template <bool RANK1>
 RT_API_ATTRS void ShallowCopyDiscontiguousToDiscontiguous(
     const Descriptor &to, const Descriptor &from) {
-  SubscriptValue toAt[maxRank], fromAt[maxRank];
-  to.GetLowerBounds(toAt);
-  from.GetLowerBounds(fromAt);
+  DescriptorIterator<RANK1> toIt{to};
+  DescriptorIterator<RANK1> fromIt{from};
   std::size_t elementBytes{to.ElementBytes()};
   for (std::size_t n{to.Elements()}; n-- > 0;
-       to.IncrementSubscripts(toAt), from.IncrementSubscripts(fromAt)) {
-    std::memcpy(
-        to.Element<char>(toAt), from.Element<char>(fromAt), elementBytes);
+      toIt.Advance(), fromIt.Advance()) {
+    // Checking the size at runtime and making sure the pointer passed to memcpy
+    // has a type that matches the element size makes it possible for the
+    // compiler to optimise out the memcpy calls altogether and can
+    // substantially improve performance for some applications.
+    if (elementBytes == 16) {
+      std::memcpy(toIt.template Get<__int128_t>(),
+          fromIt.template Get<__int128_t>(), elementBytes);
+    } else if (elementBytes == 8) {
----------------
mrkajetanp wrote:

I wrote it this way to avoid potential issues with e.g. double not necessarily being 8 bytes depending on the platform. I erred on the size of it being safer to have a guaranteed 8-byte integer type pointer rather than risk accidentally using a non-8-byte double pointer. Should I just try to add explicit checks for that and make the types match what the fortran data types are?

https://github.com/llvm/llvm-project/pull/140569


More information about the llvm-commits mailing list