[llvm] [flang-rt] Optimise ShallowCopy and use it in CopyInAssign (PR #140569)
Slava Zakharin via llvm-commits
llvm-commits at lists.llvm.org
Tue May 20 13:22:27 PDT 2025
================
@@ -437,6 +437,64 @@ class Descriptor {
};
static_assert(sizeof(Descriptor) == sizeof(ISO::CFI_cdesc_t));
+// Lightweight iterator-like API to simplify specialising Descriptor indexing
+// in cases where it can improve application performance. On account of the
+// purpose of this API being performance optimisation, it is up to the user to
+// do all the necessary checks to make sure the specialised variants can be used
+// safely and that Advance() is not called more times than the number of
+// elements in the Descriptor allows for.
+// Default RANK=-1 supports aray descriptors of any rank up to maxRank.
+template <int RANK = -1> class DescriptorIterator {
+private:
+ const Descriptor &descriptor;
+ SubscriptValue subscripts[maxRank];
+ std::size_t elementOffset = 0;
+
+public:
+ RT_API_ATTRS DescriptorIterator(const Descriptor &descriptor)
+ : descriptor(descriptor) {
+ descriptor.GetLowerBounds(subscripts);
+ if constexpr (RANK == 1) {
+ elementOffset = descriptor.SubscriptByteOffset(0, subscripts[0]);
+ }
+ };
+
+ template <typename A> RT_API_ATTRS A *Get() {
+ std::size_t offset = 0;
+ // The rank-1 case doesn't require looping at all
+ if constexpr (RANK == 1) {
+ offset = elementOffset;
+ // The compiler might be able to optimise this better if we know the rank
+ // at compile time
+ } else if (RANK != -1) {
+ for (int j{0}; j < RANK; ++j) {
+ offset += descriptor.SubscriptByteOffset(j, subscripts[j]);
+ }
+ // General fallback
+ } else {
+ offset = descriptor.SubscriptsToByteOffset(subscripts);
+ }
+
+ return descriptor.OffsetElement<A>(offset);
+ }
+
+ RT_API_ATTRS void Advance() {
+ if constexpr (RANK == 1) {
+ elementOffset += descriptor.GetDimension(0).ByteStride();
+ } else if (RANK != -1) {
----------------
vzakhari wrote:
```suggestion
} else if constexpr (RANK != -1) {
```
https://github.com/llvm/llvm-project/pull/140569
More information about the llvm-commits
mailing list