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

Slava Zakharin via llvm-commits llvm-commits at lists.llvm.org
Mon May 19 15:20:00 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);
----------------
vzakhari wrote:

Yes, the `Assign` runtime does the allocation of the temporary in the case of copy-in (see `CopyInAssign` runtime that uses the unallocated LHS).

I see the following options:
1. Allocate the temporary in the compiler generated code and call `ShallowCopy` directly.
2. Do the allocation in `CopyInAssign` and call `ShallowCopy` there.

I do not think 1 provides any benefits over 2.  I am also not sure if `CopyInAssign` should call `Assign` ever, if it does the top-level allocation itself.  @jeanPerier, isn't the shallow copy enough including for the cases of derived types with any components?

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


More information about the llvm-commits mailing list