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

via llvm-commits llvm-commits at lists.llvm.org
Tue May 20 02:16:15 PDT 2025


================
@@ -492,11 +492,21 @@ RT_API_ATTRS void Assign(Descriptor &to, const Descriptor &from,
         terminator.Crash("unexpected type code %d in blank padded Assign()",
             to.type().raw());
       }
-    } else { // elemental copies, possibly with character truncation
-      for (std::size_t n{toElements}; n-- > 0;
-           to.IncrementSubscripts(toAt), from.IncrementSubscripts(fromAt)) {
-        memmoveFct(to.Element<char>(toAt), from.Element<const char>(fromAt),
-            toElementBytes);
+    } else {
+      // We can't simply call ShallowCopy due to edge cases such as character
+      // truncation or assignments where the RHS is a scalar.
+      if (toElementBytes == fromElementBytes && to.IsContiguous()) {
+        if (to.rank() == 1 && from.rank() == 1) {
+          ShallowCopyDiscontiguousToContiguous<true>(to, from);
----------------
jeanPerier wrote:

+1 for doing the allocation directly in CopyInAssign (calling `temp.allocate()`) and skipping Assign here.

The copy-in/out logic does not need to bother with any default value/finalization/user define function/allocatable components, and since it is compiler generated, conformity checks do not matter (the result descriptor is a copy of the source).

So yes, shallow copy is enough and a lot more efficient for non trivial data types with allocatable components.

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


More information about the llvm-commits mailing list