Revert "[llvm-exegesis] Add in snippet address annotation (#74218)"
[llvm-project.git] / flang / runtime / misc-intrinsic.cpp
blob19eb9351d47fc7550a4202925a0ee830573bb74c
1 //===-- runtime/misc-intrinsic.cpp ----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "flang/Runtime/misc-intrinsic.h"
10 #include "terminator.h"
11 #include "flang/Runtime/descriptor.h"
12 #include <algorithm>
13 #include <cstring>
14 #include <optional>
16 namespace Fortran::runtime {
18 static void TransferImpl(Descriptor &result, const Descriptor &source,
19 const Descriptor &mold, const char *sourceFile, int line,
20 std::optional<std::int64_t> resultExtent) {
21 int rank{resultExtent.has_value() ? 1 : 0};
22 std::size_t elementBytes{mold.ElementBytes()};
23 result.Establish(mold.type(), elementBytes, nullptr, rank, nullptr,
24 CFI_attribute_allocatable, mold.Addendum() != nullptr);
25 if (resultExtent) {
26 result.GetDimension(0).SetBounds(1, *resultExtent);
28 if (const DescriptorAddendum * addendum{mold.Addendum()}) {
29 *result.Addendum() = *addendum;
31 if (int stat{result.Allocate()}) {
32 Terminator{sourceFile, line}.Crash(
33 "TRANSFER: could not allocate memory for result; STAT=%d", stat);
35 char *to{result.OffsetElement<char>()};
36 std::size_t resultBytes{result.Elements() * result.ElementBytes()};
37 const std::size_t sourceElementBytes{source.ElementBytes()};
38 std::size_t sourceElements{source.Elements()};
39 SubscriptValue sourceAt[maxRank];
40 source.GetLowerBounds(sourceAt);
41 while (resultBytes > 0 && sourceElements > 0) {
42 std::size_t toMove{std::min(resultBytes, sourceElementBytes)};
43 std::memcpy(to, source.Element<char>(sourceAt), toMove);
44 to += toMove;
45 resultBytes -= toMove;
46 --sourceElements;
47 source.IncrementSubscripts(sourceAt);
49 if (resultBytes > 0) {
50 std::memset(to, 0, resultBytes);
54 extern "C" {
56 void RTNAME(Transfer)(Descriptor &result, const Descriptor &source,
57 const Descriptor &mold, const char *sourceFile, int line) {
58 std::optional<std::int64_t> elements;
59 if (mold.rank() > 0) {
60 if (std::size_t sourceElementBytes{
61 source.Elements() * source.ElementBytes()}) {
62 if (std::size_t moldElementBytes{mold.ElementBytes()}) {
63 elements = static_cast<std::int64_t>(
64 (sourceElementBytes + moldElementBytes - 1) / moldElementBytes);
65 } else {
66 Terminator{sourceFile, line}.Crash("TRANSFER: zero-sized type of MOLD= "
67 "when SOURCE= is not zero-sized");
69 } else {
70 elements = 0;
73 return TransferImpl(
74 result, source, mold, sourceFile, line, std::move(elements));
77 void RTNAME(TransferSize)(Descriptor &result, const Descriptor &source,
78 const Descriptor &mold, const char *sourceFile, int line,
79 std::int64_t size) {
80 return TransferImpl(result, source, mold, sourceFile, line, size);
83 } // extern "C"
84 } // namespace Fortran::runtime